Merge pull request #1832 from Matze2010/udz-checkbox-feature

Add parsing of HTML checkbox input field
This commit is contained in:
troosan 2021-02-12 20:37:35 +01:00 committed by GitHub
commit c2e2ec4004
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 0 deletions

View File

@ -193,6 +193,7 @@ class Html
'img' => array('Image', $node, $element, $styles, null, null, null), 'img' => array('Image', $node, $element, $styles, null, null, null),
'br' => array('LineBreak', null, $element, $styles, null, null, null), 'br' => array('LineBreak', null, $element, $styles, null, null, null),
'a' => array('Link', $node, $element, $styles, null, null, null), 'a' => array('Link', $node, $element, $styles, null, null, null),
'input' => array('Input', $node, $element, $styles, null, null, null),
'hr' => array('HorizRule', $node, $element, $styles, null, null, null), 'hr' => array('HorizRule', $node, $element, $styles, null, null, null),
); );
@ -266,6 +267,30 @@ class Html
return $newElement; 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 (null === $attributes->getNamedItem('type')) {
return;
}
$inputType = $attributes->getNamedItem('type')->value;
switch ($inputType) {
case 'checkbox':
$checked = ($checked = $attributes->getNamedItem('checked')) && $checked->value === 'true' ? true : false;
$textrun = $element->addTextRun($styles['paragraph']);
$textrun->addFormField('checkbox')->setValue($checked);
break;
}
}
/** /**
* Parse heading node * Parse heading node
* *

View File

@ -639,6 +639,25 @@ class HtmlTest extends AbstractWebServerEmbeddedTest
$this->assertEquals(150 * 15, $doc->getElement('/w:document/w:body/w:p/w:r/w:rPr/w:spacing')->getAttribute('w:val')); $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'));
}
/** /**
* 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
*/ */