@@ -632,4 +632,74 @@ public function testParseLetterSpacing()
632
632
$ this ->assertTrue ($ doc ->elementExists ('/w:document/w:body/w:p/w:r/w:rPr/w:spacing ' ));
633
633
$ this ->assertEquals (150 * 15 , $ doc ->getElement ('/w:document/w:body/w:p/w:r/w:rPr/w:spacing ' )->getAttribute ('w:val ' ));
634
634
}
635
+
636
+ /**
637
+ * Parse widths in tables and cells, which also allows for controlling column width
638
+ */
639
+ public function testParseTableAndCellWidth ()
640
+ {
641
+ $ phpWord = new \PhpOffice \PhpWord \PhpWord ();
642
+ $ section = $ phpWord ->addSection ([
643
+ 'orientation ' => \PhpOffice \PhpWord \Style \Section::ORIENTATION_LANDSCAPE ,
644
+ ]);
645
+
646
+ // borders & backgrounds are here just for better visual comparison
647
+ $ html = <<<HTML
648
+ <table style="border: 1px #000000 solid; width: 100%;">
649
+ <tr>
650
+ <td style="width: 25%; border: 1px #000000 solid; text-align: center;">25%</td>
651
+ <td>
652
+ <table width="400" style="border: 1px #000000 solid; background-color: #EEEEEE;">
653
+ <tr>
654
+ <th colspan="3" style="border: 1px #000000 solid;">400px</th>
655
+ </tr>
656
+ <tr>
657
+ <th>T2.R2.C1</th>
658
+ <th style="width: 50pt; border: 1px #FF0000 dashed; background-color: #FFFFFF">50pt</th>
659
+ <th>T2.R2.C3</th>
660
+ </tr>
661
+ <tr>
662
+ <th width="300" colspan="2" style="border: 1px #000000 solid;">300px</th>
663
+ <th style="border: 1px #000000 solid;">T2.R3.C3</th>
664
+ </tr>
665
+ </table>
666
+ </td>
667
+ </tr>
668
+ </table>
669
+ HTML ;
670
+
671
+ Html::addHtml ($ section , $ html );
672
+ $ doc = TestHelperDOCX::getDocument ($ phpWord , 'Word2007 ' );
673
+
674
+ // outer table grid
675
+ $ xpath = '/w:document/w:body/w:tbl/w:tblGrid/w:gridCol ' ;
676
+ $ this ->assertTrue ($ doc ->elementExists ($ xpath ));
677
+ $ this ->assertEquals (25 * 50 , $ doc ->getElement ($ xpath )->getAttribute ('w:w ' ));
678
+ $ this ->assertEquals ('dxa ' , $ doc ->getElement ($ xpath )->getAttribute ('w:type ' ));
679
+
680
+ // <td style="width: 25%; ...
681
+ $ xpath = '/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:tcW ' ;
682
+ $ this ->assertTrue ($ doc ->elementExists ($ xpath ));
683
+ $ this ->assertEquals (25 * 50 , $ doc ->getElement ($ xpath )->getAttribute ('w:w ' ));
684
+ $ this ->assertEquals ('pct ' , $ doc ->getElement ($ xpath )->getAttribute ('w:type ' ));
685
+
686
+ // <table width="400" .. 400px = 6000 twips (400 / 96 * 1440)
687
+ $ xpath = '/w:document/w:body/w:tbl/w:tr/w:tc/w:tbl/w:tr/w:tc/w:tcPr/w:tcW ' ;
688
+ $ this ->assertTrue ($ doc ->elementExists ($ xpath ));
689
+ $ this ->assertEquals (6000 , $ doc ->getElement ($ xpath )->getAttribute ('w:w ' ));
690
+ $ this ->assertEquals ('dxa ' , $ doc ->getElement ($ xpath )->getAttribute ('w:type ' ));
691
+
692
+ // <th style="width: 50pt; .. 50pt = 750 twips (50 / 72 * 1440)
693
+ $ xpath = '/w:document/w:body/w:tbl/w:tr/w:tc/w:tbl/w:tr[2]/w:tc[2]/w:tcPr/w:tcW ' ;
694
+ $ this ->assertTrue ($ doc ->elementExists ($ xpath ));
695
+ $ this ->assertEquals (1000 , $ doc ->getElement ($ xpath )->getAttribute ('w:w ' ));
696
+ $ this ->assertEquals ('dxa ' , $ doc ->getElement ($ xpath )->getAttribute ('w:type ' ));
697
+
698
+ // <th width="300" .. 300px = 4500 twips (300 / 96 * 1440)
699
+ $ xpath = '/w:document/w:body/w:tbl/w:tr/w:tc/w:tbl/w:tr[3]/w:tc/w:tcPr/w:tcW ' ;
700
+ $ this ->assertTrue ($ doc ->elementExists ($ xpath ));
701
+ $ this ->assertEquals (4500 , $ doc ->getElement ($ xpath )->getAttribute ('w:w ' ));
702
+ $ this ->assertEquals ('dxa ' , $ doc ->getElement ($ xpath )->getAttribute ('w:type ' ));
703
+ }
704
+
635
705
}
0 commit comments