Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/tests/preinit/ve.utils.parsoid.test.js

raw symbols SCIP
1 /*!
2 * Parsoid utilities tests.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 QUnit.module( 've.utils.parsoid', ve.test.utils.newMwEnvironment() );
9
10 QUnit.test( 'reduplicateStyles/deduplicateStyles', ( assert ) => {
11 // Test cases based on this page and the templates there:
12 // https://en.wikipedia.beta.wmflabs.org/wiki/Table_templated
13 const stylesCases = [
14 {
15 msg: 'styles are deduplicated',
16 deduplicated: `<span about="#mwt1" typeof="mw:Transclusion" data-parsoid='{"pi":[[],[{"k":"1"},{"k":"2"}],[{"k":"1"},{"k":"2"}],[{"k":"1"},{"k":"2"}],[]],"dsr":[0,107,null,null]}' data-mw='{"parts":[{"template":{"target":{"wt":"table start","href":"./Template:Table_start"},"params":{},"i":0}},"\\n",{"template":{"target":{"wt":"table row","href":"./Template:Table_row"},"params":{"1":{"wt":"Hello"},"2":{"wt":"good"}},"i":1}},"\\n",{"template":{"target":{"wt":"table row","href":"./Template:Table_row"},"params":{"1":{"wt":"Goodbye"},"2":{"wt":"bad"}},"i":2}},"\\n",{"template":{"target":{"wt":"table row","href":"./Template:Table_row"},"params":{"1":{"wt":"Welcome"},"2":{"wt":"good"}},"i":3}},"\\n",{"template":{"target":{"wt":"table end","href":"./Template:Table_end"},"params":{},"i":4}}]}'>
17 </span><table class="wikitable" about="#mwt1">
18 <tbody><tr>
19 <td class="good"><style data-mw-deduplicate="TemplateStyles:r5432" typeof="mw:Extension/templatestyles" about="#mwt4" data-mw='{"name":"templatestyles","attrs":{"src":"Table row/styles.css"}}'>.mw-parser-output .good{background:#9EFF9E}.mw-parser-output .bad{background:#FFC7C7}</style>Hello</td></tr>
20 <tr>
21 <td class="bad"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r5432" about="#mwt7" typeof="mw:Extension/templatestyles" data-mw='{"name":"templatestyles","attrs":{"src":"Table row/styles.css"}}'/>Goodbye</td></tr>
22 <tr>
23 <td class="good"><link rel="mw-deduplicated-inline-style" href="mw-data:TemplateStyles:r5432" about="#mwt10" typeof="mw:Extension/templatestyles" data-mw='{"name":"templatestyles","attrs":{"src":"Table row/styles.css"}}'/>Welcome</td></tr>
24 </tbody></table>`,
25 reduplicated: `<span about="#mwt1" typeof="mw:Transclusion" data-parsoid='{"pi":[[],[{"k":"1"},{"k":"2"}],[{"k":"1"},{"k":"2"}],[{"k":"1"},{"k":"2"}],[]],"dsr":[0,107,null,null]}' data-mw='{"parts":[{"template":{"target":{"wt":"table start","href":"./Template:Table_start"},"params":{},"i":0}},"\\n",{"template":{"target":{"wt":"table row","href":"./Template:Table_row"},"params":{"1":{"wt":"Hello"},"2":{"wt":"good"}},"i":1}},"\\n",{"template":{"target":{"wt":"table row","href":"./Template:Table_row"},"params":{"1":{"wt":"Goodbye"},"2":{"wt":"bad"}},"i":2}},"\\n",{"template":{"target":{"wt":"table row","href":"./Template:Table_row"},"params":{"1":{"wt":"Welcome"},"2":{"wt":"good"}},"i":3}},"\\n",{"template":{"target":{"wt":"table end","href":"./Template:Table_end"},"params":{},"i":4}}]}'>
26 </span><table class="wikitable" about="#mwt1">
27 <tbody><tr>
28 <td class="good"><style data-mw-deduplicate="TemplateStyles:r5432" typeof="mw:Extension/templatestyles" about="#mwt4" data-mw='{"name":"templatestyles","attrs":{"src":"Table row/styles.css"}}'>.mw-parser-output .good{background:#9EFF9E}.mw-parser-output .bad{background:#FFC7C7}</style>Hello</td></tr>
29 <tr>
30 <td class="bad"><style data-mw-deduplicate="TemplateStyles:r5432" typeof="mw:Extension/templatestyles" about="#mwt7" data-mw='{"name":"templatestyles","attrs":{"src":"Table row/styles.css"}}'>.mw-parser-output .good{background:#9EFF9E}.mw-parser-output .bad{background:#FFC7C7}</style>Goodbye</td></tr>
31 <tr>
32 <td class="good"><style data-mw-deduplicate="TemplateStyles:r5432" typeof="mw:Extension/templatestyles" about="#mwt10" data-mw='{"name":"templatestyles","attrs":{"src":"Table row/styles.css"}}'>.mw-parser-output .good{background:#9EFF9E}.mw-parser-output .bad{background:#FFC7C7}</style>Welcome</td></tr>
33 </tbody></table>`
34 },
35 {
36 msg: 'styles in fosterable positions are NOT deduplicated, but they are emptied',
37 deduplicated: `<span about="#mwt1" typeof="mw:Transclusion" data-parsoid='{"pi":[[],[{"k":"1"},{"k":"2"}],[{"k":"1"},{"k":"2"}],[{"k":"1"},{"k":"2"}],[]],"dsr":[0,107,null,null]}' data-mw='{"parts":[{"template":{"target":{"wt":"table start","href":"./Template:Table_start"},"params":{},"i":0}},"\\n",{"template":{"target":{"wt":"table row","href":"./Template:Table_row"},"params":{"1":{"wt":"Hello"},"2":{"wt":"good"}},"i":1}},"\\n",{"template":{"target":{"wt":"table row","href":"./Template:Table_row"},"params":{"1":{"wt":"Goodbye"},"2":{"wt":"bad"}},"i":2}},"\\n",{"template":{"target":{"wt":"table row","href":"./Template:Table_row"},"params":{"1":{"wt":"Welcome"},"2":{"wt":"good"}},"i":3}},"\\n",{"template":{"target":{"wt":"table end","href":"./Template:Table_end"},"params":{},"i":4}}]}'>
38 </span><table class="wikitable" about="#mwt1">
39 <tbody><tr>
40 <style data-mw-deduplicate="TemplateStyles:r5432" typeof="mw:Extension/templatestyles" about="#mwt4" data-mw='{"name":"templatestyles","attrs":{"src":"Table row/styles.css"}}'>.mw-parser-output .good{background:#9EFF9E}.mw-parser-output .bad{background:#FFC7C7}</style>
41 <td class="good">Hello</td></tr>
42 <tr>
43 <style data-mw-deduplicate="TemplateStyles:r5432" typeof="mw:Extension/templatestyles" about="#mwt7" data-mw='{"name":"templatestyles","attrs":{"src":"Table row/styles.css"}}'></style>
44 <td class="bad">Goodbye</td></tr>
45 <tr>
46 <style data-mw-deduplicate="TemplateStyles:r5432" typeof="mw:Extension/templatestyles" about="#mwt10" data-mw='{"name":"templatestyles","attrs":{"src":"Table row/styles.css"}}'></style>
47 <td class="good">Welcome</td></tr>
48 </tbody></table>`,
49 reduplicated: `<span about="#mwt1" typeof="mw:Transclusion" data-parsoid='{"pi":[[],[{"k":"1"},{"k":"2"}],[{"k":"1"},{"k":"2"}],[{"k":"1"},{"k":"2"}],[]],"dsr":[0,107,null,null]}' data-mw='{"parts":[{"template":{"target":{"wt":"table start","href":"./Template:Table_start"},"params":{},"i":0}},"\\n",{"template":{"target":{"wt":"table row","href":"./Template:Table_row"},"params":{"1":{"wt":"Hello"},"2":{"wt":"good"}},"i":1}},"\\n",{"template":{"target":{"wt":"table row","href":"./Template:Table_row"},"params":{"1":{"wt":"Goodbye"},"2":{"wt":"bad"}},"i":2}},"\\n",{"template":{"target":{"wt":"table row","href":"./Template:Table_row"},"params":{"1":{"wt":"Welcome"},"2":{"wt":"good"}},"i":3}},"\\n",{"template":{"target":{"wt":"table end","href":"./Template:Table_end"},"params":{},"i":4}}]}'>
50 </span><table class="wikitable" about="#mwt1">
51 <tbody><tr>
52 <style data-mw-deduplicate="TemplateStyles:r5432" typeof="mw:Extension/templatestyles" about="#mwt4" data-mw='{"name":"templatestyles","attrs":{"src":"Table row/styles.css"}}'>.mw-parser-output .good{background:#9EFF9E}.mw-parser-output .bad{background:#FFC7C7}</style>
53 <td class="good">Hello</td></tr>
54 <tr>
55 <style data-mw-deduplicate="TemplateStyles:r5432" typeof="mw:Extension/templatestyles" about="#mwt7" data-mw='{"name":"templatestyles","attrs":{"src":"Table row/styles.css"}}'>.mw-parser-output .good{background:#9EFF9E}.mw-parser-output .bad{background:#FFC7C7}</style>
56 <td class="bad">Goodbye</td></tr>
57 <tr>
58 <style data-mw-deduplicate="TemplateStyles:r5432" typeof="mw:Extension/templatestyles" about="#mwt10" data-mw='{"name":"templatestyles","attrs":{"src":"Table row/styles.css"}}'>.mw-parser-output .good{background:#9EFF9E}.mw-parser-output .bad{background:#FFC7C7}</style>
59 <td class="good">Welcome</td></tr>
60 </tbody></table>`
61 }
62 ];
63
64 stylesCases.forEach( ( caseItem ) => {
65 const doc = ve.createDocumentFromHtml( caseItem.deduplicated );
66
67 // Test that we can re-duplicate styles, which were de-duplicated in Parsoid HTML
68 mw.libs.ve.reduplicateStyles( doc.body );
69 assert.equalDomElement(
70 doc.body,
71 ve.createDocumentFromHtml( caseItem.reduplicated ).body,
72 caseItem.msg + ' (reduplicated)'
73 );
74
75 // Test that we can de-duplicate styles again, producing a result identical to the Parsoid HTML
76 mw.libs.ve.deduplicateStyles( doc.body );
77 assert.equalDomElement(
78 doc.body,
79 ve.createDocumentFromHtml( caseItem.deduplicated ).body,
80 caseItem.msg + ' (deduplicated)'
81 );
82 } );
83 } );
84
85 QUnit.test( 'getTargetDataFromHref', ( assert ) => {
86 const doc = ve.createDocumentFromHtml( ve.test.utils.makeBaseTag( ve.dm.mwExample.baseUri ) );
87 mw.config.set( {
88 wgScript: '/w/index.php',
89 wgArticlePath: '/wiki/$1'
90 } );
91
92 const hrefCases = [
93 {
94 msg: 'Parsoid link',
95 href: './Foo',
96 expected: {
97 title: 'Foo',
98 isInternal: true
99 }
100 },
101 {
102 msg: 'Parsoid red link',
103 href: './Foo?action=edit&redlink=1',
104 expected: {
105 title: 'Foo',
106 isInternal: true
107 }
108 },
109 {
110 msg: 'Parsoid link with fragment',
111 href: './Foo#Bar',
112 expected: {
113 title: 'Foo#Bar',
114 isInternal: true
115 }
116 },
117 {
118 msg: 'Parsoid red link with fragment',
119 href: './Foo?action=edit&redlink=1#Bar',
120 expected: {
121 title: 'Foo#Bar',
122 isInternal: true
123 }
124 },
125 {
126 msg: 'Old parser link',
127 href: '/wiki/Foo',
128 expected: {
129 title: 'Foo',
130 isInternal: true
131 }
132 },
133 {
134 msg: 'Old parser red link',
135 href: '/w/index.php?title=Foo&action=edit&redlink=1',
136 expected: {
137 title: 'Foo',
138 isInternal: true
139 }
140 },
141 {
142 msg: 'Old parser link with fragment',
143 href: '/wiki/Foo#Bar',
144 expected: {
145 title: 'Foo#Bar',
146 isInternal: true
147 }
148 },
149 {
150 msg: 'Old parser red link with fragment (old parser does not actually generate links like this, but we recognize them)',
151 href: '/w/index.php?title=Foo&action=edit&redlink=1#Bar',
152 expected: {
153 title: 'Foo#Bar',
154 isInternal: true
155 }
156 },
157 {
158 msg: 'Full URL link to current wiki',
159 href: 'http://example.com/wiki/Foo',
160 expected: {
161 title: 'Foo',
162 isInternal: true
163 }
164 },
165 {
166 msg: 'Full URL red link to current wiki',
167 href: 'http://example.com/w/index.php?title=Foo&action=edit&redlink=1',
168 expected: {
169 title: 'Foo',
170 isInternal: true
171 }
172 },
173 {
174 msg: 'Full URL link to current wiki with different protocol',
175 href: 'https://example.com/wiki/Foo',
176 expected: {
177 title: 'Foo',
178 isInternal: true
179 }
180 },
181 {
182 msg: 'Full URL link to current wiki, but with no title',
183 href: 'http://example.com/wiki/',
184 expected: {
185 title: '',
186 isInternal: true
187 }
188 },
189 {
190 msg: 'Full URL link to current wiki, but with extra parameters (1)',
191 href: 'http://example.com/wiki/Foo?action=history',
192 expected: {
193 isInternal: false
194 }
195 },
196 {
197 msg: 'Full URL link to current wiki, but with extra parameters (2)',
198 href: 'http://example.com/w/index.php?title=Foo&action=edit&redlink=1&preload=Blah',
199 expected: {
200 isInternal: false
201 }
202 },
203 {
204 msg: 'Full URL link to current wiki that may be valid, but uses a weird URL pattern',
205 href: 'http://example.com/wiki/?title=Foo',
206 expected: {
207 isInternal: false
208 }
209 },
210 {
211 msg: 'Full URL link to another wiki',
212 href: 'http://example.net/wiki/Foo',
213 expected: {
214 isInternal: false
215 }
216 },
217 {
218 msg: 'Full URL red link to another wiki',
219 href: 'http://example.net/w/index.php?title=Foo&action=edit&redlink=1',
220 expected: {
221 isInternal: false
222 }
223 },
224 {
225 /* eslint-disable no-script-url */
226 msg: 'Invalid protocol is handled as internal link',
227 href: 'javascript:alert()',
228 expected: {
229 title: 'javascript:alert()',
230 isInternal: true
231 }
232 /* eslint-enable no-script-url */
233 },
234 {
235 msg: 'Invalid protocol is handled as internal link',
236 href: 'not-a-protocol:Some%20text',
237 expected: {
238 title: 'not-a-protocol:Some text',
239 isInternal: true
240 }
241 },
242 {
243 msg: 'Valid protocol is handled as external link',
244 href: 'https://example.net/',
245 expected: {
246 isInternal: false
247 }
248 },
249 {
250 msg: 'Valid protocol is handled as external link',
251 href: 'mailto:[email protected]',
252 expected: {
253 isInternal: false
254 }
255 }
256 ];
257 hrefCases.forEach( ( caseItem ) => {
258 const actualInfo = mw.libs.ve.getTargetDataFromHref( caseItem.href, doc );
259 assert.deepEqual( actualInfo, caseItem.expected, caseItem.msg );
260 } );
261 } );