Skip to content
This repository has been archived by the owner on Jan 2, 2019. It is now read-only.

Commit

Permalink
Bugfix: Work Item GH-554 - Whitespace after toRichTextObject()
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkBaker committed Jul 15, 2015
1 parent 88711dc commit f968a95
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 35 deletions.
60 changes: 36 additions & 24 deletions Examples/42richText.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,28 @@
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');

/** Include PHPExcel */
require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
require_once dirname(__FILE__) . '/../src/Bootstrap.php';


// Create new PHPExcel object
echo date('H:i:s') , " Create new PHPExcel object" , EOL;
$objPHPExcel = new PHPExcel();
$objPHPExcel = new \PHPExcel\Spreadsheet();

// Set document properties
echo date('H:i:s') , " Set document properties" , EOL;
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("PHPExcel Test Document")
->setSubject("PHPExcel Test Document")
->setDescription("Test document for PHPExcel, generated using PHP classes.")
->setKeywords("office PHPExcel php")
->setCategory("Test result file");
->setLastModifiedBy("Maarten Balliauw")
->setTitle("PHPExcel Test Document")
->setSubject("PHPExcel Test Document")
->setDescription("Test document for PHPExcel, generated using PHP classes.")
->setKeywords("office PHPExcel php")
->setCategory("Test result file");


// Add some data
echo date('H:i:s') , " Add some data" , EOL;

$html1='<font color="#0000ff">
$html1 = '<font color="#0000ff">
<h1 align="center">My very first example of rich text<br />generated from html markup</h1>
<p>
<font size="14" COLOR="rgb(0,255,128)">
Expand All @@ -64,11 +64,11 @@
</font>
</p>
<p align="right"><font size="9" color="red">
I want to eat <ins><del>healthy food</del><strong>pizza</strong></ins>.
I want to eat <ins><del>healthy food</del> <strong>pizza</strong></ins>.
</font>
';

$html2='<p>
$html2 = '<p>
<font color="#ff0000">
100&deg;C is a hot temperature
</font>
Expand All @@ -78,43 +78,55 @@
</font>
</p>';

$html3='2<sup>3</sup> equals 8';
$html3 = '2<sup>3</sup> equals 8';

$html4='H<sub>2</sub>SO<sub>4</sub> is the chemical formula for Sulphuric acid';
$html4 = 'H<sub>2</sub>SO<sub>4</sub> is the chemical formula for Sulphuric acid';

$html5 = '<strong>bold</strong>, <em>italic</em>, <strong><em>bold+italic</em></strong>';

$wizard = new PHPExcel_Helper_HTML;
$wizard = new \PHPExcel\Helper\HTML;
$richText = $wizard->toRichTextObject($html1);

$objPHPExcel->setActiveSheetIndex(0)
$objPHPExcel->getActiveSheet()
->setCellValue('A1', $richText);

$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(48);
$objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
$objPHPExcel->getActiveSheet()
->getColumnDimension('A')
->setWidth(48);
$objPHPExcel->getActiveSheet()
->getRowDimension(1)
->setRowHeight(-1);
$objPHPExcel->getActiveSheet()->getStyle('A1')
->getAlignment()
->setWrapText(true);

$richText = $wizard->toRichTextObject($html2);

$objPHPExcel->setActiveSheetIndex(0)
$objPHPExcel->getActiveSheet()
->setCellValue('A2', $richText);

$objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
$objPHPExcel->getActiveSheet()->getStyle('A2')
$objPHPExcel->getActiveSheet()
->getRowDimension(1)
->setRowHeight(-1);
$objPHPExcel->getActiveSheet()
->getStyle('A2')
->getAlignment()
->setWrapText(true);

$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A3', $wizard->toRichTextObject($html3));
->setCellValue('A3', $wizard->toRichTextObject($html3));

$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A4', $wizard->toRichTextObject($html4));

$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A5', $wizard->toRichTextObject($html5));


