Internal: More reliable internal testing helper implementation #2057
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes an issue where the old implementation does not work reliably on macOS when running the full suite.
Explanation of Changes:
Using XPath: XPath is a powerful language for querying XML documents (and HTML, which is XML-like). It's much more efficient and reliable for complex queries than manual traversal.
DOMXPath
Object: We create aDOMXPath
object using the existing$this->document
object, which is already a parsed DOM. This avoids re-parsing the HTML.Robust Class Matching: The XPath expression
//*[contains(concat(' ', normalize-space(@class), ' '), ' $class ')]
is a standard and robust way to find elements with a specific class, even if they have multiple classes. It handles leading/trailing spaces correctly.Iterating
DOMNodeList
: Thequery()
method returns aDOMNodeList
. We iterate over this list and add the matching elements to our collection.Type Safety: The added
if ($node instanceof DOMElement)
check ensures type safety and prevents unexpected behavior if non-element nodes are encountered in theDOMNodeList
.parseNodeRecursive
: We reuse your existingparseNodeRecursive
method to convert theDOMElement
objects intoTestableHtmlElement
objects for consistency.Why this is better: