From daf7d1114470d858ccbf10e3895e29f4c53f4d5c Mon Sep 17 00:00:00 2001 From: ejuhjav Date: Wed, 20 May 2015 13:17:11 +0200 Subject: [PATCH 01/12] Update elements.rst Fixed a typo in the $lineStyle example (defined previously as $linestyle but used as $lineStyle) --- docs/elements.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/elements.rst b/docs/elements.rst index eb70f9ad..0980eed0 100644 --- a/docs/elements.rst +++ b/docs/elements.rst @@ -497,7 +497,7 @@ Line elements can be added to sections by using ``addLine``. .. code-block:: php - $linestyle = array('weight' => 1, 'width' => 100, 'height' => 0, 'color' => 635552); + $lineStyle = array('weight' => 1, 'width' => 100, 'height' => 0, 'color' => 635552); $section->addLine($lineStyle) Available line style attributes: From 87a355bb821753882bee9310d00b5aed75f65c61 Mon Sep 17 00:00:00 2001 From: Nilton Date: Sat, 21 Oct 2017 00:07:52 +0200 Subject: [PATCH 02/12] Bring back b, i, and --- src/PhpWord/Shared/Html.php | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index d03d0adf..a0deb7b1 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -119,9 +119,12 @@ class Html 'h6' => array('Heading', null, $element, $styles, null, 'Heading6', null), '#text' => array('Text', $node, $element, $styles, null, null, null), 'strong' => array('Property', null, null, $styles, null, 'bold', true), + 'b' => array('Property', null, null, $styles, null, 'bold', true), 'em' => array('Property', null, null, $styles, null, 'italic', true), + 'i' => array('Property', null, null, $styles, null, 'italic', true), 'sup' => array('Property', null, null, $styles, null, 'superScript', true), 'sub' => array('Property', null, null, $styles, null, 'subScript', true), + 'span' => array('Property', null, null, $styles, null, 'span', $node), 'table' => array('Table', $node, $element, $styles, null, 'addTable', true), 'tr' => array('Table', $node, $element, $styles, null, 'addRow', true), 'td' => array('Table', $node, $element, $styles, null, 'addCell', true), @@ -251,7 +254,16 @@ class Html */ private static function parseProperty(&$styles, $argument1, $argument2) { - $styles['font'][$argument1] = $argument2; + if ($argument1 !== 'span') { + $styles['font'][$argument1] = $argument2; + } else { + if (!is_null($argument2->attributes)) { + $nodeAttr = $argument2->attributes->getNamedItem('style'); + if (!is_null($nodeAttr) && property_exists($nodeAttr, 'value')) { + $styles['font'] = self::parseStyle($nodeAttr, $styles['font']); + } + } + } return null; } @@ -369,6 +381,20 @@ class Html case 'background-color': $styles['bgColor'] = trim($cValue, "#"); break; + case 'font-weight': + $tValue = false; + if (preg_match('#bold#', $cValue)) { + $tValue = true; // also match bolder + } + $styles['bold'] = $tValue; + break; + case 'font-style': + $tValue = false; + if (preg_match('#(?:italic|oblique)#', $cValue)) { + $tValue = true; + } + $styles['italic'] = $tValue; + break; } } From 778a77ecff34779841531354990ddfa55f97e0e9 Mon Sep 17 00:00:00 2001 From: ejuhjav Date: Wed, 20 May 2015 13:17:11 +0200 Subject: [PATCH 03/12] Update elements.rst Fixed a typo in the $lineStyle example (defined previously as $linestyle but used as $lineStyle) --- docs/elements.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/elements.rst b/docs/elements.rst index 124f4431..e27b45d9 100644 --- a/docs/elements.rst +++ b/docs/elements.rst @@ -414,7 +414,7 @@ Line elements can be added to sections by using ``addLine``. .. code-block:: php - $linestyle = array('weight' => 1, 'width' => 100, 'height' => 0, 'color' => 635552); + $lineStyle = array('weight' => 1, 'width' => 100, 'height' => 0, 'color' => 635552); $section->addLine($lineStyle) Available line style attributes: From fcdafeedd47ddf9473f802df680d1e1619d63df5 Mon Sep 17 00:00:00 2001 From: Nilton Date: Sat, 21 Oct 2017 00:07:52 +0200 Subject: [PATCH 04/12] Bring back b, i, and --- src/PhpWord/Shared/Html.php | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index 670ba6e5..369b57fe 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -117,9 +117,12 @@ class Html 'h6' => array('Heading', null, $element, $styles, null, 'Heading6', null), '#text' => array('Text', $node, $element, $styles, null, null, null), 'strong' => array('Property', null, null, $styles, null, 'bold', true), + 'b' => array('Property', null, null, $styles, null, 'bold', true), 'em' => array('Property', null, null, $styles, null, 'italic', true), + 'i' => array('Property', null, null, $styles, null, 'italic', true), 'sup' => array('Property', null, null, $styles, null, 'superScript', true), 'sub' => array('Property', null, null, $styles, null, 'subScript', true), + 'span' => array('Property', null, null, $styles, null, 'span', $node), 'table' => array('Table', $node, $element, $styles, null, 'addTable', true), 'tr' => array('Table', $node, $element, $styles, null, 'addRow', true), 'td' => array('Table', $node, $element, $styles, null, 'addCell', true), @@ -246,7 +249,16 @@ class Html */ private static function parseProperty(&$styles, $argument1, $argument2) { - $styles['font'][$argument1] = $argument2; + if ($argument1 !== 'span') { + $styles['font'][$argument1] = $argument2; + } else { + if (!is_null($argument2->attributes)) { + $nodeAttr = $argument2->attributes->getNamedItem('style'); + if (!is_null($nodeAttr) && property_exists($nodeAttr, 'value')) { + $styles['font'] = self::parseStyle($nodeAttr, $styles['font']); + } + } + } return null; } @@ -362,6 +374,20 @@ class Html case 'background-color': $styles['bgColor'] = trim($cValue, '#'); break; + case 'font-weight': + $tValue = false; + if (preg_match('#bold#', $cValue)) { + $tValue = true; // also match bolder + } + $styles['bold'] = $tValue; + break; + case 'font-style': + $tValue = false; + if (preg_match('#(?:italic|oblique)#', $cValue)) { + $tValue = true; + } + $styles['italic'] = $tValue; + break; } } From 9e7e07d0bc5399d11a061a6baef314b8706d4242 Mon Sep 17 00:00:00 2001 From: troosan Date: Thu, 9 Nov 2017 00:41:56 +0100 Subject: [PATCH 05/12] Add unit tests for Html parser --- src/PhpWord/Shared/Html.php | 41 ++++----- tests/PhpWord/Reader/MsDocTest.php | 20 +++++ tests/PhpWord/Shared/HtmlTest.php | 110 +++++++++++++++++++++++- tests/PhpWord/_includes/XmlDocument.php | 12 ++- 4 files changed, 156 insertions(+), 27 deletions(-) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index f2282499..620839b4 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -18,6 +18,7 @@ namespace PhpOffice\PhpWord\Shared; use PhpOffice\PhpWord\Element\AbstractContainer; +use PhpOffice\PhpWord\SimpleType\Jc; /** * Common Html functions @@ -120,9 +121,10 @@ class Html 'b' => array('Property', null, null, $styles, null, 'bold', true), 'em' => array('Property', null, null, $styles, null, 'italic', true), 'i' => array('Property', null, null, $styles, null, 'italic', true), + 'u' => array('Property', null, null, $styles, null, 'underline', 'single'), 'sup' => array('Property', null, null, $styles, null, 'superScript', true), 'sub' => array('Property', null, null, $styles, null, 'subScript', true), - 'span' => array('Property', null, null, $styles, null, 'span', $node), + 'span' => array('Property', null, null, $styles, null, 'span', $node), 'table' => array('Table', $node, $element, $styles, null, 'addTable', true), 'tr' => array('Table', $node, $element, $styles, null, 'addRow', true), 'td' => array('Table', $node, $element, $styles, null, 'addCell', true), @@ -236,8 +238,6 @@ class Html // if (method_exists($element, 'addText')) { $element->addText($node->nodeValue, $styles['font'], $styles['paragraph']); // } - - return null; } /** @@ -259,8 +259,6 @@ class Html } } } - - return null; } /** @@ -310,8 +308,6 @@ class Html $data['listdepth'] = 0; } $styles['list']['listType'] = $argument1; - - return null; } /** @@ -337,8 +333,6 @@ class Html } $element->addListItem($text, $data['listdepth'], $styles['font'], $styles['list'], $styles['paragraph']); } - - return null; } /** @@ -366,7 +360,20 @@ class Html } break; case 'text-align': - $styles['alignment'] = $cValue; // todo: any mapping? + switch ($cValue) { + case 'left': + $styles['alignment'] = Jc::START; + break; + case 'right': + $styles['alignment'] = Jc::END; + break; + case 'center': + $styles['alignment'] = Jc::CENTER; + break; + case 'justify': + $styles['alignment'] = Jc::BOTH; + break; + } break; case 'color': $styles['color'] = trim($cValue, '#'); @@ -388,20 +395,6 @@ class Html } $styles['italic'] = $tValue; break; - case 'font-weight': - $tValue = false; - if (preg_match('#bold#', $cValue)) { - $tValue = true; // also match bolder - } - $styles['bold'] = $tValue; - break; - case 'font-style': - $tValue = false; - if (preg_match('#(?:italic|oblique)#', $cValue)) { - $tValue = true; - } - $styles['italic'] = $tValue; - break; } } diff --git a/tests/PhpWord/Reader/MsDocTest.php b/tests/PhpWord/Reader/MsDocTest.php index ed16e64b..b57f7e44 100644 --- a/tests/PhpWord/Reader/MsDocTest.php +++ b/tests/PhpWord/Reader/MsDocTest.php @@ -56,4 +56,24 @@ class MsDocTest extends \PHPUnit_Framework_TestCase $phpWord = IOFactory::load($filename, 'MsDoc'); $this->assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $phpWord); } + + /** + * Test exception on not existing file + * @expectedException \Exception + */ + public function testFailIfFileNotReadable() + { + $filename = __DIR__ . '/../_files/documents/not_existing_reader.doc'; + IOFactory::load($filename, 'MsDoc'); + } + + /** + * Test exception on non OLE document + * @expectedException \Exception + */ + public function testFailIfFileNotOle() + { + $filename = __DIR__ . '/../_files/documents/reader.odt'; + IOFactory::load($filename, 'MsDoc'); + } } diff --git a/tests/PhpWord/Shared/HtmlTest.php b/tests/PhpWord/Shared/HtmlTest.php index 602b644d..b1a9e31c 100644 --- a/tests/PhpWord/Shared/HtmlTest.php +++ b/tests/PhpWord/Shared/HtmlTest.php @@ -18,9 +18,12 @@ namespace PhpOffice\PhpWord\Shared; use PhpOffice\PhpWord\Element\Section; +use PhpOffice\PhpWord\SimpleType\Jc; +use PhpOffice\PhpWord\TestHelperDOCX; /** * Test class for PhpOffice\PhpWord\Shared\Html + * @coversDefaultClass \PhpOffice\PhpWord\Shared\Html */ class HtmlTest extends \PHPUnit_Framework_TestCase { @@ -43,7 +46,7 @@ class HtmlTest extends \PHPUnit_Framework_TestCase // Styles $content .= '

'; + . 'text-align: center; color: #999; background-color: #000; font-weight: bold; font-style: italic;">'; foreach ($styles as $style) { $content .= "<{$style}>{$style}"; } @@ -67,4 +70,109 @@ class HtmlTest extends \PHPUnit_Framework_TestCase $content .= '–   ²³¼½¾'; Html::addHtml($section, $content); } + + /** + * Test that html already in body element can be read + * @ignore + */ + public function testParseFullHtml() + { + $section = new Section(1); + Html::addHtml($section, '

test paragraph1

test paragraph2

', true); + + $this->assertCount(2, $section->getElements()); + } + + /** + * Test underline + */ + public function testParseUnderline() + { + $html = 'test'; + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $section = $phpWord->addSection(); + Html::addHtml($section, $html); + + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); + $this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:u')); + $this->assertEquals('single', $doc->getElementAttribute('/w:document/w:body/w:p/w:r/w:rPr/w:u', 'w:val')); + } + + /** + * Test text-decoration style + */ + public function testParseTextDecoration() + { + $html = 'test'; + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $section = $phpWord->addSection(); + Html::addHtml($section, $html); + + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); + $this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:u')); + $this->assertEquals('single', $doc->getElementAttribute('/w:document/w:body/w:p/w:r/w:rPr/w:u', 'w:val')); + } + + /** + * Test text-align style + */ + public function testParseTextAlign() + { + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $section = $phpWord->addSection(); + Html::addHtml($section, '

