MastermindsHTML5SerializerTraverser

Traverser for walking a DOM tree.

Defined (1)

The class is defined in the following location(s).

/vendor/masterminds/html5/src/HTML5/Serializer/Traverser.php  
  1. class Traverser 
  2.  
  3. /** 
  4. * Namespaces that should be treated as "local" to HTML5. 
  5. */ 
  6. static $local_ns = array( 
  7. 'http://www.w3.org/1999/xhtml' => 'html',  
  8. 'http://www.w3.org/1998/Math/MathML' => 'math',  
  9. 'http://www.w3.org/2000/svg' => 'svg' 
  10. ); 
  11.  
  12. protected $dom; 
  13.  
  14. protected $options; 
  15.  
  16. protected $encode = false; 
  17.  
  18. protected $rules; 
  19.  
  20. protected $out; 
  21.  
  22. /** 
  23. * Create a traverser. 
  24. * @param DOMNode|DOMNodeList $dom 
  25. * The document or node to traverse. 
  26. * @param resource $out 
  27. * A stream that allows writing. The traverser will output into this 
  28. * stream. 
  29. * @param array $options 
  30. * An array or options for the traverser as key/value pairs. These include: 
  31. * - encode_entities: A bool to specify if full encding should happen for all named 
  32. * charachter references. Defaults to false which escapes &'<>". 
  33. * - output_rules: The path to the class handling the output rules. 
  34. */ 
  35. public function __construct($dom, $out, RulesInterface $rules, $options = array()) 
  36. $this->dom = $dom; 
  37. $this->out = $out; 
  38. $this->rules = $rules; 
  39. $this->options = $options; 
  40.  
  41. $this->rules->setTraverser($this); 
  42.  
  43. /** 
  44. * Tell the traverser to walk the DOM. 
  45. * @return resource $out 
  46. * Returns the output stream. 
  47. */ 
  48. public function walk() 
  49. if ($this->dom instanceof \DOMDocument) { 
  50. $this->rules->document($this->dom); 
  51. } elseif ($this->dom instanceof \DOMDocumentFragment) { 
  52. // Document fragments are a special case. Only the children need to 
  53. // be serialized. 
  54. if ($this->dom->hasChildNodes()) { 
  55. $this->children($this->dom->childNodes); 
  56. } // If NodeList, loop 
  57. elseif ($this->dom instanceof \DOMNodeList) { 
  58. // If this is a NodeList of DOMDocuments this will not work. 
  59. $this->children($this->dom); 
  60. } // Else assume this is a DOMNode-like datastructure. 
  61. else { 
  62. $this->node($this->dom); 
  63.  
  64. return $this->out; 
  65.  
  66. /** 
  67. * Process a node in the DOM. 
  68. * @param mixed $node 
  69. * A node implementing \DOMNode. 
  70. */ 
  71. public function node($node) 
  72. // A listing of types is at http://php.net/manual/en/dom.constants.php 
  73. switch ($node->nodeType) { 
  74. case XML_ELEMENT_NODE: 
  75. $this->rules->element($node); 
  76. break; 
  77. case XML_TEXT_NODE: 
  78. $this->rules->text($node); 
  79. break; 
  80. case XML_CDATA_SECTION_NODE: 
  81. $this->rules->cdata($node); 
  82. break; 
  83. case XML_PI_NODE: 
  84. $this->rules->processorInstruction($node); 
  85. break; 
  86. case XML_COMMENT_NODE: 
  87. $this->rules->comment($node); 
  88. break; 
  89. // Currently we don't support embedding DTDs. 
  90. default: 
  91. //print '<!-- Skipped -->'; 
  92. break; 
  93.  
  94. /** 
  95. * Walk through all the nodes on a node list. 
  96. * @param \DOMNodeList $nl 
  97. * A list of child elements to walk through. 
  98. */ 
  99. public function children($nl) 
  100. foreach ($nl as $node) { 
  101. $this->node($node); 
  102.  
  103. /** 
  104. * Is an element local? 
  105. * @param mixed $ele 
  106. * An element that implement \DOMNode. 
  107. * @return bool True if local and false otherwise. 
  108. */ 
  109. public function isLocalElement($ele) 
  110. $uri = $ele->namespaceURI; 
  111. if (empty($uri)) { 
  112. return false; 
  113.  
  114. return isset(static::$local_ns[$uri]);