Skip to content

Commit bc49eac

Browse files
committed
Fix QPXML::pi() lookup by prefix
1 parent 91bd1e7 commit bc49eac

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ QueryPath Changelog
1919
- Add `composer run test:examples` (and `test:examples:network`) to run the examples locally
2020
- Fix processing instructions gaining an extra `?` each time an HTML-parsed document was serialized, so `<?php echo $title; ?>` came back out of `html()`, `innerHTML()`, `innerXML()`, `innerXHTML()`, `xml()`, `html5()`, `innerHTML5()`, and `writeXML()` as `<?php echo $title; ??>`. libxml's HTML parser keeps the closing `?` as part of the node's data, unlike its XML parser and the Masterminds HTML5 parser, so QueryPath now strips it on load. `DOMProcessingInstruction::$data` consequently no longer has a stray `?` on the end for documents read with `htmlqp()` or `qp()` on an `.html` file. Note that this normalisation applies only to documents QueryPath parses itself, and that taking the underlying `DOMDocument` out of QueryPath and calling libxml's own `saveHTML()` on it will emit `<?php ... >` without the terminator, since libxml's HTML serializer never adds one
2121
- Add `QueryPath\Document`, a `DOMDocument` subclass QueryPath parses into. The type is how QueryPath records that a document's processing instruction data does not carry the closing `?`, which cannot be determined by inspecting the document afterwards. It travels with the document, so every route to a second `DOMQuery` over one document -- iteration, `add()`, `remove()`, `replaceAll()`, `branch()`, `QueryPath::with()`, and the bundled extensions -- serializes it correctly. A `DOMDocument` supplied by the caller is a plain `DOMDocument`, makes no such promise, and is still serialized exactly as it was handed over
22+
- Fix `QPXML::pi($prefix)` always returning `NULL` with an "Undefined property" warning. It now reads the processing instruction's `target` and returns the matching instruction (issue #79)
2223
- Fix fatal error when running a CSS selector against a match set that contains non-element nodes (text, comment, CDATA or processing instruction), as `contents()` returns. Those nodes now simply do not match, instead of calling element-only DOM methods on them
2324
- Fix `find('a > b')` throwing a `TypeError` when the left-hand side matched the root element, whose parent is the document rather than an element
2425
- Fix the `:text` pseudo-class so it matches jQuery: it selects `input` elements whose `type` attribute is absent or is `text` (case-insensitively). It never indicated, and still does not indicate, whether a node is a text node

‎src/Extension/QPXML.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public function pi($prefix = null, $text = null)
144144
foreach ($ele->childNodes as $node) {
145145
if ($node->nodeType == XML_PI_NODE) {
146146
if (isset($prefix)) {
147-
if ($node->tagName == $prefix) {
147+
if ($node->target === $prefix) {
148148
return $node->textContent;
149149
}
150150
} else {

‎tests/QueryPath/Extension/QPXMLTest.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,15 @@ public function testProcessingInstruction()
4141
$msg = "Message";
4242
$this->assertEquals($msg, qp($this->file, 'second')->pi('qp', $msg)->top()->find('second')->pi());
4343
}
44+
45+
public function testProcessingInstructionByPrefix()
46+
{
47+
$xml = '<?xml version="1.0"?><root><?first one?><?second two?></root>';
48+
49+
$this->assertEquals('two', qp($xml, 'root')->pi('second'));
50+
$this->assertNull(qp($xml, 'root')->pi('missing'));
51+
52+
// A processing instruction written by prefix can be read back by the same prefix.
53+
$this->assertEquals('Message', qp($this->file, 'second')->pi('qp', 'Message')->top()->find('second')->pi('qp'));
54+
}
4455
}

0 commit comments

Comments
 (0)