-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathXMLNode.ts
More file actions
54 lines (52 loc) · 1.66 KB
/
XMLNode.ts
File metadata and controls
54 lines (52 loc) · 1.66 KB
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
import type { TagValueProcessor } from './traversable/defaultOptions.ts';
export type XMLNodeValue = string | Uint8Array | number | boolean;
export type XMLAttributeValue = string | number | boolean;
export class XMLNode {
public tagName: string;
public parent?: XMLNode;
public children: Record<string, XMLNode[]>;
public attributes?: Record<string, XMLAttributeValue>;
public bytes: Uint8Array;
public startIndex: number;
private tagValueProcessor: TagValueProcessor;
private cachedValue?: XMLNodeValue;
public constructor(
tagName: string,
parent: XMLNode | undefined,
bytes: Uint8Array,
tagValueProcessor: TagValueProcessor,
) {
this.tagName = tagName;
this.parent = parent;
this.children = Object.create(null); //child tags
this.attributes = Object.create(null); //attributes map
this.bytes = bytes; //text only
this.tagValueProcessor = tagValueProcessor;
this.startIndex = -1;
}
public append(toAppend: Uint8Array): void {
if (this.bytes.length === 0) {
this.bytes = toAppend;
return;
}
const arrayConcat = new Uint8Array(this.bytes.length + toAppend.length);
arrayConcat.set(this.bytes);
arrayConcat.set(toAppend, this.bytes.length);
this.bytes = arrayConcat;
}
public get value(): any {
if (this.cachedValue === undefined) {
const value = this.tagValueProcessor(this.bytes, this);
this.cachedValue = value;
}
return this.cachedValue;
}
public addChild(child: XMLNode) {
const existing = this.children[child.tagName];
if (Array.isArray(existing)) {
existing.push(child);
} else {
this.children[child.tagName] = [child];
}
}
}