Improving Style Parsing - handling Heading style

This commit is contained in:
Humberto Pereira 2018-07-17 19:35:31 -04:00
parent 4c9e75088a
commit f5e3807654
2 changed files with 29 additions and 3 deletions

View File

@ -64,11 +64,11 @@ class Styles extends AbstractPart
if ($nodes->length > 0) {
foreach ($nodes as $node) {
$type = $xmlReader->getAttribute('w:type', $node);
$name = $xmlReader->getAttribute('w:styleId', $node);
if (is_null($name)) {
$name = $xmlReader->getAttribute('w:val', $node, 'w:name');
if (is_null($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':

View File

@ -19,6 +19,7 @@ namespace PhpOffice\PhpWord\Reader\Word2007;
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 @@ class StyleTest extends AbstractTestReader
$this->assertSame(TblWidth::TWIP, $tableStyle->getIndent()->getType());
$this->assertSame(2160, $tableStyle->getIndent()->getValue());
}
public function testReadHeading()
{
Style::resetStyles();
$documentXml = '<w:style w:type="paragraph" w:styleId="Ttulo1">
<w:name w:val="heading 1"/>
<w:basedOn w:val="Normal"/>
<w:uiPriority w:val="1"/>
<w:qFormat/>
<w:pPr>
<w:outlineLvl w:val="0"/>
</w:pPr>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:eastAsia="Times New Roman" w:hAnsi="Times New Roman"/>
<w:b/>
<w:bCs/>
</w:rPr>
</w:style>';
$name = 'Heading_1';
$this->getDocumentFromString(array('styles' => $documentXml));
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', Style::getStyle($name));
}
}