Skip to content

Commit c88eb72

Browse files
committed
Includes: Added block-level handling to new include system
Implements block promoting to body (including position choosing based upon likely tag position within parent) and block splitting where we're only a single depth down from the body child.
1 parent 7593645 commit c88eb72

3 files changed

Lines changed: 172 additions & 50 deletions

File tree

app/Entities/Tools/PageIncludeParser.php

Lines changed: 79 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use BookStack\Util\HtmlDocument;
66
use Closure;
7+
use DOMDocument;
8+
use DOMElement;
79
use DOMNode;
810
use DOMText;
911

@@ -22,48 +24,26 @@ public function parse(): string
2224
$doc = new HtmlDocument($this->pageHtml);
2325

2426
$tags = $this->locateAndIsolateIncludeTags($doc);
27+
$topLevel = [...$doc->getBodyChildren()];
2528

2629
foreach ($tags as $tag) {
2730
$htmlContent = $this->pageContentForId->call($this, $tag->getPageId());
2831
$content = new PageIncludeContent($htmlContent, $tag);
2932

30-
if ($content->isInline()) {
31-
$adopted = $doc->adoptNodes($content->toDomNodes());
32-
foreach ($adopted as $adoptedContentNode) {
33-
$tag->domNode->parentNode->insertBefore($adoptedContentNode, $tag->domNode);
33+
if (!$content->isInline()) {
34+
$isParentTopLevel = in_array($tag->domNode->parentNode, $topLevel, true);
35+
if ($isParentTopLevel) {
36+
$this->splitNodeAtChildNode($tag->domNode->parentNode, $tag->domNode);
37+
} else {
38+
$this->promoteTagNodeToBody($tag, $doc->getBody());
3439
}
35-
$tag->domNode->parentNode->removeChild($tag->domNode);
36-
continue;
3740
}
3841

39-
// TODO - Non-inline
42+
$this->replaceNodeWithNodes($tag->domNode, $content->toDomNodes());
4043
}
4144

42-
// TODO:
43-
// Hunt down the specific text nodes with matches
44-
// Split out tag text node from rest of content
45-
// Fetch tag content->
46-
// If range or top-block: delete tag text node, [Promote to top-block], delete old top-block if empty
47-
// If inline: Replace current text node with new text or elem
48-
// !! "Range" or "inline" status should come from tag parser and content fetcher, not guessed direct from content
49-
// since we could have a range of inline elements
50-
51-
// [Promote to top-block]
52-
// Tricky operation.
53-
// Can throw in before or after current top-block depending on relative position
54-
// Could [Split] top-block but complex past a single level depth.
55-
// Maybe [Split] if one level depth, otherwise default to before/after block
56-
// Should work for the vast majority of cases, and not for those which would
57-
// technically be invalid in-editor anyway.
58-
59-
// [Split]
60-
// Copy original top-block node type and attrs (apart from ID)
61-
// Move nodes after promoted tag-node into copy
62-
// Insert copy after original (after promoted top-block eventually)
63-
64-
// Notes: May want to eventually parse through backwards, which should avoid issues
65-
// in changes affecting the next tag, where tags may be in the same/adjacent nodes.
66-
45+
// TODO Notes: May want to eventually parse through backwards, which should avoid issues
46+
// in changes affecting the next tag, where tags may be in the same/adjacent nodes.
6747

6848
return $doc->getBodyInnerHtml();
6949
}
@@ -125,4 +105,71 @@ protected function splitTextNodesAtTags(DOMNode $textNode): array
125105

126106
return $includeTags;
127107
}
108+
109+
/**
110+
* @param DOMNode[] $replacements
111+
*/
112+
protected function replaceNodeWithNodes(DOMNode $toReplace, array $replacements): void
113+
{
114+
/** @var DOMDocument $targetDoc */
115+
$targetDoc = $toReplace->ownerDocument;
116+
117+
foreach ($replacements as $replacement) {
118+
if ($replacement->ownerDocument !== $targetDoc) {
119+
$replacement = $targetDoc->adoptNode($replacement);
120+
}
121+
122+
$toReplace->parentNode->insertBefore($replacement, $toReplace);
123+
}
124+
125+
$toReplace->parentNode->removeChild($toReplace);
126+
}
127+
128+
protected function promoteTagNodeToBody(PageIncludeTag $tag, DOMNode $body): void
129+
{
130+
/** @var DOMNode $topParent */
131+
$topParent = $tag->domNode->parentNode;
132+
while ($topParent->parentNode !== $body) {
133+
$topParent = $topParent->parentNode;
134+
}
135+
136+
$parentText = $topParent->textContent;
137+
$tagPos = strpos($parentText, $tag->tagContent);
138+
$before = $tagPos < (strlen($parentText) / 2);
139+
140+
if ($before) {
141+
$body->insertBefore($tag->domNode, $topParent);
142+
} else {
143+
$body->insertBefore($tag->domNode, $topParent->nextSibling);
144+
}
145+
}
146+
147+
protected function splitNodeAtChildNode(DOMElement $parentNode, DOMNode $domNode): void
148+
{
149+
$children = [...$parentNode->childNodes];
150+
$splitPos = array_search($domNode, $children, true) ?: count($children);
151+
$parentClone = $parentNode->cloneNode();
152+
$parentClone->removeAttribute('id');
153+
154+
/** @var DOMNode $child */
155+
for ($i = 0; $i < $splitPos; $i++) {
156+
$child = $children[0];
157+
$parentClone->appendChild($child);
158+
}
159+
160+
if ($parentClone->hasChildNodes()) {
161+
$parentNode->parentNode->insertBefore($parentClone, $parentNode);
162+
}
163+
164+
$parentNode->parentNode->insertBefore($domNode, $parentNode);
165+
166+
$parentClone->normalize();
167+
$parentNode->normalize();
168+
if (!$parentNode->hasChildNodes()) {
169+
$parentNode->remove();
170+
}
171+
if (!$parentClone->hasChildNodes()) {
172+
$parentClone->remove();
173+
}
174+
}
128175
}

