mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-03 05:57:46 +00:00
4a8f2a8f0c
It currently always uses PHP_EOL, which, of course, works, but you get slightly different results in Windows and Unix. User can now set line ending to `\n` or `\r\n` to ensure consistent results regardless of environment. Default remains PHP_EOL. We already do this for CSV.
106 lines
4.7 KiB
PHP
106 lines
4.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Writer\Html;
|
|
|
|
use PhpOffice\PhpSpreadsheet\RichText\RichText;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Html;
|
|
use PhpOffice\PhpSpreadsheetTests\Functional;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
class HtmlCommentsTest extends Functional\AbstractFunctional
|
|
{
|
|
public static function providerCommentRichText(): array
|
|
{
|
|
$valueSingle = 'I am comment.';
|
|
$valueMulti = 'I am ' . PHP_EOL . 'multi-line' . PHP_EOL . 'comment.';
|
|
|
|
$plainSingle = new RichText();
|
|
$plainSingle->createText($valueSingle);
|
|
|
|
$plainMulti = new RichText();
|
|
$plainMulti->createText($valueMulti);
|
|
|
|
$richSingle = new RichText();
|
|
$font = $richSingle->createTextRun($valueSingle)->getFont();
|
|
self::assertNotNull($font);
|
|
$font->setBold(true);
|
|
|
|
$richMultiSimple = new RichText();
|
|
$font = $richMultiSimple->createTextRun($valueMulti)->getFont();
|
|
self::assertNotNull($font);
|
|
$font->setBold(true);
|
|
|
|
$richMultiMixed = new RichText();
|
|
$richMultiMixed->createText('I am' . PHP_EOL);
|
|
$font = $richMultiMixed->createTextRun('multi-line')->getFont();
|
|
self::assertNotNull($font);
|
|
$font->setBold(true);
|
|
$richMultiMixed->createText(PHP_EOL . 'comment!');
|
|
|
|
$richMultiMixed = new RichText();
|
|
$richMultiMixed->createText('I am' . PHP_EOL);
|
|
$font = $richMultiMixed->createTextRun('multi-line')->getFont();
|
|
self::assertNotNull($font);
|
|
$font->setBold(true);
|
|
$richMultiMixed->createText(PHP_EOL . 'comment!');
|
|
|
|
$scriptTextSingle = 'I am <script>alert("Hello")</script>';
|
|
$scriptSingle = new RichText();
|
|
$scriptSingle->createText($scriptTextSingle);
|
|
|
|
return [
|
|
'single line plain text' => [$plainSingle, '<td class="column0 style0 s"><a class="comment-indicator"></a><div class="comment">'
|
|
. 'I am comment.</div>' . PHP_EOL
|
|
. 'Comment</td>'],
|
|
'multi-line plain text' => [$plainMulti, '<td class="column0 style0 s"><a class="comment-indicator"></a><div class="comment">'
|
|
. 'I am <br />' . PHP_EOL
|
|
. 'multi-line<br />' . PHP_EOL
|
|
. 'comment.</div>' . PHP_EOL
|
|
. 'Comment</td>'],
|
|
'single line simple rich text' => [$richSingle, '<td class="column0 style0 s"><a class="comment-indicator"></a><div class="comment">'
|
|
. "<span style=\"font-weight:bold; text-decoration:normal; font-style:normal; color:#000000; font-family:'Calibri'; font-size:11pt\">I am comment.</span></div>" . PHP_EOL
|
|
. 'Comment</td>'],
|
|
'multi-line simple rich text' => [$richMultiSimple, '<td class="column0 style0 s"><a class="comment-indicator"></a><div class="comment">'
|
|
. "<span style=\"font-weight:bold; text-decoration:normal; font-style:normal; color:#000000; font-family:'Calibri'; font-size:11pt\">I am <br />" . PHP_EOL
|
|
. 'multi-line<br />' . PHP_EOL
|
|
. 'comment.</span></div>' . PHP_EOL
|
|
. 'Comment</td>'],
|
|
'multi-line mixed rich text' => [$richMultiMixed, '<td class="column0 style0 s"><a class="comment-indicator"></a><div class="comment">I am<br />' . PHP_EOL
|
|
. "<span style=\"font-weight:bold; text-decoration:normal; font-style:normal; color:#000000; font-family:'Calibri'; font-size:11pt\">multi-line</span><br />" . PHP_EOL
|
|
. 'comment!</div>' . PHP_EOL
|
|
. 'Comment</td>'],
|
|
'script single' => [$scriptSingle, '<td class="column0 style0 s"><a class="comment-indicator"></a><div class="comment">'
|
|
. 'I am <script>alert("Hello")</script></div>' . PHP_EOL
|
|
. 'Comment</td>'],
|
|
];
|
|
}
|
|
|
|
#[DataProvider('providerCommentRichText')]
|
|
public function testComments(RichText $richText, string $expected): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
|
|
$spreadsheet->getActiveSheet()
|
|
->getCell('A1')
|
|
->setValue('Comment');
|
|
|
|
$spreadsheet->getActiveSheet()
|
|
->getComment('A1')
|
|
->setText($richText);
|
|
$writer = new Html($spreadsheet);
|
|
$writer->setLineEnding(PHP_EOL);
|
|
$output = $writer->generateHtmlAll();
|
|
self::assertStringContainsString($expected, $output);
|
|
|
|
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Html');
|
|
$spreadsheet->disconnectWorksheets();
|
|
|
|
$actual = $reloadedSpreadsheet->getActiveSheet()->getComment('A1')->getText()->getPlainText();
|
|
self::assertSame($richText->getPlainText(), $actual);
|
|
$reloadedSpreadsheet->disconnectWorksheets();
|
|
}
|
|
}
|