- Behaviour change. Fix the jQuery positional pseudo-classes (
:eq(),:first,:last,:lt(),:gt(),:odd,:even, and the:nth()alias of:eq()), which indexed an element's siblings instead of the matched set (issue #66). They are now applied, zero-indexed and in document order, to the set the selector matched, as in jQuery. Most visibly,:eq(0)now matches the first result instead of never matching.:lt(),:gt(),:eq()and:nth()are zero-indexed where they were previously one-indexed,:odd/:evennow index the matched set rather than sibling position, and a negative index counts back from the end of the set (:eq(-1)is the last match). The CSS structural pseudo-classes (:nth-child(),:nth-of-type(),:first-child,:last-child, ...) are unchanged and still count siblings, one-indexed - Make
QueryPath\CSS\QueryPathEventHandler, the legacy selector engine, agree withQueryPath\CSS\DOMTraverseron the positional pseudo-classes, except in the argument of:not() - Support positional pseudo-classes on any simple selector in a chain, not just the last one:
ul:first liselects the list items of the firstul, and each comma-separated group is filtered independently before the union is returned in document order - Support positional pseudo-classes as the argument to
:not(),:has()and:matches(), so the commonli:not(:first)idiom now excludes the first match rather than every match QueryPath\CSS\DOMTraverser\PseudoClass::elementMatches()now throwsQueryPath\CSS\NotImplementedExceptionfor the positional pseudo-classes. They cannot be answered for a node in isolation; use aTraverser, which applies them after traversal- Breaking behaviour change:
DOMQuery::is()now behaves like jQuery's.is(). It tests the elements held in the current match set and returnstruewhen at least one of them matches the selector. Previously it ran a descendant search, sohtml5qp('<p><span>foo</span></p>', 'p')->is('span')returnedtrue. Usehas()if you need the old "does the set contain something matching this selector" behaviour (#51) DOMQuery::is()no longer raises a fatal error when the match set contains non-element nodes (text nodes, comments, processing instructions). Those nodes cannot match a CSS selector, so they are skipped (#51)- Fix
parents($selector)so the selector filters the ancestors themselves, instead of matching any ancestor that merely contains an element matching the selector.qp($xml, 'Demographics > Age > Name')->parents('Demographics')now returns only<Demographics>, matching jQuery (#62) - Apply the same fix to the other selector-filtered traversal methods in
QueryPath\Helpers\QueryFilters:parent(),parents(),parentsUntil(),closest(),next(),nextAll(),nextUntil(),prev(),prevAll(),prevUntil(),siblings(), andnot()(#62) - Behaviour change:
parents()andparentsUntil()now return their results in reverse document order with duplicates removed, as jQuery does. Previously a set built from more than one starting element was grouped by starting element (#62) - Breaking behaviour change:
DOMQuery::filter()now narrows the match set to the members that match the selector, as jQuery does, instead of keeping any element that merely contains a match.qp($file, 'inner')->filter('li')returned both<inner>elements and now returns none. Usehas()for the old behaviour - Breaking behaviour change:
find()now searches descendants only, as jQuery's.find()does. A node already in the match set is no longer a candidate for its own selector, soqp($xml, '#a1')->find('a'),find('#a1')andfind('.c')no longer return#a1itself. The document element remains matchable, becauseqp()seeds the match set with it rather than with the document. Usefilter()to match the nodes in hand.detach($selector), which usesfind(), follows suit - Fix
children($selector), which resolved:scopeagainst each child rather than the document element, sochildren(':scope')matched every child filter()andchildren($selector)now evaluate the selector against the whole candidate set in a single pass, rather than one node at a time. A per-node pass cannot evaluate a selector describing a position within the set, since each node is the only member of its own one-element setnextAll()andprevAll()stop walking at the first sibling already collected from an earlier starting element, rather than re-walking to the end of the list. On a 4,000-item listnextAll()fell from about 430 ms to under 10 ms- Reorganise, modernise, and repair the
examples/directory. Each example now lives in its own subdirectory with anindex.php, and the full set is indexed inexamples/quickstart-guide.md - Convert the remaining legacy examples:
simple_example.php,techniques.php,svg.php,rss.php,odt.php,parse_php.php, andsparql.php - Fix examples that no longer ran: send a
User-Agentwhere remote hosts now require one, resolve paths relative to the example rather than the working directory, and stop relying on the removedqp.phpautoloader and the PHP 8 incompatibleeachLambda() - Rewrite
examples/quickstart-guide.md, which documented an autoloader and a set of Composer caveats that no longer apply - Remove unused example fixtures (
The_Beatles.rdf,testGrid.html,out.svg) and generated output - Add
ext-domandext-simplexmlto thecomposer.jsonrequirements, andext-zipto the dev requirements - Add
QueryPathTests\ExamplesTest, which runs every offline example on each supported PHP version and fails if one stops working. The examples that call third-party services are run by the newExamplesworkflow, weekly and whenever an example changes - Rewrite the cURL example against the PubMed E-utilities API. MusicBrainz throttles by IP address, which made the example unusable from any shared address
- Add
composer run test:examples(andtest:examples:network) to run the examples locally - Fix processing instructions gaining an extra
?each time an HTML-parsed document was serialized, so<?php echo $title; ?>came back out ofhtml(),innerHTML(),innerXML(),innerXHTML(),xml(),html5(),innerHTML5(), andwriteXML()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::$dataconsequently no longer has a stray?on the end for documents read withhtmlqp()orqp()on an.htmlfile. Note that this normalisation applies only to documents QueryPath parses itself, and that taking the underlyingDOMDocumentout of QueryPath and calling libxml's ownsaveHTML()on it will emit<?php ... >without the terminator, since libxml's HTML serializer never adds one - Add
QueryPath\Document, aDOMDocumentsubclass 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 secondDOMQueryover one document -- iteration,add(),remove(),replaceAll(),branch(),QueryPath::with(), and the bundled extensions -- serializes it correctly. ADOMDocumentsupplied by the caller is a plainDOMDocument, makes no such promise, and is still serialized exactly as it was handed over - Fix
:nth-child(),:nth-last-child(),:nth-of-type()and:nth-last-of-type()taking quadratic time on wide sibling lists (issue #84). Each sibling list is now indexed once per query, shared with nested:not()and:has()queries and across the per-element tests thatclosest(),nextUntil()and the other walk-and-stop methods run, instead of being walked for every element.tr:nth-child(2n)on a 4000-row table drops from about 320 ms to 4 ms find('*')now matches the nodes in the match set as well as their descendants, so that a selector can be tested against an element already in hand- Behaviour change.
firstChild()now returns the first child element of every matched element, aslastChild()already did, instead of only that of the first matched element (issue #78). Matches with no child elements contribute nothing. UsefirstChild()->first()for the previous result - Fix
html()andwriteHTML()dropping the closing?of a processing instruction in anhtml5qp()document, so<?php echo $title; ?>came out as<?php echo $title; >(issue #86).html5qp()documents are nowQueryPath\Documentinstances, like those fromqp()andhtmlqp() - Behaviour change. Fix
:has()never matching infind(),filter()and the other methods that useQueryPath\CSS\DOMTraverser, so thatul:has(li)selected nothing and:not(:has(li))selected everything (issue #121).:has()now matches an element with a descendant matching its argument, and supports relative selectors:ul:has(> li)(a child),h2:has(+ p)(the next sibling) andh2:has(~ p)(a later sibling). A positional pseudo-class in the argument indexes each element's own matches, soul:has(li:eq(2))selects every list with at least three items - Behaviour change.
remove($selector)andreplaceAll($selector)now select withQueryPath\CSS\DOMTraverser, the engine behindfind(), instead of the legacyQueryPath\CSS\QueryPathEventHandler(issues #126, #123, #124).remove($selector)searches the descendants of the match set, asfind()anddetach($selector)do, soqp($xml, 'p')->remove('p')no longer removes the<p>elements themselves.replaceAll($selector, $document)still searches the whole of$document, document element included. Both now accept:matches(), which threw "Unknown Pseudo-Class: matches", and apply a positional:not()argument to the whole set, soremove('li:not(:first)')keeps only the first<li>instead of removing nothing.QueryPathEventHandleris no longer used and is deprecated - Add
QueryPath\CSS\DOMTraverser::filterNodes($nodes, $selector), which returns the given nodes that themselves match a selector, in the order given (issue #116).filter(),is(), the selector-filtered traversal methods, and:not()/:matches()all use it.DOMTraverserno longer modifies a parsed selector, so queries sharing aPseudoClasshandler parse each selector once:nextUntil()over 2000 siblings drops from about 16 ms to 6 ms, andtr:not(:nth-child(2n))on a 2000-row table from 15 ms to 6 ms - Consolidate the sibling and ancestor walks behind
next(),prev(),nextAll(),prevAll(),nextUntil(),prevUntil(),parent(),parents(),parentsUntil()andclosest()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, andnextUntil('')andprevUntil('')treat an empty selector as no selector, as the other methods already did, instead of throwingQueryPath\CSS\ParseException.closest('')returns an empty set, as in jQuery.nextUntil(),prevUntil()andparentsUntil()also stop at an element already collected from an earlier starting element, asnextAll()andprevAll()do, sonextUntil()on each of 2000 siblings falls from about 100 ms to 1 ms - Behaviour change.
prevAll(),prevUntil(),nextAll(),nextUntil(),siblings(),parent()andclosest()now return the results for a multi-element set in jQuery's order, instead of grouped by starting element (issue #117).prevAll()andprevUntil()return reverse document order, asparents()andparentsUntil()already did, and the others return document order.qp($xml, 'c')->add('d')->prevAll()now returnsc, b, arather thanb, a, c.next()andprev()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 ofnextUntil(),prevUntil()andparentsUntil()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, andnextUntil(':eq(3)')stops at the fourth element in the document.closest()no longer matches a positional selector at all, as in jQuery, whereclosest(':first')used to return the element itself.next(),prev()andparent()still test each candidate on its own, which agrees with jQuery for a single starting element
- Add support for PHP 8.6
- With libxml2 2.14+,
htmlqp()parses<?php ... ?>as a comment (<!--?php ... ?-->) and no longer throwsQueryPath\ParseExceptionon malformed HTML. Usehtml5qp()to keep processing instructions - On PHP 8.0 and later,
filterLambda()andeachLambda()now throw aQueryPath\Exceptionthat names their replacements,filterCallback()andeach(), instead of anErrorabout the undefinedcreate_function()(issue #81). They still work on PHP 7.1–7.4 and will be removed in 5.0 - Fix
QPXML::pi($prefix)always returningNULLwith an "Undefined property" warning instead of the matching processing instruction's text (issue #79) - 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 - Fix fatal error in
attr(),addClass(),removeClass(),css(),hasClass(),hasAttr()andremoveAttr()when the match set contains non-element nodes (text, comment, CDATA or processing instruction), ascontents()returns (issue #98). As in jQuery, those nodes are skipped, and theattr()andcss()getters returnNULLwhen the first match is not an element - Fix
find('a > b')throwing aTypeErrorwhen the left-hand side matched the root element, whose parent is the document rather than an element - Fix
QPXML::createElement()emitting an "Only variables should be passed by reference" notice for a namespace-prefixed element name without an explicit namespace URI (issue #82) - Fix the
:textpseudo-class so it matches jQuery: it selectsinputelements whosetypeattribute is absent or istext(case-insensitively). It never indicated, and still does not indicate, whether a node is a text node - Fix
QPXML::createElement()throwing "Undefined namespace" for a prefixed element name whose prefix is declared on the matched element or an ancestor below the document element. The prefix is now resolved against the namespaces in scope on the first matched element (issue #94) - Fix
detach($selector)ignoring its selector and detaching the current match set instead (issue #76). It now detaches only the matches of$selector, asremove($selector)does, and leaves the current match set in the document writeHTML5()now returns theDOMQuerylike the otherwrite*()methods, and throwsQueryPath\IOExceptioninstead of aTypeErrorwhen the file cannot be written (issue #80)- Fix
QueryPath\ParseExceptionandQueryPath\IOExceptionlosing the PHP warning they were raised from (issue #91). The message was the error number, as in2 (/path/to/src/DOMQuery.php: 1343), and the code was always0. The message now carries the warning text and the code is the PHP error level (for exampleE_WARNING), so code that matches on the old message text will need updating - Fix
tag()emitting an "Undefined property" warning and returningNULLwhen the first match is not an element, such as a text, comment or processing instruction node returned bycontents(). It now returns an empty string, as it does for an empty match set (issue #93) - Fix QueryPath's error handler staying installed for the rest of the request when an exception other than a
QueryPath\Exception(aTypeError, or one thrown by a stream wrapper, for example) escaped from parsing a document, inserting markup, orwriteXML()/writeHTML()(issue #95) - Fix
writeHTML()throwingQueryPath\ParseExceptioninstead ofQueryPath\IOExceptionwhen the file cannot be written, aswriteXML()andwriteHTML5()do (issue #92).IOExceptionextendsParseException, so existingcatch (ParseException $e)blocks still catch it - Fix
css()merging the inline styles of every matched element and writing the combined result back to all of them. The new declarations are now merged into each element's ownstyleattribute (issue #77) - Fix
not()keeping only the given nodes, instead of removing them, when passed anSplObjectStoragesuch asget(null, true)returns (issue #75) - Fix selectors with two or more levels of nested parentheses, such as
li:not(:not(:nth-child(1)))orul:has(li:not(:nth-child(1))), throwing "Recursion error detected" (issue #120). The scanner skipped the character after every nested argument, so the outer argument never saw its closing paren. This also stopsli:not(:nth-child(1)) bfrom foldingbinto the:not()argument, and lets quoted arguments contain parentheses, as in:contains("a)b") - Fix
QueryPath::VERSIONandQueryPath::VERSION_MAJORstill reporting3.2.2and3. They now report4.3.0and4, and both are deprecated in favour of\Composer\InstalledVersions::getPrettyVersion('gravitypdf/querypath'), which cannot drift from the installed version (issue #83)
- Add PHP 8.5 support
- Update composer.json to mark library as PHP 8.4 compatible
- Use
\QueryPath\CSS\DOMTraverser\Util::parseAnB()in\QueryPath\CSS\QueryPathEventHandlerclass to parse the:nth-child(an+b)syntax - Deprecate protected method
\QueryPath\CSS\QueryPathEventHandler::parseAnB()in favor of public static method\QueryPath\CSS\DOMTraverser\Util::parseAnB()
- Only define global functions qp(), htmlqp(), and html5qp() if they haven't been defined already.
- Fix for :nth-child(n+B) to select B-th and all following elements
- Fix for :nth-child(-n+B) to select first B elements
- Update PHPUnit Test Suite to use @dataProvider in testPseudoClassNthChild() to reduce code repetition
- Fix error when getting parents() for HTML elements
- 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().
- Return DOMQuery object if QueryMutators::wrapAll() has no matches (instead of null). This aligns the method with the Docblock return type.
- Add PHP 8.3 Support
- Fix type mismatch for classes that implement the "Query" interface
- Add PHP 8.2 Support
- Fix QueryPath\QueryPathIterator::current() deprecation notice in PHP8.1
- Fixes a number of type-related errors on PHP 8.1
- Update PHPUnit Test Suite to pass from PHP7.1 to 8.1
- Add GitHub Actions to run PHPUnit for all PRs
- Removes composer.lock file to ensure the correct PHPUnit is installed in GitHub Actions. Since this is a library this is generally the accepted practice.
- Removed the Faker dependency (it didn't appear to be used in the codebase and has been sunset).
- Apply PSR-2 Linter to Codebase, using tabs instead of spaces
- Rename QueryPath fork and prepare for publishing on Packagist
- Added return types to getIterator() and count() for compatibility with PHP 8.1
- Add DOMQuery traits - dividing mutators, filters, checks
- Add format extension + test coverage + readme
- Fix juggling equations, return types in Utils
Transform library for PHP>=7.1 support + essential bug-fixes
- Add strict types passed for parameters
- Add return types scalar/boxed
- Refactor code to OOP (DRY/KISS/SOLID/YAGNI) style - minor to be continued...
- Remove redundant code
- Remove redundant ops
- Fix bugs with equations
- Fix juggling equations
- Fix priority ops in flow structs
- Remove unused vars from stack
- Addition of namespace fetching method ns().
- Various fixes
- Basic support for HTML5 via Masterminds\HTML5
- Fixed #164
- Bug #141: Fixed :gt and :lt pseudoclasses (thanks to lsv)
- Bug #124: Fixed inheritance issue with late static binding (via noisan)
- Bug #126: text(string) was not updating all matched elements (via noisan)
- Bug #127: wrapInner() was mangling the HTML if matched element set was greater than 1 (via noisan)
- Bug #128: Improved jQuery compatibility with wrap*() *pend(), before(), after(), etc. (via noisan)
- Bug #112: children() was not correctly restricting filters.
- Bug #108: QueryPath\Query interface was too restrictive. (via mkalkbrenner)
- Feature #106: :contains() is now case-insensitive. (via katzwebservices)
- Issue #100: qp.php divided into qp.php and qp_functions.php. Composer now includes qp(). (via hakre)
- Issue #102: Added QueryPath.VERSION_MAJOR, changed QueryPath.VERSION
- Issue #97: Rewrite children() and filter() to be a little more efficient.
-
REALLY IMPORTANT CHANGE: To match jQuery, and to correctly implement a bottom-up parser, the following will no longer work: $qp($html, 'li')->find(':root ul'); You can no longer use find() to ever, ever move UP the document true (like back to the root from somewhere in the tree). You MUST use top() to do this now. This is how jQuery works, and making this minor change makes things much faster.
-
REALLY IMPORTANT CHANGE: Many "destructive" operations now return a new QueryPath object. This mirrors jQuery's behavior.
THAT MEANS: find() now works like jQuery find(), branch() is deprecated. findInPlace() does what find() used to do.
-
removeAll() now works as it does in recent jQuery. Thanks to GDMac (issues #77 #78) for the fix.
-
Refactored to use namespaces.
-
Refactored to be SPR-0 compliant.
-
Now uses Composer.
-
The traversal mechanism is now bottom-up, which means Querypath is faster.
-
Issue #83: Fixed broken forloop in techniques.php. Thanks to BillOrtell for the fix.
-
ID-based searches no longer guarantee that only one element will be returned. This accomodates XML documents that may use 'id' in a way different than HTML.
-
The base CSS Traverser is now optimized for selectors that use ID or class, but no element.
-
Pseudo-element behavior has been rewritten to better conform to the standard. Using a pseudo-element no longer changes the match. Rather, it checks to see if the condition is met by the present element, and returns TRUE if it does. This means we do not need special case logic to deal with text fragments.
-
:x-root, :x-reset, and :scope are now implemented using the same algorithm (they are, in other words, aliases). Since :scope is part of CSS 4, you should use that.
-
Support for the following CSS 4 Selectors featues has been added:
- :matches() pseudoclass, which behaves the way :has() behaves.
- :any-link
- :local-link (with some restrictions, as it does not know what the real URL of the document is).
- :scope (see above)
-
Traversing UP the DOM tree with find() is no longer allowed. Use top().
-
:first is an alias of :nth(1) instead of :first-of-type. This follows jQuery now.
-
eachLambda() is now deprecated. It WILL be removed in the future.
-
Extensions: QPList, QPTPL, and QPDB have all been moved to the new project QueryPath-Ext.
- QueryPath Templates have gotten an overhaul from TomorrowToday (Issue #59). Templates now support attributes.
- Fixed the case where remove() caused an error when no items were found to remove (issue #63). Thanks marktheunissen for the bug report and fix.
- New XML extensions to deal with adding namespaced elements (#64). Thanks to theshadow for contributing an entire extension, and to farinspace for detailed experiments with QP and XML namespaces.
- The adjacent CSS selector has been modified to ignore text elements. This seems to be inline with the spec, but I am not 100% sure. Thanks to fiveminuteargument for the patch.
- The xhtml() and writeXHTML() methods now correctly escape JS/CSS and also correctly fold some tags into unaries will keeping other empty tags. See issues #10, #47. Thanks to Alex Lawrence for his input.
- The method document() has been added. Thanks to Alex Lawrence for suggesting this addition.
- The fetch_rss.php example created broken HREFs in some cases. Thanks to yaph for the patch.
- The xpath() method now supports setting default namespaces. Thanks to Xavier Prud'homme for a patch.
- The remove() method was fixed (issue #55) to now correctly return a QueryPath with just the removed nodes, while not altering the base QueryPath object. Thanks to MarcusX for finding and reporting the problem.
- Added childrenText() convenience method. (Safe -- no changes to existing functions.) Thanks to Xavatar for suggestion and proofing initial code.
- Fixed bad character stripping in htmlqp() (Issue #58, #52) so that meaningful whitespace is no longer stripped prior to parsing. Thanks to NortherRaven for detailed report and help testing and debugging.
- Fixed broken :nth-of-type pseudo-class (Issue #57). Thanks to NorthernRaven for the detailed report and help debugging.
- Fixed broken an+b rule handling in the special case '-n+b'. Thanks to NorthernRaven for reporting and helping out.
- Xinclude support has been added via the xinclude() method on QueryPath. Thanks to Crell for the suggestion and to sdboyer for help (Issue #50).
- QueryPath now implements Countable, which means you can do
count(qp($xml, 'div')). The size() function has been marked deprecated. - is() now takes both DOMNodes and Traversables (including QueryPath) as an argument. See issue #53.
- The dirty_html.php example (contributed by Emily Brand, thanks!) is now fixed. Thanks to MartyIX for tracking down the issue (#59).
- NEW METHOD: document() returns the DOMDocument
- BUG FIX: Issue #10 has been re-fixed to correctly collapse certain empty tags.
- BUG FIX: Issue #10 has been re-fixed to correctly escape JavaScript for browsers.
- BUG FIX: Issue #47 has been fixed to only remove XML declaration, but leave DOCTYPE.
- NEW ARGUMENT: xpath() now supports $options, which includes the ability to set a namespace.
Big Changes:
- There is now an
htmlqp()function that parses crufty HTML in a far more reliable way thanqp(). Use this instead of any variant of the older@qp()setup. - The API has been brought into alignment with jQuery 1.4. See API-2.1.0 for details.
- This release was driven substantially by eabrand's GSOC 2010 contributions. Thanks, Emily!
- There are now Phar and PEAR packages available. Got to http://pear.querypath.org for PEAR packages.
- The minimal QP distribution is no longer minified, as it reportedly causes XDebug to crash.
- Data URs are now supported. QueryPath can now embed images directly into HTML and XML this way.
- Documentation is now in Doxygen instead of PhpDocumentor. Thanks to Matt Farina and Kevin O'Brien for their input.
- The
htmlqp()method has been added for parsing icky HTML. Use this for web scraping.
- The qp() function now supports the following new options:
- convert_to_encoding
- convert_from_encoding
- strip_low_ascii
- use_parser
- attach()/detach()
- has()
- emptyElement()
- even()/odd()
- first()/last()
- firstChild()/lastChild()
- nextUntil()/prevUntil()
- parentsUntil()
- encodeDataURL()
- dataURL()
- filterPreg()
- textBefore()/textAfter()
- css() has been changed to allow subsequent calls to modify the style attribute (issue #28)
- attr() has been changed. If it is called with no arguments, it now returns all attributes.
- :contains-exactly() performs as :contains() used to perform.
- The star operator (*) is now non-greedy, per spec. Before, the star would match any descendants. Now it will only match children.
- :contains() now does substring matching instead of exact matching. This conforms to jQuery's behavior.
- Quotes are now checked carefully before being stripped from pseudo-class values.
- Issue #40 identified a potential infinite looping problem on poorly formed selectors. This was fixed.