Merge "Drop index oi_name_archive_name on table oldimage"
[lhc/web/wiklou.git] / includes / tidy / RemexCompatFormatter.php
1 <?php
2
3 namespace MediaWiki\Tidy;
4
5 use RemexHtml\HTMLData;
6 use RemexHtml\Serializer\HtmlFormatter;
7 use RemexHtml\Serializer\SerializerNode;
8 use RemexHtml\Tokenizer\PlainAttributes;
9
10 /**
11 * @internal
12 */
13 class RemexCompatFormatter extends HtmlFormatter {
14 private static $markedEmptyElements = [
15 'li' => true,
16 'p' => true,
17 'tr' => true,
18 ];
19
20 public function __construct( $options = [] ) {
21 parent::__construct( $options );
22 $this->attributeEscapes["\xc2\xa0"] = '&#160;';
23 unset( $this->attributeEscapes["&"] );
24 $this->textEscapes["\xc2\xa0"] = '&#160;';
25 unset( $this->textEscapes["&"] );
26 }
27
28 public function startDocument( $fragmentNamespace, $fragmentName ) {
29 return '';
30 }
31
32 public function element( SerializerNode $parent, SerializerNode $node, $contents ) {
33 $data = $node->snData;
34 if ( $data && $data->isPWrapper ) {
35 if ( $data->nonblankNodeCount ) {
36 return "<p>$contents</p>";
37 } else {
38 return $contents;
39 }
40 }
41
42 $name = $node->name;
43 $attrs = $node->attrs;
44 if ( isset( self::$markedEmptyElements[$name] ) && $attrs->count() === 0 ) {
45 if ( strspn( $contents, "\t\n\f\r " ) === strlen( $contents ) ) {
46 return "<{$name} class=\"mw-empty-elt\">$contents</{$name}>";
47 }
48 }
49
50 $s = "<$name";
51 foreach ( $attrs->getValues() as $attrName => $attrValue ) {
52 $encValue = strtr( $attrValue, $this->attributeEscapes );
53 $s .= " $attrName=\"$encValue\"";
54 }
55 if ( $node->namespace === HTMLData::NS_HTML && isset( $this->voidElements[$name] ) ) {
56 $s .= ' />';
57 return $s;
58 }
59
60 $s .= '>';
61 if ( $node->namespace === HTMLData::NS_HTML
62 && isset( $contents[0] ) && $contents[0] === "\n"
63 && isset( $this->prefixLfElements[$name] )
64 ) {
65 $s .= "\n$contents</$name>";
66 } else {
67 $s .= "$contents</$name>";
68 }
69 return $s;
70 }
71 }