|
18 | 18 | namespace PhpOffice\PhpWord\Writer\HTML;
|
19 | 19 |
|
20 | 20 | use PhpOffice\PhpWord\Element\Text as TextElement;
|
| 21 | +use PhpOffice\PhpWord\PhpWord; |
21 | 22 | use PhpOffice\PhpWord\Writer\HTML;
|
22 | 23 | use PhpOffice\PhpWord\Writer\HTML\Element\Text;
|
23 | 24 |
|
@@ -54,4 +55,71 @@ public function testWriteTextElement()
|
54 | 55 |
|
55 | 56 | $this->assertEquals(htmlspecialchars('-A-', ENT_COMPAT, 'UTF-8'), $object->write());
|
56 | 57 | }
|
| 58 | + |
| 59 | + /** |
| 60 | + * Tests writing table with col span |
| 61 | + */ |
| 62 | + public function testWriteColSpan() |
| 63 | + { |
| 64 | + $phpWord = new PhpWord(); |
| 65 | + $section = $phpWord->addSection(); |
| 66 | + $table = $section->addTable(); |
| 67 | + $row1 = $table->addRow(); |
| 68 | + $cell11 = $row1->addCell(1000, array('gridSpan' => 2)); |
| 69 | + $cell11->addText('cell spanning 2 bellow'); |
| 70 | + $row2 = $table->addRow(); |
| 71 | + $cell21 = $row2->addCell(500); |
| 72 | + $cell21->addText('first cell'); |
| 73 | + $cell22 = $row2->addCell(500); |
| 74 | + $cell22->addText('second cell'); |
| 75 | + |
| 76 | + $dom = $this->getAsHTML($phpWord); |
| 77 | + echo $dom->saveHTML(); |
| 78 | + |
| 79 | + $xpath = new \DOMXpath($dom); |
| 80 | + |
| 81 | + $this->assertTrue($xpath->query('/html/body/table/tr[1]/td')->length == 1); |
| 82 | + $this->assertEquals('2', $xpath->query('/html/body/table/tr/td[1]')->item(0)->attributes->getNamedItem('colspan')->textContent); |
| 83 | + $this->assertTrue($xpath->query('/html/body/table/tr[2]/td')->length == 2); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Tests writing table with row span |
| 88 | + */ |
| 89 | + public function testWriteRowSpan() |
| 90 | + { |
| 91 | + $phpWord = new PhpWord(); |
| 92 | + $section = $phpWord->addSection(); |
| 93 | + $table = $section->addTable(); |
| 94 | + |
| 95 | + $row1 = $table->addRow(); |
| 96 | + $row1->addCell(1000, array('vMerge' => 'restart'))->addText('row spanning 3 bellow'); |
| 97 | + $row1->addCell(500)->addText('first cell being spanned'); |
| 98 | + |
| 99 | + $row2 = $table->addRow(); |
| 100 | + $row2->addCell(null, array('vMerge' => 'continue')); |
| 101 | + $row2->addCell(500)->addText('second cell being spanned'); |
| 102 | + |
| 103 | + $row3 = $table->addRow(); |
| 104 | + $row3->addCell(null, array('vMerge' => 'continue')); |
| 105 | + $row3->addCell(500)->addText('third cell being spanned'); |
| 106 | + |
| 107 | + $dom = $this->getAsHTML($phpWord); |
| 108 | + echo $dom->saveHTML(); |
| 109 | + |
| 110 | + $xpath = new \DOMXpath($dom); |
| 111 | + |
| 112 | + $this->assertTrue($xpath->query('/html/body/table/tr[1]/td')->length == 2); |
| 113 | + $this->assertEquals('3', $xpath->query('/html/body/table/tr[1]/td[1]')->item(0)->attributes->getNamedItem('rowspan')->textContent); |
| 114 | + $this->assertTrue($xpath->query('/html/body/table/tr[2]/td')->length == 1); |
| 115 | + } |
| 116 | + |
| 117 | + private function getAsHTML(PhpWord $phpWord) |
| 118 | + { |
| 119 | + $htmlWriter = new HTML($phpWord); |
| 120 | + $dom = new \DOMDocument(); |
| 121 | + $dom->loadHTML($htmlWriter->getContent()); |
| 122 | + |
| 123 | + return $dom; |
| 124 | + } |
57 | 125 | }
|
0 commit comments