Skip to content

Commit 253b060

Browse files
committed
correctly parse on/off values (w:val="true|false|1|0|on|off")
1 parent 194e5c4 commit 253b060

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

src/PhpWord/Reader/Word2007/AbstractPart.php

Lines changed: 15 additions & 3 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 {
@@ -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)