diff --git a/src/Welldom/Document.php b/src/Welldom/Document.php index 40e2a0d..803ffdd 100644 --- a/src/Welldom/Document.php +++ b/src/Welldom/Document.php @@ -319,6 +319,22 @@ public function queryOne($expression, $contextNode = null) return $this->getXpath()->queryOne($expression, $contextNode); } + /** + * A fix to avoid "unterminated entity reference" error with unescaping string + * {@inheritDoc} + * + * @return \Welldom\Element + */ + public function createElement($name, $value = null, $namespaceUri = null) { + $element = new Element($name, null, $namespaceUri); + $element = $this->importNode($element); + if (!empty($value)) { + $element->appendChild(new Text($value)); + } + + return $element; + } + /** * Create node * diff --git a/tests/Welldom/Tests/DocumentTest.php b/tests/Welldom/Tests/DocumentTest.php index ba09cbe..bdf98ae 100644 --- a/tests/Welldom/Tests/DocumentTest.php +++ b/tests/Welldom/Tests/DocumentTest.php @@ -169,6 +169,57 @@ public function testGetElementsByTagName() $this->assertInstanceOf('\Welldom\NodeList', $nodes, '->getElementsByTagName() returns an instance of \Welldom\NodeList'); } +// ->createElement() + + /** + * @dataProvider dataForTestCreateElement + */ + public function testCreateElement($name, $value, $namespaceUri, $expected, $expectedClass, $message) + { + $doc = Document::create(''); + $element = $doc->createElement($name, $value, $namespaceUri); + $this->assertEquals($expected, $element->getXml(), $message); + $this->assertInstanceOf($expectedClass, $element, '->createElement() returns the created element'); + } + + public function dataForTestCreateElement() + { + return array( + 1 => array( + 'foo', + null, + null, + '', + '\Welldom\Element', + '->createNode() created the element with the right name' + ), + 2 => array( + 'bar', + 'content', + null, + 'content', + '\Welldom\Element', + '->createNode() created the element with the right name and the right value' + ), + 3 => array( + 'foo', + null, + 'test', + '', + '\Welldom\Element', + '->createNode() created the element with the right name' + ), + 4 => array( + 'bar', + 'content', + 'test', + 'content', + '\Welldom\Element', + '->createNode() created the element with the right name and the right value' + ), + ); + } + // ->createNode() /**