From 7fce956c707a092ef8c9349dd8c23d61bb00fced Mon Sep 17 00:00:00 2001 From: David Ortner Date: Tue, 22 Feb 2022 17:25:51 +0100 Subject: [PATCH] #374@patch: HTMLElement.textContent should not return the content of script and style elements. --- .../src/nodes/html-element/HTMLElement.ts | 28 +++++++++--- .../happy-dom/src/xml-parser/XMLParser.ts | 12 ++--- .../nodes/html-element/HTMLElement.test.ts | 13 +++--- .../test/xml-parser/XMLParser.test.ts | 44 ++++++++++++++++--- 4 files changed, 74 insertions(+), 23 deletions(-) diff --git a/packages/happy-dom/src/nodes/html-element/HTMLElement.ts b/packages/happy-dom/src/nodes/html-element/HTMLElement.ts index 1ed1a9a9d..a483558ee 100644 --- a/packages/happy-dom/src/nodes/html-element/HTMLElement.ts +++ b/packages/happy-dom/src/nodes/html-element/HTMLElement.ts @@ -4,6 +4,7 @@ import CSSStyleDeclaration from '../../css/CSSStyleDeclaration'; import Attr from '../../attribute/Attr'; import FocusEvent from '../../event/events/FocusEvent'; import PointerEvent from '../../event/events/PointerEvent'; +import Node from '../node/Node'; /** * HTML Element. @@ -49,21 +50,34 @@ export default class HTMLElement extends Element implements IHTMLElement { } /** - * Returns inner text. + * Returns inner text, which is the rendered appearance of text. * - * @returns Text. + * @returns Inner text. */ public get innerText(): string { - return this.textContent; + let result = ''; + for (const childNode of this.childNodes) { + if (childNode instanceof HTMLElement) { + if (childNode.tagName !== 'SCRIPT' && childNode.tagName !== 'STYLE') { + result += childNode.innerText; + } + } else if ( + childNode.nodeType === Node.ELEMENT_NODE || + childNode.nodeType === Node.TEXT_NODE + ) { + result += childNode.textContent; + } + } + return result; } /** - * Sets inner text. + * Sets the inner text, which is the rendered appearance of text. * - * @param text Text. + * @param innerText Inner text. */ - public set innerText(text: string) { - this.textContent = text; + public set innerText(innerText: string) { + this.textContent = innerText; } /** diff --git a/packages/happy-dom/src/xml-parser/XMLParser.ts b/packages/happy-dom/src/xml-parser/XMLParser.ts index 9b278bd2a..aeebf8db8 100755 --- a/packages/happy-dom/src/xml-parser/XMLParser.ts +++ b/packages/happy-dom/src/xml-parser/XMLParser.ts @@ -37,7 +37,6 @@ export default class XMLParser { let parentUnnestableTagName = null; let lastTextIndex = 0; let match: RegExpExecArray; - let childLessIndex = 0; while ((match = markupRegexp.exec(data))) { const tagName = match[2].toLowerCase(); @@ -88,15 +87,18 @@ export default class XMLParser { } else { parent.appendChild(newElement); } - lastTextIndex = childLessIndex = markupRegexp.lastIndex; + lastTextIndex = markupRegexp.lastIndex; // Tags which contain non-parsed content // For example: + + + + ` + ); + + expect((root.children[0].children[0]).innerText).toBe( + `if(11){console.log("1")}` + ); + + expect((root.children[0].children[1]).innerText).toBe(''); + expect((root.children[0].children[2]).innerText).toBe(''); + expect((root.children[0].children[3]).content.textContent).toBe( + '' + ); + + expect(root.innerHTML.replace(/[\s]/gm, '')).toBe( + `
+ + + + +
`.replace(/[\s]/gm, '') + ); + }); + it('Parses an SVG with "xmlns" set to HTML.', () => { const root = XMLParser.parse( window.document,