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 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:

    • ParentNode provides a getter and setter on the children property
    • LeafNode provides a getter on the children 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, 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.

    See more

    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 to Node.accept(_:) to start the traversal. The Node object calls the appropriate methods on the Visitor object before calling Node.accept(_:) on its child nodes, performing the recursive traversal.

    The Visitor protocol defines methods that correspond to each of the Node types in the DOM. Types implementing the Visitor 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 ElementSearch class in Search.swift. For a more complex example of a visitor, see the PrettyPrinter class in Formatter.swift.

    See more

    Declaration

    Swift

    public protocol Visitor
  • The Visitable protocol is used to indicate a node can accept a visitor.

    See more

    Declaration

    Swift

    public protocol Visitable
  • Represents a node that cannot have children.

    See more

    Declaration

    Swift

    public protocol LeafNode: Node
  • Represents a node that can have children.

    See more

    Declaration

    Swift

    public protocol ParentNode: Node