File: TaskListExtensionTest.php

package info (click to toggle)
php-league-commonmark 2.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 8,264 kB
  • sloc: php: 20,396; xml: 1,988; ruby: 45; makefile: 21; javascript: 15
file content (122 lines) | stat: -rw-r--r-- 2,917 bytes parent folder | download | duplicates (2)
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
<?php

declare(strict_types=1);

/*
 * This file is part of the league/commonmark package.
 *
 * (c) Colin O'Dell <colinodell@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace League\CommonMark\Tests\Unit\Extension\TaskList;

use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\TaskList\TaskListExtension;
use League\CommonMark\MarkdownConverter;
use PHPUnit\Framework\TestCase;

final class TaskListExtensionTest extends TestCase
{
    public function testTaskLists(): void
    {
        $input = <<<'EOT'
- [x] foo
  - [ ] bar
  - [X] baz
- [ ] bim

* [x] foo
* [X] bar
* [ ] baz

This works for ordered lists too:

1. [x] foo
2. [X] bar
3. [ ] baz

Some examples which should not match:

 - Checkbox [x] in the middle
 - Checkbox at the end [ ]
 - [  ] too many spaces
 - **[x] Checkbox inside of emphasis**
 - No text, as shown in these examples:
   - [x]
   - [ ]
   -    [x]
   -           [x]

Here's a test using `<del>`:

 - [x] <del>Checkbox inside of strikeout</del>

And another which does not render the checkbox:

 - <del>[x] Checkbox inside of strikeout</del>

EOT;

        $expected = <<<'EOT'
<ul>
<li><input checked="" disabled="" type="checkbox"> foo
<ul>
<li><input disabled="" type="checkbox"> bar</li>
<li><input checked="" disabled="" type="checkbox"> baz</li>
</ul>
</li>
<li><input disabled="" type="checkbox"> bim</li>
</ul>
<ul>
<li><input checked="" disabled="" type="checkbox"> foo</li>
<li><input checked="" disabled="" type="checkbox"> bar</li>
<li><input disabled="" type="checkbox"> baz</li>
</ul>
<p>This works for ordered lists too:</p>
<ol>
<li><input checked="" disabled="" type="checkbox"> foo</li>
<li><input checked="" disabled="" type="checkbox"> bar</li>
<li><input disabled="" type="checkbox"> baz</li>
</ol>
<p>Some examples which should not match:</p>
<ul>
<li>Checkbox [x] in the middle</li>
<li>Checkbox at the end [ ]</li>
<li>[  ] too many spaces</li>
<li><strong>[x] Checkbox inside of emphasis</strong></li>
<li>No text, as shown in these examples:
<ul>
<li>[x]</li>
<li>[ ]</li>
<li>[x]</li>
<li>
<pre><code>      [x]
</code></pre>
</li>
</ul>
</li>
</ul>
<p>Here's a test using <code>&lt;del&gt;</code>:</p>
<ul>
<li><input checked="" disabled="" type="checkbox"> <del>Checkbox inside of strikeout</del></li>
</ul>
<p>And another which does not render the checkbox:</p>
<ul>
<li><del>[x] Checkbox inside of strikeout</del></li>
</ul>

EOT;

        $environment = new Environment();
        $environment->addExtension(new CommonMarkCoreExtension());
        $environment->addExtension(new TaskListExtension());

        $converter = new MarkdownConverter($environment);

        $this->assertEquals($expected, $converter->convert($input)->getContent());
    }
}