InlineParserEngineTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the league/commonmark package.
  5. *
  6. * (c) Colin O'Dell <colinodell@gmail.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace League\CommonMark\Tests\Unit\Parser;
  12. use League\CommonMark\Environment\Environment;
  13. use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
  14. use League\CommonMark\Extension\CommonMark\Parser\Inline\CloseBracketParser;
  15. use League\CommonMark\Extension\CommonMark\Parser\Inline\OpenBracketParser;
  16. use League\CommonMark\Node\Block\Paragraph;
  17. use League\CommonMark\Node\Inline\Text;
  18. use League\CommonMark\Parser\Inline\InlineParserMatch;
  19. use League\CommonMark\Parser\InlineParserEngine;
  20. use League\CommonMark\Reference\ReferenceMap;
  21. use League\CommonMark\Tests\Unit\Parser\Inline\FakeInlineParser;
  22. use PHPUnit\Framework\TestCase;
  23. final class InlineParserEngineTest extends TestCase
  24. {
  25. public function testParseWithDefaultPriorityOrder(): void
  26. {
  27. $colorParser = new FakeInlineParser(InlineParserMatch::string('brown'));
  28. $adjectiveParser = new FakeInlineParser(InlineParserMatch::oneOf('quick', 'brown', 'lazy'));
  29. $fiveLetterParser = new FakeInlineParser(InlineParserMatch::regex('\b\w{5}\b'));
  30. $environment = new Environment();
  31. $environment->addInlineParser($colorParser);
  32. $environment->addInlineParser($adjectiveParser);
  33. $environment->addInlineParser($fiveLetterParser);
  34. $engine = new InlineParserEngine($environment, new ReferenceMap());
  35. $paragraph = new Paragraph();
  36. $engine->parse('The quick brown fox jumps over the lazy dog', $paragraph);
  37. $this->assertSame(['brown'], $colorParser->getMatches());
  38. $this->assertSame(['quick', 'lazy'], $adjectiveParser->getMatches());
  39. $this->assertSame(['jumps'], $fiveLetterParser->getMatches());
  40. }
  41. public function testParseWithDifferentPriorityOrder(): void
  42. {
  43. $colorParser = new FakeInlineParser(InlineParserMatch::string('brown'));
  44. $adjectiveParser = new FakeInlineParser(InlineParserMatch::oneOf('quick', 'brown', 'lazy'));
  45. $fiveLetterParser = new FakeInlineParser(InlineParserMatch::regex('\b\w{5}\b'));
  46. $environment = new Environment();
  47. $environment->addInlineParser($colorParser, 100);
  48. $environment->addInlineParser($adjectiveParser, -100);
  49. $environment->addInlineParser($fiveLetterParser);
  50. $engine = new InlineParserEngine($environment, new ReferenceMap());
  51. $paragraph = new Paragraph();
  52. $engine->parse('The quick brown fox jumps over the lazy dog', $paragraph);
  53. $this->assertSame(['brown'], $colorParser->getMatches());
  54. $this->assertSame(['lazy'], $adjectiveParser->getMatches());
  55. $this->assertSame(['quick', 'jumps'], $fiveLetterParser->getMatches());
  56. }
  57. public function testParseWithNoInlineParsers(): void
  58. {
  59. $environment = new Environment();
  60. $engine = new InlineParserEngine($environment, new ReferenceMap());
  61. $paragraph = new Paragraph();
  62. $engine->parse('The quick brown fox jumps over the lazy dog', $paragraph);
  63. $this->assertCount(1, $paragraph->children());
  64. $child = $paragraph->firstChild();
  65. $this->assertTrue($child instanceof Text);
  66. $this->assertSame('The quick brown fox jumps over the lazy dog', $child->getLiteral());
  67. }
  68. /**
  69. * @see https://github.com/thephpleague/commonmark/issues/951
  70. *
  71. * @runInSeparateProcess to avoid polluting the global environment
  72. */
  73. public function testMultibyteDetectionRegressionFromIssue951(): void
  74. {
  75. \mb_internal_encoding('iso-8859-1'); // @phpstan-ignore-line
  76. $environment = new Environment();
  77. $environment->addInlineParser(new CloseBracketParser(), 30);
  78. $environment->addInlineParser(new OpenBracketParser(), 20);
  79. $engine = new InlineParserEngine($environment, new ReferenceMap());
  80. $paragraph = new Paragraph();
  81. $engine->parse('AAA ÀÀ [label](https://url)', $paragraph);
  82. $this->assertCount(2, $paragraph->children());
  83. $text = $paragraph->firstChild();
  84. $this->assertTrue($text instanceof Text);
  85. $this->assertSame('AAA ÀÀ ', $text->getLiteral());
  86. $link = $paragraph->lastChild();
  87. $this->assertTrue($link instanceof Link);
  88. $this->assertSame('label', $link->firstChild()->getLiteral());
  89. $this->assertSame('https://url', $link->getUrl());
  90. }
  91. }