Merge pull request #1168 from FBnil/bring_back_bold_and_italic

Html::addHTML should parse <b> <i> <u>
This commit is contained in:
troosan 2017-11-11 00:31:52 +01:00 committed by GitHub
commit 3f8892ed91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 315 additions and 15 deletions

View File

@ -414,7 +414,7 @@ Line elements can be added to sections by using ``addLine``.
.. code-block:: php
$linestyle = array('weight' => 1, 'width' => 100, 'height' => 0, 'color' => 635552);
$lineStyle = array('weight' => 1, 'width' => 100, 'height' => 0, 'color' => 635552);
$section->addLine($lineStyle)
Available line style attributes:

View File

@ -23,7 +23,7 @@ namespace PhpOffice\PhpWord\Element;
* @method Text addText(string $text, mixed $fStyle = null, mixed $pStyle = null)
* @method TextRun addTextRun(mixed $pStyle = null)
* @method Bookmark addBookmark(string $name)
* @method Link addLink(string $target, string $text = null, mixed $fStyle = null, mixed $pStyle = null)
* @method Link addLink(string $target, string $text = null, mixed $fStyle = null, mixed $pStyle = null, boolean $internal = false)
* @method PreserveText addPreserveText(string $text, mixed $fStyle = null, mixed $pStyle = null)
* @method void addTextBreak(int $count = 1, mixed $fStyle = null, mixed $pStyle = null)
* @method ListItem addListItem(string $txt, int $depth = 0, mixed $font = null, mixed $list = null, mixed $para = null)

View File

@ -18,6 +18,7 @@
namespace PhpOffice\PhpWord\Shared;
use PhpOffice\PhpWord\Element\AbstractContainer;
use PhpOffice\PhpWord\SimpleType\Jc;
/**
* Common Html functions
@ -117,9 +118,13 @@ class Html
'h6' => array('Heading', null, $element, $styles, null, 'Heading6', null),
'#text' => array('Text', $node, $element, $styles, null, null, null),
'strong' => array('Property', null, null, $styles, null, 'bold', true),
'b' => array('Property', null, null, $styles, null, 'bold', true),
'em' => array('Property', null, null, $styles, null, 'italic', true),
'i' => array('Property', null, null, $styles, null, 'italic', true),
'u' => array('Property', null, null, $styles, null, 'underline', 'single'),
'sup' => array('Property', null, null, $styles, null, 'superScript', true),
'sub' => array('Property', null, null, $styles, null, 'subScript', true),
'span' => array('Property', null, null, $styles, null, 'span', $node),
'table' => array('Table', $node, $element, $styles, null, 'addTable', true),
'tr' => array('Table', $node, $element, $styles, null, 'addRow', true),
'td' => array('Table', $node, $element, $styles, null, 'addCell', true),
@ -233,8 +238,6 @@ class Html
// if (method_exists($element, 'addText')) {
$element->addText($node->nodeValue, $styles['font'], $styles['paragraph']);
// }
return null;
}
/**
@ -246,9 +249,16 @@ class Html
*/
private static function parseProperty(&$styles, $argument1, $argument2)
{
$styles['font'][$argument1] = $argument2;
return null;
if ($argument1 !== 'span') {
$styles['font'][$argument1] = $argument2;
} else {
if (!is_null($argument2->attributes)) {
$nodeAttr = $argument2->attributes->getNamedItem('style');
if (!is_null($nodeAttr) && property_exists($nodeAttr, 'value')) {
$styles['font'] = self::parseStyle($nodeAttr, $styles['font']);
}
}
}
}
/**
@ -298,8 +308,6 @@ class Html
$data['listdepth'] = 0;
}
$styles['list']['listType'] = $argument1;
return null;
}
/**
@ -325,8 +333,6 @@ class Html
}
$element->addListItem($text, $data['listdepth'], $styles['font'], $styles['list'], $styles['paragraph']);
}
return null;
}
/**
@ -354,7 +360,20 @@ class Html
}
break;
case 'text-align':
$styles['alignment'] = $cValue; // todo: any mapping?
switch ($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;
case 'color':
$styles['color'] = trim($cValue, '#');
@ -362,6 +381,20 @@ class Html
case 'background-color':
$styles['bgColor'] = trim($cValue, '#');
break;
case 'font-weight':
$tValue = false;
if (preg_match('#bold#', $cValue)) {
$tValue = true; // also match bolder
}
$styles['bold'] = $tValue;
break;
case 'font-style':
$tValue = false;
if (preg_match('#(?:italic|oblique)#', $cValue)) {
$tValue = true;
}
$styles['italic'] = $tValue;
break;
}
}

View File

@ -56,4 +56,24 @@ class MsDocTest extends \PHPUnit\Framework\TestCase
$phpWord = IOFactory::load($filename, 'MsDoc');
$this->assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $phpWord);
}
/**
* Test exception on not existing file
* @expectedException \Exception
*/
public function testFailIfFileNotReadable()
{
$filename = __DIR__ . '/../_files/documents/not_existing_reader.doc';
IOFactory::load($filename, 'MsDoc');
}
/**
* Test exception on non OLE document
* @expectedException \Exception
*/
public function testFailIfFileNotOle()
{
$filename = __DIR__ . '/../_files/documents/reader.odt';
IOFactory::load($filename, 'MsDoc');
}
}

