add parsing of "align" HTML attribute

This commit is contained in:
troosan 2017-12-29 14:36:07 +01:00
parent fd7ee76438
commit d2b9e88047
4 changed files with 36 additions and 16 deletions

View File

@ -3,6 +3,13 @@ Change Log
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).
v0.15.0 (?? ??? 2018)
----------------------
### Added
- Parsing of "align" HTML attribute - @troosan
### Fixed
v0.14.0 (29 Dec 2017)
----------------------
This release fixes several bugs and adds some new features.

View File

@ -28,7 +28,7 @@ $html .= '<ul>
</li>
</ul>';
$html .= '<table style="width: 50%; border: 6px #0000FF double;">
$html .= '<table align="center" style="width: 50%; border: 6px #0000FF double;">
<thead>
<tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; ">
<th style="width: 50pt">header a</th>

View File

@ -85,6 +85,8 @@ class Html
case 'style':
$styles = self::parseStyle($attribute, $styles);
break;
case 'align':
$styles['alignment'] = self::mapAlign($attribute->value);
}
}
}
@ -431,20 +433,7 @@ class Html
}
break;
case 'text-align':
switch ($cValue) {
case 'left':
$styles['alignment'] = Jc::START;
break;
case 'right':
$styles['alignment'] = Jc::END;
break;
case 'center':
$styles['alignment'] = Jc::CENTER;
break;
case 'justify':
$styles['alignment'] = Jc::BOTH;
break;
}
$styles['alignment'] = self::mapAlign($cValue);
break;
case 'font-size':
$styles['size'] = Converter::cssToPoint($cValue);
@ -584,6 +573,28 @@ class Html
}
}
/**
* Transforms a HTML/CSS alignment into a \PhpOffice\PhpWord\SimpleType\Jc
*
* @param string $cssAlignment
* @return string|null
*/
private static function mapAlign($cssAlignment)
{
switch ($cssAlignment) {
case 'left':
return Jc::START;
case 'right':
return Jc::END;
case 'center':
return Jc::CENTER;
case 'justify':
return Jc::BOTH;
}
return null;
}
/**
* Parse line break
*

View File

@ -187,7 +187,7 @@ class HtmlTest extends \PHPUnit\Framework\TestCase
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = '<table style="width: 50%; border: 6px #0000FF solid;">
$html = '<table align="left" style="width: 50%; border: 6px #0000FF solid;">
<thead>
<tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; ">
<th style="width: 50pt">header a</th>
@ -205,6 +205,8 @@ class HtmlTest extends \PHPUnit\Framework\TestCase
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl'));
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr/w:tc'));
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tblPr/w:jc'));
$this->assertEquals(Jc::START, $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tblPr/w:jc', 'w:val'));
}
/**