-
Notifications
You must be signed in to change notification settings - Fork 392
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
Conversation
abf4c14
to
be3df86
Compare
|
||
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) { |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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', () => { |
There was a problem hiding this comment.
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')); |
There was a problem hiding this comment.
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]); |
There was a problem hiding this comment.
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
There was a problem hiding this 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!
merged because perf and compare was failing because of other reasons. |
Details
From https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent:
This pr fixes the current implementation to match the standard.
Does this PR introduce breaking changes?
No, it does not introduce breaking changes.