Skip to content

Commit bddea97

Browse files
committed
Improved the test coverage and some small code changes
1 parent 45d18c3 commit bddea97

File tree

5 files changed

+24
-85
lines changed

5 files changed

+24
-85
lines changed

src/PHPHtmlParser/Dom/AbstractNode.php

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -377,87 +377,6 @@ public function find($selector, $nth = null)
377377
return $nodes;
378378
}
379379

380-
/**
381-
* Function to try a few tricks to determine the displayed size of an img on the page.
382-
* NOTE: This will ONLY work on an IMG tag. Returns FALSE on all other tag types.
383-
*
384-
* Future enhancement:
385-
* Look in the tag to see if there is a class or id specified that has a height or width attribute to it.
386-
*
387-
* Far future enhancement
388-
* 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
389-
* Note that in this case, the class or id will have the img sub-selector for it to apply to the image.
390-
*
391-
* ridiculously far future development
392-
* 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.
393-
*
394-
* @author John Schlick
395-
* @return array an array containing the 'height' and 'width' of the image on the page or -1 if we can't figure it out.
396-
*/
397-
public function get_display_size()
398-
{
399-
$width = -1;
400-
$height = -1;
401-
402-
if ($this->tag->name() != 'img') {
403-
return false;
404-
}
405-
406-
// See if there is a height or width attribute in the tag itself.
407-
if ( ! is_null($this->tag->getAttribute('width'))) {
408-
$width = $this->tag->getAttribute('width');
409-
}
410-
411-
if ( ! is_null($this->tag->getAttribute('height'))) {
412-
$height = $this->tag->getAttribute('height');
413-
}
414-
415-
// Now look for an inline style.
416-
if ( ! is_null($this->tag->getAttribute('style'))) {
417-
// Thanks to user 'gnarf' from stackoverflow for this regular expression.
418-
$attributes = [];
419-
preg_match_all("/([\w-]+)\s*:\s*([^;]+)\s*;?/", $this->tag->getAttribute('style'), $matches,
420-
PREG_SET_ORDER);
421-
foreach ($matches as $match) {
422-
$attributes[$match[1]] = $match[2];
423-
}
424-
425-
$width = $this->getLength($attributes, $width, 'width');
426-
$height = $this->getLength($attributes, $width, 'height');
427-
}
428-
429-
$result = [
430-
'height' => $height,
431-
'width' => $width,
432-
];
433-
434-
return $result;
435-
}
436-
437-
/**
438-
* If there is a length in the style attributes use it.
439-
*
440-
* @param array $attributes
441-
* @param int $length
442-
* @param string $key
443-
* @return int
444-
*/
445-
protected function getLength(array $attributes, $length, $key)
446-
{
447-
if (isset($attributes[$key]) && $length == -1) {
448-
// check that the last two characters are px (pixels)
449-
if (strtolower(substr($attributes[$key], -2)) == 'px') {
450-
$proposed_length = substr($attributes[$key], 0, -2);
451-
// Now make sure that it's an integer and not something stupid.
452-
if (filter_var($proposed_length, FILTER_VALIDATE_INT)) {
453-
$length = $proposed_length;
454-
}
455-
}
456-
}
457-
458-
return $length;
459-
}
460-
461380
/**
462381
* Gets the inner html of this node.
463382
*

src/PHPHtmlParser/StaticDom.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,11 @@ public static function loadFromFile($file)
8787
* new object.
8888
*
8989
* @param string $url
90+
* @param array $options
9091
* @param CurlInterface $curl
9192
* @return $this
9293
*/
93-
public static function loadFromUrl($url, CurlInterface $curl = null)
94+
public static function loadFromUrl($url, $options = [], CurlInterface $curl = null)
9495
{
9596
$dom = new Dom;
9697
self::$dom = $dom;
@@ -99,7 +100,7 @@ public static function loadFromUrl($url, CurlInterface $curl = null)
99100
$curl = new Curl;
100101
}
101102

102-
return $dom->loadFromUrl($url, $curl);
103+
return $dom->loadFromUrl($url, $options, $curl);
103104
}
104105

105106
/**

tests/Node/HtmlTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function testInnerHtmlUnkownChild()
101101
public function testInnerHtmlMagic()
102102
{
103103
$parent = new HtmlNode('div');
104-
$parent->getTag()->setAttributes([
104+
$parent->tag->setAttributes([
105105
'class' => [
106106
'value' => 'all',
107107
'doubleQuote' => true,

tests/Node/TextTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function testAncestorByTag()
2626
public function testPreserveEntity()
2727
{
2828
$node = new TextNode('i');
29-
$text = $node->text;
29+
$text = $node->innerhtml;
3030
$this->assertEquals('i', $text);
3131
}
3232
}

tests/StaticDomTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ public function testLoadWithFile()
3535
$this->assertEquals('VonBurgermeister', $dom->find('.post-user font', 0)->text);
3636
}
3737

38+
public function testLoadFromFile()
39+
{
40+
$dom = Dom::loadFromFile('tests/files/small.html');
41+
$this->assertEquals('VonBurgermeister', $dom->find('.post-user font', 0)->text);
42+
}
43+
3844
public function testFind()
3945
{
4046
Dom::load('tests/files/horrible.html');
@@ -54,4 +60,17 @@ public function testFindI()
5460
Dom::load('tests/files/horrible.html');
5561
$this->assertEquals('[ Досие бр:12928 ]', Dom::find('i')[0]->innerHtml);
5662
}
63+
64+
public function testLoadFromUrl()
65+
{
66+
$curl = Mockery::mock('PHPHtmlParser\CurlInterface');
67+
$curl->shouldReceive('get')
68+
->once()
69+
->with('http://google.com')
70+
->andReturn(file_get_contents('tests/files/small.html'));
71+
72+
Dom::loadFromUrl('http://google.com', [], $curl);
73+
$this->assertEquals('VonBurgermeister', Dom::find('.post-row div .post-user font', 0)->text);
74+
}
75+
5776
}

0 commit comments

Comments
 (0)