mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-24 17:19:29 +00:00
ba079b3967
Fix #1154, which went stale over 5 years ago, and is now reopened. RichText elements can be a Run, which sets its own style, or a TextElement, which doesn't. As the issue states, TextElement handling is inconsistent. This PR forces it to inherit the style of the cell. As implemented, this will change it to a Run when it is read in, but there should be no practical difference as far as the end-user is concerned. There is no change as far as Run is concerned. The user suggested 3 options - TextElement and Run both inherit for any unspecified style elements, TextElement inherits and Run does not, neither inherits. Option 2 makes most sense to me. Option 1 may not even be possible (we can't tell if, say, the user explicityl set Italic to false, or if that was just the default choice). Tests are added for all of Xlsx, Xls, and Html. Html reading of RichText elements is not well-supported. The tests are fairly difficult to understand. A new sample is added to demonstrate this change.
105 lines
4.7 KiB
PHP
105 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);
|
|
$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();
|
|
}
|
|
}
|