|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use EndlessCreativity\ElephantPhp\Document\Document; |
| 6 | +use EndlessCreativity\ElephantPhp\Document\DocumentConverter; |
| 7 | +use EndlessCreativity\ElephantPhp\Document\Paragraph; |
| 8 | +use EndlessCreativity\ElephantPhp\Document\Run; |
| 9 | +use EndlessCreativity\ElephantPhp\Document\Text; |
| 10 | + |
| 11 | +function htmlOfDocument(Document $document, bool $ignoreHiddenText = true): string |
| 12 | +{ |
| 13 | + return (new DocumentConverter(ignoreHiddenText: $ignoreHiddenText)) |
| 14 | + ->convertToHtml($document) |
| 15 | + ->value; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * @param list<Run> $runs |
| 20 | + */ |
| 21 | +function paragraphWithRuns(array $runs): Document |
| 22 | +{ |
| 23 | + return new Document(children: [new Paragraph(children: $runs)]); |
| 24 | +} |
| 25 | + |
| 26 | +it('drops a hidden run by default, keeping surrounding visible text', function (): void { |
| 27 | + $document = paragraphWithRuns([ |
| 28 | + new Run(children: [new Text(value: 'before')]), |
| 29 | + new Run(isHidden: true, children: [new Text(value: 'SECRET')]), |
| 30 | + new Run(children: [new Text(value: 'after')]), |
| 31 | + ]); |
| 32 | + |
| 33 | + expect(htmlOfDocument($document))->toBe('<p>beforeafter</p>'); |
| 34 | +}); |
| 35 | + |
| 36 | +it('emits a hidden run as ordinary text when ignoreHiddenText is false', function (): void { |
| 37 | + $document = paragraphWithRuns([ |
| 38 | + new Run(children: [new Text(value: 'before')]), |
| 39 | + new Run(isHidden: true, children: [new Text(value: 'SECRET')]), |
| 40 | + new Run(children: [new Text(value: 'after')]), |
| 41 | + ]); |
| 42 | + |
| 43 | + expect(htmlOfDocument($document, ignoreHiddenText: false)) |
| 44 | + ->toBe('<p>beforeSECRETafter</p>'); |
| 45 | +}); |
| 46 | + |
| 47 | +it('drops a paragraph whose only content is a hidden run', function (): void { |
| 48 | + $document = paragraphWithRuns([ |
| 49 | + new Run(isHidden: true, children: [new Text(value: 'entirely hidden')]), |
| 50 | + ]); |
| 51 | + |
| 52 | + expect(htmlOfDocument($document))->toBe(''); |
| 53 | +}); |
| 54 | + |
| 55 | +it('leaves a visible run untouched', function (): void { |
| 56 | + $document = paragraphWithRuns([ |
| 57 | + new Run(children: [new Text(value: 'visible')]), |
| 58 | + ]); |
| 59 | + |
| 60 | + expect(htmlOfDocument($document))->toBe('<p>visible</p>'); |
| 61 | +}); |
0 commit comments