Skip to content

Commit

Permalink
Improved the test coverage and some small code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
paquettg committed Apr 6, 2016
1 parent 45d18c3 commit bddea97
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 85 deletions.
81 changes: 0 additions & 81 deletions src/PHPHtmlParser/Dom/AbstractNode.php
Expand Up @@ -377,87 +377,6 @@ public function find($selector, $nth = null)
return $nodes;
}

/**
* Function to try a few tricks to determine the displayed size of an img on the page.
* NOTE: This will ONLY work on an IMG tag. Returns FALSE on all other tag types.
*
* Future enhancement:
* Look in the tag to see if there is a class or id specified that has a height or width attribute to it.
*
* Far future enhancement
* Look at all the parent tags of this image to see if they specify a class or id that has an img selector that specifies a height or width
* Note that in this case, the class or id will have the img sub-selector for it to apply to the image.
*
* ridiculously far future development
* If the class or id is specified in a SEPARATE css file that's not on the page, go get it and do what we were just doing for the ones on the page.
*
* @author John Schlick
* @return array an array containing the 'height' and 'width' of the image on the page or -1 if we can't figure it out.
*/
public function get_display_size()
{
$width = -1;
$height = -1;

if ($this->tag->name() != 'img') {
return false;
}

// See if there is a height or width attribute in the tag itself.
if ( ! is_null($this->tag->getAttribute('width'))) {
$width = $this->tag->getAttribute('width');
}

if ( ! is_null($this->tag->getAttribute('height'))) {
$height = $this->tag->getAttribute('height');
}

// Now look for an inline style.
if ( ! is_null($this->tag->getAttribute('style'))) {
// Thanks to user 'gnarf' from stackoverflow for this regular expression.
$attributes = [];
preg_match_all("/([\w-]+)\s*:\s*([^;]+)\s*;?/", $this->tag->getAttribute('style'), $matches,
PREG_SET_ORDER);
foreach ($matches as $match) {
$attributes[$match[1]] = $match[2];
}

$width = $this->getLength($attributes, $width, 'width');
$height = $this->getLength($attributes, $width, 'height');
}

$result = [
'height' => $height,
'width' => $width,
];

return $result;
}

/**
* If there is a length in the style attributes use it.
*
* @param array $attributes
* @param int $length
* @param string $key
* @return int
*/
protected function getLength(array $attributes, $length, $key)
{
if (isset($attributes[$key]) && $length == -1) {
// check that the last two characters are px (pixels)
if (strtolower(substr($attributes[$key], -2)) == 'px') {
$proposed_length = substr($attributes[$key], 0, -2);
// Now make sure that it's an integer and not something stupid.
if (filter_var($proposed_length, FILTER_VALIDATE_INT)) {
$length = $proposed_length;
}
}
}

return $length;
}

/**
* Gets the inner html of this node.
*
Expand Down
5 changes: 3 additions & 2 deletions src/PHPHtmlParser/StaticDom.php
Expand Up @@ -87,10 +87,11 @@ public static function loadFromFile($file)
* new object.
*
* @param string $url
* @param array $options
* @param CurlInterface $curl
* @return $this
*/
public static function loadFromUrl($url, CurlInterface $curl = null)
public static function loadFromUrl($url, $options = [], CurlInterface $curl = null)
{
$dom = new Dom;
self::$dom = $dom;
Expand All @@ -99,7 +100,7 @@ public static function loadFromUrl($url, CurlInterface $curl = null)
$curl = new Curl;
}

return $dom->loadFromUrl($url, $curl);
return $dom->loadFromUrl($url, $options, $curl);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Node/HtmlTest.php
Expand Up @@ -101,7 +101,7 @@ public function testInnerHtmlUnkownChild()
public function testInnerHtmlMagic()
{
$parent = new HtmlNode('div');
$parent->getTag()->setAttributes([
$parent->tag->setAttributes([
'class' => [
'value' => 'all',
'doubleQuote' => true,
Expand Down
2 changes: 1 addition & 1 deletion tests/Node/TextTest.php
Expand Up @@ -26,7 +26,7 @@ public function testAncestorByTag()
public function testPreserveEntity()
{
$node = new TextNode('i');
$text = $node->text;
$text = $node->innerhtml;
$this->assertEquals('i', $text);
}
}
19 changes: 19 additions & 0 deletions tests/StaticDomTest.php
Expand Up @@ -35,6 +35,12 @@ public function testLoadWithFile()
$this->assertEquals('VonBurgermeister', $dom->find('.post-user font', 0)->text);
}

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

public function testFind()
{
Dom::load('tests/files/horrible.html');
Expand All @@ -54,4 +60,17 @@ public function testFindI()
Dom::load('tests/files/horrible.html');
$this->assertEquals('[ Досие бр:12928 ]', Dom::find('i')[0]->innerHtml);
}

public function testLoadFromUrl()
{
$curl = Mockery::mock('PHPHtmlParser\CurlInterface');
$curl->shouldReceive('get')
->once()
->with('http://google.com')
->andReturn(file_get_contents('tests/files/small.html'));

Dom::loadFromUrl('http://google.com', [], $curl);
$this->assertEquals('VonBurgermeister', Dom::find('.post-row div .post-user font', 0)->text);
}

}

0 comments on commit bddea97

Please sign in to comment.