MediaWiki  master
RemexCompatFormatter.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Tidy;
4 
5 use RemexHtml\HTMLData;
6 use RemexHtml\Serializer\HtmlFormatter;
7 use RemexHtml\Serializer\SerializerNode;
8 
12 class RemexCompatFormatter extends HtmlFormatter {
13  private static $markedEmptyElements = [
14  'li' => true,
15  'p' => true,
16  'tr' => true,
17  ];
18 
19  public function __construct( $options = [] ) {
20  parent::__construct( $options );
21  $this->attributeEscapes["\u{00A0}"] = '&#160;';
22  unset( $this->attributeEscapes["&"] );
23  $this->textEscapes["\u{00A0}"] = '&#160;';
24  unset( $this->textEscapes["&"] );
25  }
26 
27  public function startDocument( $fragmentNamespace, $fragmentName ) {
28  return '';
29  }
30 
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>";
36  } else {
37  return $contents;
38  }
39  }
40 
41  $name = $node->name;
42  $attrs = $node->attrs;
43  if ( isset( self::$markedEmptyElements[$name] ) && $attrs->count() === 0
44  && strspn( $contents, "\t\n\f\r " ) === strlen( $contents )
45  ) {
46  return "<{$name} class=\"mw-empty-elt\">$contents</{$name}>";
47  }
48 
49  $s = "<$name";
50  foreach ( $attrs->getValues() as $attrName => $attrValue ) {
51  $encValue = strtr( $attrValue, $this->attributeEscapes );
52  $s .= " $attrName=\"$encValue\"";
53  }
54  if ( $node->namespace === HTMLData::NS_HTML && isset( $this->voidElements[$name] ) ) {
55  $s .= ' />';
56  return $s;
57  }
58 
59  $s .= '>';
60  if ( $node->namespace === HTMLData::NS_HTML
61  && isset( $contents[0] ) && $contents[0] === "\n"
62  && isset( $this->prefixLfElements[$name] )
63  ) {
64  $s .= "\n$contents</$name>";
65  } else {
66  $s .= "$contents</$name>";
67  }
68  return $s;
69  }
70 }
MediaWiki\Tidy\RemexCompatFormatter\startDocument
startDocument( $fragmentNamespace, $fragmentName)
Definition: RemexCompatFormatter.php:27
MediaWiki\Tidy\RemexCompatFormatter\__construct
__construct( $options=[])
Definition: RemexCompatFormatter.php:19
$s
$s
Definition: mergeMessageFileList.php:185
MediaWiki\Tidy\RemexCompatFormatter
Definition: RemexCompatFormatter.php:12
MediaWiki\Tidy\RemexCompatFormatter\element
element(SerializerNode $parent, SerializerNode $node, $contents)
Definition: RemexCompatFormatter.php:31
MediaWiki\Tidy\RemexCompatFormatter\$markedEmptyElements
static $markedEmptyElements
Definition: RemexCompatFormatter.php:13
MediaWiki\Tidy
Definition: RemexCompatFormatter.php:3