Merge pull request #1430 from def-/xmldom-nil
Add some nil checks for xmldom (and clean up a bit)
This commit is contained in:
commit
ee61a39cff
1 changed files with 231 additions and 204 deletions
|
|
@ -228,7 +228,7 @@ proc createAttributeNS*(doc: PDocument, namespaceURI: string, qualifiedName: str
|
|||
raise newException(EInvalidCharacterErr, "Invalid character")
|
||||
# Exceptions
|
||||
if qualifiedName.contains(':'):
|
||||
if namespaceURI == nil:
|
||||
if isNil(namespaceURI):
|
||||
raise newException(ENamespaceErr, "When qualifiedName contains a prefix namespaceURI cannot be nil")
|
||||
elif qualifiedName.split(':')[0].toLower() == "xml" and namespaceURI != "http://www.w3.org/XML/1998/namespace":
|
||||
raise newException(ENamespaceErr,
|
||||
|
|
@ -304,7 +304,7 @@ proc createElement*(doc: PDocument, tagName: string): PElement =
|
|||
proc createElementNS*(doc: PDocument, namespaceURI: string, qualifiedName: string): PElement =
|
||||
## Creates an element of the given qualified name and namespace URI.
|
||||
if qualifiedName.contains(':'):
|
||||
if namespaceURI == nil:
|
||||
if isNIl(namespaceURI):
|
||||
raise newException(ENamespaceErr, "When qualifiedName contains a prefix namespaceURI cannot be nil")
|
||||
elif qualifiedName.split(':')[0].toLower() == "xml" and namespaceURI != "http://www.w3.org/XML/1998/namespace":
|
||||
raise newException(ENamespaceErr,
|
||||
|
|
@ -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 not isNil(n.childNodes) 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 not isNil(n.childNodes) 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 isNil(n.FParentNode) or isNil(n.FParentNode.childNodes):
|
||||
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 isNil(n.FParentNode) or isNil(n.FParentNode.childNodes):
|
||||
return nil
|
||||
var nLow: int = low(n.FParentNode.childNodes)
|
||||
var nHigh: int = high(n.FParentNode.childNodes)
|
||||
for i in nLow..nHigh:
|
||||
|
|
@ -519,7 +523,7 @@ proc `prefix=`*(n: PNode, value: string) =
|
|||
if illegalChars in value:
|
||||
raise newException(EInvalidCharacterErr, "Invalid character")
|
||||
|
||||
if n.FNamespaceURI == nil:
|
||||
if isNil(n.FNamespaceURI):
|
||||
raise newException(ENamespaceErr, "namespaceURI cannot be nil")
|
||||
elif value.toLower() == "xml" and n.FNamespaceURI != "http://www.w3.org/XML/1998/namespace":
|
||||
raise newException(ENamespaceErr,
|
||||
|
|
@ -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 not isNil(n.childNodes):
|
||||
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.")
|
||||
|
|
@ -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 not isNil(tmp):
|
||||
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 not isNil(n.attributes) and n.attributes.len() > 0
|
||||
|
||||
proc hasChildNodes*(n: PNode): bool =
|
||||
## Returns whether this node has any children.
|
||||
return n.childNodes.len() > 0
|
||||
return not isNil(n.childNodes) 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 isNil(n.childNodes):
|
||||
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
|
||||
|
|
@ -624,7 +633,7 @@ proc isSupported*(n: PNode, feature: string, version: string): bool =
|
|||
|
||||
proc isEmpty(s: string): bool =
|
||||
|
||||
if s == "" or s == nil:
|
||||
if isNil(s) or s == "":
|
||||
return True
|
||||
for i in items(s):
|
||||
if i != ' ':
|
||||
|
|
@ -638,7 +647,7 @@ proc normalize*(n: PNode) =
|
|||
|
||||
var newChildNodes: seq[PNode] = @[]
|
||||
while True:
|
||||
if i >= n.childNodes.len:
|
||||
if isNil(n.childNodes) or i >= n.childNodes.len:
|
||||
break
|
||||
if n.childNodes[i].nodeType == TextNode:
|
||||
|
||||
|
|
@ -646,7 +655,7 @@ proc normalize*(n: PNode) =
|
|||
if PText(n.childNodes[i]).data.isEmpty():
|
||||
inc(i)
|
||||
|
||||
if curTextNode == nil:
|
||||
if isNil(curTextNode):
|
||||
curTextNode = n.childNodes[i]
|
||||
else:
|
||||
PText(curTextNode).data.add(PText(n.childNodes[i]).data)
|
||||
|
|
@ -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 not isNil(n.childNodes):
|
||||
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 not isNil(n.childNodes):
|
||||
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")
|
||||
|
||||
|
|
@ -753,7 +764,7 @@ proc setNamedItem*(NList: var seq[PNode], arg: PNode): PNode =
|
|||
#Exceptions End
|
||||
|
||||
var item: PNode = NList.getNamedItem(arg.NodeName())
|
||||
if item == nil:
|
||||
if isNil(item):
|
||||
NList.add(arg)
|
||||
return nil
|
||||
else:
|
||||
|
|
@ -769,18 +780,18 @@ 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 not isNil(NList):
|
||||
if NList.len() > 0:
|
||||
# Check if newChild is from this nodes document
|
||||
if NList[0].FOwnerDocument != arg.FOwnerDocument:
|
||||
raise newException(EWrongDocumentErr, "This node belongs to a different document, use importNode.")
|
||||
|
||||
if arg.FOwnerElement != nil:
|
||||
if not isNil(arg.FOwnerElement):
|
||||
raise newException(EInuseAttributeErr, "This attribute is in use by another element, use cloneNode")
|
||||
|
||||
# Exceptions end
|
||||
var item: PAttr = NList.getNamedItem(arg.nodeName())
|
||||
if item == nil:
|
||||
if isNil(item):
|
||||
NList.add(arg)
|
||||
return nil
|
||||
else:
|
||||
|
|
@ -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 not isNil(NList):
|
||||
if NList.len() > 0:
|
||||
# Check if newChild is from this nodes document
|
||||
if NList[0].FOwnerDocument != arg.FOwnerDocument:
|
||||
|
|
@ -803,7 +814,7 @@ proc setNamedItemNS*(NList: var seq[PNode], arg: PNode): PNode =
|
|||
#Exceptions end
|
||||
|
||||
var item: PNode = NList.getNamedItemNS(arg.namespaceURI(), arg.localName())
|
||||
if item == nil:
|
||||
if isNil(item):
|
||||
NList.add(arg)
|
||||
return nil
|
||||
else:
|
||||
|
|
@ -824,12 +835,12 @@ proc setNamedItemNS*(NList: var seq[PAttr], arg: PAttr): PAttr =
|
|||
if NList[0].FOwnerDocument != arg.FOwnerDocument:
|
||||
raise newException(EWrongDocumentErr, "This node belongs to a different document, use importNode.")
|
||||
|
||||
if arg.FOwnerElement != nil:
|
||||
if not isNil(arg.FOwnerElement):
|
||||
raise newException(EInuseAttributeErr, "This attribute is in use by another element, use cloneNode")
|
||||
|
||||
# Exceptions end
|
||||
var item: PAttr = NList.getNamedItemNS(arg.namespaceURI(), arg.localName())
|
||||
if item == nil:
|
||||
if isNil(item):
|
||||
NList.add(arg)
|
||||
return nil
|
||||
else:
|
||||
|
|
@ -873,16 +884,20 @@ proc tagName*(el: PElement): string =
|
|||
# Procedures
|
||||
proc getAttribute*(el: PElement, name: string): string =
|
||||
## Retrieves an attribute value by ``name``
|
||||
if isNil(el.attributes):
|
||||
return nil
|
||||
var attribute = el.attributes.getNamedItem(name)
|
||||
if attribute != nil:
|
||||
if not isNil(attribute):
|
||||
return attribute.value
|
||||
else:
|
||||
return nil
|
||||
|
||||
proc getAttributeNS*(el: PElement, namespaceURI: string, localName: string): string =
|
||||
## Retrieves an attribute value by ``localName`` and ``namespaceURI``
|
||||
if isNil(el.attributes):
|
||||
return nil
|
||||
var attribute = el.attributes.getNamedItemNS(namespaceURI, localName)
|
||||
if attribute != nil:
|
||||
if not isNil(attribute):
|
||||
return attribute.value
|
||||
else:
|
||||
return nil
|
||||
|
|
@ -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 isNil(el.attributes):
|
||||
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 isNil(el.attributes):
|
||||
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.
|
||||
return el.attributes.getNamedItem(name) != nil
|
||||
if isNil(el.attributes):
|
||||
return false
|
||||
return not isNil(el.attributes.getNamedItem(name))
|
||||
|
||||
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
|
||||
return el.attributes.getNamedItemNS(namespaceURI, localName) != nil
|
||||
if isNil(el.attributes):
|
||||
return false
|
||||
return not isNil(el.attributes.getNamedItemNS(namespaceURI, localName))
|
||||
|
||||
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 not isNil(el.attributes):
|
||||
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 not isNil(el.attributes):
|
||||
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 not isNil(el.attributes):
|
||||
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")
|
||||
|
||||
|
|
@ -952,7 +978,7 @@ proc setAttributeNode*(el: PElement, newAttr: PAttr): PAttr =
|
|||
raise newException(EWrongDocumentErr,
|
||||
"This node belongs to a different document, use importNode.")
|
||||
|
||||
if newAttr.FOwnerElement != nil:
|
||||
if not isNil(newAttr.FOwnerElement):
|
||||
raise newException(EInuseAttributeErr,
|
||||
"This attribute is in use by another element, use cloneNode")
|
||||
# Exceptions end
|
||||
|
|
@ -970,7 +996,7 @@ proc setAttributeNodeNS*(el: PElement, newAttr: PAttr): PAttr =
|
|||
raise newException(EWrongDocumentErr,
|
||||
"This node belongs to a different document, use importNode.")
|
||||
|
||||
if newAttr.FOwnerElement != nil:
|
||||
if not isNil(newAttr.FOwnerElement):
|
||||
raise newException(EInuseAttributeErr,
|
||||
"This attribute is in use by another element, use cloneNode")
|
||||
# Exceptions end
|
||||
|
|
@ -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 not isNil(TextNode.FParentNode) and not isNil(TextNode.FParentNode.childNodes):
|
||||
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 not isNil(n.attributes):
|
||||
for i in items(n.attributes):
|
||||
result.add(" " & i.name & "=\"" & addEscaped(i.value) & "\"")
|
||||
|
||||
if n.childNodes.len() == 0:
|
||||
if isNil(n.childNodes) 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