From 726c8caf543df3d9ada12ca954328f44ea57d6da Mon Sep 17 00:00:00 2001 From: Matze2010 Date: Sun, 1 Mar 2020 18:15:27 +0000 Subject: [PATCH] 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 54e9509e..ea532249 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -161,6 +161,7 @@ class Html '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 @@ class Html 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 5bc9e241..ac7bd44f 100644 --- a/tests/PhpWord/Shared/HtmlTest.php +++ b/tests/PhpWord/Shared/HtmlTest.php @@ -632,4 +632,23 @@ class HtmlTest extends AbstractWebServerEmbeddedTest $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')); + } }