View File

@ -18,9 +18,12 @@
namespace PhpOffice\PhpWord\Shared;
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\TestHelperDOCX;
/**
* Test class for PhpOffice\PhpWord\Shared\Html
* @coversDefaultClass \PhpOffice\PhpWord\Shared\Html
*/
class HtmlTest extends \PHPUnit\Framework\TestCase
{
@ -43,7 +46,7 @@ class HtmlTest extends \PHPUnit\Framework\TestCase
// Styles
$content .= '<p style="text-decoration: underline; text-decoration: line-through; '
. 'text-align: center; color: #999; background-color: #000;">';
. 'text-align: center; color: #999; background-color: #000; font-weight: bold; font-style: italic;">';
foreach ($styles as $style) {
$content .= "<{$style}>{$style}</{$style}>";
}
@ -67,4 +70,110 @@ class HtmlTest extends \PHPUnit\Framework\TestCase
$content .= '&ndash;&nbsp;&emsp;&ensp;&sup2;&sup3;&frac14;&frac12;&frac34;';
Html::addHtml($section, $content);
}
/**
* 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 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'));
}
/**
* 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'));
$this->assertEquals('start', $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:pPr/w:jc', 'w:val'));
$this->assertEquals('end', $doc->getElementAttribute('/w:document/w:body/w:p[2]/w:pPr/w:jc', 'w:val'));
$this->assertEquals('center', $doc->getElementAttribute('/w:document/w:body/w:p[3]/w:pPr/w:jc', 'w:val'));
$this->assertEquals('both', $doc->getElementAttribute('/w:document/w:body/w:p[4]/w:pPr/w:jc', 'w:val'));
}
/**
* 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;"><span style="text-decoration: underline;">test</span></p>');
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:pPr/w:jc'));
$this->assertEquals('center', $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:pPr/w:jc', 'w:val'));
$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();
$html = '
<table style="width: 50%; border: 6px #0000FF solid;">
<thead>
<tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; ">
<th>a</th>
<th>b</th>
<th>c</th>
</tr>
</thead>
<tbody>
<tr><td>1</td><td colspan="2">2</td></tr>
<tr><td>4</td><td>5</td><td>6</td></tr>
</tbody>
</table>';
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
// echo $doc->printXml();
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p'));
// $this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:tbl/w:tr/w:tc'));
}
}

View File

@ -83,6 +83,7 @@ class ContentTest extends \PHPUnit\Framework\TestCase
$cell->addObject($objectSrc);
$textrun = $cell->addTextRun();
$textrun->addText('Test text run');
$section->addPageBreak();
$footer = $section->addFooter();
$footer->addPreserveText('{PAGE}');

View File

@ -71,6 +71,68 @@ class ElementTest extends \PHPUnit\Framework\TestCase
$this->assertTrue($doc->elementExists($element));
}
/**
* Test bookmark element
*/
public function testBookmark()
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addBookmark('test_bookmark');
$doc = TestHelperDOCX::getDocument($phpWord);
$element = '/w:document/w:body/w:bookmarkStart';
$this->assertTrue($doc->elementExists($element));
$this->assertEquals('test_bookmark', $doc->getElementAttribute($element, 'w:name'));
$element = '/w:document/w:body/w:bookmarkEnd';
$this->assertTrue($doc->elementExists($element));
}
/**
* Test link element
*/
public function testLinkElement()
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addLink('https://github.com/PHPOffice/PHPWord');
$section->addLink('internal_link', null, null, null, true);
$doc = TestHelperDOCX::getDocument($phpWord);
$element = '/w:document/w:body/w:p[1]/w:hyperlink/w:r/w:t';
$this->assertTrue($doc->elementExists($element));
$element = '/w:document/w:body/w:p[2]/w:hyperlink/w:r/w:t';
$this->assertTrue($doc->elementExists($element));
$this->assertEquals('internal_link', $doc->getElementAttribute('/w:document/w:body/w:p[2]/w:hyperlink', 'w:anchor'));
}
/**
* Basic test for table element
*/
public function testTableElements()
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$table = $section->addTable(array('alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER));
$table->addRow(900);
$table->addCell(2000)->addText('Row 1');
$table->addCell(2000)->addText('Row 2');
$table->addCell(2000)->addText('Row 3');
$table->addCell(2000)->addText('Row 4');
$doc = TestHelperDOCX::getDocument($phpWord);
$tableRootElement = '/w:document/w:body/w:tbl';
$this->assertTrue($doc->elementExists($tableRootElement . '/w:tblGrid/w:gridCol'));
$this->assertTrue($doc->elementExists($tableRootElement . '/w:tblPr/w:jc'));
$this->assertEquals('center', $doc->getElementAttribute($tableRootElement . '/w:tblPr/w:jc', 'w:val'));
}
/**
* Test shape elements
*/

View File

@ -50,4 +50,19 @@ class FontTest extends \PHPUnit\Framework\TestCase
$path = '/w:document/w:body/w:p/w:r/w:rPr/w:rtl';
$this->assertTrue($doc->elementExists($path, $file));
}
/**
* Test writing font with language
*/
public function testFontWithLang()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addText('Ce texte-ci est en français.', array('lang' => \PhpOffice\PhpWord\Style\Language::FR_BE));
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$file = 'word/document.xml';
$path = '/w:document/w:body/w:p/w:r/w:rPr/w:lang';
$this->assertTrue($doc->elementExists($path, $file));
}
}

