From 42a6323a1ad4ba483bb9cbd54cbda36956c02817 Mon Sep 17 00:00:00 2001 From: Otto Szika Date: Tue, 29 Aug 2023 14:31:39 +0300 Subject: [PATCH 1/4] #1034@minor: Add support for g SVG element. --- packages/happy-dom/src/config/ElementTag.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/happy-dom/src/config/ElementTag.ts b/packages/happy-dom/src/config/ElementTag.ts index 070c9261e..d82fc5f84 100644 --- a/packages/happy-dom/src/config/ElementTag.ts +++ b/packages/happy-dom/src/config/ElementTag.ts @@ -48,6 +48,7 @@ export default { LABEL: HTMLLabelElement, SLOT: HTMLSlotElement, SVG: SVGSVGElement, + G: SVGElement, CIRCLE: SVGElement, ELLIPSE: SVGElement, LINE: SVGElement, From 08052ae7b5598234a320b8f06473af0ed792f949 Mon Sep 17 00:00:00 2001 From: Tim Wright Date: Wed, 6 Sep 2023 13:53:39 +1200 Subject: [PATCH 2/4] #1040@patch: Don't add HTML tags when parsing SVGs. --- .../happy-dom/src/dom-parser/DOMParser.ts | 30 ++++++++++++++----- .../test/dom-parser/DOMParser.test.ts | 8 +++++ .../test/dom-parser/data/DOMParserSVG.ts | 5 ++++ 3 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 packages/happy-dom/test/dom-parser/data/DOMParserSVG.ts diff --git a/packages/happy-dom/src/dom-parser/DOMParser.ts b/packages/happy-dom/src/dom-parser/DOMParser.ts index b748f4f8a..a85c1d407 100644 --- a/packages/happy-dom/src/dom-parser/DOMParser.ts +++ b/packages/happy-dom/src/dom-parser/DOMParser.ts @@ -74,16 +74,30 @@ export default class DOMParser { } } } else { - const documentElement = newDocument.createElement('html'); - const bodyElement = newDocument.createElement('body'); - const headElement = newDocument.createElement('head'); + switch (mimeType) { + case 'image/svg+xml': + { + for (const node of root._childNodes.slice()) { + newDocument.appendChild(node); + } + } + break; + case 'text/html': + default: + { + const documentElement = newDocument.createElement('html'); + const bodyElement = newDocument.createElement('body'); + const headElement = newDocument.createElement('head'); - documentElement.appendChild(headElement); - documentElement.appendChild(bodyElement); - newDocument.appendChild(documentElement); + documentElement.appendChild(headElement); + documentElement.appendChild(bodyElement); + newDocument.appendChild(documentElement); - for (const node of root._childNodes.slice()) { - bodyElement.appendChild(node); + for (const node of root._childNodes.slice()) { + bodyElement.appendChild(node); + } + } + break; } } diff --git a/packages/happy-dom/test/dom-parser/DOMParser.test.ts b/packages/happy-dom/test/dom-parser/DOMParser.test.ts index c9e757d13..2bd2d0213 100644 --- a/packages/happy-dom/test/dom-parser/DOMParser.test.ts +++ b/packages/happy-dom/test/dom-parser/DOMParser.test.ts @@ -3,6 +3,7 @@ import XMLSerializer from '../../src/xml-serializer/XMLSerializer.js'; import DOMParser from '../../src/dom-parser/DOMParser.js'; import DOMParserHTML from './data/DOMParserHTML.js'; import { beforeEach, describe, it, expect } from 'vitest'; +import DOMParserSVG from './data/DOMParserSVG'; describe('DOMParser', () => { let domParser: DOMParser; @@ -96,5 +97,12 @@ describe('DOMParser', () => { // Spurious comment `` should be solved expect(newDocument.body.textContent).toBe('here is some html elástica '); }); + + it('parses SVGs', () => { + const newDocument = domParser.parseFromString(DOMParserSVG, 'image/svg+xml'); + expect(new XMLSerializer().serializeToString(newDocument).replace(/[\s]/gm, '')).toBe( + DOMParserSVG.replace(/[\s]/gm, '') + ); + }); }); }); diff --git a/packages/happy-dom/test/dom-parser/data/DOMParserSVG.ts b/packages/happy-dom/test/dom-parser/data/DOMParserSVG.ts new file mode 100644 index 000000000..a0214e680 --- /dev/null +++ b/packages/happy-dom/test/dom-parser/data/DOMParserSVG.ts @@ -0,0 +1,5 @@ +export default ` + + + +`; From 5565f4848b927e96b171846eaaab296b7315e382 Mon Sep 17 00:00:00 2001 From: David Ortner Date: Wed, 13 Sep 2023 14:12:44 +0200 Subject: [PATCH 3/4] #1062@patch: Fixes issue related to Jest module mocker in "@happy-dom/jest-environment". --- packages/jest-environment/src/index.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index 29d285120..b2e7aa4b3 100644 --- a/packages/jest-environment/src/index.ts +++ b/packages/jest-environment/src/index.ts @@ -81,8 +81,6 @@ export default class HappyDOMEnvironment implements JestEnvironment { }; this.window.addEventListener('error', this.errorEventListener); - this.moduleMocker = new ModuleMocker(global); - this.fakeTimers = new LegacyFakeTimers({ config: projectConfig, global: (this.window), From d418605becc1ca1f55837ca6502e4dfc6f341861 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Schleusner?= Date: Wed, 13 Sep 2023 15:46:40 +0200 Subject: [PATCH 4/4] #1064@patch: Emit close event only when dialog was open. --- .../src/nodes/html-dialog-element/HTMLDialogElement.ts | 5 ++++- .../nodes/html-dialog-element/HTMLDialogElement.test.ts | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/happy-dom/src/nodes/html-dialog-element/HTMLDialogElement.ts b/packages/happy-dom/src/nodes/html-dialog-element/HTMLDialogElement.ts index 6620712c9..f5a3ab9ed 100644 --- a/packages/happy-dom/src/nodes/html-dialog-element/HTMLDialogElement.ts +++ b/packages/happy-dom/src/nodes/html-dialog-element/HTMLDialogElement.ts @@ -30,9 +30,12 @@ export default class HTMLDialogElement extends HTMLElement implements IHTMLDialo * @param [returnValue] ReturnValue. */ public close(returnValue = ''): void { + const wasOpen = this.open; this.removeAttribute('open'); this.returnValue = returnValue; - this.dispatchEvent(new Event('close')); + if (wasOpen) { + this.dispatchEvent(new Event('close')); + } } /** diff --git a/packages/happy-dom/test/nodes/html-dialog-element/HTMLDialogElement.test.ts b/packages/happy-dom/test/nodes/html-dialog-element/HTMLDialogElement.test.ts index 9f4628c53..053cf1a71 100644 --- a/packages/happy-dom/test/nodes/html-dialog-element/HTMLDialogElement.test.ts +++ b/packages/happy-dom/test/nodes/html-dialog-element/HTMLDialogElement.test.ts @@ -92,6 +92,13 @@ describe('HTMLDialogElement', () => { expect(((dispatched)).bubbles).toBe(false); }); + it('Should only dispatch a close event when dialog wasnt already closed', () => { + let dispatched: Event | null = null; + element.addEventListener('close', (event: Event) => (dispatched = event)); + element.close(); + expect(dispatched).toBe(null); + }); + it('Should dispatch a close event when closing a modal', () => { let dispatched: Event | null = null; element.addEventListener('close', (event: Event) => (dispatched = event));