Skip to content

fix parsing of border-color and add test #1570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ v0.17.0 (?? ??? 2019)
----------------------
### Added
- Add RightToLeft table presentation. @troosan #1550
- Set complex type in template @troosan #1565
- Add support for page vertical alignment. @troosan #672 #1569

### Fixed
- Fix HTML border-color parsing. @troosan #1551 #1570

### Miscelaneous
- Use embedded http server to test loading of remote images @troosan #
Expand Down
2 changes: 1 addition & 1 deletion samples/Sample_26_Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
</tr>
</thead>
<tbody>
<tr><td style="border-style: dotted;">1</td><td colspan="2">2</td></tr>
<tr><td style="border-style: dotted; border-color: #FF0000">1</td><td colspan="2">2</td></tr>
<tr><td>This is <b>bold</b> text</td><td></td><td>6</td></tr>
</tbody>
</table>';
Expand Down
18 changes: 16 additions & 2 deletions src/PhpWord/Shared/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ private static function parseStyle($attribute, $styles)
$styles['alignment'] = self::mapAlign($cValue);
break;
case 'display':
$styles['hidden'] = $cValue === 'none';
$styles['hidden'] = $cValue === 'none' || $cValue === 'hidden';
break;
case 'direction':
$styles['rtl'] = $cValue === 'rtl';
Expand Down Expand Up @@ -581,7 +581,7 @@ private static function parseStyle($attribute, $styles)
$styles['spaceAfter'] = Converter::cssToPoint($cValue);
break;
case 'border-color':
$styles['color'] = trim($cValue, '#');
self::mapBorderColor($styles, $cValue);
break;
case 'border-width':
$styles['borderSize'] = Converter::cssToPoint($cValue);
Expand Down Expand Up @@ -738,6 +738,20 @@ private static function mapBorderStyle($cssBorderStyle)
}
}

private static function mapBorderColor(&$styles, $cssBorderColor)
{
$numColors = substr_count($cssBorderColor, '#');
if ($numColors === 1) {
$styles['borderColor'] = trim($cssBorderColor, '#');
} elseif ($numColors > 1) {
$colors = explode(' ', $cssBorderColor);
$borders = array('borderTopColor', 'borderRightColor', 'borderBottomColor', 'borderLeftColor');
for ($i = 0; $i < min(4, $numColors, count($colors)); $i++) {
$styles[$borders[$i]] = trim($colors[$i], '#');
}
}
}

/**
* Transforms a HTML/CSS alignment into a \PhpOffice\PhpWord\SimpleType\Jc
*
Expand Down
46 changes: 44 additions & 2 deletions tests/PhpWord/Shared/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ public function testParseTable()
<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">header b</th>
<th style="border-color: #00FF00; border-width: 3px">header c</th>
<th style="width: 50; border-color: #00EE00">header b</th>
<th style="border-color: #00AA00 #00BB00 #00CC00 #00DD00; border-width: 3px">header c</th>
</tr>
</thead>
<tbody>
Expand All @@ -313,6 +313,17 @@ public function testParseTable()
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr/w:tc'));
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tblPr/w:jc'));
$this->assertEquals(Jc::START, $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tblPr/w:jc', 'w:val'));

//check border colors
$this->assertEquals('00EE00', $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]/w:tcPr/w:tcBorders/w:top', 'w:color'));
$this->assertEquals('00EE00', $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]/w:tcPr/w:tcBorders/w:right', 'w:color'));
$this->assertEquals('00EE00', $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]/w:tcPr/w:tcBorders/w:bottom', 'w:color'));
$this->assertEquals('00EE00', $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]/w:tcPr/w:tcBorders/w:left', 'w:color'));

$this->assertEquals('00AA00', $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr[1]/w:tc[3]/w:tcPr/w:tcBorders/w:top', 'w:color'));
$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'));
}

/**
Expand Down Expand Up @@ -590,4 +601,35 @@ public function testParseMalformedStyleIsIgnored()
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertFalse($doc->elementExists('/w:document/w:body/w:p[1]/w:pPr/w:jc'));
}

/**
* Tests parsing hidden text
*/
public function testParseHiddenText()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = '<p style="display: hidden">This is some hidden text.</p>';
Html::addHtml($section, $html);

$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');

$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:vanish'));
}

/**
* Tests parsing letter spacing
*/
public function testParseLetterSpacing()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = '<p style="letter-spacing: 150px">This is some text with letter spacing.</p>';
Html::addHtml($section, $html);

$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');

$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:spacing'));
$this->assertEquals(150 * 15, $doc->getElement('/w:document/w:body/w:p/w:r/w:rPr/w:spacing')->getAttribute('w:val'));
}
}