2525use PhpOffice \PhpWord \Shared \Html ;
2626use PhpOffice \PhpWord \SimpleType \Jc ;
2727use PhpOffice \PhpWord \SimpleType \LineSpacingRule ;
28+ use PhpOffice \PhpWord \SimpleType \TblWidth ;
2829use PhpOffice \PhpWord \Style \Paragraph ;
2930use PhpOffice \PhpWordTests \AbstractWebServerEmbedded ;
3031use PhpOffice \PhpWordTests \TestHelperDOCX ;
@@ -156,11 +157,11 @@ public function testParseStyleTableClassName(): void
156157 }
157158
158159 /**
159- * Test underline .
160+ * Test text-decoration style .
160161 */
161- public function testParseUnderline (): void
162+ public function testParseTextDecoration (): void
162163 {
163- $ html = '<u >test</u > ' ;
164+ $ html = '<span style="text-decoration: underline;" >test</span > ' ;
164165 $ phpWord = new PhpWord ();
165166 $ section = $ phpWord ->addSection ();
166167 Html::addHtml ($ section , $ html );
@@ -171,11 +172,11 @@ public function testParseUnderline(): void
171172 }
172173
173174 /**
174- * Test text-decoration style .
175+ * Test underline .
175176 */
176- public function testParseTextDecoration (): void
177+ public function testParseUnderline (): void
177178 {
178- $ html = '<span style="text-decoration: underline;" >test</span > ' ;
179+ $ html = '<u >test</u > ' ;
179180 $ phpWord = new PhpWord ();
180181 $ section = $ phpWord ->addSection ();
181182 Html::addHtml ($ section , $ html );
@@ -185,6 +186,25 @@ public function testParseTextDecoration(): void
185186 self ::assertEquals ('single ' , $ doc ->getElementAttribute ('/w:document/w:body/w:p/w:r/w:rPr/w:u ' , 'w:val ' ));
186187 }
187188
189+ /**
190+ * Test width.
191+ *
192+ * @dataProvider providerParseWidth
193+ */
194+ public function testParseWidth (string $ htmlSize , float $ docxSize , string $ docxUnit ): void
195+ {
196+ $ html = '<table width=" ' . $ htmlSize . '"><tr><td>A</td></tr></table> ' ;
197+ $ phpWord = new PhpWord ();
198+ $ section = $ phpWord ->addSection ();
199+
200+ Html::addHtml ($ section , $ html );
201+ $ doc = TestHelperDOCX::getDocument ($ phpWord , 'Word2007 ' );
202+ $ xpath = '/w:document/w:body/w:tbl/w:tblPr/w:tblW ' ;
203+ self ::assertTrue ($ doc ->elementExists ($ xpath ));
204+ self ::assertEquals ($ docxSize , $ doc ->getElement ($ xpath )->getAttribute ('w:w ' ));
205+ self ::assertEquals ($ docxUnit , $ doc ->getElement ($ xpath )->getAttribute ('w:type ' ));
206+ }
207+
188208 /**
189209 * Test font-variant style.
190210 */
@@ -461,31 +481,31 @@ public function testParseTableAndCellWidth(): void
461481 $ xpath = '/w:document/w:body/w:tbl/w:tblGrid/w:gridCol ' ;
462482 self ::assertTrue ($ doc ->elementExists ($ xpath ));
463483 self ::assertEquals (25 * 50 , $ doc ->getElement ($ xpath )->getAttribute ('w:w ' ));
464- self ::assertEquals (' dxa ' , $ doc ->getElement ($ xpath )->getAttribute ('w:type ' ));
484+ self ::assertEquals (TblWidth:: TWIP , $ doc ->getElement ($ xpath )->getAttribute ('w:type ' ));
465485
466486 // <td style="width: 25%; ...
467487 $ xpath = '/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:tcW ' ;
468488 self ::assertTrue ($ doc ->elementExists ($ xpath ));
469489 self ::assertEquals (25 * 50 , $ doc ->getElement ($ xpath )->getAttribute ('w:w ' ));
470- self ::assertEquals (' pct ' , $ doc ->getElement ($ xpath )->getAttribute ('w:type ' ));
490+ self ::assertEquals (TblWidth:: PERCENT , $ doc ->getElement ($ xpath )->getAttribute ('w:type ' ));
471491
472492 // <table width="400" .. 400px = 6000 twips (400 / 96 * 1440)
473493 $ xpath = '/w:document/w:body/w:tbl/w:tr/w:tc/w:tbl/w:tr/w:tc/w:tcPr/w:tcW ' ;
474494 self ::assertTrue ($ doc ->elementExists ($ xpath ));
475495 self ::assertEquals (6000 , $ doc ->getElement ($ xpath )->getAttribute ('w:w ' ));
476- self ::assertEquals (' dxa ' , $ doc ->getElement ($ xpath )->getAttribute ('w:type ' ));
496+ self ::assertEquals (TblWidth:: TWIP , $ doc ->getElement ($ xpath )->getAttribute ('w:type ' ));
477497
478498 // <th style="width: 50pt; .. 50pt = 750 twips (50 / 72 * 1440)
479499 $ xpath = '/w:document/w:body/w:tbl/w:tr/w:tc/w:tbl/w:tr[2]/w:tc[2]/w:tcPr/w:tcW ' ;
480500 self ::assertTrue ($ doc ->elementExists ($ xpath ));
481501 self ::assertEquals (1000 , $ doc ->getElement ($ xpath )->getAttribute ('w:w ' ));
482- self ::assertEquals (' dxa ' , $ doc ->getElement ($ xpath )->getAttribute ('w:type ' ));
502+ self ::assertEquals (TblWidth:: TWIP , $ doc ->getElement ($ xpath )->getAttribute ('w:type ' ));
483503
484504 // <th width="300" .. 300px = 4500 twips (300 / 96 * 1440)
485505 $ xpath = '/w:document/w:body/w:tbl/w:tr/w:tc/w:tbl/w:tr[3]/w:tc/w:tcPr/w:tcW ' ;
486506 self ::assertTrue ($ doc ->elementExists ($ xpath ));
487507 self ::assertEquals (4500 , $ doc ->getElement ($ xpath )->getAttribute ('w:w ' ));
488- self ::assertEquals (' dxa ' , $ doc ->getElement ($ xpath )->getAttribute ('w:type ' ));
508+ self ::assertEquals (TblWidth:: TWIP , $ doc ->getElement ($ xpath )->getAttribute ('w:type ' ));
489509 }
490510
491511 /**
@@ -599,7 +619,7 @@ public function testParseTableCellspacingRowBgColor(): void
599619 $ xpath = '/w:document/w:body/w:tbl/w:tblPr/w:tblCellSpacing ' ;
600620 self ::assertTrue ($ doc ->elementExists ($ xpath ));
601621 self ::assertEquals (3 * 15 , $ doc ->getElement ($ xpath )->getAttribute ('w:w ' ));
602- self ::assertEquals (' dxa ' , $ doc ->getElement ($ xpath )->getAttribute ('w:type ' ));
622+ self ::assertEquals (TblWidth:: TWIP , $ doc ->getElement ($ xpath )->getAttribute ('w:type ' ));
603623
604624 $ xpath = '/w:document/w:body/w:tbl/w:tr[1]/w:tc[1]/w:tcPr/w:shd ' ;
605625 self ::assertTrue ($ doc ->elementExists ($ xpath ));
@@ -632,7 +652,7 @@ public function testParseTableStyleAttributeInlineStyle(): void
632652 $ xpath = '/w:document/w:body/w:tbl/w:tblPr/w:tblW ' ;
633653 self ::assertTrue ($ doc ->elementExists ($ xpath ));
634654 self ::assertEquals (100 * 50 , $ doc ->getElement ($ xpath )->getAttribute ('w:w ' ));
635- self ::assertEquals (' pct ' , $ doc ->getElement ($ xpath )->getAttribute ('w:type ' ));
655+ self ::assertEquals (TblWidth:: PERCENT , $ doc ->getElement ($ xpath )->getAttribute ('w:type ' ));
636656
637657 $ xpath = '/w:document/w:body/w:tbl/w:tr[1]/w:tc[1]/w:tcPr/w:shd ' ;
638658 self ::assertTrue ($ doc ->elementExists ($ xpath ));
@@ -1225,4 +1245,15 @@ public function testDontDecodeAlreadyEncodedDoubleQuotes(): void
12251245 $ doc = TestHelperDOCX::getDocument ($ phpWord , 'Word2007 ' );
12261246 self ::assertIsObject ($ doc );
12271247 }
1248+
1249+ public static function providerParseWidth (): array
1250+ {
1251+ return [
1252+ ['auto ' , 5000 , TblWidth::PERCENT ],
1253+ ['100% ' , 5000 , TblWidth::PERCENT ],
1254+ ['200pt ' , 3999.999999999999 , TblWidth::TWIP ],
1255+ ['300px ' , 4500 , TblWidth::TWIP ],
1256+ ['400 ' , 6000 , TblWidth::TWIP ],
1257+ ];
1258+ }
12281259}
0 commit comments