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

Conversation

jodarove
Copy link
Contributor

Details

From https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent:

textContent returns the concatenation of the textContent of every child node, excluding comments and processing instructions. This is an empty string if the node has no children

This pr fixes the current implementation to match the standard.

Does this PR introduce breaking changes?

  • No, it does not introduce breaking changes.

@jodarove jodarove force-pushed the jodarove/textContent-on-comment-nodes branch from abf4c14 to be3df86 Compare August 21, 2019 20:55

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]);
if (childNodes[i].nodeType !== COMMENT_NODE) {
Copy link
Member

Choose a reason for hiding this comment

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

Should we be more conservative here and implement a whitelist for Node.TEXT_NODE?

Copy link
Contributor

Choose a reason for hiding this comment

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

it is not about text node only... elements and eveyething that can have a text inside is valid here.

Copy link
Contributor

Choose a reason for hiding this comment

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

another optimization here is to cache the childNodes[i] in a const, instead of accessing it twice in the same block.

@@ -0,0 +1,16 @@
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.


elm.appendChild(secondLevelElement);

elm.appendChild(document.createComment('Some other comment'));
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I think that using innerHTML to construct the DOM would make this test easier to understand.

@@ -476,7 +477,9 @@ const NodePatchDescriptors = {
const childNodes = getInternalChildNodes(this);
let textContent = '';
for (let i = 0, len = childNodes.length; i < len; i += 1) {
textContent += getTextContent(childNodes[i]);
if (childNodes[i].nodeType !== COMMENT_NODE) {
textContent += getTextContent(childNodes[i]);
Copy link
Contributor

Choose a reason for hiding this comment

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

same... cache childNodes[i] ref

Copy link
Contributor

@caridy caridy left a comment

Choose a reason for hiding this comment

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

nothing else from my side!

@jodarove jodarove merged commit 3ad12e2 into master Aug 21, 2019
@jodarove jodarove deleted the jodarove/textContent-on-comment-nodes branch August 21, 2019 23:37
@jodarove
Copy link
Contributor Author

merged because perf and compare was failing because of other reasons.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants