Skip to content

Commit

Permalink
new remover "element by class by index"
Browse files Browse the repository at this point in the history
  • Loading branch information
panakour committed Jun 15, 2019
1 parent 5b242ce commit 8393b14
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
35 changes: 35 additions & 0 deletions src/Remove/ElementByClassByIndexRemover.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php


namespace Pkscraper\Remove;


use DOMXPath;
use Pkscraper\Dom\Dom;

class ElementByClassByIndexRemover extends Remover
{

protected $class;
protected $index;

/**
* ElementByClassByIndexRemover constructor.
* @param string $class
* @param int $index
*/
public function __construct($class, $index)
{
$this->class = $class;
$this->index = $index;
}

public function remove(): string
{
$dom = new Dom($this->document);
$xpath = new DOMXPath($dom->DOMDocument);
$elementsToBeRemove = $xpath->query('//*[contains(concat(" ", normalize-space(@class), " "), " ' . $this->class . ' ")]');
$elementsToBeRemove->item($this->index)->parentNode->removeChild($elementsToBeRemove->item($this->index));
return $dom->get();
}
}
28 changes: 28 additions & 0 deletions tests/ElementByClassByIndexRemoverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php


namespace Pkscraper\Tests;


use PHPUnit\Framework\TestCase;
use Pkscraper\Remove\ElementByClassByIndexRemover;

class ElementByClassByIndexRemoverTest extends TestCase
{

public function testRemove()
{
$remover = new ElementByClassByIndexRemover('column', 0);
$remover->setDocument(file_get_contents(__DIR__ . '/fixtures/htmlDocument.html'));
$result = $remover->remove();
$this->assertStringNotContainsString('first column', $result);
$this->assertStringContainsString('second column', $result);

$remover = new ElementByClassByIndexRemover('column', 1);
$remover->setDocument(file_get_contents(__DIR__ . '/fixtures/htmlDocument.html'));
$result = $remover->remove();
$this->assertStringContainsString('first column', $result);
$this->assertStringNotContainsString('second column', $result);
}

}
6 changes: 4 additions & 2 deletions tests/fixtures/htmlDocument.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
color: blue;
}
</style>
<div>
<div class="column">
<p style="color: red; font-family: Arial; font-size: 22px;">first paragraph content</p>
<p>first column</p>
<span>
span content
<img id="example-image-first" width="121" height="3422" alt="test" src="http://www.example.com/image.jpg">
Expand All @@ -19,7 +20,8 @@
<script>
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script>
<div id="copy" style="font-size: 18px;" class="mt20 clearfix">\n
<div id="copy" style="font-size: 18px;" class="mt20 clearfix column">\n
<p>second column</p>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's
standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make
Expand Down

0 comments on commit 8393b14

Please sign in to comment.