Debian Codegraph @

vendor/wikimedia/parsoid/tests/phpunit/Parsoid/Wt2Html/DOM/Handlers/CleanUpTest.php

raw
1 <?php
2
3 namespace Test\Parsoid\Wt2Html\DOM\Handlers;
4
5 use PHPUnit\Framework\TestCase;
6 use Wikimedia\Parsoid\DOM\Document;
7 use Wikimedia\Parsoid\DOM\Element;
8 use Wikimedia\Parsoid\Mocks\MockDataAccess;
9 use Wikimedia\Parsoid\Mocks\MockEnv;
10 use Wikimedia\Parsoid\Mocks\MockPageConfig;
11 use Wikimedia\Parsoid\Mocks\MockPageContent;
12 use Wikimedia\Parsoid\Mocks\MockSiteConfig;
13 use Wikimedia\Parsoid\Parsoid;
14 use Wikimedia\Parsoid\Utils\ContentUtils;
15 use Wikimedia\Parsoid\Utils\DOMCompat;
16 use Wikimedia\Parsoid\Utils\DOMDataUtils;
17 use Wikimedia\Parsoid\Utils\DOMTraverser;
18
19 /**
20 * Test CleanUp, the tests used for validating CleanUp class port from JS to PHP
21 * and based on tests/mocha/cleanup.js
22 * @coversDefaultClass \Wikimedia\Parsoid\Wt2Html\DOM\Handlers\CleanUp
23 */
24 class CleanUpTest extends TestCase {
25
26 /** @var Document[] */
27 private $liveDocs = [];
28
29 private function parseWT( string $wt ): Element {
30 $siteConfig = new MockSiteConfig( [] );
31 $dataAccess = new MockDataAccess( $siteConfig, [] );
32 $parsoid = new Parsoid( $siteConfig, $dataAccess );
33
34 $content = new MockPageContent( [ 'main' => $wt ] );
35 $pageConfig = new MockPageConfig( $siteConfig, [], $content );
36 $html = $parsoid->wikitext2html( $pageConfig, [ "wrapSections" => false ] );
37
38 $doc = ContentUtils::createAndLoadDocument( $html );
39
40 // Prevent GC from reclaiming $doc once we exit this function.
41 // Necessary hack because we use PHPDOM which wraps libxml.
42 $this->liveDocs[] = $doc;
43
44 return DOMCompat::getBody( $doc );
45 }
46
47 private function addHandlers( DOMTraverser $domVisitor, array $tags, bool $value ): void {
48 foreach ( $tags as $tag ) {
49 $domVisitor->addHandler( $tag,
50 function ( ...$args ) use ( $value ) {
51 return $this->autoInsValidation( $value, ...$args );
52 }
53 );
54 }
55 }
56
57 private function autoInsValidation( bool $expectedValue, Element $node ): bool {
58 $dp = DOMDataUtils::getDataParsoid( $node );
59 $autoInsEnd = isset( $dp->autoInsertedEnd );
60 $this->assertEquals( $expectedValue, $autoInsEnd );
61 return true;
62 }
63
64 /**
65 * test searching for autoInsertedEnd flags using DOM traversal helper functions
66 * @covers ::finalCleanup
67 * @dataProvider provideCleanUp
68 * @param string $test
69 */
70 public function testfinalCleanUp( string $test ): void {
71 // Cleanup DOM pass should confirm removal of autoInsertedEnd flag
72 // for wikitext table tags without closing tag syntax using DOM traversal
73 $body = $this->parseWT( $test );
74
75 $domVisitor = new DOMTraverser();
76 $tags = [ 'tr', 'td', ];
77 $this->addHandlers( $domVisitor, $tags, false );
78
79 $domVisitor->traverse( null, $body );
80 }
81
82 public function provideCleanUp(): array {
83 $test = [
84 "{|",
85 "|a",
86 "|}"
87 ];
88 return [ [ implode( "\n", $test ) ] ];
89 }
90
91 /**
92 * test for autoInsertedEnd flags in all possible WT tags with no closing tags
93 * "PRE", "LI", "DT", "DD", "HR", "TR", "TD", "TH", "CAPTION"
94 * @covers ::finalCleanup
95 * @dataProvider provideCleanUpWT
96 * @param string $test
97 */
98 public function testCleanUpWT( string $test ): void {
99 // Cleanup DOM pass should confirm removal of autoInsertedEnd flag
100 // for all wikitext tags without closing tags
101 $body = $this->parseWT( $test );
102
103 $domVisitor = new DOMTraverser();
104 $tags = [ 'pre', 'li', 'dt', 'dd', 'hr', 'tr', 'td', 'th', 'caption' ];
105 $this->addHandlers( $domVisitor, $tags, false );
106
107 $domVisitor->traverse( null, $body );
108 }
109
110 public function provideCleanUpWT(): array {
111 $test = [
112 ";Definition list",
113 ":First definition",
114 ":Second definition",
115 "{|",
116 "|+ caption",
117 "|-",
118 "! heading 1!! heading 2",
119 "|-",
120 "|a||b",
121 "|}",
122 " preformatted text using leading whitespace as a pre wikitext symbol equivalent",
123 "{|",
124 "|c",
125 "|}",
126 "# Item 1",
127 "# Item 2",
128 ];
129 return [ [ implode( "\n", $test ) ] ];
130 }
131
132 /**
133 * test for autoInsertedEnd flags in all possible HTML wikitext tags with no closing tags
134 * "PRE", "LI", "DT", "DD", "HR", "TR", "TD", "TH", "CAPTION"
135 * @covers ::finalCleanup
136 * @dataProvider provideCleanUpHTML
137 * @param string $test
138 */
139 public function testCleanUpHTML( string $test ): void {
140 // Cleanup DOM pass should confirm presence of autoInsertedEnd flag
141 // for all HTML wikitext tags that can appear without closing tags
142 $body = $this->parseWT( $test );
143
144 $domVisitor = new DOMTraverser();
145 $tags = [ 'pre', 'li', 'dt', 'dd', 'hr', 'tr', 'td', 'th', 'caption' ];
146 $this->addHandlers( $domVisitor, $tags, true );
147
148 $domVisitor->traverse( null, $body );
149 }
150
151 public function provideCleanUpHTML(): array {
152 $test = [
153 "<dl>",
154 "<dt>Definition list",
155 "<dd>First definition",
156 "<dd>Second definition",
157 "</dl>",
158 "<table>",
159 "<caption>caption",
160 "<tr>",
161 "<th>heading 1",
162 "<th>heading 2",
163 "<tr>",
164 "<td>a",
165 "<td>b",
166 "</table>",
167 "<pre>preformatted text using leading whitespace as a pre wikitext symbol equivalent",
168 "<ol>",
169 "<li>Item 1",
170 "<li>Item 2",
171 "</ol>",
172 ];
173 return [ [ implode( "\n", $test ) ] ];
174 }
175
176 /**
177 * @param string $wt
178 * @param string $selector
179 * @param int $leadingWS
180 * @param int $trailingWS
181 * @dataProvider provideWhitespaceTrimming
182 * @covers ::trimWhiteSpace
183 */
184 public function testWhitespaceTrimming( string $wt, string $selector, int $leadingWS, int $trailingWS ): void {
185 $mockEnv = new MockEnv( [] );
186 $body = $this->parseWT( $wt );
187 $node = DOMCompat::querySelector( $body, $selector );
188 $this->assertEquals( $leadingWS, DOMDataUtils::getDataParsoid( $node )->dsr->leadingWS );
189 $this->assertEquals( $trailingWS, DOMDataUtils::getDataParsoid( $node )->dsr->trailingWS );
190 }
191
192 public function provideWhitespaceTrimming(): array {
193 return [
194 /* List item tests */
195 [ "*a", "li", 0, 0 ],
196 [ "* a", "li", 1, 0 ],
197 [ "* a ", "li", 4, 2 ],
198 [ "* <!--c-->a", "li", 1, 0 ],
199 [ "* <!--c--> a", "li", -1, 0 ],
200 [ "* <!--c--> a ", "li", -1, 1 ],
201 [ "* a ", "li", 1, 1 ],
202 [ "*a<!--c--> ", "li", 0, 1 ],
203 [ "*a <!--c--> ", "li", 0, -1 ],
204 [ "* [[Category:Foo]] a", "li", -1, 0 ],
205 [ "* x[[Category:Foo]] ", "li", 1, 1 ],
206 [ "* x [[Category:Foo]] ", "li", 1, -1 ],
207
208 /* Heading tests */
209 [ "==h==", "h2", 0, 0 ],
210 [ "== h ==", "h2", 2, 3 ],
211 [ "== <!--c-->h==", "h2", 1, 0 ],
212 [ "== <!--c--> h ==", "h2", -1, 1 ],
213 [ "== h<!--c--> ==", "h2", 1, 1 ],
214
215 /* Table tests */
216 [ "{|\n|x\n|}", "td", 0, 0 ],
217 [ "{|\n| x|| y \n|}", "td:first-child", 1, 0 ],
218 [ "{|\n| x|| y \n|}", "td:first-child + td", 1, 2 ],
219 [ "{|\n| <!--c-->x\n|}", "td", 1, 0 ],
220 [ "{|\n| <!--c--> x\n|}", "td", -1, 0 ],
221 [ "{|\n| <!--c-->x<!--c--> \n|}", "td", 1, 1 ],
222 [ "{|\n| <!--c--> x <!--c--> \n|}", "td", -1, -1 ],
223 ];
224 }
225 }