diff options
Diffstat (limited to 'lib/pure/xmltree.nim')
-rw-r--r-- | lib/pure/xmltree.nim | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/lib/pure/xmltree.nim b/lib/pure/xmltree.nim index 8f85cb5c9..47658b59b 100644 --- a/lib/pure/xmltree.nim +++ b/lib/pure/xmltree.nim @@ -9,7 +9,7 @@ ## A simple XML tree. More efficient and simpler than the DOM. -import macros, strtabs +import macros, strtabs, strutils type XmlNode* = ref XmlNodeObj ## an XML tree consists of ``XmlNode``'s. @@ -217,8 +217,9 @@ proc escape*(s: string): string = result = newStringOfCap(s.len) addEscaped(result, s) -proc addIndent(result: var string, indent: int) = - result.add("\n") +proc addIndent(result: var string, indent: int, addNewLines: bool) = + if addNewLines: + result.add("\n") for i in 1..indent: result.add(' ') proc noWhitespace(n: XmlNode): bool = @@ -227,7 +228,8 @@ proc noWhitespace(n: XmlNode): bool = for i in 0..n.len-1: if n[i].kind in {xnText, xnEntity}: return true -proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2) = +proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2, + addNewLines=true) = ## adds the textual representation of `n` to `result`. proc addEscapedAttr(result: var string, s: string) = @@ -260,14 +262,15 @@ proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2) = # for mixed leaves, we cannot output whitespace for readability, # because this would be wrong. For example: ``a<b>b</b>`` is # different from ``a <b>b</b>``. - for i in 0..n.len-1: result.add(n[i], indent+indWidth, indWidth) + for i in 0..n.len-1: + result.add(n[i], indent+indWidth, indWidth, addNewLines) else: for i in 0..n.len-1: - result.addIndent(indent+indWidth) - result.add(n[i], indent+indWidth, indWidth) - result.addIndent(indent) + result.addIndent(indent+indWidth, addNewLines) + result.add(n[i], indent+indWidth, indWidth, addNewLines) + result.addIndent(indent, addNewLines) else: - result.add(n[0], indent+indWidth, indWidth) + result.add(n[0], indent+indWidth, indWidth, addNewLines) result.add("</") result.add(n.fTag) result.add(">") @@ -316,7 +319,10 @@ proc xmlConstructor(a: NimNode): NimNode {.compileTime.} = var elements = newNimNode(nnkBracket, a) for i in 1..a.len-1: if a[i].kind == nnkExprEqExpr: - attrs.add(toStrLit(a[i][0])) + # In order to support attributes like `data-lang` we have to + # replace whitespace because `toStrLit` gives `data - lang`. + let attrName = toStrLit(a[i][0]).strVal.replace(" ", "") + attrs.add(newStrLitNode(attrName)) attrs.add(a[i][1]) #echo repr(attrs) else: |