Skip to content

Commit e439381

Browse files
committed
Use NodeIterator to optimize additional listeners
1 parent 68e9742 commit e439381

File tree

4 files changed

+30
-31
lines changed

4 files changed

+30
-31
lines changed

src/Extension/DescriptionList/Event/ConsecutiveDescriptionListMerger.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,27 @@
1515

1616
use League\CommonMark\Event\DocumentParsedEvent;
1717
use League\CommonMark\Extension\DescriptionList\Node\DescriptionList;
18+
use League\CommonMark\Node\NodeIterator;
1819

1920
final class ConsecutiveDescriptionListMerger
2021
{
2122
public function __invoke(DocumentParsedEvent $event): void
2223
{
23-
$walker = $event->getDocument()->walker();
24-
while ($e = $walker->next()) {
25-
// Wait until we're exiting a description list node
26-
if ($e->isEntering()) {
27-
continue;
28-
}
29-
30-
$node = $e->getNode();
24+
foreach ($event->getDocument()->iterator(NodeIterator::FLAG_BLOCKS_ONLY) as $node) {
3125
if (! $node instanceof DescriptionList) {
3226
continue;
3327
}
3428

35-
if (! ($next = $node->next()) instanceof DescriptionList) {
29+
if (! ($prev = $node->previous()) instanceof DescriptionList) {
3630
continue;
3731
}
3832

39-
// There's another description list next; merge it into the current one
40-
foreach ($next->children() as $child) {
41-
$node->appendChild($child);
33+
// There's another description list behind this one; merge the current one into that
34+
foreach ($node->children() as $child) {
35+
$prev->appendChild($child);
4236
}
4337

44-
$next->detach();
45-
46-
$walker->resumeAt($node, false);
38+
$node->detach();
4739
}
4840
}
4941
}

src/Extension/DescriptionList/Event/LooseDescriptionHandler.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,13 @@
1919
use League\CommonMark\Extension\DescriptionList\Node\DescriptionTerm;
2020
use League\CommonMark\Node\Block\Paragraph;
2121
use League\CommonMark\Node\Inline\Newline;
22+
use League\CommonMark\Node\NodeIterator;
2223

2324
final class LooseDescriptionHandler
2425
{
2526
public function __invoke(DocumentParsedEvent $event): void
2627
{
27-
$walker = $event->getDocument()->walker();
28-
while ($e = $walker->next()) {
29-
// Wait until we're exiting a node
30-
if ($e->isEntering()) {
31-
continue;
32-
}
33-
34-
$description = $e->getNode();
28+
foreach ($event->getDocument()->iterator(NodeIterator::FLAG_BLOCKS_ONLY) as $description) {
3529
if (! $description instanceof Description) {
3630
continue;
3731
}

src/Extension/TableOfContents/TableOfContentsBuilder.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,25 @@ public function onDocumentParsed(DocumentParsedEvent $event): void
7171

7272
private function insertBeforeFirstLinkedHeading(Document $document, TableOfContents $toc): void
7373
{
74-
foreach ($document->iterator() as $node) {
75-
if ($node instanceof HeadingPermalink && ($parent = $node->parent()) instanceof Heading) {
76-
$parent->insertBefore($toc);
74+
foreach ($document->iterator(NodeIterator::FLAG_BLOCKS_ONLY) as $node) {
75+
if (! $node instanceof Heading) {
76+
continue;
77+
}
78+
79+
foreach ($node->children() as $child) {
80+
if ($child instanceof HeadingPermalink) {
81+
$node->insertBefore($toc);
7782

78-
return;
83+
return;
84+
}
7985
}
8086
}
8187
}
8288

8389
private function replacePlaceholders(Document $document, TableOfContents $toc): void
8490
{
8591
foreach ($document->iterator(NodeIterator::FLAG_BLOCKS_ONLY) as $node) {
86-
// Add the block once we find a placeholder (and we're about to leave it)
92+
// Add the block once we find a placeholder
8793
if (! $node instanceof TableOfContentsPlaceholder) {
8894
continue;
8995
}

src/Extension/TableOfContents/TableOfContentsGenerator.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use League\CommonMark\Extension\TableOfContents\Normalizer\NormalizerStrategyInterface;
2626
use League\CommonMark\Extension\TableOfContents\Normalizer\RelativeNormalizerStrategy;
2727
use League\CommonMark\Node\Block\Document;
28+
use League\CommonMark\Node\NodeIterator;
2829
use League\CommonMark\Node\RawMarkupContainerInterface;
2930
use League\CommonMark\Node\StringContainerHelper;
3031
use League\Config\Exception\InvalidConfigurationException;
@@ -130,9 +131,15 @@ private function createToc(Document $document): TableOfContents
130131
*/
131132
private function getHeadingLinks(Document $document): iterable
132133
{
133-
foreach ($document->iterator() as $node) {
134-
if ($node instanceof HeadingPermalink) {
135-
yield $node;
134+
foreach ($document->iterator(NodeIterator::FLAG_BLOCKS_ONLY) as $node) {
135+
if (! $node instanceof Heading) {
136+
continue;
137+
}
138+
139+
foreach ($node->children() as $child) {
140+
if ($child instanceof HeadingPermalink) {
141+
yield $child;
142+
}
136143
}
137144
}
138145
}

0 commit comments

Comments
 (0)