PHPWord/tests/PhpWord/Shared/HtmlTest.php

925 lines
85 KiB
PHP
Raw Normal View History

2014-05-10 23:40:34 +07:00
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
2018-03-08 23:52:25 +01:00
* @copyright 2010-2018 PHPWord contributors
2014-05-10 23:40:34 +07:00
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
2015-11-15 13:33:05 +04:00
namespace PhpOffice\PhpWord\Shared;
2014-05-10 23:40:34 +07:00
use PhpOffice\PhpWord\AbstractWebServerEmbeddedTest;
2014-05-10 23:40:34 +07:00
use PhpOffice\PhpWord\Element\Section;
2017-11-09 00:41:56 +01:00
use PhpOffice\PhpWord\SimpleType\Jc;
2018-03-19 22:49:00 +01:00
use PhpOffice\PhpWord\SimpleType\LineSpacingRule;
use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWord\TestHelperDOCX;
2014-05-10 23:40:34 +07:00
/**
* Test class for PhpOffice\PhpWord\Shared\Html
2017-11-09 00:41:56 +01:00
* @coversDefaultClass \PhpOffice\PhpWord\Shared\Html
2014-05-10 23:40:34 +07:00
*/
class HtmlTest extends AbstractWebServerEmbeddedTest
2014-05-10 23:40:34 +07:00
{
/**
* Test unit conversion functions with various numbers
*/
public function testAddHtml()
{
$content = '';
// Default
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$this->assertCount(0, $section->getElements());
2014-05-10 23:40:34 +07:00
// Heading
$styles = array('strong', 'em', 'sup', 'sub');
for ($level = 1; $level <= 6; $level++) {
$content .= "<h{$level}>Heading {$level}</h{$level}>";
}
// Styles
$content .= '<p style="text-decoration: underline; text-decoration: line-through; '
2017-11-09 00:41:56 +01:00
. 'text-align: center; color: #999; background-color: #000; font-weight: bold; font-style: italic;">';
2014-05-10 23:40:34 +07:00
foreach ($styles as $style) {
$content .= "<{$style}>{$style}</{$style}>";
}
$content .= '</p>';
// Add HTML
Html::addHtml($section, $content);
$this->assertCount(7, $section->getElements());
2014-05-10 23:40:34 +07:00
// Other parts
$section = $phpWord->addSection();
2014-05-10 23:40:34 +07:00
$content = '';
$content .= '<table><tr><th>Header</th><td>Content</td></tr></table>';
$content .= '<ul><li>Bullet</li><ul><li>Bullet</li></ul></ul>';
2014-05-10 23:40:34 +07:00
$content .= '<ol><li>Bullet</li></ol>';
$content .= "'Single Quoted Text'";
$content .= '"Double Quoted Text"';
$content .= '& Ampersand';
$content .= '&lt;&gt;&ldquo;&lsquo;&rsquo;&laquo;&raquo;&lsaquo;&rsaquo;';
$content .= '&amp;&bull;&deg;&hellip;&trade;&copy;&reg;&mdash;';
$content .= '&ndash;&nbsp;&emsp;&ensp;&sup2;&sup3;&frac14;&frac12;&frac34;';
Html::addHtml($section, $content);
2014-05-10 23:40:34 +07:00
}
2017-11-09 00:41:56 +01:00
/**
* Test that html already in body element can be read
* @ignore
*/
public function testParseFullHtml()
{
$section = new Section(1);
Html::addHtml($section, '<body><p>test paragraph1</p><p>test paragraph2</p></body>', true);
$this->assertCount(2, $section->getElements());
}
/**
* Test HTML entities
*/
public function testParseHtmlEntities()
{
\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
Html::addHtml($section, 'text with entities &lt;my text&gt;');
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p[1]/w:r/w:t'));
$this->assertEquals('text with entities <my text>', $doc->getElement('/w:document/w:body/w:p[1]/w:r/w:t')->nodeValue);
}
2017-11-09 00:41:56 +01:00
/**
* Test underline
*/
public function testParseUnderline()
{
$html = '<u>test</u>';
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:u'));
$this->assertEquals('single', $doc->getElementAttribute('/w:document/w:body/w:p/w:r/w:rPr/w:u', 'w:val'));
}
/**
* Test text-decoration style
*/
public function testParseTextDecoration()
{
$html = '<span style="text-decoration: underline;">test</span>';
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:u'));
$this->assertEquals('single', $doc->getElementAttribute('/w:document/w:body/w:p/w:r/w:rPr/w:u', 'w:val'));
}
2018-05-23 18:48:28 +02:00
2018-05-23 18:22:54 +02:00
/**
* Test font
*/
public function testParseFont()
{
$html = '<font style="font-family: Arial;">test</font>';
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr'));
//TODO check style
}
2017-11-09 00:41:56 +01:00
/**
* Test line-height style
*/
public function testParseLineHeight()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
Html::addHtml($section, '<p style="line-height: 1.5;">test</p>');
2018-03-19 22:49:00 +01:00
Html::addHtml($section, '<p style="line-height: 15pt;">test</p>');
2018-04-14 22:42:58 +02:00
Html::addHtml($section, '<p style="line-height: 120%;">test</p>');
Html::addHtml($section, '<p style="line-height: 0.17in;">test</p>');
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
2018-03-19 22:49:00 +01:00
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p[1]/w:pPr/w:spacing'));
$this->assertEquals(Paragraph::LINE_HEIGHT * 1.5, $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:pPr/w:spacing', 'w:line'));
$this->assertEquals(LineSpacingRule::AUTO, $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:pPr/w:spacing', 'w:lineRule'));
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p[2]/w:pPr/w:spacing'));
$this->assertEquals(300, $doc->getElementAttribute('/w:document/w:body/w:p[2]/w:pPr/w:spacing', 'w:line'));
$this->assertEquals(LineSpacingRule::EXACT, $doc->getElementAttribute('/w:document/w:body/w:p[2]/w:pPr/w:spacing', 'w:lineRule'));
2018-04-14 22:42:58 +02:00
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p[3]/w:pPr/w:spacing'));
$this->assertEquals(Paragraph::LINE_HEIGHT * 1.2, $doc->getElementAttribute('/w:document/w:body/w:p[3]/w:pPr/w:spacing', 'w:line'));
$this->assertEquals(LineSpacingRule::AUTO, $doc->getElementAttribute('/w:document/w:body/w:p[3]/w:pPr/w:spacing', 'w:lineRule'));
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p[4]/w:pPr/w:spacing'));
$this->assertEquals(244.8, $doc->getElementAttribute('/w:document/w:body/w:p[4]/w:pPr/w:spacing', 'w:line'));
$this->assertEquals(LineSpacingRule::EXACT, $doc->getElementAttribute('/w:document/w:body/w:p[4]/w:pPr/w:spacing', 'w:lineRule'));
}
/**
* Test text-indent style
*/
public function testParseTextIndent()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
Html::addHtml($section, '<p style="text-indent: 50px;">test</p>');
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:pPr/w:ind'));
$this->assertEquals(750, $doc->getElementAttribute('/w:document/w:body/w:p/w:pPr/w:ind', 'w:firstLine'));
}
2017-11-09 00:41:56 +01:00
/**
* Test text-align style
*/
public function testParseTextAlign()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
Html::addHtml($section, '<p style="text-align: left;">test</p>');
Html::addHtml($section, '<p style="text-align: right;">test</p>');
Html::addHtml($section, '<p style="text-align: center;">test</p>');
Html::addHtml($section, '<p style="text-align: justify;">test</p>');
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:pPr/w:jc'));
2017-11-11 23:49:23 +01:00
$this->assertEquals(Jc::START, $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:pPr/w:jc', 'w:val'));
$this->assertEquals(Jc::END, $doc->getElementAttribute('/w:document/w:body/w:p[2]/w:pPr/w:jc', 'w:val'));
$this->assertEquals(Jc::CENTER, $doc->getElementAttribute('/w:document/w:body/w:p[3]/w:pPr/w:jc', 'w:val'));
$this->assertEquals(Jc::BOTH, $doc->getElementAttribute('/w:document/w:body/w:p[4]/w:pPr/w:jc', 'w:val'));
}
/**
* Test font-size style
*/
public function testParseFontSize()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
Html::addHtml($section, '<span style="font-size: 10pt;">test</span>');
Html::addHtml($section, '<span style="font-size: 10px;">test</span>');
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:sz'));
$this->assertEquals('20', $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:r/w:rPr/w:sz', 'w:val'));
$this->assertEquals('15', $doc->getElementAttribute('/w:document/w:body/w:p[2]/w:r/w:rPr/w:sz', 'w:val'));
}
2018-02-06 23:31:56 +01:00
/**
* Test direction style
*/
public function testParseTextDirection()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
Html::addHtml($section, '<span style="direction: rtl">test</span>');
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:rtl'));
}
/**
* Test html lang
*/
public function testParseLang()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
Html::addHtml($section, '<span lang="fr-BE">test</span>');
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:lang'));
$this->assertEquals('fr-BE', $doc->getElementAttribute('/w:document/w:body/w:p/w:r/w:rPr/w:lang', 'w:val'));
}
2017-11-11 23:49:23 +01:00
/**
* Test font-family style
*/
public function testParseFontFamily()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
Html::addHtml($section, '<span style="font-family: Arial">test</span>');
Html::addHtml($section, '<span style="font-family: Times New Roman;">test</span>');
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:rFonts'));
$this->assertEquals('Arial', $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:r/w:rPr/w:rFonts', 'w:ascii'));
$this->assertEquals('Times New Roman', $doc->getElementAttribute('/w:document/w:body/w:p[2]/w:r/w:rPr/w:rFonts', 'w:ascii'));
2017-11-09 00:41:56 +01:00
}
/**
* Test parsing paragraph and span styles
*/
public function testParseParagraphAndSpanStyle()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
Html::addHtml($section, '<p style="text-align: center; margin-top: 15px; margin-bottom: 15px;"><span style="text-decoration: underline;">test</span></p>');
2017-11-09 00:41:56 +01:00
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:pPr/w:jc'));
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:pPr/w:spacing'));
2017-11-11 23:49:23 +01:00
$this->assertEquals(Jc::CENTER, $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:pPr/w:jc', 'w:val'));
2017-11-09 00:41:56 +01:00
$this->assertEquals('single', $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:r/w:rPr/w:u', 'w:val'));
}
/**
* Test parsing table
*/
public function testParseTable()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
2017-12-29 14:36:07 +01:00
$html = '<table align="left" style="width: 50%; border: 6px #0000FF solid;">
2017-11-09 00:41:56 +01:00
<thead>
<tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; ">
<th style="width: 50pt">header a</th>
2019-02-06 22:07:42 +01:00
<th style="width: 50; border-color: #00EE00">header b</th>
<th style="border-color: #00AA00 #00BB00 #00CC00 #00DD00; border-width: 3px">header c</th>
2017-11-09 00:41:56 +01:00
</tr>
</thead>
<tbody>
2017-11-15 22:49:13 +01:00
<tr><td style="border-style: dotted;">1</td><td colspan="2">2</td></tr>
2018-02-06 23:31:56 +01:00
<tr><td>This is <b>bold</b> text</td><td>5</td><td><p>6</p></td></tr>
2017-11-09 00:41:56 +01:00
</tbody>
</table>';
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
2017-11-15 22:49:13 +01:00
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl'));
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr/w:tc'));
2017-12-29 14:36:07 +01:00
$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'));
2019-02-06 22:07:42 +01:00
//check border colors
2019-02-06 22:07:42 +01:00
$this->assertEquals('00EE00', $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]/w:tcPr/w:tcBorders/w:top', 'w:color'));
$this->assertEquals('00EE00', $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]/w:tcPr/w:tcBorders/w:right', 'w:color'));
$this->assertEquals('00EE00', $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]/w:tcPr/w:tcBorders/w:bottom', 'w:color'));
$this->assertEquals('00EE00', $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]/w:tcPr/w:tcBorders/w:left', 'w:color'));
$this->assertEquals('00AA00', $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr[1]/w:tc[3]/w:tcPr/w:tcBorders/w:top', 'w:color'));
$this->assertEquals('00BB00', $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr[1]/w:tc[3]/w:tcPr/w:tcBorders/w:right', 'w:color'));
$this->assertEquals('00CC00', $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr[1]/w:tc[3]/w:tcPr/w:tcBorders/w:bottom', 'w:color'));
$this->assertEquals('00DD00', $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr[1]/w:tc[3]/w:tcPr/w:tcBorders/w:left', 'w:color'));
2017-11-09 00:41:56 +01:00
}
/**
* Tests parsing of ul/li
*/
public function testParseList()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = '<ul>
<li>
<span style="font-family: arial,helvetica,sans-serif;">
<span style="font-size: 12px;">list item1</span>
</span>
</li>
<li>
<span style="font-family: arial,helvetica,sans-serif;">
<span style="font-size: 10px; font-weight: bold;">list item2</span>
</span>
</li>
</ul>';
Html::addHtml($section, $html, false, false);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:pPr/w:numPr/w:numId'));
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:t'));
$this->assertEquals('list item1', $doc->getElement('/w:document/w:body/w:p[1]/w:r/w:t')->nodeValue);
$this->assertEquals('list item2', $doc->getElement('/w:document/w:body/w:p[2]/w:r/w:t')->nodeValue);
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:b'));
}
/**
* Tests parsing of ul/li
*/
2018-02-01 14:12:56 +01:00
public function testOrderedListNumbering()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = '<ol>
<li>List 1 item 1</li>
<li>List 1 item 2</li>
</ol>
<p>Some Text</p>
<ol>
<li>List 2 item 1</li>
<li>List 2 item 2</li>
</ol>';
Html::addHtml($section, $html, false, false);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:pPr/w:numPr/w:numId'));
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:t'));
$this->assertEquals('List 1 item 1', $doc->getElement('/w:document/w:body/w:p[1]/w:r/w:t')->nodeValue);
$this->assertEquals('List 2 item 1', $doc->getElement('/w:document/w:body/w:p[4]/w:r/w:t')->nodeValue);
$firstListnumId = $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:pPr/w:numPr/w:numId', 'w:val');
$secondListnumId = $doc->getElementAttribute('/w:document/w:body/w:p[4]/w:pPr/w:numPr/w:numId', 'w:val');
$this->assertNotEquals($firstListnumId, $secondListnumId);
}
/**
* Tests parsing of nested ul/li
*/
public function testOrderedNestedListNumbering()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = '<ol>
<li>List 1 item 1</li>
<li>List 1 item 2</li>
</ol>
<p>Some Text</p>
<ol>
<li>List 2 item 1</li>
<li>
<ol>
<li>sub list 1</li>
<li>sub list 2</li>
</ol>
</li>
</ol>';
Html::addHtml($section, $html, false, false);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
2018-02-08 07:18:02 +01:00
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:pPr/w:numPr/w:numId'));
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:t'));
$this->assertEquals('List 1 item 1', $doc->getElement('/w:document/w:body/w:p[1]/w:r/w:t')->nodeValue);
$this->assertEquals('List 2 item 1', $doc->getElement('/w:document/w:body/w:p[4]/w:r/w:t')->nodeValue);
$firstListnumId = $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:pPr/w:numPr/w:numId', 'w:val');
$secondListnumId = $doc->getElementAttribute('/w:document/w:body/w:p[4]/w:pPr/w:numPr/w:numId', 'w:val');
$this->assertNotEquals($firstListnumId, $secondListnumId);
}
/**
* Tests parsing of ul/li
*/
public function testParseListWithFormat()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = preg_replace('/\s+/', ' ', '<ul>
<li>Some text before
<span style="font-family: arial,helvetica,sans-serif;">
<span style="font-size: 12px;">list item1 <b>bold</b> with text after bold</span>
</span>
and some after
</li>
<li>
<span style="font-family: arial,helvetica,sans-serif;">
<span style="font-size: 12px;">list item2</span>
</span>
</li>
</ul>');
Html::addHtml($section, $html, false, false);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:pPr/w:numPr/w:numId'));
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:t'));
$this->assertEquals('list item2', $doc->getElement('/w:document/w:body/w:p[2]/w:r/w:t')->nodeValue);
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p[1]/w:r[3]/w:rPr/w:b'));
$this->assertEquals('bold', $doc->getElement('/w:document/w:body/w:p[1]/w:r[3]/w:t')->nodeValue);
}
/**
* Tests parsing of br
*/
public function testParseLineBreak()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = '<p>This is some text<br/>with a linebreak.</p>';
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:br'));
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:t'));
$this->assertEquals('This is some text', $doc->getElement('/w:document/w:body/w:p/w:r[1]/w:t')->nodeValue);
$this->assertEquals('with a linebreak.', $doc->getElement('/w:document/w:body/w:p/w:r[2]/w:t')->nodeValue);
}
/**
* Test parsing of img
*/
public function testParseImage()
{
$src = __DIR__ . '/../_files/images/firefox.png';
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = '<p><img src="' . $src . '" width="150" height="200" style="float: right;"/><img src="' . $src . '" style="float: left;"/></p>';
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$baseXpath = '/w:document/w:body/w:p/w:r';
$this->assertTrue($doc->elementExists($baseXpath . '/w:pict/v:shape'));
$this->assertStringMatchesFormat('%Swidth:150px%S', $doc->getElementAttribute($baseXpath . '[1]/w:pict/v:shape', 'style'));
$this->assertStringMatchesFormat('%Sheight:200px%S', $doc->getElementAttribute($baseXpath . '[1]/w:pict/v:shape', 'style'));
$this->assertStringMatchesFormat('%Smso-position-horizontal:right%S', $doc->getElementAttribute($baseXpath . '[1]/w:pict/v:shape', 'style'));
$this->assertStringMatchesFormat('%Smso-position-horizontal:left%S', $doc->getElementAttribute($baseXpath . '[2]/w:pict/v:shape', 'style'));
}
2018-01-25 23:24:21 +01:00
2018-05-23 18:22:54 +02:00
/**
* Test parsing of remote img
*/
public function testParseRemoteImage()
{
$src = self::getRemoteImageUrl();
2018-05-23 18:22:54 +02:00
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = '<p><img src="' . $src . '" width="150" height="200" style="float: right;"/><img src="' . $src . '" style="float: left;"/></p>';
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$baseXpath = '/w:document/w:body/w:p/w:r';
$this->assertTrue($doc->elementExists($baseXpath . '/w:pict/v:shape'));
}
2018-05-23 18:48:28 +02:00
2018-05-24 07:19:45 +02:00
/**
* Test parsing embedded image
*/
public function testParseEmbeddedImage()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = '<p><img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEJ7AnsAAD/2wBDAAgGBgcGBQgHBwcJCQgKDB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TD3HB5OF2g3jshz1qpUi6m8cqbtyy0LfGkS7x7FNbNrlTppWmiqTSWtGDBtjGMsa7ay5NhvnWIo84zQAAAAAJvZ+G63fPs69jG5W6vMsYtuO1JzcnhdjkenOuXQOTTtR1U3x0CrjaOV1eXazexRq65aXefEel5XOkFvmTFyOK/GnSzbxeXzPVcCs78/opx4LdDTOgYEMNc3BmMyNoxDJXlwIAAAAAAbaj13oPnXtfVi3IYsNLueX3mzWp9HrM9Kp2fJ08pT6u3oxz03PXpzcXeOnU3giWarDL0I6wTVBYxHJGJN54sXuHJm9aLm2C9FHDEFHsYTzGOpytMa7amMZZuJWc0RGmhkAAAAAAAA6fMzqfSOz839n2nf4vao4eG060vrxX7dWXydOfx/R8DviharSaSy7aZu2d98q8c9ck2g3ljxbwUc9CeOXp2sxyVyArpIDGqudXPHR2udHYjna26kYkbZuqSAxBnEoQAAAAAAAABt2uHnc+j9f536Hvi5zOnvpDmOkXeB1+zL4ff0XL1K28G5csVb+LXwxEO+20tbWbYxnO0V8TihvvDW0G0caba5Na8mYgki0iSJvltnMcuarEBKAAAAAAAAAABnp8tqeuv+N7nrx0N+tSjnTwQ6de35nOXoPNdvq4vgt/U8XcpW6UdXLHKt5tyOLENdkR40nImY5cZ12MR76Qgmj
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$baseXpath = '/w:document/w:body/w:p/w:r';
$this->assertTrue($doc->elementExists($baseXpath . '/w:pict/v:shape'));
}
2018-05-23 18:22:54 +02:00
/**
* Test parsing of remote img that can be found locally
*/
public function testParseRemoteLocalImage()
{
$src = 'https://fakedomain.io/images/firefox.png';
$localPath = __DIR__ . '/../_files/images/';
2018-05-23 18:48:28 +02:00
$options = array(
'IMG_SRC_SEARCH' => 'https://fakedomain.io/images/',
'IMG_SRC_REPLACE' => $localPath,
2018-05-23 18:35:12 +02:00
);
2018-05-23 18:22:54 +02:00
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = '<p><img src="' . $src . '" width="150" height="200" style="float: right;"/></p>';
Html::addHtml($section, $html, false, true, $options);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$baseXpath = '/w:document/w:body/w:p/w:r';
$this->assertTrue($doc->elementExists($baseXpath . '/w:pict/v:shape'));
}
/**
* Test parsing of remote img that can be found locally
2018-05-27 20:53:42 +02:00
*
* @expectedException \Exception
*/
public function testCouldNotLoadImage()
{
$src = 'https://fakedomain.io/images/firefox.png';
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = '<p><img src="' . $src . '" width="150" height="200" style="float: right;"/></p>';
Html::addHtml($section, $html, false, true);
}
2018-01-25 23:24:21 +01:00
public function testParseLink()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = '<p><a href="http://phpword.readthedocs.io/" style="text-decoration: underline">link text</a></p>';
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:hyperlink'));
$this->assertEquals('link text', $doc->getElement('/w:document/w:body/w:p/w:hyperlink/w:r/w:t')->nodeValue);
2018-04-25 23:57:07 +02:00
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:hyperlink/w:r/w:rPr/w:u'));
$this->assertEquals('single', $doc->getElementAttribute('/w:document/w:body/w:p/w:hyperlink/w:r/w:rPr/w:u', 'w:val'));
2018-04-14 21:15:36 +02:00
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addBookmark('bookmark');
$html = '<p><a href="#bookmark">internal link text</a></p>';
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:hyperlink'));
$this->assertTrue($doc->getElement('/w:document/w:body/w:p/w:hyperlink')->hasAttribute('w:anchor'));
$this->assertEquals('bookmark', $doc->getElement('/w:document/w:body/w:p/w:hyperlink')->getAttribute('w:anchor'));
2018-01-25 23:24:21 +01:00
}
public function testParseMalformedStyleIsIgnored()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = '<p style="">text</p>';
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertFalse($doc->elementExists('/w:document/w:body/w:p[1]/w:pPr/w:jc'));
}
2019-02-06 22:07:42 +01:00
/**
* Tests parsing hidden text
*/
public function testParseHiddenText()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = '<p style="display: hidden">This is some hidden text.</p>';
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:vanish'));
}
/**
* Tests parsing letter spacing
*/
public function testParseLetterSpacing()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = '<p style="letter-spacing: 150px">This is some text with letter spacing.</p>';
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:spacing'));
$this->assertEquals(150 * 15, $doc->getElement('/w:document/w:body/w:p/w:r/w:rPr/w:spacing')->getAttribute('w:val'));
}
2020-03-01 18:15:27 +00:00
/**
* Tests checkbox input field
*/
public function testInputCheckbox()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = '<input type="checkbox" checked="true" /><input type="checkbox" />';
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p[1]/w:r/w:fldChar/w:ffData/w:checkBox'));
$this->assertEquals(1, $doc->getElement('/w:document/w:body/w:p[1]/w:r/w:fldChar/w:ffData/w:checkBox/w:checked')->getAttribute('w:val'));
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p[2]/w:r/w:fldChar/w:ffData/w:checkBox'));
$this->assertEquals(0, $doc->getElement('/w:document/w:body/w:p[2]/w:r/w:fldChar/w:ffData/w:checkBox/w:checked')->getAttribute('w:val'));
}
/**
* Parse widths in tables and cells, which also allows for controlling column width
*/
public function testParseTableAndCellWidth()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection([
'orientation' => \PhpOffice\PhpWord\Style\Section::ORIENTATION_LANDSCAPE,
]);
// borders & backgrounds are here just for better visual comparison
$html = <<<HTML
<table style="border: 1px #000000 solid; width: 100%;">
<tr>
<td style="width: 25%; border: 1px #000000 solid; text-align: center;">25%</td>
<td>
<table width="400" style="border: 1px #000000 solid; background-color: #EEEEEE;">
<tr>
<th colspan="3" style="border: 1px #000000 solid;">400px</th>
</tr>
<tr>
<th>T2.R2.C1</th>
<th style="width: 50pt; border: 1px #FF0000 dashed; background-color: #FFFFFF">50pt</th>
<th>T2.R2.C3</th>
</tr>
<tr>
<th width="300" colspan="2" style="border: 1px #000000 solid;">300px</th>
<th style="border: 1px #000000 solid;">T2.R3.C3</th>
</tr>
</table>
</td>
</tr>
</table>
HTML;
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
// outer table grid
$xpath = '/w:document/w:body/w:tbl/w:tblGrid/w:gridCol';
$this->assertTrue($doc->elementExists($xpath));
$this->assertEquals(25 * 50, $doc->getElement($xpath)->getAttribute('w:w'));
$this->assertEquals('dxa', $doc->getElement($xpath)->getAttribute('w:type'));
// <td style="width: 25%; ...
$xpath = '/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:tcW';
$this->assertTrue($doc->elementExists($xpath));
$this->assertEquals(25 * 50, $doc->getElement($xpath)->getAttribute('w:w'));
$this->assertEquals('pct', $doc->getElement($xpath)->getAttribute('w:type'));
// <table width="400" .. 400px = 6000 twips (400 / 96 * 1440)
$xpath = '/w:document/w:body/w:tbl/w:tr/w:tc/w:tbl/w:tr/w:tc/w:tcPr/w:tcW';
$this->assertTrue($doc->elementExists($xpath));
$this->assertEquals(6000, $doc->getElement($xpath)->getAttribute('w:w'));
$this->assertEquals('dxa', $doc->getElement($xpath)->getAttribute('w:type'));
// <th style="width: 50pt; .. 50pt = 750 twips (50 / 72 * 1440)
$xpath = '/w:document/w:body/w:tbl/w:tr/w:tc/w:tbl/w:tr[2]/w:tc[2]/w:tcPr/w:tcW';
$this->assertTrue($doc->elementExists($xpath));
$this->assertEquals(1000, $doc->getElement($xpath)->getAttribute('w:w'));
$this->assertEquals('dxa', $doc->getElement($xpath)->getAttribute('w:type'));
// <th width="300" .. 300px = 4500 twips (300 / 96 * 1440)
$xpath = '/w:document/w:body/w:tbl/w:tr/w:tc/w:tbl/w:tr[3]/w:tc/w:tcPr/w:tcW';
$this->assertTrue($doc->elementExists($xpath));
$this->assertEquals(4500, $doc->getElement($xpath)->getAttribute('w:w'));
$this->assertEquals('dxa', $doc->getElement($xpath)->getAttribute('w:type'));
}
/**
* Test parsing background color for table rows and table cellspacing
*/
public function testParseCellspacingRowBgColor()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection([
'orientation' => \PhpOffice\PhpWord\Style\Section::ORIENTATION_LANDSCAPE,
]);
// borders & backgrounds are here just for better visual comparison
$html = <<<HTML
<table cellspacing="3" bgColor="lightgreen" width="50%" align="center">
<tr>
<td>A</td>
<td>B</td>
</tr>
<tr bgcolor="#FF0000">
<td>C</td>
<td>D</td>
</tr>
</table>
HTML;
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$xpath = '/w:document/w:body/w:tbl/w:tblPr/w:tblCellSpacing';
$this->assertTrue($doc->elementExists($xpath));
$this->assertEquals(3 * 15, $doc->getElement($xpath)->getAttribute('w:w'));
$this->assertEquals('dxa', $doc->getElement($xpath)->getAttribute('w:type'));
$xpath = '/w:document/w:body/w:tbl/w:tr[1]/w:tc[1]/w:tcPr/w:shd';
$this->assertTrue($doc->elementExists($xpath));
$this->assertEquals('lightgreen', $doc->getElement($xpath)->getAttribute('w:fill'));
$xpath = '/w:document/w:body/w:tbl/w:tr[2]/w:tc[1]/w:tcPr/w:shd';
$this->assertTrue($doc->elementExists($xpath));
$this->assertEquals('FF0000', $doc->getElement($xpath)->getAttribute('w:fill'));
}
/**
* Parse horizontal rule
*/
public function testParseHorizRule()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
// borders & backgrounds are here just for better visual comparison
$html = <<<HTML
<p>Simple default rule:</p>
<hr/>
<p>Custom style rule:</p>
<hr style="margin-top: 30px; margin-bottom: 0; border-bottom: 5px lightblue solid;" />
<p>END</p>
HTML;
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
// default rule
$xpath = '/w:document/w:body/w:p[2]/w:pPr/w:pBdr/w:bottom';
$this->assertTrue($doc->elementExists($xpath));
$this->assertEquals('single', $doc->getElement($xpath)->getAttribute('w:val')); // solid
$this->assertEquals('1', $doc->getElement($xpath)->getAttribute('w:sz')); // 1 twip
$this->assertEquals('000000', $doc->getElement($xpath)->getAttribute('w:color')); // black
// custom style rule
$xpath = '/w:document/w:body/w:p[4]/w:pPr/w:pBdr/w:bottom';
$this->assertTrue($doc->elementExists($xpath));
$this->assertEquals('single', $doc->getElement($xpath)->getAttribute('w:val'));
$this->assertEquals(intval(5 * 15 / 2), $doc->getElement($xpath)->getAttribute('w:sz'));
$this->assertEquals('lightblue', $doc->getElement($xpath)->getAttribute('w:color'));
$xpath = '/w:document/w:body/w:p[4]/w:pPr/w:spacing';
$this->assertTrue($doc->elementExists($xpath));
$this->assertEquals(450, $doc->getElement($xpath)->getAttribute('w:before'));
$this->assertEquals(0, $doc->getElement($xpath)->getAttribute('w:after'));
$this->assertEquals(240, $doc->getElement($xpath)->getAttribute('w:line'));
}
/**
* Parse ordered list start & numbering style
*/
public function testParseOrderedList()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
// borders & backgrounds are here just for better visual comparison
$html = <<<HTML
<ol>
<li>standard ordered list line 1</li>
<li>standard ordered list line 2</li>
</ol>
<ol start="5" type="A">
<li>ordered list alphabetical, <span style="background-color: #EEEEEE; color: #FF0000;">line 5 => E</span></li>
<li>ordered list alphabetical, <span style="background-color: #EEEEEE; color: #FF0000;">line 6 => F</span></li>
</ol>
<ol start="3" type="i">
<li>ordered list roman lower, line <b>3 => iii</b></li>
<li>ordered list roman lower, line <b>4 => iv</b></li>
</ol>
HTML;
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
// compare numbering file
$xmlFile = 'word/numbering.xml';
// default - decimal start = 1
$xpath = '/w:numbering/w:abstractNum[1]/w:lvl[1]/w:start';
$this->assertTrue($doc->elementExists($xpath, $xmlFile));
$this->assertEquals('1', $doc->getElement($xpath, $xmlFile)->getAttribute('w:val'));
$xpath = '/w:numbering/w:abstractNum[1]/w:lvl[1]/w:numFmt';
$this->assertTrue($doc->elementExists($xpath, $xmlFile));
$this->assertEquals('decimal', $doc->getElement($xpath, $xmlFile)->getAttribute('w:val'));
// second list - start = 5, type A = upperLetter
$xpath = '/w:numbering/w:abstractNum[2]/w:lvl[1]/w:start';
$this->assertTrue($doc->elementExists($xpath, $xmlFile));
$this->assertEquals('5', $doc->getElement($xpath, $xmlFile)->getAttribute('w:val'));
$xpath = '/w:numbering/w:abstractNum[2]/w:lvl[1]/w:numFmt';
$this->assertTrue($doc->elementExists($xpath, $xmlFile));
$this->assertEquals('upperLetter', $doc->getElement($xpath, $xmlFile)->getAttribute('w:val'));
// third list - start = 3, type i = lowerRoman
$xpath = '/w:numbering/w:abstractNum[3]/w:lvl[1]/w:start';
$this->assertTrue($doc->elementExists($xpath, $xmlFile));
$this->assertEquals('3', $doc->getElement($xpath, $xmlFile)->getAttribute('w:val'));
$xpath = '/w:numbering/w:abstractNum[3]/w:lvl[1]/w:numFmt';
$this->assertTrue($doc->elementExists($xpath, $xmlFile));
$this->assertEquals('lowerRoman', $doc->getElement($xpath, $xmlFile)->getAttribute('w:val'));
}
/**
* Parse ordered list start & numbering style
*/
public function testParseVerticalAlign()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
// borders & backgrounds are here just for better visual comparison
$html = <<<HTML
<table width="100%">
<tr>
<td width="20%" style="border: 1px #666666 solid;">default</td>
<td width="20%" style="vertical-align: top; border: 1px #666666 solid;">top</td>
<td width="20%" style="vertical-align: middle; border: 1px #666666 solid;">middle</td>
<td width="20%" valign="bottom" style="border: 1px #666666 solid;">bottom</td>
<td bgcolor="#DDDDDD"><br/><br/><br/><br/><br/><br/><br/></td>
</tr>
</table>
HTML;
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$xpath = '/w:document/w:body/w:tbl/w:tr/w:tc[1]/w:tcPr/w:vAlign';
$this->assertFalse($doc->elementExists($xpath));
$xpath = '/w:document/w:body/w:tbl/w:tr/w:tc[2]/w:tcPr/w:vAlign';
$this->assertTrue($doc->elementExists($xpath));
$this->assertEquals('top', $doc->getElement($xpath)->getAttribute('w:val'));
$xpath = '/w:document/w:body/w:tbl/w:tr/w:tc[3]/w:tcPr/w:vAlign';
$this->assertTrue($doc->elementExists($xpath));
$this->assertEquals('center', $doc->getElement($xpath)->getAttribute('w:val'));
$xpath = '/w:document/w:body/w:tbl/w:tr/w:tc[4]/w:tcPr/w:vAlign';
$this->assertTrue($doc->elementExists($xpath));
$this->assertEquals('bottom', $doc->getElement($xpath)->getAttribute('w:val'));
}
/**
* Fix bug - don't decode double quotes inside double quoted string
*/
public function testDontDecodeAlreadyEncodedDoubleQuotes()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
// borders & backgrounds are here just for better visual comparison
$html = <<<HTML
<div style="font-family: Arial, &quot;Helvetice Neue&quot;">This would crash if inline quotes also decoded at loading XML into DOMDocument!</div>
HTML;
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue(is_object($doc));
}
2014-05-10 23:40:34 +07:00
}