Repository: capricorn86/happy-dom (ts)

Issue:
direct write document.title does not work
**Describe the bug**
I use `document.title = 'title'`, but this does not work.

**To Reproduce**
Steps to reproduce the behavior:
1. open https://stackblitz.com/edit/node-jyhhtp?file=package.json,index.mjs
2. run `node index.mjs`

**Expected behavior**
described in the link above.

**Device:**
 - nodejs v16.14.2



Pull request description:
#785@minor: Add document.title.
close #785 

Patch:
diff --git a/packages/happy-dom/src/nodes/document/Document.ts b/packages/happy-dom/src/nodes/document/Document.ts
index 3518766f..4b09e7e3 100644
--- a/packages/happy-dom/src/nodes/document/Document.ts
+++ b/packages/happy-dom/src/nodes/document/Document.ts
@@ -227,6 +227,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.
 	 *
diff --git a/packages/happy-dom/src/nodes/document/IDocument.ts b/packages/happy-dom/src/nodes/document/IDocument.ts
index 56e16eb0..b345a341 100644
--- a/packages/happy-dom/src/nodes/document/IDocument.ts
+++ b/packages/happy-dom/src/nodes/document/IDocument.ts
@@ -45,6 +45,7 @@ export default interface IDocument extends IParentNode {
 	readonly visibilityState: VisibilityStateEnum;
 	readonly hidden: boolean;
 	cookie: string;
+	title: string;
 
 	// Events
 	onreadystatechange: (event: Event) => void;


Test patch:
diff --git a/packages/happy-dom/test/nodes/document/Document.test.ts b/packages/happy-dom/test/nodes/document/Document.test.ts
index ce9730d1..d1509e41 100644
--- a/packages/happy-dom/test/nodes/document/Document.test.ts
+++ b/packages/happy-dom/test/nodes/document/Document.test.ts
@@ -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);