From ac357d10d52b8c2e491e56683f8a07665d3fa5ff Mon Sep 17 00:00:00 2001
From: troosan
Date: Sat, 18 Nov 2017 15:55:05 +0100
Subject: [PATCH] Various fixes
- parse text inside list items
- add tests
- rename Cell widthType attribute to unit
---
docs/elements.rst | 2 +-
samples/Sample_26_Html.php | 20 ++++++++++--
src/PhpWord/Element/AbstractContainer.php | 2 --
src/PhpWord/Element/Footnote.php | 5 ++-
src/PhpWord/Element/Section.php | 6 ----
src/PhpWord/Settings.php | 4 ---
src/PhpWord/Shared/Html.php | 14 ++++----
src/PhpWord/SimpleType/Jc.php | 2 --
src/PhpWord/SimpleType/JcTable.php | 2 --
src/PhpWord/Style/Cell.php | 16 +++++-----
src/PhpWord/Writer/AbstractWriter.php | 4 +--
src/PhpWord/Writer/Word2007/Style/Cell.php | 12 ++++---
tests/PhpWord/Shared/ConverterTest.php | 1 +
tests/PhpWord/Shared/HtmlTest.php | 32 +++++++++++++++++--
tests/PhpWord/Writer/Word2007/ElementTest.php | 19 +++++++++++
15 files changed, 96 insertions(+), 45 deletions(-)
diff --git a/docs/elements.rst b/docs/elements.rst
index e27b45d9..a2e41566 100644
--- a/docs/elements.rst
+++ b/docs/elements.rst
@@ -415,7 +415,7 @@ Line elements can be added to sections by using ``addLine``.
.. code-block:: php
$lineStyle = array('weight' => 1, 'width' => 100, 'height' => 0, 'color' => 635552);
- $section->addLine($lineStyle)
+ $section->addLine($lineStyle);
Available line style attributes:
diff --git a/samples/Sample_26_Html.php b/samples/Sample_26_Html.php
index 8e6e9a33..ba06b063 100644
--- a/samples/Sample_26_Html.php
+++ b/samples/Sample_26_Html.php
@@ -14,11 +14,25 @@ $html .= '- Item 1
- Item 2
- Item 2.1
- Item 2.1Ordered (numbered) list:
';
$html .= '- Item 1
- Item 2
';
+$html .= 'List with complex content:
';
+$html .= '
+ -
+
+ list item1
+
+
+ -
+
+ list item2
+
+
+
';
+
$html .= '
- | header a |
- header b |
+ header a |
+ header b |
header c |
@@ -28,7 +42,7 @@ $html .= '';
-\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html);
+\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);
// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);
diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php
index cb42cf3d..d44160d8 100644
--- a/src/PhpWord/Element/AbstractContainer.php
+++ b/src/PhpWord/Element/AbstractContainer.php
@@ -157,8 +157,6 @@ abstract class AbstractContainer extends AbstractElement
* Get all elements
*
* @return array
- *
- * @codeCoverageIgnore
*/
public function getElements()
{
diff --git a/src/PhpWord/Element/Footnote.php b/src/PhpWord/Element/Footnote.php
index 9acdc4c3..e9a1bfc2 100644
--- a/src/PhpWord/Element/Footnote.php
+++ b/src/PhpWord/Element/Footnote.php
@@ -19,9 +19,6 @@ namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\Style\Paragraph;
-/**
- * @codeCoverageIgnore
- */
class Footnote extends AbstractContainer
{
/**
@@ -68,6 +65,7 @@ class Footnote extends AbstractContainer
* Get Footnote Reference ID
*
* @deprecated 0.10.0
+ * @codeCoverageIgnore
*
* @return int
*/
@@ -80,6 +78,7 @@ class Footnote extends AbstractContainer
* Set Footnote Reference ID
*
* @deprecated 0.10.0
+ * @codeCoverageIgnore
*
* @param int $rId
*/
diff --git a/src/PhpWord/Element/Section.php b/src/PhpWord/Element/Section.php
index ffc98435..8238277e 100644
--- a/src/PhpWord/Element/Section.php
+++ b/src/PhpWord/Element/Section.php
@@ -85,8 +85,6 @@ class Section extends AbstractContainer
* Get section style
*
* @return \PhpOffice\PhpWord\Style\Section
- *
- * @codeCoverageIgnore
*/
public function getStyle()
{
@@ -125,8 +123,6 @@ class Section extends AbstractContainer
* Get header elements
*
* @return Header[]
- *
- * @codeCoverageIgnore
*/
public function getHeaders()
{
@@ -137,8 +133,6 @@ class Section extends AbstractContainer
* Get footer elements
*
* @return Footer[]
- *
- * @codeCoverageIgnore
*/
public function getFooters()
{
diff --git a/src/PhpWord/Settings.php b/src/PhpWord/Settings.php
index 91efa1a6..144b0fc5 100644
--- a/src/PhpWord/Settings.php
+++ b/src/PhpWord/Settings.php
@@ -318,8 +318,6 @@ class Settings
* @since 0.13.0
*
* @return bool
- *
- * @codeCoverageIgnore
*/
public static function isOutputEscapingEnabled()
{
@@ -330,8 +328,6 @@ class Settings
* @since 0.13.0
*
* @param bool $outputEscapingEnabled
- *
- * @codeCoverageIgnore
*/
public static function setOutputEscapingEnabled($outputEscapingEnabled)
{
diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php
index 7239a046..479e0f46 100644
--- a/src/PhpWord/Shared/Html.php
+++ b/src/PhpWord/Shared/Html.php
@@ -37,8 +37,9 @@ class Html
* @param \PhpOffice\PhpWord\Element\AbstractContainer $element Where the parts need to be added
* @param string $html The code to parse
* @param bool $fullHTML If it's a full HTML, no need to add 'body' tag
+ * @param bool $preserveWhiteSpace If false, the whitespaces between nodes will be removed
*/
- public static function addHtml($element, $html, $fullHTML = false)
+ public static function addHtml($element, $html, $fullHTML = false, $preserveWhiteSpace = true)
{
/*
* @todo parse $stylesheet for default styles. Should result in an array based on id, class and element,
@@ -59,7 +60,7 @@ class Html
// Load DOM
$dom = new \DOMDocument();
- $dom->preserveWhiteSpace = true;
+ $dom->preserveWhiteSpace = $preserveWhiteSpace;
$dom->loadXML($html);
$node = $dom->getElementsByTagName('body');
@@ -395,6 +396,10 @@ class Html
$text = $cNode->nodeValue;
}
}
+ //ideally we should be parsing child nodes for any style, for now just take the text
+ if ('' == trim($text) && '' != trim($node->textContent)) {
+ $text = trim($node->textContent);
+ }
$element->addListItem($text, $data['listdepth'], $styles['font'], $styles['list'], $styles['paragraph']);
}
}
@@ -508,16 +513,13 @@ class Html
*/
private static function mapBorderStyle($cssBorderStyle)
{
- if ($cssBorderStyle == null) {
- return null;
- }
switch ($cssBorderStyle) {
case 'none':
case 'dashed':
case 'dotted':
case 'double':
return $cssBorderStyle;
- case 'solid':
+ default:
return 'single';
}
}
diff --git a/src/PhpWord/SimpleType/Jc.php b/src/PhpWord/SimpleType/Jc.php
index 1a5d33ad..5d0ee33b 100644
--- a/src/PhpWord/SimpleType/Jc.php
+++ b/src/PhpWord/SimpleType/Jc.php
@@ -29,8 +29,6 @@ use PhpOffice\PhpWord\Shared\AbstractEnum;
*
* @see \PhpOffice\PhpWord\SimpleType\JcTable For table alignment modes available since ISO/IEC-29500:2008.
* @see http://www.datypic.com/sc/ooxml/t-w_ST_Jc.html
- *
- * @codeCoverageIgnore
*/
final class Jc extends AbstractEnum
{
diff --git a/src/PhpWord/SimpleType/JcTable.php b/src/PhpWord/SimpleType/JcTable.php
index e1af89ad..71e07397 100644
--- a/src/PhpWord/SimpleType/JcTable.php
+++ b/src/PhpWord/SimpleType/JcTable.php
@@ -25,8 +25,6 @@ use PhpOffice\PhpWord\Shared\AbstractEnum;
* Introduced in ISO/IEC-29500:2008.
*
* @since 0.13.0
- *
- * @codeCoverageIgnore
*/
final class JcTable extends AbstractEnum
{
diff --git a/src/PhpWord/Style/Cell.php b/src/PhpWord/Style/Cell.php
index 7fd5814d..c281f998 100644
--- a/src/PhpWord/Style/Cell.php
+++ b/src/PhpWord/Style/Cell.php
@@ -119,11 +119,11 @@ class Cell extends Border
private $width;
/**
- * Width type
+ * Width unit
*
* @var string
*/
- private $widthType = Table::WIDTH_TWIP;
+ private $unit = Table::WIDTH_TWIP;
/**
* Get vertical align.
@@ -292,23 +292,23 @@ class Cell extends Border
}
/**
- * Get width type
+ * Get width unit
*
* @return string
*/
- public function getWidthType()
+ public function getUnit()
{
- return $this->widthType;
+ return $this->unit;
}
/**
- * Set width type
+ * Set width unit
*
* @param string $value
*/
- public function setWidthType($value)
+ public function setUnit($value)
{
- $this->widthType = $this->setEnumVal($value, array(Table::WIDTH_AUTO, Table::WIDTH_PERCENT, Table::WIDTH_TWIP), Table::WIDTH_TWIP);
+ $this->unit = $this->setEnumVal($value, array(Table::WIDTH_AUTO, Table::WIDTH_PERCENT, Table::WIDTH_TWIP), Table::WIDTH_TWIP);
return $this;
}
diff --git a/src/PhpWord/Writer/AbstractWriter.php b/src/PhpWord/Writer/AbstractWriter.php
index 09a00990..50a0cad3 100644
--- a/src/PhpWord/Writer/AbstractWriter.php
+++ b/src/PhpWord/Writer/AbstractWriter.php
@@ -223,8 +223,8 @@ abstract class AbstractWriter implements WriterInterface
if (strtolower($filename) == 'php://output' || strtolower($filename) == 'php://stdout') {
$filename = tempnam(Settings::getTempDir(), 'PhpWord');
if (false === $filename) {
- $filename = $this->originalFilename;
- }
+ $filename = $this->originalFilename; // @codeCoverageIgnore
+ } // @codeCoverageIgnore
}
$this->tempFilename = $filename;
diff --git a/src/PhpWord/Writer/Word2007/Style/Cell.php b/src/PhpWord/Writer/Word2007/Style/Cell.php
index 82944d2c..b889aa55 100644
--- a/src/PhpWord/Writer/Word2007/Style/Cell.php
+++ b/src/PhpWord/Writer/Word2007/Style/Cell.php
@@ -45,10 +45,14 @@ class Cell extends AbstractStyle
$xmlWriter->startElement('w:tcPr');
// Width
- $xmlWriter->startElement('w:tcW');
- $xmlWriter->writeAttribute('w:w', $this->width);
- $xmlWriter->writeAttribute('w:type', 'dxa');
- $xmlWriter->endElement(); // w:tcW
+ if (!is_null($this->width) || !is_null($style->getWidth())) {
+ $width = is_null($this->width) ? $style->getWidth() : $this->width;
+
+ $xmlWriter->startElement('w:tcW');
+ $xmlWriter->writeAttribute('w:w', $width);
+ $xmlWriter->writeAttribute('w:type', $style->getUnit());
+ $xmlWriter->endElement(); // w:tcW
+ }
// Text direction
$textDir = $style->getTextDirection();
diff --git a/tests/PhpWord/Shared/ConverterTest.php b/tests/PhpWord/Shared/ConverterTest.php
index 28d68e17..49d5ef6e 100644
--- a/tests/PhpWord/Shared/ConverterTest.php
+++ b/tests/PhpWord/Shared/ConverterTest.php
@@ -122,6 +122,7 @@ class ConverterTest extends \PHPUnit\Framework\TestCase
public function testCssSizeParser()
{
$this->assertEquals(null, Converter::cssToPoint('10em'));
+ $this->assertEquals(0, Converter::cssToPoint('0'));
$this->assertEquals(10, Converter::cssToPoint('10pt'));
$this->assertEquals(7.5, Converter::cssToPoint('10px'));
$this->assertEquals(720, Converter::cssToPoint('10in'));
diff --git a/tests/PhpWord/Shared/HtmlTest.php b/tests/PhpWord/Shared/HtmlTest.php
index 39db0acf..58b0d977 100644
--- a/tests/PhpWord/Shared/HtmlTest.php
+++ b/tests/PhpWord/Shared/HtmlTest.php
@@ -190,8 +190,8 @@ class HtmlTest extends \PHPUnit\Framework\TestCase
$html = '
- | header a |
- header b |
+ header a |
+ header b |
header c |
@@ -206,4 +206,32 @@ class HtmlTest extends \PHPUnit\Framework\TestCase
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl'));
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr/w:tc'));
}
+
+ /**
+ * Tests parsing of ul/li
+ */
+ public function testParseList()
+ {
+ $phpWord = new \PhpOffice\PhpWord\PhpWord();
+ $section = $phpWord->addSection();
+ $html = '
+ -
+
+ list item1
+
+
+ -
+
+ list item2
+
+
+
';
+ Html::addHtml($section, $html, false, false);
+
+ $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
+ $this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:pPr/w:numPr/w:numId'));
+ $this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:t'));
+ $this->assertEquals('list item1', $doc->getElement('/w:document/w:body/w:p[1]/w:r/w:t')->nodeValue);
+ $this->assertEquals('list item2', $doc->getElement('/w:document/w:body/w:p[2]/w:r/w:t')->nodeValue);
+ }
}
diff --git a/tests/PhpWord/Writer/Word2007/ElementTest.php b/tests/PhpWord/Writer/Word2007/ElementTest.php
index f3c0d553..12f810ce 100644
--- a/tests/PhpWord/Writer/Word2007/ElementTest.php
+++ b/tests/PhpWord/Writer/Word2007/ElementTest.php
@@ -135,6 +135,25 @@ class ElementTest extends \PHPUnit\Framework\TestCase
$this->assertEquals('center', $doc->getElementAttribute($tableRootElement . '/w:tblPr/w:jc', 'w:val'));
}
+ /**
+ * Tests that the style name gets added
+ */
+ public function testTableWithStyleName()
+ {
+ $phpWord = new PhpWord();
+ $section = $phpWord->addSection();
+
+ $table = $section->addTable('my_predefined_style');
+ $table->setWidth(75);
+ $table->addRow(900);
+
+ $doc = TestHelperDOCX::getDocument($phpWord);
+
+ $tableRootElement = '/w:document/w:body/w:tbl';
+ $this->assertTrue($doc->elementExists($tableRootElement . '/w:tblPr/w:tblStyle'));
+ $this->assertEquals('my_predefined_style', $doc->getElementAttribute($tableRootElement . '/w:tblPr/w:tblStyle', 'w:val'));
+ }
+
/**
* Test shape elements
*/