From d17a806305010b97b03441739cde7913645a745f Mon Sep 17 00:00:00 2001 From: Brandon Skrtich Date: Mon, 9 Jun 2014 15:17:40 -0600 Subject: [PATCH 1/4] Small fixes for addHtml. Adding some HTML Entities to the test for addHTML --- src/PhpWord/Shared/Html.php | 9 +++++++-- tests/PhpWord/Tests/Shared/HtmlTest.php | 6 ++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index 8abfea2f..fc23b5b0 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -42,9 +42,14 @@ class Html * which could be applied when such an element occurs in the parseNode function. */ - // Preprocess: remove all line ends, decode HTML entity, and add body tag for HTML fragments + // Preprocess: remove all line ends, decode HTML entity, + // fix ampersand and angle brackets and add body tag for HTML fragments $html = str_replace(array("\n", "\r"), '', $html); - $html = html_entity_decode($html); + $html = str_replace(array('<', '>', '&'), array('_lt_', '_gt_', '_amp_'), $html); + $html = html_entity_decode($html, ENT_QUOTES, 'UTF-8'); + $html = str_replace('&', '&', $html); + $html = str_replace(array('_lt_', '_gt_', '_amp_'), array('<', '>', '&'), $html); + if ($fullHTML === false) { $html = '' . $html . ''; } diff --git a/tests/PhpWord/Tests/Shared/HtmlTest.php b/tests/PhpWord/Tests/Shared/HtmlTest.php index 730600d7..e7b3533e 100644 --- a/tests/PhpWord/Tests/Shared/HtmlTest.php +++ b/tests/PhpWord/Tests/Shared/HtmlTest.php @@ -60,6 +60,12 @@ class HtmlTest extends \PHPUnit_Framework_TestCase $content .= '
HeaderContent
'; $content .= ''; $content .= '
  1. Bullet
