Add some nil checks for xmldom (and clean up a bit)
This commit is contained in:
parent
779f2b37aa
commit
2c6406703c
1 changed files with 223 additions and 196 deletions
|
|
@ -168,7 +168,7 @@ proc documentElement*(doc: PDocument): PElement =
|
||||||
proc findNodes(nl: PNode, name: string): seq[PNode] =
|
proc findNodes(nl: PNode, name: string): seq[PNode] =
|
||||||
# Made for getElementsByTagName
|
# Made for getElementsByTagName
|
||||||
var r: seq[PNode] = @[]
|
var r: seq[PNode] = @[]
|
||||||
if isNil(nl.childNodes): return @[]
|
if nl.childNodes == nil: return @[]
|
||||||
if nl.childNodes.len() == 0: return @[]
|
if nl.childNodes.len() == 0: return @[]
|
||||||
|
|
||||||
for i in items(nl.childNodes):
|
for i in items(nl.childNodes):
|
||||||
|
|
@ -176,7 +176,7 @@ proc findNodes(nl: PNode, name: string): seq[PNode] =
|
||||||
if i.FNodeName == name or name == "*":
|
if i.FNodeName == name or name == "*":
|
||||||
r.add(i)
|
r.add(i)
|
||||||
|
|
||||||
if not isNil(i.childNodes):
|
if i.childNodes != nil:
|
||||||
if i.childNodes.len() != 0:
|
if i.childNodes.len() != 0:
|
||||||
r.add(findNodes(i, name))
|
r.add(findNodes(i, name))
|
||||||
|
|
||||||
|
|
@ -185,7 +185,7 @@ proc findNodes(nl: PNode, name: string): seq[PNode] =
|
||||||
proc findNodesNS(nl: PNode, namespaceURI: string, localName: string): seq[PNode] =
|
proc findNodesNS(nl: PNode, namespaceURI: string, localName: string): seq[PNode] =
|
||||||
# Made for getElementsByTagNameNS
|
# Made for getElementsByTagNameNS
|
||||||
var r: seq[PNode] = @[]
|
var r: seq[PNode] = @[]
|
||||||
if isNil(nl.childNodes): return @[]
|
if nl.childNodes == nil: return @[]
|
||||||
if nl.childNodes.len() == 0: return @[]
|
if nl.childNodes.len() == 0: return @[]
|
||||||
|
|
||||||
for i in items(nl.childNodes):
|
for i in items(nl.childNodes):
|
||||||
|
|
@ -193,7 +193,7 @@ proc findNodesNS(nl: PNode, namespaceURI: string, localName: string): seq[PNode]
|
||||||
if (i.FNamespaceURI == namespaceURI or namespaceURI == "*") and (i.FLocalName == localName or localName == "*"):
|
if (i.FNamespaceURI == namespaceURI or namespaceURI == "*") and (i.FLocalName == localName or localName == "*"):
|
||||||
r.add(i)
|
r.add(i)
|
||||||
|
|
||||||
if not isNil(i.childNodes):
|
if i.childNodes != nil:
|
||||||
if i.childNodes.len() != 0:
|
if i.childNodes.len() != 0:
|
||||||
r.add(findNodesNS(i, namespaceURI, localName))
|
r.add(findNodesNS(i, namespaceURI, localName))
|
||||||
|
|
||||||
|
|
@ -445,7 +445,7 @@ proc importNode*(doc: PDocument, importedNode: PNode, deep: bool): PNode =
|
||||||
proc firstChild*(n: PNode): PNode =
|
proc firstChild*(n: PNode): PNode =
|
||||||
## Returns this node's first child
|
## Returns this node's first child
|
||||||
|
|
||||||
if n.childNodes.len() > 0:
|
if n.childNodes != nil and n.childNodes.len() > 0:
|
||||||
return n.childNodes[0]
|
return n.childNodes[0]
|
||||||
else:
|
else:
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -453,7 +453,7 @@ proc firstChild*(n: PNode): PNode =
|
||||||
proc lastChild*(n: PNode): PNode =
|
proc lastChild*(n: PNode): PNode =
|
||||||
## Returns this node's last child
|
## Returns this node's last child
|
||||||
|
|
||||||
if n.childNodes.len() > 0:
|
if n.childNodes != nil and n.childNodes.len() > 0:
|
||||||
return n.childNodes[n.childNodes.len() - 1]
|
return n.childNodes[n.childNodes.len() - 1]
|
||||||
else:
|
else:
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -474,6 +474,8 @@ proc `namespaceURI=`*(n: PNode, value: string) =
|
||||||
proc nextSibling*(n: PNode): PNode =
|
proc nextSibling*(n: PNode): PNode =
|
||||||
## Returns the next sibling of this node
|
## Returns the next sibling of this node
|
||||||
|
|
||||||
|
if n.FParentNode == nil or n.FParentNode.childNodes == nil:
|
||||||
|
return nil
|
||||||
var nLow: int = low(n.FParentNode.childNodes)
|
var nLow: int = low(n.FParentNode.childNodes)
|
||||||
var nHigh: int = high(n.FParentNode.childNodes)
|
var nHigh: int = high(n.FParentNode.childNodes)
|
||||||
for i in nLow..nHigh:
|
for i in nLow..nHigh:
|
||||||
|
|
@ -504,6 +506,8 @@ proc parentNode*(n: PNode): PNode =
|
||||||
proc previousSibling*(n: PNode): PNode =
|
proc previousSibling*(n: PNode): PNode =
|
||||||
## Returns the previous sibling of this node
|
## Returns the previous sibling of this node
|
||||||
|
|
||||||
|
if n.FParentNode == nil or n.FParentNode.childNodes == nil:
|
||||||
|
return nil
|
||||||
var nLow: int = low(n.FParentNode.childNodes)
|
var nLow: int = low(n.FParentNode.childNodes)
|
||||||
var nHigh: int = high(n.FParentNode.childNodes)
|
var nHigh: int = high(n.FParentNode.childNodes)
|
||||||
for i in nLow..nHigh:
|
for i in nLow..nHigh:
|
||||||
|
|
@ -545,7 +549,7 @@ proc appendChild*(n: PNode, newChild: PNode) =
|
||||||
## If the newChild is already in the tree, it is first removed.
|
## If the newChild is already in the tree, it is first removed.
|
||||||
|
|
||||||
# Check if n contains newChild
|
# Check if n contains newChild
|
||||||
if not IsNil(n.childNodes):
|
if n.childNodes != nil:
|
||||||
for i in low(n.childNodes)..high(n.childNodes):
|
for i in low(n.childNodes)..high(n.childNodes):
|
||||||
if n.childNodes[i] == newChild:
|
if n.childNodes[i] == newChild:
|
||||||
raise newException(EHierarchyRequestErr, "The node to append is already in this nodes children.")
|
raise newException(EHierarchyRequestErr, "The node to append is already in this nodes children.")
|
||||||
|
|
@ -560,7 +564,7 @@ proc appendChild*(n: PNode, newChild: PNode) =
|
||||||
if n.nodeType in childlessObjects:
|
if n.nodeType in childlessObjects:
|
||||||
raise newException(ENoModificationAllowedErr, "Cannot append children to a childless node")
|
raise newException(ENoModificationAllowedErr, "Cannot append children to a childless node")
|
||||||
|
|
||||||
if isNil(n.childNodes): n.childNodes = @[]
|
if n.childNodes == nil: n.childNodes = @[]
|
||||||
|
|
||||||
newChild.FParentNode = n
|
newChild.FParentNode = n
|
||||||
for i in low(n.childNodes)..high(n.childNodes):
|
for i in low(n.childNodes)..high(n.childNodes):
|
||||||
|
|
@ -586,7 +590,7 @@ proc cloneNode*(n: PNode, deep: bool): PNode =
|
||||||
# Import the childNodes
|
# Import the childNodes
|
||||||
var tmp: seq[PNode] = n.childNodes
|
var tmp: seq[PNode] = n.childNodes
|
||||||
n.childNodes = @[]
|
n.childNodes = @[]
|
||||||
if deep:
|
if deep and tmp != nil:
|
||||||
for i in low(tmp.len())..high(tmp.len()):
|
for i in low(tmp.len())..high(tmp.len()):
|
||||||
n.childNodes.add(cloneNode(tmp[i], deep))
|
n.childNodes.add(cloneNode(tmp[i], deep))
|
||||||
return newNode
|
return newNode
|
||||||
|
|
@ -598,11 +602,11 @@ proc cloneNode*(n: PNode, deep: bool): PNode =
|
||||||
|
|
||||||
proc hasAttributes*(n: PNode): bool =
|
proc hasAttributes*(n: PNode): bool =
|
||||||
## Returns whether this node (if it is an element) has any attributes.
|
## Returns whether this node (if it is an element) has any attributes.
|
||||||
return n.attributes.len() > 0
|
return n.attributes != nil and n.attributes.len() > 0
|
||||||
|
|
||||||
proc hasChildNodes*(n: PNode): bool =
|
proc hasChildNodes*(n: PNode): bool =
|
||||||
## Returns whether this node has any children.
|
## Returns whether this node has any children.
|
||||||
return n.childNodes.len() > 0
|
return n.childNodes != nil and n.childNodes.len() > 0
|
||||||
|
|
||||||
proc insertBefore*(n: PNode, newChild: PNode, refChild: PNode): PNode =
|
proc insertBefore*(n: PNode, newChild: PNode, refChild: PNode): PNode =
|
||||||
## Inserts the node ``newChild`` before the existing child node ``refChild``.
|
## Inserts the node ``newChild`` before the existing child node ``refChild``.
|
||||||
|
|
@ -612,11 +616,16 @@ proc insertBefore*(n: PNode, newChild: PNode, refChild: PNode): PNode =
|
||||||
if n.FOwnerDocument != newChild.FOwnerDocument:
|
if n.FOwnerDocument != newChild.FOwnerDocument:
|
||||||
raise newException(EWrongDocumentErr, "This node belongs to a different document, use importNode.")
|
raise newException(EWrongDocumentErr, "This node belongs to a different document, use importNode.")
|
||||||
|
|
||||||
|
if n.childNodes == nil:
|
||||||
|
n.ChildNodes = @[]
|
||||||
|
|
||||||
for i in low(n.childNodes)..high(n.childNodes):
|
for i in low(n.childNodes)..high(n.childNodes):
|
||||||
if n.childNodes[i] == refChild:
|
if n.childNodes[i] == refChild:
|
||||||
n.childNodes.insert(newChild, i - 1)
|
n.childNodes.insert(newChild, i - 1)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
n.ChildNodes.add(newChild)
|
||||||
|
|
||||||
proc isSupported*(n: PNode, feature: string, version: string): bool =
|
proc isSupported*(n: PNode, feature: string, version: string): bool =
|
||||||
## Tests whether the DOM implementation implements a specific
|
## Tests whether the DOM implementation implements a specific
|
||||||
## feature and that feature is supported by this node.
|
## feature and that feature is supported by this node.
|
||||||
|
|
@ -638,7 +647,7 @@ proc normalize*(n: PNode) =
|
||||||
|
|
||||||
var newChildNodes: seq[PNode] = @[]
|
var newChildNodes: seq[PNode] = @[]
|
||||||
while True:
|
while True:
|
||||||
if i >= n.childNodes.len:
|
if n.childNodes == nil or i >= n.childNodes.len:
|
||||||
break
|
break
|
||||||
if n.childNodes[i].nodeType == TextNode:
|
if n.childNodes[i].nodeType == TextNode:
|
||||||
|
|
||||||
|
|
@ -662,6 +671,7 @@ proc normalize*(n: PNode) =
|
||||||
|
|
||||||
proc removeChild*(n: PNode, oldChild: PNode): PNode =
|
proc removeChild*(n: PNode, oldChild: PNode): PNode =
|
||||||
## Removes the child node indicated by ``oldChild`` from the list of children, and returns it.
|
## Removes the child node indicated by ``oldChild`` from the list of children, and returns it.
|
||||||
|
if n.childNodes != nil:
|
||||||
for i in low(n.childNodes)..high(n.childNodes):
|
for i in low(n.childNodes)..high(n.childNodes):
|
||||||
if n.childNodes[i] == oldChild:
|
if n.childNodes[i] == oldChild:
|
||||||
result = n.childNodes[i]
|
result = n.childNodes[i]
|
||||||
|
|
@ -677,6 +687,7 @@ proc replaceChild*(n: PNode, newChild: PNode, oldChild: PNode): PNode =
|
||||||
if n.FOwnerDocument != newChild.FOwnerDocument:
|
if n.FOwnerDocument != newChild.FOwnerDocument:
|
||||||
raise newException(EWrongDocumentErr, "This node belongs to a different document, use importNode.")
|
raise newException(EWrongDocumentErr, "This node belongs to a different document, use importNode.")
|
||||||
|
|
||||||
|
if n.childNodes != nil:
|
||||||
for i in low(n.childNodes)..high(n.childNodes):
|
for i in low(n.childNodes)..high(n.childNodes):
|
||||||
if n.childNodes[i] == oldChild:
|
if n.childNodes[i] == oldChild:
|
||||||
result = n.childNodes[i]
|
result = n.childNodes[i]
|
||||||
|
|
@ -745,7 +756,7 @@ proc removeNamedItemNS*(NList: var seq[PNode], namespaceURI: string, localName:
|
||||||
proc setNamedItem*(NList: var seq[PNode], arg: PNode): PNode =
|
proc setNamedItem*(NList: var seq[PNode], arg: PNode): PNode =
|
||||||
## Adds ``arg`` as a ``Node`` to the ``NList``
|
## Adds ``arg`` as a ``Node`` to the ``NList``
|
||||||
## If a node with the same name is already present in this map, it is replaced by the new one.
|
## If a node with the same name is already present in this map, it is replaced by the new one.
|
||||||
if not isNil(NList):
|
if NList != nil:
|
||||||
if NList.len() > 0:
|
if NList.len() > 0:
|
||||||
#Check if newChild is from this nodes document
|
#Check if newChild is from this nodes document
|
||||||
if NList[0].FOwnerDocument != arg.FOwnerDocument:
|
if NList[0].FOwnerDocument != arg.FOwnerDocument:
|
||||||
|
|
@ -769,7 +780,7 @@ proc setNamedItem*(NList: var seq[PNode], arg: PNode): PNode =
|
||||||
proc setNamedItem*(NList: var seq[PAttr], arg: PAttr): PAttr =
|
proc setNamedItem*(NList: var seq[PAttr], arg: PAttr): PAttr =
|
||||||
## Adds ``arg`` as a ``Node`` to the ``NList``
|
## Adds ``arg`` as a ``Node`` to the ``NList``
|
||||||
## If a node with the same name is already present in this map, it is replaced by the new one.
|
## If a node with the same name is already present in this map, it is replaced by the new one.
|
||||||
if not IsNil(NList):
|
if NList != nil:
|
||||||
if NList.len() > 0:
|
if NList.len() > 0:
|
||||||
# Check if newChild is from this nodes document
|
# Check if newChild is from this nodes document
|
||||||
if NList[0].FOwnerDocument != arg.FOwnerDocument:
|
if NList[0].FOwnerDocument != arg.FOwnerDocument:
|
||||||
|
|
@ -795,7 +806,7 @@ proc setNamedItem*(NList: var seq[PAttr], arg: PAttr): PAttr =
|
||||||
|
|
||||||
proc setNamedItemNS*(NList: var seq[PNode], arg: PNode): PNode =
|
proc setNamedItemNS*(NList: var seq[PNode], arg: PNode): PNode =
|
||||||
## Adds a node using its ``namespaceURI`` and ``localName``
|
## Adds a node using its ``namespaceURI`` and ``localName``
|
||||||
if not IsNil(NList):
|
if NList != nil:
|
||||||
if NList.len() > 0:
|
if NList.len() > 0:
|
||||||
# Check if newChild is from this nodes document
|
# Check if newChild is from this nodes document
|
||||||
if NList[0].FOwnerDocument != arg.FOwnerDocument:
|
if NList[0].FOwnerDocument != arg.FOwnerDocument:
|
||||||
|
|
@ -818,7 +829,7 @@ proc setNamedItemNS*(NList: var seq[PNode], arg: PNode): PNode =
|
||||||
|
|
||||||
proc setNamedItemNS*(NList: var seq[PAttr], arg: PAttr): PAttr =
|
proc setNamedItemNS*(NList: var seq[PAttr], arg: PAttr): PAttr =
|
||||||
## Adds a node using its ``namespaceURI`` and ``localName``
|
## Adds a node using its ``namespaceURI`` and ``localName``
|
||||||
if not isNil(NList):
|
if NList != nil:
|
||||||
if NList.len() > 0:
|
if NList.len() > 0:
|
||||||
# Check if newChild is from this nodes document
|
# Check if newChild is from this nodes document
|
||||||
if NList[0].FOwnerDocument != arg.FOwnerDocument:
|
if NList[0].FOwnerDocument != arg.FOwnerDocument:
|
||||||
|
|
@ -873,6 +884,8 @@ proc tagName*(el: PElement): string =
|
||||||
# Procedures
|
# Procedures
|
||||||
proc getAttribute*(el: PElement, name: string): string =
|
proc getAttribute*(el: PElement, name: string): string =
|
||||||
## Retrieves an attribute value by ``name``
|
## Retrieves an attribute value by ``name``
|
||||||
|
if el.attributes == nil:
|
||||||
|
return nil
|
||||||
var attribute = el.attributes.getNamedItem(name)
|
var attribute = el.attributes.getNamedItem(name)
|
||||||
if attribute != nil:
|
if attribute != nil:
|
||||||
return attribute.value
|
return attribute.value
|
||||||
|
|
@ -881,6 +894,8 @@ proc getAttribute*(el: PElement, name: string): string =
|
||||||
|
|
||||||
proc getAttributeNS*(el: PElement, namespaceURI: string, localName: string): string =
|
proc getAttributeNS*(el: PElement, namespaceURI: string, localName: string): string =
|
||||||
## Retrieves an attribute value by ``localName`` and ``namespaceURI``
|
## Retrieves an attribute value by ``localName`` and ``namespaceURI``
|
||||||
|
if el.attributes == nil:
|
||||||
|
return nil
|
||||||
var attribute = el.attributes.getNamedItemNS(namespaceURI, localName)
|
var attribute = el.attributes.getNamedItemNS(namespaceURI, localName)
|
||||||
if attribute != nil:
|
if attribute != nil:
|
||||||
return attribute.value
|
return attribute.value
|
||||||
|
|
@ -890,10 +905,14 @@ proc getAttributeNS*(el: PElement, namespaceURI: string, localName: string): str
|
||||||
proc getAttributeNode*(el: PElement, name: string): PAttr =
|
proc getAttributeNode*(el: PElement, name: string): PAttr =
|
||||||
## Retrieves an attribute node by ``name``
|
## Retrieves an attribute node by ``name``
|
||||||
## To retrieve an attribute node by qualified name and namespace URI, use the `getAttributeNodeNS` method
|
## To retrieve an attribute node by qualified name and namespace URI, use the `getAttributeNodeNS` method
|
||||||
|
if el.attributes == nil:
|
||||||
|
return nil
|
||||||
return el.attributes.getNamedItem(name)
|
return el.attributes.getNamedItem(name)
|
||||||
|
|
||||||
proc getAttributeNodeNS*(el: PElement, namespaceURI: string, localName: string): PAttr =
|
proc getAttributeNodeNS*(el: PElement, namespaceURI: string, localName: string): PAttr =
|
||||||
## Retrieves an `Attr` node by ``localName`` and ``namespaceURI``
|
## Retrieves an `Attr` node by ``localName`` and ``namespaceURI``
|
||||||
|
if el.attributes == nil:
|
||||||
|
return nil
|
||||||
return el.attributes.getNamedItemNS(namespaceURI, localName)
|
return el.attributes.getNamedItemNS(namespaceURI, localName)
|
||||||
|
|
||||||
proc getElementsByTagName*(el: PElement, name: string): seq[PNode] =
|
proc getElementsByTagName*(el: PElement, name: string): seq[PNode] =
|
||||||
|
|
@ -911,21 +930,27 @@ proc getElementsByTagNameNS*(el: PElement, namespaceURI: string, localName: stri
|
||||||
proc hasAttribute*(el: PElement, name: string): bool =
|
proc hasAttribute*(el: PElement, name: string): bool =
|
||||||
## Returns ``true`` when an attribute with a given ``name`` is specified
|
## Returns ``true`` when an attribute with a given ``name`` is specified
|
||||||
## on this element , ``false`` otherwise.
|
## on this element , ``false`` otherwise.
|
||||||
|
if el.attributes == nil:
|
||||||
|
return false
|
||||||
return el.attributes.getNamedItem(name) != nil
|
return el.attributes.getNamedItem(name) != nil
|
||||||
|
|
||||||
proc hasAttributeNS*(el: PElement, namespaceURI: string, localName: string): bool =
|
proc hasAttributeNS*(el: PElement, namespaceURI: string, localName: string): bool =
|
||||||
## Returns ``true`` when an attribute with a given ``localName`` and
|
## Returns ``true`` when an attribute with a given ``localName`` and
|
||||||
## ``namespaceURI`` is specified on this element , ``false`` otherwise
|
## ``namespaceURI`` is specified on this element , ``false`` otherwise
|
||||||
|
if el.attributes == nil:
|
||||||
|
return false
|
||||||
return el.attributes.getNamedItemNS(namespaceURI, localName) != nil
|
return el.attributes.getNamedItemNS(namespaceURI, localName) != nil
|
||||||
|
|
||||||
proc removeAttribute*(el: PElement, name: string) =
|
proc removeAttribute*(el: PElement, name: string) =
|
||||||
## Removes an attribute by ``name``
|
## Removes an attribute by ``name``
|
||||||
|
if el.attributes != nil:
|
||||||
for i in low(el.attributes)..high(el.attributes):
|
for i in low(el.attributes)..high(el.attributes):
|
||||||
if el.attributes[i].FName == name:
|
if el.attributes[i].FName == name:
|
||||||
el.attributes.delete(i)
|
el.attributes.delete(i)
|
||||||
|
|
||||||
proc removeAttributeNS*(el: PElement, namespaceURI: string, localName: string) =
|
proc removeAttributeNS*(el: PElement, namespaceURI: string, localName: string) =
|
||||||
## Removes an attribute by ``localName`` and ``namespaceURI``
|
## Removes an attribute by ``localName`` and ``namespaceURI``
|
||||||
|
if el.attributes != nil:
|
||||||
for i in low(el.attributes)..high(el.attributes):
|
for i in low(el.attributes)..high(el.attributes):
|
||||||
if el.attributes[i].FNamespaceURI == namespaceURI and
|
if el.attributes[i].FNamespaceURI == namespaceURI and
|
||||||
el.attributes[i].FLocalName == localName:
|
el.attributes[i].FLocalName == localName:
|
||||||
|
|
@ -934,6 +959,7 @@ proc removeAttributeNS*(el: PElement, namespaceURI: string, localName: string) =
|
||||||
proc removeAttributeNode*(el: PElement, oldAttr: PAttr): PAttr =
|
proc removeAttributeNode*(el: PElement, oldAttr: PAttr): PAttr =
|
||||||
## Removes the specified attribute node
|
## Removes the specified attribute node
|
||||||
## If the attribute node cannot be found raises ``ENotFoundErr``
|
## If the attribute node cannot be found raises ``ENotFoundErr``
|
||||||
|
if el.attributes != nil:
|
||||||
for i in low(el.attributes)..high(el.attributes):
|
for i in low(el.attributes)..high(el.attributes):
|
||||||
if el.attributes[i] == oldAttr:
|
if el.attributes[i] == oldAttr:
|
||||||
result = el.attributes[i]
|
result = el.attributes[i]
|
||||||
|
|
@ -957,7 +983,7 @@ proc setAttributeNode*(el: PElement, newAttr: PAttr): PAttr =
|
||||||
"This attribute is in use by another element, use cloneNode")
|
"This attribute is in use by another element, use cloneNode")
|
||||||
# Exceptions end
|
# Exceptions end
|
||||||
|
|
||||||
if isNil(el.attributes): el.attributes = @[]
|
if el.attributes == nil: el.attributes = @[]
|
||||||
return el.attributes.setNamedItem(newAttr)
|
return el.attributes.setNamedItem(newAttr)
|
||||||
|
|
||||||
proc setAttributeNodeNS*(el: PElement, newAttr: PAttr): PAttr =
|
proc setAttributeNodeNS*(el: PElement, newAttr: PAttr): PAttr =
|
||||||
|
|
@ -975,7 +1001,7 @@ proc setAttributeNodeNS*(el: PElement, newAttr: PAttr): PAttr =
|
||||||
"This attribute is in use by another element, use cloneNode")
|
"This attribute is in use by another element, use cloneNode")
|
||||||
# Exceptions end
|
# Exceptions end
|
||||||
|
|
||||||
if isNil(el.attributes): el.attributes = @[]
|
if el.attributes == nil: el.attributes = @[]
|
||||||
return el.attributes.setNamedItemNS(newAttr)
|
return el.attributes.setNamedItemNS(newAttr)
|
||||||
|
|
||||||
proc setAttribute*(el: PElement, name: string, value: string) =
|
proc setAttribute*(el: PElement, name: string, value: string) =
|
||||||
|
|
@ -1025,7 +1051,7 @@ proc splitData*(TextNode: PText, offset: int): PText =
|
||||||
TextNode.data = left
|
TextNode.data = left
|
||||||
var right: string = TextNode.data.substr(offset, TextNode.data.len())
|
var right: string = TextNode.data.substr(offset, TextNode.data.len())
|
||||||
|
|
||||||
if TextNode.FParentNode != nil:
|
if TextNode.FParentNode != nil and TextNode.FParentNode.childNodes != nil:
|
||||||
for i in low(TextNode.FParentNode.childNodes)..high(TextNode.FParentNode.childNodes):
|
for i in low(TextNode.FParentNode.childNodes)..high(TextNode.FParentNode.childNodes):
|
||||||
if TextNode.FParentNode.childNodes[i] == TextNode:
|
if TextNode.FParentNode.childNodes[i] == TextNode:
|
||||||
var newNode: PText = TextNode.FOwnerDocument.createTextNode(right)
|
var newNode: PText = TextNode.FOwnerDocument.createTextNode(right)
|
||||||
|
|
@ -1057,10 +1083,11 @@ proc addEscaped(s: string): string =
|
||||||
|
|
||||||
proc nodeToXml(n: PNode, indent: int = 0): string =
|
proc nodeToXml(n: PNode, indent: int = 0): string =
|
||||||
result = repeatChar(indent, ' ') & "<" & n.nodeName
|
result = repeatChar(indent, ' ') & "<" & n.nodeName
|
||||||
for i in items(n.Attributes):
|
if n.attributes != nil:
|
||||||
|
for i in items(n.attributes):
|
||||||
result.add(" " & i.name & "=\"" & addEscaped(i.value) & "\"")
|
result.add(" " & i.name & "=\"" & addEscaped(i.value) & "\"")
|
||||||
|
|
||||||
if n.childNodes.len() == 0:
|
if n.childNodes == nil or n.childNodes.len() == 0:
|
||||||
result.add("/>") # No idea why this doesn't need a \n :O
|
result.add("/>") # No idea why this doesn't need a \n :O
|
||||||
else:
|
else:
|
||||||
# End the beginning of this tag
|
# End the beginning of this tag
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue