Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: issue with Node.textContent returning text in comment nodes #1461

Merged
merged 4 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
// This code is inspired by Polymer ShadyDOM Polyfill

import { getFilteredChildNodes } from '../../faux-shadow/traverse';
import { ELEMENT_NODE } from '../../env/node';
import { ELEMENT_NODE, COMMENT_NODE } from '../../env/node';

export function getTextContent(node: Node): string {
switch (node.nodeType) {
case ELEMENT_NODE: {
const childNodes = getFilteredChildNodes(node);
let content = '';
for (let i = 0, len = childNodes.length; i < len; i += 1) {
content += getTextContent(childNodes[i]);
const currentNode = childNodes[i];

if (currentNode.nodeType !== COMMENT_NODE) {
content += getTextContent(currentNode);
}
}
return content;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
insertBefore,
replaceChild,
appendChild,
COMMENT_NODE,
} from '../env/node';
import { createStaticHTMLCollection } from '../shared/static-html-collection';
import { getOuterHTML } from '../3rdparty/polymer/outer-html';
Expand Down Expand Up @@ -476,7 +477,11 @@ const NodePatchDescriptors = {
const childNodes = getInternalChildNodes(this);
let textContent = '';
for (let i = 0, len = childNodes.length; i < len; i += 1) {
textContent += getTextContent(childNodes[i]);
const currentNode = childNodes[i];

if (currentNode.nodeType !== COMMENT_NODE) {
textContent += getTextContent(currentNode);
}
}
return textContent;
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { createElement } from 'lwc';
import XTest from 'x/test';

describe('Node.textContent', () => {
it('should not return comment text when Node.nodeType is ELEMENT_NODE', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add another test that has the context in the second level, not as a root element, just to make sure that we cover that too.

const elm = document.createElement('div');
elm.innerHTML =
'<div>' +
'<!-- Some comment -->' +
'text content' +
'<!-- Some other comment -->' +
'</div>';

expect(elm.textContent).toBe('text content');
});

it('should not return comment text from 2nd level ELEMENT_NODE', () => {
const elm = document.createElement('div');

elm.innerHTML =
'<div>' +
'<!-- Some comment -->' +
'text content' +
'<div>' +
'<!-- 2nd level comment -->' +
'2nd level text' +
'</div>' +
'<!-- Some other comment -->' +
'</div>';

expect(elm.textContent).toBe('text content2nd level text');
});

it('should return comment text when Node.nodeType is COMMENT_NODE', () => {
const elm = document.createComment('Some comment');

expect(elm.textContent).toBe('Some comment');
});

it('should not return comment text from the shadowRoot', () => {
const elm = createElement('x-parent', { is: XTest });
document.body.appendChild(elm);

// since we remove the comments from the template, we need to add it manually
elm.shadowRoot.appendChild(document.createComment('Some comment'));

expect(elm.shadowRoot.textContent).toBe('text content');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<template>
<!-- This is a comment, it should not be part of the textContent -->
text content
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { LightningElement } from 'lwc';

export default class Test extends LightningElement {}