AdjacentTextMergerTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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\Node\Inline;
  12. use League\CommonMark\Node\Block\Paragraph;
  13. use League\CommonMark\Node\Inline\AdjacentTextMerger;
  14. use League\CommonMark\Node\Inline\Newline;
  15. use League\CommonMark\Node\Inline\Text;
  16. use PHPUnit\Framework\TestCase;
  17. final class AdjacentTextMergerTest extends TestCase
  18. {
  19. public function testMergeTextNodesBetweenExclusive(): void
  20. {
  21. $paragraph = new Paragraph();
  22. $paragraph->appendChild($from = new Text('https://eventum.example.net/history.php?iss'));
  23. $paragraph->appendChild(new Text('_'));
  24. $paragraph->appendChild(new Text('id=107092'));
  25. $paragraph->appendChild(new Newline(Newline::SOFTBREAK));
  26. $paragraph->appendChild($to = new Text('https://gitlab.example.net/group/project/merge'));
  27. $paragraph->appendChild(new Text('_'));
  28. $paragraph->appendChild(new Text('requests/39#note'));
  29. $paragraph->appendChild(new Text('_'));
  30. $paragraph->appendChild(new Text('150630'));
  31. AdjacentTextMerger::mergeTextNodesBetweenExclusive($from, $to);
  32. $children = $paragraph->children();
  33. $this->assertCount(8, $children);
  34. $this->assertTrue($children[0] instanceof Text);
  35. $this->assertEquals('https://eventum.example.net/history.php?iss', $children[0]->getLiteral());
  36. $this->assertTrue($children[1] instanceof Text);
  37. $this->assertEquals('_id=107092', $children[1]->getLiteral());
  38. $this->assertTrue($children[2] instanceof Newline);
  39. }
  40. public function testMergeChildNodes(): void
  41. {
  42. $paragraph = new Paragraph();
  43. $paragraph->appendChild(new Text('https://eventum.example.net/history.php?iss'));
  44. $paragraph->appendChild(new Text('_'));
  45. $paragraph->appendChild(new Text('id=107092'));
  46. $paragraph->appendChild(new Newline(Newline::SOFTBREAK));
  47. $paragraph->appendChild(new Text('https://gitlab.example.net/group/project/merge'));
  48. $paragraph->appendChild(new Text('_'));
  49. $paragraph->appendChild(new Text('requests/39#note'));
  50. $paragraph->appendChild(new Text('_'));
  51. $paragraph->appendChild(new Text('150630'));
  52. AdjacentTextMerger::mergeChildNodes($paragraph);
  53. $children = $paragraph->children();
  54. $this->assertCount(3, $children);
  55. $this->assertTrue($children[0] instanceof Text);
  56. $this->assertEquals('https://eventum.example.net/history.php?iss_id=107092', $children[0]->getLiteral());
  57. $this->assertTrue($children[1] instanceof Newline);
  58. $this->assertTrue($children[2] instanceof Text);
  59. $this->assertEquals('https://gitlab.example.net/group/project/merge_requests/39#note_150630', $children[2]->getLiteral());
  60. }
  61. public function testMergeWithDirectlyAdjacentNodes(): void
  62. {
  63. $paragraph = new Paragraph();
  64. $paragraph->appendChild($text1 = new Text('commonmark.thephpleague.com'));
  65. AdjacentTextMerger::mergeWithDirectlyAdjacentNodes($text1);
  66. $this->assertCount(1, $paragraph->children());
  67. $this->assertSame($text1, $paragraph->firstChild());
  68. $this->assertSame('commonmark.thephpleague.com', $paragraph->firstChild()->getLiteral());
  69. $paragraph->appendChild(new Text('/2.0'));
  70. AdjacentTextMerger::mergeWithDirectlyAdjacentNodes($text1);
  71. $this->assertCount(1, $paragraph->children());
  72. $this->assertSame($text1, $paragraph->firstChild());
  73. $this->assertSame('commonmark.thephpleague.com/2.0', $paragraph->firstChild()->getLiteral());
  74. $paragraph->prependChild($new = new Text('://'));
  75. $this->assertSame($new, $text1->previous());
  76. $this->assertSame($text1, $new->next());
  77. $this->assertSame($paragraph, $text1->parent());
  78. $this->assertSame($paragraph, $new->parent());
  79. AdjacentTextMerger::mergeWithDirectlyAdjacentNodes($text1);
  80. $this->assertCount(1, $paragraph->children());
  81. $this->assertSame($new, $paragraph->firstChild());
  82. $this->assertSame('://commonmark.thephpleague.com/2.0', $paragraph->firstChild()->getLiteral());
  83. $this->assertNull($text1->previous());
  84. $this->assertNull($text1->next());
  85. $this->assertNull($text1->parent());
  86. $this->assertNull($new->previous());
  87. $this->assertNull($new->next());
  88. $this->assertSame($paragraph, $new->parent());
  89. $target = $paragraph->firstChild();
  90. $paragraph->prependChild($new = new Text('https'));
  91. $paragraph->appendChild(new Text('/'));
  92. AdjacentTextMerger::mergeWithDirectlyAdjacentNodes($target);
  93. $this->assertCount(1, $paragraph->children());
  94. $this->assertSame($new, $paragraph->firstChild());
  95. $this->assertSame('https://commonmark.thephpleague.com/2.0/', $paragraph->firstChild()->getLiteral());
  96. }
  97. }