Node
public protocol Node: Visitable
The Node protocol is the primary data type for the entire Document Object
Model. It represents a single node in the document tree. While all objects
implementing the Node protocol expose functionality related to children, not
all objects implementing the Node protocol may have children. To address this
distinction, there are two additional protocols implemented by node types:
ParentNodeprovides a getter and setter on thechildrenpropertyLeafNodeprovides a getter on thechildrenproperty that always returns an empty array.
This is a departure from the standard DOM which would throw an error when
attempting to modify the children array of a leaf node.
The attributes nodeName, nodeValue, and attributes are included as a
mechanism to get at node information without casting down to the specific
derived type. In cases where there is no obvious mapping of these attributes
for a specific nodeType (e.g., nodeValue for an Element or attributes for
a Comment), this returns nil.
The values of nodeName, nodeValue, and attributes vary according to the
node type.
-
nodeTypeDefault implementationIndicates which type of node this is.
Default Implementation
Convenience accessor for the static
nodeTypeproperty.Declaration
Swift
static var nodeType: NodeType -
The value of this node, depending on its type.
See also
Text.nodeValueSee also
Comment.nodeValueSee also
CDATASection.nodeValueDeclaration
Swift
var nodeValue: String? -
The children of this node.
See also
Text.childrenSee also
ProcessingInstruction.childrenSee also
Comment.childrenSee also
CDATASection.childrenDeclaration
Swift
var children: [Node] -
Puts all
Textnodes in the full depth of the sub-tree underneath thisNode, into anormal
form where only structure (e.g., elements, comments, processing instructions, and CDATA sections) separatesTextnodes, i.e., there are neither adjacentTextnodes nor emptyTextnodes. This can be used to ensure that the DOM view of a document is the same as if it were saved and re-loaded.Declaration
Swift
mutating func normalize()
-
evaluate(path:)Extension methodSelects nodes based on a path relative to this node. For example, in the following document:
<a id="1"> <b id="2"> <c id="3"/> </b> <c id="4"> <d id="5"/> </c> </a>evaluating the path
["a", "b", "c"]relative to the document would select the<c>element withid="3"but not the<c>element withid="4".In this example, starting at the document object (the parent of the root
<a>element), select all children withnodeName == "a". From that set of nodes (withnodeName == "a"), select all children withnodeName == "b". Finally, from that set of nodes (withnodeName == "b"that are children of nodes withnodeName == "a"), select all children withnodeName == "c".Declaration
Swift
public final func evaluate(path: [String]) -> [Node]Parameters
pathAn array of strings, each representing a
nodeNamein the path.Return Value
An array of nodes corresponding to the specified path, relative to this node.
-
prettyPrint(indentWith:)Extension methodGenerates a formatted XML string representation of this node and its descendants.
Declaration
Swift
public func prettyPrint(indentWith: String = "\t") -> String?Parameters
indentWithThe string used to indent the formatted string.
Return Value
A formatted XML string representation of this node and its descendants.
-
dump()Extension methodGenerates an unformatted XML string representation of this node and its descendants.
Declaration
Swift
public func dump() -> String?Return Value
A formatted XML string representation of this node and its descendants.
-
children(ofType:)Extension methodFilters the
childrenarray, keeping only nodes of the specified type. Casts the nodes in the resulting array to the specified type.Declaration
Swift
public final func children<T: Node>(ofType type: T.Type) -> [T]Parameters
typeInclude children of this type in the resulting array
Return Value
The nodes in the
childrenarray of the specified type -
hasChildrenExtension methodA Boolean value indicating whether the
childrenarray is not empty.Declaration
Swift
public final var hasChildren: Bool -
firstChildExtension methodThe first node in the
childrenarray.Declaration
Swift
public final var firstChild: Node? -
lastChildExtension methodThe last node in the
childrenarray.Declaration
Swift
public final var lastChild: Node? -
children(withName:)Extension methodReturns an array of children with the given
nodeName.Declaration
Swift
public final func children(withName name: String) -> [Node]Parameters
nameThe node name to find.
Return Value
The children with the given node name.
-
childElementsExtension method -
hasChildElementsExtension methodA Boolean value indicating whether the
childElementsarray is not emptyDeclaration
Swift
public final var hasChildElements: Bool -
firstChildElementExtension methodThe first element in the
childElementsarray.Declaration
Swift
public final var firstChildElement: Element? -
lastChildElementExtension methodThe last element in the
childElementsarray.Declaration
Swift
public final var lastChildElement: Element? -
childElements(withName:)Extension method
View on GitHub
Install in Dash
Node Protocol Reference