2
2
* Parameters for constructing a new Node object.
3
3
*/
4
4
export interface NodeConstructorParams {
5
+ /**
6
+ * The name of the node.
7
+ *
8
+ * Ref: {@link Node.name}
9
+ * @example `[b]Hello World![/b]` -> `b`
10
+ */
5
11
name : string ;
12
+ /**
13
+ * The attributes of the node.
14
+ *
15
+ * Ref: {@link Node.attributes}
16
+ * @example `[img width=100 height=100]https://example.com/image.png[/img]` -> `{ width: "100", height: "100" }`
17
+ */
6
18
attributes ?: { [ key : string ] : string } ;
19
+ /**
20
+ * The value of the node.
21
+ *
22
+ * Ref: {@link Node.value}
23
+ * @example `[color=red]Hello World![/color]` -> `red`
24
+ */
7
25
value ?: string ;
8
26
}
9
27
@@ -25,37 +43,55 @@ export abstract class BaseNode {
25
43
* Converts the node into a textual representation of the node as BBCode.
26
44
*/
27
45
abstract toString ( ) : string ;
46
+
47
+ /**
48
+ * Gets the textual representation of the node as BBCode.
49
+ * An alias for {@link BaseNode.toString}.
50
+ */
51
+ toBBCode ( ) : string {
52
+ return this . toString ( ) ;
53
+ }
28
54
}
29
55
30
56
/**
31
57
* An interface for nodes that can hold children.
32
58
*/
33
59
export interface ChildrenHolder {
34
60
/**
35
- * The children of the node.
61
+ * The children of the node. A node can have any number of children of all types.
36
62
*/
37
63
children : BaseNode [ ] ;
38
64
39
65
/**
40
- * Adds a child to the node. If the node is a {@link TextNode}, attempts to flatten the node with an previous TextNode.
66
+ * Adds a child to the node. If the node is a {@link TextNode}, attempts to flatten the node with a previous TextNode.
41
67
* @param child The child to add.
42
68
*/
43
69
addChild ( child : BaseNode ) : void ;
44
70
}
45
71
72
+ /**
73
+ * A type for attributes.
74
+ *
75
+ * A BBCode attribute is a key-value pair, where the key is a string and the value is a string. A tag can have multiple attributes.
76
+ * @example `[img width=100 height=100]https://example.com/image.png[/img]` -> `{ width: "100", height: "100" }`
77
+ */
78
+ export type AttributeType = { [ key : string ] : string } ;
79
+
46
80
/**
47
81
* An interface for nodes that can hold attributes.
48
82
*/
49
83
export interface AttributeHolder {
50
84
/**
51
85
* The attributes of the node.
86
+ * This should be public, so it can be directly accessed and modified.
52
87
*/
53
- attributes : { [ key : string ] : string } ;
88
+ attributes : AttributeType ;
54
89
55
90
/**
56
- * Adds an attribute to the node.
57
- * @param key The name of the attribute.
91
+ * Sets the attribute of the node.
92
+ * @param key The key of the attribute.
58
93
* @param value The value of the attribute.
94
+ * @deprecated Use {@link AttributeHolder.attributes} instead.
59
95
*/
60
96
setAttribute ( key : string , value : string ) : void ;
61
97
}
@@ -65,16 +101,29 @@ export interface AttributeHolder {
65
101
*/
66
102
export class Node extends BaseNode implements ChildrenHolder , AttributeHolder {
67
103
/**
68
- * The name of the tag. If a Node is used to represent a `[b]` tag, `name` will be `b`.
104
+ * The name of the tag.
105
+ *
106
+ * @example `[b]Hello World![/b]` -> `b`
69
107
*/
70
- name : string ;
108
+ public name : string ;
71
109
72
- attributes : { [ key : string ] : string } ;
110
+ /**
111
+ * The attributes of the tag.
112
+ *
113
+ * Ref: {@link AttributeType}
114
+ */
115
+ public attributes : AttributeType ;
73
116
74
- children : BaseNode [ ] = [ ] ;
117
+ /**
118
+ * The children of the tag.
119
+ *
120
+ * Ref: {@link ChildrenHolder.children}
121
+ */
122
+ public children : BaseNode [ ] = [ ] ;
75
123
76
124
/**
77
125
* Stores the [simple parameterized value](https://www.bbcode.org/reference.php) of the tag.
126
+ *
78
127
* @example `[b=red]Hello World![/b]` -> `red`
79
128
*/
80
129
value ?: string ;
@@ -111,7 +160,9 @@ export class Node extends BaseNode implements ChildrenHolder, AttributeHolder {
111
160
112
161
/**
113
162
* Sets the value of the node.
114
- * @param value
163
+ *
164
+ * @param {string } value The value of the node.
165
+ * @deprecated Use {@link Node.value} instead.
115
166
*/
116
167
setValue ( value : string ) : void {
117
168
this . value = value ;
@@ -121,6 +172,10 @@ export class Node extends BaseNode implements ChildrenHolder, AttributeHolder {
121
172
this . attributes [ key ] = value ;
122
173
}
123
174
175
+ /**
176
+ * Makes the opening tag of the node. This will include the name, and all attributes.
177
+ * @returns The opening tag of the node.
178
+ */
124
179
makeOpeningTag ( ) : string {
125
180
let nodeString = `[${ this . name } ` ;
126
181
if ( this . value ) {
@@ -153,8 +208,12 @@ export class TextNode extends BaseNode {
153
208
*/
154
209
text : string ;
155
210
156
- name = "TextNode" ;
211
+ readonly name = "TextNode" ;
157
212
213
+ /**
214
+ * Create a new TextNode.
215
+ * @param {string } text The text of the node.
216
+ */
158
217
constructor ( text : string ) {
159
218
super ( ) ;
160
219
this . text = text ;
@@ -177,6 +236,10 @@ export class RootNode extends BaseNode implements ChildrenHolder {
177
236
178
237
children : BaseNode [ ] ;
179
238
239
+ /**
240
+ * Create a new RootNode.
241
+ * @param {BaseNode[] } children The children of the node.
242
+ */
180
243
constructor ( children : BaseNode [ ] = [ ] ) {
181
244
super ( ) ;
182
245
this . children = children ;
0 commit comments