Skip to content

Commit

Permalink
Reverse logic of html5() Method
Browse files Browse the repository at this point in the history
Resolves issue where calling $qp('<strong>Content</strong>')->html5() would return an empty string, and calling $qp('<strong>Content</strong>')->html5('') would return <strong>Content</strong>. The correct logic, per ->html(), is to reverse how this method works.
  • Loading branch information
jakejackson1 committed Aug 11, 2024
1 parent a9da950 commit 2ecba48
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
QueryPath Changelog
===========================

# 3.2.4

- Reverse logic in DomQuery::html5() so that DomQuery::html5() returns the content of the current match, and DomQuery::html5('') replaces the content of the current matches. This matches the existing logic used in DomQuery::html().

# 3.2.3

- Add PHP 8.3 Support
Expand Down
8 changes: 4 additions & 4 deletions src/DOMQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,22 +708,22 @@ public function html($markup = null)
}

/**
* Write the QueryPath document to HTML5.
*
* See html()
* Set or get the markup for an element using the HTML5 parser
*
* @param null|string $markup
*
* @return null|DOMQuery|string
* @throws QueryPath
* @throws \QueryPath\Exception
*
* @see html()
*/
public function html5($markup = null)
{
$html5 = new HTML5($this->options);

// append HTML to existing
if ($markup === null) {
if (isset($markup)) {
// Parse the HTML and insert it into the DOM
$doc = $html5->loadHTMLFragment($markup);
$this->removeChildren();
Expand Down
60 changes: 58 additions & 2 deletions tests/QueryPath/DOMQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,45 @@ public function testDOMQueryHtmlConstructors()

public function testHtml5()
{
$doc = qp(DATA_HTML_FILE);
$this->assertEquals('range', $doc->find('input')->attr('type'));
$file = DATA_FILE;
$qp = qp($file, 'unary');
$html = '<b>test</b>';
$this->assertEquals($html, $qp->html5($html)->find('b')->html5());

$html = '<html><head><title>foo</title></head><body>bar</body></html>';
// We expect a DocType to be prepended:
$this->assertEquals('<!DOCTYPE', substr(qp($html)->html5(), 0, 9));

// Check that HTML is not added to empty finds. Note the # is for a special
// case.
$this->assertEquals('', qp($html, '#nonexistant')->html5('<p>Hello</p>')->html5());
$this->assertEquals('', qp($html, 'nonexistant')->html5('<p>Hello</p>')->html5());

// We expect NULL if the document is empty.
$this->assertNull(qp()->html5());

// Non-DOMNodes should not be rendered:
$fn = 'mapCallbackFunction';
$this->assertNull(qp($file, 'li')->map([$this, $fn])->html5());

// Check html5() getter works correctly
$this->assertSame('<span>Content</span>', qp('<span>Content</span>')->find('span')->html5());

// Check html5() gets the first match only
$this->assertSame('<td>Foo</td>', qp(DATA_HTML_FILE, 'td')->html5());

// Check html5() setter works correctly
$this->assertSame('', qp('<span>Content</span>')->html5('')->innerHTML5());

// Check html5() setter works on all matches
$this->assertSame('
<tr>
<td>FooBar</td>
</tr>
<tr>
<td>FooBar</td>
</tr>
', qp(DATA_HTML_FILE, 'td')->html5('FooBar')->parent('table')->innerHTML5());
}

public function testInnerHtml5()
Expand Down Expand Up @@ -1189,6 +1226,25 @@ public function testHTML()
// Non-DOMNodes should not be rendered:
$fn = 'mapCallbackFunction';
$this->assertNull(qp($file, 'li')->map([$this, $fn])->html());

// Check html() getter works correctly
$this->assertSame('<span>Content</span>', qp('<span>Content</span>')->find('span')->html());

// Check html() gets the first match only
$this->assertSame('<td>Foo</td>', qp(DATA_HTML_FILE, 'td')->html());

// Check html() setter works correctly
$this->assertSame('', qp('<span>Content</span>')->html('')->innerHTML());

// Check html() setter works on all matches
$this->assertSame('
<tr>
<td>FooBar</td>
</tr>
<tr>
<td>FooBar</td>
</tr>
', qp(DATA_HTML_FILE, 'td')->html('FooBar')->parent('table')->innerHTML());
}

public function testInnerHTML()
Expand Down

0 comments on commit 2ecba48

Please sign in to comment.