Skip to content

Commit c2e2ec4

Browse files
authored
Merge pull request #1832 from Matze2010/udz-checkbox-feature
Add parsing of HTML checkbox input field
2 parents 4c5e760 + 659d3d7 commit c2e2ec4

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/PhpWord/Shared/Html.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ protected static function parseNode($node, $element, $styles = array(), $data =
193193
'img' => array('Image', $node, $element, $styles, null, null, null),
194194
'br' => array('LineBreak', null, $element, $styles, null, null, null),
195195
'a' => array('Link', $node, $element, $styles, null, null, null),
196+
'input' => array('Input', $node, $element, $styles, null, null, null),
196197
'hr' => array('HorizRule', $node, $element, $styles, null, null, null),
197198
);
198199

@@ -266,6 +267,30 @@ protected static function parseParagraph($node, $element, &$styles)
266267
return $newElement;
267268
}
268269

270+
/**
271+
* Parse input node
272+
*
273+
* @param \DOMNode $node
274+
* @param \PhpOffice\PhpWord\Element\AbstractContainer $element
275+
* @param array &$styles
276+
*/
277+
protected static function parseInput($node, $element, &$styles)
278+
{
279+
$attributes = $node->attributes;
280+
if (null === $attributes->getNamedItem('type')) {
281+
return;
282+
}
283+
284+
$inputType = $attributes->getNamedItem('type')->value;
285+
switch ($inputType) {
286+
case 'checkbox':
287+
$checked = ($checked = $attributes->getNamedItem('checked')) && $checked->value === 'true' ? true : false;
288+
$textrun = $element->addTextRun($styles['paragraph']);
289+
$textrun->addFormField('checkbox')->setValue($checked);
290+
break;
291+
}
292+
}
293+
269294
/**
270295
* Parse heading node
271296
*

tests/PhpWord/Shared/HtmlTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,25 @@ public function testParseLetterSpacing()
639639
$this->assertEquals(150 * 15, $doc->getElement('/w:document/w:body/w:p/w:r/w:rPr/w:spacing')->getAttribute('w:val'));
640640
}
641641

642+
/**
643+
* Tests checkbox input field
644+
*/
645+
public function testInputCheckbox()
646+
{
647+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
648+
$section = $phpWord->addSection();
649+
$html = '<input type="checkbox" checked="true" /><input type="checkbox" />';
650+
Html::addHtml($section, $html);
651+
652+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
653+
654+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p[1]/w:r/w:fldChar/w:ffData/w:checkBox'));
655+
$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'));
656+
657+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p[2]/w:r/w:fldChar/w:ffData/w:checkBox'));
658+
$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'));
659+
}
660+
642661
/**
643662
* Parse widths in tables and cells, which also allows for controlling column width
644663
*/

0 commit comments

Comments
 (0)