25
25
use PhpOffice \PhpWord \Shared \Html ;
26
26
use PhpOffice \PhpWord \SimpleType \Jc ;
27
27
use PhpOffice \PhpWord \SimpleType \LineSpacingRule ;
28
+ use PhpOffice \PhpWord \SimpleType \TblWidth ;
28
29
use PhpOffice \PhpWord \Style \Paragraph ;
29
30
use PhpOffice \PhpWordTests \AbstractWebServerEmbedded ;
30
31
use PhpOffice \PhpWordTests \TestHelperDOCX ;
@@ -156,11 +157,11 @@ public function testParseStyleTableClassName(): void
156
157
}
157
158
158
159
/**
159
- * Test underline .
160
+ * Test text-decoration style .
160
161
*/
161
- public function testParseUnderline (): void
162
+ public function testParseTextDecoration (): void
162
163
{
163
- $ html = '<u >test</u > ' ;
164
+ $ html = '<span style="text-decoration: underline;" >test</span > ' ;
164
165
$ phpWord = new PhpWord ();
165
166
$ section = $ phpWord ->addSection ();
166
167
Html::addHtml ($ section , $ html );
@@ -171,11 +172,11 @@ public function testParseUnderline(): void
171
172
}
172
173
173
174
/**
174
- * Test text-decoration style .
175
+ * Test underline .
175
176
*/
176
- public function testParseTextDecoration (): void
177
+ public function testParseUnderline (): void
177
178
{
178
- $ html = '<span style="text-decoration: underline;" >test</span > ' ;
179
+ $ html = '<u >test</u > ' ;
179
180
$ phpWord = new PhpWord ();
180
181
$ section = $ phpWord ->addSection ();
181
182
Html::addHtml ($ section , $ html );
@@ -185,6 +186,25 @@ public function testParseTextDecoration(): void
185
186
self ::assertEquals ('single ' , $ doc ->getElementAttribute ('/w:document/w:body/w:p/w:r/w:rPr/w:u ' , 'w:val ' ));
186
187
}
187
188
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
+
188
208
/**
189
209
* Test font-variant style.
190
210
*/
@@ -461,31 +481,31 @@ public function testParseTableAndCellWidth(): void
461
481
$ xpath = '/w:document/w:body/w:tbl/w:tblGrid/w:gridCol ' ;
462
482
self ::assertTrue ($ doc ->elementExists ($ xpath ));
463
483
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 ' ));
465
485
466
486
// <td style="width: 25%; ...
467
487
$ xpath = '/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:tcW ' ;
468
488
self ::assertTrue ($ doc ->elementExists ($ xpath ));
469
489
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 ' ));
471
491
472
492
// <table width="400" .. 400px = 6000 twips (400 / 96 * 1440)
473
493
$ xpath = '/w:document/w:body/w:tbl/w:tr/w:tc/w:tbl/w:tr/w:tc/w:tcPr/w:tcW ' ;
474
494
self ::assertTrue ($ doc ->elementExists ($ xpath ));
475
495
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 ' ));
477
497
478
498
// <th style="width: 50pt; .. 50pt = 750 twips (50 / 72 * 1440)
479
499
$ xpath = '/w:document/w:body/w:tbl/w:tr/w:tc/w:tbl/w:tr[2]/w:tc[2]/w:tcPr/w:tcW ' ;
480
500
self ::assertTrue ($ doc ->elementExists ($ xpath ));
481
501
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 ' ));
483
503
484
504
// <th width="300" .. 300px = 4500 twips (300 / 96 * 1440)
485
505
$ xpath = '/w:document/w:body/w:tbl/w:tr/w:tc/w:tbl/w:tr[3]/w:tc/w:tcPr/w:tcW ' ;
486
506
self ::assertTrue ($ doc ->elementExists ($ xpath ));
487
507
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 ' ));
489
509
}
490
510
491
511
/**
@@ -599,7 +619,7 @@ public function testParseTableCellspacingRowBgColor(): void
599
619
$ xpath = '/w:document/w:body/w:tbl/w:tblPr/w:tblCellSpacing ' ;
600
620
self ::assertTrue ($ doc ->elementExists ($ xpath ));
601
621
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 ' ));
603
623
604
624
$ xpath = '/w:document/w:body/w:tbl/w:tr[1]/w:tc[1]/w:tcPr/w:shd ' ;
605
625
self ::assertTrue ($ doc ->elementExists ($ xpath ));
@@ -632,7 +652,7 @@ public function testParseTableStyleAttributeInlineStyle(): void
632
652
$ xpath = '/w:document/w:body/w:tbl/w:tblPr/w:tblW ' ;
633
653
self ::assertTrue ($ doc ->elementExists ($ xpath ));
634
654
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 ' ));
636
656
637
657
$ xpath = '/w:document/w:body/w:tbl/w:tr[1]/w:tc[1]/w:tcPr/w:shd ' ;
638
658
self ::assertTrue ($ doc ->elementExists ($ xpath ));
@@ -1225,4 +1245,15 @@ public function testDontDecodeAlreadyEncodedDoubleQuotes(): void
1225
1245
$ doc = TestHelperDOCX::getDocument ($ phpWord , 'Word2007 ' );
1226
1246
self ::assertIsObject ($ doc );
1227
1247
}
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
+ }
1228
1259
}
0 commit comments