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] =
|
||||
# Made for getElementsByTagName
|
||||
var r: seq[PNode] = @[]
|
||||
if isNil(nl.childNodes): return @[]
|
||||
if nl.childNodes == nil: return @[]
|
||||
if nl.childNodes.len() == 0: return @[]
|
||||
|
||||
for i in items(nl.childNodes):
|
||||
|
|
@ -176,7 +176,7 @@ proc findNodes(nl: PNode, name: string): seq[PNode] =
|
|||
if i.FNodeName == name or name == "*":
|
||||
r.add(i)
|
||||
|
||||
if not isNil(i.childNodes):
|
||||
if i.childNodes != nil:
|
||||
if i.childNodes.len() != 0:
|
||||
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] =
|
||||
# Made for getElementsByTagNameNS
|
||||
var r: seq[PNode] = @[]
|
||||
if isNil(nl.childNodes): return @[]
|
||||
if nl.childNodes == nil: return @[]
|
||||
if nl.childNodes.len() == 0: return @[]
|
||||
|
||||
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 == "*"):
|
||||
r.add(i)
|
||||
|
||||
if not isNil(i.childNodes):
|
||||
if i.childNodes != nil:
|
||||
if i.childNodes.len() != 0:
|
||||
r.add(findNodesNS(i, namespaceURI, localName))
|
||||
|
||||
|
|
@ -445,7 +445,7 @@ proc importNode*(doc: PDocument, importedNode: PNode, deep: bool): PNode =
|
|||
proc firstChild*(n: PNode): PNode =
|
||||
## Returns this node's first child
|
||||
|
||||
if n.childNodes.len() > 0:
|
||||
if n.childNodes != nil and n.childNodes.len() > 0:
|
||||
return n.childNodes[0]
|
||||
else:
|
||||
return nil
|
||||
|
|
@ -453,7 +453,7 @@ proc firstChild*(n: PNode): PNode =
|
|||
proc lastChild*(n: PNode): PNode =
|
||||
## 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]
|
||||
else:
|
||||
return nil
|
||||
|
|
@ -474,6 +474,8 @@ proc `namespaceURI=`*(n: PNode, value: string) =
|
|||
proc nextSibling*(n: PNode): PNode =
|
||||
## 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 nHigh: int = high(n.FParentNode.childNodes)
|
||||
for i in nLow..nHigh:
|
||||
|
|
@ -504,6 +506,8 @@ proc parentNode*(n: PNode): PNode =
|
|||
proc previousSibling*(n: PNode): PNode =
|
||||
## 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 nHigh: int = high(n.FParentNode.childNodes)
|
||||
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.
|
||||
|
||||
# Check if n contains newChild
|
||||
if not IsNil(n.childNodes):
|
||||
if n.childNodes != nil:
|
||||
for i in low(n.childNodes)..high(n.childNodes):
|
||||
if n.childNodes[i] == newChild:
|
||||
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:
|
||||
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
|
||||
for i in low(n.childNodes)..high(n.childNodes):
|
||||
|
|
@ -586,7 +590,7 @@ proc cloneNode*(n: PNode, deep: bool): PNode =
|
|||
# Import the childNodes
|
||||
var tmp: seq[PNode] = n.childNodes
|
||||
n.childNodes = @[]
|
||||
if deep:
|
||||
if deep and tmp != nil:
|
||||
for i in low(tmp.len())..high(tmp.len()):
|
||||
n.childNodes.add(cloneNode(tmp[i], deep))
|
||||
return newNode
|
||||
|
|
@ -598,11 +602,11 @@ proc cloneNode*(n: PNode, deep: bool): PNode =
|
|||
|
||||
proc hasAttributes*(n: PNode): bool =
|
||||
## 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 =
|
||||
## 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 =
|
||||
## Inserts the node ``newChild`` before the existing child node ``refChild``.
|
||||
|
|
@ -612,10 +616,15 @@ proc insertBefore*(n: PNode, newChild: PNode, refChild: PNode): PNode =
|
|||
if n.FOwnerDocument != newChild.FOwnerDocument:
|
||||
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):
|
||||
if n.childNodes[i] == refChild:
|
||||
n.childNodes.insert(newChild, i - 1)
|
||||
return
|
||||
return
|
||||
|
||||
n.ChildNodes.add(newChild)
|
||||
|
||||
proc isSupported*(n: PNode, feature: string, version: string): bool =
|
||||
## Tests whether the DOM implementation implements a specific
|
||||
|
|
@ -638,7 +647,7 @@ proc normalize*(n: PNode) =
|
|||
|
||||
var newChildNodes: seq[PNode] = @[]
|
||||
while True:
|
||||
if i >= n.childNodes.len:
|
||||
if n.childNodes == nil or i >= n.childNodes.len:
|
||||
break
|
||||
if n.childNodes[i].nodeType == TextNode:
|
||||
|
||||
|
|
@ -662,11 +671,12 @@ proc normalize*(n: PNode) =
|
|||
|
||||
proc removeChild*(n: PNode, oldChild: PNode): PNode =
|
||||
## Removes the child node indicated by ``oldChild`` from the list of children, and returns it.
|
||||
for i in low(n.childNodes)..high(n.childNodes):
|
||||
if n.childNodes[i] == oldChild:
|
||||
result = n.childNodes[i]
|
||||
n.childNodes.delete(i)
|
||||
return result
|
||||
if n.childNodes != nil:
|
||||
for i in low(n.childNodes)..high(n.childNodes):
|
||||
if n.childNodes[i] == oldChild:
|
||||
result = n.childNodes[i]
|
||||
n.childNodes.delete(i)
|
||||
return result
|
||||
|
||||
raise newException(ENotFoundErr, "Node not found")
|
||||
|
||||
|
|
@ -677,11 +687,12 @@ proc replaceChild*(n: PNode, newChild: PNode, oldChild: PNode): PNode =
|
|||
if n.FOwnerDocument != newChild.FOwnerDocument:
|
||||
raise newException(EWrongDocumentErr, "This node belongs to a different document, use importNode.")
|
||||
|
||||
for i in low(n.childNodes)..high(n.childNodes):
|
||||
if n.childNodes[i] == oldChild:
|
||||
result = n.childNodes[i]
|
||||
n.childNodes[i] = newChild
|
||||
return result
|
||||
if n.childNodes != nil:
|
||||
for i in low(n.childNodes)..high(n.childNodes):
|
||||
if n.childNodes[i] == oldChild:
|
||||
result = n.childNodes[i]
|
||||
n.childNodes[i] = newChild
|
||||
return result
|
||||
|
||||
raise newException(ENotFoundErr, "Node not found")
|
||||
|
||||
|
|
@ -745,7 +756,7 @@ proc removeNamedItemNS*(NList: var seq[PNode], namespaceURI: string, localName:
|
|||
proc setNamedItem*(NList: var seq[PNode], arg: PNode): PNode =
|
||||
## 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 not isNil(NList):
|
||||
if NList != nil:
|
||||
if NList.len() > 0:
|
||||
#Check if newChild is from this nodes document
|
||||
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 =
|
||||
## 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 not IsNil(NList):
|
||||
if NList != nil:
|
||||
if NList.len() > 0:
|
||||
# Check if newChild is from this nodes document
|
||||
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 =
|
||||
## Adds a node using its ``namespaceURI`` and ``localName``
|
||||
if not IsNil(NList):
|
||||
if NList != nil:
|
||||
if NList.len() > 0:
|
||||
# Check if newChild is from this nodes document
|
||||
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 =
|
||||
## Adds a node using its ``namespaceURI`` and ``localName``
|
||||
if not isNil(NList):
|
||||
if NList != nil:
|
||||
if NList.len() > 0:
|
||||
# Check if newChild is from this nodes document
|
||||
if NList[0].FOwnerDocument != arg.FOwnerDocument:
|
||||
|
|
@ -873,6 +884,8 @@ proc tagName*(el: PElement): string =
|
|||
# Procedures
|
||||
proc getAttribute*(el: PElement, name: string): string =
|
||||
## Retrieves an attribute value by ``name``
|
||||
if el.attributes == nil:
|
||||
return nil
|
||||
var attribute = el.attributes.getNamedItem(name)
|
||||
if attribute != nil:
|
||||
return attribute.value
|
||||
|
|
@ -881,6 +894,8 @@ proc getAttribute*(el: PElement, name: string): string =
|
|||
|
||||
proc getAttributeNS*(el: PElement, namespaceURI: string, localName: string): string =
|
||||
## Retrieves an attribute value by ``localName`` and ``namespaceURI``
|
||||
if el.attributes == nil:
|
||||
return nil
|
||||
var attribute = el.attributes.getNamedItemNS(namespaceURI, localName)
|
||||
if attribute != nil:
|
||||
return attribute.value
|
||||
|
|
@ -890,10 +905,14 @@ proc getAttributeNS*(el: PElement, namespaceURI: string, localName: string): str
|
|||
proc getAttributeNode*(el: PElement, name: string): PAttr =
|
||||
## Retrieves an attribute node by ``name``
|
||||
## 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)
|
||||
|
||||
proc getAttributeNodeNS*(el: PElement, namespaceURI: string, localName: string): PAttr =
|
||||
## Retrieves an `Attr` node by ``localName`` and ``namespaceURI``
|
||||
if el.attributes == nil:
|
||||
return nil
|
||||
return el.attributes.getNamedItemNS(namespaceURI, localName)
|
||||
|
||||
proc getElementsByTagName*(el: PElement, name: string): seq[PNode] =
|
||||
|
|
@ -911,34 +930,41 @@ proc getElementsByTagNameNS*(el: PElement, namespaceURI: string, localName: stri
|
|||
proc hasAttribute*(el: PElement, name: string): bool =
|
||||
## Returns ``true`` when an attribute with a given ``name`` is specified
|
||||
## on this element , ``false`` otherwise.
|
||||
if el.attributes == nil:
|
||||
return false
|
||||
return el.attributes.getNamedItem(name) != nil
|
||||
|
||||
proc hasAttributeNS*(el: PElement, namespaceURI: string, localName: string): bool =
|
||||
## Returns ``true`` when an attribute with a given ``localName`` and
|
||||
## ``namespaceURI`` is specified on this element , ``false`` otherwise
|
||||
if el.attributes == nil:
|
||||
return false
|
||||
return el.attributes.getNamedItemNS(namespaceURI, localName) != nil
|
||||
|
||||
proc removeAttribute*(el: PElement, name: string) =
|
||||
## Removes an attribute by ``name``
|
||||
for i in low(el.attributes)..high(el.attributes):
|
||||
if el.attributes[i].FName == name:
|
||||
el.attributes.delete(i)
|
||||
if el.attributes != nil:
|
||||
for i in low(el.attributes)..high(el.attributes):
|
||||
if el.attributes[i].FName == name:
|
||||
el.attributes.delete(i)
|
||||
|
||||
proc removeAttributeNS*(el: PElement, namespaceURI: string, localName: string) =
|
||||
## Removes an attribute by ``localName`` and ``namespaceURI``
|
||||
for i in low(el.attributes)..high(el.attributes):
|
||||
if el.attributes[i].FNamespaceURI == namespaceURI and
|
||||
el.attributes[i].FLocalName == localName:
|
||||
el.attributes.delete(i)
|
||||
if el.attributes != nil:
|
||||
for i in low(el.attributes)..high(el.attributes):
|
||||
if el.attributes[i].FNamespaceURI == namespaceURI and
|
||||
el.attributes[i].FLocalName == localName:
|
||||
el.attributes.delete(i)
|
||||
|
||||
proc removeAttributeNode*(el: PElement, oldAttr: PAttr): PAttr =
|
||||
## Removes the specified attribute node
|
||||
## If the attribute node cannot be found raises ``ENotFoundErr``
|
||||
for i in low(el.attributes)..high(el.attributes):
|
||||
if el.attributes[i] == oldAttr:
|
||||
result = el.attributes[i]
|
||||
el.attributes.delete(i)
|
||||
return result
|
||||
if el.attributes != nil:
|
||||
for i in low(el.attributes)..high(el.attributes):
|
||||
if el.attributes[i] == oldAttr:
|
||||
result = el.attributes[i]
|
||||
el.attributes.delete(i)
|
||||
return result
|
||||
|
||||
raise newException(ENotFoundErr, "oldAttr is not a member of el's Attributes")
|
||||
|
||||
|
|
@ -957,7 +983,7 @@ proc setAttributeNode*(el: PElement, newAttr: PAttr): PAttr =
|
|||
"This attribute is in use by another element, use cloneNode")
|
||||
# Exceptions end
|
||||
|
||||
if isNil(el.attributes): el.attributes = @[]
|
||||
if el.attributes == nil: el.attributes = @[]
|
||||
return el.attributes.setNamedItem(newAttr)
|
||||
|
||||
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")
|
||||
# Exceptions end
|
||||
|
||||
if isNil(el.attributes): el.attributes = @[]
|
||||
if el.attributes == nil: el.attributes = @[]
|
||||
return el.attributes.setNamedItemNS(newAttr)
|
||||
|
||||
proc setAttribute*(el: PElement, name: string, value: string) =
|
||||
|
|
@ -1025,7 +1051,7 @@ proc splitData*(TextNode: PText, offset: int): PText =
|
|||
TextNode.data = left
|
||||
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):
|
||||
if TextNode.FParentNode.childNodes[i] == TextNode:
|
||||
var newNode: PText = TextNode.FOwnerDocument.createTextNode(right)
|
||||
|
|
@ -1057,10 +1083,11 @@ proc addEscaped(s: string): string =
|
|||
|
||||
proc nodeToXml(n: PNode, indent: int = 0): string =
|
||||
result = repeatChar(indent, ' ') & "<" & n.nodeName
|
||||
for i in items(n.Attributes):
|
||||
result.add(" " & i.name & "=\"" & addEscaped(i.value) & "\"")
|
||||
if n.attributes != nil:
|
||||
for i in items(n.attributes):
|
||||
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
|
||||
else:
|
||||
# End the beginning of this tag
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue