3131class 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 }
@@ -333,7 +339,7 @@ private static function parseRow($node, $element, &$styles)
333339 * @param \DOMNode $node
334340 * @param \PhpOffice\PhpWord\Element\Table $element
335341 * @param array &$styles
336- * @return \PhpOffice\PhpWord\Element\Cell $element
342+ * @return \PhpOffice\PhpWord\Element\Cell|\PhpOffice\PhpWord\Element\TextRun $element
337343 */
338344 private static function parseCell ($ node , $ element , &$ styles )
339345 {
@@ -343,8 +349,29 @@ 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+ }
346357
347- return $ element ->addCell (null , $ cellStyles );
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+ $ containsBlockElement = self ::$ xpath ->query ('.//table|./p|./ul|./ol ' , $ node )->length > 0 ;
370+ if ($ containsBlockElement ) {
371+ return false ;
372+ }
373+
374+ return true ;
348375 }
349376
350377 /**
@@ -375,14 +402,17 @@ private static function recursiveParseStylesInHierarchy(\DOMNode $node, array $s
375402 */
376403 private static function parseList ($ node , $ element , &$ styles , &$ data )
377404 {
378- $ isOrderedList = $ node ->nodeName == 'ol ' ;
405+ $ isOrderedList = $ node ->nodeName === 'ol ' ;
379406 if (isset ($ data ['listdepth ' ])) {
380407 $ data ['listdepth ' ]++;
381408 } else {
382409 $ data ['listdepth ' ] = 0 ;
383410 $ styles ['list ' ] = 'listStyle_ ' . self ::$ listIndex ++;
384411 $ element ->getPhpWord ()->addNumberingStyle ($ styles ['list ' ], self ::getListStyle ($ isOrderedList ));
385412 }
413+ if ($ node ->parentNode ->nodeName === 'li ' ) {
414+ return $ element ->getParent ();
415+ }
386416 }
387417
388418 private static function getListStyle ($ isOrderedList )
@@ -469,6 +499,9 @@ private static function parseStyle($attribute, $styles)
469499 case 'text-align ' :
470500 $ styles ['alignment ' ] = self ::mapAlign ($ cValue );
471501 break ;
502+ case 'direction ' :
503+ $ styles ['rtl ' ] = $ cValue === 'rtl ' ;
504+ break ;
472505 case 'font-size ' :
473506 $ styles ['size ' ] = Converter::cssToPoint ($ cValue );
474507 break ;
@@ -556,10 +589,12 @@ private static function parseImage($node, $element)
556589 case 'width ' :
557590 $ width = $ attribute ->value ;
558591 $ style ['width ' ] = $ width ;
592+ $ style ['unit ' ] = \PhpOffice \PhpWord \Style \Image::UNIT_PX ;
559593 break ;
560594 case 'height ' :
561595 $ height = $ attribute ->value ;
562596 $ style ['height ' ] = $ height ;
597+ $ style ['unit ' ] = \PhpOffice \PhpWord \Style \Image::UNIT_PX ;
563598 break ;
564599 case 'style ' :
565600 $ styleattr = explode ('; ' , $ attribute ->value );
0 commit comments