5 use BookStack\Entities\Tools\PageIncludeParser;
6 use BookStack\Util\HtmlDocument;
9 class PageIncludeParserTest extends TestCase
11 public function test_simple_inline_text()
14 '<p>{{@45#content}}</p>',
15 ['45' => '<p id="content">Testing</p>'],
20 public function test_simple_inline_text_with_existing_siblings()
23 '<p>{{@45#content}} <strong>Hi</strong>there!</p>',
24 ['45' => '<p id="content">Testing</p>'],
25 '<p>Testing <strong>Hi</strong>there!</p>',
29 public function test_simple_inline_text_within_other_text()
32 '<p>Hello {{@45#content}}there!</p>',
33 ['45' => '<p id="content">Testing</p>'],
34 '<p>Hello Testingthere!</p>',
38 public function test_complex_inline_text_within_other_text()
41 '<p>Hello {{@45#content}}there!</p>',
42 ['45' => '<p id="content"><strong>Testing</strong> with<em>some</em><i>extra</i>tags</p>'],
43 '<p>Hello <strong>Testing</strong> with<em>some</em><i>extra</i>tagsthere!</p>',
47 public function test_block_content_types()
50 '<table id="content"><td>Text</td></table>',
51 '<ul id="content"><li>Item A</li></ul>',
52 '<ol id="content"><li>Item A</li></ol>',
53 '<pre id="content">Code</pre>',
56 foreach ($inputs as $input) {
58 '<p>A{{@45#content}}B</p>',
60 '<p>A</p>' . $input . '<p>B</p>',
65 public function test_block_content_nested_origin_gets_placed_before()
68 '<p><strong>A {{@45#content}} there!</strong></p>',
69 ['45' => '<pre id="content">Testing</pre>'],
70 '<pre id="content">Testing</pre><p><strong>A there!</strong></p>',
74 public function test_block_content_nested_origin_gets_placed_after()
77 '<p><strong>Some really good {{@45#content}} there!</strong></p>',
78 ['45' => '<pre id="content">Testing</pre>'],
79 '<p><strong>Some really good there!</strong></p><pre id="content">Testing</pre>',
83 public function test_block_content_in_shallow_origin_gets_split()
86 '<p>Some really good {{@45#content}} there!</p>',
87 ['45' => '<pre id="content">doggos</pre>'],
88 '<p>Some really good </p><pre id="content">doggos</pre><p> there!</p>',
92 public function test_block_content_in_shallow_origin_split_does_not_duplicate_id()
95 '<p id="test" title="Hi">Some really good {{@45#content}} there!</p>',
96 ['45' => '<pre id="content">doggos</pre>'],
97 '<p title="Hi">Some really good </p><pre id="content">doggos</pre><p id="test" title="Hi"> there!</p>',
101 public function test_block_content_in_shallow_origin_does_not_leave_empty_nodes()
103 $this->runParserTest(
104 '<p>{{@45#content}}</p>',
105 ['45' => '<pre id="content">doggos</pre>'],
106 '<pre id="content">doggos</pre>',
110 public function test_block_content_in_allowable_parent_element()
112 $this->runParserTest(
113 '<div>{{@45#content}}</div>',
114 ['45' => '<pre id="content">doggos</pre>'],
115 '<div><pre id="content">doggos</pre></div>',
119 public function test_block_content_in_paragraph_origin_with_allowable_grandparent()
121 $this->runParserTest(
122 '<div><p>{{@45#content}}</p></div>',
123 ['45' => '<pre id="content">doggos</pre>'],
124 '<div><pre id="content">doggos</pre></div>',
128 public function test_block_content_in_paragraph_origin_with_allowable_grandparent_with_adjacent_content()
130 $this->runParserTest(
131 '<div><p>Cute {{@45#content}} over there!</p></div>',
132 ['45' => '<pre id="content">doggos</pre>'],
133 '<div><p>Cute </p><pre id="content">doggos</pre><p> over there!</p></div>',
137 public function test_block_content_in_child_within_paragraph_origin_with_allowable_grandparent_with_adjacent_content()
139 $this->runParserTest(
140 '<div><p><strong>Cute {{@45#content}} over there!</strong></p></div>',
141 ['45' => '<pre id="content">doggos</pre>'],
142 '<div><pre id="content">doggos</pre><p><strong>Cute over there!</strong></p></div>',
146 public function test_block_content_in_paragraph_origin_within_details()
148 $this->runParserTest(
149 '<details><p>{{@45#content}}</p></details>',
150 ['45' => '<pre id="content">doggos</pre>'],
151 '<details><pre id="content">doggos</pre></details>',
155 public function test_simple_whole_document()
157 $this->runParserTest(
159 ['45' => '<p id="content">Testing</p>'],
160 '<p id="content">Testing</p>',
164 public function test_multi_source_elem_whole_document()
166 $this->runParserTest(
168 ['45' => '<p>Testing</p><blockquote>This</blockquote>'],
169 '<p>Testing</p><blockquote>This</blockquote>',
173 public function test_multi_source_elem_whole_document_with_shared_content_origin()
175 $this->runParserTest(
176 '<p>This is {{@45}} some text</p>',
177 ['45' => '<p>Testing</p><blockquote>This</blockquote>'],
178 '<p>This is </p><p>Testing</p><blockquote>This</blockquote><p> some text</p>',
182 public function test_multiple_tags_in_same_origin_with_inline_content()
184 $this->runParserTest(
185 '<p>This {{@45#content}}{{@45#content}} content is {{@45#content}}</p>',
186 ['45' => '<p id="content">inline</p>'],
187 '<p>This inlineinline content is inline</p>',
191 public function test_multiple_tags_in_same_origin_with_block_content()
193 $this->runParserTest(
194 '<p>This {{@45#content}}{{@45#content}} content is {{@45#content}}</p>',
195 ['45' => '<pre id="content">block</pre>'],
196 '<p>This </p><pre id="content">block</pre><pre id="content">block</pre><p> content is </p><pre id="content">block</pre>',
200 public function test_multiple_tags_in_differing_origin_levels_with_block_content()
202 $this->runParserTest(
203 '<div><p>This <strong>{{@45#content}}</strong> content is {{@45#content}}</p>{{@45#content}}</div>',
204 ['45' => '<pre id="content">block</pre>'],
205 '<div><pre id="content">block</pre><p>This <strong></strong> content is </p><pre id="content">block</pre><pre id="content">block</pre></div>',
209 public function test_multiple_tags_in_shallow_origin_with_multi_block_content()
211 $this->runParserTest(
212 '<p>{{@45}}C{{@45}}</p><div>{{@45}}{{@45}}</div>',
213 ['45' => '<p>A</p><p>B</p>'],
214 '<p>A</p><p>B</p><p>C</p><p>A</p><p>B</p><div><p>A</p><p>B</p><p>A</p><p>B</p></div>',
218 protected function runParserTest(string $html, array $contentById, string $expected): void
220 $doc = new HtmlDocument($html);
221 $parser = new PageIncludeParser($doc, function (int $id) use ($contentById) {
222 return $contentById[strval($id)] ?? '';
226 $this->assertEquals($expected, $doc->getBodyInnerHtml());