@@ -163,56 +163,48 @@ proc newNode(name: string, text = ""): XmlNode =
163
163
new(result )
164
164
result .name = name
165
165
result .text = text
166
+ result .attributes = newStringTable(modeCaseInsensitive)
167
+ result .children = @ []
166
168
167
169
proc child* (node: XmlNode, name: string ): XmlNode =
168
170
# # finds the first element of `node` with name `name`
169
171
# # returns `nil` on failure
170
- if not node.children.isNil:
171
- for n in node.children:
172
- if n.name == name:
173
- result = n
174
- break
172
+ for n in node.children:
173
+ if n.name == name:
174
+ result = n
175
+ break
175
176
176
177
proc `$` * (node: XmlNode): string =
177
178
result = " <"
178
179
result .add(node.name)
179
- if not node.attributes.isNil:
180
- for k, v in node.attributes.pairs:
181
- result .add(fmt" { k} ="" { v} "" " )
182
- if node.text.len == 0 and node.children.isNil :
180
+
181
+ for k, v in node.attributes.pairs:
182
+ result .add(fmt" { k} ="" { v} "" " )
183
+ if node.text.len == 0 and node.children.len == 0 :
183
184
result .add(" />" )
184
185
return
185
186
elif node.text.len > 0 :
186
187
result .add(" >" & node.text)
187
188
else :
188
189
result .add(" >" )
189
190
190
- if not node.children.isNil:
191
- for child in node.children:
192
- result .add($ child)
191
+ for child in node.children:
192
+ result .add($ child)
193
193
result .add(" </" & node.name & " >" )
194
194
195
195
196
196
proc addChild* (node, child: XmlNode) =
197
- if node.children.isNil:
198
- node.children = @ []
199
197
node.children.add(child)
200
198
201
199
proc hasAttr* (node: XmlNode, name: string ): bool =
202
200
# # returns `true` if `node` has attribute `name`
203
- if node.attributes.isNil:
204
- result = false
205
- else :
206
- result = node.attributes.hasKey(name)
201
+ result = node.attributes.hasKey(name)
207
202
208
203
proc attr* (node: XmlNode, name: string ): string =
209
204
# # returns value of attribute `name`, returns "" on failure
210
- if not node.attributes.isNil:
211
- result = node.attributes.getOrDefault(name)
205
+ result = node.attributes.getOrDefault(name)
212
206
213
207
proc setAttr(node: XmlNode, name, value: string ) =
214
- if node.attributes.isNil:
215
- node.attributes = newStringTable(modeCaseInsensitive)
216
208
node.attributes[name] = value
217
209
218
210
proc parseNode(tokens: seq [XmlToken], start = 0 ) : (XmlNode, int ) =
0 commit comments