Debian Codegraph @

includes/tidy/RemexCompatFormatter.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tidy;
4
5 use MediaWiki\Parser\Sanitizer;
6 use Wikimedia\RemexHtml\HTMLData;
7 use Wikimedia\RemexHtml\Serializer\HtmlFormatter;
8 use Wikimedia\RemexHtml\Serializer\SerializerNode;
9
10 /**
11 * @internal
12 *
13 * WATCH OUT! Unlike normal HtmlFormatter, this class requires the 'ignoreCharRefs' option
14 * in Tokenizer to be used. If that option is not used, it will produce wrong results (T354361).
15 */
16 class RemexCompatFormatter extends HtmlFormatter {
17 private const MARKED_EMPTY_ELEMENTS = [
18 'li' => true,
19 'p' => true,
20 'tr' => true,
21 ];
22
23 /** @var ?callable */
24 private $textProcessor;
25
26 public function __construct( $options = [] ) {
27 parent::__construct( $options );
28 // Escape non-breaking space
29 $this->attributeEscapes["\u{00A0}"] = '&#160;';
30 $this->textEscapes["\u{00A0}"] = '&#160;';
31 // Escape U+0338 (T387130)
32 $this->textEscapes["\u{0338}"] = '&#x338;';
33 // Disable escaping of '&', because we expect to see entities, due to 'ignoreCharRefs'
34 unset( $this->attributeEscapes["&"] );
35 unset( $this->textEscapes["&"] );
36 $this->textProcessor = $options['textProcessor'] ?? null;
37 }
38
39 public function startDocument( $fragmentNamespace, $fragmentName ) {
40 return '';
41 }
42
43 /**
44 * WATCH OUT! Unlike normal HtmlFormatter, this class expects that the $text argument contains
45 * unexpanded character references (entities), as a result of using the 'ignoreCharRefs' option
46 * in Tokenizer. If that option is not used, this method will produce wrong results (T354361).
47 *
48 * @inheritDoc
49 */
50 public function characters( SerializerNode $parent, $text, $start, $length ) {
51 $text = parent::characters( $parent, $text, $start, $length );
52
53 if ( $parent->namespace !== HTMLData::NS_HTML
54 || !isset( $this->rawTextElements[$parent->name] )
55 ) {
56 if ( $this->textProcessor !== null ) {
57 $text = call_user_func( $this->textProcessor, $text );
58 }
59
60 // Ensure a consistent representation for all entities
61 $text = Sanitizer::normalizeCharReferences( $text );
62 }
63
64 return $text;
65 }
66
67 public function element( SerializerNode $parent, SerializerNode $node, $contents ) {
68 $data = $node->snData;
69 if ( $data && $data->isPWrapper ) {
70 if ( $data->nonblankNodeCount ) {
71 return "<p>$contents</p>";
72 } else {
73 return $contents;
74 }
75 }
76
77 $name = $node->name;
78 $attrs = $node->attrs;
79 if ( isset( self::MARKED_EMPTY_ELEMENTS[$name] ) && $attrs->count() === 0
80 && strspn( $contents, "\t\n\f\r " ) === strlen( $contents )
81 ) {
82 return "<{$name} class=\"mw-empty-elt\">$contents</{$name}>";
83 }
84
85 $s = "<$name";
86 foreach ( $attrs->getValues() as $attrName => $attrValue ) {
87 $encValue = strtr( $attrValue, $this->attributeEscapes );
88 $encValue = Sanitizer::normalizeCharReferences( $encValue );
89 $s .= " $attrName=\"$encValue\"";
90 }
91 if ( $node->namespace === HTMLData::NS_HTML && isset( $this->voidElements[$name] ) ) {
92 $s .= ' />';
93 return $s;
94 }
95
96 $s .= '>';
97 if ( $node->namespace === HTMLData::NS_HTML
98 && isset( $contents[0] ) && $contents[0] === "\n"
99 && isset( $this->prefixLfElements[$name] )
100 ) {
101 $s .= "\n$contents</$name>";
102 } else {
103 $s .= "$contents</$name>";
104 }
105 return $s;
106 }
107 }