Skip to content

Commit a01d22e

Browse files
committed
improve HTML parser and add tests
1 parent e724464 commit a01d22e

File tree

14 files changed

+187
-28
lines changed

14 files changed

+187
-28
lines changed

src/PhpWord/Element/Image.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ private function getArchiveImageSize($source)
449449

450450
$tempFilename = tempnam(Settings::getTempDir(), 'PHPWordImage');
451451
if (false === $tempFilename) {
452-
throw new CreateTemporaryFileException();
452+
throw new CreateTemporaryFileException(); // @codeCoverageIgnore
453453
}
454454

455455
$zip = new ZipArchive();

src/PhpWord/Shared/Converter.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Converter
2626
const INCH_TO_TWIP = 1440;
2727
const INCH_TO_PIXEL = 96;
2828
const INCH_TO_POINT = 72;
29+
const INCH_TO_PICA = 6;
2930
const PIXEL_TO_EMU = 9525;
3031
const DEGREE_TO_ANGLE = 60000;
3132

@@ -227,6 +228,17 @@ public static function emuToPixel($emu = 1)
227228
return round($emu / self::PIXEL_TO_EMU);
228229
}
229230

231+
/**
232+
* Convert pica to point
233+
*
234+
* @param int $pica
235+
* @return float
236+
*/
237+
public static function picaToPoint($pica = 1)
238+
{
239+
return $pica / self::INCH_TO_PICA * self::INCH_TO_POINT;
240+
}
241+
230242
/**
231243
* Convert degree to angle
232244
*
@@ -275,4 +287,34 @@ public static function htmlToRgb($value)
275287

276288
return array($red, $green, $blue);
277289
}
290+
291+
/**
292+
* Transforms a size in CSS format (eg. 10px, 10px, ...) to points
293+
*
294+
* @param string $value
295+
* @return float
296+
*/
297+
public static function cssToPoint($value)
298+
{
299+
preg_match('/^[+-]?([0-9]+.?[0-9]+)?(px|em|ex|%|in|cm|mm|pt|pc)$/i', $value, $matches);
300+
$size = $matches[1];
301+
$unit = $matches[2];
302+
303+
switch ($unit) {
304+
case 'pt':
305+
return $size;
306+
case 'px':
307+
return self::pixelToPoint($size);
308+
case 'cm':
309+
return self::cmToPoint($size);
310+
case 'mm':
311+
return self::cmToPoint($size / 10);
312+
case 'in':
313+
return self::inchToPoint($size);
314+
case 'pc':
315+
return self::picaToPoint($size);
316+
default:
317+
return null;
318+
}
319+
}
278320
}

src/PhpWord/Shared/Html.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,9 @@ private static function parseText($node, $element, &$styles)
233233
{
234234
$styles['font'] = self::parseInlineStyle($node, $styles['font']);
235235

236-
// Commented as source of bug #257. `method_exists` doesn't seems to work properly in this case.
237-
// @todo Find better error checking for this one
238-
// if (method_exists($element, 'addText')) {
239-
$element->addText($node->nodeValue, $styles['font'], $styles['paragraph']);
240-
// }
236+
if (is_callable(array($element, 'addText'))) {
237+
$element->addText($node->nodeValue, $styles['font'], $styles['paragraph']);
238+
}
241239
}
242240

