Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
language: php

php:
- 5.6
- 7.0
- hhvm
- 7.1
- 7.2
- nightly

install:
- composer self-update
- composer install --dev --no-interaction

allowed_failures:
- nightly

script:
- vendor/bin/phpunit

Expand Down
16 changes: 10 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
}
],
"require": {
"php": "^5.6 || ^7.0",
"paquettg/string-encode": "~0.1.0"
"php": "^7.1",
"paquettg/string-encode": "^0.1.1"
},
"require-dev": {
"phpunit/phpunit": "~5.3.0",
"satooshi/php-coveralls": "~1.0.0",
"mockery/mockery": "~0.9.0"
"phpunit/phpunit": "^6.5",
"php-coveralls/php-coveralls": "^2.0",
"mockery/mockery": "^1.0"
},
"replace": {
"paquettg/php-html-parser": "self.version"
Expand All @@ -34,5 +34,9 @@
"PHPHtmlParser": "src/"
}
},
"prefer-stable": true
"extra": {
"branch-alias": {
"dev-master": "1.8.x-dev"
}
}
}
22 changes: 11 additions & 11 deletions src/PHPHtmlParser/Dom.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,8 @@ protected function clean($str)
}

// remove white space before closing tags
$str = mb_eregi_replace("'\s+>", "'>", $str);
$str = mb_eregi_replace('"\s+>', '">', $str);
$str = preg_replace("#'\s+>#i", "'>", $str);
$str = preg_replace('#"\s+>#i', '">', $str);

// clean out the \n\r
$replace = ' ';
Expand All @@ -370,31 +370,31 @@ protected function clean($str)
$str = str_replace(["\r\n", "\r", "\n"], $replace, $str);

// strip the doctype
$str = mb_eregi_replace("<!doctype(.*?)>", '', $str);
$str = preg_replace("#<!doctype(.*?)>#i", '', $str);

// strip out comments
$str = mb_eregi_replace("<!--(.*?)-->", '', $str);
$str = preg_replace("#<!--(.*?)-->#i", '', $str);

// strip out cdata
$str = mb_eregi_replace("<!\[CDATA\[(.*?)\]\]>", '', $str);
$str = preg_replace("#<!\[CDATA\[(.*?)\]\]>#i", '', $str);

// strip out <script> tags
if ($this->options->get('removeScripts') == true) {
$str = mb_eregi_replace("<\s*script[^>]*[^/]>(.*?)<\s*/\s*script\s*>", '', $str);
$str = mb_eregi_replace("<\s*script\s*>(.*?)<\s*/\s*script\s*>", '', $str);
$str = preg_replace("#<\s*script[^>]*[^/]>(.*?)<\s*/\s*script\s*>#i", '', $str);
$str = preg_replace("#<\s*script\s*>(.*?)<\s*/\s*script\s*>#i", '', $str);
}

// strip out <style> tags
if ($this->options->get('removeStyles') == true) {
$str = mb_eregi_replace("<\s*style[^>]*[^/]>(.*?)<\s*/\s*style\s*>", '', $str);
$str = mb_eregi_replace("<\s*style\s*>(.*?)<\s*/\s*style\s*>", '', $str);
$str = preg_replace("#<\s*style[^>]*[^/]>(.*?)<\s*/\s*style\s*>#", '', $str);
$str = preg_replace("#<\s*style\s*>(.*?)<\s*/\s*style\s*>#", '', $str);
}

// strip out server side scripts
$str = mb_eregi_replace("(<\?)(.*?)(\?>)", '', $str);
$str = preg_replace("#(<\?)(.*?)(\?>)#i", '', $str);

// strip smarty scripts
$str = mb_eregi_replace("(\{\w)(.*?)(\})", '', $str);
$str = preg_replace("#(\{\w)(.*?)(\})#i", '', $str);