app/Util/HtmlDocument.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -149,19 +149,4 @@ public function getNodeOuterHtml(DOMNode $node): string
149149
{
150150
return $this->document->saveHTML($node);
151151
}
152-
153-
/**
154-
* Adopt the given nodes into this document.
155-
* @param DOMNode[] $nodes
156-
* @return DOMNode[]
157-
*/
158-
public function adoptNodes(array $nodes): array
159-
{
160-
$adopted = [];
161-
foreach ($nodes as $node) {
162-
$adopted[] = $this->document->importNode($node, true);
163-
}
164-
165-
return $adopted;
166-
}
167152
}

tests/Unit/PageIncludeParserTest.php

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class PageIncludeParserTest extends TestCase
99
{
10-
public function test_include_simple_inline_text()
10+
public function test_simple_inline_text()
1111
{
1212
$this->runParserTest(
1313
'<p>{{@45#content}}</p>',
@@ -16,7 +16,7 @@ public function test_include_simple_inline_text()
1616
);
1717
}
1818

19-
public function test_include_simple_inline_text_with_existing_siblings()
19+
public function test_simple_inline_text_with_existing_siblings()
2020
{
2121
$this->runParserTest(
2222
'<p>{{@45#content}} <strong>Hi</strong>there!</p>',
@@ -25,7 +25,7 @@ public function test_include_simple_inline_text_with_existing_siblings()
2525
);
2626
}
2727

28-
public function test_include_simple_inline_text_within_other_text()
28+
public function test_simple_inline_text_within_other_text()
2929
{
3030
$this->runParserTest(
3131
'<p>Hello {{@45#content}}there!</p>',
@@ -34,6 +34,96 @@ public function test_include_simple_inline_text_within_other_text()
3434
);
3535
}
3636

37+
public function test_block_content_types()
38+
{
39+
$inputs = [
40+
'<table id="content"><td>Text</td></table>',
41+
'<ul id="content"><li>Item A</li></ul>',
42+
'<ol id="content"><li>Item A</li></ol>',
43+
'<pre id="content">Code</pre>',
44+
];
45+
46+
foreach ($inputs as $input) {
47+
$this->runParserTest(
48+
'<p>A{{@45#content}}B</p>',
49+
['45' => $input],
50+
'<p>A</p>' . $input . '<p>B</p>',
51+
);
52+
}
53+
}
54+
55+
public function test_block_content_nested_origin_gets_placed_before()
56+
{
57+
$this->runParserTest(
58+
'<p><strong>A {{@45#content}} there!</strong></p>',
59+
['45' => '<pre id="content">Testing</pre>'],
60+
'<pre id="content">Testing</pre><p><strong>A there!</strong></p>',
61+
);
62+
}
63+
64+
public function test_block_content_nested_origin_gets_placed_after()
65+
{
66+
$this->runParserTest(
67+
'<p><strong>Some really good {{@45#content}} there!</strong></p>',
68+
['45' => '<pre id="content">Testing</pre>'],
69+
'<p><strong>Some really good there!</strong></p><pre id="content">Testing</pre>',
70+
);
71+
}
72+
73+
public function test_block_content_in_shallow_origin_gets_split()
74+
{
75+
$this->runParserTest(
76+
'<p>Some really good {{@45#content}} there!</p>',
77+
['45' => '<pre id="content">doggos</pre>'],
78+
'<p>Some really good </p><pre id="content">doggos</pre><p> there!</p>',
79+
);
80+
}
81+
82+
public function test_block_content_in_shallow_origin_split_does_not_duplicate_id()
83+
{
84+
$this->runParserTest(
85+
'<p id="test" title="Hi">Some really good {{@45#content}} there!</p>',
86+
['45' => '<pre id="content">doggos</pre>'],
87+
'<p title="Hi">Some really good </p><pre id="content">doggos</pre><p id="test" title="Hi"> there!</p>',
88+
);
89+
}
90+
91+
public function test_block_content_in_shallow_origin_does_not_leave_empty_nodes()
92+
{
93+
$this->runParserTest(
94+
'<p>{{@45#content}}</p>',
95+
['45' => '<pre id="content">doggos</pre>'],
96+
'<pre id="content">doggos</pre>',
97+
);
98+
}
99+
100+
public function test_simple_whole_document()
101+
{
102+
$this->runParserTest(
103+
'<p>{{@45}}</p>',
104+
['45' => '<p id="content">Testing</p>'],
105+
'<p id="content">Testing</p>',
106+
);
107+
}
108+
109+
public function test_multi_source_elem_whole_document()
110+
{
111+
$this->runParserTest(
112+
'<p>{{@45}}</p>',
113+
['45' => '<p>Testing</p><blockquote>This</blockquote>'],
114+
'<p>Testing</p><blockquote>This</blockquote>',
115+
);
116+
}
117+
118+
public function test_multi_source_elem_whole_document_with_shared_content_origin()
119+
{
120+
$this->runParserTest(
121+
'<p>This is {{@45}} some text</p>',
122+
['45' => '<p>Testing</p><blockquote>This</blockquote>'],
123+
'<p>This is </p><p>Testing</p><blockquote>This</blockquote><p> some text</p>',
124+
);
125+
}
126+
37127
protected function runParserTest(string $html, array $contentById, string $expected)
38128
{
39129
$parser = new PageIncludeParser($html, function (int $id) use ($contentById) {

0 commit comments

Comments
 (0)