Skip to content

Commit

Permalink
Merge branch 'master' into Ayc0/crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
capricorn86 authored Sep 14, 2023
2 parents 85df6fb + 4724033 commit cfe4b1a
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/happy-dom/src/config/ElementTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default {
LABEL: HTMLLabelElement,
SLOT: HTMLSlotElement,
SVG: SVGSVGElement,
G: SVGElement,
CIRCLE: SVGElement,
ELLIPSE: SVGElement,
LINE: SVGElement,
Expand Down
30 changes: 22 additions & 8 deletions packages/happy-dom/src/dom-parser/DOMParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}

/**
Expand Down
8 changes: 8 additions & 0 deletions packages/happy-dom/test/dom-parser/DOMParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -96,5 +97,12 @@ describe('DOMParser', () => {
// Spurious comment `<!--[a-z]/,end:/-->` 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, '')
);
});
});
});
5 changes: 5 additions & 0 deletions packages/happy-dom/test/dom-parser/data/DOMParserSVG.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default `
<svg width="14" height="14" viewBox="0 0 14 14" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M14 6.53333V7.46667H7.46667V14H6.53333V7.46667H0V6.53333H6.53333V0H7.46667V6.53333H14Z" fill="#0078D4"></path>
</svg>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ describe('HTMLDialogElement', () => {
expect((<Event>(<unknown>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));
Expand Down
2 changes: 0 additions & 2 deletions packages/jest-environment/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: <typeof globalThis>(<unknown>this.window),
Expand Down

0 comments on commit cfe4b1a

Please sign in to comment.