Debian Codegraph @

vendor/wikimedia/remex-html/src/Serializer/HtmlFormatter.php

raw
1 <?php
2
3 namespace Wikimedia\RemexHtml\Serializer;
4
5 use Wikimedia\RemexHtml\DOM\DOMFormatter;
6 use Wikimedia\RemexHtml\DOM\DOMUtils;
7 use Wikimedia\RemexHtml\HTMLData;
8
9 /**
10 * A formatter which follows the HTML 5 fragment serialization algorithm.
11 */
12 class HtmlFormatter implements Formatter, DOMFormatter {
13 /**
14 * The elements for which a closing tag is omitted.
15 *
16 * @var array<string,bool>
17 */
18 protected $voidElements = [
19 'area' => true,
20 'base' => true,
21 'basefont' => true,
22 'bgsound' => true,
23 'br' => true,
24 'col' => true,
25 'embed' => true,
26 'frame' => true,
27 'hr' => true,
28 'img' => true,
29 'input' => true,
30 'keygen' => true,
31 'link' => true,
32 'menuitem' => true,
33 'meta' => true,
34 'param' => true,
35 'source' => true,
36 'track' => true,
37 'wbr' => true,
38 ];
39
40 /**
41 * The elements which need a leading newline in their contents to be
42 * duplicated, since the parser strips a leading newline.
43 *
44 * @var array<string,bool>
45 */
46 protected $prefixLfElements = [
47 'pre' => true,
48 'textarea' => true,
49 'listing' => true
50 ];
51
52 /**
53 * The elements which have unescaped contents.
54 *
55 * @var array<string,bool>
56 */
57 protected $rawTextElements = [
58 'style' => true,
59 'script' => true,
60 'xmp' => true,
61 'iframe' => true,
62 'noembed' => true,
63 'noframes' => true,
64 'plaintext' => true,
65 ];
66
67 /**
68 * The escape table for attribute values
69 *
70 * @var array<string,string>
71 */
72 protected $attributeEscapes = [
73 '&' => '&amp;',
74 "\xc2\xa0" => '&nbsp;',
75 '"' => '&quot;',
76 ];
77
78 /**
79 * The escape table for text nodes
80 *
81 * @var array<string,string>
82 */
83 protected $textEscapes = [
84 '&' => '&amp;',
85 "\xc2\xa0" => '&nbsp;',
86 '<' => '&lt;',
87 '>' => '&gt;',
88 ];
89
90 /**
91 * Attribute namespaces which have unqualified local names
92 *
93 * @var array<string,bool>
94 */
95 protected $unqualifiedNamespaces = [
96 HTMLData::NS_HTML => true,
97 HTMLData::NS_MATHML => true,
98 HTMLData::NS_SVG => true,
99 ];
100
101 /** @var bool */
102 protected $useSourceDoctype;
103 /** @var bool */
104 protected $reverseCoercion;
105
106 /**
107 * Constructor.
108 *
109 * @param array $options An associative array of options:
110 * - scriptingFlag : Set this to false to disable scripting. True by default.
111 * - useSourceDoctype : Emit the doctype used in the source. If this is
112 * false or absent, an HTML doctype will be used.
113 * - reverseCoercion : When formatting a DOM node, reverse the encoding
114 * of invalid names. False by default.
115 */
116 public function __construct( $options = [] ) {
117 $options += [
118 'scriptingFlag' => true,
119 'useSourceDoctype' => false,
120 'reverseCoercion' => false,
121 ];
122 if ( $options['scriptingFlag'] ) {
123 $this->rawTextElements['noscript'] = true;
124 }
125 $this->useSourceDoctype = $options['useSourceDoctype'];
126 $this->reverseCoercion = $options['reverseCoercion'];
127 }
128
129 /** @inheritDoc */
130 public function startDocument( $fragmentNamespace, $fragmentName ) {
131 return "<!DOCTYPE html>";
132 }
133
134 /** @inheritDoc */
135 public function characters( SerializerNode $parent, $text, $start, $length ) {
136 $text = substr( $text, $start, $length );
137 if ( $parent->namespace !== HTMLData::NS_HTML
138 || !isset( $this->rawTextElements[$parent->name] )
139 ) {
140 $text = strtr( $text, $this->textEscapes );
141 }
142 return $text;
143 }
144
145 /** @inheritDoc */
146 public function element( SerializerNode $parent, SerializerNode $node, $contents ) {
147 $name = $node->name;
148 $s = "<$name";
149 foreach ( $node->attrs->getValues() as $attrName => $attrValue ) {
150 $encValue = strtr( $attrValue, $this->attributeEscapes );
151 $s .= " $attrName=\"$encValue\"";
152 }
153 $s .= '>';
154 if ( $node->namespace === HTMLData::NS_HTML ) {
155 if ( isset( $contents[0] ) && $contents[0] === "\n"
156 && isset( $this->prefixLfElements[$name] )
157 ) {
158 $s .= "\n$contents</$name>";
159 } elseif ( !isset( $this->voidElements[$name] ) ) {
160 $s .= "$contents</$name>";
161 }
162 } else {
163 $s .= "$contents</$name>";
164 }
165 return $s;
166 }
167
168 /** @inheritDoc */
169 public function comment( SerializerNode $parent, $text ) {
170 return "<!--$text-->";
171 }
172
173 /** @inheritDoc */
174 public function doctype( $name, $public, $system ) {
175 return '';
176 }
177
178 /**
179 * @param \DOMNode $node
180 * @return string
181 */
182 public function formatDOMNode( $node ) {
183 $contents = '';
184 if ( $node->firstChild ) {
185 foreach ( $node->childNodes as $child ) {
186 $contents .= $this->formatDOMNode( $child );
187 }
188 }
189
190 switch ( $node->nodeType ) {
191 case XML_ELEMENT_NODE:
192 '@phan-var \DOMElement $node'; /** @var \DOMElement $node */
193 return $this->formatDOMElement( $node, $contents );
194
195 case XML_DOCUMENT_NODE:
196 if ( !$this->useSourceDoctype ) {
197 return "<!DOCTYPE html>" . $contents;
198 } else {
199 return $contents;
200 }
201
202 case XML_DOCUMENT_FRAG_NODE:
203 return $contents;
204
205 case XML_TEXT_NODE:
206 '@phan-var \DOMCharacterData $node'; /** @var \DOMCharacterData $node */
207 $text = $node->data;
208 $parent = $node->parentNode;
209 if ( $parent->namespaceURI !== HTMLData::NS_HTML
210 || !isset( $this->rawTextElements[$parent->nodeName] )
211 ) {
212 $text = strtr( $text, $this->textEscapes );
213 }
214 return $text;
215
216 case XML_CDATA_SECTION_NODE:
217 '@phan-var \DOMCdataSection $node'; /** @var \DOMCdataSection $node */
218 $parent = $node->parentNode;
219 if ( $parent->namespaceURI === HTMLData::NS_HTML ) {
220 // CDATA is not allowed in HTML nodes
221 return $node->data;
222 } else {
223 return "<![CDATA[{$node->data}]]>";
224 }
225
226 case XML_PI_NODE:
227 '@phan-var \DOMProcessingInstruction $node'; /** @var \DOMProcessingInstruction $node */
228 return "<?{$node->target} {$node->data}>";
229
230 case XML_COMMENT_NODE:
231 '@phan-var \DOMComment $node'; /** @var \DOMComment $node */
232 return "<!--{$node->data}-->";
233
234 case XML_DOCUMENT_TYPE_NODE:
235 '@phan-var \DOMDocumentType $node'; /** @var \DOMDocumentType $node */
236 if ( $this->useSourceDoctype ) {
237 return "<!DOCTYPE {$node->name}>";
238 } else {
239 return '';
240 }
241
242 default:
243 return '';
244 }
245 }
246
247 /**
248 * @param \DOMElement $node
249 * @param string $contents
250 * @return string
251 */
252 public function formatDOMElement( $node, $contents ) {
253 $ns = $node->namespaceURI;
254 if ( $ns === null
255 || isset( $this->unqualifiedNamespaces[$ns] )
256 || !( $node->prefix )
257 ) {
258 $name = (string)$node->localName;
259 } else {
260 $name = $node->prefix . ':' . $node->localName;
261 }
262 if ( $this->reverseCoercion ) {
263 $name = DOMUtils::uncoerceName( $name );
264 }
265
266 $s = '<' . $name;
267 foreach ( $node->attributes as $attr ) {
268 switch ( $attr->namespaceURI ) {
269 case HTMLData::NS_XML:
270 $attrName = 'xml:' . $attr->localName;
271 break;
272 case HTMLData::NS_XMLNS:
273 if ( $attr->localName === 'xmlns' ) {
274 $attrName = 'xmlns';
275 } else {
276 $attrName = 'xmlns:' . $attr->localName;
277 }
278 break;
279 case HTMLData::NS_XLINK:
280 $attrName = 'xlink:' . $attr->localName;
281 break;
282 default:
283 if ( strlen( $attr->prefix ) ) {
284 $attrName = $attr->prefix . ':' . $attr->localName;
285 } else {
286 $attrName = $attr->localName;
287 }
288 }
289 if ( $this->reverseCoercion ) {
290 $attrName = DOMUtils::uncoerceName( $attrName );
291 }
292 $encValue = strtr( $attr->value, $this->attributeEscapes );
293 $s .= " $attrName=\"$encValue\"";
294 }
295 $s .= '>';
296 if ( $ns === HTMLData::NS_HTML ) {
297 if ( isset( $contents[0] ) && $contents[0] === "\n"
298 && isset( $this->prefixLfElements[$name] )
299 ) {
300 $s .= "\n$contents</$name>";
301 } elseif ( !isset( $this->voidElements[$name] ) ) {
302 $s .= "$contents</$name>";
303 }
304 } else {
305 $s .= "$contents</$name>";
306 }
307 return $s;
308 }
309 }