Bugfix: Work Item GH-554 - Whitespace after toRichTextObject()

This commit is contained in:
MarkBaker 2015-07-15 23:23:20 +01:00
parent 88711dc7b9
commit f968a95ca5
3 changed files with 68 additions and 35 deletions

View File

@ -34,12 +34,12 @@ date_default_timezone_set('Europe/London');
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />'); define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
/** Include PHPExcel */ /** Include PHPExcel */
require_once dirname(__FILE__) . '/../Classes/PHPExcel.php'; require_once dirname(__FILE__) . '/../src/Bootstrap.php';
// Create new PHPExcel object // Create new PHPExcel object
echo date('H:i:s') , " Create new PHPExcel object" , EOL; echo date('H:i:s') , " Create new PHPExcel object" , EOL;
$objPHPExcel = new PHPExcel(); $objPHPExcel = new \PHPExcel\Spreadsheet();
// Set document properties // Set document properties
echo date('H:i:s') , " Set document properties" , EOL; echo date('H:i:s') , " Set document properties" , EOL;
@ -55,7 +55,7 @@ $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
// Add some data // Add some data
echo date('H:i:s') , " Add some data" , EOL; 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> <h1 align="center">My very first example of rich text<br />generated from html markup</h1>
<p> <p>
<font size="14" COLOR="rgb(0,255,128)"> <font size="14" COLOR="rgb(0,255,128)">
@ -64,11 +64,11 @@ while this block uses an <u>underline</u>.
</font> </font>
</p> </p>
<p align="right"><font size="9" color="red"> <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> </font>
'; ';
$html2='<p> $html2 = '<p>
<font color="#ff0000"> <font color="#ff0000">
100&deg;C is a hot temperature 100&deg;C is a hot temperature
</font> </font>
@ -78,30 +78,38 @@ $html2='<p>
</font> </font>
</p>'; </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); $richText = $wizard->toRichTextObject($html1);
$objPHPExcel->setActiveSheetIndex(0) $objPHPExcel->getActiveSheet()
->setCellValue('A1', $richText); ->setCellValue('A1', $richText);
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(48); $objPHPExcel->getActiveSheet()
$objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1); ->getColumnDimension('A')
->setWidth(48);
$objPHPExcel->getActiveSheet()
->getRowDimension(1)
->setRowHeight(-1);
$objPHPExcel->getActiveSheet()->getStyle('A1') $objPHPExcel->getActiveSheet()->getStyle('A1')
->getAlignment() ->getAlignment()
->setWrapText(true); ->setWrapText(true);
$richText = $wizard->toRichTextObject($html2); $richText = $wizard->toRichTextObject($html2);
$objPHPExcel->setActiveSheetIndex(0) $objPHPExcel->getActiveSheet()
->setCellValue('A2', $richText); ->setCellValue('A2', $richText);
$objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1); $objPHPExcel->getActiveSheet()
$objPHPExcel->getActiveSheet()->getStyle('A2') ->getRowDimension(1)
->setRowHeight(-1);
$objPHPExcel->getActiveSheet()
->getStyle('A2')
->getAlignment() ->getAlignment()
->setWrapText(true); ->setWrapText(true);
@ -111,10 +119,14 @@ $objPHPExcel->setActiveSheetIndex(0)
$objPHPExcel->setActiveSheetIndex(0) $objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A4', $wizard->toRichTextObject($html4)); ->setCellValue('A4', $wizard->toRichTextObject($html4));
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A5', $wizard->toRichTextObject($html5));
// Rename worksheet // Rename worksheet
echo date('H:i:s') , " Rename worksheet" , EOL; 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 // Set active sheet index to the first sheet, so Excel opens this as the first sheet
@ -125,7 +137,7 @@ $objPHPExcel->setActiveSheetIndex(0);
echo date('H:i:s') , " Write to Excel2007 format" , EOL; echo date('H:i:s') , " Write to Excel2007 format" , EOL;
$callStartTime = microtime(true); $callStartTime = microtime(true);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter = \PHPExcel\IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__)); $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
$callEndTime = microtime(true); $callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime; $callTime = $callEndTime - $callStartTime;
@ -140,7 +152,7 @@ echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) / 1024
echo date('H:i:s') , " Write to Excel5 format" , EOL; echo date('H:i:s') , " Write to Excel5 format" , EOL;
$callStartTime = microtime(true); $callStartTime = microtime(true);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter = \PHPExcel\IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(str_replace('.php', '.xls', __FILE__)); $objWriter->save(str_replace('.php', '.xls', __FILE__));
$callEndTime = microtime(true); $callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime; $callTime = $callEndTime - $callStartTime;

View File

@ -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: (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: (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 - General: (umpirsky) Work Item GH-548 - Optimize vlookup() sort
2015-04-30 (v1.8.1): 2015-04-30 (v1.8.1):
- Bugfix: (goncons) Work Item GH-397 - Fix for Writing an Open Document cell with non-numeric formula - 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 - Bugfix: (sarciszewski) Work Item GH-329 - Avoid potential divide by zero in basedrawing

View File

@ -615,7 +615,7 @@ class HTML
$this->initialise(); $this->initialise();
// Create a new DOM object // Create a new DOM object
$dom = new domDocument; $dom = new \DOMDocument;
// Load the HTML file into the DOM object // 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 // Note the use of error suppression, because typically this will be an html fragment, so not fully valid markup
$loaded = @$dom->loadHTML($html); $loaded = @$dom->loadHTML($html);
@ -625,9 +625,27 @@ class HTML
$this->richTextObject = new \PHPExcel\RichText(); $this->richTextObject = new \PHPExcel\RichText();
$this->parseElements($dom); $this->parseElements($dom);
// Clean any further spurious whitespace
$this->cleanWhitespace();
return $this->richTextObject; 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() protected function buildTextRun()
{ {
$text = $this->stringData; $text = $this->stringData;
@ -767,12 +785,16 @@ class HTML
protected function breakTag() 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->stringData .= $domText;
$this->buildTextRun(); $this->buildTextRun();
} }
@ -787,7 +809,7 @@ class HTML
} }
} }
protected function parseElementNode(DOMElement $element) protected function parseElementNode(\DOMElement $element)
{ {
$callbackTag = strtolower($element->nodeName); $callbackTag = strtolower($element->nodeName);
$this->stack[] = $callbackTag; $this->stack[] = $callbackTag;
@ -795,18 +817,17 @@ class HTML
$this->handleCallback($element, $callbackTag, $this->startTagCallbacks); $this->handleCallback($element, $callbackTag, $this->startTagCallbacks);
$this->parseElements($element); $this->parseElements($element);
$this->stringData .= ' ';
array_pop($this->stack); array_pop($this->stack);
$this->handleCallback($element, $callbackTag, $this->endTagCallbacks); $this->handleCallback($element, $callbackTag, $this->endTagCallbacks);
} }
protected function parseElements(DOMNode $element) protected function parseElements(\DOMNode $element)
{ {
foreach ($element->childNodes as $child) { foreach ($element->childNodes as $child) {
if ($child instanceof DOMText) { if ($child instanceof \DOMText) {
$this->parseTextNode($child); $this->parseTextNode($child);
} elseif ($child instanceof DOMElement) { } elseif ($child instanceof \DOMElement) {
$this->parseElementNode($child); $this->parseElementNode($child);
} }
} }