From 2b5a6c2c7baa8d6c52b573b0708c56a2ee2b4c58 Mon Sep 17 00:00:00 2001 From: Lenz Weber Date: Tue, 14 Mar 2017 16:53:49 +0100 Subject: [PATCH 1/3] add functionality to use MACROBUTTON as Field, use Styles for Field, add noProof to Font Style --- samples/Sample_27_Field.php | 8 ++ src/PhpWord/Element/Field.php | 52 +++++++++++++ src/PhpWord/Style/Font.php | 31 ++++++++ src/PhpWord/Writer/Word2007/Element/Field.php | 74 +++++++++++++++++-- src/PhpWord/Writer/Word2007/Style/Font.php | 4 + 5 files changed, 163 insertions(+), 6 deletions(-) diff --git a/samples/Sample_27_Field.php b/samples/Sample_27_Field.php index 5774789561..218799cbc3 100644 --- a/samples/Sample_27_Field.php +++ b/samples/Sample_27_Field.php @@ -24,6 +24,14 @@ $textrun->addField('DATE', array('dateformat' => 'd-M-yyyy H:mm:ss'), array('PreserveFormat', 'LunarCalendar')); $textrun->addText(' written in a textrun.'); +$section->addText('Simple text Macrobutton'); +$section->addField('MACROBUTTON', array('MacroName'=>'DoFieldClick', 'DisplayText'=>'Enter Value here'), array()); + +$section->addText('Styled text Macrobutton'); +$font = new \PhpOffice\PhpWord\Style\Font(); +$font->setColor('FF0000'); +$section->addField('MACROBUTTON', array('MacroName' => 'DoFieldClick', 'DisplayText' => 'Enter Value here'), array())->setFontStyle($font); + // Save file echo write($phpWord, basename(__FILE__, '.php'), $writers); if (!CLI) { diff --git a/src/PhpWord/Element/Field.php b/src/PhpWord/Element/Field.php index 48dc1d2eba..1ad18c71c6 100644 --- a/src/PhpWord/Element/Field.php +++ b/src/PhpWord/Element/Field.php @@ -17,6 +17,8 @@ namespace PhpOffice\PhpWord\Element; +use PhpOffice\PhpWord\Style\Font; + /** * Field element * @@ -52,6 +54,9 @@ class Field extends AbstractElement 'h:mm am/pm', 'h:mm:ss am/pm', 'HH:mm', 'HH:mm:ss') ), 'options'=>array('PreserveFormat', 'LunarCalendar', 'SakaEraCalendar', 'LastUsedFormat') + ), + 'MACROBUTTON' => array( + 'properties'=> array( 'MacroName'=>'', 'DisplayText'=>'' ) ) ); @@ -76,6 +81,13 @@ class Field extends AbstractElement */ protected $options = array(); + /** + * Font style + * + * @var \PhpOffice\PhpWord\Style\Font + */ + protected $fontStyle; + /** * Create a new Field Element * @@ -184,4 +196,44 @@ public function getOptions() { return $this->options; } + + /** + * Set Text style + * + * @param \PhpOffice\PhpWord\Style\Font $style + * @return \PhpOffice\PhpWord\Style\Font + */ + public function setFontStyle($style = null) + { + if (!$style instanceof Font) { + throw new \InvalidArgumentException('font style must be of type Font'); + } + + if ($style->getNoProof()) + { + $this->fontStyle = $style; + } else { + // make a copy of the font so the original is not altered + $this->fontStyle = clone $style; + $this->fontStyle->setNoProof(true); + } + + return $this->fontStyle; + } + + /** + * Get Text style + * + * @return \PhpOffice\PhpWord\Style\Font + */ + public function getFontStyle() + { + if ($this->fontStyle == null){ + $font = new Font(); + $font->setNoProof(true); + return $font; + } + + return $this->fontStyle; + } } diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index b625e3b8a9..ad14adeb48 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -228,6 +228,14 @@ class Font extends AbstractStyle */ private $rtl = false; + /** + * noProof (disables AutoCorrect) + * + * @var bool + * http://www.datypic.com/sc/ooxml/e-w_noProof-1.html + */ + private $noProof = false; + /** * Create new font style * @@ -691,6 +699,29 @@ public function setKerning($value = null) return $this; } + /** + * Get noProof (disables autocorrect) + * + * @return bool + */ + public function getNoProof() + { + return $this->noProof; + } + + /** + * Set noProof (disables autocorrect) + * + * @param bool $value + * @return $this + */ + public function setNoProof($value = false) + { + $this->noProof = $value; + + return $this; + } + /** * Get line height * diff --git a/src/PhpWord/Writer/Word2007/Element/Field.php b/src/PhpWord/Writer/Word2007/Element/Field.php index ae4c66ba8e..67f0715ca1 100644 --- a/src/PhpWord/Writer/Word2007/Element/Field.php +++ b/src/PhpWord/Writer/Word2007/Element/Field.php @@ -17,6 +17,8 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; +use PhpOffice\PhpWord\Settings; + /** * Field element writer * @@ -24,6 +26,8 @@ */ class Field extends Text { + protected static $simpleFields = array('PAGE', 'NUMPAGES', 'DATE'); + /** * Write field element. * @@ -31,12 +35,74 @@ class Field extends Text */ public function write() { - $xmlWriter = $this->getXmlWriter(); $element = $this->getElement(); if (!$element instanceof \PhpOffice\PhpWord\Element\Field) { return; } + if (in_array($element->getType(), self::$simpleFields)){ + $writeField = "writeSimpleField"; + } else { + $type = ucfirst(strtolower($element->getType())); + $writeField = "write{$type}"; + } + $this->$writeField(); + } + + /** + * write MACROBUTTON + */ + private function writeMacrobutton(){ + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); + $properties = $element->getProperties(); + + $macroName = $properties['MacroName']; + $displayText = $properties['DisplayText']; + + $this->startElementP(); + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'begin'); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:instrText'); + $xmlWriter->writeAttribute('xml:space', 'preserve'); + $xmlWriter->text("MACROBUTTON {$macroName} "); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + + $xmlWriter->startElement('w:r'); + $this->writeFontStyle(); + $xmlWriter->startElement('w:instrText'); + if (Settings::isOutputEscapingEnabled()) { + $xmlWriter->text($this->getText($displayText)); + } else { + $xmlWriter->writeRaw($this->getText($displayText)); + } + $xmlWriter->endElement(); + $xmlWriter->endElement(); + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'end'); + $xmlWriter->endElement(); + $xmlWriter->endElement(); + + $this->endElementP(); // w:p + } + + /** + * write one of 'PAGE', 'NUMPAGES' or 'DATE' + */ + private function writeSimpleField() + { + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); + $instruction = ' ' . $element->getType() . ' '; $properties = $element->getProperties(); foreach ($properties as $propkey => $propval) { @@ -74,11 +140,7 @@ public function write() $xmlWriter->startElement('w:fldSimple'); $xmlWriter->writeAttribute('w:instr', $instruction); $xmlWriter->startElement('w:r'); - $xmlWriter->startElement('w:rPr'); - $xmlWriter->startElement('w:noProof'); - $xmlWriter->endElement(); // w:noProof - $xmlWriter->endElement(); // w:rPr - + $this->writeFontStyle(); $xmlWriter->writeElement('w:t', '1'); $xmlWriter->endElement(); // w:r $xmlWriter->endElement(); // w:fldSimple diff --git a/src/PhpWord/Writer/Word2007/Style/Font.php b/src/PhpWord/Writer/Word2007/Style/Font.php index 97cf3088b9..e1f32da9b6 100644 --- a/src/PhpWord/Writer/Word2007/Style/Font.php +++ b/src/PhpWord/Writer/Word2007/Style/Font.php @@ -123,6 +123,10 @@ private function writeStyle() $xmlWriter->writeElementIf($style->getSpacing() !== null, 'w:spacing', 'w:val', $style->getSpacing()); $xmlWriter->writeElementIf($style->getKerning() !== null, 'w:kern', 'w:val', $style->getKerning() * 2); + + // noProof + $xmlWriter->writeElementIf($style->getNoProof() !== false, 'w:noProof'); + // Background-Color $shading = $style->getShading(); if (!is_null($shading)) { From 2a5ab3cdfd2a9525be2da05644715e26355c30b4 Mon Sep 17 00:00:00 2001 From: Maria Haubner Date: Tue, 14 Mar 2017 17:22:10 +0100 Subject: [PATCH 2/3] code review --- samples/Sample_27_Field.php | 4 +-- src/PhpWord/Element/Field.php | 7 ++--- src/PhpWord/Writer/Word2007/Element/Field.php | 31 ++++++++++--------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/samples/Sample_27_Field.php b/samples/Sample_27_Field.php index 218799cbc3..5e47abbfc6 100644 --- a/samples/Sample_27_Field.php +++ b/samples/Sample_27_Field.php @@ -25,12 +25,12 @@ $textrun->addText(' written in a textrun.'); $section->addText('Simple text Macrobutton'); -$section->addField('MACROBUTTON', array('MacroName'=>'DoFieldClick', 'DisplayText'=>'Enter Value here'), array()); +$section->addField('MACROBUTTON', array('macroname'=>'DoFieldClick', 'displaytext'=>'Enter Value here'), array()); $section->addText('Styled text Macrobutton'); $font = new \PhpOffice\PhpWord\Style\Font(); $font->setColor('FF0000'); -$section->addField('MACROBUTTON', array('MacroName' => 'DoFieldClick', 'DisplayText' => 'Enter Value here'), array())->setFontStyle($font); +$section->addField('MACROBUTTON', array('macroname' => 'DoFieldClick', 'displaytext' => 'Enter Value here'), array())->setFontStyle($font); // Save file echo write($phpWord, basename(__FILE__, '.php'), $writers); diff --git a/src/PhpWord/Element/Field.php b/src/PhpWord/Element/Field.php index 1ad18c71c6..ffbaf1be26 100644 --- a/src/PhpWord/Element/Field.php +++ b/src/PhpWord/Element/Field.php @@ -56,7 +56,7 @@ class Field extends AbstractElement 'options'=>array('PreserveFormat', 'LunarCalendar', 'SakaEraCalendar', 'LastUsedFormat') ), 'MACROBUTTON' => array( - 'properties'=> array( 'MacroName'=>'', 'DisplayText'=>'' ) + 'properties' => array('macroname' => '', 'displaytext' => '') ) ); @@ -209,8 +209,7 @@ public function setFontStyle($style = null) throw new \InvalidArgumentException('font style must be of type Font'); } - if ($style->getNoProof()) - { + if ($style->getNoProof()) { $this->fontStyle = $style; } else { // make a copy of the font so the original is not altered @@ -228,7 +227,7 @@ public function setFontStyle($style = null) */ public function getFontStyle() { - if ($this->fontStyle == null){ + if ($this->fontStyle == null) { $font = new Font(); $font->setNoProof(true); return $font; diff --git a/src/PhpWord/Writer/Word2007/Element/Field.php b/src/PhpWord/Writer/Word2007/Element/Field.php index 67f0715ca1..b3de581f92 100644 --- a/src/PhpWord/Writer/Word2007/Element/Field.php +++ b/src/PhpWord/Writer/Word2007/Element/Field.php @@ -50,30 +50,31 @@ public function write() } /** - * write MACROBUTTON + * write 'MACROBUTTON' */ - private function writeMacrobutton(){ - $xmlWriter = $this->getXmlWriter(); - $element = $this->getElement(); - $properties = $element->getProperties(); + private function writeMacrobutton() + { + $xmlWriter = $this->getXmlWriter(); + $element = $this->getElement(); + $properties = $element->getProperties(); - $macroName = $properties['MacroName']; - $displayText = $properties['DisplayText']; + $macroName = $properties['macroname']; + $displayText = $properties['displaytext']; $this->startElementP(); $xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:fldChar'); $xmlWriter->writeAttribute('w:fldCharType', 'begin'); - $xmlWriter->endElement(); - $xmlWriter->endElement(); + $xmlWriter->endElement(); // w:fldChar + $xmlWriter->endElement(); // w:r $xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:instrText'); $xmlWriter->writeAttribute('xml:space', 'preserve'); $xmlWriter->text("MACROBUTTON {$macroName} "); - $xmlWriter->endElement(); - $xmlWriter->endElement(); + $xmlWriter->endElement(); // w:instrText + $xmlWriter->endElement(); // w:r $xmlWriter->startElement('w:r'); $this->writeFontStyle(); @@ -83,14 +84,14 @@ private function writeMacrobutton(){ } else { $xmlWriter->writeRaw($this->getText($displayText)); } - $xmlWriter->endElement(); - $xmlWriter->endElement(); + $xmlWriter->endElement(); // w:instrText + $xmlWriter->endElement(); // w:r $xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:fldChar'); $xmlWriter->writeAttribute('w:fldCharType', 'end'); - $xmlWriter->endElement(); - $xmlWriter->endElement(); + $xmlWriter->endElement(); // w:fldChar + $xmlWriter->endElement(); // w:r $this->endElementP(); // w:p } From cf7708979dc18400293823ccb9296d8b435e4657 Mon Sep 17 00:00:00 2001 From: troosan Date: Mon, 19 Feb 2018 23:36:28 +0100 Subject: [PATCH 3/3] refactoring + fixes + unit tests --- CHANGELOG.md | 1 + samples/Sample_27_Field.php | 18 ++- src/PhpWord/Element/Field.php | 3 +- src/PhpWord/Writer/Word2007/Element/Field.php | 123 ++++++++---------- src/PhpWord/Writer/Word2007/Style/Font.php | 3 +- tests/PhpWord/Writer/Word2007/ElementTest.php | 31 +++++ 6 files changed, 102 insertions(+), 77 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6f87158a6..c684a37d89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ v0.15.0 (?? ??? 2018) - Add support for fixed Table Layout @aoloe @ekopach @troosan #841 #1276 - Add support for Cell Spacing @dox07 @troosan #1040 - Add parsing of formatting inside lists @atomicalnet @troosan #594 +- Add support for MACROBUTTON field @phryneas @troosan #1021 ### Fixed - Fix reading of docx default style - @troosan #1238 diff --git a/samples/Sample_27_Field.php b/samples/Sample_27_Field.php index d86037919f..9c37dffe91 100644 --- a/samples/Sample_27_Field.php +++ b/samples/Sample_27_Field.php @@ -20,6 +20,7 @@ $section->addText('Number of pages field:'); $section->addField('NUMPAGES', array('numformat' => '0,00', 'format' => 'Arabic'), array('PreserveFormat')); +$section->addTextBreak(); $textrun = $section->addTextRun(); $textrun->addText('An index field is '); @@ -43,14 +44,19 @@ $textrun->addText('This is the date of lunar calendar '); $textrun->addField('DATE', array('dateformat' => 'd-M-yyyy H:mm:ss'), array('PreserveFormat', 'LunarCalendar')); $textrun->addText(' written in a textrun.'); +$section->addTextBreak(); -$section->addText('Simple text Macrobutton'); -$section->addField('MACROBUTTON', array('macroname'=>'DoFieldClick', 'displaytext'=>'Enter Value here'), array()); +$macroText = new TextRun(); +$macroText->addText('Double click', array('bold' => true)); +$macroText->addText(' to '); +$macroText->addText('zoom to 100%', array('italic' => true)); -$section->addText('Styled text Macrobutton'); -$font = new \PhpOffice\PhpWord\Style\Font(); -$font->setColor('FF0000'); -$section->addField('MACROBUTTON', array('macroname' => 'DoFieldClick', 'displaytext' => 'Enter Value here'), array())->setFontStyle($font); +$section->addText('A macro button with styled text:'); +$section->addField('MACROBUTTON', array('macroname' => 'Zoom100'), array(), $macroText); +$section->addTextBreak(); + +$section->addText('A macro button with simple text:'); +$section->addField('MACROBUTTON', array('macroname' => 'Zoom100'), array(), 'double click to zoom'); // Save file echo write($phpWord, basename(__FILE__, '.php'), $writers); diff --git a/src/PhpWord/Element/Field.php b/src/PhpWord/Element/Field.php index 919bb1e8db..5aeffbc168 100644 --- a/src/PhpWord/Element/Field.php +++ b/src/PhpWord/Element/Field.php @@ -57,7 +57,7 @@ class Field extends AbstractElement 'options' => array('PreserveFormat', 'LunarCalendar', 'SakaEraCalendar', 'LastUsedFormat'), ), 'MACROBUTTON' => array( - 'properties' => array('macroname' => '', 'displaytext' => '') + 'properties' => array('macroname' => ''), ), 'XE' => array( 'properties' => array(), @@ -248,6 +248,7 @@ public function getFontStyle() if ($this->fontStyle == null) { $font = new Font(); $font->setNoProof(true); + return $font; } diff --git a/src/PhpWord/Writer/Word2007/Element/Field.php b/src/PhpWord/Writer/Word2007/Element/Field.php index 46a71a9635..336a432574 100644 --- a/src/PhpWord/Writer/Word2007/Element/Field.php +++ b/src/PhpWord/Writer/Word2007/Element/Field.php @@ -17,8 +17,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; -use PhpOffice\PhpWord\Settings; - /** * Field element writer * @@ -26,39 +24,27 @@ */ class Field extends Text { - protected static $simpleFields = array('PAGE', 'NUMPAGES', 'DATE'); - /** * Write field element. */ public function write() { - $element = $this->getElement(); + $element = $this->getElement(); if (!$element instanceof \PhpOffice\PhpWord\Element\Field) { return; } - if (in_array($element->getType(), self::$simpleFields)){ - $writeField = "writeSimpleField"; + $methodName = 'write' . ucfirst(strtolower($element->getType())); + if (method_exists($this, $methodName)) { + $this->$methodName($element); } else { - $type = ucfirst(strtolower($element->getType())); - $writeField = "write{$type}"; + $this->writeDefault($element); } - $this->$writeField(); } - /** - * write 'MACROBUTTON' - */ - protected function writeMacrobutton() + private function writeDefault(\PhpOffice\PhpWord\Element\Field $element) { - $xmlWriter = $this->getXmlWriter(); - $element = $this->getElement(); - $properties = $element->getProperties(); - - $macroName = $properties['macroname']; - $displayText = $properties['displaytext']; - + $xmlWriter = $this->getXmlWriter(); $this->startElementP(); $xmlWriter->startElement('w:r'); @@ -67,41 +53,6 @@ protected function writeMacrobutton() $xmlWriter->endElement(); // w:fldChar $xmlWriter->endElement(); // w:r - $xmlWriter->startElement('w:r'); - $xmlWriter->startElement('w:instrText'); - $xmlWriter->writeAttribute('xml:space', 'preserve'); - $xmlWriter->text("MACROBUTTON {$macroName} "); - $xmlWriter->endElement(); // w:instrText - $xmlWriter->endElement(); // w:r - - $xmlWriter->startElement('w:r'); - $this->writeFontStyle(); - $xmlWriter->startElement('w:instrText'); - if (Settings::isOutputEscapingEnabled()) { - $xmlWriter->text($this->getText($displayText)); - } else { - $xmlWriter->writeRaw($this->getText($displayText)); - } - $xmlWriter->endElement(); // w:instrText - $xmlWriter->endElement(); // w:r - - $xmlWriter->startElement('w:r'); - $xmlWriter->startElement('w:fldChar'); - $xmlWriter->writeAttribute('w:fldCharType', 'end'); - $xmlWriter->endElement(); // w:fldChar - $xmlWriter->endElement(); // w:r - - $this->endElementP(); // w:p - } - - /** - * write one of 'PAGE', 'NUMPAGES' or 'DATE' - */ - protected function writeSimpleField() - { - $xmlWriter = $this->getXmlWriter(); - $element = $this->getElement(); - $instruction = ' ' . $element->getType() . ' '; if ($element->getText() != null) { if (is_string($element->getText())) { @@ -163,6 +114,51 @@ protected function writeSimpleField() $this->endElementP(); // w:p } + /** + * Writes a macrobutton field + * + * //TODO A lot of code duplication with general method, should maybe be refactored + * @param \PhpOffice\PhpWord\Element\Field $element + */ + protected function writeMacrobutton(\PhpOffice\PhpWord\Element\Field $element) + { + $xmlWriter = $this->getXmlWriter(); + $this->startElementP(); + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'begin'); + $xmlWriter->endElement(); // w:fldChar + $xmlWriter->endElement(); // w:r + + $instruction = ' ' . $element->getType() . ' ' . $this->buildPropertiesAndOptions($element); + if (is_string($element->getText())) { + $instruction .= $element->getText() . ' '; + } + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:instrText'); + $xmlWriter->writeAttribute('xml:space', 'preserve'); + $xmlWriter->text($instruction); + $xmlWriter->endElement(); // w:instrText + $xmlWriter->endElement(); // w:r + + if ($element->getText() != null) { + if ($element->getText() instanceof \PhpOffice\PhpWord\Element\TextRun) { + $containerWriter = new Container($xmlWriter, $element->getText(), true); + $containerWriter->write(); + } + } + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'end'); + $xmlWriter->endElement(); // w:fldChar + $xmlWriter->endElement(); // w:r + + $this->endElementP(); // w:p + } + private function buildPropertiesAndOptions(\PhpOffice\PhpWord\Element\Field $element) { $propertiesAndOptions = ''; @@ -178,6 +174,9 @@ private function buildPropertiesAndOptions(\PhpOffice\PhpWord\Element\Field $ele case 'dateformat': $propertiesAndOptions .= '\@ "' . $propval . '" '; break; + case 'macroname': + $propertiesAndOptions .= $propval . ' '; + break; } } @@ -207,18 +206,6 @@ private function buildPropertiesAndOptions(\PhpOffice\PhpWord\Element\Field $ele } } - $this->startElementP(); - - $xmlWriter->startElement('w:fldSimple'); - $xmlWriter->writeAttribute('w:instr', $instruction); - $xmlWriter->startElement('w:r'); - $this->writeFontStyle(); - $xmlWriter->writeElement('w:t', '1'); - $xmlWriter->endElement(); // w:r - $xmlWriter->endElement(); // w:fldSimple - - $this->endElementP(); // w:p - return $propertiesAndOptions; } } diff --git a/src/PhpWord/Writer/Word2007/Style/Font.php b/src/PhpWord/Writer/Word2007/Style/Font.php index 25ffd63b81..a13db155db 100644 --- a/src/PhpWord/Writer/Word2007/Style/Font.php +++ b/src/PhpWord/Writer/Word2007/Style/Font.php @@ -135,9 +135,8 @@ private function writeStyle() $xmlWriter->writeElementIf($style->getSpacing() !== null, 'w:spacing', 'w:val', $style->getSpacing()); $xmlWriter->writeElementIf($style->getKerning() !== null, 'w:kern', 'w:val', $style->getKerning() * 2); - // noProof - $xmlWriter->writeElementIf($style->getNoProof() !== false, 'w:noProof'); + $xmlWriter->writeElementIf($style->isNoProof() !== false, 'w:noProof'); // Background-Color $shading = $style->getShading(); diff --git a/tests/PhpWord/Writer/Word2007/ElementTest.php b/tests/PhpWord/Writer/Word2007/ElementTest.php index f91a8479da..887e8e5881 100644 --- a/tests/PhpWord/Writer/Word2007/ElementTest.php +++ b/tests/PhpWord/Writer/Word2007/ElementTest.php @@ -322,6 +322,37 @@ public function testFieldElementWithComplexText() $this->assertEquals('"\\b \\i ', $doc->getElement($element)->textContent); } + /** + * Test writing the macrobutton field + */ + public function testMacroButtonField() + { + $phpWord = new PhpWord(); + $section = $phpWord->addSection(); + + $macroText = new TextRun(); + $macroText->addText('Double click', array('bold' => true)); + $macroText->addText(' to '); + $macroText->addText('zoom to 100%', array('italic' => true)); + + $section->addField('MACROBUTTON', array('macroname' => 'Zoom100'), array(), $macroText); + $section->addField('MACROBUTTON', array('macroname' => 'Zoom100'), array(), 'double click to zoom'); + $doc = TestHelperDOCX::getDocument($phpWord); + + $element = '/w:document/w:body/w:p[1]/w:r[2]/w:instrText'; + $this->assertTrue($doc->elementExists($element)); + $this->assertEquals(' MACROBUTTON Zoom100 ', $doc->getElement($element)->textContent); + + $element = '/w:document/w:body/w:p[1]/w:r[3]/'; + $this->assertTrue($doc->elementExists($element . 'w:t')); + $this->assertEquals('Double click', $doc->getElement($element . 'w:t')->textContent); + $this->assertTrue($doc->elementExists($element . 'w:rPr/w:b')); + + $element = '/w:document/w:body/w:p[2]/w:r[2]/w:instrText'; + $this->assertTrue($doc->elementExists($element)); + $this->assertEquals(' MACROBUTTON Zoom100 double click to zoom ', $doc->getElement($element)->textContent); + } + /** * Test form fields */