Skip to content

Commit ac357d1

Browse files
committed
Various fixes
- parse text inside list items - add tests - rename Cell widthType attribute to unit
1 parent b22208f commit ac357d1

File tree

15 files changed

+96
-45
lines changed

15 files changed

+96
-45
lines changed

docs/elements.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ Line elements can be added to sections by using ``addLine``.
415415
.. code-block:: php
416416
417417
$lineStyle = array('weight' => 1, 'width' => 100, 'height' => 0, 'color' => 635552);
418-
$section->addLine($lineStyle)
418+
$section->addLine($lineStyle);
419419
420420
Available line style attributes:
421421

samples/Sample_26_Html.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,25 @@
1414
$html .= '<p>Ordered (numbered) list:</p>';
1515
$html .= '<ol><li>Item 1</li><li>Item 2</li></ol>';
1616

17+
$html .= '<p>List with complex content:</p>';
18+
$html .= '<ul>
19+
<li>
20+
<span style="font-family: arial,helvetica,sans-serif;">
21+
<span style="font-size: 12px;">list item1</span>
22+
</span>
23+
</li>
24+
<li>
25+
<span style="font-family: arial,helvetica,sans-serif;">
26+
<span style="font-size: 12px;">list item2</span>
27+
</span>
28+
</li>
29+
</ul>';
30+
1731
$html .= '<table style="width: 50%; border: 6px #0000FF double;">
1832
<thead>
1933
<tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; ">
20-
<th>header a</th>
21-
<th>header b</th>
34+
<th style="width: 50pt">header a</th>
35+
<th style="width: 50">header b</th>
2236
<th style="background-color: #FFFF00; border-width: 12px"><span style="background-color: #00FF00;">header c</span></th>
2337
</tr>
2438
</thead>
@@ -28,7 +42,7 @@
2842
</tbody>
2943
</table>';
3044

31-
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html);
45+
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);
3246

3347
// Save file
3448
echo write($phpWord, basename(__FILE__, '.php'), $writers);

