Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
HTML checkbox input field
  • Loading branch information
Matze2010 committed Mar 1, 2020
commit 726c8caf543df3d9ada12ca954328f44ea57d6da
19 changes: 19 additions & 0 deletions src/PhpWord/Shared/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down
19 changes: 19 additions & 0 deletions tests/PhpWord/Shared/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<input type="checkbox" checked="true" /><input type="checkbox" />';
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'));
}
}