]> BookStack Code Mirror - bookstack/commitdiff
Page nav: Fixed nbsp being represented as nothing
authorDan Brown <redacted>
Sat, 9 Mar 2024 15:52:09 +0000 (15:52 +0000)
committerDan Brown <redacted>
Sat, 9 Mar 2024 15:52:09 +0000 (15:52 +0000)
Now represented in page nav using a normal space to avoid complete
removal of space.
Added test to cover.
For #4836

app/Entities/Tools/PageContent.php
tests/Entity/PageContentTest.php

index 4f68b828fc56e794bfd196535e338d3632df4d98..d2f5de65c3e58cd83e6fb37dd650bc780581734b 100644 (file)
@@ -379,7 +379,7 @@ class PageContent
     protected function headerNodesToLevelList(DOMNodeList $nodeList): array
     {
         $tree = collect($nodeList)->map(function (DOMElement $header) {
-            $text = trim(str_replace("\xc2\xa0", '', $header->nodeValue));
+            $text = trim(str_replace("\xc2\xa0", ' ', $header->nodeValue));
             $text = mb_substr($text, 0, 100);
 
             return [
index 28897c14d2f20fd8dd6c1d9761afe1ad093500c1..23a38b5735ba83c8c79ae985bf150e3008d4dec5 100644 (file)
@@ -469,6 +469,20 @@ class PageContentTest extends TestCase
         ], $navMap[2]);
     }
 
+    public function test_get_page_nav_respects_non_breaking_spaces()
+    {
+        $content = '<h1 id="testa">Hello&nbsp;There</h1>';
+        $pageContent = new PageContent(new Page(['html' => $content]));
+        $navMap = $pageContent->getNavigation($content);
+
+        $this->assertEquals([
+            'nodeName' => 'h1',
+            'link'     => '#testa',
+            'text'     => 'Hello There',
+            'level'    => 1,
+        ], $navMap[0]);
+    }
+
     public function test_page_text_decodes_html_entities()
     {
         $page = $this->entities->page();