243241
/**
@@ -375,6 +373,13 @@ private static function parseStyle($attribute, $styles)
375373
break;
376374
}
377375
break;
376+
case 'font-size':
377+
$styles['size'] = Converter::cssToPoint($cValue);
378+
break;
379+
case 'font-family':
380+
$cValue = array_map('trim', explode(',', $cValue));
381+
$styles['name'] = ucwords($cValue[0]);
382+
break;
378383
case 'color':
379384
$styles['color'] = trim($cValue, '#');
380385
break;

src/PhpWord/Shared/OLERead.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,18 @@ public function read($sFileName)
110110

111111
$bbdBlocks = $this->numBigBlockDepotBlocks;
112112

113+
// @codeCoverageIgnoreStart
113114
if ($this->numExtensionBlocks != 0) {
114115
$bbdBlocks = (self::BIG_BLOCK_SIZE - self::BIG_BLOCK_DEPOT_BLOCKS_POS)/4;
115116
}
116-
117+
// @codeCoverageIgnoreEnd
118+
117119
for ($i = 0; $i < $bbdBlocks; ++$i) {
118120
$bigBlockDepotBlocks[$i] = self::getInt4d($this->data, $pos);
119121
$pos += 4;
120122
}
121123

124+
// @codeCoverageIgnoreStart
122125
for ($j = 0; $j < $this->numExtensionBlocks; ++$j) {
123126
$pos = ($this->extensionBlock + 1) * self::BIG_BLOCK_SIZE;
124127
$blocksToRead = min($this->numBigBlockDepotBlocks - $bbdBlocks, self::BIG_BLOCK_SIZE / 4 - 1);
@@ -133,6 +136,7 @@ public function read($sFileName)
133136
$this->extensionBlock = self::getInt4d($this->data, $pos);
134137
}
135138
}
139+
// @codeCoverageIgnoreEnd
136140

137141
$pos = 0;
138142
$this->bigBlockChain = '';
@@ -196,7 +200,7 @@ public function getStream($stream)
196200
}
197201

198202
if ($numBlocks == 0) {
199-
return '';
203+
return '';// @codeCoverageIgnore
200204
}
201205

202206
$block = $this->props[$stream]['startBlock'];

src/PhpWord/Writer/HTML/Style/Paragraph.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ public function write()
4444
$textAlign = '';
4545

4646
switch ($style->getAlignment()) {
47-
case Jc::START:
48-
case Jc::NUM_TAB:
49-
case Jc::LEFT:
50-
$textAlign = 'left';
51-
break;
5247
case Jc::CENTER:
5348
$textAlign = 'center';
5449
break;
@@ -65,7 +60,7 @@ public function write()
6560
case Jc::JUSTIFY:
6661
$textAlign = 'justify';
6762
break;
68-
default:
63+
default: //all others, align left
6964
$textAlign = 'left';
7065
break;
7166
}

src/PhpWord/Writer/Word2007/Element/AbstractElement.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,21 @@ protected function writeCommentRangeEnd()
139139
{
140140
if ($this->element->getCommentRangeEnd() != null) {
141141
$comment = $this->element->getCommentRangeEnd();
142-
//only set the ID if it is not yet set, otherwise it will overwrite it
142+
//only set the ID if it is not yet set, otherwise it will overwrite it, this should normally not happen
143143
if ($comment->getElementId() == null) {
144-
$comment->setElementId();
145-
}
144+
$comment->setElementId(); // @codeCoverageIgnore
145+
} // @codeCoverageIgnore
146146

147147
$this->xmlWriter->writeElementBlock('w:commentRangeEnd', array('w:id' => $comment->getElementId()));
148148
$this->xmlWriter->startElement('w:r');
149149
$this->xmlWriter->writeElementBlock('w:commentReference', array('w:id' => $comment->getElementId()));
150150
$this->xmlWriter->endElement();
151151
} elseif ($this->element->getCommentRangeStart() != null && $this->element->getCommentRangeStart()->getEndElement() == null) {
152152
$comment = $this->element->getCommentRangeStart();
153-
//only set the ID if it is not yet set, otherwise it will overwrite it
153+
//only set the ID if it is not yet set, otherwise it will overwrite it, this should normally not happen
154154
if ($comment->getElementId() == null) {
155-
$comment->setElementId();
156-
}
155+
$comment->setElementId(); // @codeCoverageIgnore
156+
} // @codeCoverageIgnore
157157

158158
$this->xmlWriter->writeElementBlock('w:commentRangeEnd', array('w:id' => $comment->getElementId()));
159159
$this->xmlWriter->startElement('w:r');

src/PhpWord/Writer/Word2007/Part/Comments.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ protected function writeComment(XMLWriter $xmlWriter, Comment $comment)
7878
$xmlWriter->startElement('w:comment');
7979
$xmlWriter->writeAttribute('w:id', $comment->getElementId());
8080
$xmlWriter->writeAttribute('w:author', $comment->getAuthor());
81-
$xmlWriter->writeAttribute('w:date', $comment->getDate()->format($this->dateFormat));
82-
$xmlWriter->writeAttribute('w:initials', $comment->getInitials());
81+
if ($comment->getDate() != null) {
82+
$xmlWriter->writeAttribute('w:date', $comment->getDate()->format($this->dateFormat));
83+
}
84+
$xmlWriter->writeAttributeIf($comment->getInitials() != null, 'w:initials', $comment->getInitials());
8385

8486
$containerWriter = new Container($xmlWriter, $comment);
8587
$containerWriter->write();

src/PhpWord/Writer/Word2007/Style/Font.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ private function writeStyle()
5959
if (!$style instanceof \PhpOffice\PhpWord\Style\Font) {
6060
return;
6161
}
62+
6263
$xmlWriter = $this->getXmlWriter();
6364

6465
$xmlWriter->startElement('w:rPr');

tests/PhpWord/Element/ImageTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ public function testConstructFromString()
206206
$this->assertEquals('imagecreatefromstring', $image->getImageCreateFunction());
207207
$this->assertEquals('imagejpeg', $image->getImageFunction());
208208
$this->assertTrue($image->isMemImage());
209+
210+
$this->assertNotNull($image->getImageStringData());
211+
$this->assertNotNull($image->getImageStringData(true));
209212
}
210213

211214
/**

tests/PhpWord/Shared/ConverterTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ public function testUnitConversions()
8888
$result = Converter::emuToPixel($value);
8989
$this->assertEquals(round($value / 9525), $result);
9090

91+
$result = Converter::picaToPoint($value);
92+
$this->assertEquals($value / 6 * 72, $result, '', 0.00001);
93+
9194
$result = Converter::degreeToAngle($value);
9295
$this->assertEquals((int) round($value * 60000), $result);
9396

@@ -112,4 +115,18 @@ public function testHtmlToRGB()
112115
$this->assertEquals($value[1], $result);
113116
}
114117
}
118+
119+
/**
120+
* Test css size to point
121+
*/
122+
public function testCssSizeParser()
123+
{
124+
$this->assertEquals(null, Converter::cssToPoint('10em'));
125+
$this->assertEquals(10, Converter::cssToPoint('10pt'));
126+
$this->assertEquals(7.5, Converter::cssToPoint('10px'));
127+
$this->assertEquals(720, Converter::cssToPoint('10in'));
128+
$this->assertEquals(120, Converter::cssToPoint('10pc'));
129+
$this->assertEquals(28.346457, Converter::cssToPoint('10mm'), '', 0.000001);
130+
$this->assertEquals(283.464567, Converter::cssToPoint('10cm'), '', 0.000001);
131+
}
115132
}

0 commit comments

Comments
 (0)