Repository navigation
Expand file tree
/
Copy pathParserTest.php
More file actions
607 lines (556 loc) · 30.4 KB
/
Copy pathParserTest.php
File metadata and controls
607 lines (556 loc) · 30.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
<?php
namespace Thunder\Shortcode\Tests;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\PreserveGlobalState;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use Thunder\Shortcode\HandlerContainer\HandlerContainer;
use Thunder\Shortcode\Parser\RegularParser;
use Thunder\Shortcode\Parser\StackParser;
use Thunder\Shortcode\Parser\HybridParser;
use Thunder\Shortcode\Parser\ParserInterface;
use Thunder\Shortcode\Parser\RegexParser;
use Thunder\Shortcode\Parser\WordpressParser;
use Thunder\Shortcode\Shortcode\ParsedShortcode;
use Thunder\Shortcode\Shortcode\Shortcode;
use Thunder\Shortcode\Syntax\CommonSyntax;
use Thunder\Shortcode\Syntax\Syntax;
/**
* @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
*/
final class ParserTest extends AbstractTestCase
{
public function testParsersCountOffsetsInCharacters()
{
foreach(array(new RegularParser(), new StackParser(), new HybridParser()) as $parser) {
$this->assertShortcodes($parser->parse('żż[a]x[/a]'), array(
new ParsedShortcode(new Shortcode('a', array(), 'x', null), '[a]x[/a]', 2),
));
}
}
public function testParsersWithRegexSpecialCharactersInSyntax()
{
$syntax = new Syntax('[', ']', '.', '*', '|');
foreach(array(new RegularParser($syntax), new StackParser($syntax), new HybridParser($syntax), new RegexParser($syntax)) as $parser) {
$this->assertShortcodes($parser->parse('[a b*|x y|]c[.a]'), array(
new ParsedShortcode(new Shortcode('a', array('b' => 'x y'), 'c', null), '[a b*|x y|]c[.a]', 0),
));
}
}
public function testWordpressParserLowercasesParameterNames()
{
$parser = new WordpressParser();
$shortcodes = $parser->parse('[a FOO="x" Bar=y]');
$this->assertSame(array('foo' => 'x', 'bar' => 'y'), $shortcodes[0]->getParameters());
}
public function testWordpressParserQuotesNames()
{
$parser = WordpressParser::createFromNames(array('a.b'));
$this->assertCount(1, $parser->parse('[a.b]'));
$this->assertCount(0, $parser->parse('[axb]'));
}
public function testExceptionOnInvalidParserSyntax()
{
$classes = array(
'Thunder\Shortcode\Parser\RegularParser',
'Thunder\Shortcode\Parser\StackParser',
'Thunder\Shortcode\Parser\HybridParser',
'Thunder\Shortcode\Parser\RegexParser',
);
foreach($classes as $class) {
try {
new $class(new \stdClass());
$this->fail('Expected LogicException.');
} catch(\LogicException $e) {
$this->assertSame('Parameter $syntax must be an instance of SyntaxInterface.', $e->getMessage());
}
}
}
public function testStackParserExceptionOnInvalidUtf8()
{
$parser = new StackParser();
$this->willThrowException('RuntimeException');
$parser->parse("[a]\xff[/a]");
}
public function testHybridParserExceptionOnInvalidUtf8()
{
$parser = new HybridParser();
$this->willThrowException('RuntimeException');
$parser->parse("[a]\xff[/a]");
}
/**
* @param ParserInterface $parser
* @param string $code
* @param ParsedShortcode[] $expected
*
* @dataProvider provideShortcodes
*/
#[DataProvider('provideShortcodes')]
public function testParser(ParserInterface $parser, $code, array $expected)
{
$this->assertShortcodes($parser->parse($code), $expected);
}
public function testParsersEndValuesAtSyntaxSeparator()
{
$syntax = new Syntax('@', '#', '!', '&', '~');
foreach(array(new RegexParser($syntax), new RegularParser($syntax), new StackParser($syntax), new HybridParser($syntax)) as $parser) {
$this->assertShortcodes($parser->parse('@a&c=d b&e=f#x@!a# @b x&y&z#'), array(
new ParsedShortcode(new Shortcode('a', array('b' => 'e=f'), 'x', 'c=d'), '@a&c=d b&e=f#x@!a#', 0),
));
}
}
private function assertShortcodes(array $actual, array $expected)
{
$count = count($actual);
static::assertCount($count, $expected, 'counts');
for ($i = 0; $i < $count; $i++) {
static::assertSame($actual[$i]->getName(), $expected[$i]->getName(), 'name');
static::assertSame($actual[$i]->getParameters(), $expected[$i]->getParameters(), 'parameters');
static::assertSame($actual[$i]->getContent(), $expected[$i]->getContent(), 'content');
static::assertSame($actual[$i]->getText(), $expected[$i]->getText(), 'text');
static::assertSame($actual[$i]->getOffset(), $expected[$i]->getOffset(), 'offset');
static::assertSame($actual[$i]->getBbCode(), $expected[$i]->getBbCode(), 'bbCode');
}
}
public function testParsersHandleLongRunsOfEscapes()
{
$value = str_repeat('a\\x', 9000);
$text = '[a k='.$value.']x[/a] [b /]';
foreach(array(new RegularParser(), new StackParser(), new HybridParser()) as $parser) {
$this->assertShortcodes($parser->parse($text), array(
new ParsedShortcode(new Shortcode('a', array('k' => $value), 'x', null), '[a k='.$value.']x[/a]', 0),
new ParsedShortcode(new Shortcode('b', array(), null, null), '[b /]', 27012),
));
}
}
public static function provideShortcodes()
{
$s = new CommonSyntax();
$tests = array(
// invalid
array($s, '', array()),
array($s, '[]', array()),
array($s, '', array()),
array($s, 'x html([a. title][, alt][, classes]) x', array()),
array($s, '[/y]', array()),
array($s, '[sc', array()),
array($s, '[sc / [/sc]', array()),
array($s, '[sc arg="val', array()),
// single shortcodes
array($s, '[sc]', array(
new ParsedShortcode(new Shortcode('sc', array(), null), '[sc]', 0),
)),
array($s, '[sc arg=val]', array(
new ParsedShortcode(new Shortcode('sc', array('arg' => 'val'), null), '[sc arg=val]', 0),
)),
array($s, '[sc novalue arg="complex value"]', array(
new ParsedShortcode(new Shortcode('sc', array('novalue' => null, 'arg' => 'complex value'), null), '[sc novalue arg="complex value"]', 0),
)),
array($s, '[sc x="ąćęłńóśżź ĄĆĘŁŃÓŚŻŹ"]', array(
new ParsedShortcode(new Shortcode('sc', array('x' => 'ąćęłńóśżź ĄĆĘŁŃÓŚŻŹ'), null), '[sc x="ąćęłńóśżź ĄĆĘŁŃÓŚŻŹ"]', 0),
)),
array($s, '[sc x="multi'."\n".'line"]', array(
new ParsedShortcode(new Shortcode('sc', array('x' => 'multi'."\n".'line'), null), '[sc x="multi'."\n".'line"]', 0),
)),
array($s, '[sc noval x="val" y]content[/sc]', array(
new ParsedShortcode(new Shortcode('sc', array('noval' => null, 'x' => 'val', 'y' => null), 'content'), '[sc noval x="val" y]content[/sc]', 0),
)),
array($s, '[sc x="{..}"]', array(
new ParsedShortcode(new Shortcode('sc', array('x' => '{..}'), null), '[sc x="{..}"]', 0),
)),
array($s, '[sc a="x y" b="x" c=""]', array(
new ParsedShortcode(new Shortcode('sc', array('a' => 'x y', 'b' => 'x', 'c' => ''), null), '[sc a="x y" b="x" c=""]', 0),
)),
array($s, '[sc a="a \"\" b"]', array(
new ParsedShortcode(new Shortcode('sc', array('a' => 'a \"\" b'), null), '[sc a="a \"\" b"]', 0),
)),
array($s, '[sc/]', array(
new ParsedShortcode(new Shortcode('sc', array(), null), '[sc/]', 0),
)),
array($s, '[sc /]', array(
new ParsedShortcode(new Shortcode('sc', array(), null), '[sc /]', 0),
)),
array($s, '[sc arg=val cmp="a b"/]', array(
new ParsedShortcode(new Shortcode('sc', array('arg' => 'val', 'cmp' => 'a b'), null), '[sc arg=val cmp="a b"/]', 0),
)),
array($s, '[sc x y /]', array(
new ParsedShortcode(new Shortcode('sc', array('x' => null, 'y' => null), null), '[sc x y /]', 0),
)),
array($s, '[sc x="\ " /]', array(
new ParsedShortcode(new Shortcode('sc', array('x' => '\ '), null), '[sc x="\ " /]', 0),
)),
array($s, '[ sc x = "\ " y = value z / ]', array(
new ParsedShortcode(new Shortcode('sc', array('x' => '\ ', 'y' => 'value', 'z' => null), null), '[ sc x = "\ " y = value z / ]', 0),
)),
array($s, '[ sc x= "\ " y =value ] vv [ / sc ]', array(
new ParsedShortcode(new Shortcode('sc', array('x' => '\ ', 'y' => 'value'), ' vv '), '[ sc x= "\ " y =value ] vv [ / sc ]', 0),
)),
array($s, '[sc url="http://giggle.com/search" /]', array(
new ParsedShortcode(new Shortcode('sc', array('url' => 'http://giggle.com/search'), null), '[sc url="http://giggle.com/search" /]', 0),
)),
// bbcode
array($s, '[sc = "http://giggle.com/search" /]', array(
new ParsedShortcode(new Shortcode('sc', array(), null, 'http://giggle.com/search'), '[sc = "http://giggle.com/search" /]', 0),
)),
// multiple shortcodes
array($s, 'Lorem [ipsum] random [code-code arg=val] which is here', array(
new ParsedShortcode(new Shortcode('ipsum', array(), null), '[ipsum]', 6),
new ParsedShortcode(new Shortcode('code-code', array('arg' => 'val'), null), '[code-code arg=val]', 21),
)),
array($s, 'x [aa] x [aa] x', array(
new ParsedShortcode(new Shortcode('aa', array(), null), '[aa]', 2),
new ParsedShortcode(new Shortcode('aa', array(), null), '[aa]', 9),
)),
array($s, 'x [x]a[/x] x [x]a[/x] x', array(
new ParsedShortcode(new Shortcode('x', array(), 'a'), '[x]a[/x]', 2),
new ParsedShortcode(new Shortcode('x', array(), 'a'), '[x]a[/x]', 13),
)),
array($s, 'x [x x y=z a="b c"]a[/x] x [x x y=z a="b c"]a[/x] x', array(
new ParsedShortcode(new Shortcode('x', array('x' => null, 'y' => 'z', 'a' => 'b c'), 'a'), '[x x y=z a="b c"]a[/x]', 2),
new ParsedShortcode(new Shortcode('x', array('x' => null, 'y' => 'z', 'a' => 'b c'), 'a'), '[x x y=z a="b c"]a[/x]', 27),
)),
array($s, 'x [code /] y [code]z[/code] x [code] y [code/] a', array(
new ParsedShortcode(new Shortcode('code', array(), null), '[code /]', 2),
new ParsedShortcode(new Shortcode('code', array(), 'z'), '[code]z[/code]', 13),
new ParsedShortcode(new Shortcode('code', array(), null), '[code]', 30),
new ParsedShortcode(new Shortcode('code', array(), null), '[code/]', 39),
)),
array($s, 'x [code arg=val /] y [code cmp="xx"/] x [code x=y/] a', array(
new ParsedShortcode(new Shortcode('code', array('arg' => 'val'), null), '[code arg=val /]', 2),
new ParsedShortcode(new Shortcode('code', array('cmp' => 'xx'), null), '[code cmp="xx"/]', 21),
new ParsedShortcode(new Shortcode('code', array('x' => 'y'), null), '[code x=y/]', 40),
)),
array($s, 'x [ code arg=val /]a[ code/]c[x / ] m [ y ] c [ / y]', array(
new ParsedShortcode(new Shortcode('code', array('arg' => 'val'), null), '[ code arg=val /]', 2),
new ParsedShortcode(new Shortcode('code', array(), null), '[ code/]', 23),
new ParsedShortcode(new Shortcode('x', array(), null), '[x / ]', 32),
new ParsedShortcode(new Shortcode('y', array(), ' c '), '[ y ] c [ / y]', 47),
)),
// other syntax
array(new Syntax('[[', ']]', '//', '==', '""'), '[[code arg==""val oth""]]cont[[//code]]', array(
new ParsedShortcode(new Shortcode('code', array('arg' => 'val oth'), 'cont'), '[[code arg==""val oth""]]cont[[//code]]', 0),
)),
array(new Syntax('^', '$', '&', '!!!', '@@'), '^code a!!!@@\"\"@@ b!!!@@x\"y@@ c$cnt^&code$', array(
new ParsedShortcode(new Shortcode('code', array('a' => '\"\"', 'b' => 'x\"y', 'c' => null), 'cnt'), '^code a!!!@@\"\"@@ b!!!@@x\"y@@ c$cnt^&code$', 0),
)),
// UTF-8 sequences
array($s, '’’’’[sc]’’[sc]', array(
new ParsedShortcode(new Shortcode('sc', array(), null), '[sc]', 4),
new ParsedShortcode(new Shortcode('sc', array(), null), '[sc]', 10),
)),
// performance
// array($s, 'x [[aa]] y', array()),
array($s, str_repeat('[a]', 20), array_map(function($offset) { // 20
return new ParsedShortcode(new Shortcode('a', array(), null), '[a]', $offset);
}, range(0, 57, 3))),
array($s, '[b][a]x[a][/a][/a][/b] [b][a][a][/a]y[/a][/b]', array(
new ParsedShortcode(new Shortcode('b', array(), '[a]x[a][/a][/a]'), '[b][a]x[a][/a][/a][/b]', 0),
new ParsedShortcode(new Shortcode('b', array(), '[a][a][/a]y[/a]'), '[b][a][a][/a]y[/a][/b]', 23),
)),
array($s, '[b] [a][a][a] [/b] [b] [a][a][a] [/b]', array(
new ParsedShortcode(new Shortcode('b', array(), ' [a][a][a] '), '[b] [a][a][a] [/b]', 0),
new ParsedShortcode(new Shortcode('b', array(), ' [a][a][a] '), '[b] [a][a][a] [/b]', 19),
)),
array($s, '[name]random[/other]', array(
new ParsedShortcode(new Shortcode('name', array(), null), '[name]', 0),
)),
array($s, '[0][1][2][3]', array(
new ParsedShortcode(new Shortcode('0', array(), null), '[0]', 0),
new ParsedShortcode(new Shortcode('1', array(), null), '[1]', 3),
new ParsedShortcode(new Shortcode('2', array(), null), '[2]', 6),
new ParsedShortcode(new Shortcode('3', array(), null), '[3]', 9),
)),
array($s, '[_][na_me][_name][name_][n_am_e][_n_]', array(
new ParsedShortcode(new Shortcode('_', array(), null), '[_]', 0),
new ParsedShortcode(new Shortcode('na_me', array(), null), '[na_me]', 3),
new ParsedShortcode(new Shortcode('_name', array(), null), '[_name]', 10),
new ParsedShortcode(new Shortcode('name_', array(), null), '[name_]', 17),
new ParsedShortcode(new Shortcode('n_am_e', array(), null), '[n_am_e]', 24),
new ParsedShortcode(new Shortcode('_n_', array(), null), '[_n_]', 32),
)),
array($s, '[x]/[/x] [x]"[/x] [x]=[/x] [x]][/x] [x] [/x] [x]x[/x]', array(
new ParsedShortcode(new Shortcode('x', array(), '/'), '[x]/[/x]', 0),
new ParsedShortcode(new Shortcode('x', array(), '"'), '[x]"[/x]', 9),
new ParsedShortcode(new Shortcode('x', array(), '='), '[x]=[/x]', 18),
new ParsedShortcode(new Shortcode('x', array(), ']'), '[x]][/x]', 27),
new ParsedShortcode(new Shortcode('x', array(), ' '), '[x] [/x]', 36),
new ParsedShortcode(new Shortcode('x', array(), 'x'), '[x]x[/x]', 45),
)),
array($s, '[a]0[/a]', array(
new ParsedShortcode(new Shortcode('a', array(), '0'), '[a]0[/a]', 0),
)),
array($s, '[fa icon=fa-camera /] [fa icon=fa-camera extras=fa-4x /]', array(
new ParsedShortcode(new Shortcode('fa', array('icon' => 'fa-camera'), null), '[fa icon=fa-camera /]', 0),
new ParsedShortcode(new Shortcode('fa', array('icon' => 'fa-camera', 'extras' => 'fa-4x'), null), '[fa icon=fa-camera extras=fa-4x /]', 22),
)),
array($s, '[fa icon=fa-circle-o-notch extras=fa-spin,fa-3x /]', array(
new ParsedShortcode(new Shortcode('fa', array('icon' => 'fa-circle-o-notch', 'extras' => 'fa-spin,fa-3x'), null), '[fa icon=fa-circle-o-notch extras=fa-spin,fa-3x /]', 0),
)),
array($s, '[z =]', array()),
array($s, '[x=#F00 one=#F00 two="#F00"]', array(
new ParsedShortcode(new Shortcode('x', array('one' => '#F00', 'two' => '#F00'), null, '#F00'), '[x=#F00 one=#F00 two="#F00"]', 0),
)),
array($s, '[*] [* xyz arg=val]', array(
new ParsedShortcode(new Shortcode('*', array(), null, null), '[*]', 0),
new ParsedShortcode(new Shortcode('*', array('xyz' => null, 'arg' => 'val'), null, null), '[* xyz arg=val]', 4),
)),
array($s, '[*=bb x=y]cnt[/*]', array(
new ParsedShortcode(new Shortcode('*', array('x' => 'y'), 'cnt', 'bb'), '[*=bb x=y]cnt[/*]', 0),
)),
array($s, '[ [] ] [x] [ ] [/x] ] [] [ [y] ] [] [ [z] [/#] [/z] [ [] ] [/] [/y] ] [z] [ [/ [/] /] ] [/z]', array(
new ParsedShortcode(new Shortcode('x', array(), ' [ ] ', null), '[x] [ ] [/x]', 7),
new ParsedShortcode(new Shortcode('y', array(), ' ] [] [ [z] [/#] [/z] [ [] ] [/] ', null), '[y] ] [] [ [z] [/#] [/z] [ [] ] [/] [/y]', 27),
new ParsedShortcode(new Shortcode('z', array(), ' [ [/ [/] /] ] ', null), '[z] [ [/ [/] /] ] [/z]', 70),
)),
// falsy string values
array($s, '[a=0 b=0]0[/a]', array(
new ParsedShortcode(new Shortcode('a', array('b' => '0'), '0', '0'), '[a=0 b=0]0[/a]', 0),
)),
);
/**
* WordPress can't handle:
* - incorrect shortcode opening tag (blindly matches everything
* between opening token and closing token)
* - spaces between shortcode open tag and its name ([ name]),
* - spaces around BBCode part ([name = "bbcode"]),
* - escaped tokens anywhere in the arguments ([x arg=" \" "]),
* - configurable syntax (that's intended),
* - numbers in shortcode name.
*
* Tests cases from array above with identifiers in the array below must be skipped.
*/
$wordpressSkip = array(3, 6, 16, 21, 22, 23, 25, 32, 33, 34, 46, 47, 49, 51);
$result = array();
foreach($tests as $key => $test) {
$syntax = array_shift($test);
$result[] = array_merge(array(new RegexParser($syntax)), $test);
$result[] = array_merge(array(new StackParser($syntax)), $test);
$result[] = array_merge(array(new HybridParser($syntax)), $test);
$result[] = array_merge(array(new RegularParser($syntax)), $test);
if(!in_array($key, $wordpressSkip, true)) {
$result[] = array_merge(array(new WordpressParser()), $test);
}
}
return $result;
}
public function testStackParserMatchesRegularParserOnUnbalancedTags()
{
$inputs = array(
'[/x][a][/b]x[/a][/a][/c]',
'[a][b][c][d]x[/a][/d][/c][/b]',
'[a][b][c][/a][b][c][/c][/b][/a]',
'[a][b][/b][c][/a][c][/c][b][/b]',
'[a][a][a][/a][/a][/a][/a]',
'[a][b][a][b][/a][/b][/a][/b]',
'[0][0][/0][1][/0][/1][/0]',
'[a][a /][/a][a /][/a]',
str_repeat('[a][b]', 100).str_repeat('[/c][/a]', 100),
str_repeat('[a][b][c]', 50).str_repeat('[/b][c]', 50).str_repeat('[/c][/a]', 50),
str_repeat('[x][y][/x]', 100).str_repeat('[/y]', 100),
);
$regular = new RegularParser();
$stack = new StackParser();
$hybrid = new HybridParser();
foreach($inputs as $input) {
$this->assertShortcodes($stack->parse($input), $regular->parse($input));
$this->assertShortcodes($hybrid->parse($input), $regular->parse($input));
}
}
public function testIssue77()
{
// StackParser must reproduce RegularParser's backtracking behaviour exactly
foreach(array(new RegularParser(), new StackParser(), new HybridParser()) as $parser) {
$this->assertShortcodes($parser->parse('[a][x][/x][x k="v][/x][y]x[/y]'), array(
new ParsedShortcode(new Shortcode('a', array(), null, null), '[a]', 0),
new ParsedShortcode(new Shortcode('x', array(), '', null), '[x][/x]', 3),
new ParsedShortcode(new Shortcode('y', array(), 'x', null), '[y]x[/y]', 22),
));
$this->assertShortcodes($parser->parse('[a k="v][x][/x]'), array(
new ParsedShortcode(new Shortcode('x', array(), '', null), '[x][/x]', 8),
));
}
}
public function testStackParserSkipsEscapedTags()
{
foreach(array(new RegularParser(), new StackParser(), new HybridParser()) as $parser) {
$this->assertShortcodes($parser->parse('\\[a] [b]'), array(
new ParsedShortcode(new Shortcode('b', array(), null, null), '[b]', 5),
));
$this->assertShortcodes($parser->parse('[a]\\[/a][/a]'), array(
new ParsedShortcode(new Shortcode('a', array(), '\\[/a]', null), '[a]\\[/a][/a]', 0),
));
}
}
/**
* @dataProvider provideRunsOfOpeningTags
*/
#[DataProvider('provideRunsOfOpeningTags')]
public function testParsersFindTagsInsideRunsOfOpeningTags(ParserInterface $parser, $code, array $expected)
{
$this->assertShortcodes($parser->parse($code), $expected);
}
public static function provideRunsOfOpeningTags()
{
$double = new Syntax('[[', ']]', '//', '==', '""');
$curly = new Syntax('{{', '}}', '/', '=', '"');
$cases = array(
array($double, '[[[a]]', array(
new ParsedShortcode(new Shortcode('a', array(), null, null), '[[a]]', 1),
)),
array($double, '[[a]][[[b]]', array(
new ParsedShortcode(new Shortcode('a', array(), null, null), '[[a]]', 0),
new ParsedShortcode(new Shortcode('b', array(), null, null), '[[b]]', 6),
)),
array($double, '[[a]]x[[[//a]]', array(
new ParsedShortcode(new Shortcode('a', array(), 'x[', null), '[[a]]x[[[//a]]', 0),
)),
array($double, '[[[[a]]', array(
new ParsedShortcode(new Shortcode('a', array(), null, null), '[[a]]', 2),
)),
array($curly, 'a{{{x}}', array(
new ParsedShortcode(new Shortcode('x', array(), null, null), '{{x}}', 2),
)),
array($curly, '{{x}}{{{/x}}', array(
new ParsedShortcode(new Shortcode('x', array(), '{', null), '{{x}}{{{/x}}', 0),
)),
array($curly, '{{a k="{{"}}', array(
new ParsedShortcode(new Shortcode('a', array('k' => '{{'), null, null), '{{a k="{{"}}', 0),
)),
array(new Syntax('««', '»»', '/', '=', '"'), 'ż«««x»»', array(
new ParsedShortcode(new Shortcode('x', array(), null, null), '««x»»', 2),
)),
);
$result = array();
foreach($cases as $case) {
$result[] = array(new RegularParser($case[0]), $case[1], $case[2]);
$result[] = array(new StackParser($case[0]), $case[1], $case[2]);
$result[] = array(new HybridParser($case[0]), $case[1], $case[2]);
}
return $result;
}
public function testIssue119()
{
$cases = array(
'[a k="\"y"]inner[/a]' => new ParsedShortcode(new Shortcode('a', array('k' => '\"y'), 'inner', null), '[a k="\"y"]inner[/a]', 0),
'[a k=" \"y"]inner[/a]' => new ParsedShortcode(new Shortcode('a', array('k' => ' \"y'), 'inner', null), '[a k=" \"y"]inner[/a]', 0),
'[a k=" x\"y"]inner[/a]' => new ParsedShortcode(new Shortcode('a', array('k' => ' x\"y'), 'inner', null), '[a k=" x\"y"]inner[/a]', 0),
'[a k="x\"y"]inner[/a]' => new ParsedShortcode(new Shortcode('a', array('k' => 'x\"y'), 'inner', null), '[a k="x\"y"]inner[/a]', 0),
'[mention id=1 name="foo\"ff\""][/mention]' => new ParsedShortcode(new Shortcode('mention', array('id' => '1', 'name' => 'foo\"ff\"'), '', null), '[mention id=1 name="foo\"ff\""][/mention]', 0),
);
foreach(array(new RegularParser(), new StackParser(), new HybridParser()) as $parser) {
foreach($cases as $input => $expected) {
$this->assertShortcodes($parser->parse($input), array($expected));
}
$this->assertShortcodes($parser->parse('[a k="x\"y"]inner[/a] [mention id=1 name="foo\"ff\""][/mention]'), array(
new ParsedShortcode(new Shortcode('a', array('k' => 'x\"y'), 'inner', null), '[a k="x\"y"]inner[/a]', 0),
new ParsedShortcode(new Shortcode('mention', array('id' => '1', 'name' => 'foo\"ff\"'), '', null), '[mention id=1 name="foo\"ff\""][/mention]', 22),
));
}
}
public function testRegularParserRestoresXdebugNestingLevelOnException()
{
$level = ini_get('xdebug.max_nesting_level');
$parser = new RegularParser();
$exception = null;
try {
$parser->parse("[a]\xff[/a]");
} catch(\RuntimeException $e) {
$exception = $e;
}
$this->assertInstanceOf('RuntimeException', $exception);
$this->assertSame($level, ini_get('xdebug.max_nesting_level'));
}
public function testWordPress()
{
$parser = new WordpressParser();
$this->testParser($parser, '[code arg="<html" oth=\'val\']', array(
new ParsedShortcode(new Shortcode('code', array('arg' => '', 'oth' => 'val'), null), '[code arg="<html" oth=\'val\']', 0)
));
$this->testParser($parser, '[code "xxx"]', array(
new ParsedShortcode(new Shortcode('code', array('xxx' => null), null, null), '[code "xxx"]', 0)
));
$this->testParser($parser, '[code="xxx"] [code=yyy-aaa]', array(
new ParsedShortcode(new Shortcode('code', array('="xxx"' => null), null), '[code="xxx"]', 0),
new ParsedShortcode(new Shortcode('code', array('=yyy-aaa' => null), null), '[code=yyy-aaa]', 13)
));
$handlers = new HandlerContainer();
$handlers->add('_', function() {});
$handlers->add('na_me', function() {});
$handlers->add('_n_', function() {});
$this->testParser(WordpressParser::createFromHandlers($handlers), '[_][na_me][_name][name_][n_am_e][_n_]', array(
new ParsedShortcode(new Shortcode('_', array(), null), '[_]', 0),
new ParsedShortcode(new Shortcode('na_me', array(), null), '[na_me]', 3),
new ParsedShortcode(new Shortcode('_n_', array(), null), '[_n_]', 32),
));
$this->testParser(WordpressParser::createFromNames(array('_', 'na_me', '_n_')), '[_][na_me][_name][name_][n_am_e][_n_]', array(
new ParsedShortcode(new Shortcode('_', array(), null), '[_]', 0),
new ParsedShortcode(new Shortcode('na_me', array(), null), '[na_me]', 3),
new ParsedShortcode(new Shortcode('_n_', array(), null), '[_n_]', 32),
));
}
public function testParsersHandleLongUnbrokenText()
{
$text = '[a]x[/a] '.str_repeat('Q', 50000).' [b /]';
foreach(array(new RegularParser(), new StackParser(), new HybridParser(), new RegexParser(), new WordpressParser()) as $parser) {
$this->assertShortcodes($parser->parse($text), array(
new ParsedShortcode(new Shortcode('a', array(), 'x', null), '[a]x[/a]', 0),
new ParsedShortcode(new Shortcode('b', array(), null, null), '[b /]', 50010),
));
}
}
public function testWordpressInvalidNamesException()
{
$this->willThrowException('InvalidArgumentException');
WordpressParser::createFromNames(array('string', new \stdClass()));
}
public function testInstances()
{
static::assertInstanceOf('Thunder\Shortcode\Parser\WordPressParser', new WordpressParser());
static::assertInstanceOf('Thunder\Shortcode\Parser\StackParser', new StackParser());
static::assertInstanceOf('Thunder\Shortcode\Parser\HybridParser', new HybridParser());
static::assertInstanceOf('Thunder\Shortcode\Parser\RegularParser', new RegularParser());
}
public function testHybridParserAliasesStackParser()
{
static::assertSame('Thunder\Shortcode\Parser\StackParser', get_class(new \Thunder\Shortcode\Parser\HybridParser()));
static::assertInstanceOf('Thunder\Shortcode\Parser\HybridParser', new StackParser());
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
#[RunInSeparateProcess]
#[PreserveGlobalState(false)]
public function testHybridParserLoadsStackParser()
{
static::assertFalse(class_exists('Thunder\Shortcode\Parser\StackParser', false));
static::assertInstanceOf('Thunder\Shortcode\Parser\StackParser', new \Thunder\Shortcode\Parser\HybridParser());
}
public function testParsersEndNamesAtSyntaxTokens()
{
$syntax = new Syntax('(', ')', '*', '=', '"');
foreach(array(new RegularParser($syntax), new StackParser($syntax), new HybridParser($syntax)) as $parser) {
$this->assertShortcodes($parser->parse('(a*) (b)x(*b) (c*d)'), array(
new ParsedShortcode(new Shortcode('a', array(), null, null), '(a*)', 0),
new ParsedShortcode(new Shortcode('b', array(), 'x', null), '(b)x(*b)', 5),
));
}
}
public function testRegexParserCountsCharacterOffsetsWithCustomSyntax()
{
$parser = new RegexParser(new Syntax('(', ')', '/', '=', '"'));
$this->assertShortcodes($parser->parse('ż (a)'), array(
new ParsedShortcode(new Shortcode('a', array(), null, null), '(a)', 2),
));
}
public function testRegexParserParameterValues()
{
$parser = new RegexParser();
$this->assertShortcodes($parser->parse('[a k=a/b]'), array(
new ParsedShortcode(new Shortcode('a', array('k' => 'a/b'), null, null), '[a k=a/b]', 0),
));
$this->assertShortcodes($parser->parse('[a k="x\"y"] [b k="z"]'), array(
new ParsedShortcode(new Shortcode('a', array('k' => 'x\"y'), null, null), '[a k="x\"y"]', 0),
new ParsedShortcode(new Shortcode('b', array('k' => 'z'), null, null), '[b k="z"]', 13),
));
}
}