Skip to content

Commit 60e5fdd

Browse files
maribethbgmajoulet
authored andcommitted
🐛Remove text nodes from amp-story on build (ampproject#22918)
* Remove text nodes from amp-story and add tests * Make prettier happy * Clean up debug statements. * Use childNodes helper to find text nodes instead of Array.from
1 parent 85f8e8c commit 60e5fdd

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

extensions/amp-story/1.0/amp-story.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import {
8686
childElement,
8787
childElementByTag,
8888
childElements,
89+
childNodes,
8990
closest,
9091
createElementWithAttributes,
9192
isRTL,
@@ -442,6 +443,15 @@ export class AmpStory extends AMP.BaseElement {
442443
// hover. (See 17654)
443444
this.element.removeAttribute('title');
444445

446+
// Remove text nodes which would be shown outside of the amp-story
447+
const textNodes = childNodes(
448+
this.element,
449+
node => node.nodeType === Node.TEXT_NODE
450+
);
451+
textNodes.forEach(node => {
452+
this.element.removeChild(node);
453+
});
454+
445455
if (isExperimentOn(this.win, 'amp-story-branching')) {
446456
this.registerAction('goToPage', invocation => {
447457
const {args} = invocation;

extensions/amp-story/1.0/test/test-amp-story.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,17 @@ describes.realWin(
183183
});
184184
});
185185

186+
it('should remove text child nodes when built', () => {
187+
createPages(story.element, 1, ['cover']);
188+
const textToRemove = 'this should be removed';
189+
const textNode = win.document.createTextNode(textToRemove);
190+
story.element.appendChild(textNode);
191+
story.buildCallback();
192+
return story.layoutCallback().then(() => {
193+
expect(story.element.innerText).to.not.have.string(textToRemove);
194+
});
195+
});
196+
186197
it('should preload the bookend if navigating to the last page', () => {
187198
createPages(story.element, 1, ['cover']);
188199

0 commit comments

Comments
 (0)