From f5e38076543b989b62e572cf6d5f57f9e22e3950 Mon Sep 17 00:00:00 2001 From: Humberto Pereira Date: Tue, 17 Jul 2018 19:35:31 -0400 Subject: [PATCH] Improving Style Parsing - handling Heading style --- src/PhpWord/Reader/Word2007/Styles.php | 6 ++--- tests/PhpWord/Reader/Word2007/StyleTest.php | 26 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/PhpWord/Reader/Word2007/Styles.php b/src/PhpWord/Reader/Word2007/Styles.php index f343ad9296..140a3f09a6 100644 --- a/src/PhpWord/Reader/Word2007/Styles.php +++ b/src/PhpWord/Reader/Word2007/Styles.php @@ -64,11 +64,11 @@ public function read(PhpWord $phpWord) if ($nodes->length > 0) { foreach ($nodes as $node) { $type = $xmlReader->getAttribute('w:type', $node); - $name = $xmlReader->getAttribute('w:styleId', $node); + $name = $xmlReader->getAttribute('w:val', $node, 'w:name'); if (is_null($name)) { - $name = $xmlReader->getAttribute('w:val', $node, 'w:name'); + $name = $xmlReader->getAttribute('w:styleId', $node); } - preg_match('/Heading(\d)/', $name, $headingMatches); + preg_match('/Heading\s*(\d)/i', $name, $headingMatches); // $default = ($xmlReader->getAttribute('w:default', $node) == 1); switch ($type) { case 'paragraph': diff --git a/tests/PhpWord/Reader/Word2007/StyleTest.php b/tests/PhpWord/Reader/Word2007/StyleTest.php index d64079fa0e..561067ee94 100644 --- a/tests/PhpWord/Reader/Word2007/StyleTest.php +++ b/tests/PhpWord/Reader/Word2007/StyleTest.php @@ -19,6 +19,7 @@ use PhpOffice\PhpWord\AbstractTestReader; use PhpOffice\PhpWord\SimpleType\TblWidth; +use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style\Table; use PhpOffice\PhpWord\Style\TablePosition; @@ -145,4 +146,29 @@ public function testReadIndent() $this->assertSame(TblWidth::TWIP, $tableStyle->getIndent()->getType()); $this->assertSame(2160, $tableStyle->getIndent()->getValue()); } + + public function testReadHeading() + { + Style::resetStyles(); + + $documentXml = ' + + + + + + + + + + + + + '; + + $name = 'Heading_1'; + + $this->getDocumentFromString(array('styles' => $documentXml)); + $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', Style::getStyle($name)); + } }