Skip to content

Commit

Permalink
PHPOfficeGH-554 - Whitespace after toRichTextObject() - abide by codi…
Browse files Browse the repository at this point in the history
…ng standards
  • Loading branch information
MarkBaker committed Jul 12, 2015
1 parent 879f86c commit 78378f1
Showing 1 changed file with 72 additions and 43 deletions.
115 changes: 72 additions & 43 deletions Classes/PHPExcel/Helper/HTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class PHPExcel_Helper_HTML
{
protected static $colourMap = array(
'aliceblue' => 'f0f8ff',
'aliceblue' => 'f0f8ff',
'antiquewhite' => 'faebd7',
'antiquewhite1' => 'ffefdb',
'antiquewhite2' => 'eedfcc',
Expand Down Expand Up @@ -526,12 +526,12 @@ class PHPExcel_Helper_HTML
protected $size;
protected $color;

protected $bold = false;
protected $italic = false;
protected $underline = false;
protected $superscript = false;
protected $subscript = false;
protected $strikethrough = false;
protected $bold = false;
protected $italic = false;
protected $underline = false;
protected $superscript = false;
protected $subscript = false;
protected $strikethrough = false;

protected $startTagCallbacks = array(
'font' => 'startFontTag',
Expand Down Expand Up @@ -573,7 +573,8 @@ class PHPExcel_Helper_HTML

protected $richTextObject;

protected function initialise() {
protected function initialise()
{
$this->face = $this->size = $this->color = null;
$this->bold = $this->italic = $this->underline = $this->superscript = $this->subscript = $this->strikethrough = false;

Expand All @@ -582,16 +583,17 @@ protected function initialise() {
$this->stringData = '';
}

public function toRichTextObject($html) {
public function toRichTextObject($html)
{
$this->initialise();

// Create a new DOM object
// Create a new DOM object
$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
$loaded = @$dom->loadHTML($html);

// Discard excess white space
// Discard excess white space
$dom->preserveWhiteSpace = false;

$this->richTextObject = new PHPExcel_RichText();;
Expand All @@ -603,8 +605,9 @@ public function toRichTextObject($html) {
return $this->richTextObject;
}

protected function cleanWhitespace() {
foreach($this->richTextObject->getRichTextElements() as $key => $element) {
protected function cleanWhitespace()
{
foreach ($this->richTextObject->getRichTextElements() as $key => $element) {
$text = $element->getText();
// Trim any leading spaces on the first run
if ($key == 0) {
Expand All @@ -615,11 +618,13 @@ protected function cleanWhitespace() {
$element->setText($text);
}
}

protected function buildTextRun() {

protected function buildTextRun()
{
$text = $this->stringData;
if (trim($text) === '')
if (trim($text) === '') {
return;
}

$richtextRun = $this->richTextObject->createTextRun($this->stringData);
if ($this->face) {
Expand All @@ -629,7 +634,7 @@ protected function buildTextRun() {
$richtextRun->getFont()->setSize($this->size);
}
if ($this->color) {
$richtextRun->getFont()->setColor( new PHPExcel_Style_Color( 'ff' . $this->color ) );
$richtextRun->getFont()->setColor(new PHPExcel_Style_Color('ff' . $this->color));
}
if ($this->bold) {
$richtextRun->getFont()->setBold(true);
Expand All @@ -652,27 +657,30 @@ protected function buildTextRun() {
$this->stringData = '';
}

protected function rgbToColour($rgb) {
protected function rgbToColour($rgb)
{
preg_match_all('/\d+/', $rgb, $values);
foreach($values[0] as &$value) {
foreach ($values[0] as &$value) {
$value = str_pad(dechex($value), 2, '0', STR_PAD_LEFT);
}
return implode($values[0]);
}

protected function colourNameLookup($rgb) {
protected function colourNameLookup($rgb)
{
return self::$colourMap[$rgb];
}

protected function startFontTag($tag) {
protected function startFontTag($tag)
{
foreach ($tag->attributes as $attribute) {
$attributeName = strtolower($attribute->name);
$attributeValue = $attribute->value;

if ($attributeName == 'color') {
if (preg_match('/rgb\s*\(/', $attributeValue)) {
$this->$attributeName = $this->rgbToColour($attributeValue);
} elseif(strpos(trim($attributeValue), '#') === 0) {
} elseif (strpos(trim($attributeValue), '#') === 0) {
$this->$attributeName = ltrim($attributeValue, '#');
} else {
$this->$attributeName = $this->colourNameLookup($attributeValue);
Expand All @@ -683,69 +691,89 @@ protected function startFontTag($tag) {
}
}

protected function endFontTag() {
protected function endFontTag()
{
$this->face = $this->size = $this->color = null;
}

protected function startBoldTag() {
protected function startBoldTag()
{
$this->bold = true;
}

protected function endBoldTag() {
protected function endBoldTag()
{
$this->bold = false;
}

protected function startItalicTag() {
protected function startItalicTag()
{
$this->italic = true;
}

protected function endItalicTag() {
protected function endItalicTag()
{
$this->italic = false;
}

protected function startUnderlineTag() {
protected function startUnderlineTag()
{
$this->underline = true;
}

protected function endUnderlineTag() {
protected function endUnderlineTag()
{
$this->underline = false;
}

protected function startSubscriptTag() {
protected function startSubscriptTag()
{
$this->subscript = true;
}

protected function endSubscriptTag() {
protected function endSubscriptTag()
{
$this->subscript = false;
}

protected function startSuperscriptTag() {
protected function startSuperscriptTag()
{
$this->superscript = true;
}

protected function endSuperscriptTag() {
protected function endSuperscriptTag()
{
$this->superscript = false;
}

protected function startStrikethruTag() {
protected function startStrikethruTag()
{
$this->strikethrough = true;
}

protected function endStrikethruTag() {
protected function endStrikethruTag()
{
$this->strikethrough = false;
}

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

protected function parseTextNode(DOMText $textNode) {
$domText = preg_replace('/\s+/u', ' ', str_replace(["\r", "\n"], ' ', $textNode->nodeValue));
protected function parseTextNode(DOMText $textNode)
{
$domText = preg_replace(
'/\s+/u',
' ',
str_replace(["\r", "\n"], ' ', $textNode->nodeValue)
);
$this->stringData .= $domText;
$this->buildTextRun();
}

protected function handleCallback($element, $callbackTag, $callbacks) {
protected function handleCallback($element, $callbackTag, $callbacks)
{
if (isset($callbacks[$callbackTag])) {
$elementHandler = $callbacks[$callbackTag];
if (method_exists($this, $elementHandler)) {
Expand All @@ -754,20 +782,21 @@ 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) {
$this->parseTextNode($child);
Expand Down

0 comments on commit 78378f1

Please sign in to comment.