Skip to content
Permalink
Browse files

Merge branch '0.18-dev'

  • Loading branch information...
colinodell committed Mar 28, 2019
2 parents 4475f0f + c349848 commit d6425fab0a7b3c2848b7526be76cbb5534537fdc
@@ -52,6 +52,12 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
- `RegexHelper::getThematicBreakRegex()`
- Removed the second `$preserveEntities` parameters from `Xml:escape()`

## [0.18.5] - 2019-03-28

### Fixed

- Fixed the adjoining `Text` collapser not handling the full tree (thephpleague/commonmark-ext-autolink#10)

## [0.18.4] - 2019-03-23

### Changed
@@ -750,7 +756,8 @@ An unused constant and static method were deprecated and will be removed in a fu
### Added
- Initial commit (compatible with jgm/stmd:spec.txt @ 0275f34)

[unreleased]: https://github.com/thephpleague/commonmark/compare/0.18.4...HEAD
[unreleased]: https://github.com/thephpleague/commonmark/compare/0.18.5...HEAD
[0.18.5]: https://github.com/thephpleague/commonmark/compare/0.18.4...0.18.5
[0.18.4]: https://github.com/thephpleague/commonmark/compare/0.18.3...0.18.4
[0.18.3]: https://github.com/thephpleague/commonmark/compare/0.18.2...0.18.3
[0.18.2]: https://github.com/thephpleague/commonmark/compare/0.18.1...0.18.2
@@ -18,7 +18,6 @@
use League\CommonMark\Block\Element\Document;
use League\CommonMark\Block\Element\InlineContainerInterface;
use League\CommonMark\Block\Element\Paragraph;
use League\CommonMark\Node\NodeWalker;
class DocParser
{
@@ -95,7 +94,7 @@ public function parse(string $input): Document
$tip->finalize($context, $lineCount);
}
$this->processInlines($context, $context->getDocument()->walker());
$this->processInlines($context);
$this->processDocument($context);
@@ -148,8 +147,10 @@ private function processDocument(ContextInterface $context)
}
}
private function processInlines(ContextInterface $context, NodeWalker $walker)
private function processInlines(ContextInterface $context)
{
$walker = $context->getDocument()->walker();
while ($event = $walker->next()) {
if (!$event->isEntering()) {
continue;
@@ -0,0 +1,43 @@
<?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();
$next = $node->next();
if ($node instanceof Text && $next instanceof Text) {
$node->append($next->getContent());
$next->detach();
// Re-start the next `while` iteration at the same spot as before
$walker->resumeAt($node, true);
}
}
}
}
}
@@ -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;
@@ -42,7 +43,7 @@ public function parse(Node $container, ReferenceMap $referenceMap)
$this->processInlines($inlineParserContext);
$this->collapseAdjoiningTextElements($inlineParserContext);
AdjoiningTextCollapser::collapseTextNodes($container);
}
/**
@@ -100,21 +101,4 @@ private function addPlainText(string $character, Node $container, InlineParserCo
$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,0 +1,50 @@
<?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\Tests\Unit\Inline;
use League\CommonMark\Block\Element\Paragraph;
use League\CommonMark\Inline\AdjoiningTextCollapser;
use League\CommonMark\Inline\Element\Newline;
use League\CommonMark\Inline\Element\Text;
use PHPUnit\Framework\TestCase;
class AdjoiningTextCollapserTest extends TestCase
{
public function testCollapseTextNodes()
{
$paragraph = new Paragraph();
$paragraph->appendChild(new Text('https://eventum.example.net/history.php?iss'));
$paragraph->appendChild(new Text('_'));
$paragraph->appendChild(new Text('id=107092'));
$paragraph->appendChild(new Newline(Newline::SOFTBREAK));
$paragraph->appendChild(new Text('https://gitlab.example.net/group/project/merge'));
$paragraph->appendChild(new Text('_'));
$paragraph->appendChild(new Text('requests/39#note'));
$paragraph->appendChild(new Text('_'));
$paragraph->appendChild(new Text('150630'));
AdjoiningTextCollapser::collapseTextNodes($paragraph);
$children = $paragraph->children();
$this->assertCount(3, $children);
$this->assertTrue($children[0] instanceof Text);
$this->assertEquals('https://eventum.example.net/history.php?iss_id=107092', $children[0]->getContent());
$this->assertTrue($children[1] instanceof Newline);
$this->assertTrue($children[2] instanceof Text);
$this->assertEquals('https://gitlab.example.net/group/project/merge_requests/39#note_150630', $children[2]->getContent());
}
}

0 comments on commit d6425fa

Please sign in to comment.
You can’t perform that action at this time.