Skip to content

Commit a5424a8

Browse files
fix: [#1151] Skips SVG elements when in HTMLElement.innerText (#1753)
* fix: [#1151] Skips svg elements when innerText is used * fix: [#1151] Add newline if it's a block/flex element and there's more content coming after * chore: [#1151] Improves performance and removes incorrect comment --------- Co-authored-by: David Ortner <david@ortner.se>
1 parent 1417bad commit a5424a8

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

packages/happy-dom/src/nodes/html-element/HTMLElement.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -671,17 +671,20 @@ export default class HTMLElement extends Element {
671671

672672
if (
673673
childElement[PropertySymbol.tagName] !== 'SCRIPT' &&
674-
childElement[PropertySymbol.tagName] !== 'STYLE'
674+
childElement[PropertySymbol.tagName] !== 'STYLE' &&
675+
childElement[PropertySymbol.tagName] !== 'svg'
675676
) {
676677
const display = computedStyle.display;
677678
if (display !== 'none') {
678679
const textTransform = computedStyle.textTransform;
680+
const innerText = childElement.innerText;
679681

680-
if ((display === 'block' || display === 'flex') && result) {
682+
// Only add newline if it's a block/flex element and there's more content coming after
683+
if ((display === 'block' || display === 'flex') && result && innerText) {
681684
result += '\n';
682685
}
683686

684-
let text = childElement.innerText;
687+
let text = innerText;
685688

686689
switch (textTransform) {
687690
case 'uppercase':

packages/happy-dom/test/nodes/html-element/HTMLElement.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ describe('HTMLElement', () => {
280280
it('Returns rendered text with line breaks between block and flex elements and without hidden elements being rendered if element is connected to the document.', () => {
281281
document.body.appendChild(element);
282282

283-
element.innerHTML = `<div>The <strong>quick</strong> brown fox</div><script>var key = "value";</script><style>button { background: red; }</style><div>Jumped over the lazy dog</div>`;
283+
element.innerHTML = `<div>The <strong>quick</strong> brown fox</div><script>var key = "value";</script><style>button { background: red; }</style><div><svg></svg>Jumped over the lazy dog</div>`;
284284
expect(element.innerText).toBe('The quick brown fox\nJumped over the lazy dog');
285285

286286
element.innerHTML = `<div>The <strong>quick</strong> brown fox</div><span style="display: flex">Jumped over the lazy dog</span><div>.</div>`;
@@ -299,6 +299,15 @@ describe('HTMLElement', () => {
299299
element.innerHTML = `<div>The <strong>quick</strong> brown fox</div><span>jumped over the lazy dog</span><style>span { text-transform: capitalize; display: block; }</style>`;
300300
expect(element.innerText).toBe('The quick brown fox\nJumped Over The Lazy Dog');
301301
});
302+
303+
it("It skips svg elements when innerText is used and add a newline only if there's more content coming after", () => {
304+
document.body.appendChild(element);
305+
// notice the lack of closing div tag
306+
element.innerHTML = '<div><span><svg></svg></span>123<div>';
307+
expect(element.innerText).toBe('123');
308+
element.innerHTML = '<div><span><svg>Test</svg></span>123<div>';
309+
expect(element.innerText).toBe('123');
310+
});
302311
});
303312

304313
describe('set innerText()', () => {

0 commit comments

Comments
 (0)