Skip to content

Commit 726c8ca

Browse files
committed
HTML checkbox input field
1 parent b8346af commit 726c8ca

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/PhpWord/Shared/Html.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ protected static function parseNode($node, $element, $styles = array(), $data =
161161
'img' => array('Image', $node, $element, $styles, null, null, null),
162162
'br' => array('LineBreak', null, $element, $styles, null, null, null),
163163
'a' => array('Link', $node, $element, $styles, null, null, null),
164+
'input' => array('Input', $node, $element, $styles, null, null, null),
164165
);
165166

166167
$newElement = null;
@@ -233,6 +234,24 @@ protected static function parseParagraph($node, $element, &$styles)
233234
return $newElement;
234235
}
235236

237+
/**
238+
* Parse input node
239+
*
240+
* @param \DOMNode $node
241+
* @param \PhpOffice\PhpWord\Element\AbstractContainer $element
242+
* @param array &$styles
243+
*/
244+
protected static function parseInput($node, $element, &$styles)
245+
{
246+
$attributes = $node->attributes;
247+
248+
if (($type = $attributes->getNamedItem('type')->value) === 'checkbox') {
249+
$checked = ($checked = $attributes->getNamedItem('checked')) && $checked->value === "true" ?? false;
250+
$textrun = $element->addTextRun();
251+
$textrun->addFormField('checkbox')->setValue($checked);
252+
}
253+
}
254+
236255
/**
237256
* Parse heading node
238257
*

tests/PhpWord/Shared/HtmlTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,4 +632,23 @@ public function testParseLetterSpacing()
632632
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:spacing'));
633633
$this->assertEquals(150 * 15, $doc->getElement('/w:document/w:body/w:p/w:r/w:rPr/w:spacing')->getAttribute('w:val'));
634634
}
635+
636+
/**
637+
* Tests checkbox input field
638+
*/
639+
public function testInputCheckbox()
640+
{
641+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
642+
$section = $phpWord->addSection();
643+
$html = '<input type="checkbox" checked="true" /><input type="checkbox" />';
644+
Html::addHtml($section, $html);
645+
646+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
647+
648+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p[1]/w:r/w:fldChar/w:ffData/w:checkBox'));
649+
$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'));
650+
651+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p[2]/w:r/w:fldChar/w:ffData/w:checkBox'));
652+
$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'));
653+
}
635654
}

0 commit comments

Comments
 (0)