Skip to content
This repository was archived by the owner on Oct 12, 2024. It is now read-only.

Commit d26e615

Browse files
authored
Merge pull request #3 from staabm/patch-1
Added new Element->appendTextNode() API
2 parents 83d00dc + 3b674f7 commit d26e615

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/Element.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,32 @@
1919
*/
2020
class Element extends \DOMElement implements XPathAware
2121
{
22+
/**
23+
* Create and append a text-node with the given name and value.
24+
*
25+
* @param string $name
26+
* @param string $value
27+
*
28+
* @return Element
29+
*/
30+
public function appendTextNode($name, $value)
31+
{
32+
$el = new self($name);
33+
$element = $this->appendChild($el);
34+
assert($element instanceof Element);
35+
36+
$element->appendChild(
37+
$this->owner()->createTextNode($value)
38+
);
39+
40+
return $element;
41+
}
42+
2243
/**
2344
* Create and append an element with the given name and optionally given value.
2445
*
46+
* Note: The value will not be escaped. Use DOMDocument::createTextNode() to create a text node with escaping support.
47+
*
2548
* @param string $name
2649
* @param mixed $value
2750
*

tests/Unit/ElementTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ public function testAppendElement(): void
3535
$this->assertInstanceOf('PhpBench\Dom\Element', $element);
3636
$this->assertEquals(1, $result);
3737
}
38+
39+
/**
40+
* It should create and append text.
41+
*/
42+
public function testAppendTextNode(): void
43+
{
44+
$element = $this->element->appendTextNode('hello', 'fix&foxy');
45+
$result = $this->document->evaluate('count(//hello)');
46+
$this->assertInstanceOf('PhpBench\Dom\Element', $element);
47+
$this->assertEquals(1, $result);
48+
}
3849

3950
/**
4051
* It should exeucte an XPath query.

0 commit comments

Comments
 (0)