From 962367c95f4a0b743b80682fc8cb3c6e1187fcc9 Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Mon, 11 Dec 2017 11:08:41 +0900 Subject: [PATCH] Can read very small HTML files Fixes #194 --- CHANGELOG.md | 2 ++ src/PhpSpreadsheet/Reader/Html.php | 9 ++++-- tests/PhpSpreadsheetTests/Reader/HtmlTest.php | 30 +++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d09c30b..3f783d65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/PhpSpreadsheet/Reader/Html.php b/src/PhpSpreadsheet/Reader/Html.php index 5df8f49b..a6720de6 100644 --- a/src/PhpSpreadsheet/Reader/Html.php +++ b/src/PhpSpreadsheet/Reader/Html.php @@ -112,8 +112,6 @@ class Html extends BaseReader * * @param string $pFilename * - * @throws Exception - * * @return bool */ public function canRead($pFilename) @@ -148,7 +146,14 @@ class Html extends BaseReader $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); diff --git a/tests/PhpSpreadsheetTests/Reader/HtmlTest.php b/tests/PhpSpreadsheetTests/Reader/HtmlTest.php index fa75fac7..9f9b11eb 100644 --- a/tests/PhpSpreadsheetTests/Reader/HtmlTest.php +++ b/tests/PhpSpreadsheetTests/Reader/HtmlTest.php @@ -13,4 +13,34 @@ class HtmlTest extends TestCase $reader = new Html(); self::assertFalse($reader->canRead($filename)); } + + public function providerCanReadVerySmallFile() + { + $padding = str_repeat('a', 2048); + + return [ + [true, ' ' . $padding . ' '], + [true, ' ' . $padding . ''], + [true, ''], + [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); + } }