Skip to content

Commit 219f3bc

Browse files
authored
Merge pull request #1221 from troosan/fix_parsing_of_on-off
correctly parse on/off values (w:val="true|false|1|0|on|off")
2 parents 194e5c4 + dc7cb1e commit 219f3bc

File tree

7 files changed

+29
-5
lines changed

7 files changed

+29
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ This is the last version to support PHP 5.3
4242
- Padded the $args array to remove error - @kaigoh #1150, @reformed #870
4343
- Fix incorrect image size between windows and mac - @bskrtich #874
4444
- Fix adding HTML table to document - @mogilvie @arivanbastos #324
45+
- Fix parsing on/off values (w:val="true|false|1|0|on|off") - @troosan #1221 #1219
4546

4647
### Deprecated
4748
- PhpWord->getProtection(), get it from the settings instead PhpWord->getSettings()->getDocumentProtection();

src/PhpWord/Element/Comment.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
/**
2121
* Comment element
22+
* @see http://datypic.com/sc/ooxml/t-w_CT_Comment.html
2223
*/
2324
class Comment extends TrackChange
2425
{

src/PhpWord/Element/TrackChange.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
/**
2121
* TrackChange element
22+
* @see http://datypic.com/sc/ooxml/t-w_CT_TrackChange.html
2223
*/
2324
class TrackChange extends AbstractContainer
2425
{

src/PhpWord/Reader/Word2007/AbstractPart.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ protected function readRun(XMLReader $xmlReader, \DOMElement $domNode, $parent,
223223
// $rIdIcon = $xmlReader->getAttribute('r:id', $domNode, 'w:object/v:shape/v:imagedata');
224224
$target = $this->getMediaTarget($docPart, $rId);
225225
if (!is_null($target)) {
226-
$textContent = "<Object: {$target}>";
226+
$textContent = "&lt;Object: {$target}>";
227227
$parent->addText($textContent, $fontStyle, $paragraphStyle);
228228
}
229229
} else {
@@ -384,7 +384,7 @@ protected function readTableStyle(XMLReader $xmlReader, \DOMElement $domNode)
384384
{
385385
$style = null;
386386
$margins = array('top', 'left', 'bottom', 'right');
387-
$borders = $margins + array('insideH', 'insideV');
387+
$borders = array_merge($margins, array('insideH', 'insideV'));
388388

389389
if ($xmlReader->elementExists('w:tblPr', $domNode)) {
390390
if ($xmlReader->elementExists('w:tblPr/w:tblStyle', $domNode)) {
@@ -422,7 +422,7 @@ private function readCellStyle(XMLReader $xmlReader, \DOMElement $domNode)
422422
'textDirection' => array(self::READ_VALUE, 'w:textDirection'),
423423
'gridSpan' => array(self::READ_VALUE, 'w:gridSpan'),
424424
'vMerge' => array(self::READ_VALUE, 'w:vMerge'),
425-
'bgColor' => array(self::READ_VALUE, 'w:shd/w:fill'),
425+
'bgColor' => array(self::READ_VALUE, 'w:shd', 'w:fill'),
426426
);
427427

428428
return $this->readStyleDefs($xmlReader, $domNode, $styleDefs);
@@ -477,16 +477,28 @@ private function readStyleDef($method, $attributeValue, $expected)
477477
if (self::READ_SIZE == $method) {
478478
$style = $attributeValue / 2;
479479
} elseif (self::READ_TRUE == $method) {
480-
$style = true;
480+
$style = $this->isOn($attributeValue);
481481
} elseif (self::READ_FALSE == $method) {
482-
$style = false;
482+
$style = !$this->isOn($attributeValue);
483483
} elseif (self::READ_EQUAL == $method) {
484484
$style = $attributeValue == $expected;
485485
}
486486

487487
return $style;
488488
}
489489

490+
/**
491+
* Parses the value of the on/off value, null is considered true as it means the w:val attribute was not present
492+
*
493+
* @see http://www.datypic.com/sc/ooxml/t-w_ST_OnOff.html
494+
* @param string $value
495+
* @return bool
496+
*/
497+
private function isOn($value = null)
498+
{
499+
return $value == null || $value == '1' || $value == 'true' || $value == 'on';
500+
}
501+
490502
/**
491503
* Returns the target of image, object, or link as stored in ::readMainRels
492504
*

tests/PhpWord/Reader/Word2007Test.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace PhpOffice\PhpWord\Reader;
1919

2020
use PhpOffice\PhpWord\IOFactory;
21+
use PhpOffice\PhpWord\TestHelperDOCX;
2122

2223
/**
2324
* Test class for PhpOffice\PhpWord\Reader\Word2007
@@ -54,6 +55,13 @@ public function testLoad()
5455
{
5556
$filename = __DIR__ . '/../_files/documents/reader.docx';
5657
$phpWord = IOFactory::load($filename);
58+
5759
$this->assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $phpWord);
60+
$this->assertTrue($phpWord->getSettings()->hasDoNotTrackMoves());
61+
$this->assertFalse($phpWord->getSettings()->hasDoNotTrackFormatting());
62+
$this->assertEquals(100, $phpWord->getSettings()->getZoom());
63+
64+
$doc = TestHelperDOCX::getDocument($phpWord);
65+
$this->assertFalse($doc->elementExists('/w:document/w:body/w:p/w:r[w:t/node()="italics"]/w:rPr/w:b'));
5866
}
5967
}
180 Bytes
Binary file not shown.

tests/PhpWord/_includes/XmlDocument.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public function getNodeList($path, $file = 'word/document.xml')
9797

9898
if (null === $this->xpath) {
9999
$this->xpath = new \DOMXpath($this->dom);
100+
$this->xpath->registerNamespace('w14', 'http://schemas.microsoft.com/office/word/2010/wordml');
100101
}
101102

102103
return $this->xpath->query($path);

0 commit comments

Comments
 (0)