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. All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/). 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) v0.14.0 (29 Dec 2017)
---------------------- ----------------------
This release fixes several bugs and adds some new features. This release fixes several bugs and adds some new features.

View File

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

View File

@ -85,6 +85,8 @@ class Html
case 'style': case 'style':
$styles = self::parseStyle($attribute, $styles); $styles = self::parseStyle($attribute, $styles);
break; break;
case 'align':
$styles['alignment'] = self::mapAlign($attribute->value);
} }
} }
} }
@ -431,20 +433,7 @@ class Html
} }
break; break;
case 'text-align': case 'text-align':
switch ($cValue) { $styles['alignment'] = self::mapAlign($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;
}
break; break;
case 'font-size': case 'font-size':
$styles['size'] = Converter::cssToPoint($cValue); $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 * Parse line break
* *

View File

@ -187,7 +187,7 @@ class HtmlTest extends \PHPUnit\Framework\TestCase
{ {
$phpWord = new \PhpOffice\PhpWord\PhpWord(); $phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection(); $section = $phpWord->addSection();
$html = '<table style="width: 50%; border: 6px #0000FF solid;"> $html = '<table align="left" style="width: 50%; border: 6px #0000FF solid;">
<thead> <thead>
<tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; "> <tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; ">
<th style="width: 50pt">header a</th> <th style="width: 50pt">header a</th>
@ -205,6 +205,8 @@ class HtmlTest extends \PHPUnit\Framework\TestCase
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); $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'));
$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: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'));
} }
/** /**