Skip to content

Commit

Permalink
chore: [#1508] Fixes problem where the release build failed
Browse files Browse the repository at this point in the history
  • Loading branch information
capricorn86 committed Aug 21, 2024
1 parent 6f99ffd commit d79bfc8
Show file tree
Hide file tree
Showing 18 changed files with 181 additions and 150 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import DocumentType from '../nodes/document-type/DocumentType.js';
import * as PropertySymbol from '../PropertySymbol.js';
import Document from '../nodes/document/Document.js';
import NodeFactory from '../nodes/NodeFactory.js';

/**
* The DOMImplementation interface represents an object providing methods which are not dependent on any particular document. Such an object is returned by the.
Expand Down Expand Up @@ -45,12 +46,15 @@ export default class DOMImplementation {
publicId: string,
systemId: string
): DocumentType {
const documentType = new this.#document[PropertySymbol.ownerWindow].DocumentType(
this.#document
const documentType = NodeFactory.createNode(
this.#document,
this.#document[PropertySymbol.ownerWindow].DocumentType
);

documentType[PropertySymbol.name] = qualifiedName;
documentType[PropertySymbol.publicId] = publicId;
documentType[PropertySymbol.systemId] = systemId;

return documentType;
}
}
38 changes: 38 additions & 0 deletions packages/happy-dom/src/nodes/NodeFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Document from '../nodes/document/Document.js';
import Node from './node/Node.js';
import * as PropertySymbol from '../PropertySymbol.js';

/**
* Node factory used for setting the owner document to nodes.
*/
export default class NodeFactory {
public static ownerDocuments: Document[] = [];

/**
* Creates a node instance with the given owner document.
*
* @param ownerDocument Owner document.
* @param nodeClass Node class.
* @param [args] Node arguments.
* @returns Node instance.
*/
public static createNode<T extends Node>(
ownerDocument: Document,
nodeClass: new (...args) => T,
...args: any[]
): T {
if (!nodeClass[PropertySymbol.ownerDocument]) {
this.ownerDocuments.push(ownerDocument);
}
return new nodeClass(...args);
}

/**
* Pulls an owner document from the queue.
*
* @returns Document.
*/
public static pullOwnerDocument(): Document {
return this.ownerDocuments.pop();
}
}
11 changes: 11 additions & 0 deletions packages/happy-dom/src/nodes/character-data/CharacterData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ export default abstract class CharacterData
public [PropertySymbol.data] = '';
public declare cloneNode: (deep?: boolean) => CharacterData;

/**
* Constructor.
*
* @param [data] Data.
*/
constructor(data?: string) {
super();

this[PropertySymbol.data] = data !== undefined ? String(data) : '';
}

/**
* Returns text content.
*
Expand Down
13 changes: 0 additions & 13 deletions packages/happy-dom/src/nodes/comment/Comment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import CharacterData from '../character-data/CharacterData.js';
import * as PropertySymbol from '../../PropertySymbol.js';
import NodeTypeEnum from '../node/NodeTypeEnum.js';
import Document from '../document/Document.js';

/**
* Comment node.
Expand All @@ -10,18 +9,6 @@ export default class Comment extends CharacterData {
public [PropertySymbol.nodeType] = NodeTypeEnum.commentNode;
public declare cloneNode: (deep?: boolean) => Comment;

/**
* Constructor.
*
* @param [ownerDocument] Owner document.
* @param [data] Data.
*/
constructor(ownerDocument?: Document, data?: string) {
super(ownerDocument);

this[PropertySymbol.data] = data !== undefined ? String(data) : '';
}

/**
* Node name.
*
Expand Down
48 changes: 32 additions & 16 deletions packages/happy-dom/src/nodes/document/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ import HTMLHeadElement from '../html-head-element/HTMLHeadElement.js';
import HTMLBaseElement from '../html-base-element/HTMLBaseElement.js';
import ICachedResult from '../node/ICachedResult.js';
import HTMLTitleElement from '../html-title-element/HTMLTitleElement.js';
import NodeFactory from '../NodeFactory.js';

const PROCESSING_INSTRUCTION_TARGET_REGEXP = /^[a-z][a-z0-9-]+$/;

/**
* Document.
*/
export default class Document extends Node {
// Static properties
public static [PropertySymbol.ownerDocument]: Document = <Document>{};

// Internal properties
public [PropertySymbol.children]: HTMLCollection<Element> | null = null;
public [PropertySymbol.activeElement]: HTMLElement | SVGElement = null;
Expand Down Expand Up @@ -201,7 +205,7 @@ export default class Document extends Node {
* @param injected.window Window.
*/
constructor(injected: { browserFrame: IBrowserFrame; window: BrowserWindow }) {
super(<Document>{});
super();
this.#browserFrame = injected.browserFrame;
this[PropertySymbol.ownerWindow] = injected.window;
this[PropertySymbol.ownerDocument] = null;
Expand Down Expand Up @@ -1074,15 +1078,19 @@ export default class Document extends Node {

// SVG element
if (namespaceURI === NamespaceURI.svg) {
const element =
const elementClass =
qualifiedName === 'svg'
? new this[PropertySymbol.ownerWindow].SVGSVGElement(this)
: new this[PropertySymbol.ownerWindow].SVGElement(this);
? this[PropertySymbol.ownerWindow].SVGSVGElement
: this[PropertySymbol.ownerWindow].SVGElement;

const element = NodeFactory.createNode<SVGElement>(this, elementClass);

element[PropertySymbol.tagName] = qualifiedName;
element[PropertySymbol.localName] = qualifiedName;
element[PropertySymbol.namespaceURI] = namespaceURI;
element[PropertySymbol.isValue] = options && options.is ? String(options.is) : null;
return <HTMLElement>(<unknown>element);

return element;
}

// Custom HTML element
Expand All @@ -1107,7 +1115,7 @@ export default class Document extends Node {

// Known HTML element
if (elementClass) {
const element = new elementClass(this);
const element = NodeFactory.createNode<Element>(this, elementClass);

element[PropertySymbol.tagName] = qualifiedName.toUpperCase();
element[PropertySymbol.localName] = localName;
Expand All @@ -1118,16 +1126,18 @@ export default class Document extends Node {
}

// Unknown HTML element
const element = localName.includes('-')
? new this[PropertySymbol.ownerWindow].HTMLElement(this)
: new this[PropertySymbol.ownerWindow].HTMLUnknownElement(this);
const unknownElementClass = localName.includes('-')
? this[PropertySymbol.ownerWindow].HTMLElement
: this[PropertySymbol.ownerWindow].HTMLUnknownElement;

const element = NodeFactory.createNode<Element>(this, unknownElementClass);

element[PropertySymbol.tagName] = qualifiedName.toUpperCase();
element[PropertySymbol.localName] = localName;
element[PropertySymbol.namespaceURI] = namespaceURI;
element[PropertySymbol.isValue] = options && options.is ? String(options.is) : null;

return <HTMLElement>element;
return element;
}

/* eslint-enable jsdoc/valid-types */
Expand Down Expand Up @@ -1225,13 +1235,15 @@ export default class Document extends Node {
* @returns Element.
*/
public createAttributeNS(namespaceURI: string, qualifiedName: string): Attr {
const attribute = new this[PropertySymbol.ownerWindow].Attr(this);
const attribute = NodeFactory.createNode<Attr>(this, this[PropertySymbol.ownerWindow].Attr);

const parts = qualifiedName.split(':');
attribute[PropertySymbol.namespaceURI] = namespaceURI;
attribute[PropertySymbol.name] = qualifiedName;
attribute[PropertySymbol.localName] = parts[1] ?? qualifiedName;
attribute[PropertySymbol.prefix] = parts[0] ?? null;
return <Attr>attribute;

return attribute;
}

/**
Expand Down Expand Up @@ -1325,12 +1337,16 @@ export default class Document extends Node {
`Failed to execute 'createProcessingInstruction' on 'Document': The data provided ('?>') contains '?>'`
);
}
const processingInstruction = new this[PropertySymbol.ownerWindow].ProcessingInstruction(this);

processingInstruction[PropertySymbol.data] = data;
processingInstruction[PropertySymbol.target] = target;
const element = NodeFactory.createNode<ProcessingInstruction>(
this,
this[PropertySymbol.ownerWindow].ProcessingInstruction
);

element[PropertySymbol.data] = data;
element[PropertySymbol.target] = target;

return processingInstruction;
return element;
}

/**
Expand Down
10 changes: 6 additions & 4 deletions packages/happy-dom/src/nodes/element/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import NamespaceURI from '../../config/NamespaceURI.js';
import NodeList from '../node/NodeList.js';
import CSSStyleDeclaration from '../../css/declaration/CSSStyleDeclaration.js';
import NamedNodeMapProxyFactory from './NamedNodeMapProxyFactory.js';
import NodeFactory from '../NodeFactory.js';

type InsertAdjacentPosition = 'beforebegin' | 'afterbegin' | 'beforeend' | 'afterend';

Expand Down Expand Up @@ -848,9 +849,10 @@ export default class Element
);
}

const shadowRoot = new this[PropertySymbol.ownerDocument][
PropertySymbol.ownerWindow
].ShadowRoot(this[PropertySymbol.ownerDocument]);
const shadowRoot = NodeFactory.createNode<ShadowRoot>(
this[PropertySymbol.ownerDocument],
this[PropertySymbol.ownerDocument][PropertySymbol.ownerWindow].ShadowRoot
);

this[PropertySymbol.shadowRoot] = shadowRoot;

Expand All @@ -862,7 +864,7 @@ export default class Element
shadowRoot[PropertySymbol.slotAssignment] =
init.slotAssignment === 'manual' ? 'manual' : 'named';

(<ShadowRoot>shadowRoot)[PropertySymbol.connectedToNode]();
shadowRoot[PropertySymbol.connectedToNode]();

return this[PropertySymbol.shadowRoot];
}
Expand Down
6 changes: 2 additions & 4 deletions packages/happy-dom/src/nodes/html-audio-element/Audio.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Document from '../document/Document.js';
import HTMLAudioElement from './HTMLAudioElement.js';

/**
Expand All @@ -11,11 +10,10 @@ export default class Audio extends HTMLAudioElement {
/**
* Constructor.
*
* @param [ownerDocument] Owner document.
* @param [url] source URL.
*/
constructor(ownerDocument?: Document, url: string = null) {
super(ownerDocument);
constructor(url: string = null) {
super();

if (url !== null) {
this.src = url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import Element from '../element/Element.js';
import EventTarget from '../../event/EventTarget.js';
import Node from '../node/Node.js';
import ClassMethodBinder from '../../ClassMethodBinder.js';
import Document from '../document/Document.js';

/**
* HTML Form Element.
Expand All @@ -44,14 +43,12 @@ export default class HTMLFormElement extends HTMLElement {
/**
* Constructor.
*
* @param injected Injected properties.
* @param injected.browserFrame Browser frame.
* @param injected.ownerDocument Owner document.
* @param browserFrame Browser frame.
*/
constructor(injected: { browserFrame: IBrowserFrame; ownerDocument: Document }) {
super(injected.ownerDocument);
constructor(browserFrame) {
super();

this.#browserFrame = injected.browserFrame;
this.#browserFrame = browserFrame;

ClassMethodBinder.bindMethods(
this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,12 @@ export default class HTMLIFrameElement extends HTMLElement {
/**
* Constructor.
*
* @param injected Injected properties.
* @param injected.browserFrame Browser frame.
* @param injected.ownerDocument Owner document.
* @param browserFrame Browser frame.
*/
constructor(injected: { browserFrame: IBrowserFrame; ownerDocument: Document }) {
super(injected.ownerDocument);
constructor(browserFrame) {
super();

this.#browserFrame = injected.browserFrame;
this.#browserFrame = browserFrame;
}

/**
Expand Down
6 changes: 2 additions & 4 deletions packages/happy-dom/src/nodes/html-image-element/Image.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Document from '../document/Document.js';
import HTMLImageElement from './HTMLImageElement.js';

/**
Expand All @@ -11,12 +10,11 @@ export default class Image extends HTMLImageElement {
/**
* Constructor.
*
* @param [ownerDocument] Owner document.
* @param [width] Width.
* @param [height] Height.
*/
constructor(ownerDocument?: Document, width: number = null, height: number = null) {
super(ownerDocument);
constructor(width: number = null, height: number = null) {
super();

if (width !== null) {
this.width = width;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import DOMException from '../../exception/DOMException.js';
import DOMExceptionNameEnum from '../../exception/DOMExceptionNameEnum.js';
import ResourceFetch from '../../fetch/ResourceFetch.js';
import DocumentReadyStateManager from '../document/DocumentReadyStateManager.js';
import Document from '../document/Document.js';

/**
* HTML Link Element.
Expand All @@ -34,14 +33,12 @@ export default class HTMLLinkElement extends HTMLElement {
/**
* Constructor.
*
* @param injected Injected properties.
* @param injected.browserFrame Browser frame.
* @param injected.ownerDocument Owner document.
* @param browserFrame Browser frame.
*/
constructor(injected: { browserFrame: IBrowserFrame; ownerDocument: Document }) {
super(injected.ownerDocument);
constructor(browserFrame) {
super();

this.#browserFrame = injected.browserFrame;
this.#browserFrame = browserFrame;
}

/**
Expand Down
Loading

0 comments on commit d79bfc8

Please sign in to comment.