Skip to content

Commit

Permalink
feat: [#1377] Adds support for constructing Text and Comment using th…
Browse files Browse the repository at this point in the history
…e new operator
  • Loading branch information
capricorn86 committed Apr 6, 2024
1 parent 64e4a37 commit 6343665
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
16 changes: 12 additions & 4 deletions packages/happy-dom/src/window/BrowserWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import XMLDocumentImplementation from '../nodes/xml-document/XMLDocument.js';
import SVGDocumentImplementation from '../nodes/svg-document/SVGDocument.js';
import Node from '../nodes/node/Node.js';
import NodeFilter from '../tree-walker/NodeFilter.js';
import Text from '../nodes/text/Text.js';
import Comment from '../nodes/comment/Comment.js';
import ShadowRoot from '../nodes/shadow-root/ShadowRoot.js';
import HTMLTemplateElement from '../nodes/html-template-element/HTMLTemplateElement.js';
import HTMLFormElementImplementation from '../nodes/html-form-element/HTMLFormElement.js';
Expand Down Expand Up @@ -142,6 +140,8 @@ import IRequestInfo from '../fetch/types/IRequestInfo.js';
import BrowserErrorCaptureEnum from '../browser/enums/BrowserErrorCaptureEnum.js';
import AudioImplementation from '../nodes/html-audio-element/Audio.js';
import ImageImplementation from '../nodes/html-image-element/Image.js';
import TextImplementation from '../nodes/text/Text.js';
import CommentImplementation from '../nodes/comment/Comment.js';
import DocumentFragmentImplementation from '../nodes/document-fragment/DocumentFragment.js';
import DOMParserImplementation from '../dom-parser/DOMParser.js';
import FileReaderImplementation from '../file/FileReader.js';
Expand Down Expand Up @@ -176,8 +176,6 @@ export default class BrowserWindow extends EventTarget implements INodeJSGlobal
public readonly SVGSVGElement: typeof SVGSVGElement = SVGSVGElement;
public readonly SVGElement: typeof SVGElement = SVGElement;
public readonly SVGGraphicsElement: typeof SVGGraphicsElement = SVGGraphicsElement;
public readonly Text: typeof Text = Text;
public readonly Comment: typeof Comment = Comment;
public readonly ShadowRoot: typeof ShadowRoot = ShadowRoot;
public readonly ProcessingInstruction: typeof ProcessingInstruction = ProcessingInstruction;
public readonly Element: typeof Element = Element;
Expand All @@ -187,6 +185,8 @@ export default class BrowserWindow extends EventTarget implements INodeJSGlobal
public readonly HTMLDocument: new () => HTMLDocumentImplementation;
public readonly XMLDocument: new () => XMLDocumentImplementation;
public readonly SVGDocument: new () => SVGDocumentImplementation;
public readonly Text: typeof TextImplementation;
public readonly Comment: typeof CommentImplementation;

// Element classes
public readonly HTMLAnchorElement: typeof HTMLAnchorElement = HTMLAnchorElement;
Expand Down Expand Up @@ -616,12 +616,16 @@ export default class BrowserWindow extends EventTarget implements INodeJSGlobal
class Audio extends AudioImplementation {}
class Image extends ImageImplementation {}
class DocumentFragment extends DocumentFragmentImplementation {}
class Text extends TextImplementation {}
class Comment extends CommentImplementation {}

/* eslint-enable jsdoc/require-jsdoc */

this.Response = Response;
this.Request = Request;
this.Image = Image;
this.Text = Text;
this.Comment = Comment;
this.DocumentFragment = DocumentFragment;
this.FileReader = FileReader;
this.DOMParser = DOMParser;
Expand Down Expand Up @@ -651,6 +655,8 @@ export default class BrowserWindow extends EventTarget implements INodeJSGlobal
this.Audio[PropertySymbol.ownerDocument] = this.document;
this.Image[PropertySymbol.ownerDocument] = this.document;
this.DocumentFragment[PropertySymbol.ownerDocument] = this.document;
this.Text[PropertySymbol.ownerDocument] = this.document;
this.Comment[PropertySymbol.ownerDocument] = this.document;

// Ready state manager
this[PropertySymbol.readyStateManager].waitUntilComplete().then(() => {
Expand Down Expand Up @@ -1296,6 +1302,8 @@ export default class BrowserWindow extends EventTarget implements INodeJSGlobal
this.Audio[PropertySymbol.ownerDocument] = null;
this.Image[PropertySymbol.ownerDocument] = null;
this.DocumentFragment[PropertySymbol.ownerDocument] = null;
this.Text[PropertySymbol.ownerDocument] = null;
this.Comment[PropertySymbol.ownerDocument] = null;

const mutationObservers = this[PropertySymbol.mutationObservers];

Expand Down
9 changes: 9 additions & 0 deletions packages/happy-dom/test/nodes/comment/Comment.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Window from '../../../src/window/Window.js';
import Document from '../../../src/nodes/document/Document.js';
import Comment from '../../../src/nodes/comment/Comment.js';
import { beforeEach, describe, it, expect } from 'vitest';

describe('Comment', () => {
Expand All @@ -11,6 +12,14 @@ describe('Comment', () => {
document = window.document;
});

describe('constructor()', () => {
it('Creates a new Comment node.', () => {
const node = new window.Comment('test');
expect(node).toBeInstanceOf(Comment);
expect(node.data).toBe('test');
});
});

describe('get nodeName()', () => {
it('Returns "#comment".', () => {
const node = document.createComment('test');
Expand Down
8 changes: 8 additions & 0 deletions packages/happy-dom/test/nodes/text/Text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ describe('Text', () => {
document = window.document;
});

describe('constructor()', () => {
it('Creates a new Text node.', () => {
const node = new window.Text('test');
expect(node).toBeInstanceOf(Text);
expect(node.data).toBe('test');
});
});

describe('get nodeName()', () => {
it('Returns "#text".', () => {
const node = document.createTextNode('test');
Expand Down

0 comments on commit 6343665

Please sign in to comment.