fixes #21290; deindent if the last same level is a text node (#21293)

* fixes #21290; deindent if the last same level is a text node

* add one more test
This commit is contained in:
ringabout 2023-01-24 20:35:19 +08:00 • committed by GitHub
commit 5e7f7109e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 17 deletions

View file

@ -723,20 +723,8 @@ proc addIndent(result: var string, indent: int, addNewLines: bool) =
for i in 1 .. indent: for i in 1 .. indent:
result.add(' ') result.add(' ')
proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2, proc addImpl(result: var string, n: XmlNode, indent = 0, indWidth = 2,
addNewLines = true) = addNewLines = true, lastNodeIsText = false) =
## Adds the textual representation of `n` to string `result`.
runnableExamples:
var
a = newElement("firstTag")
b = newText("my text")
c = newComment("my comment")
s = ""
s.add(c)
s.add(a)
s.add(b)
assert s == "<!-- my comment --><firstTag />my text"
proc noWhitespace(n: XmlNode): bool = proc noWhitespace(n: XmlNode): bool =
for i in 0 ..< n.len: for i in 0 ..< n.len:
if n[i].kind in {xnText, xnEntity}: return true if n[i].kind in {xnText, xnEntity}: return true
@ -756,7 +744,7 @@ proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2,
case n.k case n.k
of xnElement: of xnElement:
if indent > 0: if indent > 0 and not lastNodeIsText:
result.addIndent(indent, addNewLines) result.addIndent(indent, addNewLines)
let let
@ -785,8 +773,10 @@ proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2,
else: else:
indent+indWidth indent+indWidth
result.add('>') result.add('>')
var lastNodeIsText = false
for i in 0 ..< n.len: for i in 0 ..< n.len:
result.add(n[i], indentNext, indWidth, addNewLines) result.addImpl(n[i], indentNext, indWidth, addNewLines, lastNodeIsText)
lastNodeIsText = n[i].kind == xnText
if not n.noWhitespace(): if not n.noWhitespace():
result.addIndent(indent, addNewLines) result.addIndent(indent, addNewLines)
@ -811,6 +801,21 @@ proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2,
result.add(n.fText) result.add(n.fText)
result.add(';') result.add(';')
proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2,
addNewLines = true) {.inline.} =
## Adds the textual representation of `n` to string `result`.
runnableExamples:
var
a = newElement("firstTag")
b = newText("my text")
c = newComment("my comment")
s = ""
s.add(c)
s.add(a)
s.add(b)
assert s == "<!-- my comment --><firstTag />my text"
result.addImpl(n, indent, indWidth, addNewLines)
proc `$`*(n: XmlNode): string = proc `$`*(n: XmlNode): string =
## Converts `n` into its string representation. ## Converts `n` into its string representation.
## ##

View file

@ -1,4 +1,4 @@
import std/[xmltree, assertions] import std/[xmltree, assertions, xmlparser]
block: block:
@ -83,3 +83,19 @@ block:
x.add newElement("sonTag") x.add newElement("sonTag")
x.add newEntity("my entity") x.add newEntity("my entity")
doAssert $x == "<myTag>my text<sonTag />&my entity;</myTag>" doAssert $x == "<myTag>my text<sonTag />&my entity;</myTag>"
block: # bug #21290
let x = newXmlTree("foo",[
newXmlTree("bar",[
newText("Hola"),
newXmlTree("qux",[
newXmlTree("plugh",[])
])
])
])
let s = $x
doAssert $parseXml(s) == s
doAssert s == """<foo>
<bar>Hola<qux> <plugh /> </qux></bar>
</foo>"""