5 use BookStack\Entities\Tools\PageIncludeContent;
6 use BookStack\Entities\Tools\PageIncludeParser;
7 use BookStack\Entities\Tools\PageIncludeTag;
8 use BookStack\Util\HtmlDocument;
11 class PageIncludeParserTest extends TestCase
13 public function test_simple_inline_text()
16 '<p>{{@45#content}}</p>',
17 ['45' => '<p id="content">Testing</p>'],
22 public function test_simple_inline_text_with_existing_siblings()
25 '<p>{{@45#content}} <strong>Hi</strong>there!</p>',
26 ['45' => '<p id="content">Testing</p>'],
27 '<p>Testing <strong>Hi</strong>there!</p>',
31 public function test_simple_inline_text_within_other_text()
34 '<p>Hello {{@45#content}}there!</p>',
35 ['45' => '<p id="content">Testing</p>'],
36 '<p>Hello Testingthere!</p>',
40 public function test_complex_inline_text_within_other_text()
43 '<p>Hello {{@45#content}}there!</p>',
44 ['45' => '<p id="content"><strong>Testing</strong> with<em>some</em><i>extra</i>tags</p>'],
45 '<p>Hello <strong>Testing</strong> with<em>some</em><i>extra</i>tagsthere!</p>',
49 public function test_block_content_types()
52 '<table id="content"><td>Text</td></table>',
53 '<ul id="content"><li>Item A</li></ul>',
54 '<ol id="content"><li>Item A</li></ol>',
55 '<pre id="content">Code</pre>',
58 foreach ($inputs as $input) {
60 '<p>A{{@45#content}}B</p>',
62 '<p>A</p>' . $input . '<p>B</p>',
67 public function test_block_content_nested_origin_gets_placed_before()
70 '<p><strong>A {{@45#content}} there!</strong></p>',
71 ['45' => '<pre id="content">Testing</pre>'],
72 '<pre id="content">Testing</pre><p><strong>A there!</strong></p>',
76 public function test_block_content_nested_origin_gets_placed_after()
79 '<p><strong>Some really good {{@45#content}} there!</strong></p>',
80 ['45' => '<pre id="content">Testing</pre>'],
81 '<p><strong>Some really good there!</strong></p><pre id="content">Testing</pre>',
85 public function test_block_content_in_shallow_origin_gets_split()
88 '<p>Some really good {{@45#content}} there!</p>',
89 ['45' => '<pre id="content">doggos</pre>'],
90 '<p>Some really good </p><pre id="content">doggos</pre><p> there!</p>',
94 public function test_block_content_in_shallow_origin_split_does_not_duplicate_id()
97 '<p id="test" title="Hi">Some really good {{@45#content}} there!</p>',
98 ['45' => '<pre id="content">doggos</pre>'],
99 '<p title="Hi">Some really good </p><pre id="content">doggos</pre><p id="test" title="Hi"> there!</p>',
103 public function test_block_content_in_shallow_origin_does_not_leave_empty_nodes()
105 $this->runParserTest(
106 '<p>{{@45#content}}</p>',
107 ['45' => '<pre id="content">doggos</pre>'],
108 '<pre id="content">doggos</pre>',
112 public function test_block_content_in_allowable_parent_element()
114 $this->runParserTest(
115 '<div>{{@45#content}}</div>',
116 ['45' => '<pre id="content">doggos</pre>'],
117 '<div><pre id="content">doggos</pre></div>',
121 public function test_block_content_in_paragraph_origin_with_allowable_grandparent()
123 $this->runParserTest(
124 '<div><p>{{@45#content}}</p></div>',
125 ['45' => '<pre id="content">doggos</pre>'],
126 '<div><pre id="content">doggos</pre></div>',
130 public function test_block_content_in_paragraph_origin_with_allowable_grandparent_with_adjacent_content()
132 $this->runParserTest(
133 '<div><p>Cute {{@45#content}} over there!</p></div>',
134 ['45' => '<pre id="content">doggos</pre>'],
135 '<div><p>Cute </p><pre id="content">doggos</pre><p> over there!</p></div>',
139 public function test_block_content_in_child_within_paragraph_origin_with_allowable_grandparent_with_adjacent_content()
141 $this->runParserTest(
142 '<div><p><strong>Cute {{@45#content}} over there!</strong></p></div>',
143 ['45' => '<pre id="content">doggos</pre>'],
144 '<div><pre id="content">doggos</pre><p><strong>Cute over there!</strong></p></div>',
148 public function test_block_content_in_paragraph_origin_within_details()
150 $this->runParserTest(
151 '<details><p>{{@45#content}}</p></details>',
152 ['45' => '<pre id="content">doggos</pre>'],
153 '<details><pre id="content">doggos</pre></details>',
157 public function test_simple_whole_document()
159 $this->runParserTest(
161 ['45' => '<p id="content">Testing</p>'],
162 '<p id="content">Testing</p>',
166 public function test_multi_source_elem_whole_document()
168 $this->runParserTest(
170 ['45' => '<p>Testing</p><blockquote>This</blockquote>'],
171 '<p>Testing</p><blockquote>This</blockquote>',
175 public function test_multi_source_elem_whole_document_with_shared_content_origin()
177 $this->runParserTest(
178 '<p>This is {{@45}} some text</p>',
179 ['45' => '<p>Testing</p><blockquote>This</blockquote>'],
180 '<p>This is </p><p>Testing</p><blockquote>This</blockquote><p> some text</p>',
184 public function test_multi_source_elem_whole_document_with_nested_content_origin()
186 $this->runParserTest(
187 '<p><strong>{{@45}}</strong></p>',
188 ['45' => '<p>Testing</p><blockquote>This</blockquote>'],
189 '<p>Testing</p><blockquote>This</blockquote>',
193 public function test_multiple_tags_in_same_origin_with_inline_content()
195 $this->runParserTest(
196 '<p>This {{@45#content}}{{@45#content}} content is {{@45#content}}</p>',
197 ['45' => '<p id="content">inline</p>'],
198 '<p>This inlineinline content is inline</p>',
202 public function test_multiple_tags_in_same_origin_with_block_content()
204 $this->runParserTest(
205 '<p>This {{@45#content}}{{@45#content}} content is {{@45#content}}</p>',
206 ['45' => '<pre id="content">block</pre>'],
207 '<p>This </p><pre id="content">block</pre><pre id="content">block</pre><p> content is </p><pre id="content">block</pre>',
211 public function test_multiple_tags_in_differing_origin_levels_with_block_content()
213 $this->runParserTest(
214 '<div><p>This <strong>{{@45#content}}</strong> content is {{@45#content}}</p>{{@45#content}}</div>',
215 ['45' => '<pre id="content">block</pre>'],
216 '<div><pre id="content">block</pre><p>This content is </p><pre id="content">block</pre><pre id="content">block</pre></div>',
220 public function test_multiple_tags_in_shallow_origin_with_multi_block_content()
222 $this->runParserTest(
223 '<p>{{@45}}C{{@45}}</p><div>{{@45}}{{@45}}</div>',
224 ['45' => '<p>A</p><p>B</p>'],
225 '<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>',
229 protected function runParserTest(string $html, array $contentById, string $expected): void
231 $doc = new HtmlDocument($html);
232 $parser = new PageIncludeParser($doc, function (PageIncludeTag $tag) use ($contentById): PageIncludeContent {
233 $html = $contentById[strval($tag->getPageId())] ?? '';
234 return PageIncludeContent::fromHtmlAndTag($html, $tag);
238 $this->assertEquals($expected, $doc->getBodyInnerHtml());