Skip to content

Commit

Permalink
Merge pull request #122 from Icinga/feature/improve-html-document-class
Browse files Browse the repository at this point in the history
Improve `HtmlDocument` class
  • Loading branch information
nilmerg committed Jun 2, 2023
2 parents 8740b9e + 2cf15bf commit 8891477
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/HtmlDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Exception;
use InvalidArgumentException;
use ipl\Html\Contract\Wrappable;
use ipl\Stdlib\Events;
use RuntimeException;

/**
Expand All @@ -15,6 +16,11 @@
*/
class HtmlDocument implements Countable, Wrappable
{
use Events;

/** @var string Emitted after the content has been assembled */
public const ON_ASSEMBLED = 'assembled';

/** @var string Content separator */
protected $contentSeparator = '';

Expand Down Expand Up @@ -152,6 +158,50 @@ public function getFirst($tag)
));
}

/**
* Insert Html after an existing Html node
*
* @param ValidHtml $newNode
* @param ValidHtml $existingNode
*
* @return $this
*/
public function insertAfter(ValidHtml $newNode, ValidHtml $existingNode): self
{
$index = array_search($existingNode, $this->content, true);
if ($index === false) {
throw new InvalidArgumentException('The content does not contain the $existingNode');
}

array_splice($this->content, $index + 1, 0, [$newNode]);

$this->reIndexContent();

return $this;
}

/**
* Insert Html after an existing Html node
*
* @param ValidHtml $newNode
* @param ValidHtml $existingNode
*
* @return $this
*/
public function insertBefore(ValidHtml $newNode, ValidHtml $existingNode): self
{
$index = array_search($existingNode, $this->content);
if ($index === false) {
throw new InvalidArgumentException('The content does not contain the $existingNode');
}

array_splice($this->content, $index, 0, [$newNode]);

$this->reIndexContent();

return $this;
}

/**
* Add content
*
Expand Down Expand Up @@ -292,6 +342,8 @@ public function ensureAssembled()
if (! $this->hasBeenAssembled) {
$this->hasBeenAssembled = true;
$this->assemble();

$this->emit(static::ON_ASSEMBLED, [$this]);
}

return $this;
Expand Down
49 changes: 49 additions & 0 deletions tests/HtmlDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,4 +421,53 @@ public function testIsEmptyRespectsContentAddedInAssemble()
{
$this->assertFalse((new AddsContentDuringAssemble())->isEmpty());
}

public function testOnAssembledDocument()
{
$a = new HtmlDocument();
$s1 = h::tag('span', 'one');
$s2 = h::tag('span', 'two');
$s3 = h::tag('span', 'three');
$a->add([$s1, $s2]);

$a->on(HtmlDocument::ON_ASSEMBLED, function ($b) use ($s2, $s3) {
$b->remove($s2);
$b->add($s3);
});

$this->assertEquals(
'<span>one</span><span>three</span>',
$a->render()
);
}

public function testInsertAfter()
{
$a = new HtmlDocument();
$s1 = h::tag('span', 'one');
$s2 = h::tag('span', 'two');
$s3 = h::tag('span', 'three');
$a->add([$s1, $s2]);
$a->insertAfter($s3, $s1);

$this->assertEquals(
'<span>one</span><span>three</span><span>two</span>',
$a->render()
);
}

public function testInsertBefore()
{
$a = new HtmlDocument();
$s1 = h::tag('span', 'one');
$s2 = h::tag('span', 'two');
$s3 = h::tag('span', 'three');
$a->add([$s1, $s2]);
$a->insertBefore($s3, $s1);

$this->assertEquals(
'<span>three</span><span>one</span><span>two</span>',
$a->render()
);
}
}

0 comments on commit 8891477

Please sign in to comment.