-
Notifications
You must be signed in to change notification settings - Fork 0
/
DOMParser.test.ts
97 lines (88 loc) · 3.1 KB
/
DOMParser.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { assert, equal } from "https://deno.land/std/testing/asserts.ts";
import { DOMParser } from "./DOMParser.ts";
Deno.test("appendChild", () => {
const parser = new DOMParser();
const document = parser.parseFromString(
"<html><body></body></html>",
"text/html",
);
const element = document.createElement("div");
document.body!.appendChild(element);
equal(element.parentElement, document.body);
equal(document.body!.childElementCount, 1);
equal(document.body!.children.item(0), element);
const element2 = document.createElement("div");
element.appendChild(element2);
equal(element2.parentElement, element);
equal(element.childElementCount, 1);
equal(element.children.item(0), element2);
document.body!.appendChild(element2);
equal(element2.parentElement, document.body);
equal(document.body!.childElementCount, 2);
equal(document.body!.children.item(0), element);
equal(document.body!.children.item(1), element2);
equal(element.childElementCount, 0);
equal(element.childNodes.length, 0);
});
Deno.test("body", () => {
const parser = new DOMParser();
const document = parser.parseFromString(
"<html><body></body></html>",
"text/html",
);
assert(document.body !== null);
equal(document.body.tagName, "body");
});
Deno.test("getElementById", () => {
const parser = new DOMParser();
const document = parser.parseFromString(
"<html><body><span id='hello'>Hello, <span id='world'>World</span></span></body></html>",
"text/html",
);
const element = document.getElementById("hello");
assert(element !== null);
equal(element.getAttribute("id"), "hello");
});
Deno.test("getElementsByClassName", () => {
const parser = new DOMParser();
const document = parser.parseFromString(
"<html><body><span class='hello'><span class='world'></span></span><div class='world'></div></body></html>",
"text/html",
);
const elements = document.getElementsByClassName("world");
assert(elements !== null);
equal(elements.length, 2);
for (let i = 0; i < elements.length; i++) {
const element = elements.item(i);
assert(element !== null);
equal(element.className, "world");
equal(element.getAttribute("class"), "world");
}
});
Deno.test("getElementsByTagName", () => {
const parser = new DOMParser();
const document = parser.parseFromString(
"<html><body><span id='hello'><span id='world'></span></span><div id='other'></div></body></html>",
"text/html",
);
const elements = document.getElementsByTagName("span");
assert(elements !== null);
equal(elements.length, 2);
for (let i = 0; i < elements.length; i++) {
const element = elements.item(i);
assert(element !== null);
equal(element.tagName, "span");
}
});
Deno.test("textContent", () => {
const parser = new DOMParser();
const document = parser.parseFromString(
"<html><body><span id='hello'>Hello, World!</span></body></html>",
"text/html",
);
const element = document.getElementById("hello");
assert(element !== null);
equal(element.textContent, "Hello, World!");
element.textContent = "World, Hello!";
equal(element.textContent, "World, Hello!");
});