@@ -25,8 +25,11 @@ export interface AttributeHolder {
25
25
26
26
export class Node extends BaseNode implements ChildrenHolder , AttributeHolder {
27
27
name : string ;
28
+
28
29
attributes : { [ key : string ] : string } ;
30
+
29
31
children : BaseNode [ ] = [ ] ;
32
+
30
33
// For simple parameterized values, like [x=y]...[/x]
31
34
value ?: string ;
32
35
@@ -38,20 +41,25 @@ export class Node extends BaseNode implements ChildrenHolder, AttributeHolder {
38
41
}
39
42
40
43
clone ( ) : Node {
41
- const node = new Node ( { name : this . name , attributes : this . attributes , value : this . value } ) ;
42
- node . children = this . children . map ( child => child . clone ( ) ) ;
44
+ const node = new Node ( {
45
+ name : this . name ,
46
+ attributes : this . attributes ,
47
+ value : this . value ,
48
+ } ) ;
49
+ node . children = this . children . map ( ( child ) => child . clone ( ) ) ;
43
50
return node ;
44
51
}
45
52
46
53
addChild ( child : BaseNode ) : void {
47
- if ( child instanceof TextNode ) {
54
+ if ( child instanceof TextNode ) {
48
55
const previousChild = this . children [ this . children . length - 1 ] ;
49
- if ( previousChild instanceof TextNode ) {
56
+ if ( previousChild instanceof TextNode ) {
50
57
// We flatten the text nodes.
51
58
previousChild . text += child . text ;
52
59
return ;
53
60
}
54
61
}
62
+
55
63
this . children . push ( child . clone ( ) ) ;
56
64
}
57
65
@@ -63,17 +71,17 @@ export class Node extends BaseNode implements ChildrenHolder, AttributeHolder {
63
71
this . attributes [ key ] = value ;
64
72
}
65
73
66
-
67
74
toString ( ) : string {
68
75
let nodeString = `[${ this . name } ` ;
69
- if ( this . value ) {
76
+ if ( this . value ) {
70
77
nodeString += `=${ this . value } ` ;
71
78
}
79
+
72
80
Object . entries ( this . attributes ) . forEach ( ( [ key , value ] ) => {
73
81
nodeString += ` ${ key } =${ value } ` ;
74
82
} ) ;
75
- nodeString += ']' ;
76
- this . children . forEach ( child => {
83
+ nodeString += "]" ;
84
+ this . children . forEach ( ( child ) => {
77
85
nodeString += child . toString ( ) ;
78
86
} ) ;
79
87
nodeString += `[/${ this . name } ]` ;
@@ -83,7 +91,8 @@ export class Node extends BaseNode implements ChildrenHolder, AttributeHolder {
83
91
84
92
export class TextNode extends BaseNode {
85
93
text : string ;
86
- name : string = 'TextNode' ;
94
+
95
+ name = "TextNode" ;
87
96
88
97
constructor ( text : string ) {
89
98
super ( ) ;
@@ -101,6 +110,7 @@ export class TextNode extends BaseNode {
101
110
102
111
export class RootNode extends BaseNode implements ChildrenHolder {
103
112
name = "RootNode" ;
113
+
104
114
children : BaseNode [ ] ;
105
115
106
116
constructor ( children : BaseNode [ ] = [ ] ) {
@@ -109,34 +119,35 @@ export class RootNode extends BaseNode implements ChildrenHolder {
109
119
}
110
120
111
121
addChild ( child : BaseNode ) : void {
112
- if ( child instanceof TextNode ) {
122
+ if ( child instanceof TextNode ) {
113
123
const previousChild = this . children [ this . children . length - 1 ] ;
114
- if ( previousChild instanceof TextNode ) {
124
+ if ( previousChild instanceof TextNode ) {
115
125
// We flatten the text nodes.
116
126
previousChild . text += child . text ;
117
127
return ;
118
128
}
119
129
}
130
+
120
131
this . children . push ( child . clone ( ) ) ;
121
132
}
122
133
123
134
clone ( ) : RootNode {
124
- return new RootNode ( this . children . map ( child => child . clone ( ) ) ) ;
135
+ return new RootNode ( this . children . map ( ( child ) => child . clone ( ) ) ) ;
125
136
}
126
137
127
138
toString ( ) : string {
128
- return this . children . map ( child => child . toString ( ) ) . join ( '' ) ;
139
+ return this . children . map ( ( child ) => child . toString ( ) ) . join ( "" ) ;
129
140
}
130
141
}
131
142
132
143
export class ListItemNode extends RootNode {
133
144
name = "*" ;
134
145
135
146
toString ( ) : string {
136
- return " [*]" + super . toString ( ) ;
147
+ return ` [*]${ super . toString ( ) } ` ;
137
148
}
138
149
139
150
clone ( ) : ListItemNode {
140
- return new ListItemNode ( this . children . map ( child => child . clone ( ) ) ) ;
151
+ return new ListItemNode ( this . children . map ( ( child ) => child . clone ( ) ) ) ;
141
152
}
142
153
}
0 commit comments