Skip to content

Xml Reader Rich Text #4007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- Protect Sheet But Allow Sort [Issue #3951](https://github.com/PHPOffice/PhpSpreadsheet/issues/3951) [PR #3956](https://github.com/PHPOffice/PhpSpreadsheet/pull/3956)
- Default Value for Conditional::$text [PR #3946](https://github.com/PHPOffice/PhpSpreadsheet/pull/3946)
- Table Filter Buttons [Issue #3988](https://github.com/PHPOffice/PhpSpreadsheet/issues/3988) [PR #3992](https://github.com/PHPOffice/PhpSpreadsheet/pull/3992)
- Improvements to Xml Reader [Issue #3999](https://github.com/PHPOffice/PhpSpreadsheet/issues/3999) [Issue #4000](https://github.com/PHPOffice/PhpSpreadsheet/issues/4000) [Issue #4002](https://github.com/PHPOffice/PhpSpreadsheet/issues/4002) [PR #4003](https://github.com/PHPOffice/PhpSpreadsheet/pull/4003)
- Improvements to Xml Reader [Issue #3999](https://github.com/PHPOffice/PhpSpreadsheet/issues/3999) [Issue #4000](https://github.com/PHPOffice/PhpSpreadsheet/issues/4000) [Issue #4001](https://github.com/PHPOffice/PhpSpreadsheet/issues/4001) [Issue #4002](https://github.com/PHPOffice/PhpSpreadsheet/issues/4002) [PR #4003](https://github.com/PHPOffice/PhpSpreadsheet/pull/4003) [PR #4007](https://github.com/PHPOffice/PhpSpreadsheet/pull/4007)

## 2.0.0 - 2024-01-04

Expand Down
21 changes: 15 additions & 6 deletions src/PhpSpreadsheet/Helper/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,8 @@ class Html

private RichText $richTextObject;

private bool $preserveWhiteSpace = false;

private function initialise(): void
{
$this->face = $this->size = $this->color = null;
Expand All @@ -608,7 +610,7 @@ private function initialise(): void
/**
* Parse HTML formatting and return the resulting RichText.
*/
public function toRichTextObject(string $html): RichText
public function toRichTextObject(string $html, bool $preserveWhiteSpace = false): RichText
{
$this->initialise();

Expand All @@ -622,7 +624,9 @@ public function toRichTextObject(string $html): RichText
$dom->preserveWhiteSpace = false;

$this->richTextObject = new RichText();
$this->preserveWhiteSpace = $preserveWhiteSpace;
$this->parseElements($dom);
$this->preserveWhiteSpace = false;

// Clean any further spurious whitespace
$this->cleanWhitespace();
Expand Down Expand Up @@ -706,6 +710,7 @@ protected function startFontTag(DOMElement $tag): void
if ($attrs !== null) {
foreach ($attrs as $attribute) {
$attributeName = strtolower($attribute->name);
$attributeName = preg_replace('/^html:/', '', $attributeName) ?? $attributeName; // in case from Xml spreadsheet
$attributeValue = $attribute->value;

if ($attributeName == 'color') {
Expand Down Expand Up @@ -795,11 +800,15 @@ public function breakTag(): void

private function parseTextNode(DOMText $textNode): void
{
$domText = (string) preg_replace(
'/\s+/u',
' ',
str_replace(["\r", "\n"], ' ', $textNode->nodeValue ?? '')
);
if ($this->preserveWhiteSpace) {
$domText = $textNode->nodeValue ?? '';
} else {
$domText = (string) preg_replace(
'/\s+/u',
' ',
str_replace(["\r", "\n"], ' ', $textNode->nodeValue ?? '')
);
}
$this->stringData .= $domText;
$this->buildTextRun();
}
Expand Down
9 changes: 9 additions & 0 deletions src/PhpSpreadsheet/Reader/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\DefinedName;
use PhpOffice\PhpSpreadsheet\Helper\Html as HelperHtml;
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\Namespaces;
use PhpOffice\PhpSpreadsheet\Reader\Xml\PageSettings;
Expand Down Expand Up @@ -459,6 +460,14 @@ public function loadIntoExisting(string $filename, Spreadsheet $spreadsheet, boo
*/
case 'String':
$type = DataType::TYPE_STRING;
$rich = $cellData->children('http://www.w3.org/TR/REC-html40');
if ($rich) {
// in case of HTML content we extract the payload
// and convert it into a rich text object
$content = $cellData->asXML() ?: '';
$html = new HelperHtml();
$cellValue = $html->toRichTextObject($content, true);
}

break;
case 'Number':
Expand Down
164 changes: 164 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xml/XmlRichTextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Reader\Xml;

use PhpOffice\PhpSpreadsheet\Reader\Xml;
use PhpOffice\PhpSpreadsheet\RichText\RichText;
use PhpOffice\PhpSpreadsheet\RichText\Run;
use PHPUnit\Framework\TestCase;

class XmlRichTextTest extends TestCase
{
public function testBreakTag(): void
{
$xmldata = <<< 'EOT'
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<Worksheet ss:Name="Test">
<ss:Table>
<ss:Row>
<ss:Cell>
<ss:Data ss:Type="String" xmlns="http://www.w3.org/TR/REC-html40"><I>italic</I><B>bold</B><BR />second line</ss:Data>
</ss:Cell>
</ss:Row>
</ss:Table>
</Worksheet>
</Workbook>
EOT;
$reader = new Xml();
$spreadsheet = $reader->loadSpreadsheetFromString($xmldata);
self::assertEquals(1, $spreadsheet->getSheetCount());

$sheet = $spreadsheet->getActiveSheet();
self::assertEquals('Test', $sheet->getTitle());
$richText = $sheet->getCell('A1')->getValue();
self::assertInstanceOf(RichText::class, $richText);
$elements = $richText->getRichTextElements();
self::assertCount(3, $elements);
$run = $elements[0];
self::assertInstanceOf(Run::class, $run);
self::assertSame('italic', $run->getText());
self::assertNotNull($run->getFont());
self::assertTrue($run->getFont()->getItalic());
self::assertFalse($run->getFont()->getBold());

$run = $elements[1];
self::assertInstanceOf(Run::class, $run);
self::assertSame('bold', $run->getText());
self::assertNotNull($run->getFont());
self::assertFalse($run->getFont()->getItalic());
self::assertTrue($run->getFont()->getBold());

$run = $elements[2];
self::assertInstanceOf(Run::class, $run);
self::assertSame("\nsecond line", $run->getText());

$spreadsheet->disconnectWorksheets();
}

public function testNewlineAndFontTag(): void
{
$xmldata = <<< 'EOT'
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<LastAuthor>Owen Leibman</LastAuthor>
<Created>2024-04-28T06:03:14Z</Created>
<Version>16.00</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<AllowPNG/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>6510</WindowHeight>
<WindowWidth>19200</WindowWidth>
<WindowTopX>32767</WindowTopX>
<WindowTopY>32767</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="Aptos Narrow" x:Family="Swiss" ss:Size="11"
ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s63">
<Alignment ss:Vertical="Bottom" ss:WrapText="1"/>
<Borders/>
<Font ss:FontName="Aptos Narrow" x:Family="Swiss" ss:Size="11" ss:Italic="1"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
</Styles>
<Worksheet ss:Name="Test">
<Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1"
x:FullRows="1" ss:DefaultRowHeight="14.5">
<Row ss:AutoFitHeight="0" ss:Height="47.5">
<Cell ss:StyleID="s63"><ss:Data ss:Type="String"
xmlns="http://www.w3.org/TR/REC-html40"><I>italic</I><B>bold&#10;</B><Font
html:Color="#FF0000">second</Font><Font> line</Font></ss:Data></Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<Unsynced/>
<Selected/>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>
EOT;
$reader = new Xml();
$spreadsheet = $reader->loadSpreadsheetFromString($xmldata);
self::assertEquals(1, $spreadsheet->getSheetCount());

$sheet = $spreadsheet->getActiveSheet();
self::assertEquals('Test', $sheet->getTitle());
$richText = $sheet->getCell('A1')->getValue();
self::assertInstanceOf(RichText::class, $richText);
$elements = $richText->getRichTextElements();
self::assertCount(4, $elements);
$run = $elements[0];
self::assertInstanceOf(Run::class, $run);
self::assertSame('italic', $run->getText());
self::assertNotNull($run->getFont());
self::assertTrue($run->getFont()->getItalic());
self::assertFalse($run->getFont()->getBold());

$run = $elements[1];
self::assertInstanceOf(Run::class, $run);
self::assertSame("bold\n", $run->getText());
self::assertNotNull($run->getFont());
self::assertFalse($run->getFont()->getItalic());
self::assertTrue($run->getFont()->getBold());

$run = $elements[2];
self::assertInstanceOf(Run::class, $run);
self::assertSame('second', $run->getText());
self::assertSame('FF0000', $run->getFont()?->getColor()->getRgb());

$run = $elements[3];
self::assertInstanceOf(Run::class, $run);
self::assertSame(' line', $run->getText());

$spreadsheet->disconnectWorksheets();
}
}