From ba29c8302156ad9bf1c71dfa6270999e9c0be50d Mon Sep 17 00:00:00 2001 From: Sami Puro Date: Wed, 2 Dec 2026 00:04:45 +0200 Subject: [PATCH] Fix dataURL() returning NULL for MIME types such as application/octet-stream --- CHANGELOG.md | 1 + src/DOMQuery.php | 3 ++- tests/QueryPath/DOMQueryTest.php | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a34d87..3661679 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ QueryPath Changelog - Consolidate the sibling and ancestor walks behind `next()`, `prev()`, `nextAll()`, `prevAll()`, `nextUntil()`, `prevUntil()`, `parent()`, `parents()`, `parentsUntil()` and `closest()` into one helper, so a fix to one no longer has to be repeated in its mirror (issue #115). As a side effect, `parentsUntil()` no longer loops forever on an element that is not attached to the document, and `nextUntil('')` and `prevUntil('')` treat an empty selector as no selector, as the other methods already did, instead of throwing `QueryPath\CSS\ParseException`. `closest('')` returns an empty set, as in jQuery. `nextUntil()`, `prevUntil()` and `parentsUntil()` also stop at an element already collected from an earlier starting element, as `nextAll()` and `prevAll()` do, so `nextUntil()` on each of 2000 siblings falls from about 100 ms to 1 ms - **Behaviour change.** `prevAll()`, `prevUntil()`, `nextAll()`, `nextUntil()`, `siblings()`, `parent()` and `closest()` now return the results for a multi-element set in jQuery's order, instead of grouped by starting element (issue #117). `prevAll()` and `prevUntil()` return reverse document order, as `parents()` and `parentsUntil()` already did, and the others return document order. `qp($xml, 'c')->add('d')->prevAll()` now returns `c, b, a` rather than `b, a, c`. `next()` and `prev()` keep the order of the starting elements, as in jQuery, and the results for a single starting element are unchanged - **Behaviour change.** A positional pseudo-class (`:first`, `:eq()`, `:last`, ...) in the stop selector of `nextUntil()`, `prevUntil()` and `parentsUntil()` now counts across the whole document, as jQuery's does, instead of seeing each sibling or ancestor as a one-element set (issue #118). `nextUntil(':first')` used to stop at once and now walks to the end, and `nextUntil(':eq(3)')` stops at the fourth element in the document. `closest()` no longer matches a positional selector at all, as in jQuery, where `closest(':first')` used to return the element itself. `next()`, `prev()` and `parent()` still test each candidate on its own, which agrees with jQuery for a single starting element +- Fix `dataURL($attr)` returning `NULL` for a data URL whose MIME type contains punctuation, including the `application/octet-stream` default that `dataURL($attr, $data)` writes, so the getter could not read back what the setter wrote. Types such as `image/svg+xml` are now read too # 4.3.0 diff --git a/src/DOMQuery.php b/src/DOMQuery.php index 4c9028b..24dcb94 100644 --- a/src/DOMQuery.php +++ b/src/DOMQuery.php @@ -395,7 +395,8 @@ public function dataURL($attr, $data = null, $mime = 'application/octet-stream', } // So 1 and 2 should be MIME types, and 3 should be the base64-encoded data. - $regex = '/^data:([a-zA-Z0-9]+)\/([a-zA-Z0-9]+);base64,(.*)$/'; + // The type and subtype accept the characters RFC 6838 allows, such as the "-" in application/octet-stream. + $regex = '/^data:([a-zA-Z0-9!#$&^_.+-]+)\/([a-zA-Z0-9!#$&^_.+-]+);base64,(.*)$/'; $matches = []; preg_match($regex, $data, $matches); diff --git a/tests/QueryPath/DOMQueryTest.php b/tests/QueryPath/DOMQueryTest.php index 70f086b..5409968 100644 --- a/tests/QueryPath/DOMQueryTest.php +++ b/tests/QueryPath/DOMQueryTest.php @@ -2663,6 +2663,22 @@ public function testDataURL() $this->assertEquals('text/plain', $result['mime'], 'Should return the original MIME'); } + public function testDataURLRoundTripsMimeTypesWithPunctuation() + { + $xml = ''; + $qp = qp($xml, 'item'); + + // The default MIME type, written when none is given. + $qp->dataURL('secret', 'Hi!'); + $this->assertSame(['mime' => 'application/octet-stream', 'data' => 'Hi!'], $qp->dataURL('secret')); + + $qp->dataURL('secret', '', 'image/svg+xml'); + $this->assertSame(['mime' => 'image/svg+xml', 'data' => ''], $qp->dataURL('secret')); + + $qp->dataURL('secret', '{}', 'application/vnd.api+json'); + $this->assertSame(['mime' => 'application/vnd.api+json', 'data' => '{}'], $qp->dataURL('secret')); + } + public function testEncodeDataURL() { $data = QueryPath::encodeDataURL('Hi!', 'text/plain');