src/PhpWord/Element/AbstractContainer.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,6 @@ protected function addElement($elementName)
157157
* Get all elements
158158
*
159159
* @return array
160-
*
161-
* @codeCoverageIgnore
162160
*/
163161
public function getElements()
164162
{

src/PhpWord/Element/Footnote.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919

2020
use PhpOffice\PhpWord\Style\Paragraph;
2121

22-
/**
23-
* @codeCoverageIgnore
24-
*/
2522
class Footnote extends AbstractContainer
2623
{
2724
/**
@@ -68,6 +65,7 @@ public function getParagraphStyle()
6865
* Get Footnote Reference ID
6966
*
7067
* @deprecated 0.10.0
68+
* @codeCoverageIgnore
7169
*
7270
* @return int
7371
*/
@@ -80,6 +78,7 @@ public function getReferenceId()
8078
* Set Footnote Reference ID
8179
*
8280
* @deprecated 0.10.0
81+
* @codeCoverageIgnore
8382
*
8483
* @param int $rId
8584
*/

src/PhpWord/Element/Section.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ public function setStyle($style = null)
8585
* Get section style
8686
*
8787
* @return \PhpOffice\PhpWord\Style\Section
88-
*
89-
* @codeCoverageIgnore
9088
*/
9189
public function getStyle()
9290
{
@@ -125,8 +123,6 @@ public function addFooter($type = Header::AUTO)
125123
* Get header elements
126124
*
127125
* @return Header[]
128-
*
129-
* @codeCoverageIgnore
130126
*/
131127
public function getHeaders()
132128
{
@@ -137,8 +133,6 @@ public function getHeaders()
137133
* Get footer elements
138134
*
139135
* @return Footer[]
140-
*
141-
* @codeCoverageIgnore
142136
*/
143137
public function getFooters()
144138
{

src/PhpWord/Settings.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,6 @@ public static function getTempDir()
318318
* @since 0.13.0
319319
*
320320
* @return bool
321-
*
322-
* @codeCoverageIgnore
323321
*/
324322
public static function isOutputEscapingEnabled()
325323
{
@@ -330,8 +328,6 @@ public static function isOutputEscapingEnabled()
330328
* @since 0.13.0
331329
*
332330
* @param bool $outputEscapingEnabled
333-
*
334-
* @codeCoverageIgnore
335331
*/
336332
public static function setOutputEscapingEnabled($outputEscapingEnabled)
337333
{

src/PhpWord/Shared/Html.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ class Html
3737
* @param \PhpOffice\PhpWord\Element\AbstractContainer $element Where the parts need to be added
3838
* @param string $html The code to parse
3939
* @param bool $fullHTML If it's a full HTML, no need to add 'body' tag
40+
* @param bool $preserveWhiteSpace If false, the whitespaces between nodes will be removed
4041
*/
41-
public static function addHtml($element, $html, $fullHTML = false)
42+
public static function addHtml($element, $html, $fullHTML = false, $preserveWhiteSpace = true)
4243
{
4344
/*
4445
* @todo parse $stylesheet for default styles. Should result in an array based on id, class and element,
@@ -59,7 +60,7 @@ public static function addHtml($element, $html, $fullHTML = false)
5960

6061
// Load DOM
6162
$dom = new \DOMDocument();
62-
$dom->preserveWhiteSpace = true;
63+
$dom->preserveWhiteSpace = $preserveWhiteSpace;
6364
$dom->loadXML($html);
6465
$node = $dom->getElementsByTagName('body');
6566

@@ -395,6 +396,10 @@ private static function parseListItem($node, $element, &$styles, $data)
395396
$text = $cNode->nodeValue;
396397
}
397398
}
399+
//ideally we should be parsing child nodes for any style, for now just take the text
400+
if ('' == trim($text) && '' != trim($node->textContent)) {
401+
$text = trim($node->textContent);
402+
}
398403
$element->addListItem($text, $data['listdepth'], $styles['font'], $styles['list'], $styles['paragraph']);
399404
}
400405
}
@@ -508,16 +513,13 @@ private static function parseStyle($attribute, $styles)
508513
*/
509514
private static function mapBorderStyle($cssBorderStyle)
510515
{
511-
if ($cssBorderStyle == null) {
512-
return null;
513-
}
514516
switch ($cssBorderStyle) {
515517
case 'none':
516518
case 'dashed':
517519
case 'dotted':
518520
case 'double':
519521
return $cssBorderStyle;
520-
case 'solid':
522+
default:
521523
return 'single';
522524
}
523525
}

src/PhpWord/SimpleType/Jc.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
*
3030
* @see \PhpOffice\PhpWord\SimpleType\JcTable For table alignment modes available since ISO/IEC-29500:2008.
3131
* @see http://www.datypic.com/sc/ooxml/t-w_ST_Jc.html
32-
*
33-
* @codeCoverageIgnore
3432
*/
3533
final class Jc extends AbstractEnum
3634
{

src/PhpWord/SimpleType/JcTable.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
* Introduced in ISO/IEC-29500:2008.
2626
*
2727
* @since 0.13.0
28-
*
29-
* @codeCoverageIgnore
3028
*/
3129
final class JcTable extends AbstractEnum
3230
{

src/PhpWord/Style/Cell.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ class Cell extends Border
119119
private $width;
120120

121121
/**
122-
* Width type
122+
* Width unit
123123
*
124124
* @var string
125125
*/
126-
private $widthType = Table::WIDTH_TWIP;
126+
private $unit = Table::WIDTH_TWIP;
127127

128128
/**
129129
* Get vertical align.
@@ -292,23 +292,23 @@ public function setWidth($value)
292292
}
293293

294294
/**
295-
* Get width type
295+
* Get width unit
296296
*
297297
* @return string
298298
*/
299-
public function getWidthType()
299+
public function getUnit()
300300
{
301-
return $this->widthType;
301+
return $this->unit;
302302
}
303303

304304
/**
305-
* Set width type
305+
* Set width unit
306306
*
307307
* @param string $value
308308
*/
309-
public function setWidthType($value)
309+
public function setUnit($value)
310310
{
311-
$this->widthType = $this->setEnumVal($value, array(Table::WIDTH_AUTO, Table::WIDTH_PERCENT, Table::WIDTH_TWIP), Table::WIDTH_TWIP);
311+
$this->unit = $this->setEnumVal($value, array(Table::WIDTH_AUTO, Table::WIDTH_PERCENT, Table::WIDTH_TWIP), Table::WIDTH_TWIP);
312312

313313
return $this;
314314
}

0 commit comments

Comments
 (0)