Debian Codegraph @

vendor/wikimedia/parsoid/tests/phpunit/Parsoid/Wt2Html/XMLSerializerTest.php

raw
1 <?php
2
3 namespace Test\Parsoid\Wt2Html;
4
5 use Wikimedia\Parsoid\Utils\DOMCompat;
6 use Wikimedia\Parsoid\Wt2Html\XMLSerializer;
7 use Wikimedia\TestingAccessWrapper;
8
9 /**
10 * Test the entity encoding logic (which the JS version did not have as it called
11 * on the entities npm package).
12 * @coversDefaultClass \Wikimedia\Parsoid\Wt2Html\XMLSerializer
13 */
14 class XMLSerializerTest extends \PHPUnit\Framework\TestCase {
15 private static function parse( string $html, int $options = 0 ) {
16 $doc = DOMCompat::newDocument( true );
17 $doc->loadHTML( $html, $options );
18 return $doc;
19 }
20
21 /**
22 * @covers ::serialize
23 */
24 public function testSerialize() {
25 $html = '<html><head><title>hi</title><body>'
26 . '<div id="123">ok<div id="234">nope</div></div>'
27 . "\n\n" . '<!--comment--><div id="345">end</div></body></html>';
28 $expectedHtml = "<!DOCTYPE html>\n<html><head><title>hi</title></head><body>"
29 . '<div id="123">ok<div id="234">nope</div></div>'
30 . "\n\n" . '<!--comment--><div id="345">end</div></body></html>';
31 $expectedInnerHtml = "<head><title>hi</title></head><body>"
32 . '<div id="123">ok<div id="234">nope</div></div>'
33 . "\n\n" . '<!--comment--><div id="345">end</div></body>';
34 $doc = self::parse( $html );
35
36 $ret = XMLSerializer::serialize( $doc );
37 $this->assertIsArray( $ret );
38 $this->assertArrayHasKey( 'html', $ret );
39 $this->assertSame( $expectedHtml, $ret['html'] );
40
41 $ret = XMLSerializer::serialize( $doc, [ 'captureOffsets' => true ] );
42 $this->assertIsArray( $ret );
43 $this->assertArrayHasKey( 'html', $ret );
44 $this->assertSame( $expectedHtml, $ret['html'] );
45
46 $ret = XMLSerializer::serialize( $doc, [ 'innerXML' => true ] );
47 $this->assertIsArray( $ret );
48 $this->assertArrayHasKey( 'html', $ret );
49 $this->assertSame( $expectedInnerHtml, $ret['html'] );
50 }
51
52 /**
53 * @covers ::serialize
54 */
55 public function testSerialize_captureOffsets() {
56 $html = '<html><head><title>hi</title><body>'
57 . '<div id="123">ok<div id="234">nope</div></div>'
58 . "\n\n" . '<!--comment--><div id="345">end</div></body></html>';
59 $doc = self::parse( $html );
60
61 $ret = XMLSerializer::serialize( $doc, [ 'captureOffsets' => true ] );
62 $this->assertIsArray( $ret );
63 $this->assertArrayHasKey( 'offsets', $ret );
64 $this->assertArrayHasKey( '123', $ret['offsets'] );
65 $this->assertSame( [ 0, 62 ], $ret['offsets']['123']['html'] );
66 $this->assertArrayNotHasKey( '234', $ret['offsets'] );
67 $this->assertArrayHasKey( '345', $ret['offsets'] );
68 $this->assertSame( [ 62, 85 ], $ret['offsets']['345']['html'] );
69
70 $ret = XMLSerializer::serialize( $doc, [ 'captureOffsets' => true, 'innerXML' => true ] );
71 $this->assertIsArray( $ret );
72 $this->assertArrayHasKey( 'offsets', $ret );
73 $this->assertArrayHasKey( '123', $ret['offsets'] );
74 $this->assertSame( [ 0, 62 ], $ret['offsets']['123']['html'] );
75 $this->assertArrayNotHasKey( '234', $ret['offsets'] );
76 $this->assertArrayHasKey( '345', $ret['offsets'] );
77 $this->assertSame( [ 62, 85 ], $ret['offsets']['345']['html'] );
78 }
79
80 /**
81 * @covers ::serialize
82 */
83 public function testSerialize_captureOffsets_template() {
84 $html = '<html><head><title>hi</title><body>'
85 . '<p about="#mwt1" typeof="mw:Transclusion" id="mwAQ">a</p>'
86 . '<p about="#mwt1" id="justhappenstobehere">b</p>'
87 . '<p id="mwAg">c</p>'
88 . '</body></html>';
89 $doc = self::parse( $html );
90
91 $ret = XMLSerializer::serialize( $doc, [ 'captureOffsets' => true ] );
92 $this->assertIsArray( $ret );
93 $this->assertArrayHasKey( 'offsets', $ret );
94 $this->assertArrayHasKey( 'mwAQ', $ret['offsets'] );
95 $this->assertArrayHasKey( 'mwAg', $ret['offsets'] );
96 $this->assertArrayNotHasKey( 'justhappenstobehere', $ret['offsets'] );
97 $this->assertSame( [ 0, 104 ], $ret['offsets']['mwAQ']['html'] );
98 $this->assertSame( [ 104, 122 ], $ret['offsets']['mwAg']['html'] );
99 }
100
101 /**
102 * @covers ::serialize
103 */
104 public function testSerialize_captureOffsets_expandedAttrs() {
105 $html = '<html><head><title>hi</title><body>'
106 // phpcs:ignore Generic.Files.LineLength.TooLong
107 . '<div style="color:red" about="#mwt2" typeof="mw:ExpandedAttrs" id="mwAQ" data-mw=\'{"attribs":[[{"txt":"style"},{"html":"&lt;span about=\"#mwt1\" typeof=\"mw:Transclusion\" data-parsoid=\"{&amp;quot;pi&amp;quot;:[[{&amp;quot;k&amp;quot;:&amp;quot;1&amp;quot;}]],&amp;quot;dsr&amp;quot;:[12,30,null,null]}\" data-mw=\"{&amp;quot;parts&amp;quot;:[{&amp;quot;template&amp;quot;:{&amp;quot;target&amp;quot;:{&amp;quot;wt&amp;quot;:&amp;quot;1x&amp;quot;,&amp;quot;href&amp;quot;:&amp;quot;./Template:1x&amp;quot;},&amp;quot;params&amp;quot;:{&amp;quot;1&amp;quot;:{&amp;quot;wt&amp;quot;:&amp;quot;color:red&amp;quot;}},&amp;quot;i&amp;quot;:0}}]}\">color:red&lt;/span>"}]]}\'>boo</div>'
108 . '<p id="mwAg">next!</p>'
109 . '</body></html>';
110 $doc = self::parse( $html );
111
112 $ret = XMLSerializer::serialize( $doc, [ 'captureOffsets' => true ] );
113 $this->assertIsArray( $ret );
114 $this->assertArrayHasKey( 'offsets', $ret );
115 $this->assertArrayHasKey( 'mwAQ', $ret['offsets'] );
116 $this->assertArrayHasKey( 'mwAg', $ret['offsets'] );
117 $this->assertSame( [ 0, 680 ], $ret['offsets']['mwAQ']['html'] );
118 $this->assertSame( [ 680, 702 ], $ret['offsets']['mwAg']['html'] );
119 }
120
121 /**
122 * @covers ::serialize
123 */
124 public function testSerialize_captureOffsets_extensionContentNestedInTemplates() {
125 // Mostly scooped from, echo "{{Demografia/Apricale}}" | node tests/parse --prefix itwiki --dp
126 $html = '<html><head><title>hi</title><body>'
127 // phpcs:ignore Generic.Files.LineLength.TooLong
128 . '<p about="#mwt1" typeof="mw:Transclusion" id="mwAQ" data-mw=\'{"parts":[{"template":{"target":{"wt":"Demografia/Apricale","href":"./Template:Demografia/Apricale"},"params":{},"i":0}}]}\'><i>Abitanti censiti</i></p>'
129 // phpcs:ignore Generic.Files.LineLength.TooLong
130 . '<map name="timeline" id="timeline" typeof="mw:Extension/timeline" data-mw=\'{"name":"timeline","attrs":{},"body":{"extsrc":"yadayadayada"}}\' about="#mwt1"></map>'
131 . '</body></html>';
132 $doc = self::parse( $html );
133
134 $ret = XMLSerializer::serialize( $doc, [ 'captureOffsets' => true ] );
135 $this->assertIsArray( $ret );
136 $this->assertArrayHasKey( 'offsets', $ret );
137 $this->assertArrayHasKey( 'mwAQ', $ret['offsets'] );
138 $this->assertArrayNotHasKey( 'timeline', $ret['offsets'] );
139 $this->assertSame( [ 0, 372 ], $ret['offsets']['mwAQ']['html'] );
140 }
141
142 /**
143 * @covers ::serialize
144 */
145 public function testSerialize_uppercaseTagnames() {
146 $html = '<HTML><HeAD><Title>hi</title><body>'
147 . '<DIV ID="123">ok<div id="234">nope</DIV></div>'
148 . "\n\n" . '<!--comment--><div id="345">end</div></Body></HTML>';
149 $expectedHtml = "<!DOCTYPE html>\n<html><head><title>hi</title></head><body>"
150 . '<div id="123">ok<div id="234">nope</div></div>'
151 . "\n\n" . '<!--comment--><div id="345">end</div></body></html>';
152 $doc = self::parse( $html );
153
154 $ret = XMLSerializer::serialize( $doc );
155 $this->assertIsArray( $ret );
156 $this->assertArrayHasKey( 'html', $ret );
157 $this->assertSame( $expectedHtml, $ret['html'] );
158 }
159
160 /**
161 * @covers ::serialize
162 */
163 public function testSerialize_quotes() {
164 $html = '<html><body>'
165 . '<div attr="&quot;&apos;&quot;"></div>'
166 . '<div attr=\'&quot;&apos;&quot;\'></div>'
167 . '<div attr="&apos;&quot;&apos;"></div>'
168 . '<div attr=\'&apos;&quot;&apos;\'></div>'
169 . '</body></html>';
170 $expectedNonSmart = "<!DOCTYPE html>\n<html><body>"
171 . '<div attr="&quot;\'&quot;"></div>'
172 . '<div attr="&quot;\'&quot;"></div>'
173 . '<div attr="\'&quot;\'"></div>'
174 . '<div attr="\'&quot;\'"></div>'
175 . '</body></html>';
176 $expectedSmart = "<!DOCTYPE html>\n<html><body>"
177 . '<div attr=\'"&apos;"\'></div>'
178 . '<div attr=\'"&apos;"\'></div>'
179 . '<div attr="\'&quot;\'"></div>'
180 . '<div attr="\'&quot;\'"></div>'
181 . '</body></html>';
182 $doc = self::parse( $html );
183
184 $ret = XMLSerializer::serialize( $doc, [ 'smartQuote' => false ] );
185 $this->assertIsArray( $ret );
186 $this->assertArrayHasKey( 'html', $ret );
187 $this->assertSame( $expectedNonSmart, $ret['html'] );
188
189 $ret = XMLSerializer::serialize( $doc, [ 'smartQuote' => true ] );
190 $this->assertIsArray( $ret );
191 $this->assertArrayHasKey( 'html', $ret );
192 $this->assertSame( $expectedSmart, $ret['html'] );
193 }
194
195 /**
196 * @covers ::serialize
197 */
198 public function testSerialize_emptyElements() {
199 // Must have a single root node, otherwise libxml messes up parsing in NOIMPLIED mode.
200 $html = '<div><span /><hr/></div>';
201 $expectedHtml = '<div><span></span><hr/></div>';
202 $doc = self::parse( $html, LIBXML_HTML_NOIMPLIED );
203
204 $ret = XMLSerializer::serialize( $doc, [ 'smartQuote' => false ] );
205 $this->assertIsArray( $ret );
206 $this->assertArrayHasKey( 'html', $ret );
207 $this->assertSame( $expectedHtml, $ret['html'] );
208 }
209
210 /**
211 * @covers ::serialize
212 */
213 public function testSerialize_rawContent() {
214 $html = '<script>x</script>';
215 $doc = self::parse( $html, LIBXML_HTML_NOIMPLIED );
216
217 $ret = XMLSerializer::serialize( $doc, [ 'smartQuote' => false ] );
218 $this->assertIsArray( $ret );
219 $this->assertArrayHasKey( 'html', $ret );
220 $this->assertSame( $html, $ret['html'] );
221 }
222
223 /**
224 * @covers ::serialize
225 */
226 public function testSerialize_newlineStrippingElements() {
227 // Must have a single root node, otherwise libxml messes up parsing in NOIMPLIED mode.
228 // This test looks confusing because Document::loadHTML doesn't fully follow the spec;
229 // it should strip the first newline within a pre block.
230 $html = "<div><pre>\n</pre><div>\n</div></div>";
231 $expectedHtml = "<div><pre>\n\n</pre><div>\n</div></div>";
232 $doc = self::parse( $html, LIBXML_HTML_NOIMPLIED );
233
234 $ret = XMLSerializer::serialize( $doc, [ 'smartQuote' => false ] );
235 $this->assertIsArray( $ret );
236 $this->assertArrayHasKey( 'html', $ret );
237 $this->assertSame( $expectedHtml, $ret['html'] );
238 }
239
240 /**
241 * @covers ::encodeHtmlEntities
242 * @dataProvider provideEncodeHtmlEntities
243 */
244 public function testEncodeHtmlEntities( $raw, $encodeChars, $expected ) {
245 /** @var XMLSerializer $XMLSerializer */
246 $XMLSerializer = TestingAccessWrapper::newFromClass( XMLSerializer::class );
247 $actual = $XMLSerializer->encodeHtmlEntities( $raw, $encodeChars );
248 $this->assertEquals( $expected, $actual );
249 }
250
251 public function provideEncodeHtmlEntities(): array {
252 return [
253 [ 'ab&cd<>e"f\'g&h"j', 'single', 'ab&amp;cd&lt;>e"f&apos;g&amp;h"j' ],
254 [ 'ab&cd<>e"f\'g&h"j', 'double', 'ab&amp;cd&lt;>e&quot;f\'g&amp;h&quot;j' ],
255 [ 'ab&cd<>e"f\'g&h"j', 'xml', 'ab&amp;cd&lt;>e"f\'g&amp;h"j' ],
256 ];
257 }
258
259 }