| 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 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class RemexCompatFormatter extends HtmlFormatter { |
| 17 |
private const MARKED_EMPTY_ELEMENTS = [ |
| 18 |
'li' => true, |
| 19 |
'p' => true, |
| 20 |
'tr' => true, |
| 21 |
]; |
| 22 |
|
| 23 |
|
| 24 |
private $textProcessor; |
| 25 |
|
| 26 |
public function __construct( $options = [] ) { |
| 27 |
parent::__construct( $options ); |
| 28 |
|
| 29 |
$this->attributeEscapes["\u{00A0}"] = ' '; |
| 30 |
$this->textEscapes["\u{00A0}"] = ' '; |
| 31 |
|
| 32 |
$this->textEscapes["\u{0338}"] = '̸'; |
| 33 |
|
| 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 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 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 |
|
| 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 |
} |