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: [#1494] Escape content in getInnerHTML #1498

Closed
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
5 changes: 3 additions & 2 deletions packages/happy-dom/src/nodes/element/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ export default class Element
* @returns HTML.
*/
public get outerHTML(): string {
return new XMLSerializer({ escapeEntities: false }).serializeToString(this);
return new XMLSerializer({ escapeEntities: true }).serializeToString(this);
}

/**
Expand Down Expand Up @@ -448,14 +448,15 @@ export default class Element
*
* @see https://web.dev/declarative-shadow-dom/
* @see https://chromestatus.com/feature/5191745052606464
* @see https://html.spec.whatwg.org/#serialising-html-fragments
* @param [options] Options.
* @param [options.includeShadowRoots] Set to "true" to include shadow roots.
* @returns HTML.
*/
public getInnerHTML(options?: { includeShadowRoots?: boolean }): string {
const xmlSerializer = new XMLSerializer({
includeShadowRoots: options && options.includeShadowRoots,
escapeEntities: false
escapeEntities: true
});
let xml = '';
for (const node of this[PropertySymbol.childNodes]) {
Expand Down
2 changes: 1 addition & 1 deletion packages/happy-dom/test/nodes/document/Document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ describe('Document', () => {
const html = `<html test="1"><body>Test></body></html>`;
document.write(html);
expect(document.documentElement.outerHTML).toBe(
'<html test="1"><head></head><body>Test></body></html>'
'<html test="1"><head></head><body>Test&gt;</body></html>'
);
});
});
Expand Down
38 changes: 23 additions & 15 deletions packages/happy-dom/test/nodes/element/Element.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,21 +201,6 @@ describe('Element', () => {
});
});

describe('get innerHTML()', () => {
it('Returns HTML of children as a concatenated string.', () => {
const div = document.createElement('div');

element.appendChild(div);

vi.spyOn(XMLSerializer.prototype, 'serializeToString').mockImplementation((rootElement) => {
expect(rootElement).toBe(div);
return 'EXPECTED_HTML';
});

expect(element.innerHTML).toBe('EXPECTED_HTML');
});
});

describe('set innerHTML()', () => {
it('Creates child nodes from provided HTML.', () => {
const div = document.createElement('div');
Expand Down Expand Up @@ -247,6 +232,29 @@ describe('Element', () => {

expect(element.outerHTML).toBe('<div><div></div>text1</div>');
});

it('Returns HTML of children as a concatenated string.', () => {
const div = document.createElement('div');

element.appendChild(div);

vi.spyOn(XMLSerializer.prototype, 'serializeToString').mockImplementation((rootElement) => {
expect(rootElement).toBe(div);
return 'EXPECTED_HTML';
});

expect(element.innerHTML).toBe('EXPECTED_HTML');
});

it('Returns encoded HTML string with setter innerText.', () => {
const div = document.createElement('div');

div.innerText = '<b>&a</b>';

element.appendChild(div);

expect(element.innerHTML).toBe('<div>&lt;b&gt;&amp;a&lt;/b&gt;</div>');
});
});

describe('get outerHTML()', () => {
Expand Down
Loading