PluginProbe
WindPress – Tailwind CSS integration for WordPress / 3.2.89
WindPress – Tailwind CSS integration for WordPress v3.2.89
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.2.89, at vendor/masterminds/html5/src/HTML5/Serializer/OutputRules.php

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