Files
oleibman f3f09bc9a6 Mpdf Styling of Multi-Line Strings
Fix #4773. Mpdf is applying cell style only to the first line of a multi-line string. Explanation - Html Writer replaces newlines with `<br />` by calling `nl2br`, at least that was the intention. However, `nl2br` doesn't replace - it prepends. This doesn't do any harm in Html, Dompdf, or Tcpdf. However, Mpdf is known to be subject to occasional regexp backtracking errors when parsing large blocks of html. To avoid this, we chunk the html by splitting it at newlines, but this causes the styling to be lost for rich text elements following a newline.

The solution to this problem seems easy enough - replace `nl2br` with a routine that replaces rather than prepends. Unfortunately, Html Reader needs the newlines intact in order to parse things correctly. So a solution where the `nl2br` calls are used for non-Mpdf and using a substitute routine for Mpdf ought to work. However, doing my testing uncovered another bug - rich text with newlines can result in multiple `<br />` tags (see new Html test). The solution to this is a bit kludgey, but it does seem to fix both the Mpdf problem and the newly discovered Html problem. It will also tend to avoid the very minor problem of generating different line endings on Windows systems.
2026-01-07 21:34:34 -08:00

113 lines
4.4 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Writer\Html;
use PhpOffice\PhpSpreadsheet\Comment;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
class CommentAlignmentTest extends AbstractFunctional
{
public function testIssue4004(): void
{
$type = 'Html';
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A3')->setValue('A3');
$sheet->getCell('A4')->setValue('A4');
$sheet->getComment('A3')->getText()->createText('Comment');
$sheet->getComment('A4')->getText()->createText('שלום');
$sheet->getComment('A4')->setAlignment(Alignment::HORIZONTAL_RIGHT);
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $type);
$spreadsheet->disconnectWorksheets();
self::assertCount(1, $reloadedSpreadsheet->getAllSheets());
$rsheet = $reloadedSpreadsheet->getActiveSheet();
$comment1 = $rsheet->getComment('A3');
self::assertSame('Comment', $comment1->getText()->getPlainText());
self::assertSame('general', $comment1->getAlignment());
$comment2 = $rsheet->getComment('A4');
self::assertSame('שלום', $comment2->getText()->getPlainText());
self::assertSame('right', $comment2->getAlignment());
$reloadedSpreadsheet->disconnectWorksheets();
}
public function testIssue4004td(): void
{
$type = 'Html';
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setRightToLeft(true);
$sheet->getCell('A1')->setValue('ברקוד');
$comment = $sheet->getComment('A1');
$comment->setTextboxDirection(Comment::TEXTBOX_DIRECTION_RTL);
$comment->setAlignment(Alignment::HORIZONTAL_RIGHT);
$text = <<<EOF
Report : ProductsExcel
סטטוס הגדרות בזמן הרצת הדו"ח
2024-06-04 21:07:04
תאריך התחלה :
תאריך סיום :
berber@berber.co.il
הצגת ברקוד מקוצר = 0
הצגת חנויות אינטרנט בתוצאות = 1
הצגת מחיר ליחידת מידה = 0
הצגת כל רשומות המחיר לתאריך = 0
רשומות עם מחיר בכל הרשתות = 0
% נפיצות מינימלית = 0
נפיצות מינימלית= 0
% נפיצות מקסימלית = 0
נפיצות מקסימלית= 0
פער אחוזי = 0
התעלמות מכלל המבצעים = 0
התעלמות ממבצעי אשראי = 0
התעלמות ממבצעי מועדון = 0
התעלמות ממבצעים המותנים בסכום מעל 100 ₪. = 0
התעלמות ממבצעים המותנים בקניה של 3 מוצרים ומעלה. = 0
ניתוח מבצעים
============
הצגת כל המבצעים = 0
מבצעי מועדון = 0
מבצעי אשראי = 0
מבצעי ארנק = 0
מחיר מוזל = 0
X יחידות ב-Y ₪ = 0
השני ב = 0
X+Y מתנה = 0
אחוז הנחה הפעלה = 0
אחוז הנחה מספר = 0
מוצרים חסרים - הגבלת חודשים - כמות= 0
EOF;
$comment->getText()->createTextRun($text);
$comment->setWidth('300pt');
$comment->setHeight('550pt');
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $type);
$spreadsheet->disconnectWorksheets();
self::assertCount(1, $reloadedSpreadsheet->getAllSheets());
$rsheet = $reloadedSpreadsheet->getActiveSheet();
$comment1 = $rsheet->getComment('A1');
$textSplit = preg_split('/\r?\n/', $text);
$resultSplit = preg_split('/\r?\n/', $comment1->getText()->getPlainText());
self::assertSame($textSplit, $resultSplit);
$comment->setTextboxDirection(Comment::TEXTBOX_DIRECTION_RTL);
self::assertSame('right', $comment1->getAlignment());
self::assertSame('rtl', $comment1->getTextboxDirection());
$reloadedSpreadsheet->disconnectWorksheets();
}
}