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