'; + $content .= "'Single Quoted Text'"; + $content .= '"Double Quoted Text"'; + $content .= '& Ampersand'; + $content .= '<>“‘’«»‹›'; + $content .= '&•°…™©®—'; + $content .= '–   ²³¼½¾'; Html::addHtml($section, $content); } } From 8e93c91bb00c892347dc0f63b741eb5347d4e71b Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Wed, 11 Jun 2014 00:57:52 +0700 Subject: [PATCH 2/4] Update changelog for #270 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d4010ec..9b741e57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ This release added drawing shapes (arc, curve, line, polyline, rect, oval) eleme ### Bugfixes - Fix rare PclZip/realpath/PHP version problem - @andrew-kzoo GH-261 +- `addHTML` encoding and ampersand fixes for PHP 5.3 - @bskrtich GH-270 ### Deprecated From f54d9a1eb443dbb8dc631ff8eb8f25fe9bafa25c Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 12 Jun 2014 02:25:34 +0700 Subject: [PATCH 3/4] #274: Page breaks on titles and tables --- CHANGELOG.md | 1 + .../Word2007/Element/AbstractElement.php | 32 ------------------- .../Writer/Word2007/Element/Container.php | 12 ------- .../Writer/Word2007/Element/PageBreak.php | 6 ++-- src/PhpWord/Writer/Word2007/Part/Settings.php | 3 +- .../Writer/Word2007/Part/DocumentTest.php | 6 ++-- 6 files changed, 8 insertions(+), 52 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b741e57..ebfd3263 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ This release added drawing shapes (arc, curve, line, polyline, rect, oval) eleme - Fix rare PclZip/realpath/PHP version problem - @andrew-kzoo GH-261 - `addHTML` encoding and ampersand fixes for PHP 5.3 - @bskrtich GH-270 +- Page breaks on titles and tables - @ivanlanin GH-274 ### Deprecated diff --git a/src/PhpWord/Writer/Word2007/Element/AbstractElement.php b/src/PhpWord/Writer/Word2007/Element/AbstractElement.php index e17640c8..1cbf213b 100644 --- a/src/PhpWord/Writer/Word2007/Element/AbstractElement.php +++ b/src/PhpWord/Writer/Word2007/Element/AbstractElement.php @@ -50,13 +50,6 @@ abstract class AbstractElement */ protected $withoutP = false; - /** - * Has page break before - * - * @var bool - */ - private $pageBreakBefore = false; - /** * Write element */ @@ -96,26 +89,6 @@ abstract class AbstractElement return $this->element; } - /** - * Has page break before - * - * @return bool - */ - public function hasPageBreakBefore() - { - return $this->pageBreakBefore; - } - - /** - * Set page break before - * - * @param bool $value - */ - public function setPageBreakBefore($value = true) - { - $this->pageBreakBefore = (bool)$value; - } - /** * Start w:p DOM element * @@ -129,11 +102,6 @@ abstract class AbstractElement if (method_exists($this->element, 'getParagraphStyle')) { $this->writeParagraphStyle(); } - // PageBreak - if ($this->pageBreakBefore) { - $elementWriter = new PageBreak($this->xmlWriter, new PageBreakElement()); - $elementWriter->write(); - } } } diff --git a/src/PhpWord/Writer/Word2007/Element/Container.php b/src/PhpWord/Writer/Word2007/Element/Container.php index 3dad824d..9504b7a6 100644 --- a/src/PhpWord/Writer/Word2007/Element/Container.php +++ b/src/PhpWord/Writer/Word2007/Element/Container.php @@ -81,21 +81,9 @@ class Container extends AbstractElement $elementClass = substr(get_class($element), strrpos(get_class($element), '\\') + 1); $writerClass = $this->namespace . '\\' . $elementClass; - // Check it's a page break. No need to write it, instead, flag containers' - // pageBreakBefore to be assigned to the next element - if ($elementClass == 'PageBreak') { - $this->setPageBreakBefore(true); - return $elementClass; - } - if (class_exists($writerClass)) { - // Get container's page break before and reset it - $pageBreakBefore = $this->hasPageBreakBefore(); - $this->setPageBreakBefore(false); - /** @var \PhpOffice\PhpWord\Writer\Word2007\Element\AbstractElement $writer Type hint */ $writer = new $writerClass($xmlWriter, $element, $withoutP); - $writer->setPageBreakBefore($pageBreakBefore); $writer->write(); } diff --git a/src/PhpWord/Writer/Word2007/Element/PageBreak.php b/src/PhpWord/Writer/Word2007/Element/PageBreak.php index 47f3feb5..be317089 100644 --- a/src/PhpWord/Writer/Word2007/Element/PageBreak.php +++ b/src/PhpWord/Writer/Word2007/Element/PageBreak.php @@ -20,9 +20,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; /** * PageBreak element writer * - * Originally, page break is rendered as a `w:p`, but this turns out to produce bug #150. - * As of 0.11.0, page break is rendered as a `w:r` with `w:br` type "page" and `w:lastRenderedPageBreak` - * * @since 0.10.0 */ class PageBreak extends AbstractElement @@ -36,11 +33,12 @@ class PageBreak extends AbstractElement { $xmlWriter = $this->getXmlWriter(); + $xmlWriter->startElement('w:p'); $xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:br'); $xmlWriter->writeAttribute('w:type', 'page'); $xmlWriter->endElement(); // w:br - $xmlWriter->writeElement('w:lastRenderedPageBreak'); $xmlWriter->endElement(); // w:r + $xmlWriter->endElement(); // w:p } } diff --git a/src/PhpWord/Writer/Word2007/Part/Settings.php b/src/PhpWord/Writer/Word2007/Part/Settings.php index 784a5d1e..d8535d8c 100644 --- a/src/PhpWord/Writer/Word2007/Part/Settings.php +++ b/src/PhpWord/Writer/Word2007/Part/Settings.php @@ -50,7 +50,8 @@ class Settings extends AbstractPart 'w:autofitToFirstFixedWidthCell' => '', 'w:underlineTabInNumList' => '', 'w:displayHangulFixedWidth' => '', - 'w:splitPgBreakAndParaMark' => '', + // Commented for GH-274 + // 'w:splitPgBreakAndParaMark' => '', 'w:doNotVertAlignCellWithSp' => '', 'w:doNotBreakConstrainedForcedTable' => '', 'w:doNotVertAlignInTxbx' => '', diff --git a/tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php b/tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php index de5d63e8..56be15d0 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php @@ -114,15 +114,15 @@ class DocumentTest extends \PHPUnit_Framework_TestCase $this->assertEquals('page', $element->getAttribute('w:type')); // Title - $element = $doc->getElement('/w:document/w:body/w:p[5]/w:pPr/w:pStyle'); + $element = $doc->getElement('/w:document/w:body/w:p[6]/w:pPr/w:pStyle'); $this->assertEquals('Heading1', $element->getAttribute('w:val')); // List item - $element = $doc->getElement('/w:document/w:body/w:p[6]/w:pPr/w:numPr/w:numId'); + $element = $doc->getElement('/w:document/w:body/w:p[7]/w:pPr/w:numPr/w:numId'); $this->assertEquals(3, $element->getAttribute('w:val')); // Object - $element = $doc->getElement('/w:document/w:body/w:p[11]/w:r/w:object/o:OLEObject'); + $element = $doc->getElement('/w:document/w:body/w:p[12]/w:r/w:object/o:OLEObject'); $this->assertEquals('Embed', $element->getAttribute('Type')); } From e9f8e889f8fef4e141d14505e861d7995ad558a3 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 12 Jun 2014 02:41:49 +0700 Subject: [PATCH 4/4] #275: Recipe for adding a link within a title --- docs/recipes.rst | 22 ++++++++++++++++++++++ docs/src/documentation.md | 21 +++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/docs/recipes.rst b/docs/recipes.rst index d5678a52..1b529d7b 100644 --- a/docs/recipes.rst +++ b/docs/recipes.rst @@ -65,3 +65,25 @@ Define a numbering style and title styles, and match the two styles (with ``pSty $section->addTitle('Heading 1', 1); $section->addTitle('Heading 2', 2); $section->addTitle('Heading 3', 3); + +Add a link within a title +------------------------- + +Apply 'HeadingN' paragraph style to TextRun or Link. Sample code: + +.. code-block:: php + + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $phpWord->addTitleStyle(1, array('size' => 16, 'bold' => true)); + $phpWord->addTitleStyle(2, array('size' => 14, 'bold' => true)); + $phpWord->addFontStyle('Link', array('color' => '0000FF', 'underline' => 'single')); + + $section = $phpWord->addSection(); + + // Textrun + $textrun = $section->addTextRun('Heading1'); + $textrun->addText('The '); + $textrun->addLink('https://github.com/PHPOffice/PHPWord', 'PHPWord', 'Link'); + + // Link + $section->addLink('https://github.com/', 'GitHub', 'Link', 'Heading2'); diff --git a/docs/src/documentation.md b/docs/src/documentation.md index 144222c1..49505c7f 100644 --- a/docs/src/documentation.md +++ b/docs/src/documentation.md @@ -1017,6 +1017,27 @@ $section->addTitle('Heading 2', 2); $section->addTitle('Heading 3', 3); ``` +## Add a link within a title + +Apply 'HeadingN' paragraph style to TextRun or Link. Sample code: + +```php +$phpWord = new \PhpOffice\PhpWord\PhpWord(); +$phpWord->addTitleStyle(1, array('size' => 16, 'bold' => true)); +$phpWord->addTitleStyle(2, array('size' => 14, 'bold' => true)); +$phpWord->addFontStyle('Link', array('color' => '0000FF', 'underline' => 'single')); + +$section = $phpWord->addSection(); + +// Textrun +$textrun = $section->addTextRun('Heading1'); +$textrun->addText('The '); +$textrun->addLink('https://github.com/PHPOffice/PHPWord', 'PHPWord', 'Link'); + +// Link +$section->addLink('https://github.com/', 'GitHub', 'Link', 'Heading2'); +``` + # Frequently asked questions ## Is this the same with PHPWord that I found in CodePlex?