test

'); + Html::addHtml($section, '

test

'); + Html::addHtml($section, '

test

'); + Html::addHtml($section, '

test

'); + + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); + $this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:pPr/w:jc')); + $this->assertEquals('start', $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:pPr/w:jc', 'w:val')); + $this->assertEquals('end', $doc->getElementAttribute('/w:document/w:body/w:p[2]/w:pPr/w:jc', 'w:val')); + $this->assertEquals('center', $doc->getElementAttribute('/w:document/w:body/w:p[3]/w:pPr/w:jc', 'w:val')); + $this->assertEquals('both', $doc->getElementAttribute('/w:document/w:body/w:p[4]/w:pPr/w:jc', 'w:val')); + } + + /** + * Test parsing paragraph and span styles + */ + public function testParseParagraphAndSpanStyle() + { + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $section = $phpWord->addSection(); + Html::addHtml($section, '

test

'); + + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); + $this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:pPr/w:jc')); + $this->assertEquals('center', $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:pPr/w:jc', 'w:val')); + $this->assertEquals('single', $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:r/w:rPr/w:u', 'w:val')); + } + + /** + * Test parsing table + */ + public function testParseTable() + { + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $section = $phpWord->addSection(); + $html = ' + + + + + + + + + + + + +
abc
12
456
'; + Html::addHtml($section, $html); + + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); +// echo $doc->printXml(); +// $this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr/w:tc')); + } } diff --git a/tests/PhpWord/_includes/XmlDocument.php b/tests/PhpWord/_includes/XmlDocument.php index eb335278..c82c5a8e 100644 --- a/tests/PhpWord/_includes/XmlDocument.php +++ b/tests/PhpWord/_includes/XmlDocument.php @@ -170,12 +170,20 @@ class XmlDocument * @param string $file * @return string */ - public function printXml($path = '/w:document', $file = 'word/document.xml') + public function printXml($path = '/', $file = 'word/document.xml') { + $element = $this->getElement($path, $file); + if ($element instanceof \DOMDocument) { + $element->formatOutput = true; + $element->preserveWhiteSpace = false; + + return $element->saveXML(); + } + $newdoc = new \DOMDocument(); $newdoc->formatOutput = true; $newdoc->preserveWhiteSpace = false; - $node = $newdoc->importNode($this->getElement($path, $file), true); + $node = $newdoc->importNode($element, true); $newdoc->appendChild($node); return $newdoc->saveXML($node); From 6328b833881c3d4908bb3fdfd2b95c740b25e2dc Mon Sep 17 00:00:00 2001 From: troosan Date: Thu, 9 Nov 2017 23:07:17 +0100 Subject: [PATCH 06/12] fix warning --- tests/PhpWord/Shared/HtmlTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/PhpWord/Shared/HtmlTest.php b/tests/PhpWord/Shared/HtmlTest.php index b1a9e31c..c35cc6d5 100644 --- a/tests/PhpWord/Shared/HtmlTest.php +++ b/tests/PhpWord/Shared/HtmlTest.php @@ -173,6 +173,7 @@ class HtmlTest extends \PHPUnit_Framework_TestCase $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); // echo $doc->printXml(); -// $this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr/w:tc')); + $this->assertTrue($doc->elementExists('/w:document/w:body/w:p')); +// $this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:tbl/w:tr/w:tc')); } } From e4fe0c66731a0bff17e95d6278db6ddf90e53fff Mon Sep 17 00:00:00 2001 From: ejuhjav Date: Wed, 20 May 2015 13:17:11 +0200 Subject: [PATCH 07/12] Update elements.rst Fixed a typo in the $lineStyle example (defined previously as $linestyle but used as $lineStyle) --- docs/elements.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/elements.rst b/docs/elements.rst index 124f4431..e27b45d9 100644 --- a/docs/elements.rst +++ b/docs/elements.rst @@ -414,7 +414,7 @@ Line elements can be added to sections by using ``addLine``. .. code-block:: php - $linestyle = array('weight' => 1, 'width' => 100, 'height' => 0, 'color' => 635552); + $lineStyle = array('weight' => 1, 'width' => 100, 'height' => 0, 'color' => 635552); $section->addLine($lineStyle) Available line style attributes: From 379df3cebd4ace1400a5bfa9a89474da67083a91 Mon Sep 17 00:00:00 2001 From: Nilton Date: Sat, 21 Oct 2017 00:07:52 +0200 Subject: [PATCH 08/12] Bring back b, i, and --- src/PhpWord/Shared/Html.php | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index 670ba6e5..369b57fe 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -117,9 +117,12 @@ class Html 'h6' => array('Heading', null, $element, $styles, null, 'Heading6', null), '#text' => array('Text', $node, $element, $styles, null, null, null), 'strong' => array('Property', null, null, $styles, null, 'bold', true), + 'b' => array('Property', null, null, $styles, null, 'bold', true), 'em' => array('Property', null, null, $styles, null, 'italic', true), + 'i' => array('Property', null, null, $styles, null, 'italic', true), 'sup' => array('Property', null, null, $styles, null, 'superScript', true), 'sub' => array('Property', null, null, $styles, null, 'subScript', true), + 'span' => array('Property', null, null, $styles, null, 'span', $node), 'table' => array('Table', $node, $element, $styles, null, 'addTable', true), 'tr' => array('Table', $node, $element, $styles, null, 'addRow', true), 'td' => array('Table', $node, $element, $styles, null, 'addCell', true), @@ -246,7 +249,16 @@ class Html */ private static function parseProperty(&$styles, $argument1, $argument2) { - $styles['font'][$argument1] = $argument2; + if ($argument1 !== 'span') { + $styles['font'][$argument1] = $argument2; + } else { + if (!is_null($argument2->attributes)) { + $nodeAttr = $argument2->attributes->getNamedItem('style'); + if (!is_null($nodeAttr) && property_exists($nodeAttr, 'value')) { + $styles['font'] = self::parseStyle($nodeAttr, $styles['font']); + } + } + } return null; } @@ -362,6 +374,20 @@ class Html case 'background-color': $styles['bgColor'] = trim($cValue, '#'); break; + case 'font-weight': + $tValue = false; + if (preg_match('#bold#', $cValue)) { + $tValue = true; // also match bolder + } + $styles['bold'] = $tValue; + break; + case 'font-style': + $tValue = false; + if (preg_match('#(?:italic|oblique)#', $cValue)) { + $tValue = true; + } + $styles['italic'] = $tValue; + break; } } From 07c9d9fd005b0746580f8fc153e4b93fdaf5b0dc Mon Sep 17 00:00:00 2001 From: troosan Date: Thu, 9 Nov 2017 00:41:56 +0100 Subject: [PATCH 09/12] Add unit tests for Html parser --- src/PhpWord/Shared/Html.php | 41 ++++----- tests/PhpWord/Reader/MsDocTest.php | 20 +++++ tests/PhpWord/Shared/HtmlTest.php | 110 +++++++++++++++++++++++- tests/PhpWord/_includes/XmlDocument.php | 12 ++- 4 files changed, 156 insertions(+), 27 deletions(-) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index 369b57fe..876d0a3a 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -18,6 +18,7 @@ namespace PhpOffice\PhpWord\Shared; use PhpOffice\PhpWord\Element\AbstractContainer; +use PhpOffice\PhpWord\SimpleType\Jc; /** * Common Html functions @@ -120,9 +121,10 @@ class Html 'b' => array('Property', null, null, $styles, null, 'bold', true), 'em' => array('Property', null, null, $styles, null, 'italic', true), 'i' => array('Property', null, null, $styles, null, 'italic', true), + 'u' => array('Property', null, null, $styles, null, 'underline', 'single'), 'sup' => array('Property', null, null, $styles, null, 'superScript', true), 'sub' => array('Property', null, null, $styles, null, 'subScript', true), - 'span' => array('Property', null, null, $styles, null, 'span', $node), + 'span' => array('Property', null, null, $styles, null, 'span', $node), 'table' => array('Table', $node, $element, $styles, null, 'addTable', true), 'tr' => array('Table', $node, $element, $styles, null, 'addRow', true), 'td' => array('Table', $node, $element, $styles, null, 'addCell', true), @@ -236,8 +238,6 @@ class Html // if (method_exists($element, 'addText')) { $element->addText($node->nodeValue, $styles['font'], $styles['paragraph']); // } - - return null; } /** @@ -259,8 +259,6 @@ class Html } } } - - return null; } /** @@ -310,8 +308,6 @@ class Html $data['listdepth'] = 0; } $styles['list']['listType'] = $argument1; - - return null; } /** @@ -337,8 +333,6 @@ class Html } $element->addListItem($text, $data['listdepth'], $styles['font'], $styles['list'], $styles['paragraph']); } - - return null; } /** @@ -366,7 +360,20 @@ class Html } break; case 'text-align': - $styles['alignment'] = $cValue; // todo: any mapping? + switch ($cValue) { + case 'left': + $styles['alignment'] = Jc::START; + break; + case 'right': + $styles['alignment'] = Jc::END; + break; + case 'center': + $styles['alignment'] = Jc::CENTER; + break; + case 'justify': + $styles['alignment'] = Jc::BOTH; + break; + } break; case 'color': $styles['color'] = trim($cValue, '#'); @@ -374,20 +381,6 @@ class Html case 'background-color': $styles['bgColor'] = trim($cValue, '#'); break; - case 'font-weight': - $tValue = false; - if (preg_match('#bold#', $cValue)) { - $tValue = true; // also match bolder - } - $styles['bold'] = $tValue; - break; - case 'font-style': - $tValue = false; - if (preg_match('#(?:italic|oblique)#', $cValue)) { - $tValue = true; - } - $styles['italic'] = $tValue; - break; } } diff --git a/tests/PhpWord/Reader/MsDocTest.php b/tests/PhpWord/Reader/MsDocTest.php index 12d50247..e407547d 100644 --- a/tests/PhpWord/Reader/MsDocTest.php +++ b/tests/PhpWord/Reader/MsDocTest.php @@ -56,4 +56,24 @@ class MsDocTest extends \PHPUnit\Framework\TestCase $phpWord = IOFactory::load($filename, 'MsDoc'); $this->assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $phpWord); } + + /** + * Test exception on not existing file + * @expectedException \Exception + */ + public function testFailIfFileNotReadable() + { + $filename = __DIR__ . '/../_files/documents/not_existing_reader.doc'; + IOFactory::load($filename, 'MsDoc'); + } + + /** + * Test exception on non OLE document + * @expectedException \Exception + */ + public function testFailIfFileNotOle() + { + $filename = __DIR__ . '/../_files/documents/reader.odt'; + IOFactory::load($filename, 'MsDoc'); + } } diff --git a/tests/PhpWord/Shared/HtmlTest.php b/tests/PhpWord/Shared/HtmlTest.php index 0ba7717f..b89adec9 100644 --- a/tests/PhpWord/Shared/HtmlTest.php +++ b/tests/PhpWord/Shared/HtmlTest.php @@ -18,9 +18,12 @@ namespace PhpOffice\PhpWord\Shared; use PhpOffice\PhpWord\Element\Section; +use PhpOffice\PhpWord\SimpleType\Jc; +use PhpOffice\PhpWord\TestHelperDOCX; /** * Test class for PhpOffice\PhpWord\Shared\Html + * @coversDefaultClass \PhpOffice\PhpWord\Shared\Html */ class HtmlTest extends \PHPUnit\Framework\TestCase { @@ -43,7 +46,7 @@ class HtmlTest extends \PHPUnit\Framework\TestCase // Styles $content .= '

'; + . 'text-align: center; color: #999; background-color: #000; font-weight: bold; font-style: italic;">'; foreach ($styles as $style) { $content .= "<{$style}>{$style}"; } @@ -67,4 +70,109 @@ class HtmlTest extends \PHPUnit\Framework\TestCase $content .= '–   ²³¼½¾'; Html::addHtml($section, $content); } + + /** + * Test that html already in body element can be read + * @ignore + */ + public function testParseFullHtml() + { + $section = new Section(1); + Html::addHtml($section, '

test paragraph1

test paragraph2

', true); + + $this->assertCount(2, $section->getElements()); + } + + /** + * Test underline + */ + public function testParseUnderline() + { + $html = 'test'; + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $section = $phpWord->addSection(); + Html::addHtml($section, $html); + + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); + $this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:u')); + $this->assertEquals('single', $doc->getElementAttribute('/w:document/w:body/w:p/w:r/w:rPr/w:u', 'w:val')); + } + + /** + * Test text-decoration style + */ + public function testParseTextDecoration() + { + $html = 'test'; + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $section = $phpWord->addSection(); + Html::addHtml($section, $html); + + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); + $this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:u')); + $this->assertEquals('single', $doc->getElementAttribute('/w:document/w:body/w:p/w:r/w:rPr/w:u', 'w:val')); + } + + /** + * Test text-align style + */ + public function testParseTextAlign() + { + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $section = $phpWord->addSection(); + Html::addHtml($section, '

test

'); + Html::addHtml($section, '

test

'); + Html::addHtml($section, '

test

'); + Html::addHtml($section, '

test

'); + + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); + $this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:pPr/w:jc')); + $this->assertEquals('start', $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:pPr/w:jc', 'w:val')); + $this->assertEquals('end', $doc->getElementAttribute('/w:document/w:body/w:p[2]/w:pPr/w:jc', 'w:val')); + $this->assertEquals('center', $doc->getElementAttribute('/w:document/w:body/w:p[3]/w:pPr/w:jc', 'w:val')); + $this->assertEquals('both', $doc->getElementAttribute('/w:document/w:body/w:p[4]/w:pPr/w:jc', 'w:val')); + } + + /** + * Test parsing paragraph and span styles + */ + public function testParseParagraphAndSpanStyle() + { + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $section = $phpWord->addSection(); + Html::addHtml($section, '

test

'); + + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); + $this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:pPr/w:jc')); + $this->assertEquals('center', $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:pPr/w:jc', 'w:val')); + $this->assertEquals('single', $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:r/w:rPr/w:u', 'w:val')); + } + + /** + * Test parsing table + */ + public function testParseTable() + { + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $section = $phpWord->addSection(); + $html = ' + + + + + + + + + + + + +
abc
12
456
'; + Html::addHtml($section, $html); + + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); +// echo $doc->printXml(); +// $this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr/w:tc')); + } } diff --git a/tests/PhpWord/_includes/XmlDocument.php b/tests/PhpWord/_includes/XmlDocument.php index eb335278..c82c5a8e 100644 --- a/tests/PhpWord/_includes/XmlDocument.php +++ b/tests/PhpWord/_includes/XmlDocument.php @@ -170,12 +170,20 @@ class XmlDocument * @param string $file * @return string */ - public function printXml($path = '/w:document', $file = 'word/document.xml') + public function printXml($path = '/', $file = 'word/document.xml') { + $element = $this->getElement($path, $file); + if ($element instanceof \DOMDocument) { + $element->formatOutput = true; + $element->preserveWhiteSpace = false; + + return $element->saveXML(); + } + $newdoc = new \DOMDocument(); $newdoc->formatOutput = true; $newdoc->preserveWhiteSpace = false; - $node = $newdoc->importNode($this->getElement($path, $file), true); + $node = $newdoc->importNode($element, true); $newdoc->appendChild($node); return $newdoc->saveXML($node); From 8bb9a999751671ee715b35b3efa110691fb0ff34 Mon Sep 17 00:00:00 2001 From: troosan Date: Thu, 9 Nov 2017 23:07:17 +0100 Subject: [PATCH 10/12] fix warning --- tests/PhpWord/Shared/HtmlTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/PhpWord/Shared/HtmlTest.php b/tests/PhpWord/Shared/HtmlTest.php index b89adec9..8ec6840f 100644 --- a/tests/PhpWord/Shared/HtmlTest.php +++ b/tests/PhpWord/Shared/HtmlTest.php @@ -173,6 +173,7 @@ class HtmlTest extends \PHPUnit\Framework\TestCase $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); // echo $doc->printXml(); -// $this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr/w:tc')); + $this->assertTrue($doc->elementExists('/w:document/w:body/w:p')); +// $this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:tbl/w:tr/w:tc')); } } From ff8234bce46c01b8e9ecf4910e1b6c70bcfbdebc Mon Sep 17 00:00:00 2001 From: troosan Date: Fri, 10 Nov 2017 23:37:02 +0100 Subject: [PATCH 11/12] add tests --- src/PhpWord/Element/AbstractContainer.php | 2 +- .../Writer/ODText/Part/ContentTest.php | 1 + tests/PhpWord/Writer/Word2007/ElementTest.php | 62 +++++++++++++++++++ .../Writer/Word2007/Style/FontTest.php | 15 +++++ .../Writer/Word2007/Style/ParagraphTest.php | 52 ++++++++++++++++ 5 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 tests/PhpWord/Writer/Word2007/Style/ParagraphTest.php diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index d7171798..cb42cf3d 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -23,7 +23,7 @@ namespace PhpOffice\PhpWord\Element; * @method Text addText(string $text, mixed $fStyle = null, mixed $pStyle = null) * @method TextRun addTextRun(mixed $pStyle = null) * @method Bookmark addBookmark(string $name) - * @method Link addLink(string $target, string $text = null, mixed $fStyle = null, mixed $pStyle = null) + * @method Link addLink(string $target, string $text = null, mixed $fStyle = null, mixed $pStyle = null, boolean $internal = false) * @method PreserveText addPreserveText(string $text, mixed $fStyle = null, mixed $pStyle = null) * @method void addTextBreak(int $count = 1, mixed $fStyle = null, mixed $pStyle = null) * @method ListItem addListItem(string $txt, int $depth = 0, mixed $font = null, mixed $list = null, mixed $para = null) diff --git a/tests/PhpWord/Writer/ODText/Part/ContentTest.php b/tests/PhpWord/Writer/ODText/Part/ContentTest.php index 28542379..d5681143 100644 --- a/tests/PhpWord/Writer/ODText/Part/ContentTest.php +++ b/tests/PhpWord/Writer/ODText/Part/ContentTest.php @@ -83,6 +83,7 @@ class ContentTest extends \PHPUnit\Framework\TestCase $cell->addObject($objectSrc); $textrun = $cell->addTextRun(); $textrun->addText('Test text run'); + $section->addPageBreak(); $footer = $section->addFooter(); $footer->addPreserveText('{PAGE}'); diff --git a/tests/PhpWord/Writer/Word2007/ElementTest.php b/tests/PhpWord/Writer/Word2007/ElementTest.php index 85a9bb38..a81a617b 100644 --- a/tests/PhpWord/Writer/Word2007/ElementTest.php +++ b/tests/PhpWord/Writer/Word2007/ElementTest.php @@ -71,6 +71,68 @@ class ElementTest extends \PHPUnit\Framework\TestCase $this->assertTrue($doc->elementExists($element)); } + /** + * Test bookmark element + */ + public function testBookmark() + { + $phpWord = new PhpWord(); + $section = $phpWord->addSection(); + + $section->addBookmark('test_bookmark'); + $doc = TestHelperDOCX::getDocument($phpWord); + + $element = '/w:document/w:body/w:bookmarkStart'; + $this->assertTrue($doc->elementExists($element)); + $this->assertEquals('test_bookmark', $doc->getElementAttribute($element, 'w:name')); + + $element = '/w:document/w:body/w:bookmarkEnd'; + $this->assertTrue($doc->elementExists($element)); + } + + /** + * Test link element + */ + public function testLinkElement() + { + $phpWord = new PhpWord(); + $section = $phpWord->addSection(); + + $section->addLink('https://github.com/PHPOffice/PHPWord'); + $section->addLink('internal_link', null, null, null, true); + $doc = TestHelperDOCX::getDocument($phpWord); + + $element = '/w:document/w:body/w:p[1]/w:hyperlink/w:r/w:t'; + $this->assertTrue($doc->elementExists($element)); + + $element = '/w:document/w:body/w:p[2]/w:hyperlink/w:r/w:t'; + $this->assertTrue($doc->elementExists($element)); + $this->assertEquals('internal_link', $doc->getElementAttribute('/w:document/w:body/w:p[2]/w:hyperlink', 'w:anchor')); + } + + /** + * Basic test for table element + */ + public function testTableElements() + { + $phpWord = new PhpWord(); + $section = $phpWord->addSection(); + + $table = $section->addTable(array('alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER)); + $table->addRow(900); + $table->addCell(2000)->addText('Row 1'); + $table->addCell(2000)->addText('Row 2'); + $table->addCell(2000)->addText('Row 3'); + $table->addCell(2000)->addText('Row 4'); + + $doc = TestHelperDOCX::getDocument($phpWord); + + $tableRootElement = '/w:document/w:body/w:tbl'; + $this->assertTrue($doc->elementExists($tableRootElement . '/w:tblGrid/w:gridCol')); + $this->assertTrue($doc->elementExists($tableRootElement . '/w:tblPr/w:jc')); + $this->assertEquals('center', $doc->getElementAttribute($tableRootElement . '/w:tblPr/w:jc', 'w:val')); + } + /** * Test shape elements */ diff --git a/tests/PhpWord/Writer/Word2007/Style/FontTest.php b/tests/PhpWord/Writer/Word2007/Style/FontTest.php index 78b2c320..d36a3037 100644 --- a/tests/PhpWord/Writer/Word2007/Style/FontTest.php +++ b/tests/PhpWord/Writer/Word2007/Style/FontTest.php @@ -50,4 +50,19 @@ class FontTest extends \PHPUnit\Framework\TestCase $path = '/w:document/w:body/w:p/w:r/w:rPr/w:rtl'; $this->assertTrue($doc->elementExists($path, $file)); } + + /** + * Test writing font with language + */ + public function testFontWithLang() + { + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $section = $phpWord->addSection(); + $section->addText('Ce texte-ci est en français.', array('lang' => \PhpOffice\PhpWord\Style\Language::FR_BE)); + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); + + $file = 'word/document.xml'; + $path = '/w:document/w:body/w:p/w:r/w:rPr/w:lang'; + $this->assertTrue($doc->elementExists($path, $file)); + } } diff --git a/tests/PhpWord/Writer/Word2007/Style/ParagraphTest.php b/tests/PhpWord/Writer/Word2007/Style/ParagraphTest.php new file mode 100644 index 00000000..9bc2756b --- /dev/null +++ b/tests/PhpWord/Writer/Word2007/Style/ParagraphTest.php @@ -0,0 +1,52 @@ +addParagraphStyle('testStyle', array('indent' => '10')); + $section = $phpWord->addSection(); + $section->addText('test', null, array('numStyle' => 'testStyle', 'numLevel' => '1')); + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); + + $path = '/w:document/w:body/w:p/w:pPr/w:numPr/w:ilvl'; + $this->assertTrue($doc->elementExists($path)); + } +} From e72446442bd213b2c26bcc5d36fda588c54291c4 Mon Sep 17 00:00:00 2001 From: troosan Date: Fri, 10 Nov 2017 23:47:10 +0100 Subject: [PATCH 12/12] fix format --- tests/PhpWord/Writer/Word2007/ElementTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PhpWord/Writer/Word2007/ElementTest.php b/tests/PhpWord/Writer/Word2007/ElementTest.php index a81a617b..b31b223a 100644 --- a/tests/PhpWord/Writer/Word2007/ElementTest.php +++ b/tests/PhpWord/Writer/Word2007/ElementTest.php @@ -78,7 +78,7 @@ class ElementTest extends \PHPUnit\Framework\TestCase { $phpWord = new PhpWord(); $section = $phpWord->addSection(); - + $section->addBookmark('test_bookmark'); $doc = TestHelperDOCX::getDocument($phpWord);