Visitor

public protocol Visitor

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.

  • beginVisit(_:) Default implementation

    Called when the traversal algorithm encounters a Document object before its children are traversed.

    Visiting a Document object is split into a begin and end method to allow implementing operations before and after the children of the Document object are visited.

    Default Implementation

    Default implementation does nothing.

    Default implementation does nothing.

    Declaration

    Swift

    func beginVisit(_ document: Document)
  • endVisit(_:) Default implementation

    Called when the traversal algorithm encounters a Document object after its children are traversed.

    Visiting a Document object is split into a begin and end method to allow implementing operations before and after the children of the Document object are visited.

    Default Implementation

    Default implementation does nothing.

    Default implementation does nothing.

    Declaration

    Swift

    func endVisit(_ document: Document)
  • Called when the traversal algorithm encounters an Element object before its children are traversed.

    Visiting an Element object is split into a begin and end method to allow implementing operations before and after the children of the Element object are visited.

    Declaration

    Swift

    func beginVisit(_ element: Element)
  • Called when the traversal algorithm encounters an Element object after its children are traversed.

    Visiting an Element object is split into a begin and end method to allow implementing operations before and after the children of the Element object are visited.

    Declaration

    Swift

    func endVisit(_ element: Element)
  • visit(_:) Default implementation

    Called when the traversal algorithm encounters a Text object.

    Default Implementation

    Default implementation does nothing.

    Default implementation does nothing.

    Default implementation does nothing.

    Default implementation does nothing.

    Declaration

    Swift

    func visit(_ text: Text)
  • Called when the traversal algorithm encounters a ProcessingInstruction object.

    Declaration

    Swift

    func visit(_ processingInstruction: ProcessingInstruction)
  • Called when the traversal algorithm encounters a Comment object.

    Declaration

    Swift

    func visit(_ comment: Comment)
  • Called when the traversal algorithm encounters an CDATASection object.

    Declaration

    Swift

    func visit(_ cdataSection: CDATASection)