Skip to content

Commit

Permalink
Merge pull request PHPOffice#1965 from naept/feature_handle-style-inh…
Browse files Browse the repository at this point in the history
…eritance-for-html

feat: Update addHtml to handle style inheritance
  • Loading branch information
troosan committed Feb 7, 2021
2 parents ca45d14 + b86c606 commit 6db2927
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
44 changes: 40 additions & 4 deletions src/PhpWord/Shared/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ protected static function parseCell($node, $element, &$styles)
$cell = $element->addCell($width, $cellStyles);

if (self::shouldAddTextRun($node)) {
return $cell->addTextRun(self::parseInlineStyle($node, $styles['paragraph']));
return $cell->addTextRun(self::filterOutNonInheritedStyles(self::parseInlineStyle($node, $styles['paragraph'])));
}

return $cell;
Expand Down Expand Up @@ -432,15 +432,51 @@ protected static function shouldAddTextRun(\DOMNode $node)
*/
protected static function recursiveParseStylesInHierarchy(\DOMNode $node, array $style)
{
$parentStyle = self::parseInlineStyle($node, array());
$style = array_merge($parentStyle, $style);
$parentStyle = array();
if ($node->parentNode != null && XML_ELEMENT_NODE == $node->parentNode->nodeType) {
$style = self::recursiveParseStylesInHierarchy($node->parentNode, $style);
$parentStyle = self::recursiveParseStylesInHierarchy($node->parentNode, array());
}
if ($node->nodeName === '#text') {
$parentStyle = array_merge($parentStyle, $style);
} else {
$parentStyle = self::filterOutNonInheritedStyles($parentStyle);
}
$style = self::parseInlineStyle($node, $parentStyle);

return $style;
}

/**
* Removes non-inherited styles from array
*
* @param array &$styles
*/
protected static function filterOutNonInheritedStyles(array $styles)
{
$nonInheritedStyles = array(
'borderSize',
'borderTopSize',
'borderRightSize',
'borderBottomSize',
'borderLeftSize',
'borderColor',
'borderTopColor',
'borderRightColor',
'borderBottomColor',
'borderLeftColor',
'borderStyle',
'spaceAfter',
'spaceBefore',
'underline',
'strikethrough',
'hidden',
);

$styles = array_diff_key($styles, array_flip($nonInheritedStyles));

return $styles;
}

/**
* Parse list node
*
Expand Down
14 changes: 10 additions & 4 deletions tests/PhpWord/Shared/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,11 @@ public function testParseTable()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = '<table align="left" style="width: 50%; border: 6px #0000FF solid;">
$html = '<table align="left" style="width: 50%; border: 12px #0000FF double">
<thead>
<tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; ">
<th style="width: 50pt">header a</th>
<th style="width: 50; border-color: #00EE00">header b</th>
<tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold">
<th style="width: 50pt"><p>header a</p></th>
<th style="width: 50; border-color: #00EE00; border-width: 3px"><span>header b</span></th>
<th style="border-color: #00AA00 #00BB00 #00CC00 #00DD00; border-width: 3px">header c</th>
</tr>
</thead>
Expand All @@ -324,6 +324,12 @@ public function testParseTable()
$this->assertEquals('00BB00', $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr[1]/w:tc[3]/w:tcPr/w:tcBorders/w:right', 'w:color'));
$this->assertEquals('00CC00', $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr[1]/w:tc[3]/w:tcPr/w:tcBorders/w:bottom', 'w:color'));
$this->assertEquals('00DD00', $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr[1]/w:tc[3]/w:tcPr/w:tcBorders/w:left', 'w:color'));

//check borders are not propagated inside cells
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc[1]/w:p'));
$this->assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc[1]/w:p/w:pPr/w:pBdr'));
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]/w:p'));
$this->assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]/w:p/w:pPr/w:pBdr'));
}

/**
Expand Down

0 comments on commit 6db2927

Please sign in to comment.