Skip to content

Commit 6db2927

Browse files
authored
Merge pull request #1965 from naept/feature_handle-style-inheritance-for-html
feat: Update addHtml to handle style inheritance
2 parents ca45d14 + b86c606 commit 6db2927

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

src/PhpWord/Shared/Html.php

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ protected static function parseCell($node, $element, &$styles)
401401
$cell = $element->addCell($width, $cellStyles);
402402

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

407407
return $cell;
@@ -432,15 +432,51 @@ protected static function shouldAddTextRun(\DOMNode $node)
432432
*/
433433
protected static function recursiveParseStylesInHierarchy(\DOMNode $node, array $style)
434434
{
435-
$parentStyle = self::parseInlineStyle($node, array());
436-
$style = array_merge($parentStyle, $style);
435+
$parentStyle = array();
437436
if ($node->parentNode != null && XML_ELEMENT_NODE == $node->parentNode->nodeType) {
438-
$style = self::recursiveParseStylesInHierarchy($node->parentNode, $style);
437+
$parentStyle = self::recursiveParseStylesInHierarchy($node->parentNode, array());
439438
}
439+
if ($node->nodeName === '#text') {
440+
$parentStyle = array_merge($parentStyle, $style);
441+
} else {
442+
$parentStyle = self::filterOutNonInheritedStyles($parentStyle);
443+
}
444+
$style = self::parseInlineStyle($node, $parentStyle);
440445

441446
return $style;
442447
}
443448

449+
/**
450+
* Removes non-inherited styles from array
451+
*
452+
* @param array &$styles
453+
*/
454+
protected static function filterOutNonInheritedStyles(array $styles)
455+
{
456+
$nonInheritedStyles = array(
457+
'borderSize',
458+
'borderTopSize',
459+
'borderRightSize',
460+
'borderBottomSize',
461+
'borderLeftSize',
462+
'borderColor',
463+
'borderTopColor',
464+
'borderRightColor',
465+
'borderBottomColor',
466+
'borderLeftColor',
467+
'borderStyle',
468+
'spaceAfter',
469+
'spaceBefore',
470+
'underline',
471+
'strikethrough',
472+
'hidden',
473+
);
474+
475+
$styles = array_diff_key($styles, array_flip($nonInheritedStyles));
476+
477+
return $styles;
478+
}
479+
444480
/**
445481
* Parse list node
446482
*

tests/PhpWord/Shared/HtmlTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,11 @@ public function testParseTable()
293293
{
294294
$phpWord = new \PhpOffice\PhpWord\PhpWord();
295295
$section = $phpWord->addSection();
296-
$html = '<table align="left" style="width: 50%; border: 6px #0000FF solid;">
296+
$html = '<table align="left" style="width: 50%; border: 12px #0000FF double">
297297
<thead>
298-
<tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; ">
299-
<th style="width: 50pt">header a</th>
300-
<th style="width: 50; border-color: #00EE00">header b</th>
298+
<tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold">
299+
<th style="width: 50pt"><p>header a</p></th>
300+
<th style="width: 50; border-color: #00EE00; border-width: 3px"><span>header b</span></th>
301301
<th style="border-color: #00AA00 #00BB00 #00CC00 #00DD00; border-width: 3px">header c</th>
302302
</tr>
303303
</thead>
@@ -324,6 +324,12 @@ public function testParseTable()
324324
$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'));
325325
$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'));
326326
$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'));
327+
328+
//check borders are not propagated inside cells
329+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc[1]/w:p'));
330+
$this->assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc[1]/w:p/w:pPr/w:pBdr'));
331+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]/w:p'));
332+
$this->assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]/w:p/w:pPr/w:pBdr'));
327333
}
328334

329335
/**

0 commit comments

Comments
 (0)