Skip to content

Commit

Permalink
capricorn86#785@minor: Add document.title.
Browse files Browse the repository at this point in the history
  • Loading branch information
btea committed Feb 24, 2023
1 parent e1fb3ee commit 5e19bc3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/happy-dom/src/nodes/document/Document.ts
Expand Up @@ -226,6 +226,34 @@ export default class Document extends Node implements IDocument {
return charset ? charset : 'UTF-8';
}

/**
* Returns title.
*
* @returns Title.
*/
public get title(): string {
const el = this.querySelector('title');
if (el) {
return el.textContent;
}
return '';
}

/**
* Returns set title.
*
*/
public set title(title: string) {
const el = this.querySelector('title');
if (el) {
el.textContent = title;
} else {
const titleEl = this.createElement('title');
titleEl.textContent = title;
this.head.appendChild(titleEl);
}
}

/**
* Last element child.
*
Expand Down
1 change: 1 addition & 0 deletions packages/happy-dom/src/nodes/document/IDocument.ts
Expand Up @@ -42,6 +42,7 @@ export default interface IDocument extends IParentNode {
readonly URL: string;
readonly documentURI: string;
cookie: string;
title: string;

// Events
onreadystatechange: (event: Event) => void;
Expand Down
14 changes: 14 additions & 0 deletions packages/happy-dom/test/nodes/document/Document.test.ts
Expand Up @@ -228,6 +228,20 @@ describe('Document', () => {
});
});

describe('get title() and set title()', () => {
it('Returns and sets title.', () => {
document.title = 'test title';
expect(document.title).toBe('test title');
const title = document.head.querySelector('title');
expect(title.textContent).toBe('test title');
document.title = 'new title';
expect(document.title).toBe('new title');
expect(title.textContent).toBe('new title');
title.textContent = 'new title 2';
expect(document.title).toBe('new title 2');
});
});

describe('get body()', () => {
it('Returns <body> element.', () => {
expect(document.body === document.children[0].children[1]).toBe(true);
Expand Down

0 comments on commit 5e19bc3

Please sign in to comment.