Skip to content

Commit 3219950

Browse files
committed
trim color codes and add tests
1 parent 5206c7f commit 3219950

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

src/PhpWord/Shared/Html.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ private static function parseStyle($attribute, $styles)
516516
$styles['alignment'] = self::mapAlign($cValue);
517517
break;
518518
case 'display':
519-
$styles['hidden'] = $cValue === 'none';
519+
$styles['hidden'] = $cValue === 'none' || $cValue === 'hidden';
520520
break;
521521
case 'direction':
522522
$styles['rtl'] = $cValue === 'rtl';
@@ -747,7 +747,7 @@ private static function mapBorderColor(&$styles, $cssBorderColor)
747747
$colors = explode(' ', $cssBorderColor);
748748
$borders = array('borderTopColor', 'borderRightColor', 'borderBottomColor', 'borderLeftColor');
749749
for ($i = 0; $i < min(4, $numColors, count($colors)); $i++) {
750-
$styles[$borders[$i]] = $colors[$i];
750+
$styles[$borders[$i]] = trim($colors[$i], '#');
751751
}
752752
}
753753
}

tests/PhpWord/Shared/HtmlTest.php

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ public function testParseTable()
297297
<thead>
298298
<tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; ">
299299
<th style="width: 50pt">header a</th>
300-
<th style="width: 50">header b</th>
300+
<th style="width: 50; border-color: #00EE00">header b</th>
301301
<th style="border-color: #00AA00 #00BB00 #00CC00 #00DD00; border-width: 3px">header c</th>
302302
</tr>
303303
</thead>
@@ -313,11 +313,17 @@ public function testParseTable()
313313
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr/w:tc'));
314314
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tblPr/w:jc'));
315315
$this->assertEquals(Jc::START, $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tblPr/w:jc', 'w:val'));
316+
316317
//check border colors
317-
$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'));
318-
$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'));
319-
$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'));
320-
$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'));
318+
$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'));
319+
$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'));
320+
$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'));
321+
$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'));
322+
323+
$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'));
324+
$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'));
325+
$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'));
326+
$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'));
321327
}
322328

323329
/**
@@ -595,4 +601,35 @@ public function testParseMalformedStyleIsIgnored()
595601
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
596602
$this->assertFalse($doc->elementExists('/w:document/w:body/w:p[1]/w:pPr/w:jc'));
597603
}
604+
605+
/**
606+
* Tests parsing hidden text
607+
*/
608+
public function testParseHiddenText()
609+
{
610+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
611+
$section = $phpWord->addSection();
612+
$html = '<p style="display: hidden">This is some hidden text.</p>';
613+
Html::addHtml($section, $html);
614+
615+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
616+
617+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:vanish'));
618+
}
619+
620+
/**
621+
* Tests parsing letter spacing
622+
*/
623+
public function testParseLetterSpacing()
624+
{
625+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
626+
$section = $phpWord->addSection();
627+
$html = '<p style="letter-spacing: 150px">This is some text with letter spacing.</p>';
628+
Html::addHtml($section, $html);
629+
630+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
631+
632+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:spacing'));
633+
$this->assertEquals(150 * 15, $doc->getElement('/w:document/w:body/w:p/w:r/w:rPr/w:spacing')->getAttribute('w:val'));
634+
}
598635
}

0 commit comments

Comments
 (0)