From 04d21c8a97da9463e6bedda620ecf1282722ea3e Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Wed, 22 Nov 2023 22:14:28 +0000 Subject: [PATCH] Includes: Started foundations for new include tag parser --- app/Entities/Tools/PageIncludeParser.php | 57 ++++++++++++++++++++++++ tests/Unit/PageIncludeParserTest.php | 46 +++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 app/Entities/Tools/PageIncludeParser.php create mode 100644 tests/Unit/PageIncludeParserTest.php diff --git a/app/Entities/Tools/PageIncludeParser.php b/app/Entities/Tools/PageIncludeParser.php new file mode 100644 index 000000000..63d3ea8d6 --- /dev/null +++ b/app/Entities/Tools/PageIncludeParser.php @@ -0,0 +1,57 @@ +pageHtml); + + $includeHosts = $html->queryXPath("//body//*[contains(text(), '{{@')]"); + $node = $includeHosts->item(0); + + // One of the direct child textnodes of the "$includeHosts" should be + // the one with the include tag within. + $textNode = $node->childNodes->item(0); + + // TODO: + // Hunt down the specific text nodes with matches + // Split out tag text node from rest of content + // Fetch tag content-> + // If range or top-block: delete tag text node, [Promote to top-block], delete old top-block if empty + // If inline: Replace current text node with new text or elem + // !! "Range" or "inline" status should come from tag parser and content fetcher, not guessed direct from content + // since we could have a range of inline elements + + // [Promote to top-block] + // Tricky operation. + // Can throw in before or after current top-block depending on relative position + // Could [Split] top-block but complex past a single level depth. + // Maybe [Split] if one level depth, otherwise default to before/after block + // Should work for the vast majority of cases, and not for those which would + // technically be invalid in-editor anyway. + + // [Split] + // Copy original top-block node type and attrs (apart from ID) + // Move nodes after promoted tag-node into copy + // Insert copy after original (after promoted top-block eventually) + + // Notes: May want to eventually parse through backwards, which should avoid issues + // in changes affecting the next tag, where tags may be in the same/adjacent nodes. + + + return $html->getBodyInnerHtml(); + } +} diff --git a/tests/Unit/PageIncludeParserTest.php b/tests/Unit/PageIncludeParserTest.php new file mode 100644 index 000000000..de31504ff --- /dev/null +++ b/tests/Unit/PageIncludeParserTest.php @@ -0,0 +1,46 @@ +runParserTest( + '

{{@45#content}}

', + ['45' => '

Testing

'], + '

Testing

', + ); + } + + public function test_include_simple_inline_text_with_existing_siblings() + { + $this->runParserTest( + '

{{@45#content}} Hithere!

', + ['45' => '

Testing

'], + '

Testing Hithere!

', + ); + } + + public function test_include_simple_inline_text_within_other_text() + { + $this->runParserTest( + '

Hello {{@45#content}}there!

', + ['45' => '

Testing

'], + '

Hello Testingthere!

', + ); + } + + protected function runParserTest(string $html, array $contentById, string $expected) + { + $parser = new PageIncludeParser($html, function (int $id) use ($contentById) { + return $contentById[strval($id)] ?? null; + }); + + $result = $parser->parse(); + $this->assertEquals($expected, $result); + } +}