From a78ef9bf9391ea5956297c93651e4db5ca2704be Mon Sep 17 00:00:00 2001 From: Sami Puro Date: Wed, 2 Dec 2026 00:04:37 +0200 Subject: [PATCH] Split class attributes on any whitespace in hasClass(), removeClass() and class selectors --- CHANGELOG.md | 1 + src/CSS/DOMTraverser.php | 2 +- src/Helpers/QueryChecks.php | 2 +- src/Helpers/QueryMutators.php | 4 ++-- tests/QueryPath/DOMQueryTest.php | 17 +++++++++++++++++ 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a34d87..ef38027 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 `hasClass()`, `removeClass()` and a class selector with no element name (`find('.two')`, `html5qp($html, '.two')`) splitting the `class` attribute on spaces only, so a class separated from the others by a tab or newline was not found. The HTML parsers keep those characters in attribute values. `p.two` already matched, and still does # 4.3.0 diff --git a/src/CSS/DOMTraverser.php b/src/CSS/DOMTraverser.php index 7f443c9..bd1d762 100644 --- a/src/CSS/DOMTraverser.php +++ b/src/CSS/DOMTraverser.php @@ -820,7 +820,7 @@ protected function initialMatchOnClasses(SimpleSelector $selector, SplObjectStor /** @var DOMElement $subNode */ foreach ($nl as $subNode) { $classes = $subNode->getAttribute('class'); - $classArray = explode(' ', $classes); + $classArray = preg_split('/\s+/', $classes); $intersect = array_intersect($selector->classes, $classArray); if (count($intersect) === count($selector->classes)) { diff --git a/src/Helpers/QueryChecks.php b/src/Helpers/QueryChecks.php index 8073a94..e987177 100644 --- a/src/Helpers/QueryChecks.php +++ b/src/Helpers/QueryChecks.php @@ -149,7 +149,7 @@ public function hasClass($class): bool { foreach ($this->elements() as $m) { if ($m->hasAttribute('class')) { - $vals = explode(' ', $m->getAttribute('class')); + $vals = preg_split('/\s+/', $m->getAttribute('class')); if (in_array($class, $vals)) { return true; } diff --git a/src/Helpers/QueryMutators.php b/src/Helpers/QueryMutators.php index 93ab6b6..934c33c 100644 --- a/src/Helpers/QueryMutators.php +++ b/src/Helpers/QueryMutators.php @@ -655,10 +655,10 @@ public function removeClass($class = false): Query $m->removeAttribute('class'); } } else { - $to_remove = array_filter(explode(' ', $class)); + $to_remove = array_filter(preg_split('/\s+/', $class)); foreach ($this->elements() as $m) { if ($m->hasAttribute('class')) { - $vals = array_filter(explode(' ', $m->getAttribute('class'))); + $vals = array_filter(preg_split('/\s+/', $m->getAttribute('class'))); $buf = []; foreach ($vals as $v) { if (! in_array($v, $to_remove)) { diff --git a/tests/QueryPath/DOMQueryTest.php b/tests/QueryPath/DOMQueryTest.php index 70f086b..e6c40bb 100644 --- a/tests/QueryPath/DOMQueryTest.php +++ b/tests/QueryPath/DOMQueryTest.php @@ -1530,6 +1530,23 @@ public function testRemoveClass() )->addClass('testClass')->removeClass('innerClass')->hasClass('testClass')); } + public function testClassMethodsSplitOnAnyWhitespace() + { + // The HTML parsers keep tabs and newlines in attribute values, unlike the XML parser. + $html = "

a

b
"; + + $this->assertTrue(html5qp($html, 'p')->hasClass('one')); + $this->assertTrue(html5qp($html, 'p')->hasClass('two')); + $this->assertSame('two', html5qp($html, 'p')->removeClass('one')->attr('class')); + $this->assertSame('three', html5qp($html, 'div')->removeClass('four')->attr('class')); + + // A class selector with no element name finds them as well. + $this->assertSame(1, html5qp($html)->find('.one')->count()); + $this->assertSame(1, html5qp($html)->find('.two')->count()); + $this->assertSame(1, html5qp($html)->find('.three.four')->count()); + $this->assertSame(1, html5qp($html, '.four')->count()); + } + public function testAdd() { $file = DATA_FILE;