return $str;
}
Expand Down
2 changes: 1 addition & 1 deletion src/PHPHtmlParser/Dom/TextNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class TextNode extends LeafNode
public function __construct($text)
{
// remove double spaces
$text = mb_ereg_replace('\s+', ' ', $text);
$text = preg_replace('#\s+#', ' ', $text);

// restore line breaks
$text = str_replace('&#10;', "\n", $text);
Expand Down
3 changes: 1 addition & 2 deletions src/PHPHtmlParser/Selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ protected function parseSelectorString($selector)
protected function seek(array $nodes, array $rule, array $options)
{
// XPath index
if (count($rule['tag']) > 0 &&
count($rule['key']) > 0 &&
if (!empty($rule['tag']) &&
is_numeric($rule['key'])
) {
$count = 0;
Expand Down
7 changes: 4 additions & 3 deletions tests/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
use PHPHtmlParser\Dom\HtmlNode;
use PHPHtmlParser\Dom\Tag;
use PHPHtmlParser\Dom\Collection;
use PHPUnit\Framework\TestCase;

class CollectionTest extends PHPUnit_Framework_TestCase {
class CollectionTest extends TestCase {

public function testEach()
{
Expand All @@ -29,10 +30,10 @@ public function testEach()
}

/**
* @expectedException PHPHtmlParser\Exceptions\EmptyCollectionException
*/
public function testCallNoNodes()
{
$this->expectException('PHPHtmlParser\Exceptions\EmptyCollectionException');
$collection = new Collection();
$collection->innerHtml();
}
Expand Down Expand Up @@ -70,10 +71,10 @@ public function testGetMagic()
}

/**
* @expectedException PHPHtmlParser\Exceptions\EmptyCollectionException
*/
public function testGetNoNodes()
{
$this->expectException('PHPHtmlParser\Exceptions\EmptyCollectionException');
$collection = new Collection();
$collection->innerHtml;
}
Expand Down
3 changes: 2 additions & 1 deletion tests/ContentTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

use PHPHtmlParser\Content;
use PHPUnit\Framework\TestCase;

class ContentTest extends PHPUnit_Framework_TestCase {
class ContentTest extends TestCase {

public function testChar()
{
Expand Down
24 changes: 12 additions & 12 deletions tests/DomTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

use PHPHtmlParser\Dom;
use PHPHtmlParser\Exceptions\NotLoadedException;
use PHPUnit\Framework\TestCase;

class DomTest extends PHPUnit_Framework_TestCase {
class DomTest extends TestCase {

public function tearDown()
{
Expand All @@ -17,11 +19,9 @@ public function testLoad()
$this->assertEquals('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>', $div->outerHtml);
}

/**
* @expectedException PHPHtmlParser\Exceptions\NotLoadedException
*/
public function testNotLoaded()
{
$this->expectException(NotLoadedException::class);
$dom = new Dom;
$div = $dom->find('div', 0);
}
Expand Down Expand Up @@ -147,21 +147,21 @@ public function testLoadUpperCase()
public function testLoadWithFile()
{
$dom = new Dom;
$dom->loadFromFile('tests/files/small.html');
$dom->loadFromFile(__DIR__ . '/files/small.html');
$this->assertEquals('VonBurgermeister', $dom->find('.post-user font', 0)->text);
}

public function testLoadFromFile()
{
$dom = new Dom;
$dom->loadFromFile('tests/files/small.html');
$dom->loadFromFile(__DIR__ . '/files/small.html');
$this->assertEquals('VonBurgermeister', $dom->find('.post-user font', 0)->text);
}

public function testLoadFromFileFind()
{
$dom = new Dom;
$dom->loadFromFile('tests/files/small.html');
$dom->loadFromFile(__DIR__ . '/files/small.html');
$this->assertEquals('VonBurgermeister', $dom->find('.post-row div .post-user font', 0)->text);
}

Expand All @@ -175,22 +175,22 @@ public function testLoadUtf8()
public function testLoadFileBig()
{
$dom = new Dom;
$dom->loadFromFile('tests/files/big.html');
$dom->loadFromFile(__DIR__ . '/files/big.html');
$this->assertEquals(10, count($dom->find('.content-border')));
}

public function testLoadFileBigTwice()
{
$dom = new Dom;
$dom->loadFromFile('tests/files/big.html');
$dom->loadFromFile(__DIR__ . '/files/big.html');
$post = $dom->find('.post-row', 0);
$this->assertEquals(' <p>Журчанье воды<br /> Черно-белые тени<br /> Вновь на фонтане</p> ', $post->find('.post-message', 0)->innerHtml);
}

public function testLoadFileBigTwicePreserveOption()
{
$dom = new Dom;
$dom->loadFromFile('tests/files/big.html', ['preserveLineBreaks' => true]);
$dom->loadFromFile(__DIR__ . '/files/big.html', ['preserveLineBreaks' => true]);
$post = $dom->find('.post-row', 0);
$this->assertEquals('<p>Журчанье воды<br />
Черно-белые тени<br />
Expand All @@ -203,7 +203,7 @@ public function testLoadFromUrl()
$curl->shouldReceive('get')
->once()
->with('http://google.com')
->andReturn(file_get_contents('tests/files/small.html'));
->andReturn(file_get_contents(__DIR__ . '/files/small.html'));

$dom = new Dom;
$dom->loadFromUrl('http://google.com', [], $curl);
Expand Down Expand Up @@ -262,7 +262,7 @@ public function testGetElementsByClass()
public function testEnforceEncoding()
{
$dom = new Dom;
$dom->load('tests/files/horrible.html', [
$dom->load(__DIR__ . '/files/horrible.html', [
'enforceEncoding' => 'UTF-8',
]);
$this->assertNotEquals('<input type="submit" tabindex="0" name="submit" value="Информации" />', $dom->find('table input', 1)->outerHtml);
Expand Down
25 changes: 12 additions & 13 deletions tests/Node/ChildrenTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php

use PHPHtmlParser\Dom\MockNode as Node;
use PHPHtmlParser\Exceptions\ChildNotFoundException;
use PHPHtmlParser\Exceptions\ParentNotFoundException;
use PHPUnit\Framework\TestCase;

class NodeChildTest extends PHPUnit_Framework_TestCase {
class NodeChildTest extends TestCase {

public function testGetParent()
{
Expand Down Expand Up @@ -32,22 +35,20 @@ public function testNextSibling()
$this->assertEquals($child2->id(), $child->nextSibling()->id());
}

/**
* @expectedException PHPHtmlParser\Exceptions\ChildNotFoundException
*/
public function testNextSiblingNotFound()
{
$this->expectException(ChildNotFoundException::class);

$parent = new Node;
$child = new Node;
$child->setParent($parent);
$child->nextSibling();
}

/**
* @expectedException PHPHtmlParser\Exceptions\ParentNotFoundException
*/
public function testNextSiblingNoParent()
{
$this->expectException(ParentNotFoundException::class);

$child = new Node;
$child->nextSibling();
}
Expand All @@ -62,22 +63,20 @@ public function testPreviousSibling()
$this->assertEquals($child->id(), $child2->previousSibling()->id());
}

/**
* @expectedException PHPHtmlParser\Exceptions\ChildNotFoundException
*/
public function testPreviousSiblingNotFound()
{
$this->expectException(ChildNotFoundException::class);

$parent = new Node;
$node = new Node;
$node->setParent($parent);
$node->previousSibling();
}

/**
* @expectedException PHPHtmlParser\Exceptions\ParentNotFoundException
*/
public function testPreviousSiblingNoParent()
{
$this->expectException(ParentNotFoundException::class);

$child = new Node;
$child->previousSibling();
}
Expand Down
15 changes: 8 additions & 7 deletions tests/Node/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
use PHPHtmlParser\Dom\TextNode;
use PHPHtmlParser\Dom\MockNode;
use PHPHtmlParser\Dom\Tag;
use PHPHtmlParser\Exceptions\ParentNotFoundException;
use PHPHtmlParser\Exceptions\UnknownChildTypeException;
use PHPUnit\Framework\TestCase;

class NodeHtmlTest extends PHPUnit_Framework_TestCase {
class NodeHtmlTest extends TestCase {

public function testInnerHtml()
{
Expand Down Expand Up @@ -65,11 +68,10 @@ public function testInnerHtmlTwice()
$this->assertEquals($inner, $parent->innerHtml());
}

/**
* @expectedException PHPHtmlParser\Exceptions\UnknownChildTypeException
*/
public function testInnerHtmlUnkownChild()
{
$this->expectException(UnknownChildTypeException::class);

$div = new Tag('div');
$div->setAttributes([
'class' => [
Expand Down Expand Up @@ -447,11 +449,10 @@ public function testIterator()
$this->assertEquals(2, $children);
}

/**
* @expectedException PHPHtmlParser\Exceptions\ParentNotFoundException
*/
public function testAncestorByTagFailure()
{
$this->expectException(ParentNotFoundException::class);

$a = new Tag('a');
$node = new HtmlNode($a);
$node->ancestorByTag('div');
Expand Down
Loading