Skip to content

Commit

Permalink
Implement Html::extractAsSnippets
Browse files Browse the repository at this point in the history
  • Loading branch information
theseer committed Apr 28, 2022
1 parent 61c7b9c commit 710d330
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

use DOMDocument;
use DOMDocumentType;
use DOMElement;
use DOMNode;
use DOMText;

class Html {

Expand Down Expand Up @@ -69,6 +72,39 @@ public function toSnippet(string $id): Snippet {
return new SimpleSnippet($id, $imported);
}

public function extractAsSnippets(Selector $selector, string $targetId): SnippetList {
$selection = $selector->select($this->dom->documentElement);
if ($selection->isEmpty()) {
throw new TempladoException('Selection result is empty - cannot extract');
}

$list = new SnippetList();
foreach($selection as $item) {
assert($item instanceof DOMNode);

if ($item instanceof DOMText) {
$list->addSnippet(
new TextSnippet($targetId, $item)
);

continue;
}

if ($item instanceof DOMElement) {
$list->addSnippet(
new SimpleSnippet($targetId, $item)
);

continue;
}

throw new TempladoException('Unspported node type - cannot extract to snippet');
}

return $list;
}


public function asString(Filter $filter = null): string {
$content = $this->serializeDomDocument();
$content = (new EmptyElementsFilter())->apply($content);
Expand Down
92 changes: 92 additions & 0 deletions tests/HTMLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,96 @@ public function testCanBeConvertedToSnippet(): void {
$this->assertInstanceOf(Snippet::class, $snippet);
$this->assertEquals('test', $snippet->getTargetId());
}

/**
* @uses \Templado\Engine\XPathSelector
* @uses \Templado\Engine\Selection
* @uses \Templado\Engine\TempladoException
*/
public function testEmptySelectionOnExtractThrowsException(): void {
$dom = new \DOMDocument();
$dom->loadXML('<?xml version="1.0" ?><root><a/><a/><a/></root>');

$selector = new XPathSelector('//no-match');

$this->expectException(TempladoException::class);
(new Html($dom))->extractAsSnippets($selector, 'some-id');
}

/**
* @uses \Templado\Engine\XPathSelector
* @uses \Templado\Engine\SimpleSnippet
* @uses \Templado\Engine\SnippetList
* @uses \Templado\Engine\Selection
*/
public function testSimpleSnippetsCanBeExtracted(): void {
$dom = new \DOMDocument();
$dom->loadXML('<?xml version="1.0" ?><root><a/><a/><a/></root>');

$selector = new XPathSelector('//a');

$list = (new Html($dom))->extractAsSnippets($selector, 'some-id');

$testDom = new DOMDocument();

$this->assertCount(3, $list);
foreach($list as $snippet) {
$this->assertInstanceOf(SimpleSnippet::class, $snippet);
$this->assertSame('some-id', $snippet->getTargetId());

$targetNode = $testDom->createElement('target');

$snippet->applyTo($targetNode);

$this->assertSame('<target><a/></target>', $testDom->saveXML($targetNode));
}
}

/**
* @uses \Templado\Engine\XPathSelector
* @uses \Templado\Engine\SimpleSnippet
* @uses \Templado\Engine\TextSnippet
* @uses \Templado\Engine\SnippetList
* @uses \Templado\Engine\Selection
*/
public function testTextSnippetsCanBeExtracted(): void {
$dom = new \DOMDocument();
$dom->loadXML('<?xml version="1.0" ?><root>Text</root>');

$selector = new XPathSelector('/root/text()');

$list = (new Html($dom))->extractAsSnippets($selector, 'some-id');

$testDom = new DOMDocument();

$this->assertCount(1, $list);

foreach($list as $snippet) {
$this->assertInstanceOf(TextSnippet::class, $snippet);
$this->assertSame('some-id', $snippet->getTargetId());

$targetNode = $testDom->createElement('target');

$snippet->applyTo($targetNode);

$this->assertSame('<target>Text</target>', $testDom->saveXML($targetNode));
}
}

/**
* @uses \Templado\Engine\Selection
* @uses \Templado\Engine\TempladoException
* @uses \Templado\Engine\XPathSelector
*/
public function testTryingToExtractNonElementNodeThrowsException(): void {
$dom = new \DOMDocument();
$dom->loadXML('<?xml version="1.0" ?><root><?php // ?></root>');

$selector = new XPathSelector('//processing-instruction("php")');

$this->expectException(TempladoException::class);
(new Html($dom))->extractAsSnippets($selector, 'some-id');

}

}

0 comments on commit 710d330

Please sign in to comment.