Parsoid
A bidirectional parser between wikitext and HTML5
Loading...
Searching...
No Matches
Wikimedia\Parsoid\Utils\DOMTraverser Class Reference

Class for helping us traverse the DOM. More...

+ Inheritance diagram for Wikimedia\Parsoid\Utils\DOMTraverser:

Public Member Functions

 __construct (bool $traverseWithTplInfo=false, bool $applyToAttributeEmbeddedHTML=false)
 
 addHandler (?string $nodeName, callable $action)
 Add a handler to the DOM traverser.
 
 traverse (?ParsoidExtensionAPI $extAPI, Node $workNode, ?DTState $state=null)
 Traverse the DOM and fire the handlers that are registered.
 

Detailed Description

Class for helping us traverse the DOM.

This class currently does a pre-order depth-first traversal. See DOMPostOrder for post-order traversal.

Constructor & Destructor Documentation

◆ __construct()

Wikimedia\Parsoid\Utils\DOMTraverser::__construct ( bool $traverseWithTplInfo = false,
bool $applyToAttributeEmbeddedHTML = false )
Parameters
bool$traverseWithTplInfo
bool$applyToAttributeEmbeddedHTML

Member Function Documentation

◆ addHandler()

Wikimedia\Parsoid\Utils\DOMTraverser::addHandler ( ?string $nodeName,
callable $action )

Add a handler to the DOM traverser.

Parameters
?string$nodeNameAn optional node name filter
callable$actionA callback, called on each node we traverse that matches nodeName. Will be called with the following parameters:
  • Node $node: the node being processed
  • Env $env: the parser environment
  • DTState $state: State. Return value: Node|null|true.
  • true: proceed normally
  • Node: traversal will continue on the new node (further handlers will not be called on the current node); after processing it and its siblings, it will continue with the next sibling of the closest ancestor which has one.
  • null: like the Node case, except there is no new node to process before continuing.

◆ traverse()

Wikimedia\Parsoid\Utils\DOMTraverser::traverse ( ?ParsoidExtensionAPI $extAPI,
Node $workNode,
?DTState $state = null )

Traverse the DOM and fire the handlers that are registered.

Handlers can return

  • the next node to process: aborts processing for current node (ie. no further handlers are called) and continues processing on returned node. Essentially, that node and its siblings replace the current node and its siblings for the purposes of the traversal; after they are fully processed, the algorithm moves back to the parent of $workNode to look for the next sibling.
  • null: same as above, except it continues from the next sibling of the parent (or if that does not exist, the next sibling of the grandparent etc). This is so that returning $workNode->nextSibling works even when workNode is a last child of its parent.
  • true: continues regular processing on current node.
Parameters
?ParsoidExtensionAPI$extAPI
Node$workNodeThe starting node for the traversal. The traversal could go beyond the subtree rooted at $workNode if the handlers called during traversal return an arbitrary node elsewhere in the DOM in which case the traversal scope can be pretty much the whole DOM that $workNode is present in. This behavior would be confusing but there is nothing in the traversal code to prevent that.
DTState | null$state

The documentation for this class was generated from the following file: