Skip to content

Commit 07c9d9f

Browse files
committed
Add unit tests for Html parser
1 parent 379df3c commit 07c9d9f

File tree

4 files changed

+156
-27
lines changed

4 files changed

+156
-27
lines changed

src/PhpWord/Shared/Html.php

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace PhpOffice\PhpWord\Shared;
1919

2020
use PhpOffice\PhpWord\Element\AbstractContainer;
21+
use PhpOffice\PhpWord\SimpleType\Jc;
2122

2223
/**
2324
* Common Html functions
@@ -120,9 +121,10 @@ protected static function parseNode($node, $element, $styles = array(), $data =
120121
'b' => array('Property', null, null, $styles, null, 'bold', true),
121122
'em' => array('Property', null, null, $styles, null, 'italic', true),
122123
'i' => array('Property', null, null, $styles, null, 'italic', true),
124+
'u' => array('Property', null, null, $styles, null, 'underline', 'single'),
123125
'sup' => array('Property', null, null, $styles, null, 'superScript', true),
124126
'sub' => array('Property', null, null, $styles, null, 'subScript', true),
125-
'span' => array('Property', null, null, $styles, null, 'span', $node),
127+
'span' => array('Property', null, null, $styles, null, 'span', $node),
126128
'table' => array('Table', $node, $element, $styles, null, 'addTable', true),
127129
'tr' => array('Table', $node, $element, $styles, null, 'addRow', true),
128130
'td' => array('Table', $node, $element, $styles, null, 'addCell', true),
@@ -236,8 +238,6 @@ private static function parseText($node, $element, &$styles)
236238
// if (method_exists($element, 'addText')) {
237239
$element->addText($node->nodeValue, $styles['font'], $styles['paragraph']);
238240
// }
239-
240-
return null;
241241
}
242242

243243
/**
@@ -259,8 +259,6 @@ private static function parseProperty(&$styles, $argument1, $argument2)
259259
}
260260
}
261261
}
262-
263-
return null;
264262
}
265263

266264
/**
@@ -310,8 +308,6 @@ private static function parseList(&$styles, &$data, $argument1)
310308
$data['listdepth'] = 0;
311309
}
312310
$styles['list']['listType'] = $argument1;
313-
314-
return null;
315311
}
316312

317313
/**
@@ -337,8 +333,6 @@ private static function parseListItem($node, $element, &$styles, $data)
337333
}
338334
$element->addListItem($text, $data['listdepth'], $styles['font'], $styles['list'], $styles['paragraph']);
339335
}
340-
341-
return null;
342336
}
343337

344338
/**
@@ -366,28 +360,27 @@ private static function parseStyle($attribute, $styles)
366360
}
367361
break;
368362
case 'text-align':
369-
$styles['alignment'] = $cValue; // todo: any mapping?
363+
switch ($cValue) {
364+
case 'left':
365+
$styles['alignment'] = Jc::START;
366+
break;
367+
case 'right':
368+
$styles['alignment'] = Jc::END;
369+
break;
370+
case 'center':
371+
$styles['alignment'] = Jc::CENTER;
372+
break;
373+
case 'justify':
374+
$styles['alignment'] = Jc::BOTH;
375+
break;
376+
}
370377
break;
371378
case 'color':
372379
$styles['color'] = trim($cValue, '#');
373380
break;
374381
case 'background-color':
375382
$styles['bgColor'] = trim($cValue, '#');
376383
break;
377-
case 'font-weight':
378-
$tValue = false;
379-
if (preg_match('#bold#', $cValue)) {
380-
$tValue = true; // also match bolder
381-
}
382-
$styles['bold'] = $tValue;
383-
break;
384-
case 'font-style':
385-
$tValue = false;
386-
if (preg_match('#(?:italic|oblique)#', $cValue)) {
387-
$tValue = true;
388-
}
389-
$styles['italic'] = $tValue;
390-
break;
391384
}
392385
}
393386

tests/PhpWord/Reader/MsDocTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,24 @@ public function testLoad()
5656
$phpWord = IOFactory::load($filename, 'MsDoc');
5757
$this->assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $phpWord);
5858
}
59+
60+
/**
61+
* Test exception on not existing file
62+
* @expectedException \Exception
63+
*/
64+
public function testFailIfFileNotReadable()
65+
{
66+
$filename = __DIR__ . '/../_files/documents/not_existing_reader.doc';
67+
IOFactory::load($filename, 'MsDoc');
68+
}
69+
70+
/**
71+
* Test exception on non OLE document
72+
* @expectedException \Exception
73+
*/
74+
public function testFailIfFileNotOle()
75+
{
76+
$filename = __DIR__ . '/../_files/documents/reader.odt';
77+
IOFactory::load($filename, 'MsDoc');
78+
}
5979
}

tests/PhpWord/Shared/HtmlTest.php

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818
namespace PhpOffice\PhpWord\Shared;
1919

2020
use PhpOffice\PhpWord\Element\Section;
21+
use PhpOffice\PhpWord\SimpleType\Jc;
22+
use PhpOffice\PhpWord\TestHelperDOCX;
2123

