Skip to content

Commit

Permalink
Merge branch 'master' into element-style-setter
Browse files Browse the repository at this point in the history
  • Loading branch information
capricorn86 committed Oct 24, 2022
2 parents 3e7ed6b + b02d1c7 commit e273e7b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 10 deletions.
6 changes: 4 additions & 2 deletions packages/happy-dom/src/nodes/parent-node/ParentNodeUtility.ts
Expand Up @@ -97,9 +97,10 @@ export default class ParentNodeUtility {
): IHTMLCollection<IElement> {
const upperTagName = tagName.toUpperCase();
const matches = HTMLCollectionFactory.create();
const includeAll = tagName === '*';

for (const child of parentNode.children) {
if (child.tagName === upperTagName) {
if (includeAll || child.tagName === upperTagName) {
matches.push(child);
}
for (const match of this.getElementsByTagName(<IElement>child, tagName)) {
Expand All @@ -125,9 +126,10 @@ export default class ParentNodeUtility {
): IHTMLCollection<IElement> {
const upperTagName = tagName.toUpperCase();
const matches = HTMLCollectionFactory.create();
const includeAll = tagName === '*';

for (const child of parentNode.children) {
if (child.tagName === upperTagName && child.namespaceURI === namespaceURI) {
if ((includeAll || child.tagName === upperTagName) && child.namespaceURI === namespaceURI) {
matches.push(child);
}
for (const match of this.getElementsByTagNameNS(<IElement>child, namespaceURI, tagName)) {
Expand Down
22 changes: 15 additions & 7 deletions packages/happy-dom/src/window/Window.ts
Expand Up @@ -252,20 +252,20 @@ export default class Window extends EventTarget implements IWindow {

// Public Properties
public readonly document: Document;
public readonly customElements: CustomElementRegistry = new CustomElementRegistry();
public readonly location = new Location();
public readonly history = new History();
public readonly navigator = new Navigator();
public readonly customElements: CustomElementRegistry;
public readonly location: Location;
public readonly history: History;
public readonly navigator: Navigator;
public readonly console = console;
public readonly self = this;
public readonly top = this;
public readonly parent = this;
public readonly window = this;
public readonly globalThis = this;
public readonly screen = new Screen();
public readonly screen: Screen;
public readonly devicePixelRatio = 1;
public readonly sessionStorage = new Storage();
public readonly localStorage = new Storage();
public readonly sessionStorage: Storage;
public readonly localStorage: Storage;
public readonly performance = PerfHooks.performance;
public readonly innerWidth: number;
public readonly innerHeight: number;
Expand Down Expand Up @@ -350,6 +350,14 @@ export default class Window extends EventTarget implements IWindow {
constructor(options?: { innerWidth?: number; innerHeight?: number; url?: string }) {
super();

this.customElements = new CustomElementRegistry();
this.location = new Location();
this.navigator = new Navigator();
this.history = new History();
this.screen = new Screen();
this.sessionStorage = new Storage();
this.localStorage = new Storage();

this.innerWidth = options?.innerWidth ? options.innerWidth : 0;
this.innerHeight = options?.innerHeight ? options.innerHeight : 0;

Expand Down
Expand Up @@ -155,6 +155,27 @@ describe('ParentNodeUtility', () => {
div4
]);
});

it('Returns all elements when tag name is *.', () => {
const parent = document.createElement('div');
const div1 = document.createElement('div');
const div2 = document.createElement('div');
const div3 = document.createElement('div');
const div4 = document.createElement('div');
const span1 = document.createElement('span');
const span2 = document.createElement('span');
const span3 = document.createElement('span');

parent.appendChild(div1);
div1.appendChild(div2);
div2.appendChild(span1);
span1.appendChild(div3);
div3.appendChild(span2);
div3.appendChild(span3);
span3.appendChild(div4);

expect(ParentNodeUtility.getElementsByTagName(parent, '*').length).toEqual(7);
});
});

describe('getElementsByTagNameNS()', () => {
Expand All @@ -164,7 +185,7 @@ describe('ParentNodeUtility', () => {
const div2 = document.createElement('div');
const div3 = document.createElementNS(NamespaceURI.svg, 'div');
const div4 = document.createElement('div');
const span1 = document.createElement('span');
const span1 = document.createElementNS(NamespaceURI.svg, 'span');
const span2 = document.createElement('span');
const span3 = document.createElement('span');

Expand All @@ -181,6 +202,29 @@ describe('ParentNodeUtility', () => {
div3
]);
});

it('Returns all elements when tag name is *.', () => {
const parent = document.createElement('div');
const div1 = document.createElementNS(NamespaceURI.svg, 'div');
const div2 = document.createElement('div');
const div3 = document.createElementNS(NamespaceURI.svg, 'div');
const div4 = document.createElement('div');
const span1 = document.createElementNS(NamespaceURI.svg, 'span');
const span2 = document.createElement('span');
const span3 = document.createElement('span');

parent.appendChild(div1);
div1.appendChild(div2);
div2.appendChild(span1);
span1.appendChild(div3);
div3.appendChild(span2);
div3.appendChild(span3);
span3.appendChild(div4);

expect(
ParentNodeUtility.getElementsByTagNameNS(parent, NamespaceURI.svg, '*').length
).toEqual(3);
});
});

describe('getElementByTagName()', () => {
Expand Down
12 changes: 12 additions & 0 deletions packages/happy-dom/test/window/Window.test.ts
Expand Up @@ -79,6 +79,18 @@ describe('Window', () => {
expect(secondComment.ownerDocument === secondWindow.document).toBe(true);
expect(thirdComment.ownerDocument === thirdWindow.document).toBe(true);
});

it('Initializes by using given options', () => {
const windowWithOptions = new Window({
innerWidth: 1024,
innerHeight: 768,
url: 'http://localhost:8080'
});

expect(windowWithOptions.innerWidth).toBe(1024);
expect(windowWithOptions.innerHeight).toBe(768);
expect(windowWithOptions.location.href).toBe('http://localhost:8080/');
});
});

describe('get Object()', () => {
Expand Down

0 comments on commit e273e7b

Please sign in to comment.