Skip to content

Commit 46a5f96

Browse files
committed
fix parsing of table and p inside table cells
1 parent cf6319d commit 46a5f96

File tree

5 files changed

+60
-6
lines changed

5 files changed

+60
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ v0.15.0 (?? ??? 2018)
88
### Added
99
- Parsing of "align" HTML attribute - @troosan #1231
1010
- Parse formatting inside HTML lists - @troosan @samimussbach #1239 #945 #1215 #508
11+
- Parsing of CSS `direction` instruction, HTML `lang` attribute, formatting inside table cell - @troosan #
1112

1213
### Fixed
1314
- fix reading of docx default style - @troosan #1238
15+
- fix the size unit of when parsing html images - @troosan #1254
1416

1517

1618

samples/Sample_26_Html.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77

88
$section = $phpWord->addSection();
99
$html = '<h1>Adding element via HTML</h1>';
10-
$html .= '<p>Some well formed HTML snippet needs to be used</p>';
10+
$html .= '<p>Some well-formed HTML snippet needs to be used</p>';
1111
$html .= '<p>With for example <strong>some<sup>1</sup> <em>inline</em> formatting</strong><sub>1</sub></p>';
1212

1313
$html .= '<p>A link to <a href="http://phpword.readthedocs.io/">Read the docs</a></p>';
1414

15+
$html .= '<p lang="he-IL" style="text-align: right; direction: rtl">היי, זה פסקה מימין לשמאל</p>';
16+
1517
$html .= '<p style="margin-top: 240pt;">Unordered (bulleted) list:</p>';
1618
$html .= '<ul><li>Item 1</li><li>Item 2</li><ul><li>Item 2.1</li><li>Item 2.1</li></ul></ul>';
1719

@@ -65,10 +67,20 @@
6567
</thead>
6668
<tbody>
6769
<tr><td style="border-style: dotted;">1</td><td colspan="2">2</td></tr>
68-
<tr><td>4</td><td>5</td><td>6</td></tr>
70+
<tr><td>This is <b>bold</b> text</td><td></td><td>6</td></tr>
6971
</tbody>
7072
</table>';
7173

74+
$html .= '<p style="margin-top: 240pt;">Table inside another table:</p>';
75+
$html .= '<table align="center" style="width: 80%; border: 6px #0000FF double;">
76+
<tr><td>
77+
<table style="width: 100%; border: 4px #FF0000 dotted;">
78+
<tr><td>column 1</td><td>column 2</td></tr>
79+
</table>
80+
</td></tr>
81+
<tr><td style="text-align: center;">Cell in parent table</td></tr>
82+
</table>';
83+
7284
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);
7385

7486
// Save file

