Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: issue with Node.textContent returning text in comment nodes (#1461)
* fix: issue with Node.textContent returning text in comment nodes * fix: also patch shadowRoot.textContent * wip: use innerHTML instead of dom api * wip: minor perf optimization
- Loading branch information
Showing
5 changed files
with
68 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
packages/integration-karma/test/polyfills/text-content/index.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', () => { | ||
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'); | ||
}); | ||
}); |
4 changes: 4 additions & 0 deletions
4
packages/integration-karma/test/polyfills/text-content/x/test/test.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
3 changes: 3 additions & 0 deletions
3
packages/integration-karma/test/polyfills/text-content/x/test/test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { LightningElement } from 'lwc'; | ||
|
||
export default class Test extends LightningElement {} |