1 |
|
2 |
|
3 | import utils from "./utils";
|
4 |
|
5 | import type {CssStyle, HtmlDomNode} from "./domTree";
|
6 | import type {MathDomNode} from "./mathMLTree";
|
7 |
|
8 |
|
9 |
|
10 | export interface VirtualNode {
|
11 | toNode(): Node;
|
12 | toMarkup(): string;
|
13 | }
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | export class DocumentFragment<ChildType: VirtualNode>
|
22 | implements HtmlDomNode, MathDomNode {
|
23 | children: ChildType[];
|
24 |
|
25 | classes: string[];
|
26 | height: number;
|
27 | depth: number;
|
28 | maxFontSize: number;
|
29 | style: CssStyle;
|
30 |
|
31 | constructor(children: ChildType[]) {
|
32 | this.children = children;
|
33 | this.classes = [];
|
34 | this.height = 0;
|
35 | this.depth = 0;
|
36 | this.maxFontSize = 0;
|
37 | this.style = {};
|
38 | }
|
39 |
|
40 | hasClass(className: string): boolean {
|
41 | return utils.contains(this.classes, className);
|
42 | }
|
43 |
|
44 |
|
45 | toNode(): Node {
|
46 | const frag = document.createDocumentFragment();
|
47 |
|
48 | for (let i = 0; i < this.children.length; i++) {
|
49 | frag.appendChild(this.children[i].toNode());
|
50 | }
|
51 |
|
52 | return frag;
|
53 | }
|
54 |
|
55 |
|
56 | toMarkup(): string {
|
57 | let markup = "";
|
58 |
|
59 |
|
60 | for (let i = 0; i < this.children.length; i++) {
|
61 | markup += this.children[i].toMarkup();
|
62 | }
|
63 |
|
64 | return markup;
|
65 | }
|
66 |
|
67 | |
68 |
|
69 |
|
70 |
|
71 | toText(): string {
|
72 |
|
73 |
|
74 |
|
75 | const toText = (child: ChildType): string => child.toText();
|
76 | return this.children.map(toText).join("");
|
77 | }
|
78 | }
|