src/PhpWord/Shared/Html.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
class Html
3232
{
3333
private static $listIndex = 0;
34+
private static $xpath;
3435

3536
/**
3637
* Add HTML parts.
@@ -65,6 +66,7 @@ public static function addHtml($element, $html, $fullHTML = false, $preserveWhit
6566
$dom = new \DOMDocument();
6667
$dom->preserveWhiteSpace = $preserveWhiteSpace;
6768
$dom->loadXML($html);
69+
self::$xpath = new \DOMXpath($dom);
6870
$node = $dom->getElementsByTagName('body');
6971

7072
self::parseNode($node->item(0), $element);
@@ -89,6 +91,10 @@ protected static function parseInlineStyle($node, $styles = array())
8991
break;
9092
case 'align':
9193
$styles['alignment'] = self::mapAlign($attribute->value);
94+
break;
95+
case 'lang':
96+
$styles['lang'] = $attribute->value;
97+
break;
9298
}
9399
}
94100
}
@@ -343,8 +349,33 @@ private static function parseCell($node, $element, &$styles)
343349
if (!empty($colspan)) {
344350
$cellStyles['gridSpan'] = $colspan - 0;
345351
}
352+
$cell = $element->addCell(null, $cellStyles);
353+
354+
if (self::shouldAddTextRun($node)) {
355+
return $cell->addTextRun(self::parseInlineStyle($node, $styles['paragraph']));
356+
}
357+
358+
return $cell;
359+
}
360+
361+
/**
362+
* Checks if $node contains an HTML element that cannot be added to TextRun
363+
*
364+
* @param \DOMNode $node
365+
* @return bool Returns true if the node contains an HTML element that cannot be added to TextRun
366+
*/
367+
private static function shouldAddTextRun(\DOMNode $node)
368+
{
369+
if (!$node->hasChildNodes()) {
370+
return false;
371+
}
372+
373+
$containsBlockElement = self::$xpath->query('.//table|./p', $node)->length > 0;
374+
if ($containsBlockElement) {
375+
return false;
376+
}
346377

347-
return $element->addCell(null, $cellStyles);
378+
return true;
348379
}
349380

350381
/**
@@ -469,6 +500,9 @@ private static function parseStyle($attribute, $styles)
469500
case 'text-align':
470501
$styles['alignment'] = self::mapAlign($cValue);
471502
break;
503+
case 'direction':
504+
$styles['rtl'] = $cValue === 'rtl';
505+
break;
472506
case 'font-size':
473507
$styles['size'] = Converter::cssToPoint($cValue);
474508
break;
@@ -556,10 +590,12 @@ private static function parseImage($node, $element)
556590
case 'width':
557591
$width = $attribute->value;
558592
$style['width'] = $width;
593+
$style['unit'] = \PhpOffice\PhpWord\Style\Image::UNIT_PX;
559594
break;
560595
case 'height':
561596
$height = $attribute->value;
562597
$style['height'] = $height;
598+
$style['unit'] = \PhpOffice\PhpWord\Style\Image::UNIT_PX;
563599
break;
564600
case 'style':
565601
$styleattr = explode(';', $attribute->value);

src/PhpWord/Writer/Word2007/Style/Font.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ private function writeStyle()
9090
$xmlWriter->writeAttributeIf($language->getLatin() !== null, 'w:val', $language->getLatin());
9191
$xmlWriter->writeAttributeIf($language->getEastAsia() !== null, 'w:eastAsia', $language->getEastAsia());
9292
$xmlWriter->writeAttributeIf($language->getBidirectional() !== null, 'w:bidi', $language->getBidirectional());
93+
//if bidi is not set but we are writing RTL, write the latin language in the bidi tag
94+
if ($style->isRTL() && $language->getBidirectional() === null && $language->getLatin() !== null) {
95+
$xmlWriter->writeAttribute('w:bidi', $language->getLatin());
96+
}
9397
$xmlWriter->endElement();
9498
}
9599

tests/PhpWord/Shared/HtmlTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public function testOrderedListNumbering()
259259
Html::addHtml($section, $html, false, false);
260260

261261
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
262-
echo $doc->printXml();
262+
263263
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:pPr/w:numPr/w:numId'));
264264
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:t'));
265265

@@ -336,8 +336,8 @@ public function testParseImage()
336336

337337
$baseXpath = '/w:document/w:body/w:p/w:r';
338338
$this->assertTrue($doc->elementExists($baseXpath . '/w:pict/v:shape'));
339-
$this->assertStringMatchesFormat('%Swidth:150pt%S', $doc->getElementAttribute($baseXpath . '[1]/w:pict/v:shape', 'style'));
340-
$this->assertStringMatchesFormat('%Sheight:200pt%S', $doc->getElementAttribute($baseXpath . '[1]/w:pict/v:shape', 'style'));
339+
$this->assertStringMatchesFormat('%Swidth:150px%S', $doc->getElementAttribute($baseXpath . '[1]/w:pict/v:shape', 'style'));
340+
$this->assertStringMatchesFormat('%Sheight:200px%S', $doc->getElementAttribute($baseXpath . '[1]/w:pict/v:shape', 'style'));
341341
$this->assertStringMatchesFormat('%Smso-position-horizontal:right%S', $doc->getElementAttribute($baseXpath . '[1]/w:pict/v:shape', 'style'));
342342
$this->assertStringMatchesFormat('%Smso-position-horizontal:left%S', $doc->getElementAttribute($baseXpath . '[2]/w:pict/v:shape', 'style'));
343343
}

0 commit comments

Comments
 (0)