Various fixes

- parse text inside list items
- add tests
- rename Cell widthType attribute to unit
This commit is contained in:
troosan 2017-11-18 15:55:05 +01:00
parent b22208f810
commit ac357d10d5
15 changed files with 96 additions and 45 deletions

View File

@ -415,7 +415,7 @@ Line elements can be added to sections by using ``addLine``.
.. code-block:: php .. 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) $section->addLine($lineStyle);
Available line style attributes: Available line style attributes:

View File

@ -14,11 +14,25 @@ $html .= '<ul><li>Item 1</li><li>Item 2</li><ul><li>Item 2.1</li><li>Item 2.1</l
$html .= '<p>Ordered (numbered) list:</p>'; $html .= '<p>Ordered (numbered) list:</p>';
$html .= '<ol><li>Item 1</li><li>Item 2</li></ol>'; $html .= '<ol><li>Item 1</li><li>Item 2</li></ol>';
$html .= '<p>List with complex content:</p>';
$html .= '<ul>
<li>
<span style="font-family: arial,helvetica,sans-serif;">
<span style="font-size: 12px;">list item1</span>
</span>
</li>
<li>
<span style="font-family: arial,helvetica,sans-serif;">
<span style="font-size: 12px;">list item2</span>
</span>
</li>
</ul>';
$html .= '<table style="width: 50%; border: 6px #0000FF double;"> $html .= '<table style="width: 50%; border: 6px #0000FF double;">
<thead> <thead>
<tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; "> <tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; ">
<th>header a</th> <th style="width: 50pt">header a</th>
<th>header b</th> <th style="width: 50">header b</th>
<th style="background-color: #FFFF00; border-width: 12px"><span style="background-color: #00FF00;">header c</span></th> <th style="background-color: #FFFF00; border-width: 12px"><span style="background-color: #00FF00;">header c</span></th>
</tr> </tr>
</thead> </thead>
@ -28,7 +42,7 @@ $html .= '<table style="width: 50%; border: 6px #0000FF double;">
</tbody> </tbody>
</table>'; </table>';
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html); \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);
// Save file // Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers); echo write($phpWord, basename(__FILE__, '.php'), $writers);

View File

