Skip to content

Commit

Permalink
Merge pull request #1 from zckrs/master
Browse files Browse the repository at this point in the history
Override DOMDocument->createElement() to avoid "unterminated entity reference" error.
  • Loading branch information
j0k3r committed Aug 11, 2014
2 parents 90168b0 + 49b7e05 commit 912ea81
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Welldom/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
51 changes: 51 additions & 0 deletions tests/Welldom/Tests/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<test></test>');
$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,
'<foo/>',
'\Welldom\Element',
'->createNode() created the element with the right name'
),
2 => array(
'bar',
'content',
null,
'<bar>content</bar>',
'\Welldom\Element',
'->createNode() created the element with the right name and the right value'
),
3 => array(
'foo',
null,
'test',
'<foo xmlns="test"/>',
'\Welldom\Element',
'->createNode() created the element with the right name'
),
4 => array(
'bar',
'content',
'test',
'<bar xmlns="test">content</bar>',
'\Welldom\Element',
'->createNode() created the element with the right name and the right value'
),
);
}

// ->createNode()

/**
Expand Down

0 comments on commit 912ea81

Please sign in to comment.