Merge pull request #1221 from troosan/fix_parsing_of_on-off

correctly parse on/off values (w:val="true|false|1|0|on|off")
This commit is contained in:
troosan 2017-12-18 08:52:35 +01:00 committed by GitHub
commit 219f3bcee1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 29 additions and 5 deletions

View File

@ -42,6 +42,7 @@ This is the last version to support PHP 5.3
- Padded the $args array to remove error - @kaigoh #1150, @reformed #870 - Padded the $args array to remove error - @kaigoh #1150, @reformed #870
- Fix incorrect image size between windows and mac - @bskrtich #874 - Fix incorrect image size between windows and mac - @bskrtich #874
- Fix adding HTML table to document - @mogilvie @arivanbastos #324 - Fix adding HTML table to document - @mogilvie @arivanbastos #324
- Fix parsing on/off values (w:val="true|false|1|0|on|off") - @troosan #1221 #1219
### Deprecated ### Deprecated
- PhpWord->getProtection(), get it from the settings instead PhpWord->getSettings()->getDocumentProtection(); - PhpWord->getProtection(), get it from the settings instead PhpWord->getSettings()->getDocumentProtection();

View File

@ -19,6 +19,7 @@ namespace PhpOffice\PhpWord\Element;
/** /**
* Comment element * Comment element
* @see http://datypic.com/sc/ooxml/t-w_CT_Comment.html
*/ */
class Comment extends TrackChange class Comment extends TrackChange
{ {

View File

@ -19,6 +19,7 @@ namespace PhpOffice\PhpWord\Element;
/** /**
* TrackChange element * TrackChange element
* @see http://datypic.com/sc/ooxml/t-w_CT_TrackChange.html
*/ */
class TrackChange extends AbstractContainer class TrackChange extends AbstractContainer
{ {

View File

@ -223,7 +223,7 @@ abstract class AbstractPart
// $rIdIcon = $xmlReader->getAttribute('r:id', $domNode, 'w:object/v:shape/v:imagedata'); // $rIdIcon = $xmlReader->getAttribute('r:id', $domNode, 'w:object/v:shape/v:imagedata');
$target = $this->getMediaTarget($docPart, $rId); $target = $this->getMediaTarget($docPart, $rId);
if (!is_null($target)) { if (!is_null($target)) {
$textContent = "<Object: {$target}>"; $textContent = "&lt;Object: {$target}>";
$parent->addText($textContent, $fontStyle, $paragraphStyle); $parent->addText($textContent, $fontStyle, $paragraphStyle);
} }
} else { } else {
@ -384,7 +384,7 @@ abstract class AbstractPart
{ {
$style = null; $style = null;
$margins = array('top', 'left', 'bottom', 'right'); $margins = array('top', 'left', 'bottom', 'right');
$borders = $margins + array('insideH', 'insideV'); $borders = array_merge($margins, array('insideH', 'insideV'));
if ($xmlReader->elementExists('w:tblPr', $domNode)) { if ($xmlReader->elementExists('w:tblPr', $domNode)) {
if ($xmlReader->elementExists('w:tblPr/w:tblStyle', $domNode)) { if ($xmlReader->elementExists('w:tblPr/w:tblStyle', $domNode)) {
@ -422,7 +422,7 @@ abstract class AbstractPart
'textDirection' => array(self::READ_VALUE, 'w:textDirection'), 'textDirection' => array(self::READ_VALUE, 'w:textDirection'),
'gridSpan' => array(self::READ_VALUE, 'w:gridSpan'), 'gridSpan' => array(self::READ_VALUE, 'w:gridSpan'),
'vMerge' => array(self::READ_VALUE, 'w:vMerge'), 'vMerge' => array(self::READ_VALUE, 'w:vMerge'),
'bgColor' => array(self::READ_VALUE, 'w:shd/w:fill'), 'bgColor' => array(self::READ_VALUE, 'w:shd', 'w:fill'),
); );
return $this->readStyleDefs($xmlReader, $domNode, $styleDefs); return $this->readStyleDefs($xmlReader, $domNode, $styleDefs);
@ -477,9 +477,9 @@ abstract class AbstractPart
if (self::READ_SIZE == $method) { if (self::READ_SIZE == $method) {
$style = $attributeValue / 2; $style = $attributeValue / 2;
} elseif (self::READ_TRUE == $method) { } elseif (self::READ_TRUE == $method) {
$style = true; $style = $this->isOn($attributeValue);
} elseif (self::READ_FALSE == $method) { } elseif (self::READ_FALSE == $method) {
$style = false; $style = !$this->isOn($attributeValue);
} elseif (self::READ_EQUAL == $method) { } elseif (self::READ_EQUAL == $method) {
$style = $attributeValue == $expected; $style = $attributeValue == $expected;
} }
@ -487,6 +487,18 @@ abstract class AbstractPart
return $style; return $style;
} }
/**
* Parses the value of the on/off value, null is considered true as it means the w:val attribute was not present
*
* @see http://www.datypic.com/sc/ooxml/t-w_ST_OnOff.html
* @param string $value
* @return bool
*/
private function isOn($value = null)
{
return $value == null || $value == '1' || $value == 'true' || $value == 'on';
}
/** /**
* Returns the target of image, object, or link as stored in ::readMainRels * Returns the target of image, object, or link as stored in ::readMainRels
* *

View File

@ -18,6 +18,7 @@
namespace PhpOffice\PhpWord\Reader; namespace PhpOffice\PhpWord\Reader;
use PhpOffice\PhpWord\IOFactory; use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\TestHelperDOCX;
/** /**
* Test class for PhpOffice\PhpWord\Reader\Word2007 * Test class for PhpOffice\PhpWord\Reader\Word2007
@ -54,6 +55,13 @@ class Word2007Test extends \PHPUnit\Framework\TestCase
{ {
$filename = __DIR__ . '/../_files/documents/reader.docx'; $filename = __DIR__ . '/../_files/documents/reader.docx';
$phpWord = IOFactory::load($filename); $phpWord = IOFactory::load($filename);
$this->assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $phpWord); $this->assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $phpWord);
$this->assertTrue($phpWord->getSettings()->hasDoNotTrackMoves());
$this->assertFalse($phpWord->getSettings()->hasDoNotTrackFormatting());
$this->assertEquals(100, $phpWord->getSettings()->getZoom());
$doc = TestHelperDOCX::getDocument($phpWord);
$this->assertFalse($doc->elementExists('/w:document/w:body/w:p/w:r[w:t/node()="italics"]/w:rPr/w:b'));
} }
} }

View File

@ -97,6 +97,7 @@ class XmlDocument
if (null === $this->xpath) { if (null === $this->xpath) {
$this->xpath = new \DOMXpath($this->dom); $this->xpath = new \DOMXpath($this->dom);
$this->xpath->registerNamespace('w14', 'http://schemas.microsoft.com/office/word/2010/wordml');
} }
return $this->xpath->query($path); return $this->xpath->query($path);