-
Notifications
You must be signed in to change notification settings - Fork 0
/
Document.ts
50 lines (41 loc) · 1.57 KB
/
Document.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
import { Element } from "./Element.ts";
import { HTMLCollection } from "./HTMLCollection.ts";
import { Node } from "./Node.ts";
import { findChildById } from "./utils.ts";
export class Document extends Node {
readonly #root: Element;
public get body(): Element | null {
return this.getElementsByTagName("body").item(0);
}
public get documentElement(): Element {
return this.#root;
}
public constructor(root: Element) {
super("#document", null);
this.#root = root;
}
public createElement(tagName: string): Element {
return new Element(tagName, null);
}
public getElementById(id: string): Element | null {
return findChildById(this.#root, id);
}
/**
* Returns a list of elements with the given class name.
* @param className is a string representing the class name(s) to match; multiple class names are separated by whitespace.
* @returns is a live HTMLCollection of found elements.
*/
public getElementsByClassName(className: string): HTMLCollection {
return this.#root.getElementsByClassName(className);
}
/**
* Returns a list of elements with the given tag name.
* @param name is a string representing the name of the elements. The special string "*" represents all elements.
* @returns a live HTMLCollection (but see the note below) of found elements in the order they appear in the tree.
*/
public getElementsByTagName(name: string): HTMLCollection {
return this.#root.getElementsByTagName(name);
}
// querySelector(value: string): Element | null;
// querySelectorAll(value: string): Element[];
}