diff --git a/CHANGELOG.md b/CHANGELOG.md index b3013e09a2..d96c83323c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi ## [Unreleased][unreleased] +### Changed +- Promoted the `delim` status on `Text` nodes to a first-class property (`$isDelimiter`) instead of storing it in the `$data` array (#963). + - This improves performance in the inline parser engine. + - **Breaking Change:** The `$isDelimiter` property on `Text` nodes is now public. The `isDelimiter()` and `setDelimiter()` methods have been removed. Accessing this status via `$text->data['delim']` is no longer supported. You must now access the public `$text->isDelimiter` property directly. + ## [2.8.0] - 2025-11-26 ### Added diff --git a/src/Delimiter/DelimiterParser.php b/src/Delimiter/DelimiterParser.php index fdfe093c90..edffb2f239 100644 --- a/src/Delimiter/DelimiterParser.php +++ b/src/Delimiter/DelimiterParser.php @@ -68,9 +68,8 @@ public function parse(InlineParserContext $inlineContext): bool return true; } - $node = new Text(\str_repeat($character, $numDelims), [ - 'delim' => true, - ]); + $node = new Text(\str_repeat($character, $numDelims)); + $node->isDelimiter = true; $inlineContext->getContainer()->appendChild($node); // Add entry to stack to this opener diff --git a/src/Extension/CommonMark/Parser/Inline/BangParser.php b/src/Extension/CommonMark/Parser/Inline/BangParser.php index cbf6ca3828..f7b14f99eb 100644 --- a/src/Extension/CommonMark/Parser/Inline/BangParser.php +++ b/src/Extension/CommonMark/Parser/Inline/BangParser.php @@ -33,7 +33,8 @@ public function parse(InlineParserContext $inlineContext): bool $cursor = $inlineContext->getCursor(); $cursor->advanceBy(2); - $node = new Text('![', ['delim' => true]); + $node = new Text('!['); + $node->isDelimiter = true; $inlineContext->getContainer()->appendChild($node); // Add entry to stack for this opener diff --git a/src/Extension/CommonMark/Parser/Inline/OpenBracketParser.php b/src/Extension/CommonMark/Parser/Inline/OpenBracketParser.php index 1ba8c133ab..e207ee9141 100644 --- a/src/Extension/CommonMark/Parser/Inline/OpenBracketParser.php +++ b/src/Extension/CommonMark/Parser/Inline/OpenBracketParser.php @@ -31,7 +31,8 @@ public function getMatchDefinition(): InlineParserMatch public function parse(InlineParserContext $inlineContext): bool { $inlineContext->getCursor()->advanceBy(1); - $node = new Text('[', ['delim' => true]); + $node = new Text('['); + $node->isDelimiter = true; $inlineContext->getContainer()->appendChild($node); // Add entry to stack for this opener diff --git a/src/Extension/SmartPunct/QuoteParser.php b/src/Extension/SmartPunct/QuoteParser.php index 31ba8c7738..c5f5235728 100644 --- a/src/Extension/SmartPunct/QuoteParser.php +++ b/src/Extension/SmartPunct/QuoteParser.php @@ -64,7 +64,7 @@ public function parse(InlineParserContext $inlineContext): bool $canOpen = $leftFlanking && ! $rightFlanking; $canClose = $rightFlanking; - $node = new Quote($char, ['delim' => true]); + $node = new Quote($char); $inlineContext->getContainer()->appendChild($node); // Add entry to stack to this opener diff --git a/src/Node/Inline/Text.php b/src/Node/Inline/Text.php index 31387f918d..68e712978c 100644 --- a/src/Node/Inline/Text.php +++ b/src/Node/Inline/Text.php @@ -18,6 +18,17 @@ final class Text extends AbstractStringContainer { + public bool $isDelimiter = false; + + /** + * @param string $contents The text contents + * @param array $data Arbitrary data + */ + public function __construct(string $contents = '', array $data = []) + { + parent::__construct($contents, $data); + } + public function append(string $literal): void { $this->literal .= $literal; diff --git a/src/Parser/InlineParserEngine.php b/src/Parser/InlineParserEngine.php index 6a26979329..004c7ce598 100644 --- a/src/Parser/InlineParserEngine.php +++ b/src/Parser/InlineParserEngine.php @@ -112,7 +112,7 @@ public function parse(string $contents, AbstractBlock $block): void private function addPlainText(string $text, AbstractBlock $container): void { $lastInline = $container->lastChild(); - if ($lastInline instanceof Text && ! $lastInline->data->has('delim')) { + if ($lastInline instanceof Text && ! $lastInline->isDelimiter) { $lastInline->append($text); } else { $container->appendChild(new Text($text));