Html parser (addHtml) - support attributes start, type in ordered list <ol>
This commit is contained in:
parent
ca5f081302
commit
108c1cdc55
|
|
@ -447,7 +447,31 @@ class Html
|
|||
} else {
|
||||
$data['listdepth'] = 0;
|
||||
$styles['list'] = 'listStyle_' . self::$listIndex++;
|
||||
$element->getPhpWord()->addNumberingStyle($styles['list'], self::getListStyle($isOrderedList));
|
||||
$style = $element->getPhpWord()->addNumberingStyle($styles['list'], self::getListStyle($isOrderedList));
|
||||
|
||||
// extract attributes start & type e.g. <ol type="A" start="3">
|
||||
$start = 0;
|
||||
$type = '';
|
||||
foreach ($node->attributes as $attribute) {
|
||||
switch ($attribute->name) {
|
||||
case 'start':
|
||||
$start = (int) $attribute->value;
|
||||
break;
|
||||
case 'type':
|
||||
$type = $attribute->value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$levels = $style->getLevels();
|
||||
/** @var \PhpOffice\PhpWord\Style\NumberingLevel */
|
||||
$level = $levels[0];
|
||||
if($start > 0){
|
||||
$level->setStart($start);
|
||||
}
|
||||
if($type && !!($type = self::mapListType($type))){
|
||||
$level->setFormat($type);
|
||||
}
|
||||
}
|
||||
if ($node->parentNode->nodeName === 'li') {
|
||||
return $element->getParent();
|
||||
|
|
@ -818,6 +842,28 @@ class Html
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Map list style for ordered list
|
||||
*
|
||||
* @param string $cssListType
|
||||
*/
|
||||
protected static function mapListType($cssListType)
|
||||
{
|
||||
switch ($cssListType) {
|
||||
case 'a':
|
||||
return NumberFormat::LOWER_LETTER; // a, b, c, ..
|
||||
case 'A':
|
||||
return NumberFormat::UPPER_LETTER; // A, B, C, ..
|
||||
case 'i':
|
||||
return NumberFormat::LOWER_ROMAN; // i, ii, iii, iv, ..
|
||||
case 'I':
|
||||
return NumberFormat::UPPER_ROMAN; // I, II, III, IV, ..
|
||||
case '1':
|
||||
default:
|
||||
return NumberFormat::DECIMAL; // 1, 2, 3, ..
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse line break
|
||||
*
|
||||
|
|
|
|||
|
|
@ -784,4 +784,64 @@ HTML;
|
|||
$this->assertEquals(240, $doc->getElement($xpath)->getAttribute('w:line'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse ordered list start & numbering style
|
||||
*/
|
||||
public function testParseOrderedList()
|
||||
{
|
||||
$phpWord = new \PhpOffice\PhpWord\PhpWord();
|
||||
$section = $phpWord->addSection();
|
||||
|
||||
// borders & backgrounds are here just for better visual comparison
|
||||
$html = <<<HTML
|
||||
<ol>
|
||||
<li>standard ordered list line 1</li>
|
||||
<li>standard ordered list line 2</li>
|
||||
</ol>
|
||||
|
||||
<ol start="5" type="A">
|
||||
<li>ordered list alphabetical, <span style="background-color: #EEEEEE; color: #FF0000;">line 5 => E</span></li>
|
||||
<li>ordered list alphabetical, <span style="background-color: #EEEEEE; color: #FF0000;">line 6 => F</span></li>
|
||||
</ol>
|
||||
|
||||
<ol start="3" type="i">
|
||||
<li>ordered list roman lower, line <b>3 => iii</b></li>
|
||||
<li>ordered list roman lower, line <b>4 => iv</b></li>
|
||||
</ol>
|
||||
|
||||
HTML;
|
||||
|
||||
Html::addHtml($section, $html);
|
||||
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
|
||||
|
||||
// compare numbering file
|
||||
$xmlFile = 'word/numbering.xml';
|
||||
|
||||
// default - decimal start = 1
|
||||
$xpath = '/w:numbering/w:abstractNum[1]/w:lvl[1]/w:start';
|
||||
$this->assertTrue($doc->elementExists($xpath, $xmlFile));
|
||||
$this->assertEquals('1', $doc->getElement($xpath, $xmlFile)->getAttribute('w:val'));
|
||||
|
||||
$xpath = '/w:numbering/w:abstractNum[1]/w:lvl[1]/w:numFmt';
|
||||
$this->assertTrue($doc->elementExists($xpath, $xmlFile));
|
||||
$this->assertEquals('decimal', $doc->getElement($xpath, $xmlFile)->getAttribute('w:val'));
|
||||
|
||||
// second list - start = 5, type A = upperLetter
|
||||
$xpath = '/w:numbering/w:abstractNum[2]/w:lvl[1]/w:start';
|
||||
$this->assertTrue($doc->elementExists($xpath, $xmlFile));
|
||||
$this->assertEquals('5', $doc->getElement($xpath, $xmlFile)->getAttribute('w:val'));
|
||||
|
||||
$xpath = '/w:numbering/w:abstractNum[2]/w:lvl[1]/w:numFmt';
|
||||
$this->assertTrue($doc->elementExists($xpath, $xmlFile));
|
||||
$this->assertEquals('upperLetter', $doc->getElement($xpath, $xmlFile)->getAttribute('w:val'));
|
||||
|
||||
// third list - start = 3, type i = lowerRoman
|
||||
$xpath = '/w:numbering/w:abstractNum[3]/w:lvl[1]/w:start';
|
||||
$this->assertTrue($doc->elementExists($xpath, $xmlFile));
|
||||
$this->assertEquals('3', $doc->getElement($xpath, $xmlFile)->getAttribute('w:val'));
|
||||
|
||||
$xpath = '/w:numbering/w:abstractNum[3]/w:lvl[1]/w:numFmt';
|
||||
$this->assertTrue($doc->elementExists($xpath, $xmlFile));
|
||||
$this->assertEquals('lowerRoman', $doc->getElement($xpath, $xmlFile)->getAttribute('w:val'));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue