Skip to content

Commit 833293a

Browse files
committed
add set html function in html method
1 parent b550f7e commit 833293a

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ echo $links->get(-1)->textContent; // output 3
7070
#### Manipulation > DOM Insertion, Inside
7171

7272
- `.text( [text] )`
73-
- `.html()`
73+
- `.html( [html_string] )`
7474
- `.append( [content], )`
7575
- `.prepend( [content], )`
7676

src/Rct567/DomQuery/DomQuery.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public function find(string $css_expression)
247247
* Get the combined text contents of each element in the set of matched elements, including their descendants,
248248
* or set the text contents of the matched elements.
249249
*
250-
* @param string $val
250+
* @param string|null $val
251251
*
252252
* @return self|string|void
253253
*/
@@ -259,7 +259,7 @@ public function text($val=null)
259259
$node->nodeValue = $val;
260260
}
261261

262-
return $this;
262+
return $this;
263263
} else { // get value for first node
264264
if ($node = $this->getFirstElmNode()) {
265265
return $node->nodeValue;
@@ -270,19 +270,29 @@ public function text($val=null)
270270
/**
271271
* Get the HTML contents of the first element in the set of matched elements
272272
*
273-
* @return string
273+
* @param string|null $html_string
274+
*
275+
* @return self|string|void
274276
*/
275-
public function html()
277+
public function html($html_string=null)
276278
{
277-
if ($node = $this->getFirstElmNode()) {
278-
$html = '';
279-
$document = $node->ownerDocument;
280-
281-
foreach ($node->childNodes as $node) {
282-
$html .= $document->saveHTML($node);
279+
if (!is_null($html_string)) { // set html for all nodes
280+
foreach ($this as $node) {
281+
$node->get(0)->nodeValue = '';
282+
$node->append($html_string);
283283
}
284+
return $this;
285+
} else { // get html for first node
286+
if ($node = $this->getFirstElmNode()) {
287+
$html = '';
288+
$document = $node->ownerDocument;
284289

285-
return $html;
290+
foreach ($node->childNodes as $node) {
291+
$html .= $document->saveHTML($node);
292+
}
293+
294+
return $html;
295+
}
286296
}
287297
}
288298

tests/Rct567/DomQuery/Tests/DomQueryTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ public function testFilter()
322322
}
323323

324324
/*
325-
* Test html
325+
* Test get html
326326
*/
327327
public function testGetHtml()
328328
{
@@ -332,6 +332,16 @@ public function testGetHtml()
332332
$this->assertEquals('A', $dom->find('a i')->html());
333333
}
334334

335+
/*
336+
* Test set html
337+
*/
338+
public function testSetHtml()
339+
{
340+
$dom = new Domquery('<p> <a>M<i>A</i></a> <span></span> </p>');
341+
$dom->find('a')->html('<i>x</i>');
342+
$this->assertEquals('<p> <a><i>x</i></a> <span></span> </p>', (string) $dom);
343+
}
344+
335345
/*
336346
* Test instance without nodes
337347
*/

0 commit comments

Comments
 (0)