From 77f125208ed281912bb6ae9d57abe7b4b26df4c6 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sat, 9 Mar 2024 15:52:09 +0000 Subject: [PATCH] Page nav: Fixed nbsp being represented as nothing 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 | 2 +- tests/Entity/PageContentTest.php | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/Entities/Tools/PageContent.php b/app/Entities/Tools/PageContent.php index 4f68b828f..d2f5de65c 100644 --- a/app/Entities/Tools/PageContent.php +++ b/app/Entities/Tools/PageContent.php @@ -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 [ diff --git a/tests/Entity/PageContentTest.php b/tests/Entity/PageContentTest.php index 28897c14d..23a38b573 100644 --- a/tests/Entity/PageContentTest.php +++ b/tests/Entity/PageContentTest.php @@ -469,6 +469,20 @@ class PageContentTest extends TestCase ], $navMap[2]); } + public function test_get_page_nav_respects_non_breaking_spaces() + { + $content = '

Hello There

'; + $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();