Skip to content

Commit

Permalink
Improving Style Parsing - handling Heading style
Browse files Browse the repository at this point in the history
  • Loading branch information
begnini committed Jul 17, 2018
1 parent 4c9e750 commit f5e3807
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/PhpWord/Reader/Word2007/Styles.php
Expand Up @@ -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':
Expand Down
26 changes: 26 additions & 0 deletions tests/PhpWord/Reader/Word2007/StyleTest.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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 = '<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));
}
}

0 comments on commit f5e3807

Please sign in to comment.