Skip to content

Commit

Permalink
Fixed broken table/ol/ul page includes
Browse files Browse the repository at this point in the history
Fixes #640
  • Loading branch information
ssddanbrown committed Dec 30, 2017
1 parent 7bb336d commit 359b1b4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
10 changes: 8 additions & 2 deletions app/Repos/EntityRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,7 @@ public function renderPage(Page $page, $ignorePermissions = false)
preg_match_all("/{{@\s?([0-9].*?)}}/", $content, $matches);
if (count($matches[0]) === 0) return $content;

$topLevelTags = ['table', 'ul', 'ol'];
foreach ($matches[1] as $index => $includeId) {
$splitInclude = explode('#', $includeId, 2);
$pageId = intval($splitInclude[0]);
Expand All @@ -714,8 +715,13 @@ public function renderPage(Page $page, $ignorePermissions = false)
continue;
}
$innerContent = '';
foreach ($matchingElem->childNodes as $childNode) {
$innerContent .= $doc->saveHTML($childNode);
$isTopLevel = in_array(strtolower($matchingElem->nodeName), $topLevelTags);
if ($isTopLevel) {
$innerContent .= $doc->saveHTML($matchingElem);
} else {
foreach ($matchingElem->childNodes as $childNode) {
$innerContent .= $doc->saveHTML($childNode);
}
}
$content = str_replace($matches[0][$index], trim($innerContent), $content);
}
Expand Down
21 changes: 19 additions & 2 deletions tests/Entity/PageContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PageContentTest extends TestCase
public function test_page_includes()
{
$page = Page::first();
$secondPage = Page::all()->get(2);
$secondPage = Page::where('id', '!=', $page->id)->first();

$secondPage->html = "<p id='section1'>Hello, This is a test</p><p id='section2'>This is a second block of content</p>";
$secondPage->save();
Expand Down Expand Up @@ -38,7 +38,7 @@ public function test_page_includes()
public function test_saving_page_with_includes()
{
$page = Page::first();
$secondPage = Page::all()->get(2);
$secondPage = Page::where('id', '!=', $page->id)->first();
$this->asEditor();
$page->html = "<p>{{@$secondPage->id}}</p>";

Expand All @@ -50,6 +50,23 @@ public function test_saving_page_with_includes()
$this->assertContains("{{@$secondPage->id}}", $page->html);
}

public function test_page_includes_do_not_break_tables()
{
$page = Page::first();
$secondPage = Page::where('id', '!=', $page->id)->first();

$content = '<table id="table"><tbody><tr><td>test</td></tr></tbody></table>';
$secondPage->html = $content;
$secondPage->save();

$page->html = "{{@{$secondPage->id}#table}}";
$page->save();

$this->asEditor();
$pageResp = $this->get($page->getUrl());
$pageResp->assertSee($content);
}

public function test_page_revision_views_viewable()
{
$this->asEditor();
Expand Down

0 comments on commit 359b1b4

Please sign in to comment.