Skip to content

Commit 37b4160

Browse files
committed
Fix in HTML_Index::detect_charset() for too short html #91
1 parent 591542a commit 37b4160

3 files changed

Lines changed: 17 additions & 3 deletions

File tree

examples/playground.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
hQuery::$cache_path = sys_get_temp_dir() . '/hQuery/';
1919
isset($ch) && $ch !== '' and hQuery::$cache_expires = (int)$ch;
2020

21-
// Results acumulator
21+
// Results accumulator
2222
$return = array();
2323

2424
// If we have $url to parse and $sel (selector) to fetch, we a good to go

src/hQuery/HTML_Index.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,9 @@ public static function abs_url($url, $base)
402402
*/
403403
public static function detect_charset($str)
404404
{
405-
$l = 1024;
406-
$str = substr($str, 0, $l);
405+
$str = substr($str, 0, 1024);
407406
$str_ = strtolower($str);
407+
$l = strlen($str_);
408408
$p = 0;
409409
while ($p < $l) {
410410
$p = strpos($str_, '<meta', $p);

tests/hQueryCore.Test.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ class hQueryCore extends PHPUnit_BaseClass
122122
</body></html>
123123
EOS;
124124

125+
public static $badHTML1 = '<iframe><meta http-equiv="refresh" content="1;/>';
126+
125127
// Before any test
126128
public static function mySetUpBeforeClass()
127129
{
@@ -216,6 +218,10 @@ public function test_fromHTML()
216218
$emptyDoc = $doc::fromHTML('');
217219
$this->assertInstanceOf(get_class($doc), $emptyDoc);
218220
$this->assertEquals('', $emptyDoc->html);
221+
222+
// Bad HTML
223+
$badDoc = hQueryTestSurrogate::fromHTML(self::$badHTML1);
224+
$this->assertInstanceOf(get_class($doc), $badDoc);
219225
}
220226

221227
// -----------------------------------------------------
@@ -347,6 +353,14 @@ public function test_find()
347353
$b = $edoc->find('body');
348354
$this->assertEquals(1, count($b));
349355

356+
// 6)
357+
$bdoc = hQueryTestSurrogate::fromHTML(self::$badHTML1, self::$baseUrl . 'index.html');
358+
$a = $bdoc->find('iframe');
359+
$this->assertEquals(1, count($a));
360+
361+
$b = $bdoc->find('meta');
362+
$this->assertEquals(1, count($b));
363+
350364
return $ff;
351365
}
352366

0 commit comments

Comments
 (0)