Skip to content

Commit 1bde449

Browse files
authored
tag ending follow html protocol (#101)
1 parent b10839c commit 1bde449

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

src/fusion/htmlparser/xmltree.nim

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ type
6969
const
7070
xmlHeader* = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
7171
## Header to use for complete XML output.
72+
singleTags = ["area", "base", "basefont",
73+
"br", "col","embed", "frame", "hr", "img",
74+
"input","source","track",
75+
"isindex", # obsolete HTML 4.01
76+
"link", "meta", "param", "wbr",
77+
"command", # obsolete
78+
"keygen", # obsolete
79+
]
7280

7381
proc newXmlNode(kind: XmlNodeKind): XmlNode =
7482
## Creates a new ``XmlNode``.
@@ -85,7 +93,8 @@ proc newElement*(tag: string): XmlNode =
8593
a.add newElement("childTag")
8694
assert a.kind == xnElement
8795
assert $a == """<firstTag>
88-
<childTag />
96+
<childTag>
97+
</childTag>
8998
</firstTag>"""
9099

91100
result = newXmlNode(xnElement)
@@ -218,7 +227,8 @@ proc tag*(n: XmlNode): string {.inline.} =
218227
var a = newElement("firstTag")
219228
a.add newElement("childTag")
220229
assert $a == """<firstTag>
221-
<childTag />
230+
<childTag>
231+
</childTag>
222232
</firstTag>"""
223233
assert a.tag == "firstTag"
224234

@@ -238,11 +248,13 @@ proc `tag=`*(n: XmlNode, tag: string) {.inline.} =
238248
var a = newElement("firstTag")
239249
a.add newElement("childTag")
240250
assert $a == """<firstTag>
241-
<childTag />
251+
<childTag>
252+
</childTag>
242253
</firstTag>"""
243254
a.tag = "newTag"
244255
assert $a == """<newTag>
245-
<childTag />
256+
<childTag>
257+
</childTag>
246258
</newTag>"""
247259

248260
assert n.k == xnElement
@@ -308,7 +320,7 @@ proc add*(father, son: XmlNode) {.inline.} =
308320
f.add newText("my text")
309321
f.add newElement("sonTag")
310322
f.add newEntity("my entity")
311-
assert $f == "<myTag>my text<sonTag />&my entity;</myTag>"
323+
assert $f == "<myTag>my text<sonTag></sonTag>&my entity;</myTag>"
312324
add(father.s, son)
313325

314326
proc insert*(father, son: XmlNode, index: int) {.inline.} =
@@ -324,8 +336,10 @@ proc insert*(father, son: XmlNode, index: int) {.inline.} =
324336
f.add newElement("first")
325337
f.insert(newElement("second"), 0)
326338
assert $f == """<myTag>
327-
<second />
328-
<first />
339+
<second>
340+
</second>
341+
<first>
342+
</first>
329343
</myTag>"""
330344

331345
assert father.k == xnElement and son.k == xnElement
@@ -346,7 +360,8 @@ proc delete*(n: XmlNode, i: Natural) {.noSideEffect.} =
346360
f.insert(newElement("second"), 0)
347361
f.delete(0)
348362
assert $f == """<myTag>
349-
<first />
363+
<first>
364+
</first>
350365
</myTag>"""
351366

352367
assert n.k == xnElement
@@ -376,8 +391,8 @@ proc `[]`*(n: XmlNode, i: int): XmlNode {.inline.} =
376391
var f = newElement("myTag")
377392
f.add newElement("first")
378393
f.insert(newElement("second"), 0)
379-
assert $f[1] == "<first />"
380-
assert $f[0] == "<second />"
394+
assert $f[1] == "<first>\n</first>"
395+
assert $f[0] == "<second>\n</second>"
381396

382397
assert n.k == xnElement
383398
result = n.s[i]
@@ -591,9 +606,9 @@ proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2,
591606
c = newComment("my comment")
592607
s = ""
593608
s.add(c)
609+
a.add(b)
594610
s.add(a)
595-
s.add(b)
596-
assert s == "<!-- my comment --><firstTag />my text"
611+
assert s == "<!-- my comment --><firstTag>my text</firstTag>"
597612

598613
proc noWhitespace(n: XmlNode): bool =
599614
for i in 0 ..< n.len:
@@ -633,8 +648,8 @@ proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2,
633648
result.addEscapedAttr(val)
634649
result.add('"')
635650

636-
if n.len == 0:
637-
result.add(" />")
651+
if n.len == 0 and n.fTag in singleTags:
652+
result.add("/>")
638653
return
639654

640655
let
@@ -685,7 +700,7 @@ proc child*(n: XmlNode, name: string): XmlNode =
685700
f.add newElement("firstSon")
686701
f.add newElement("secondSon")
687702
f.add newElement("thirdSon")
688-
assert $(f.child("secondSon")) == "<secondSon />"
703+
assert $(f.child("secondSon")) == "<secondSon>\n</secondSon>"
689704

690705
assert n.kind == xnElement
691706
for i in items(n):

0 commit comments

Comments
 (0)