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