Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/capricorn86/happy-dom int…
Browse files Browse the repository at this point in the history
…o task/450-typeerror-targetownerdocumentcreaterange-is-not-a-function
  • Loading branch information
capricorn86 committed Jun 28, 2022
2 parents 944769d + 7667c0a commit 48d5ff2
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 47 deletions.
6 changes: 3 additions & 3 deletions packages/global-registrator/src/GlobalRegistrator.ts
Expand Up @@ -11,7 +11,7 @@ export default class GlobalRegistrator {
*/
public static register(): void {
if (this.registered.length) {
throw new Error('Failed to registered. Happy DOM has already been globally registered.');
throw new Error('Failed to register. Happy DOM has already been globally registered.');
}
const window = new GlobalWindow();
for (const key of Object.keys(window)) {
Expand All @@ -26,9 +26,9 @@ export default class GlobalRegistrator {
* Registers Happy DOM globally.
*/
public static unregister(): void {
if (this.registered.length) {
if (!this.registered.length) {
throw new Error(
'Failed to unregistered. Happy DOM has not previously been globally registered.'
'Failed to unregister. Happy DOM has not previously been globally registered.'
);
}
for (const key of this.registered) {
Expand Down
2 changes: 2 additions & 0 deletions packages/global-registrator/test/react/React.test.tsx
Expand Up @@ -26,3 +26,5 @@ function unmountReactComponent(): void {

mountReactComponent();
unmountReactComponent();

GlobalRegistrator.unregister();
Expand Up @@ -24,9 +24,7 @@ export default class HTMLTemplateElement extends HTMLElement implements IHTMLTem
}

/**
* Sets inner HTML.
*
* @param html HTML.
* @override
*/
public set innerHTML(html: string) {
for (const child of this.content.childNodes.slice()) {
Expand All @@ -39,18 +37,14 @@ export default class HTMLTemplateElement extends HTMLElement implements IHTMLTem
}

/**
* Returns outer HTML.
*
* @returns HTML.
* @override
*/
public get outerHTML(): string {
return new XMLSerializer().serializeToString(this);
return new XMLSerializer().serializeToString(this.content);
}

/**
* Returns outer HTML.
*
* @param html HTML.
* @override
*/
public set outerHTML(_html: string) {
throw new DOMException(
Expand All @@ -59,36 +53,28 @@ export default class HTMLTemplateElement extends HTMLElement implements IHTMLTem
}

/**
* Previous sibling.
*
* @returns Node.
* @override
*/
public get previousSibling(): INode {
return this.content.previousSibling;
}

/**
* Next sibling.
*
* @returns Node.
* @override
*/
public get nextSibling(): INode {
return this.content.nextSibling;
}

/**
* First child.
*
* @returns Node.
* @override
*/
public get firstChild(): INode {
return this.content.firstChild;
}

/**
* Last child.
*
* @returns Node.
* @override
*/
public get lastChild(): INode {
return this.content.lastChild;
Expand All @@ -107,52 +93,35 @@ export default class HTMLTemplateElement extends HTMLElement implements IHTMLTem
}

/**
* Append a child node to childNodes.
*
* @param node Node to append.
* @returns Appended node.
* @override
*/
public appendChild(node: INode): INode {
return this.content.appendChild(node);
}

/**
* Remove Child element from childNodes array.
*
* @param node Node to remove.
* @override
*/
public removeChild(node: Node): INode {
return this.content.removeChild(node);
}

/**
* Inserts a node before another.
*
* @param newNode Node to insert.
* @param referenceNode Node to insert before.
* @returns Inserted node.
* @override
*/
public insertBefore(newNode: INode, referenceNode: INode): INode {
return this.content.insertBefore(newNode, referenceNode);
}

/**
* Replaces a node with another.
*
* @param newChild New child.
* @param oldChild Old child.
* @returns Replaced node.
* @override
*/
public replaceChild(newChild: INode, oldChild: INode): INode {
return this.content.replaceChild(newChild, oldChild);
}

/**
* Clones a node.
*
* @override
* @param [deep=false] "true" to clone deep.
* @returns Cloned node.
*/
public cloneNode(deep = false): IHTMLTemplateElement {
const clone = <IHTMLTemplateElement>super.cloneNode(deep);
Expand Down
2 changes: 1 addition & 1 deletion packages/happy-dom/src/xml-parser/XMLParser.ts
Expand Up @@ -12,7 +12,7 @@ import IElement from '../nodes/element/IElement';
import HTMLLinkElement from '../nodes/html-link-element/HTMLLinkElement';
import IDocumentFragment from '../nodes/document-fragment/IDocumentFragment';

const MARKUP_REGEXP = /<(\/?)([a-z][-.0-9_a-z]*)\s*([^>]*?)(\/?)>/gi;
const MARKUP_REGEXP = /<(\/?)([a-z][-.0-9_a-z]*)\s*([^<>]*?)(\/?)>/gi;
const COMMENT_REGEXP = /<!--(.*?)-->|<([!?])([^>]*)>/gi;
const DOCUMENT_TYPE_ATTRIBUTE_REGEXP = /"([^"]+)"/gm;
const ATTRIBUTE_REGEXP = /([^\s=]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|(\S+)))/gms;
Expand Down
@@ -0,0 +1,29 @@
import Window from '../../../src/window/Window';
import Document from '../../../src/nodes/document/Document';
import HTMLTemplateElement from '../../../src/nodes/html-template-element/HTMLTemplateElement';

describe('HTMLTemplateElement', () => {
let window: Window;
let document: Document;
let element: HTMLTemplateElement;

beforeEach(() => {
window = new Window();
document = window.document;
element = <HTMLTemplateElement>document.createElement('template');
});

afterEach(() => {
jest.restoreAllMocks();
});

it('InnerHTML', () => {
const div = '<div>happy-dom is cool!</div>';
expect(element.content.childNodes.length).toBe(0);
element.innerHTML = div;
expect(element.innerHTML).toBe(div);
expect(element.content.childNodes.length).toBe(1);
element.innerHTML = '';
expect(element.content.childNodes.length).toBe(0);
});
});
15 changes: 15 additions & 0 deletions packages/happy-dom/test/xml-parser/XMLParser.test.ts
Expand Up @@ -192,6 +192,21 @@ describe('XMLParser', () => {
<template></template>
</div>`.replace(/[\s]/gm, '')
);

const root2 = XMLParser.parse(
window.document,
`<html>
<head>
<title>Title</title>
</head>
<body>
<script type="text/javascript">var vars = []; for (var i=0;i<vars.length;i++) {}</script>
</body>
</html>`
);
expect((<IHTMLElement>root2.children[0].children[1].children[0]).innerText).toBe(
'var vars = []; for (var i=0;i<vars.length;i++) {}'
);
});

it('Handles unclosed regular elements.', () => {
Expand Down

0 comments on commit 48d5ff2

Please sign in to comment.