mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-30 03:59:11 +00:00
5de82981d8
* Html Reader Not Handling non-ASCII Data Correctly Fix #2942. Code was changed by #2894 because PHP8.2 will deprecate how it was being done. See linked issue for more details. Dom loadhtml assumes ISO-8859-1 in the absence of a charset attribute or equivalent, and there is no way to override that assumption. Sigh. The suggested replacements are unsuitable in one way or another. I think this will work with minimal disruption (replace ampersand, less than, and greater than with entities representing illegal characters, then use htmlentities, then restore ampersand, less than, and greater than). * Better Implementation Use regexp to escape non-ASCII. Less kludgey, less reliant on the vagaries of the PHP maintainers. * Additional Tests Test non-ASCII outside of cell contents: sheet title, image alt attribute. * Apply Same Change in Second Location Forgot to change loadFromString. * Additional Test Confirm escaped ampersand is handled correctly.
85 lines
3.0 KiB
PHP
85 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader\Html;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class HtmlImageTest extends TestCase
|
|
{
|
|
public function testCanInsertImage(): void
|
|
{
|
|
$imagePath = realpath(__DIR__ . '/../../../data/Reader/HTML/image.jpg');
|
|
|
|
$html = '<table>
|
|
<tr>
|
|
<td><img src="' . $imagePath . '" alt="test image voilà"></td>
|
|
</tr>
|
|
</table>';
|
|
$filename = HtmlHelper::createHtml($html);
|
|
$spreadsheet = HtmlHelper::loadHtmlIntoSpreadsheet($filename, true);
|
|
$firstSheet = $spreadsheet->getSheet(0);
|
|
|
|
/** @var Drawing $drawing */
|
|
$drawing = $firstSheet->getDrawingCollection()[0];
|
|
self::assertEquals($imagePath, $drawing->getPath());
|
|
self::assertEquals('A1', $drawing->getCoordinates());
|
|
self::assertEquals('test image voilà', $drawing->getName());
|
|
self::assertEquals('100', $drawing->getWidth());
|
|
self::assertEquals('100', $drawing->getHeight());
|
|
}
|
|
|
|
public function testCanInsertImageWidth(): void
|
|
{
|
|
$imagePath = realpath(__DIR__ . '/../../../data/Reader/HTML/image.jpg');
|
|
|
|
$html = '<table>
|
|
<tr>
|
|
<td><img src="' . $imagePath . '" alt="test image" width="50"></td>
|
|
</tr>
|
|
</table>';
|
|
$filename = HtmlHelper::createHtml($html);
|
|
$spreadsheet = HtmlHelper::loadHtmlIntoSpreadsheet($filename, true);
|
|
$firstSheet = $spreadsheet->getSheet(0);
|
|
|
|
/** @var Drawing $drawing */
|
|
$drawing = $firstSheet->getDrawingCollection()[0];
|
|
self::assertEquals('50', $drawing->getWidth());
|
|
self::assertEquals('50', $drawing->getHeight());
|
|
}
|
|
|
|
public function testCanInsertImageHeight(): void
|
|
{
|
|
$imagePath = realpath(__DIR__ . '/../../../data/Reader/HTML/image.jpg');
|
|
|
|
$html = '<table>
|
|
<tr>
|
|
<td><img src="' . $imagePath . '" height="75"></td>
|
|
</tr>
|
|
</table>';
|
|
$filename = HtmlHelper::createHtml($html);
|
|
$spreadsheet = HtmlHelper::loadHtmlIntoSpreadsheet($filename, true);
|
|
$firstSheet = $spreadsheet->getSheet(0);
|
|
|
|
/** @var Drawing $drawing */
|
|
$drawing = $firstSheet->getDrawingCollection()[0];
|
|
self::assertEquals('', $drawing->getName());
|
|
self::assertEquals('75', $drawing->getWidth());
|
|
self::assertEquals('75', $drawing->getHeight());
|
|
}
|
|
|
|
public function testImageWithourSrc(): void
|
|
{
|
|
$html = '<table>
|
|
<tr>
|
|
<td><img></td>
|
|
</tr>
|
|
</table>';
|
|
$filename = HtmlHelper::createHtml($html);
|
|
$spreadsheet = HtmlHelper::loadHtmlIntoSpreadsheet($filename, true);
|
|
$firstSheet = $spreadsheet->getSheet(0);
|
|
|
|
self::assertCount(0, $firstSheet->getDrawingCollection());
|
|
}
|
|
}
|