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 implementationCalled when the traversal algorithm encounters a
Document
object before itschildren
are traversed.Visiting a
Document
object is split into a begin and end method to allow implementing operations before and after thechildren
of theDocument
object are visited.Default Implementation
Default implementation does nothing.
Default implementation does nothing.
Declaration
Swift
func beginVisit(_ document: Document)
-
endVisit(_:)
Default implementationCalled when the traversal algorithm encounters a
Document
object after itschildren
are traversed.Visiting a
Document
object is split into a begin and end method to allow implementing operations before and after thechildren
of theDocument
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 itschildren
are traversed.Visiting an
Element
object is split into a begin and end method to allow implementing operations before and after thechildren
of theElement
object are visited.Declaration
Swift
func beginVisit(_ element: Element)
-
Called when the traversal algorithm encounters an
Element
object after itschildren
are traversed.Visiting an
Element
object is split into a begin and end method to allow implementing operations before and after thechildren
of theElement
object are visited.Declaration
Swift
func endVisit(_ element: Element)
-
visit(_:)
Default implementation -
Called when the traversal algorithm encounters a
ProcessingInstruction
object.Declaration
Swift
func visit(_ processingInstruction: ProcessingInstruction)
-
Called when the traversal algorithm encounters an
CDATASection
object.Declaration
Swift
func visit(_ cdataSection: CDATASection)