Debian Codegraph @

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

raw
1 <?php
2
3 namespace Wikimedia\RemexHtml\Serializer;
4
5 use Wikimedia\RemexHtml\HTMLData;
6
7 /**
8 * A formatter which produces a serialization extremely similar to the
9 * Html5Depurate service, which uses the validator.nu library for tree
10 * construction.
11 *
12 * For use in comparative testing.
13 *
14 * https://www.mediawiki.org/wiki/Html5Depurate
15 */
16 class DepurateFormatter extends HtmlFormatter {
17 /** @inheritDoc */
18 public function __construct( $options = [] ) {
19 parent::__construct( $options );
20 $this->textEscapes["\xc2\xa0"] = '&#160;';
21 }
22
23 /** @inheritDoc */
24 public function element( SerializerNode $parent, SerializerNode $node, $contents ) {
25 $name = $node->name;
26 $s = "<$name";
27 foreach ( $node->attrs->getValues() as $attrName => $attrValue ) {
28 $encValue = strtr( $attrValue, $this->attributeEscapes );
29 $s .= " $attrName=\"$encValue\"";
30 }
31 if ( $node->namespace === HTMLData::NS_HTML ) {
32 if ( isset( $this->prefixLfElements[$name] )
33 ) {
34 $s .= ">\n$contents</$name>";
35 } elseif ( !isset( $this->voidElements[$name] ) ) {
36 $s .= ">$contents</$name>";
37 } else {
38 $s .= " />";
39 }
40 } else {
41 $s .= ">$contents</$name>";
42 }
43 return $s;
44 }
45 }