Protocols
The following protocols are available globally.
-
The
Nodeprotocol is the primary data type for the entire Document Object Model. It represents a single node in the document tree. While all objects implementing theNodeprotocol expose functionality related to children, not all objects implementing theNodeprotocol 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
childrenarray of a leaf node.The attributes
nodeName,nodeValue, andattributesare 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.,nodeValuefor an Element orattributesfor a Comment), this returnsnil.The values of
See morenodeName,nodeValue, andattributesvary 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
Visitorobject is provided toNode.accept(_:)to start the traversal. TheNodeobject calls the appropriate methods on theVisitorobject before callingNode.accept(_:)on its child nodes, performing the recursive traversal.The
Visitorprotocol defines methods that correspond to each of theNodetypes in the DOM. Types implementing theVisitorprotocol 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 moreElementSearchclass inSearch.swift. For a more complex example of a visitor, see thePrettyPrinterclass inFormatter.swift.Declaration
Swift
public protocol Visitor
-
The
See moreVisitableprotocol is used to indicate a node can accept a visitor.Declaration
Swift
public protocol Visitable
View on GitHub
Install in Dash
Protocols Reference