Skip to content

Commit 889f4e3

Browse files
committed
fix converting margin to incorrect unit (points instead of twips)
fix image alignment on float - relative to inner margin instead of page margin
1 parent 3066d47 commit 889f4e3

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

src/PhpWord/Shared/Html.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -633,11 +633,18 @@ protected static function parseStyle($attribute, $styles)
633633
}
634634
$styles['italic'] = $tValue;
635635
break;
636+
case 'margin':
637+
$cValue = Converter::cssToTwip($cValue);
638+
$styles['spaceBefore'] = $cValue;
639+
$styles['spaceAfter'] = $cValue;
640+
break;
636641
case 'margin-top':
637-
$styles['spaceBefore'] = Converter::cssToPoint($cValue);
642+
// BC change: up to ver. 0.17.0 incorrectly converted to points - Converter::cssToPoint($cValue)
643+
$styles['spaceBefore'] = Converter::cssToTwip($cValue);
638644
break;
639645
case 'margin-bottom':
640-
$styles['spaceAfter'] = Converter::cssToPoint($cValue);
646+
// BC change: up to ver. 0.17.0 incorrectly converted to points - Converter::cssToPoint($cValue)
647+
$styles['spaceAfter'] = Converter::cssToTwip($cValue);
641648
break;
642649
case 'border-color':
643650
self::mapBorderColor($styles, $cValue);
@@ -676,7 +683,7 @@ protected static function parseStyle($attribute, $styles)
676683
}
677684
// normalization: in HTML 1px means tinest possible line width, so we cannot convert 1px -> 15 twips, coz line'd be bold, we use smallest twip instead
678685
$size = strtolower(trim($matches[1]));
679-
// (!) BC change: up to ver. 0.17.0 Converter was incorrectly converting to points - Converter::cssToPoint($matches[1])
686+
// BC change: up to ver. 0.17.0 incorrectly converted to points - Converter::cssToPoint($size)
680687
$size = ($size == '1px') ? 1 : Converter::cssToTwip($size);
681688
// valid variants may be e.g. borderSize, borderTopSize, borderLeftColor, etc ..
682689
$styles["border{$which}Size"] = $size; // twips
@@ -732,14 +739,14 @@ protected static function parseImage($node, $element)
732739
case 'float':
733740
if (trim($v) == 'right') {
734741
$style['hPos'] = \PhpOffice\PhpWord\Style\Image::POS_RIGHT;
735-
$style['hPosRelTo'] = \PhpOffice\PhpWord\Style\Image::POS_RELTO_PAGE;
742+
$style['hPosRelTo'] = \PhpOffice\PhpWord\Style\Image::POS_RELTO_MARGIN; // inner section area
736743
$style['pos'] = \PhpOffice\PhpWord\Style\Image::POS_RELATIVE;
737744
$style['wrap'] = \PhpOffice\PhpWord\Style\Image::WRAP_TIGHT;
738745
$style['overlap'] = true;
739746
}
740747
if (trim($v) == 'left') {
741748
$style['hPos'] = \PhpOffice\PhpWord\Style\Image::POS_LEFT;
742-
$style['hPosRelTo'] = \PhpOffice\PhpWord\Style\Image::POS_RELTO_PAGE;
749+
$style['hPosRelTo'] = \PhpOffice\PhpWord\Style\Image::POS_RELTO_MARGIN; // inner section area
743750
$style['pos'] = \PhpOffice\PhpWord\Style\Image::POS_RELATIVE;
744751
$style['wrap'] = \PhpOffice\PhpWord\Style\Image::WRAP_TIGHT;
745752
$style['overlap'] = true;

tests/PhpWord/Shared/HtmlTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ public function testParseHorizRule()
779779

780780
$xpath = '/w:document/w:body/w:p[4]/w:pPr/w:spacing';
781781
$this->assertTrue($doc->elementExists($xpath));
782-
$this->assertEquals(22.5, $doc->getElement($xpath)->getAttribute('w:before'));
782+
$this->assertEquals(450, $doc->getElement($xpath)->getAttribute('w:before'));
783783
$this->assertEquals(0, $doc->getElement($xpath)->getAttribute('w:after'));
784784
$this->assertEquals(240, $doc->getElement($xpath)->getAttribute('w:line'));
785785
}
@@ -869,10 +869,6 @@ public function testParseVerticalAlign()
869869
Html::addHtml($section, $html);
870870
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
871871

872-
// uncomment to see results
873-
file_put_contents('./table_src.html', $html);
874-
file_put_contents('./table_result_'.time().'.docx', file_get_contents( TestHelperDOCX::getFile() ) );
875-
876872
$xpath = '/w:document/w:body/w:tbl/w:tr/w:tc[1]/w:tcPr/w:vAlign';
877873
$this->assertFalse($doc->elementExists($xpath));
878874

0 commit comments

Comments
 (0)