Skip to content

Commit

Permalink
Can read very small HTML files
Browse files Browse the repository at this point in the history
Fixes #194
  • Loading branch information
PowerKiKi committed Dec 11, 2017
1 parent dfcab0c commit 962367c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- Can read very small HTML files - [#194](https://github.com/PHPOffice/PhpSpreadsheet/issues/194)

## [1.0.0-beta2] - 2017-11-26

### Added
Expand Down
9 changes: 7 additions & 2 deletions src/PhpSpreadsheet/Reader/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ public function __construct()
*
* @param string $pFilename
*
* @throws Exception
*
* @return bool
*/
public function canRead($pFilename)
Expand Down Expand Up @@ -148,7 +146,14 @@ private function readEnding()
$filename = $meta['uri'];

$size = filesize($filename);
if ($size === 0) {
return '';
}

$blockSize = self::TEST_SAMPLE_SIZE;
if ($size < $blockSize) {
$blockSize = $size;
}

fseek($this->fileHandle, $size - $blockSize);

Expand Down
30 changes: 30 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,34 @@ public function testCsvWithAngleBracket()
$reader = new Html();
self::assertFalse($reader->canRead($filename));
}

public function providerCanReadVerySmallFile()
{
$padding = str_repeat('a', 2048);

return [
[true, ' <html> ' . $padding . ' </html> '],
[true, ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html>' . $padding . '</html>'],
[true, '<html></html>'],
[false, ''],
];
}

/**
* @dataProvider providerCanReadVerySmallFile
*
* @param bool $expected
* @param string $content
*/
public function testCanReadVerySmallFile($expected, $content)
{
$filename = tempnam(sys_get_temp_dir(), 'html');
file_put_contents($filename, $content);

$reader = new Html();
$actual = $reader->canRead($filename);
unlink($filename);

self::assertSame($expected, $actual);
}
}

0 comments on commit 962367c

Please sign in to comment.