Skip to content

Commit d2b9e88

Browse files
committed
add parsing of "align" HTML attribute
1 parent fd7ee76 commit d2b9e88

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ Change Log
33
All notable changes to this project will be documented in this file.
44
This project adheres to [Semantic Versioning](http://semver.org/).
55

6+
v0.15.0 (?? ??? 2018)
7+
----------------------
8+
### Added
9+
- Parsing of "align" HTML attribute - @troosan
10+
11+
### Fixed
12+
613
v0.14.0 (29 Dec 2017)
714
----------------------
815
This release fixes several bugs and adds some new features.

samples/Sample_26_Html.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</li>
2929
</ul>';
3030

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

src/PhpWord/Shared/Html.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ protected static function parseInlineStyle($node, $styles = array())
8585
case 'style':
8686
$styles = self::parseStyle($attribute, $styles);
8787
break;
88+
case 'align':
89+
$styles['alignment'] = self::mapAlign($attribute->value);
8890
}
8991
}
9092
}
@@ -431,20 +433,7 @@ private static function parseStyle($attribute, $styles)
431433
}
432434
break;
433435
case 'text-align':
434-
switch ($cValue) {
435-
case 'left':
436-
$styles['alignment'] = Jc::START;
437-
break;
438-
case 'right':
439-
$styles['alignment'] = Jc::END;
440-
break;
441-
case 'center':
442-
$styles['alignment'] = Jc::CENTER;
443-
break;
444-
case 'justify':
445-
$styles['alignment'] = Jc::BOTH;
446-
break;
447-
}
436+
$styles['alignment'] = self::mapAlign($cValue);
448437
break;
449438
case 'font-size':
450439
$styles['size'] = Converter::cssToPoint($cValue);
@@ -584,6 +573,28 @@ private static function mapBorderStyle($cssBorderStyle)
584573
}
585574
}
586575

576+
/**
577+
* Transforms a HTML/CSS alignment into a \PhpOffice\PhpWord\SimpleType\Jc
578+
*
579+
* @param string $cssAlignment
580+
* @return string|null
581+
*/
582+
private static function mapAlign($cssAlignment)
583+
{
584+
switch ($cssAlignment) {
585+
case 'left':
586+
return Jc::START;
587+
case 'right':
588+
return Jc::END;
589+
case 'center':
590+
return Jc::CENTER;
591+
case 'justify':
592+
return Jc::BOTH;
593+
}
594+
595+
return null;
596+
}
597+
587598
/**
588599
* Parse line break
589600
*

tests/PhpWord/Shared/HtmlTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public function testParseTable()
187187
{
188188
$phpWord = new \PhpOffice\PhpWord\PhpWord();
189189
$section = $phpWord->addSection();
190-
$html = '<table style="width: 50%; border: 6px #0000FF solid;">
190+
$html = '<table align="left" style="width: 50%; border: 6px #0000FF solid;">
191191
<thead>
192192
<tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; ">
193193
<th style="width: 50pt">header a</th>
@@ -205,6 +205,8 @@ public function testParseTable()
205205
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
206206
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl'));
207207
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr/w:tc'));
208+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tblPr/w:jc'));
209+
$this->assertEquals(Jc::START, $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tblPr/w:jc', 'w:val'));
208210
}
209211

210212
/**

0 commit comments

Comments
 (0)