Protocols
The following protocols are available globally.
-
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 theNode
protocol expose functionality related to children, not all objects implementing theNode
protocol may have children. To address this distinction, there are two additional protocols implemented by node types:ParentNode
provides a getter and setter on thechildren
propertyLeafNode
provides a getter on thechildren
property 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
, andattributes
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 specificnodeType
(e.g.,nodeValue
for an Element orattributes
for a Comment), this returnsnil
.The values of
See morenodeName
,nodeValue
, andattributes
vary according to the node type.Declaration
Swift
public protocol Node: Visitable
-
The Visitor Design Pattern is used throughout the MiniDOM library to implement algorithms that involve traversing the DOM tree. It provides a convenient mechanism to separate an algorithm from the object structure on which it operates. It allows operations to be added to the DOM structure without modifying the structures themselves.
A
Visitor
object is provided toNode.accept(_:)
to start the traversal. TheNode
object calls the appropriate methods on theVisitor
object before callingNode.accept(_:)
on its child nodes, performing the recursive traversal.The
Visitor
protocol defines methods that correspond to each of theNode
types in the DOM. Types implementing theVisitor
protocol do not need to deal with the actual traversal; its methods are called by the traversal algorithm provided by the DOM classes.For a simple example of a visitor, see the
See moreElementSearch
class inSearch.swift
. For a more complex example of a visitor, see thePrettyPrinter
class inFormatter.swift
.Declaration
Swift
public protocol Visitor
-
The
See moreVisitable
protocol is used to indicate a node can accept a visitor.Declaration
Swift
public protocol Visitable