Skip to content

Commit da5afae

Browse files
committed
$61
Implemented new setText method
1 parent c1aaa0d commit da5afae

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ PHPHtmlParser is a simple, flexible, html parser which allows you to select tags
1212
Install
1313
-------
1414

15-
This package can be found on [packagist](https://packagist.org/packages/paquettg/php-html-parser) and is best loaded using [composer](http://getcomposer.org/). We support php 5.6, 7.0, and hhvm 2.3.
15+
This package can be found on [packagist](https://packagist.org/packages/paquettg/php-html-parser) and is best loaded using [composer](http://getcomposer.org/). We support php 5.6, 7.0, 7.1.
1616

1717
Usage
1818
-----
@@ -217,3 +217,13 @@ $a->delete();
217217
unset($a);
218218
echo $dom; // '<div class="all"><p>Hey bro, <br /> :)</p></div>');
219219
```
220+
221+
You can modify the text of `TextNode` objects easely. Please note that, if you set an encoding, the new text will be encoded using the existing encoding.
222+
223+
```php
224+
$dom = new Dom;
225+
$dom->load('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
226+
$a = $dom->find('a')[0];
227+
$a->firstChild()->setText('biz baz');
228+
echo $dom; // '<div class="all"><p>Hey bro, <a href="google.com">biz baz</a><br /> :)</p></div>'
229+
```

src/PHPHtmlParser/Dom/TextNode.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,24 @@ public function text()
7272
}
7373
}
7474

75+
/**
76+
* Sets the text for this node.
77+
*
78+
* @var string $text
79+
* @return void
80+
*/
81+
public function setText($text)
82+
{
83+
$this->text = $text;
84+
85+
if ( ! is_null($this->encode)) {
86+
$text = $this->encode->convert($text);
87+
88+
// remember the conversion
89+
$this->convertedText = $text;
90+
}
91+
}
92+
7593
/**
7694
* This node has no html, just return the text.
7795
*

tests/Node/TextTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

3+
use PHPHtmlParser\Dom;
34
use PHPHtmlParser\Dom\TextNode;
5+
use stringEncode\Encode;
46

57
class NodeTextTest extends PHPUnit_Framework_TestCase {
68

@@ -29,4 +31,25 @@ public function testPreserveEntity()
2931
$text = $node->innerhtml;
3032
$this->assertEquals('&#x69;', $text);
3133
}
34+
35+
public function testSetText()
36+
{
37+
$dom = new Dom;
38+
$dom->load('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
39+
$a = $dom->find('a')[0];
40+
$a->firstChild()->setText('biz baz');
41+
$this->assertEquals('<div class="all"><p>Hey bro, <a href="google.com">biz baz</a><br /> :)</p></div>', (string) $dom);
42+
}
43+
44+
public function testSetTextEncoded()
45+
{
46+
$encode = new Encode;
47+
$encode->from('UTF-8');
48+
$encode->to('UTF-8');
49+
50+
$node = new TextNode('foo bar');
51+
$node->propagateEncoding($encode);
52+
$node->setText('biz baz');
53+
$this->assertEquals('biz baz', $node->text());
54+
}
3255
}

0 commit comments

Comments
 (0)