Debian Codegraph @

tests/phpunit/unit/includes/ResourceLoader/VueComponentParserTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Unit\ResourceLoader;
4
5 use MediaWiki\ResourceLoader\VueComponentParser;
6
7 /**
8 * @group ResourceLoader
9 * @covers \MediaWiki\ResourceLoader\VueComponentParser
10 */
11 class VueComponentParserTest extends \PHPUnit\Framework\TestCase {
12 /**
13 * @dataProvider provideTestParse
14 */
15 public function testParse( $html, $minify, $expectedResult, $message, $expectedException = '' ) {
16 $parser = new VueComponentParser;
17 if ( $expectedException !== '' ) {
18 $this->expectExceptionMessage( $expectedException );
19 }
20 $actualResult = $parser->parse( $html, [ 'minifyTemplate' => $minify ] );
21 $this->assertEquals( $expectedResult, $actualResult, $message );
22 }
23
24 public static function provideTestParse() {
25 return [
26 [
27 '<template><p>{{foo}}</p></template><script>bar</script><style>baz</style>',
28 false,
29 [
30 'script' => 'bar',
31 'template' => '<p>{{foo}}</p>',
32 'style' => 'baz',
33 'styleLang' => 'css',
34 ],
35 'Basic test'
36 ],
37 [
38 '<template><p>{{foo}}</p></template><style>baz</style>',
39 false,
40 null,
41 'Missing <script> tag',
42 'No <script> tag found',
43 ],
44 [
45 '<script>bar</script><style>baz</style>',
46 false,
47 null,
48 'Missing <template> tag',
49 'No <template> tag found',
50 ],
51 [
52 '<template><p>{{foo}}</p></template><script>bar</script>',
53 false,
54 [
55 'script' => 'bar',
56 'template' => '<p>{{foo}}</p>',
57 'style' => null,
58 'styleLang' => null,
59 ],
60 'Missing <style> tag'
61 ],
62 [
63 '<template><p>{{foo}}</p></template><template><p>{{quux}}</p></template><script>bar</script>',
64 false,
65 null,
66 'Two template tags',
67 'More than one <template> tag found'
68 ],
69 [
70 '<template><p>{{foo}}</p></template><script>bar</script><script>quux</script>',
71 false,
72 null,
73 'Two script tags',
74 'More than one <script> tag found'
75 ],
76 [
77 '<template><p>{{foo}}</p></template><script>bar</script><style>baz</style><style>quux</style>',
78 false,
79 null,
80 'Two style tags',
81 'More than one <style> tag found'
82 ],
83 [
84 '<template></template><script>bar</script><style>baz</style>',
85 false,
86 [
87 'script' => 'bar',
88 'template' => '',
89 'style' => 'baz',
90 'styleLang' => 'css',
91 ],
92 'Empty <template> tag',
93 ],
94 [
95 '<template><p>{{foo}}</p><p></p></template><script>bar</script>',
96 false,
97 [
98 'script' => 'bar',
99 'template' => '<p>{{foo}}</p><p></p>',
100 'style' => null,
101 'styleLang' => null,
102 ],
103 'Template with two root nodes',
104 ],
105 [
106 '<template><!-- Explanation --><p>{{foo}}</p></template><script>bar</script>',
107 false,
108 [
109 'script' => 'bar',
110 'template' => '<!-- Explanation --><p>{{foo}}</p>',
111 'style' => null,
112 'styleLang' => null,
113 ],
114 'Template with comment outside: comment preserved when not minifiying',
115 ],
116 [
117 '<template><!-- Explanation --><p>{{foo}}</p></template><script>bar</script>',
118 true,
119 [
120 'script' => 'bar',
121 'template' => '<p>{{foo}}</p>',
122 'style' => null,
123 'styleLang' => null,
124 ],
125 'Template with comment outside: comment removed when minifiying',
126 ],
127 [
128 '<template><p><!-- Explanation -->{{foo}}</p></template><script>bar</script>',
129 false,
130 [
131 'script' => 'bar',
132 'template' => '<p><!-- Explanation -->{{foo}}</p>',
133 'style' => null,
134 'styleLang' => null,
135 ],
136 'Template with comment inside: comment preserved when not minifying',
137 ],
138 [
139 '<template><p><!-- Explanation -->{{foo}}</p></template><script>bar</script>',
140 true,
141 [
142 'script' => 'bar',
143 'template' => '<p>{{foo}}</p>',
144 'style' => null,
145 'styleLang' => null,
146 ],
147 'Template with comment inside: comment removed when minifying',
148 ],
149 [
150 '<!-- Explanation --><template><p>{{foo}}</p></template><script>bar</script>',
151 true,
152 [
153 'script' => 'bar',
154 'template' => '<p>{{foo}}</p>',
155 'style' => null,
156 'styleLang' => null,
157 ],
158 'Comment before template tag',
159 ],
160 [
161 "<template>\n\t<div>\t\t<div> {{foo}}\n{{bar}} </div>\n\t</div>\n</template><script>blah</script>",
162 false,
163 [
164 'script' => 'blah',
165 'template' => "<div>\t\t<div> {{foo}}\n{{bar}} </div>\n\t</div>",
166 'style' => null,
167 'styleLang' => null,
168 ],
169 'Whitespace in <template> tag is preserved when not minifying',
170 ],
171 [
172 "<template>\n\t<div>\t\t<div> {{foo}}\n{{bar}} </div>\n\t</div>\n</template><script>blah</script>",
173 true,
174 [
175 'script' => 'blah',
176 'template' => "<div><div>{{foo}} {{bar}}</div></div>",
177 'style' => null,
178 'styleLang' => null,
179 ],
180 'Whitespace in <template> tag is stripped and collapsed when minifying',
181 ],
182 [
183 "<template>\n\t<div>\t\t<pre> {{foo}}\n{{bar}} </pre>\n\t</div>\n</template><script>blah</script>",
184 true,
185 [
186 'script' => 'blah',
187 'template' => "<div><pre> {{foo}}\n{{bar}} </pre></div>",
188 'style' => null,
189 'styleLang' => null,
190 ],
191 'Whitespace stripping and collapsing skips <pre> tags',
192 ],
193 [
194 "<template>\n\t<div>\t\t<pre>\n {{foo}}\n{{bar}} </pre>\n\t</div>\n</template><script>blah</script>",
195 false,
196 [
197 'script' => 'blah',
198 'template' => "<div>\t\t<pre> {{foo}}\n{{bar}} </pre>\n\t</div>",
199 'style' => null,
200 'styleLang' => null,
201 ],
202 'Single leading newline in <pre> is removed when not minifying',
203 ],
204 [
205 "<template>\n\t<div>\t\t<pre>\n {{foo}}\n{{bar}} </pre>\n\t</div>\n</template><script>blah</script>",
206 true,
207 [
208 'script' => 'blah',
209 'template' => "<div><pre> {{foo}}\n{{bar}} </pre></div>",
210 'style' => null,
211 'styleLang' => null,
212 ],
213 'Single leading newline in <pre> is removed when minifying',
214 ],
215 [
216 "<template>\n\t<div>\t\t<pre>\n\n {{foo}}\n{{bar}} </pre>\n\t</div>\n</template><script>blah</script>",
217 false,
218 [
219 'script' => 'blah',
220 'template' => "<div>\t\t<pre>\n\n {{foo}}\n{{bar}} </pre>\n\t</div>",
221 'style' => null,
222 'styleLang' => null,
223 ],
224 'Double leading newline in <pre> is preserved when not minifying',
225 ],
226 [
227 "<template>\n\t<div>\t\t<pre>\n\n {{foo}}\n{{bar}} </pre>\n\t</div>\n</template><script>blah</script>",
228 true,
229 [
230 'script' => 'blah',
231 'template' => "<div><pre>\n\n {{foo}}\n{{bar}} </pre></div>",
232 'style' => null,
233 'styleLang' => null,
234 ],
235 'Double leading newline in <pre> is preserved when minifying',
236 ],
237 [
238 "<template><a>!!!</a></template><script>var x = '<a>!!!</a>';</script>",
239 false,
240 [
241 'script' => "var x = '<a>!!!</a>';",
242 'template' => "<a>!!!</a>",
243 'style' => null,
244 'styleLang' => null,
245 ],
246 'Script tag with HTML string'
247 ],
248 [
249 '<template><p>{{foo}}</p></template><script>bar</script><style lang="less">baz</style>',
250 false,
251 [
252 'script' => 'bar',
253 'template' => '<p>{{foo}}</p>',
254 'style' => 'baz',
255 'styleLang' => 'less',
256 ],
257 'Style tag with lang="less"',
258 ],
259 [
260 '<template><p>{{foo}}</p></template><script>bar</script><style lang="quux">baz</style>',
261 false,
262 null,
263 'Style tag with invalid language',
264 '<style lang="quux"> is invalid, lang must be "css" or "less"',
265 ],
266 [
267 '<template><p>{{foo}}</p></template><script>bar</script><style scoped>baz</style>',
268 false,
269 null,
270 'Scoped style tag',
271 '<style> may not have the scoped attribute',
272 ],
273 [
274 '<template><p>{{foo}}</p></template><script>bar</script><style lang="less" scoped>baz</style>',
275 false,
276 null,
277 'Scoped style tag with lang="less"',
278 '<style> may not have the scoped attribute',
279 ],
280 [
281 '<template><p>{{foo}}</p></template><script>bar</script><style module>baz</style>',
282 false,
283 null,
284 '<style module> tag',
285 '<style> may not have the module attribute',
286 ],
287 [
288 '<template functional><p>{{foo}}</p></template><script>bar</script><style>baz</style>',
289 false,
290 null,
291 '<template functional> tag',
292 '<template> may not have any attributes'
293 ],
294 [
295 '<template><p>{{foo}}</p></template><script foo>bar</script><style>baz</style>',
296 false,
297 null,
298 '<script> tag with attribute',
299 '<script> may not have any attributes'
300 ],
301 [
302 '<template><p :class="classObj">{{foo}}</p></template><script>bar</script>',
303 false,
304 [
305 'script' => 'bar',
306 'template' => '<p :class="classObj">{{foo}}</p>',
307 'style' => null,
308 'styleLang' => null,
309 ],
310 'Attribute with : in the name'
311 ],
312 [
313 '<template><a @click="onClick">{{foo}}</a></template><script>bar</script>',
314 false,
315 [
316 'script' => 'bar',
317 'template' => '<a @click="onClick">{{foo}}</a>',
318 'style' => null,
319 'styleLang' => null,
320 ],
321 'Attribute with @ in the name'
322 ],
323 [
324 '<template><div><template #header></template><template #footer="foo"></template></div></template><script>bar</script>',
325 false,
326 [
327 'script' => 'bar',
328 'template' => '<div><template #header=""></template><template #footer="foo"></template></div>',
329 'style' => null,
330 'styleLang' => null,
331 ],
332 'Attribute with # in the name'
333 ],
334 [
335 '<template><p><mw-button /><mw-blah>foo</mw-blah></p></template><script>bar</script>',
336 false,
337 [
338 'script' => 'bar',
339 'template' => '<p><mw-button><mw-blah>foo</mw-blah></mw-button></p>',
340 'style' => null,
341 'styleLang' => null,
342 ],
343 'Template with self-closing tag (broken)'
344 ],
345 ];
346 // phpcs:enable
347 }
348 }