| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @file |
| 5 |
* The rules for generating output in the serializer. |
| 6 |
* |
| 7 |
* These output rules are likely to generate output similar to the document that |
| 8 |
* was parsed. It is not intended to output exactly the document that was parsed. |
| 9 |
*/ |
| 10 |
namespace WindPressPackages\Masterminds\HTML5\Serializer; |
| 11 |
|
| 12 |
use WindPressPackages\Masterminds\HTML5\Elements; |
| 13 |
/** |
| 14 |
* Generate the output html5 based on element rules. |
| 15 |
*/ |
| 16 |
class OutputRules implements RulesInterface |
| 17 |
{ |
| 18 |
/** |
| 19 |
* Defined in http://www.w3.org/TR/html51/infrastructure.html#html-namespace-0. |
| 20 |
*/ |
| 21 |
const NAMESPACE_HTML = 'http://www.w3.org/1999/xhtml'; |
| 22 |
const NAMESPACE_MATHML = 'http://www.w3.org/1998/Math/MathML'; |
| 23 |
const NAMESPACE_SVG = 'http://www.w3.org/2000/svg'; |
| 24 |
const NAMESPACE_XLINK = 'http://www.w3.org/1999/xlink'; |
| 25 |
const NAMESPACE_XML = 'http://www.w3.org/XML/1998/namespace'; |
| 26 |
const NAMESPACE_XMLNS = 'http://www.w3.org/2000/xmlns/'; |
| 27 |
/** |
| 28 |
* Holds the HTML5 element names that causes a namespace switch. |
| 29 |
* |
| 30 |
* @var array |
| 31 |
*/ |
| 32 |
protected $implicitNamespaces = array(self::NAMESPACE_HTML, self::NAMESPACE_SVG, self::NAMESPACE_MATHML, self::NAMESPACE_XML, self::NAMESPACE_XMLNS); |
| 33 |
const IM_IN_HTML = 1; |
| 34 |
const IM_IN_SVG = 2; |
| 35 |
const IM_IN_MATHML = 3; |
| 36 |
/** |
| 37 |
* Used as cache to detect if is available ENT_HTML5. |
| 38 |
* |
| 39 |
* @var bool |
| 40 |
*/ |
| 41 |
private $hasHTML5 = \false; |
| 42 |
protected $traverser; |
| 43 |
protected $encode = \false; |
| 44 |
protected $out; |
| 45 |
protected $outputMode; |
| 46 |
private $xpath; |
| 47 |
protected $nonBooleanAttributes = array( |
| 48 |
/* |
| 49 |
array( |
| 50 |
'nodeNamespace'=>'http://www.w3.org/1999/xhtml', |
| 51 |
'attrNamespace'=>'http://www.w3.org/1999/xhtml', |
| 52 |
|
| 53 |
'nodeName'=>'img', 'nodeName'=>array('img', 'a'), |
| 54 |
'attrName'=>'alt', 'attrName'=>array('title', 'alt'), |
| 55 |
), |
| 56 |
*/ |
| 57 |
array('nodeNamespace' => 'http://www.w3.org/1999/xhtml', 'attrName' => array('href', 'hreflang', 'http-equiv', 'icon', 'id', 'keytype', 'kind', 'label', 'lang', 'language', 'list', 'maxlength', 'media', 'method', 'name', 'placeholder', 'rel', 'rows', 'rowspan', 'sandbox', 'spellcheck', 'scope', 'seamless', 'shape', 'size', 'sizes', 'span', 'src', 'srcdoc', 'srclang', 'srcset', 'start', 'step', 'style', 'summary', 'tabindex', 'target', 'title', 'type', 'value', 'width', 'border', 'charset', 'cite', 'class', 'code', 'codebase', 'color', 'cols', 'colspan', 'content', 'coords', 'data', 'datetime', 'default', 'dir', 'dirname', 'enctype', 'for', 'form', 'formaction', 'headers', 'height', 'accept', 'accept-charset', 'accesskey', 'action', 'align', 'alt', 'bgcolor')), |
| 58 |
array('nodeNamespace' => 'http://www.w3.org/1999/xhtml', 'xpath' => 'starts-with(local-name(), \'data-\')'), |
| 59 |
); |
| 60 |
const DOCTYPE = '<!DOCTYPE html>'; |
| 61 |
public function __construct($output, $options = array()) |
| 62 |
{ |
| 63 |
if (isset($options['encode_entities'])) { |
| 64 |
$this->encode = $options['encode_entities']; |
| 65 |
} |
| 66 |
$this->outputMode = static::IM_IN_HTML; |
| 67 |
$this->out = $output; |
| 68 |
$this->hasHTML5 = \defined('ENT_HTML5'); |
| 69 |
} |
| 70 |
public function addRule(array $rule) |
| 71 |
{ |
| 72 |
$this->nonBooleanAttributes[] = $rule; |
| 73 |
} |
| 74 |
public function setTraverser(Traverser $traverser) |
| 75 |
{ |
| 76 |
$this->traverser = $traverser; |
| 77 |
return $this; |
| 78 |
} |
| 79 |
public function unsetTraverser() |
| 80 |
{ |
| 81 |
$this->traverser = null; |
| 82 |
return $this; |
| 83 |
} |
| 84 |
public function document($dom) |
| 85 |
{ |
| 86 |
$this->doctype(); |
| 87 |
if ($dom->documentElement) { |
| 88 |
foreach ($dom->childNodes as $node) { |
| 89 |
$this->traverser->node($node); |
| 90 |
} |
| 91 |
$this->nl(); |
| 92 |
} |
| 93 |
} |
| 94 |
protected function doctype() |
| 95 |
{ |
| 96 |
$this->wr(static::DOCTYPE); |
| 97 |
$this->nl(); |
| 98 |
} |
| 99 |
public function element($ele) |
| 100 |
{ |
| 101 |
$name = $ele->tagName; |
| 102 |
// Per spec: |
| 103 |
// If the element has a declared namespace in the HTML, MathML or |
| 104 |
// SVG namespaces, we use the lname instead of the tagName. |
| 105 |
if ($this->traverser->isLocalElement($ele)) { |
| 106 |
$name = $ele->localName; |
| 107 |
} |
| 108 |
// If we are in SVG or MathML there is special handling. |
| 109 |
// Using if/elseif instead of switch because it's faster in PHP. |
| 110 |
if ('svg' == $name) { |
| 111 |
$this->outputMode = static::IM_IN_SVG; |
| 112 |
$name = Elements::normalizeSvgElement($name); |
| 113 |
} elseif ('math' == $name) { |
| 114 |
$this->outputMode = static::IM_IN_MATHML; |
| 115 |
} |
| 116 |
$this->openTag($ele); |
| 117 |
if (Elements::isA($name, Elements::TEXT_RAW)) { |
| 118 |
foreach ($ele->childNodes as $child) { |
| 119 |
if ($child instanceof \DOMCharacterData) { |
| 120 |
$this->wr($child->data); |
| 121 |
} elseif ($child instanceof \DOMElement) { |
| 122 |
$this->element($child); |
| 123 |
} |
| 124 |
} |
| 125 |
} else { |
| 126 |
// Handle children. |
| 127 |
if ($ele->hasChildNodes()) { |
| 128 |
$this->traverser->children($ele->childNodes); |
| 129 |
} |
| 130 |
// Close out the SVG or MathML special handling. |
| 131 |
if ('svg' == $name || 'math' == $name) { |
| 132 |
$this->outputMode = static::IM_IN_HTML; |
| 133 |
} |
| 134 |
} |
| 135 |
// If not unary, add a closing tag. |
| 136 |
if (!Elements::isA($name, Elements::VOID_TAG)) { |
| 137 |
$this->closeTag($ele); |
| 138 |
} |
| 139 |
} |
| 140 |
/** |
| 141 |
* Write a text node. |
| 142 |
* |
| 143 |
* @param \DOMText $ele The text node to write. |
| 144 |
*/ |
| 145 |
public function text($ele) |
| 146 |
{ |
| 147 |
if (isset($ele->parentNode) && isset($ele->parentNode->tagName) && Elements::isA($ele->parentNode->localName, Elements::TEXT_RAW)) { |
| 148 |
$this->wr($ele->data); |
| 149 |
return; |
| 150 |
} |
| 151 |
// FIXME: This probably needs some flags set. |
| 152 |
$this->wr($this->enc($ele->data)); |
| 153 |
} |
| 154 |
public function cdata($ele) |
| 155 |
{ |
| 156 |
// This encodes CDATA. |
| 157 |
$this->wr($ele->ownerDocument->saveXML($ele)); |
| 158 |
} |
| 159 |
public function comment($ele) |
| 160 |
{ |
| 161 |
// These produce identical output. |
| 162 |
// $this->wr('<!--')->wr($ele->data)->wr('-->'); |
| 163 |
$this->wr($ele->ownerDocument->saveXML($ele)); |
| 164 |
} |
| 165 |
public function processorInstruction($ele) |
| 166 |
{ |
| 167 |
$this->wr('<?')->wr($ele->target)->wr(' ')->wr($ele->data)->wr('?>'); |
| 168 |
} |
| 169 |
/** |
| 170 |
* Write the namespace attributes. |
| 171 |
* |
| 172 |
* @param \DOMNode $ele The element being written. |
| 173 |
*/ |
| 174 |
protected function namespaceAttrs($ele) |
| 175 |
{ |
| 176 |
if (!$this->xpath || $this->xpath->document !== $ele->ownerDocument) { |
| 177 |
$this->xpath = new \DOMXPath($ele->ownerDocument); |
| 178 |
} |
| 179 |
foreach ($this->xpath->query('namespace::*[not(.=../../namespace::*)]', $ele) as $nsNode) { |
| 180 |
if (!\in_array($nsNode->nodeValue, $this->implicitNamespaces)) { |
| 181 |
$this->wr(' ')->wr($nsNode->nodeName)->wr('="')->wr($nsNode->nodeValue)->wr('"'); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
/** |
| 186 |
* Write the opening tag. |
| 187 |
* |
| 188 |
* Tags for HTML, MathML, and SVG are in the local name. Otherwise, use the |
| 189 |
* qualified name (8.3). |
| 190 |
* |
| 191 |
* @param \DOMNode $ele The element being written. |
| 192 |
*/ |
| 193 |
protected function openTag($ele) |
| 194 |
{ |
| 195 |
$this->wr('<')->wr($this->traverser->isLocalElement($ele) ? $ele->localName : $ele->tagName); |
| 196 |
$this->attrs($ele); |
| 197 |
$this->namespaceAttrs($ele); |
| 198 |
if ($this->outputMode == static::IM_IN_HTML) { |
| 199 |
$this->wr('>'); |
| 200 |
} else { |
| 201 |
if ($ele->hasChildNodes()) { |
| 202 |
$this->wr('>'); |
| 203 |
} else { |
| 204 |
$this->wr(' />'); |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
protected function attrs($ele) |
| 209 |
{ |
| 210 |
// FIXME: Needs support for xml, xmlns, xlink, and namespaced elements. |
| 211 |
if (!$ele->hasAttributes()) { |
| 212 |
return $this; |
| 213 |
} |
| 214 |
// TODO: Currently, this always writes name="value", and does not do |
| 215 |
// value-less attributes. |
| 216 |
$map = $ele->attributes; |
| 217 |
$len = $map->length; |
| 218 |
for ($i = 0; $i < $len; ++$i) { |
| 219 |
$node = $map->item($i); |
| 220 |
$val = $this->enc($node->value, \true); |
| 221 |
// XXX: The spec says that we need to ensure that anything in |
| 222 |
// the XML, XMLNS, or XLink NS's should use the canonical |
| 223 |
// prefix. It seems that DOM does this for us already, but there |
| 224 |
// may be exceptions. |
| 225 |
$name = $node->nodeName; |
| 226 |
// Special handling for attributes in SVG and MathML. |
| 227 |
// Using if/elseif instead of switch because it's faster in PHP. |
| 228 |
if ($this->outputMode == static::IM_IN_SVG) { |
| 229 |
$name = Elements::normalizeSvgAttribute($name); |
| 230 |
} elseif ($this->outputMode == static::IM_IN_MATHML) { |
| 231 |
$name = Elements::normalizeMathMlAttribute($name); |
| 232 |
} |
| 233 |
$this->wr(' ')->wr($name); |
| 234 |
if (isset($val) && '' !== $val || $this->nonBooleanAttribute($node)) { |
| 235 |
$this->wr('="')->wr($val)->wr('"'); |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
protected function nonBooleanAttribute(\DOMAttr $attr) |
| 240 |
{ |
| 241 |
$ele = $attr->ownerElement; |
| 242 |
foreach ($this->nonBooleanAttributes as $rule) { |
| 243 |
if (isset($rule['nodeNamespace']) && $rule['nodeNamespace'] !== $ele->namespaceURI) { |
| 244 |
continue; |
| 245 |
} |
| 246 |
if (isset($rule['attNamespace']) && $rule['attNamespace'] !== $attr->namespaceURI) { |
| 247 |
continue; |
| 248 |
} |
| 249 |
if (isset($rule['nodeName']) && !\is_array($rule['nodeName']) && $rule['nodeName'] !== $ele->localName) { |
| 250 |
continue; |
| 251 |
} |
| 252 |
if (isset($rule['nodeName']) && \is_array($rule['nodeName']) && !\in_array($ele->localName, $rule['nodeName'], \true)) { |
| 253 |
continue; |
| 254 |
} |
| 255 |
if (isset($rule['attrName']) && !\is_array($rule['attrName']) && $rule['attrName'] !== $attr->localName) { |
| 256 |
continue; |
| 257 |
} |
| 258 |
if (isset($rule['attrName']) && \is_array($rule['attrName']) && !\in_array($attr->localName, $rule['attrName'], \true)) { |
| 259 |
continue; |
| 260 |
} |
| 261 |
if (isset($rule['xpath'])) { |
| 262 |
$xp = $this->getXPath($attr); |
| 263 |
if (isset($rule['prefixes'])) { |
| 264 |
foreach ($rule['prefixes'] as $nsPrefix => $ns) { |
| 265 |
$xp->registerNamespace($nsPrefix, $ns); |
| 266 |
} |
| 267 |
} |
| 268 |
if (!$xp->evaluate($rule['xpath'], $attr)) { |
| 269 |
continue; |
| 270 |
} |
| 271 |
} |
| 272 |
return \true; |
| 273 |
} |
| 274 |
return \false; |
| 275 |
} |
| 276 |
private function getXPath(\DOMNode $node) |
| 277 |
{ |
| 278 |
if (!$this->xpath) { |
| 279 |
$this->xpath = new \DOMXPath($node->ownerDocument); |
| 280 |
} |
| 281 |
return $this->xpath; |
| 282 |
} |
| 283 |
/** |
| 284 |
* Write the closing tag. |
| 285 |
* |
| 286 |
* Tags for HTML, MathML, and SVG are in the local name. Otherwise, use the |
| 287 |
* qualified name (8.3). |
| 288 |
* |
| 289 |
* @param \DOMNode $ele The element being written. |
| 290 |
*/ |
| 291 |
protected function closeTag($ele) |
| 292 |
{ |
| 293 |
if ($this->outputMode == static::IM_IN_HTML || $ele->hasChildNodes()) { |
| 294 |
$this->wr('</')->wr($this->traverser->isLocalElement($ele) ? $ele->localName : $ele->tagName)->wr('>'); |
| 295 |
} |
| 296 |
} |
| 297 |
/** |
| 298 |
* Write to the output. |
| 299 |
* |
| 300 |
* @param string $text The string to put into the output |
| 301 |
* |
| 302 |
* @return $this |
| 303 |
*/ |
| 304 |
protected function wr($text) |
| 305 |
{ |
| 306 |
\fwrite($this->out, $text); |
| 307 |
return $this; |
| 308 |
} |
| 309 |
/** |
| 310 |
* Write a new line character. |
| 311 |
* |
| 312 |
* @return $this |
| 313 |
*/ |
| 314 |
protected function nl() |
| 315 |
{ |
| 316 |
\fwrite($this->out, \PHP_EOL); |
| 317 |
return $this; |
| 318 |
} |
| 319 |
/** |
| 320 |
* Encode text. |
| 321 |
* |
| 322 |
* When encode is set to false, the default value, the text passed in is |
| 323 |
* escaped per section 8.3 of the html5 spec. For details on how text is |
| 324 |
* escaped see the escape() method. |
| 325 |
* |
| 326 |
* When encoding is set to true the text is converted to named character |
| 327 |
* references where appropriate. Section 8.1.4 Character references of the |
| 328 |
* html5 spec refers to using named character references. This is useful for |
| 329 |
* characters that can't otherwise legally be used in the text. |
| 330 |
* |
| 331 |
* The named character references are listed in section 8.5. |
| 332 |
* |
| 333 |
* @see http://www.w3.org/TR/2013/CR-html5-20130806/syntax.html#named-character-references True encoding will turn all named character references into their entities. |
| 334 |
* This includes such characters as +.# and many other common ones. By default |
| 335 |
* encoding here will just escape &'<>". |
| 336 |
* |
| 337 |
* Note, PHP 5.4+ has better html5 encoding. |
| 338 |
* |
| 339 |
* @todo Use the Entities class in php 5.3 to have html5 entities. |
| 340 |
* |
| 341 |
* @param string $text Text to encode. |
| 342 |
* @param bool $attribute True if we are encoding an attrubute, false otherwise. |
| 343 |
* |
| 344 |
* @return string The encoded text. |
| 345 |
*/ |
| 346 |
protected function enc($text, $attribute = \false) |
| 347 |
{ |
| 348 |
// Escape the text rather than convert to named character references. |
| 349 |
if (!$this->encode) { |
| 350 |
return $this->escape($text, $attribute); |
| 351 |
} |
| 352 |
// If we are in PHP 5.4+ we can use the native html5 entity functionality to |
| 353 |
// convert the named character references. |
| 354 |
if ($this->hasHTML5) { |
| 355 |
return \htmlentities($text, \ENT_HTML5 | \ENT_SUBSTITUTE | \ENT_QUOTES, 'UTF-8', \false); |
| 356 |
} else { |
| 357 |
return \strtr($text, HTML5Entities::$map); |
| 358 |
} |
| 359 |
} |
| 360 |
/** |
| 361 |
* Escape test. |
| 362 |
* |
| 363 |
* According to the html5 spec section 8.3 Serializing HTML fragments, text |
| 364 |
* within tags that are not style, script, xmp, iframe, noembed, and noframes |
| 365 |
* need to be properly escaped. |
| 366 |
* |
| 367 |
* The & should be converted to &, no breaking space unicode characters |
| 368 |
* converted to , when in attribute mode the " should be converted to |
| 369 |
* ", and when not in attribute mode the < and > should be converted to |
| 370 |
* < and >. |
| 371 |
* |
| 372 |
* @see http://www.w3.org/TR/2013/CR-html5-20130806/syntax.html#escapingString |
| 373 |
* |
| 374 |
* @param string $text Text to escape. |
| 375 |
* @param bool $attribute True if we are escaping an attrubute, false otherwise. |
| 376 |
*/ |
| 377 |
protected function escape($text, $attribute = \false) |
| 378 |
{ |
| 379 |
// Not using htmlspecialchars because, while it does escaping, it doesn't |
| 380 |
// match the requirements of section 8.5. For example, it doesn't handle |
| 381 |
// non-breaking spaces. |
| 382 |
if ($attribute) { |
| 383 |
$replace = array('"' => '"', '&' => '&', " " => ' '); |
| 384 |
} else { |
| 385 |
$replace = array('<' => '<', '>' => '>', '&' => '&', " " => ' '); |
| 386 |
} |
| 387 |
return \strtr($text, $replace); |
| 388 |
} |
| 389 |
} |
| 390 |
|