View File

@ -0,0 +1,52 @@
<?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
* @copyright 2010-2017 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
use PhpOffice\PhpWord\TestHelperDOCX;
/**
* Test class for PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph
*
* @coversDefaultClass \PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph
* @runTestsInSeparateProcesses
*/
class ParagraphTest extends \PHPUnit\Framework\TestCase
{
/**
* Executed before each method of the class
*/
public function tearDown()
{
TestHelperDOCX::clear();
}
/**
* Test write styles
*/
public function testParagraphNumbering()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->addParagraphStyle('testStyle', array('indent' => '10'));
$section = $phpWord->addSection();
$section->addText('test', null, array('numStyle' => 'testStyle', 'numLevel' => '1'));
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$path = '/w:document/w:body/w:p/w:pPr/w:numPr/w:ilvl';
$this->assertTrue($doc->elementExists($path));
}
}

View File

@ -170,12 +170,20 @@ class XmlDocument
* @param string $file
* @return string
*/
public function printXml($path = '/w:document', $file = 'word/document.xml')
public function printXml($path = '/', $file = 'word/document.xml')
{
$element = $this->getElement($path, $file);
if ($element instanceof \DOMDocument) {
$element->formatOutput = true;
$element->preserveWhiteSpace = false;
return $element->saveXML();
}
$newdoc = new \DOMDocument();
$newdoc->formatOutput = true;
$newdoc->preserveWhiteSpace = false;
$node = $newdoc->importNode($this->getElement($path, $file), true);
$node = $newdoc->importNode($element, true);
$newdoc->appendChild($node);
return $newdoc->saveXML($node);