3 namespace MediaWiki\Tidy
;
5 use RemexHtml\HTMLData
;
6 use RemexHtml\Serializer\HtmlFormatter
;
7 use RemexHtml\Serializer\SerializerNode
;
12 class RemexCompatFormatter
extends HtmlFormatter
{
13 private static $markedEmptyElements = [
19 public function __construct( $options = [] ) {
20 parent
::__construct( $options );
21 $this->attributeEscapes
["\u{00A0}"] = ' ';
22 unset( $this->attributeEscapes
["&"] );
23 $this->textEscapes
["\u{00A0}"] = ' ';
24 unset( $this->textEscapes
["&"] );
27 public function startDocument( $fragmentNamespace, $fragmentName ) {
31 public function element( SerializerNode
$parent, SerializerNode
$node, $contents ) {
32 $data = $node->snData
;
33 if ( $data && $data->isPWrapper
) {
34 if ( $data->nonblankNodeCount
) {
35 return "<p>$contents</p>";
42 $attrs = $node->attrs
;
43 if ( isset( self
::$markedEmptyElements[$name] ) && $attrs->count() === 0 ) {
44 if ( strspn( $contents, "\t\n\f\r " ) === strlen( $contents ) ) {
45 return "<{$name} class=\"mw-empty-elt\">$contents</{$name}>";
50 foreach ( $attrs->getValues() as $attrName => $attrValue ) {
51 $encValue = strtr( $attrValue, $this->attributeEscapes
);
52 $s .= " $attrName=\"$encValue\"";
54 if ( $node->namespace === HTMLData
::NS_HTML
&& isset( $this->voidElements
[$name] ) ) {
60 if ( $node->namespace === HTMLData
::NS_HTML
61 && isset( $contents[0] ) && $contents[0] === "\n"
62 && isset( $this->prefixLfElements
[$name] )
64 $s .= "\n$contents</$name>";
66 $s .= "$contents</$name>";