@ -157,8 +157,6 @@ abstract class AbstractContainer extends AbstractElement
* Get all elements * Get all elements
* *
* @return array * @return array
*
* @codeCoverageIgnore
*/ */
public function getElements() public function getElements()
{ {

View File

@ -19,9 +19,6 @@ namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\Style\Paragraph; use PhpOffice\PhpWord\Style\Paragraph;
/**
* @codeCoverageIgnore
*/
class Footnote extends AbstractContainer class Footnote extends AbstractContainer
{ {
/** /**
@ -68,6 +65,7 @@ class Footnote extends AbstractContainer
* Get Footnote Reference ID * Get Footnote Reference ID
* *
* @deprecated 0.10.0 * @deprecated 0.10.0
* @codeCoverageIgnore
* *
* @return int * @return int
*/ */
@ -80,6 +78,7 @@ class Footnote extends AbstractContainer
* Set Footnote Reference ID * Set Footnote Reference ID
* *
* @deprecated 0.10.0 * @deprecated 0.10.0
* @codeCoverageIgnore
* *
* @param int $rId * @param int $rId
*/ */

View File

@ -85,8 +85,6 @@ class Section extends AbstractContainer
* Get section style * Get section style
* *
* @return \PhpOffice\PhpWord\Style\Section * @return \PhpOffice\PhpWord\Style\Section
*
* @codeCoverageIgnore
*/ */
public function getStyle() public function getStyle()
{ {
@ -125,8 +123,6 @@ class Section extends AbstractContainer
* Get header elements * Get header elements
* *
* @return Header[] * @return Header[]
*
* @codeCoverageIgnore
*/ */
public function getHeaders() public function getHeaders()
{ {
@ -137,8 +133,6 @@ class Section extends AbstractContainer
* Get footer elements * Get footer elements
* *
* @return Footer[] * @return Footer[]
*
* @codeCoverageIgnore
*/ */
public function getFooters() public function getFooters()
{ {

View File

@ -318,8 +318,6 @@ class Settings
* @since 0.13.0 * @since 0.13.0
* *
* @return bool * @return bool
*
* @codeCoverageIgnore
*/ */
public static function isOutputEscapingEnabled() public static function isOutputEscapingEnabled()
{ {
@ -330,8 +328,6 @@ class Settings
* @since 0.13.0 * @since 0.13.0
* *
* @param bool $outputEscapingEnabled * @param bool $outputEscapingEnabled
*
* @codeCoverageIgnore
*/ */
public static function setOutputEscapingEnabled($outputEscapingEnabled) public static function setOutputEscapingEnabled($outputEscapingEnabled)
{ {

View File

@ -37,8 +37,9 @@ class Html
* @param \PhpOffice\PhpWord\Element\AbstractContainer $element Where the parts need to be added * @param \PhpOffice\PhpWord\Element\AbstractContainer $element Where the parts need to be added
* @param string $html The code to parse * @param string $html The code to parse
* @param bool $fullHTML If it's a full HTML, no need to add 'body' tag * @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, * @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 // Load DOM
$dom = new \DOMDocument(); $dom = new \DOMDocument();
$dom->preserveWhiteSpace = true; $dom->preserveWhiteSpace = $preserveWhiteSpace;
$dom->loadXML($html); $dom->loadXML($html);
$node = $dom->getElementsByTagName('body'); $node = $dom->getElementsByTagName('body');
@ -395,6 +396,10 @@ class Html
$text = $cNode->nodeValue; $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']); $element->addListItem($text, $data['listdepth'], $styles['font'], $styles['list'], $styles['paragraph']);
} }
} }
@ -508,16 +513,13 @@ class Html
*/ */
private static function mapBorderStyle($cssBorderStyle) private static function mapBorderStyle($cssBorderStyle)
{ {
if ($cssBorderStyle == null) {
return null;
}
switch ($cssBorderStyle) { switch ($cssBorderStyle) {
case 'none': case 'none':
case 'dashed': case 'dashed':
case 'dotted': case 'dotted':
case 'double': case 'double':
return $cssBorderStyle; return $cssBorderStyle;
case 'solid': default:
return 'single'; return 'single';
} }
} }

View File

@ -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 \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 * @see http://www.datypic.com/sc/ooxml/t-w_ST_Jc.html
*
* @codeCoverageIgnore
*/ */
final class Jc extends AbstractEnum final class Jc extends AbstractEnum
{ {

View File

@ -25,8 +25,6 @@ use PhpOffice\PhpWord\Shared\AbstractEnum;
* Introduced in ISO/IEC-29500:2008. * Introduced in ISO/IEC-29500:2008.
* *
* @since 0.13.0 * @since 0.13.0
*
* @codeCoverageIgnore
*/ */
final class JcTable extends AbstractEnum final class JcTable extends AbstractEnum
{ {

View File

@ -119,11 +119,11 @@ class Cell extends Border
private $width; private $width;
/** /**
* Width type * Width unit
* *
* @var string * @var string
*/ */
private $widthType = Table::WIDTH_TWIP; private $unit = Table::WIDTH_TWIP;
/** /**
* Get vertical align. * Get vertical align.
@ -292,23 +292,23 @@ class Cell extends Border
} }
/** /**
* Get width type * Get width unit
* *
* @return string * @return string
*/ */
public function getWidthType() public function getUnit()
{ {
return $this->widthType; return $this->unit;
} }
/** /**
* Set width type * Set width unit
* *
* @param string $value * @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; return $this;
} }

View File

@ -223,8 +223,8 @@ abstract class AbstractWriter implements WriterInterface
if (strtolower($filename) == 'php://output' || strtolower($filename) == 'php://stdout') { if (strtolower($filename) == 'php://output' || strtolower($filename) == 'php://stdout') {
$filename = tempnam(Settings::getTempDir(), 'PhpWord'); $filename = tempnam(Settings::getTempDir(), 'PhpWord');
if (false === $filename) { if (false === $filename) {
$filename = $this->originalFilename; $filename = $this->originalFilename; // @codeCoverageIgnore
} } // @codeCoverageIgnore
} }
$this->tempFilename = $filename; $this->tempFilename = $filename;

View File

@ -45,10 +45,14 @@ class Cell extends AbstractStyle
$xmlWriter->startElement('w:tcPr'); $xmlWriter->startElement('w:tcPr');
// Width // Width
$xmlWriter->startElement('w:tcW'); if (!is_null($this->width) || !is_null($style->getWidth())) {
$xmlWriter->writeAttribute('w:w', $this->width); $width = is_null($this->width) ? $style->getWidth() : $this->width;
$xmlWriter->writeAttribute('w:type', 'dxa');
$xmlWriter->endElement(); // w:tcW $xmlWriter->startElement('w:tcW');
$xmlWriter->writeAttribute('w:w', $width);
$xmlWriter->writeAttribute('w:type', $style->getUnit());
$xmlWriter->endElement(); // w:tcW
}
// Text direction // Text direction
$textDir = $style->getTextDirection(); $textDir = $style->getTextDirection();

View File

@ -122,6 +122,7 @@ class ConverterTest extends \PHPUnit\Framework\TestCase
public function testCssSizeParser() public function testCssSizeParser()
{ {
$this->assertEquals(null, Converter::cssToPoint('10em')); $this->assertEquals(null, Converter::cssToPoint('10em'));
$this->assertEquals(0, Converter::cssToPoint('0'));
$this->assertEquals(10, Converter::cssToPoint('10pt')); $this->assertEquals(10, Converter::cssToPoint('10pt'));
$this->assertEquals(7.5, Converter::cssToPoint('10px')); $this->assertEquals(7.5, Converter::cssToPoint('10px'));
$this->assertEquals(720, Converter::cssToPoint('10in')); $this->assertEquals(720, Converter::cssToPoint('10in'));

View File

@ -190,8 +190,8 @@ class HtmlTest extends \PHPUnit\Framework\TestCase
$html = '<table style="width: 50%; border: 6px #0000FF solid;"> $html = '<table style="width: 50%; border: 6px #0000FF solid;">
<thead> <thead>
<tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; "> <tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; ">
<th>header a</th> <th style="width: 50pt">header a</th>
<th>header b</th> <th style="width: 50">header b</th>
<th style="border-color: #00FF00; border-width: 3px">header c</th> <th style="border-color: #00FF00; border-width: 3px">header c</th>
</tr> </tr>
</thead> </thead>
@ -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'));
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr/w:tc')); $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 = '<ul>
<li>
<span style="font-family: arial,helvetica,sans-serif;">
<span style="font-size: 12px;">list item1</span>
</span>
</li>
<li>
<span style="font-family: arial,helvetica,sans-serif;">
<span style="font-size: 12px;">list item2</span>
</span>
</li>
</ul>';
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);
}
} }

View File

@ -135,6 +135,25 @@ class ElementTest extends \PHPUnit\Framework\TestCase
$this->assertEquals('center', $doc->getElementAttribute($tableRootElement . '/w:tblPr/w:jc', 'w:val')); $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 * Test shape elements
*/ */