Skip to content

Commit

Permalink
Merge branch 'master' into task/898-shadowroot-changed-to-shadowrootmode
Browse files Browse the repository at this point in the history
  • Loading branch information
capricorn86 committed May 11, 2023
2 parents 4d90214 + 7f5bd17 commit b23d564
Show file tree
Hide file tree
Showing 25 changed files with 863 additions and 472 deletions.
2 changes: 1 addition & 1 deletion packages/happy-dom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"webidl-conversions": "^7.0.0",
"whatwg-encoding": "^2.0.0",
"whatwg-mimetype": "^3.0.0",
"iconv-lite": "^0.6.3"
"iconv-lite": "^0.6.3"
},
"devDependencies": {
"@types/he": "^1.1.2",
Expand Down
1 change: 0 additions & 1 deletion packages/happy-dom/src/config/ChildLessElements.ts

This file was deleted.

5 changes: 4 additions & 1 deletion packages/happy-dom/src/config/PlainTextElements.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export default ['style', 'script'];
export default {
STYLE: true,
SCRIPT: true
};
36 changes: 18 additions & 18 deletions packages/happy-dom/src/config/UnnestableElements.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
export default [
'a',
'button',
'dd',
'dt',
'form',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'li',
'option',
'p',
'select',
'table'
];
export default {
A: true,
BUTTON: true,
DD: true,
DT: true,
FORM: true,
H1: true,
H2: true,
H3: true,
H4: true,
H5: true,
H6: true,
LI: true,
OPTION: true,
P: true,
SELECT: true,
TABLE: true
};
33 changes: 16 additions & 17 deletions packages/happy-dom/src/config/VoidElements.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
export default [
'area',
'base',
'br',
'col',
'embed',
'hr',
'img',
'input',
'link',
'meta',
'param',
'source',
'track',
'wbr',
'meta'
];
export default {
AREA: true,
BASE: true,
BR: true,
COL: true,
EMBED: true,
HR: true,
IMG: true,
INPUT: true,
LINK: true,
META: true,
PARAM: true,
SOURCE: true,
TRACK: true,
WBR: true
};
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ export default class CSSStyleDeclarationElementStyle {
}
} else {
for (const element of options.elements) {
// Skip @-rules.
if (selectorText.startsWith('@')) {
continue;
}
const matchResult = QuerySelector.match(<IElement>element.element, selectorText);
if (matchResult) {
element.cssTexts.push({
Expand Down
4 changes: 2 additions & 2 deletions packages/happy-dom/src/dom-parser/DOMParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default class DOMParser {
newDocument.childNodes.length = 0;
newDocument.children.length = 0;

const root = XMLParser.parse(newDocument, string, true);
const root = XMLParser.parse(newDocument, string, { evaluateScripts: true });
let documentElement = null;
let documentTypeNode = null;

Expand All @@ -65,7 +65,7 @@ export default class DOMParser {
newDocument.appendChild(documentTypeNode);
}
newDocument.appendChild(documentElement);
const body = newDocument.querySelector('body');
const body = newDocument.body;
if (body) {
for (const child of root.childNodes.slice()) {
body.appendChild(child);
Expand Down
44 changes: 26 additions & 18 deletions packages/happy-dom/src/nodes/document/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ export default class Document extends Node implements IDocument {
* @returns Title.
*/
public get title(): string {
const el = this.querySelector('title');
if (el) {
return el.textContent;
const element = ParentNodeUtility.getElementByTagName(this, 'title');
if (element) {
return element.textContent;
}
return '';
}
Expand All @@ -248,16 +248,23 @@ export default class Document extends Node implements IDocument {
*
*/
public set title(title: string) {
const el = this.querySelector('title');
if (el) {
el.textContent = title;
const element = ParentNodeUtility.getElementByTagName(this, 'title');
if (element) {
element.textContent = title;
} else {
const titleEl = this.createElement('title');
titleEl.textContent = title;
this.head.appendChild(titleEl);
const newElement = this.createElement('title');
newElement.textContent = title;
this.head.appendChild(newElement);
}
}

/**
* Returns a collection of all area elements and a elements in a document with a value for the href attribute.
*/
public get links(): IHTMLCollection<IHTMLElement> {
return <IHTMLCollection<IHTMLElement>>this.querySelectorAll('a[href],area[href]');
}

/**
* Last element child.
*
Expand Down Expand Up @@ -430,9 +437,9 @@ export default class Document extends Node implements IDocument {
* @returns Base URI.
*/
public get baseURI(): string {
const base = <IHTMLBaseElement>this.querySelector('base');
if (base) {
return base.href;
const element = <IHTMLBaseElement | null>ParentNodeUtility.getElementByTagName(this, 'base');
if (element) {
return element.href;
}
return this.defaultView.location.href;
}
Expand Down Expand Up @@ -653,7 +660,7 @@ export default class Document extends Node implements IDocument {
* @param html HTML.
*/
public write(html: string): void {
const root = XMLParser.parse(this, html, true);
const root = XMLParser.parse(this, html, { evaluateScripts: true });

if (this._isFirstWrite || this._isFirstWriteAfterOpen) {
if (this._isFirstWrite) {
Expand Down Expand Up @@ -688,16 +695,16 @@ export default class Document extends Node implements IDocument {

this.appendChild(documentElement);
} else {
const rootBody = root.querySelector('body');
const body = this.querySelector('body');
const rootBody = ParentNodeUtility.getElementByTagName(root, 'body');
const body = ParentNodeUtility.getElementByTagName(this, 'body');
if (rootBody && body) {
for (const child of rootBody.childNodes.slice()) {
body.appendChild(child);
}
}
}

const body = this.querySelector('body');
const body = ParentNodeUtility.getElementByTagName(this, 'body');
if (body) {
for (const child of root.childNodes.slice()) {
if (child['tagName'] !== 'HTML' && child.nodeType !== Node.DOCUMENT_TYPE_NODE) {
Expand All @@ -720,9 +727,10 @@ export default class Document extends Node implements IDocument {
this.appendChild(documentElement);
}
} else {
const bodyNode = root.querySelector('body');
const bodyNode = ParentNodeUtility.getElementByTagName(root, 'body');
const body = ParentNodeUtility.getElementByTagName(this, 'body');
for (const child of (bodyNode || root).childNodes.slice()) {
this.body.appendChild(child);
body.appendChild(child);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/happy-dom/src/nodes/document/IDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default interface IDocument extends IParentNode {
readonly documentURI: string;
readonly visibilityState: VisibilityStateEnum;
readonly hidden: boolean;
readonly links: IHTMLCollection<IHTMLElement>;
cookie: string;
title: string;

Expand Down
4 changes: 1 addition & 3 deletions packages/happy-dom/src/nodes/element/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,7 @@ export default class Element extends Node implements IElement {
this.removeChild(child);
}

for (const node of XMLParser.parse(this.ownerDocument, html).childNodes.slice()) {
this.appendChild(node);
}
XMLParser.parse(this.ownerDocument, html, { rootNode: this });
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import HTMLElement from '../html-element/HTMLElement';
import IDocumentFragment from '../document-fragment/IDocumentFragment';
import INode from '../node/INode';
import IHTMLTemplateElement from './IHTMLTemplateElement';
import XMLParser from '../../xml-parser/XMLParser';
import XMLSerializer from '../../xml-serializer/XMLSerializer';
import XMLParser from '../../xml-parser/XMLParser';

/**
* HTML Template Element.
Expand All @@ -29,9 +29,7 @@ export default class HTMLTemplateElement extends HTMLElement implements IHTMLTem
this.content.removeChild(child);
}

for (const node of XMLParser.parse(this.ownerDocument, html).childNodes.slice()) {
this.content.appendChild(node);
}
XMLParser.parse(this.ownerDocument, html, { rootNode: this.content });
}

/**
Expand Down
24 changes: 13 additions & 11 deletions packages/happy-dom/src/nodes/parent-node/ParentNodeUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@ export default class ParentNodeUtility {
* @param parentNode Parent node.
* @param nodes List of Node or DOMString.
*/
public static append(parentNode: INode, ...nodes: (INode | string)[]): void {
public static append(
parentNode: IElement | IDocument | IDocumentFragment,
...nodes: (INode | string)[]
): void {
for (const node of nodes) {
if (typeof node === 'string') {
const newChildNodes = XMLParser.parse(
<IDocument>parentNode.ownerDocument,
node
).childNodes.slice();
for (const newChildNode of newChildNodes) {
parentNode.appendChild(newChildNode);
}
XMLParser.parse(<IDocument>parentNode.ownerDocument, node, { rootNode: parentNode });
} else {
parentNode.appendChild(node);
}
Expand All @@ -38,9 +35,11 @@ export default class ParentNodeUtility {
* @param parentNode Parent node.
* @param nodes List of Node or DOMString.
*/
public static prepend(parentNode: INode, ...nodes: (string | INode)[]): void {
public static prepend(
parentNode: IElement | IDocument | IDocumentFragment,
...nodes: (string | INode)[]
): void {
const firstChild = parentNode.firstChild;

for (const node of nodes) {
if (typeof node === 'string') {
const newChildNodes = XMLParser.parse(
Expand All @@ -62,7 +61,10 @@ export default class ParentNodeUtility {
* @param parentNode Parent node.
* @param nodes List of Node or DOMString.
*/
public static replaceChildren(parentNode: INode, ...nodes: (string | INode)[]): void {
public static replaceChildren(
parentNode: IElement | IDocument | IDocumentFragment,
...nodes: (string | INode)[]
): void {
for (const node of parentNode.childNodes.slice()) {
parentNode.removeChild(node);
}
Expand Down
4 changes: 1 addition & 3 deletions packages/happy-dom/src/nodes/shadow-root/ShadowRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ export default class ShadowRoot extends DocumentFragment implements IShadowRoot
this.removeChild(child);
}

for (const node of XMLParser.parse(this.ownerDocument, html).childNodes.slice()) {
this.appendChild(node);
}
XMLParser.parse(this.ownerDocument, html, { rootNode: this });
}

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/happy-dom/src/query-selector/ISelectorPseudo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import SelectorItem from './SelectorItem';

export default interface ISelectorPseudo {
name: string;
arguments: string | null;
selectorItem: SelectorItem | null;
nthFunction: ((n: number) => boolean) | null;
}

0 comments on commit b23d564

Please sign in to comment.