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: [#1151] Skips svg elements when innerText is used #1753

Merged
merged 6 commits into from
Mar 6, 2025
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
9 changes: 6 additions & 3 deletions packages/happy-dom/src/nodes/html-element/HTMLElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,17 +671,20 @@ export default class HTMLElement extends Element {

if (
childElement[PropertySymbol.tagName] !== 'SCRIPT' &&
childElement[PropertySymbol.tagName] !== 'STYLE'
childElement[PropertySymbol.tagName] !== 'STYLE' &&
childElement[PropertySymbol.tagName] !== 'svg'
) {
const display = computedStyle.display;
if (display !== 'none') {
const textTransform = computedStyle.textTransform;
const innerText = childElement.innerText;

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

let text = childElement.innerText;
let text = innerText;

switch (textTransform) {
case 'uppercase':
Expand Down
11 changes: 10 additions & 1 deletion packages/happy-dom/test/nodes/html-element/HTMLElement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ describe('HTMLElement', () => {
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.', () => {
document.body.appendChild(element);

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>`;
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>`;
expect(element.innerText).toBe('The quick brown fox\nJumped over the lazy dog');

element.innerHTML = `<div>The <strong>quick</strong> brown fox</div><span style="display: flex">Jumped over the lazy dog</span><div>.</div>`;
Expand All @@ -299,6 +299,15 @@ describe('HTMLElement', () => {
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>`;
expect(element.innerText).toBe('The quick brown fox\nJumped Over The Lazy Dog');
});

it("It skips svg elements when innerText is used and add a newline only if there's more content coming after", () => {
document.body.appendChild(element);
// notice the lack of closing div tag
element.innerHTML = '<div><span><svg></svg></span>123<div>';
expect(element.innerText).toBe('123');
element.innerHTML = '<div><span><svg>Test</svg></span>123<div>';
expect(element.innerText).toBe('123');
});
});

describe('set innerText()', () => {
Expand Down