diff --git a/packages/global-registrator/src/GlobalRegistrator.ts b/packages/global-registrator/src/GlobalRegistrator.ts index 6cc615da5..c0854cf54 100644 --- a/packages/global-registrator/src/GlobalRegistrator.ts +++ b/packages/global-registrator/src/GlobalRegistrator.ts @@ -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)) { @@ -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) { diff --git a/packages/global-registrator/test/react/React.test.tsx b/packages/global-registrator/test/react/React.test.tsx index 0d371d53c..3130031c0 100644 --- a/packages/global-registrator/test/react/React.test.tsx +++ b/packages/global-registrator/test/react/React.test.tsx @@ -26,3 +26,5 @@ function unmountReactComponent(): void { mountReactComponent(); unmountReactComponent(); + +GlobalRegistrator.unregister(); diff --git a/packages/happy-dom/src/nodes/html-template-element/HTMLTemplateElement.ts b/packages/happy-dom/src/nodes/html-template-element/HTMLTemplateElement.ts index 7b2ecf47c..8911a01da 100644 --- a/packages/happy-dom/src/nodes/html-template-element/HTMLTemplateElement.ts +++ b/packages/happy-dom/src/nodes/html-template-element/HTMLTemplateElement.ts @@ -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()) { @@ -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( @@ -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; @@ -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 = super.cloneNode(deep); diff --git a/packages/happy-dom/src/xml-parser/XMLParser.ts b/packages/happy-dom/src/xml-parser/XMLParser.ts index 9adea57a5..1268f3e91 100755 --- a/packages/happy-dom/src/xml-parser/XMLParser.ts +++ b/packages/happy-dom/src/xml-parser/XMLParser.ts @@ -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; diff --git a/packages/happy-dom/test/nodes/html-template-element/HTMLTemplateElement.test.ts b/packages/happy-dom/test/nodes/html-template-element/HTMLTemplateElement.test.ts new file mode 100644 index 000000000..22d3879c3 --- /dev/null +++ b/packages/happy-dom/test/nodes/html-template-element/HTMLTemplateElement.test.ts @@ -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 = document.createElement('template'); + }); + + afterEach(() => { + jest.restoreAllMocks(); + }); + + it('InnerHTML', () => { + const div = '
happy-dom is cool!
'; + 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); + }); +}); diff --git a/packages/happy-dom/test/xml-parser/XMLParser.test.ts b/packages/happy-dom/test/xml-parser/XMLParser.test.ts index bf94943af..1909ffef9 100644 --- a/packages/happy-dom/test/xml-parser/XMLParser.test.ts +++ b/packages/happy-dom/test/xml-parser/XMLParser.test.ts @@ -192,6 +192,21 @@ describe('XMLParser', () => { `.replace(/[\s]/gm, '') ); + + const root2 = XMLParser.parse( + window.document, + ` + + Title + + + + +` + ); + expect((root2.children[0].children[1].children[0]).innerText).toBe( + 'var vars = []; for (var i=0;i {