Skip to content

Commit

Permalink
Extract text node collapsing into a separate class for easier testing
Browse files Browse the repository at this point in the history
  • Loading branch information
colinodell committed Mar 28, 2019
1 parent d48ea46 commit a7149ee
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
42 changes: 42 additions & 0 deletions src/Inline/AdjoiningTextCollapser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace League\CommonMark\Inline;

use League\CommonMark\Inline\Element\Text;
use League\CommonMark\Node\Node;

/**
* @internal
*/
final class AdjoiningTextCollapser
{
/**
* @param Node $container
*
* @internal
*/
public static function collapseTextNodes(Node $container)
{
$walker = $container->walker();
while (($event = $walker->next()) !== null) {
if ($event->isEntering()) {
$node = $event->getNode();
if ($node instanceof Text) {
while (($next = $node->next()) && $next instanceof Text) {
$node->append($next->getContent());
$next->detach();
}
}
}
}
}
}
20 changes: 2 additions & 18 deletions src/InlineParserEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace League\CommonMark;

use League\CommonMark\Inline\AdjoiningTextCollapser;
use League\CommonMark\Inline\Element\Text;
use League\CommonMark\Node\Node;
use League\CommonMark\Reference\ReferenceMap;
Expand Down Expand Up @@ -42,7 +43,7 @@ public function parse(Node $container, ReferenceMap $referenceMap)

$this->processInlines($inlineParserContext);

$this->collapseAdjoiningTextElements($inlineParserContext);
AdjoiningTextCollapser::collapseTextNodes($container);
}

/**
Expand Down Expand Up @@ -100,21 +101,4 @@ private function addPlainText($character, Node $container, InlineParserContext $
$container->appendChild(new Text($text));
}
}

private function collapseAdjoiningTextElements(InlineParserContext $context)
{
$walker = $context->getContainer()->walker();

while (($event = $walker->next()) !== null) {
if ($event->isEntering()) {
$node = $event->getNode();
if ($node instanceof Text) {
while (($next = $node->next()) && $next instanceof Text) {
$node->append($next->getContent());
$next->detach();
}
}
}
}
}
}

0 comments on commit a7149ee

Please sign in to comment.