Debian Codegraph @

vendor/wikimedia/parsoid/tests/phpunit/Parsoid/Html2Wt/ConstrainedText/ConstrainedTextTest.php

raw
1 <?php
2 namespace Test\Parsoid\Html2Wt\ConstrainedText;
3
4 use Wikimedia\Parsoid\Html2Wt\ConstrainedText\AutoURLLinkText;
5 use Wikimedia\Parsoid\Html2Wt\ConstrainedText\ConstrainedText;
6 use Wikimedia\Parsoid\Html2Wt\ConstrainedText\ExtLinkText;
7 use Wikimedia\Parsoid\Html2Wt\ConstrainedText\LanguageVariantText;
8 use Wikimedia\Parsoid\Html2Wt\ConstrainedText\MagicLinkText;
9 use Wikimedia\Parsoid\Html2Wt\ConstrainedText\WikiLinkText;
10 use Wikimedia\Parsoid\Mocks\MockEnv;
11 use Wikimedia\Parsoid\Utils\ContentUtils;
12 use Wikimedia\Parsoid\Utils\DOMCompat;
13 use Wikimedia\Parsoid\Utils\DOMDataUtils;
14 use Wikimedia\Parsoid\Utils\DOMUtils;
15
16 /**
17 * @coversDefaultClass \Wikimedia\Parsoid\Html2Wt\ConstrainedText\ConstrainedText
18 */
19 class ConstrainedTextTest extends \PHPUnit\Framework\TestCase {
20
21 /**
22 * @covers ::fromSelSer
23 * @covers ::fromSelSerImpl
24 * @covers ::escape
25 * @covers ::escapeLine
26 * @dataProvider provideConstrainedText
27 */
28 public function testConstrainedText( array $t ) {
29 $t = (object)$t;
30 // Set up environment and test data
31 $env = new MockEnv( [
32 'linkPrefixRegex' => $t->linkPrefixRegex ?? null,
33 'linkTrailRegex' => $t->linkTrailRegex ?? null,
34 ] );
35 $doc = ContentUtils::createAndLoadDocument( $t->html );
36 $node = DOMCompat::getBody( $doc )->firstChild;
37 DOMUtils::assertElt( $node );
38 $dataParsoid = DOMDataUtils::getDataParsoid( $node );
39
40 // Test ConstrainedText.fromSelSer
41 $ct = ConstrainedText::fromSelSer( $t->text, $node, $dataParsoid, $env );
42 $this->assertIsArray( $ct );
43 $this->assertSameSize( $t->types, $ct );
44 $actualNames = array_map( static function ( $x ) {
45 return get_class( $x );
46 }, $ct );
47 foreach ( $t->types as $i => $name ) {
48 $this->assertEquals( $name, $actualNames[$i] );
49 }
50
51 // Test ConstrainedText::escapeLine
52 foreach ( $t->escapes as $e ) {
53 $e = (object)$e;
54 $nct = $ct; // copy
55 if ( isset( $e->left ) ) {
56 $n = $node->ownerDocument->createTextNode( $e->left );
57 array_unshift( $nct, ConstrainedText::cast( $e->left, $n ) );
58 }
59 if ( isset( $e->right ) ) {
60 $n = $node->ownerDocument->createTextNode( $e->right );
61 $nct[] = ConstrainedText::cast( $e->right, $n );
62 }
63 $r = ConstrainedText::escapeLine( $nct );
64 $this->assertEquals( $e->output, $r );
65 }
66 }
67
68 // phpcs:disable Generic.Files.LineLength.TooLong
69 public function provideConstrainedText(): array {
70 return [
71 [ [
72 'name' => 'WikiLinkText: Simple',
73 'linkTrailRegex' => /* RegExp */ '/^([a-z]+)/', // enwiki
74 'html' => "<a rel=\"mw:WikiLink\" href=\"./Foo\" title=\"Foo\" data-parsoid='{\"stx\":\"simple\",\"a\":{\"href\":\"./Foo\"},\"sa\":{\"href\":\"Foo\"}}'>Foo</a>",
75 'types' => [ WikiLinkText::class ],
76 'text' => '[[Foo]]',
77 'escapes' => [
78 [
79 'output' => '[[Foo]]'
80 ],
81 [
82 'left' => 'bar ',
83 'right' => ' bat',
84 'output' => 'bar [[Foo]] bat'
85 ],
86 [
87 'left' => '[',
88 'right' => ']',
89 'output' => '[<nowiki/>[[Foo]]]'
90 ],
91 [
92 // not a link trail
93 'right' => "'s",
94 'output' => "[[Foo]]'s"
95 ],
96 [
97 // a link trail
98 'right' => 's',
99 'output' => '[[Foo]]<nowiki/>s'
100 ]
101 ]
102 ] ],
103 [ [
104 'name' => 'WikiLinkText: iswiki linkprefix/linktrail',
105 'linkPrefixRegex' => /* RegExp */ "/[áÁðÐéÉíÍóÓúÚýÝþÞæÆöÖA-Za-z–-]+\$/",
106 'linkTrailRegex' => /* RegExp */ "/^([áðéíóúýþæöa-z-–]+)/",
107 'html' => "<a rel=\"mw:WikiLink\" href=\"./Foo\" title=\"Foo\" data-parsoid='{\"stx\":\"simple\",\"a\":{\"href\":\"./Foo\"},\"sa\":{\"href\":\"Foo\"}}'>Foo</a>",
108 'types' => [ WikiLinkText::class ],
109 'text' => '[[Foo]]',
110 'escapes' => [
111 [
112 'left' => 'bar ',
113 'right' => ' bat',
114 'output' => 'bar [[Foo]] bat'
115 ],
116 [
117 'left' => '-',
118 'right' => '-',
119 'output' => '-<nowiki/>[[Foo]]<nowiki/>-'
120 ]
121 ]
122 ] ],
123 [ [
124 'name' => 'WikiLinkText: iswiki greedy linktrails',
125 'linkPrefixRegex' => /* RegExp */ "/[áÁðÐéÉíÍóÓúÚýÝþÞæÆöÖA-Za-z–-]+\$/",
126 'linkTrailRegex' => /* RegExp */ "/^([áðéíóúýþæöa-z-–]+)/",
127 'html' => "<p data-parsoid='{\"dsr\":[0,11,0,0]}'><a rel=\"mw:WikiLink\" href=\"./A\" title=\"A\" data-parsoid='{\"stx\":\"simple\",\"a\":{\"href\":\"./A\"},\"sa\":{\"href\":\"a\"},\"dsr\":[0,6,2,3],\"tail\":\"-\"}'>a-</a><a rel=\"mw:WikiLink\" href=\"./B\" title=\"B\" data-parsoid='{\"stx\":\"simple\",\"a\":{\"href\":\"./B\"},\"sa\":{\"href\":\"b\"},\"dsr\":[6,11,2,2]}'>b</a></p>",
128 'types' => [
129 ConstrainedText::class,
130 WikiLinkText::class,
131 ConstrainedText::class,
132 WikiLinkText::class,
133 ],
134 'text' => '[[a]]-[[b]]',
135 'escapes' => [ [
136 // this would be '[[a]]-<nowiki/>[[b]] if the "greedy"
137 // functionality wasn't present; see commit
138 // 88605a4a7a37a61da76238db6d3fff756e8514f1
139 'output' => '[[a]]-[[b]]'
140 ]
141 ]
142 ] ],
143 [ [
144 'name' => 'ExtLinkText',
145 'html' => "<a rel=\"mw:ExtLink\" href=\"https://example.com\" class=\"external autonumber\"></a>",
146 'types' => [
147 ExtLinkText::class,
148 ],
149 'text' => '[https://example.com]',
150 'escapes' => [
151 [
152 // ExtLinkText isn't very interesting
153 'output' => '[https://example.com]'
154 ],
155 [
156 'left' => '[',
157 'right' => ']',
158 // FIXME This output is wrong! See: T220018
159 'output' => '[[https://example.com]]'
160 ]
161 ]
162 ] ],
163 [ [
164 'name' => 'AutoURLLinkText: no paren',
165 'html' => "<a rel=\"mw:ExtLink\" href=\"http://example.com\" class=\"external free\" data-parsoid='{\"stx\":\"url\",\"dsr\":[0,18,0,0]}'>http://example.com</a>",
166 'types' => [
167 AutoURLLinkText::class
168 ],
169 'text' => 'https://example.com',
170 'escapes' => [
171 [
172 'output' => 'https://example.com'
173 ],
174 [
175 // Non-word characters are find in the prefix
176 'left' => '(',
177 'output' => '(https://example.com'
178 ],
179 [
180 // Word characters need an escape
181 'left' => 'foo',
182 'right' => 'bar',
183 'output' => 'foo<nowiki/>https://example.com<nowiki/>bar'
184 ],
185 [
186 // Close paren is fine in the trailing context so long
187 // as the URL doesn't have a paren.
188 'left' => '(',
189 'right' => ')',
190 'output' => '(https://example.com)'
191 ],
192 [
193 // Ampersand isn't allowed in the trailing context...
194 'right' => '&',
195 'output' => 'https://example.com<nowiki/>&'
196 ],
197 [
198 // ...but an entity will terminate the autourl
199 'right' => '&lt;',
200 'output' => 'https://example.com&lt;'
201 ],
202 [
203 // Single quote isn't allowed...
204 'right' => "'",
205 'output' => "https://example.com<nowiki/>'"
206 ],
207 [
208 // ...but double-quote (aka bold or italics) is fine
209 'left' => "''",
210 'right' => "''",
211 'output' => "''https://example.com''"
212 ],
213 [
214 // Punctuation is okay.
215 'right' => '.',
216 'output' => 'https://example.com.'
217 ],
218 [
219 'left' => '[',
220 'right' => ' foo]',
221 // FIXME This output is wrong! See: T220018
222 'output' => '[https://example.com foo]'
223 ]
224 ]
225 ] ],
226 [ [
227 'name' => 'AutoURLLinkText: w/ paren',
228 'html' => "<a rel=\"mw:ExtLink\" href=\"http://example.com/foo(bar\" class=\"external free\" data-parsoid='{\"stx\":\"url\",\"dsr\":[0,26,0,0]}'>http://example.com/foo(bar</a></p>",
229 'types' => [
230 AutoURLLinkText::class
231 ],
232 'text' => 'https://example.com/foo(bar',
233 'escapes' => [
234 [
235 'output' => 'https://example.com/foo(bar'
236 ],
237 [
238 // Close paren is escaped in the trailing context since
239 // the URL has a paren.
240 'left' => '(',
241 'right' => ')',
242 'output' => '(https://example.com/foo(bar<nowiki/>)'
243 ]
244 ]
245 ] ],
246 [ [
247 'name' => 'AutoURLLinkText: w/ ampersand',
248 'html' => "<a rel=\"mw:ExtLink\" href=\"http://example.com?foo&amp;lt\" class=\"external free\" data-parsoid='{\"stx\":\"url\",\"dsr\":[0,25,0,0]}'>http://example.com?foo&amp;lt</a>",
249 'types' => [
250 AutoURLLinkText::class
251 ],
252 'text' => 'https://example.com?foo&lt',
253 'escapes' => [
254 [
255 'output' => 'https://example.com?foo&lt'
256 ],
257 [
258 'right' => '.',
259 'output' => 'https://example.com?foo&lt.'
260 ],
261 [
262 // Careful of right contexts which could complete an
263 // entity
264 'right' => ';',
265 'output' => 'https://example.com?foo&lt<nowiki/>;'
266 ]
267 ]
268 ] ],
269 [ [
270 'name' => 'MagicLinkText',
271 'html' => "<a href=\"./Special:BookSources/1234567890\" rel=\"mw:WikiLink\" data-parsoid='{\"stx\":\"magiclink\",\"dsr\":[0,15,2,2]}'>ISBN 1234567890</a>",
272 'types' => [
273 MagicLinkText::class
274 ],
275 'text' => 'ISBN 1234567890',
276 'escapes' => [
277 [
278 'output' => 'ISBN 1234567890'
279 ],
280 [
281 'left' => 'I',
282 'right' => '1',
283 'output' => 'I<nowiki/>ISBN 1234567890<nowiki/>1'
284 ]
285 ]
286 ] ],
287 [ [
288 'name' => 'LanguageVariantText',
289 'html' => "<span typeof=\"mw:LanguageVariant\" data-mw-variant='{\"disabled\":{\"t\":\"raw\"}}' data-parsoid='{\"fl\":[],\"src\":\"-{raw}-\",\"dsr\":[0,7,null,2]}'></span>",
290 'types' => [
291 LanguageVariantText::class
292 ],
293 'text' => '-{raw}-',
294 'escapes' => [
295 [
296 'output' => '-{raw}-'
297 ],
298 [
299 // single | at SOL causes issues with table markup
300 'left' => '|',
301 'output' => '|<nowiki/>-{raw}-'
302 ],
303 [
304 'left' => '||',
305 'output' => '||-{raw}-'
306 ]
307 ]
308 ] ],
309 ];
310 }
311 }