From 726c8caf543df3d9ada12ca954328f44ea57d6da Mon Sep 17 00:00:00 2001 From: Matze2010 Date: Sun, 1 Mar 2020 18:15:27 +0000 Subject: [PATCH 1/9] HTML checkbox input field --- src/PhpWord/Shared/Html.php | 19 +++++++++++++++++++ tests/PhpWord/Shared/HtmlTest.php | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index 54e9509e5f..ea5322492f 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -161,6 +161,7 @@ protected static function parseNode($node, $element, $styles = array(), $data = 'img' => array('Image', $node, $element, $styles, null, null, null), 'br' => array('LineBreak', null, $element, $styles, null, null, null), 'a' => array('Link', $node, $element, $styles, null, null, null), + 'input' => array('Input', $node, $element, $styles, null, null, null), ); $newElement = null; @@ -233,6 +234,24 @@ protected static function parseParagraph($node, $element, &$styles) return $newElement; } + /** + * Parse input node + * + * @param \DOMNode $node + * @param \PhpOffice\PhpWord\Element\AbstractContainer $element + * @param array &$styles + */ + protected static function parseInput($node, $element, &$styles) + { + $attributes = $node->attributes; + + if (($type = $attributes->getNamedItem('type')->value) === 'checkbox') { + $checked = ($checked = $attributes->getNamedItem('checked')) && $checked->value === "true" ?? false; + $textrun = $element->addTextRun(); + $textrun->addFormField('checkbox')->setValue($checked); + } + } + /** * Parse heading node * diff --git a/tests/PhpWord/Shared/HtmlTest.php b/tests/PhpWord/Shared/HtmlTest.php index 5bc9e2411a..ac7bd44f90 100644 --- a/tests/PhpWord/Shared/HtmlTest.php +++ b/tests/PhpWord/Shared/HtmlTest.php @@ -632,4 +632,23 @@ public function testParseLetterSpacing() $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')); } + + /** + * Tests checkbox input field + */ + public function testInputCheckbox() + { + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $section = $phpWord->addSection(); + $html = ''; + 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')); + } } From ad8eeccd724650bcc9e75379aceb7760f40c0a08 Mon Sep 17 00:00:00 2001 From: Matze2010 Date: Mon, 2 Mar 2020 06:00:51 +0000 Subject: [PATCH 2/9] remove dead code --- src/PhpWord/Shared/Html.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index ea5322492f..f917951b05 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -244,11 +244,14 @@ protected static function parseParagraph($node, $element, &$styles) protected static function parseInput($node, $element, &$styles) { $attributes = $node->attributes; - - if (($type = $attributes->getNamedItem('type')->value) === 'checkbox') { - $checked = ($checked = $attributes->getNamedItem('checked')) && $checked->value === "true" ?? false; - $textrun = $element->addTextRun(); - $textrun->addFormField('checkbox')->setValue($checked); + $inputType = $attributes->getNamedItem('type')->value; + + switch ($inputType) { + case 'checkbox': + $checked = ($checked = $attributes->getNamedItem('checked')) && $checked->value === "true" ?? false; + $textrun = $element->addTextRun(); + $textrun->addFormField('checkbox')->setValue($checked); + break; } } From 258b9a65c888a0f8cca29a60a6f6a0b0d6875416 Mon Sep 17 00:00:00 2001 From: Matze2010 Date: Mon, 2 Mar 2020 06:10:14 +0000 Subject: [PATCH 3/9] fix --- src/PhpWord/Shared/Html.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index f917951b05..bd564a40ef 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -244,8 +244,11 @@ protected static function parseParagraph($node, $element, &$styles) protected static function parseInput($node, $element, &$styles) { $attributes = $node->attributes; - $inputType = $attributes->getNamedItem('type')->value; + if (null === $attributes->getNamedItem('type')) { + return; + } + $inputType = $attributes->getNamedItem('type')->value; switch ($inputType) { case 'checkbox': $checked = ($checked = $attributes->getNamedItem('checked')) && $checked->value === "true" ?? false; From 486321bb02c67c7ddc717df3c847cf1d620adc64 Mon Sep 17 00:00:00 2001 From: troosan Date: Mon, 8 Feb 2021 00:58:00 +0100 Subject: [PATCH 4/9] Compatibility with old PHP versions --- src/PhpWord/Shared/Html.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index 244e4aa538..1bd2983302 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -284,7 +284,7 @@ protected static function parseInput($node, $element, &$styles) $inputType = $attributes->getNamedItem('type')->value; switch ($inputType) { case 'checkbox': - $checked = ($checked = $attributes->getNamedItem('checked')) && $checked->value === "true" ?? false; + $checked = ($checked = $attributes->getNamedItem('checked')) && $checked->value === "true" ? true ? false; $textrun = $element->addTextRun(); $textrun->addFormField('checkbox')->setValue($checked); break; From cba1edc9e8c7297a2ebc1b689e6ac4350cc73cf8 Mon Sep 17 00:00:00 2001 From: troosan Date: Mon, 8 Feb 2021 00:59:44 +0100 Subject: [PATCH 5/9] remove trailing whitespaces --- tests/PhpWord/Shared/HtmlTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/PhpWord/Shared/HtmlTest.php b/tests/PhpWord/Shared/HtmlTest.php index a6f0b3b661..c12f33aca3 100644 --- a/tests/PhpWord/Shared/HtmlTest.php +++ b/tests/PhpWord/Shared/HtmlTest.php @@ -636,12 +636,12 @@ public function testParseLetterSpacing() /** * Tests checkbox input field */ - public function testInputCheckbox() + public function testInputCheckbox() { $phpWord = new \PhpOffice\PhpWord\PhpWord(); $section = $phpWord->addSection(); $html = ''; - Html::addHtml($section, $html); + Html::addHtml($section, $html); $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); From 8bb0b73733d5b54dfc0f09cc1a8e7b9b85feeb6d Mon Sep 17 00:00:00 2001 From: troosan Date: Wed, 10 Feb 2021 21:10:06 +0100 Subject: [PATCH 6/9] fix merge --- tests/PhpWord/Shared/HtmlTest.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/PhpWord/Shared/HtmlTest.php b/tests/PhpWord/Shared/HtmlTest.php index fd5d56644e..c6061632e2 100644 --- a/tests/PhpWord/Shared/HtmlTest.php +++ b/tests/PhpWord/Shared/HtmlTest.php @@ -640,7 +640,6 @@ public function testParseLetterSpacing() } /** -<<<<<<< HEAD * Tests checkbox input field */ public function testInputCheckbox() @@ -659,13 +658,9 @@ public function testInputCheckbox() $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 - */ -======= + /** * Parse widths in tables and cells, which also allows for controlling column width */ ->>>>>>> branch 'develop' of https://github.com/PHPOffice/PHPWord public function testParseTableAndCellWidth() { $phpWord = new \PhpOffice\PhpWord\PhpWord(); From a382f9d754cd29b25c356cca29f0ae8f7951e3ac Mon Sep 17 00:00:00 2001 From: troosan Date: Thu, 11 Feb 2021 19:25:28 +0100 Subject: [PATCH 7/9] fix --- src/PhpWord/Shared/Html.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index 982f946260..dfc9fe39c3 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -284,7 +284,7 @@ protected static function parseInput($node, $element, &$styles) $inputType = $attributes->getNamedItem('type')->value; switch ($inputType) { case 'checkbox': - $checked = ($checked = $attributes->getNamedItem('checked')) && $checked->value === "true" ? true ? false; + $checked = ($checked = $attributes->getNamedItem('checked')) && $checked->value === "true" ? true : false; $textrun = $element->addTextRun(); $textrun->addFormField('checkbox')->setValue($checked); break; From bed1f7afbfb808a67db01621c8883614e03e7232 Mon Sep 17 00:00:00 2001 From: troosan Date: Thu, 11 Feb 2021 21:44:20 +0100 Subject: [PATCH 8/9] pass the paragraph style --- src/PhpWord/Shared/Html.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index dfc9fe39c3..23a9e75d65 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -285,7 +285,7 @@ protected static function parseInput($node, $element, &$styles) switch ($inputType) { case 'checkbox': $checked = ($checked = $attributes->getNamedItem('checked')) && $checked->value === "true" ? true : false; - $textrun = $element->addTextRun(); + $textrun = $element->addTextRun($styles['paragraph']); $textrun->addFormField('checkbox')->setValue($checked); break; } From 659d3d7b4bbcfbf530f1cfa0f69f4a04226f9a3d Mon Sep 17 00:00:00 2001 From: troosan Date: Fri, 12 Feb 2021 20:16:18 +0100 Subject: [PATCH 9/9] Update Html.php fix --- src/PhpWord/Shared/Html.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index 23a9e75d65..eabd102d61 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -284,7 +284,7 @@ protected static function parseInput($node, $element, &$styles) $inputType = $attributes->getNamedItem('type')->value; switch ($inputType) { case 'checkbox': - $checked = ($checked = $attributes->getNamedItem('checked')) && $checked->value === "true" ? true : false; + $checked = ($checked = $attributes->getNamedItem('checked')) && $checked->value === 'true' ? true : false; $textrun = $element->addTextRun($styles['paragraph']); $textrun->addFormField('checkbox')->setValue($checked); break;