Debian Codegraph @

vendor/wikimedia/remex-html/src/Serializer/TestFormatter.php

raw
1 <?php
2
3 namespace Wikimedia\RemexHtml\Serializer;
4
5 use Wikimedia\RemexHtml\DOM\DOMFormatter;
6 use Wikimedia\RemexHtml\DOM\DOMUtils;
7 use Wikimedia\RemexHtml\HTMLData;
8 use Wikimedia\RemexHtml\Tokenizer\Attribute;
9
10 /**
11 * A Formatter which is used to format documents in (almost) the way they
12 * appear in the html5lib tests. A little bit of post-processing is required
13 * in the PHPUnit tests.
14 */
15 class TestFormatter implements Formatter, DOMFormatter {
16 private const ATTR_NAMESPACES = [
17 HTMLData::NS_XML => 'xml',
18 HTMLData::NS_XLINK => 'xlink',
19 HTMLData::NS_XMLNS => 'xmlns',
20 ];
21
22 /** @inheritDoc */
23 public function startDocument( $fragmentNamespace, $fragmentName ) {
24 return '';
25 }
26
27 /** @inheritDoc */
28 public function doctype( $name, $public, $system ) {
29 $ret = "<!DOCTYPE $name";
30 if ( $public !== '' || $system !== '' ) {
31 $ret .= " \"$public\" \"$system\"";
32 }
33 $ret .= ">\n";
34 return $ret;
35 }
36
37 /** @inheritDoc */
38 public function characters( SerializerNode $parent, $text, $start, $length ) {
39 return $this->formatCharacters( substr( $text, $start, $length ) );
40 }
41
42 private function formatCharacters( string $text ): string {
43 return '"' .
44 str_replace( "\n", "<EOL>", $text ) .
45 "\"\n";
46 }
47
48 /** @inheritDoc */
49 public function element( SerializerNode $parent, SerializerNode $node, $contents ) {
50 return $this->formatElement( $node->namespace, $node->name,
51 $node->attrs->getObjects(), $contents );
52 }
53
54 private function formatElement( ?string $namespace, string $name, array $attrs, ?string $contents ): string {
55 $name = DOMUtils::uncoerceName( $name );
56 if ( $namespace === HTMLData::NS_HTML ) {
57 $tagName = $name;
58 } elseif ( $namespace === HTMLData::NS_SVG ) {
59 $tagName = "svg $name";
60 } elseif ( $namespace === HTMLData::NS_MATHML ) {
61 $tagName = "math $name";
62 } else {
63 $tagName = $name;
64 }
65 $ret = "<$tagName>\n";
66 $sortedAttrs = $attrs;
67 ksort( $sortedAttrs, SORT_STRING );
68 foreach ( $sortedAttrs as $attrName => $attr ) {
69 $localName = DOMUtils::uncoerceName( $attr->localName );
70 if ( $attr->namespaceURI === null
71 || isset( $attr->reallyNoNamespace )
72 ) {
73 $prefix = '';
74 } elseif ( isset( self::ATTR_NAMESPACES[$attr->namespaceURI] ) ) {
75 $prefix = self::ATTR_NAMESPACES[$attr->namespaceURI] . ' ';
76 } else {
77 $prefix = '';
78 }
79 $ret .= " $prefix$localName=\"{$attr->value}\"\n";
80 }
81 if ( $contents !== null && $contents !== '' ) {
82 $contents = preg_replace( '/^/m', ' ', $contents );
83 } else {
84 $contents = '';
85 }
86 if ( $namespace === HTMLData::NS_HTML && $name === 'template' ) {
87 if ( $contents === '' ) {
88 $contents = " content\n";
89 } else {
90 $contents = " content\n" . preg_replace( '/^/m', ' ', $contents );
91 }
92 }
93 $ret .= $contents;
94 return $ret;
95 }
96
97 /** @inheritDoc */
98 public function comment( SerializerNode $parent, $text ) {
99 return $this->formatComment( $text );
100 }
101
102 private function formatComment( string $text ): string {
103 return "<!-- $text -->\n";
104 }
105
106 /** @inheritDoc */
107 public function formatDOMNode( \DOMNode $node ) {
108 $contents = '';
109 if ( $node->firstChild ) {
110 foreach ( $node->childNodes as $child ) {
111 $contents .= $this->formatDOMNode( $child );
112 }
113 }
114
115 switch ( $node->nodeType ) {
116 case XML_ELEMENT_NODE:
117 '@phan-var \DOMElement $node'; /** @var \DOMElement $node */
118 return $this->formatDOMElement( $node, $contents );
119
120 case XML_DOCUMENT_NODE:
121 case XML_DOCUMENT_FRAG_NODE:
122 return $contents;
123
124 case XML_TEXT_NODE:
125 case XML_CDATA_SECTION_NODE:
126 '@phan-var \DOMCharacterData $node'; /** @var \DOMCharacterData $node */
127 return $this->formatCharacters( $node->data );
128
129 case XML_COMMENT_NODE:
130 '@phan-var \DOMComment $node'; /** @var \DOMComment $node */
131 return $this->formatComment( $node->data );
132
133 case XML_DOCUMENT_TYPE_NODE:
134 '@phan-var \DOMDocumentType $node'; /** @var \DOMDocumentType $node */
135 return $this->doctype( $node->name, $node->publicId, $node->systemId );
136
137 case XML_PI_NODE:
138 default:
139 return '';
140 }
141 }
142
143 /** @inheritDoc */
144 public function formatDOMElement( \DOMElement $node, $content ) {
145 $attrs = [];
146 foreach ( $node->attributes as $attr ) {
147 $prefix = null;
148 switch ( $attr->namespaceURI ) {
149 case HTMLData::NS_XML:
150 $prefix = 'xml';
151 $qName = 'xml:' . $attr->localName;
152 break;
153 case HTMLData::NS_XMLNS:
154 if ( $attr->localName === 'xmlns' ) {
155 $qName = 'xmlns';
156 } else {
157 $prefix = 'xmlns';
158 $qName = 'xmlns:' . $attr->localName;
159 }
160 break;
161 case HTMLData::NS_XLINK:
162 $prefix = 'xlink';
163 $qName = 'xlink:' . $attr->localName;
164 break;
165 default:
166 if ( strlen( $attr->prefix ) ) {
167 $qName = $attr->prefix . ':' . $attr->localName;
168 } else {
169 $prefix = $attr->prefix;
170 $qName = $attr->localName;
171 }
172 }
173
174 $attrs[$qName] = new Attribute( $qName, $attr->namespaceURI, $prefix,
175 $attr->localName, $attr->value );
176 }
177
178 return $this->formatElement( $node->namespaceURI, $node->nodeName, $attrs, $content );
179 }
180 }