Skip to content

Commit f5e3807

Browse files
committed
Improving Style Parsing - handling Heading style
1 parent 4c9e750 commit f5e3807

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/PhpWord/Reader/Word2007/Styles.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ public function read(PhpWord $phpWord)
6464
if ($nodes->length > 0) {
6565
foreach ($nodes as $node) {
6666
$type = $xmlReader->getAttribute('w:type', $node);
67-
$name = $xmlReader->getAttribute('w:styleId', $node);
67+
$name = $xmlReader->getAttribute('w:val', $node, 'w:name');
6868
if (is_null($name)) {
69-
$name = $xmlReader->getAttribute('w:val', $node, 'w:name');
69+
$name = $xmlReader->getAttribute('w:styleId', $node);
7070
}
71-
preg_match('/Heading(\d)/', $name, $headingMatches);
71+
preg_match('/Heading\s*(\d)/i', $name, $headingMatches);
7272
// $default = ($xmlReader->getAttribute('w:default', $node) == 1);
7373
switch ($type) {
7474
case 'paragraph':

tests/PhpWord/Reader/Word2007/StyleTest.php

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

2020
use PhpOffice\PhpWord\AbstractTestReader;
2121
use PhpOffice\PhpWord\SimpleType\TblWidth;
22+
use PhpOffice\PhpWord\Style;
2223
use PhpOffice\PhpWord\Style\Table;
2324
use PhpOffice\PhpWord\Style\TablePosition;
2425

@@ -145,4 +146,29 @@ public function testReadIndent()
145146
$this->assertSame(TblWidth::TWIP, $tableStyle->getIndent()->getType());
146147
$this->assertSame(2160, $tableStyle->getIndent()->getValue());
147148
}
149+
150+
public function testReadHeading()
151+
{
152+
Style::resetStyles();
153+
154+
$documentXml = '<w:style w:type="paragraph" w:styleId="Ttulo1">
155+
<w:name w:val="heading 1"/>
156+
<w:basedOn w:val="Normal"/>
157+
<w:uiPriority w:val="1"/>
158+
<w:qFormat/>
159+
<w:pPr>
160+
<w:outlineLvl w:val="0"/>
161+
</w:pPr>
162+
<w:rPr>
163+
<w:rFonts w:ascii="Times New Roman" w:eastAsia="Times New Roman" w:hAnsi="Times New Roman"/>
164+
<w:b/>
165+
<w:bCs/>
166+
</w:rPr>
167+
</w:style>';
168+
169+
$name = 'Heading_1';
170+
171+
$this->getDocumentFromString(array('styles' => $documentXml));
172+
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', Style::getStyle($name));
173+
}
148174
}

0 commit comments

Comments
 (0)