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
$lineStyle = array('weight' => 1, 'width' => 100, 'height' => 0, 'color' => 635552);
$section->addLine($lineStyle)
$section->addLine($lineStyle);
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 .= '<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;">
<thead>
<tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; ">
<th>header a</th>
<th>header b</th>
<th style="width: 50pt">header a</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>
</tr>
</thead>
@ -28,7 +42,7 @@ $html .= '<table style="width: 50%; border: 6px #0000FF double;">
</tbody>
</table>';
\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);

View File

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

View File

@ -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
*/

View File

@ -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()
{

View File

@ -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)
{

View File

@ -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';
}
}

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

View File

@ -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
{

View File

@ -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;
}

View File

@ -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;

View File

@ -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();

View File

@ -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'));

View File

@ -190,8 +190,8 @@ class HtmlTest extends \PHPUnit\Framework\TestCase
$html = '<table style="width: 50%; border: 6px #0000FF solid;">
<thead>
<tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; ">
<th>header a</th>
<th>header b</th>
<th style="width: 50pt">header a</th>
<th style="width: 50">header b</th>
<th style="border-color: #00FF00; border-width: 3px">header c</th>
</tr>
</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/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'));
}
/**
* 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
*/