1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
<?php
declare(strict_types=1);
/*
* 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\Parser;
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
use League\CommonMark\Extension\CommonMark\Parser\Inline\CloseBracketParser;
use League\CommonMark\Extension\CommonMark\Parser\Inline\OpenBracketParser;
use League\CommonMark\Node\Block\Paragraph;
use League\CommonMark\Node\Inline\Text;
use League\CommonMark\Parser\Inline\InlineParserMatch;
use League\CommonMark\Parser\InlineParserEngine;
use League\CommonMark\Reference\ReferenceMap;
use League\CommonMark\Tests\Unit\Parser\Inline\FakeInlineParser;
use PHPUnit\Framework\TestCase;
final class InlineParserEngineTest extends TestCase
{
public function testParseWithDefaultPriorityOrder(): void
{
$colorParser = new FakeInlineParser(InlineParserMatch::string('brown'));
$adjectiveParser = new FakeInlineParser(InlineParserMatch::oneOf('quick', 'brown', 'lazy'));
$fiveLetterParser = new FakeInlineParser(InlineParserMatch::regex('\b\w{5}\b'));
$environment = new Environment();
$environment->addInlineParser($colorParser);
$environment->addInlineParser($adjectiveParser);
$environment->addInlineParser($fiveLetterParser);
$engine = new InlineParserEngine($environment, new ReferenceMap());
$paragraph = new Paragraph();
$engine->parse('The quick brown fox jumps over the lazy dog', $paragraph);
$this->assertSame(['brown'], $colorParser->getMatches());
$this->assertSame(['quick', 'lazy'], $adjectiveParser->getMatches());
$this->assertSame(['jumps'], $fiveLetterParser->getMatches());
}
public function testParseWithDifferentPriorityOrder(): void
{
$colorParser = new FakeInlineParser(InlineParserMatch::string('brown'));
$adjectiveParser = new FakeInlineParser(InlineParserMatch::oneOf('quick', 'brown', 'lazy'));
$fiveLetterParser = new FakeInlineParser(InlineParserMatch::regex('\b\w{5}\b'));
$environment = new Environment();
$environment->addInlineParser($colorParser, 100);
$environment->addInlineParser($adjectiveParser, -100);
$environment->addInlineParser($fiveLetterParser);
$engine = new InlineParserEngine($environment, new ReferenceMap());
$paragraph = new Paragraph();
$engine->parse('The quick brown fox jumps over the lazy dog', $paragraph);
$this->assertSame(['brown'], $colorParser->getMatches());
$this->assertSame(['lazy'], $adjectiveParser->getMatches());
$this->assertSame(['quick', 'jumps'], $fiveLetterParser->getMatches());
}
public function testParseWithNoInlineParsers(): void
{
$environment = new Environment();
$engine = new InlineParserEngine($environment, new ReferenceMap());
$paragraph = new Paragraph();
$engine->parse('The quick brown fox jumps over the lazy dog', $paragraph);
$this->assertCount(1, $paragraph->children());
$child = $paragraph->firstChild();
$this->assertTrue($child instanceof Text);
$this->assertSame('The quick brown fox jumps over the lazy dog', $child->getLiteral());
}
/**
* @see https://github.com/thephpleague/commonmark/issues/951
*
* @runInSeparateProcess to avoid polluting the global environment
*/
public function testMultibyteDetectionRegressionFromIssue951(): void
{
\mb_internal_encoding('iso-8859-1'); // @phpstan-ignore-line
$environment = new Environment();
$environment->addInlineParser(new CloseBracketParser(), 30);
$environment->addInlineParser(new OpenBracketParser(), 20);
$engine = new InlineParserEngine($environment, new ReferenceMap());
$paragraph = new Paragraph();
$engine->parse('AAA ÀÀ [label](https://url)', $paragraph);
$this->assertCount(2, $paragraph->children());
$text = $paragraph->firstChild();
$this->assertTrue($text instanceof Text);
$this->assertSame('AAA ÀÀ ', $text->getLiteral());
$link = $paragraph->lastChild();
$this->assertTrue($link instanceof Link);
$this->assertSame('label', $link->firstChild()->getLiteral());
$this->assertSame('https://url', $link->getUrl());
}
}
|