2224
/**
2325
* Test class for PhpOffice\PhpWord\Shared\Html
26+
* @coversDefaultClass \PhpOffice\PhpWord\Shared\Html
2427
*/
2528
class HtmlTest extends \PHPUnit\Framework\TestCase
2629
{
@@ -43,7 +46,7 @@ public function testAddHtml()
4346

4447
// Styles
4548
$content .= '<p style="text-decoration: underline; text-decoration: line-through; '
46-
. 'text-align: center; color: #999; background-color: #000;">';
49+
. 'text-align: center; color: #999; background-color: #000; font-weight: bold; font-style: italic;">';
4750
foreach ($styles as $style) {
4851
$content .= "<{$style}>{$style}</{$style}>";
4952
}
@@ -67,4 +70,109 @@ public function testAddHtml()
6770
$content .= '&ndash;&nbsp;&emsp;&ensp;&sup2;&sup3;&frac14;&frac12;&frac34;';
6871
Html::addHtml($section, $content);
6972
}
73+
74+
/**
75+
* Test that html already in body element can be read
76+
* @ignore
77+
*/
78+
public function testParseFullHtml()
79+
{
80+
$section = new Section(1);
81+
Html::addHtml($section, '<body><p>test paragraph1</p><p>test paragraph2</p></body>', true);
82+
83+
$this->assertCount(2, $section->getElements());
84+
}
85+
86+
/**
87+
* Test underline
88+
*/
89+
public function testParseUnderline()
90+
{
91+
$html = '<u>test</u>';
92+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
93+
$section = $phpWord->addSection();
94+
Html::addHtml($section, $html);
95+
96+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
97+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:u'));
98+
$this->assertEquals('single', $doc->getElementAttribute('/w:document/w:body/w:p/w:r/w:rPr/w:u', 'w:val'));
99+
}
100+
101+
/**
102+
* Test text-decoration style
103+
*/
104+
public function testParseTextDecoration()
105+
{
106+
$html = '<span style="text-decoration: underline;">test</span>';
107+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
108+
$section = $phpWord->addSection();
109+
Html::addHtml($section, $html);
110+
111+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
112+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:u'));
113+
$this->assertEquals('single', $doc->getElementAttribute('/w:document/w:body/w:p/w:r/w:rPr/w:u', 'w:val'));
114+
}
115+
116+
/**
117+
* Test text-align style
118+
*/
119+
public function testParseTextAlign()
120+
{
121+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
122+
$section = $phpWord->addSection();
123+
Html::addHtml($section, '<p style="text-align: left;">test</p>');
124+
Html::addHtml($section, '<p style="text-align: right;">test</p>');
125+
Html::addHtml($section, '<p style="text-align: center;">test</p>');
126+
Html::addHtml($section, '<p style="text-align: justify;">test</p>');
127+
128+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
129+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:pPr/w:jc'));
130+
$this->assertEquals('start', $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:pPr/w:jc', 'w:val'));
131+
$this->assertEquals('end', $doc->getElementAttribute('/w:document/w:body/w:p[2]/w:pPr/w:jc', 'w:val'));
132+
$this->assertEquals('center', $doc->getElementAttribute('/w:document/w:body/w:p[3]/w:pPr/w:jc', 'w:val'));
133+
$this->assertEquals('both', $doc->getElementAttribute('/w:document/w:body/w:p[4]/w:pPr/w:jc', 'w:val'));
134+
}
135+
136+
/**
137+
* Test parsing paragraph and span styles
138+
*/
139+
public function testParseParagraphAndSpanStyle()
140+
{
141+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
142+
$section = $phpWord->addSection();
143+
Html::addHtml($section, '<p style="text-align: center;"><span style="text-decoration: underline;">test</span></p>');
144+
145+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
146+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:pPr/w:jc'));
147+
$this->assertEquals('center', $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:pPr/w:jc', 'w:val'));
148+
$this->assertEquals('single', $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:r/w:rPr/w:u', 'w:val'));
149+
}
150+
151+
/**
152+
* Test parsing table
153+
*/
154+
public function testParseTable()
155+
{
156+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
157+
$section = $phpWord->addSection();
158+
$html = '
159+
<table style="width: 50%; border: 6px #0000FF solid;">
160+
<thead>
161+
<tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; ">
162+
<th>a</th>
163+
<th>b</th>
164+
<th>c</th>
165+
</tr>
166+
</thead>
167+
<tbody>
168+
<tr><td>1</td><td colspan="2">2</td></tr>
169+
<tr><td>4</td><td>5</td><td>6</td></tr>
170+
</tbody>
171+
</table>';
172+
Html::addHtml($section, $html);
173+
174+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
175+
// echo $doc->printXml();
176+
// $this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr/w:tc'));
177+
}
70178
}

tests/PhpWord/_includes/XmlDocument.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,20 @@ public function elementExists($path, $file = 'word/document.xml')
170170
* @param string $file
171171
* @return string
172172
*/
173-
public function printXml($path = '/w:document', $file = 'word/document.xml')
173+
public function printXml($path = '/', $file = 'word/document.xml')
174174
{
175+
$element = $this->getElement($path, $file);
176+
if ($element instanceof \DOMDocument) {
177+
$element->formatOutput = true;
178+
$element->preserveWhiteSpace = false;
179+
180+
return $element->saveXML();
181+
}
182+
175183
$newdoc = new \DOMDocument();
176184
$newdoc->formatOutput = true;
177185
$newdoc->preserveWhiteSpace = false;
178-
$node = $newdoc->importNode($this->getElement($path, $file), true);
186+
$node = $newdoc->importNode($element, true);
179187
$newdoc->appendChild($node);
180188

181189
return $newdoc->saveXML($node);

0 commit comments

Comments
 (0)