|
| 1 | +export interface NodeConstructorParams { |
| 2 | + name: string; |
| 3 | + attributes?: { [key: string]: string }; |
| 4 | + value?: string; |
| 5 | +} |
| 6 | + |
| 7 | +export abstract class BaseNode { |
| 8 | + abstract name: string; |
| 9 | + |
| 10 | + abstract clone(): BaseNode; |
| 11 | + abstract toString(): string; |
| 12 | +} |
| 13 | + |
| 14 | +export interface ChildrenHolder { |
| 15 | + children: BaseNode[]; |
| 16 | + |
| 17 | + addChild(child: BaseNode): void; |
| 18 | +} |
| 19 | + |
| 20 | +export interface AttributeHolder { |
| 21 | + attributes: { [key: string]: string }; |
| 22 | + |
| 23 | + setAttribute(key: string, value: string): void; |
| 24 | +} |
| 25 | + |
| 26 | +export class Node extends BaseNode implements ChildrenHolder, AttributeHolder { |
| 27 | + name: string; |
| 28 | + attributes: { [key: string]: string }; |
| 29 | + children: BaseNode[] = []; |
| 30 | + // For simple parameterized values, like [x=y]...[/x] |
| 31 | + value?: string; |
| 32 | + |
| 33 | + constructor(params: NodeConstructorParams) { |
| 34 | + super(); |
| 35 | + this.name = params.name; |
| 36 | + this.attributes = params.attributes || {}; |
| 37 | + } |
| 38 | + |
| 39 | + clone(): Node { |
| 40 | + const node = new Node({name: this.name, attributes: this.attributes, value: this.value}); |
| 41 | + node.children = this.children.map(child => child.clone()); |
| 42 | + return node; |
| 43 | + } |
| 44 | + |
| 45 | + addChild(child: Node): void { |
| 46 | + this.children.push(child.clone()); |
| 47 | + } |
| 48 | + |
| 49 | + setValue(value: string): void { |
| 50 | + this.value = value; |
| 51 | + } |
| 52 | + |
| 53 | + setAttribute(key: string, value: string): void { |
| 54 | + this.attributes[key] = value; |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + toString(): string { |
| 59 | + let nodeString = `[${this.name}`; |
| 60 | + if (this.value){ |
| 61 | + nodeString += `=${this.value}`; |
| 62 | + } |
| 63 | + Object.entries(this.attributes).forEach(([key, value]) => { |
| 64 | + nodeString += ` ${key}="${value}"`; |
| 65 | + }); |
| 66 | + nodeString += ']'; |
| 67 | + this.children.forEach(child => { |
| 68 | + nodeString += child.toString(); |
| 69 | + }); |
| 70 | + nodeString += `[/${this.name}]`; |
| 71 | + return nodeString; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +export class TextNode extends BaseNode { |
| 76 | + text: string; |
| 77 | + name: string = 'TextNode'; |
| 78 | + |
| 79 | + constructor(text: string) { |
| 80 | + super(); |
| 81 | + this.text = text; |
| 82 | + } |
| 83 | + |
| 84 | + clone(): TextNode { |
| 85 | + return new TextNode(this.text); |
| 86 | + } |
| 87 | + |
| 88 | + toString(): string { |
| 89 | + return this.text; |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +export class RootNode extends BaseNode implements ChildrenHolder { |
| 94 | + name = "RootNode"; |
| 95 | + children: BaseNode[]; |
| 96 | + |
| 97 | + constructor(children: BaseNode[] = []) { |
| 98 | + super(); |
| 99 | + this.children = children; |
| 100 | + } |
| 101 | + |
| 102 | + addChild(child: Node): void { |
| 103 | + this.children.push(child.clone()); |
| 104 | + } |
| 105 | + |
| 106 | + clone(): RootNode { |
| 107 | + return new RootNode(this.children.map(child => child.clone())); |
| 108 | + } |
| 109 | + |
| 110 | + toString(): string { |
| 111 | + return this.children.map(child => child.toString()).join(''); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +export class ListItemNode extends RootNode { |
| 116 | + name = "ListItemNode"; |
| 117 | + |
| 118 | + toString(): string { |
| 119 | + return "[*]" + super.toString(); |
| 120 | + } |
| 121 | +} |
0 commit comments