Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
timreichen committed Aug 12, 2024
1 parent d50e746 commit d548412
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/dom/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ export class Document extends Node {
get title(): string {
return this.querySelector("title")?.textContent || "";
}
set title(value) {
let titleElement = this.querySelector("title");
if (!titleElement) {
titleElement = this.createElement("title");
this.head.appendChild(titleElement);
}
titleElement.textContent = value;
}

get cookie(): string {
return ""; // TODO
Expand Down
24 changes: 24 additions & 0 deletions test/units/Document.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { DOMParser } from "../../deno-dom-wasm.ts";
import { assertStrictEquals as assertEquals } from "assert";

Deno.test("Document.title sets title element value", () => {
const doc = new DOMParser().parseFromString(
`<!DOCTYPE html><html><head><title>foo</title></head><body></body></html>`,
"text/html",
);
assertEquals(doc.title, "foo");
doc.title = "bar";
assertEquals(doc.title, "bar");
assertEquals(doc.querySelector("title")?.textContent, "bar");
});

Deno.test("Document.title adds missing title element", () => {
const doc = new DOMParser().parseFromString(
`<!DOCTYPE html><html><head></head><body></body></html>`,
"text/html",
);
assertEquals(doc.title, "");
doc.title = "foo";
assertEquals(doc.title, "foo");
assertEquals(doc.querySelector("title")?.textContent, "foo");
});

0 comments on commit d548412

Please sign in to comment.