PluginProbe
WindPress – Tailwind CSS integration for WordPress / 3.0.1
WindPress – Tailwind CSS integration for WordPress v3.0.1
3.2.89 3.2.88 3.2.87 3.2.86 3.2.85 3.2.84 3.2.83 3.2.82 3.2.81 trunk 3.0.0 3.0.1 3.0.10 3.0.11 3.0.12 3.0.13 3.0.14 3.0.15 3.0.16 3.0.17 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 All 143 releases
windpress / vendor / masterminds / html5 / src / HTML5 / Serializer / OutputRules.php

OutputRules.php in WindPress – Tailwind CSS integration for WordPress 3.0.1, at vendor/masterminds/html5/src/HTML5/Serializer/OutputRules.php

388 lines 14.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 if ($ele->hasChildNodes()) {
201 $this->wr('>');
202 } else {
203 $this->wr(' />');
204 }
205 }
206 protected function attrs($ele)
207 {
208 // FIXME: Needs support for xml, xmlns, xlink, and namespaced elements.
209 if (!$ele->hasAttributes()) {
210 return $this;
211 }
212 // TODO: Currently, this always writes name="value", and does not do
213 // value-less attributes.
214 $map = $ele->attributes;
215 $len = $map->length;
216 for ($i = 0; $i < $len; ++$i) {
217 $node = $map->item($i);
218 $val = $this->enc($node->value, \true);
219 // XXX: The spec says that we need to ensure that anything in
220 // the XML, XMLNS, or XLink NS's should use the canonical
221 // prefix. It seems that DOM does this for us already, but there
222 // may be exceptions.
223 $name = $node->nodeName;
224 // Special handling for attributes in SVG and MathML.
225 // Using if/elseif instead of switch because it's faster in PHP.
226 if ($this->outputMode == static::IM_IN_SVG) {
227 $name = Elements::normalizeSvgAttribute($name);
228 } elseif ($this->outputMode == static::IM_IN_MATHML) {
229 $name = Elements::normalizeMathMlAttribute($name);
230 }
231 $this->wr(' ')->wr($name);
232 if (isset($val) && '' !== $val || $this->nonBooleanAttribute($node)) {
233 $this->wr('="')->wr($val)->wr('"');
234 }
235 }
236 }
237 protected function nonBooleanAttribute(\DOMAttr $attr)
238 {
239 $ele = $attr->ownerElement;
240 foreach ($this->nonBooleanAttributes as $rule) {
241 if (isset($rule['nodeNamespace']) && $rule['nodeNamespace'] !== $ele->namespaceURI) {
242 continue;
243 }
244 if (isset($rule['attNamespace']) && $rule['attNamespace'] !== $attr->namespaceURI) {
245 continue;
246 }
247 if (isset($rule['nodeName']) && !is_array($rule['nodeName']) && $rule['nodeName'] !== $ele->localName) {
248 continue;
249 }
250 if (isset($rule['nodeName']) && is_array($rule['nodeName']) && !in_array($ele->localName, $rule['nodeName'], \true)) {
251 continue;
252 }
253 if (isset($rule['attrName']) && !is_array($rule['attrName']) && $rule['attrName'] !== $attr->localName) {
254 continue;
255 }
256 if (isset($rule['attrName']) && is_array($rule['attrName']) && !in_array($attr->localName, $rule['attrName'], \true)) {
257 continue;
258 }
259 if (isset($rule['xpath'])) {
260 $xp = $this->getXPath($attr);
261 if (isset($rule['prefixes'])) {
262 foreach ($rule['prefixes'] as $nsPrefix => $ns) {
263 $xp->registerNamespace($nsPrefix, $ns);
264 }
265 }
266 if (!$xp->evaluate($rule['xpath'], $attr)) {
267 continue;
268 }
269 }
270 return \true;
271 }
272 return \false;
273 }
274 private function getXPath(\DOMNode $node)
275 {
276 if (!$this->xpath) {
277 $this->xpath = new \DOMXPath($node->ownerDocument);
278 }
279 return $this->xpath;
280 }
281 /**
282 * Write the closing tag.
283 *
284 * Tags for HTML, MathML, and SVG are in the local name. Otherwise, use the
285 * qualified name (8.3).
286 *
287 * @param \DOMNode $ele The element being written.
288 */
289 protected function closeTag($ele)
290 {
291 if ($this->outputMode == static::IM_IN_HTML || $ele->hasChildNodes()) {
292 $this->wr('</')->wr($this->traverser->isLocalElement($ele) ? $ele->localName : $ele->tagName)->wr('>');
293 }
294 }
295 /**
296 * Write to the output.
297 *
298 * @param string $text The string to put into the output
299 *
300 * @return $this
301 */
302 protected function wr($text)
303 {
304 fwrite($this->out, $text);
305 return $this;
306 }
307 /**
308 * Write a new line character.
309 *
310 * @return $this
311 */
312 protected function nl()
313 {
314 fwrite($this->out, \PHP_EOL);
315 return $this;
316 }
317 /**
318 * Encode text.
319 *
320 * When encode is set to false, the default value, the text passed in is
321 * escaped per section 8.3 of the html5 spec. For details on how text is
322 * escaped see the escape() method.
323 *
324 * When encoding is set to true the text is converted to named character
325 * references where appropriate. Section 8.1.4 Character references of the
326 * html5 spec refers to using named character references. This is useful for
327 * characters that can't otherwise legally be used in the text.
328 *
329 * The named character references are listed in section 8.5.
330 *
331 * @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.
332 * This includes such characters as +.# and many other common ones. By default
333 * encoding here will just escape &'<>".
334 *
335 * Note, PHP 5.4+ has better html5 encoding.
336 *
337 * @todo Use the Entities class in php 5.3 to have html5 entities.
338 *
339 * @param string $text Text to encode.
340 * @param bool $attribute True if we are encoding an attrubute, false otherwise.
341 *
342 * @return string The encoded text.
343 */
344 protected function enc($text, $attribute = \false)
345 {
346 // Escape the text rather than convert to named character references.
347 if (!$this->encode) {
348 return $this->escape($text, $attribute);
349 }
350 // If we are in PHP 5.4+ we can use the native html5 entity functionality to
351 // convert the named character references.
352 if ($this->hasHTML5) {
353 return htmlentities($text, \ENT_HTML5 | \ENT_SUBSTITUTE | \ENT_QUOTES, 'UTF-8', \false);
354 } else {
355 return strtr($text, HTML5Entities::$map);
356 }
357 }
358 /**
359 * Escape test.
360 *
361 * According to the html5 spec section 8.3 Serializing HTML fragments, text
362 * within tags that are not style, script, xmp, iframe, noembed, and noframes
363 * need to be properly escaped.
364 *
365 * The & should be converted to &amp;, no breaking space unicode characters
366 * converted to &nbsp;, when in attribute mode the " should be converted to
367 * &quot;, and when not in attribute mode the < and > should be converted to
368 * &lt; and &gt;.
369 *
370 * @see http://www.w3.org/TR/2013/CR-html5-20130806/syntax.html#escapingString
371 *
372 * @param string $text Text to escape.
373 * @param bool $attribute True if we are escaping an attrubute, false otherwise.
374 */
375 protected function escape($text, $attribute = \false)
376 {
377 // Not using htmlspecialchars because, while it does escaping, it doesn't
378 // match the requirements of section 8.5. For example, it doesn't handle
379 // non-breaking spaces.
380 if ($attribute) {
381 $replace = array('"' => '&quot;', '&' => '&amp;', " " => '&nbsp;');
382 } else {
383 $replace = array('<' => '&lt;', '>' => '&gt;', '&' => '&amp;', " " => '&nbsp;');
384 }
385 return strtr($text, $replace);
386 }
387 }
388