// Rename worksheet
echo date('H:i:s') , " Rename worksheet" , EOL;
$objPHPExcel->getActiveSheet()->setTitle('Simple');
$objPHPExcel->getActiveSheet()
->setTitle('Rich Text Examples');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
Expand All @@ -125,7 +137,7 @@
echo date('H:i:s') , " Write to Excel2007 format" , EOL;
$callStartTime = microtime(true);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter = \PHPExcel\IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
$callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime;
Expand All @@ -140,7 +152,7 @@
echo date('H:i:s') , " Write to Excel5 format" , EOL;
$callStartTime = microtime(true);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter = \PHPExcel\IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(str_replace('.php', '.xls', __FILE__));
$callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime;
Expand Down
4 changes: 2 additions & 2 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
**************************************************************************************


Planned for 1.8.2
Planned for 1.9
- Bugfix: (MBaker) - Fix to getCell() method when cell reference includes a worksheet reference
- Bugfix: (ncrypthic) Work Item GH-570 - Ignore inlineStr type if formula element exists
- Bugfix: (MBaker) Work Item GH-554 - Whitespace after toRichTextObject()
- General: (umpirsky) Work Item GH-548 - Optimize vlookup() sort


2015-04-30 (v1.8.1):
- Bugfix: (goncons) Work Item GH-397 - Fix for Writing an Open Document cell with non-numeric formula
- Bugfix: (sarciszewski) Work Item GH-329 - Avoid potential divide by zero in basedrawing
Expand Down
39 changes: 30 additions & 9 deletions src/PhpSpreadsheet/Helper/HTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ public function toRichTextObject($html)
$this->initialise();

// Create a new DOM object
$dom = new domDocument;
$dom = new \DOMDocument;
// Load the HTML file into the DOM object
// Note the use of error suppression, because typically this will be an html fragment, so not fully valid markup
$loaded = @$dom->loadHTML($html);
Expand All @@ -625,9 +625,27 @@ public function toRichTextObject($html)

$this->richTextObject = new \PHPExcel\RichText();
$this->parseElements($dom);

// Clean any further spurious whitespace
$this->cleanWhitespace();

return $this->richTextObject;
}

protected function cleanWhitespace()
{
foreach($this->richTextObject->getRichTextElements() as $key => $element) {
$text = $element->getText();
// Trim any leading spaces on the first run
if ($key == 0) {
$text = ltrim($text);
}
// Trim any spaces immediately after a line break
$text = preg_replace('/\n */mu', "\n", $text);
$element->setText($text);
}
}

protected function buildTextRun()
{
$text = $this->stringData;
Expand Down Expand Up @@ -767,12 +785,16 @@ protected function endStrikethruTag()

protected function breakTag()
{
$this->stringData .= PHP_EOL;
$this->stringData .= "\n";
}

protected function parseTextNode(DOMText $textNode)
protected function parseTextNode(\DOMText $textNode)
{
$domText = preg_replace('/\s+/u', ' ', ltrim($textNode->nodeValue));
$domText = preg_replace(
'/\s+/u',
' ',
str_replace(["\r", "\n"], ' ', $textNode->nodeValue)
);
$this->stringData .= $domText;
$this->buildTextRun();
}
Expand All @@ -787,26 +809,25 @@ protected function handleCallback($element, $callbackTag, $callbacks)
}
}

protected function parseElementNode(DOMElement $element)
protected function parseElementNode(\DOMElement $element)
{
$callbackTag = strtolower($element->nodeName);
$this->stack[] = $callbackTag;

$this->handleCallback($element, $callbackTag, $this->startTagCallbacks);

$this->parseElements($element);
$this->stringData .= ' ';
array_pop($this->stack);

$this->handleCallback($element, $callbackTag, $this->endTagCallbacks);
}

protected function parseElements(DOMNode $element)
protected function parseElements(\DOMNode $element)
{
foreach ($element->childNodes as $child) {
if ($child instanceof DOMText) {
if ($child instanceof \DOMText) {
$this->parseTextNode($child);
} elseif ($child instanceof DOMElement) {
} elseif ($child instanceof \DOMElement) {
$this->parseElementNode($child);
}
}
Expand Down

0 comments on commit f968a95

Please sign in to comment.