|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use ARKEcosystem\Foundation\CommonMark\Extensions\Highlighter\FencedCodeRenderer; |
| 6 | +use League\CommonMark\Environment\Environment; |
| 7 | +use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode; |
| 8 | +use League\CommonMark\Node\Block\Document; |
| 9 | +use League\CommonMark\Renderer\HtmlRenderer; |
| 10 | +use League\CommonMark\Renderer\NodeRendererInterface; |
| 11 | +use League\CommonMark\Util\HtmlElement; |
| 12 | + |
| 13 | +beforeEach(function () { |
| 14 | + $this->block = new FencedCode(3, '`', 0); |
| 15 | + $this->renderer = new FencedCodeRenderer(); |
| 16 | + |
| 17 | + $document = new Document(); |
| 18 | + |
| 19 | + $documentRenderer = $this->createMock(NodeRendererInterface::class); |
| 20 | + $documentRenderer->method('render')->willReturn('::document::'); |
| 21 | + |
| 22 | + $environment = new Environment(); |
| 23 | + $environment->addRenderer(Document::class, $documentRenderer); |
| 24 | + $this->htmlRenderer = new HtmlRenderer($environment); |
| 25 | +}); |
| 26 | + |
| 27 | +it('should render', function () { |
| 28 | + $this->block->setInfo('blade'); |
| 29 | + $this->block->setLiteral('<span>test</span>'); |
| 30 | + |
| 31 | + $result = $this->renderer->render($this->block, $this->htmlRenderer); |
| 32 | + |
| 33 | + expect($result)->toBeInstanceOf(HtmlElement::class); |
| 34 | + expect($result->getTagName())->toBe('div'); |
| 35 | + expect($result->getContents(false)->getTagName())->toBe('pre'); |
| 36 | + expect($result->getContents())->toContain('<code class="hljs-copy language-blade">'); |
| 37 | + expect($result->getContents())->toContain('<span>test</span>'); |
| 38 | + expect($result->getAllAttributes())->toBe([ |
| 39 | + 'class' => 'p-4 mb-6 rounded-xl bg-theme-secondary-800 overflow-x-auto', |
| 40 | + ]); |
| 41 | + expect($result->getContents(false)->getAllAttributes())->toBe(['class' => 'hljs']); |
| 42 | +}); |
| 43 | + |
| 44 | +it('should parse encoded html characters', function () { |
| 45 | + $this->block->setInfo('html'); |
| 46 | + $this->block->setLiteral('<span>test</span>'); |
| 47 | + |
| 48 | + $result = $this->renderer->render($this->block, $this->htmlRenderer); |
| 49 | + |
| 50 | + expect($result)->toBeInstanceOf(HtmlElement::class); |
| 51 | + expect($result->getTagName())->toBe('div'); |
| 52 | + expect($result->getContents(false)->getTagName())->toBe('pre'); |
| 53 | + expect($result->getContents())->toContain('<code class="hljs-copy language-html">'); |
| 54 | + expect($result->getContents())->toContain('<span>test</span>'); |
| 55 | + expect($result->getAllAttributes())->toBe([ |
| 56 | + 'class' => 'p-4 mb-6 rounded-xl bg-theme-secondary-800 overflow-x-auto', |
| 57 | + ]); |
| 58 | + expect($result->getContents(false)->getAllAttributes())->toBe(['class' => 'hljs']); |
| 59 | +}); |
| 60 | + |
| 61 | +it('should do nothing if no encoded html characters', function () { |
| 62 | + $this->block->setInfo('html'); |
| 63 | + $this->block->setLiteral('this is a test'); |
| 64 | + |
| 65 | + $result = $this->renderer->render($this->block, $this->htmlRenderer); |
| 66 | + |
| 67 | + expect($result)->toBeInstanceOf(HtmlElement::class); |
| 68 | + expect($result->getTagName())->toBe('div'); |
| 69 | + expect($result->getContents(false)->getTagName())->toBe('pre'); |
| 70 | + expect($result->getContents())->toContain('<code class="hljs-copy language-html">'); |
| 71 | + expect($result->getContents())->toContain('this is a test'); |
| 72 | + expect($result->getAllAttributes())->toBe([ |
| 73 | + 'class' => 'p-4 mb-6 rounded-xl bg-theme-secondary-800 overflow-x-auto', |
| 74 | + ]); |
| 75 | + expect($result->getContents(false)->getAllAttributes())->toBe(['class' => 'hljs']); |
| 76 | +}); |
| 77 | + |
| 78 | +it('should handle line numbers', function () { |
| 79 | + $this->block->setInfo('plaintext'); |
| 80 | + $this->block->setLiteral('test |
| 81 | + test |
| 82 | + test'); |
| 83 | + |
| 84 | + $result = $this->renderer->render($this->block, $this->htmlRenderer); |
| 85 | + |
| 86 | + expect($result)->toBeInstanceOf(HtmlElement::class); |
| 87 | + expect($result->getTagName())->toBe('div'); |
| 88 | + expect($result->getContents(false)->getTagName())->toBe('pre'); |
| 89 | + expect($result->getContents())->toContain('<code class="hljs-copy language-plaintext">'); |
| 90 | + expect($result->getContents())->toContain('test |
| 91 | + test |
| 92 | + test'); |
| 93 | + expect($result->getAllAttributes())->toBe([ |
| 94 | + 'class' => 'p-4 mb-6 rounded-xl bg-theme-secondary-800 overflow-x-auto', |
| 95 | + ]); |
| 96 | + expect($result->getContents(false)->getAllAttributes())->toBe(['class' => 'hljs line-numbers']); |
| 97 | +}); |
| 98 | + |
| 99 | +it('should handle no codeblock type', function () { |
| 100 | + $this->block->setInfo(''); |
| 101 | + $this->block->setLiteral('test |
| 102 | + test |
| 103 | + test'); |
| 104 | + |
| 105 | + $result = $this->renderer->render($this->block, $this->htmlRenderer); |
| 106 | + |
| 107 | + expect($result)->toBeInstanceOf(HtmlElement::class); |
| 108 | + expect($result->getTagName())->toBe('div'); |
| 109 | + expect($result->getContents(false)->getTagName())->toBe('pre'); |
| 110 | + expect($result->getContents())->toContain('<code class="hljs-copy language-plaintext">'); |
| 111 | + expect($result->getContents())->toContain('test |
| 112 | + test |
| 113 | + test'); |
| 114 | + expect($result->getAllAttributes())->toBe([ |
| 115 | + 'class' => 'p-4 mb-6 rounded-xl bg-theme-secondary-800 overflow-x-auto', |
| 116 | + ]); |
| 117 | + expect($result->getContents(false)->getAllAttributes())->toBe(['class' => 'hljs line-numbers']); |
| 118 | +}); |
| 119 | + |
| 120 | +it('should get an xml tag name', function () { |
| 121 | + expect($this->renderer->getXmlTagName($this->block))->toBe('code_block'); |
| 122 | +}); |
| 123 | + |
| 124 | +it('should get xml attributes', function () { |
| 125 | + $this->block->setInfo('blade'); |
| 126 | + |
| 127 | + expect($this->renderer->getXmlAttributes($this->block))->toBe(['info' => 'blade']); |
| 128 | +}); |
| 129 | + |
| 130 | +it('should get no xml attributes if no codeblock type', function () { |
| 131 | + expect($this->renderer->getXmlAttributes($this->block))->toBe([]); |
| 132 | +}); |
0 commit comments