Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLEANUP] Add CssInliner::querySelectorAll method #1316

Merged
merged 4 commits into from
Sep 18, 2024

Conversation

JakeQZ
Copy link
Contributor

@JakeQZ JakeQZ commented Sep 17, 2024

This private method provides functionality that is used in three separate places (albeit a one-liner), and includes taking care of exception handling according to the debug mode setting.

The new method and parameter names match the equivalent in the Web API.

There will be a tiny performance impact due to additional method calls. But the benefits include code re-use and common error handling.

Also add ensureNodeIsElement method for use with the result from querySelectorAll to ensure type safety. querySelectorAll should always return a DOMNodeList of DOMElements. But this cannot be guaranteed if there is a bug in CssSelector whereby it returns an XPath expression which may match non-element nodes. And it would (probably) be sub-optimal for querySelectorAll to have an extra loop checking this, when it can be done in the caller's loop over the returned data.

@JakeQZ JakeQZ added the cleanup label Sep 17, 2024
@JakeQZ JakeQZ added this to the 8.0.0 milestone Sep 17, 2024
@JakeQZ JakeQZ self-assigned this Sep 17, 2024
@coveralls
Copy link

coveralls commented Sep 17, 2024

Coverage Status

coverage: 96.943% (-0.4%) from 97.301%
when pulling 3032fdc on cleanup/query-selector-all
into 597eaf2 on main.

@JakeQZ
Copy link
Contributor Author

JakeQZ commented Sep 17, 2024

Coverage Status

coverage: 96.688% (-0.6%) from 97.311% when pulling 5c03b33 on cleanup/query-selector-all into fe0bb4d on main.

If the expression is malformed or the contextNode is invalid, DOMXPath::query() returns false.

That will never happen unless there is a bug in CssSelectorConverter. We are not using the $contextNode parameter. So the error handling code path cannot be exercised in tests (for coverage or otherwise).

@JakeQZ JakeQZ marked this pull request as draft September 17, 2024 04:51
@JakeQZ
Copy link
Contributor Author

JakeQZ commented Sep 17, 2024

Can improve this. One instance that could call the new method is missed. And excpetion handling logic (and/or its definition) can be made more precise.

@oliverklee oliverklee changed the title [CLEANUP] Add CssInliner::querySelectorAll method [CLEANUP] Add CssInliner::querySelectorAll method Sep 17, 2024
@JakeQZ
Copy link
Contributor Author

JakeQZ commented Sep 17, 2024

Will be ready for review when I repush. I'm marking as ready for review now, to see if we get a Coveralls comment on repush when it's not in draft status.

@JakeQZ JakeQZ marked this pull request as ready for review September 17, 2024 23:54
@JakeQZ JakeQZ force-pushed the cleanup/query-selector-all branch 3 times, most recently from 2b5b3f0 to dbf26c9 Compare September 18, 2024 00:07
This `private` method provides functionality that is used in three separate
places (albeit a one-liner), and includes taking care of exception handling
according to the debug mode setting.

The new method and parameter names match the equivalent in the
[Web API](
  https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll
).

There will be a tiny performance impact due to additional method calls.  But the
benefits include code re-use and common error handling.

Also add `ensureNodeIsElement` method for use with the result from
`querySelectorAll` to ensure type safety.  `querySelectorAll` should always
return a `DOMNodeList` of `DOMElement`s.  But this cannot be guaranteed if there
is a bug in CssSelector whereby it returns an XPath expression which may match
non-element nodes.  And it would (probably) be sub-optimal for
`querySelectorAll` to have an extra loop checking this, when it can be done in
the caller's loop over the returned data.
@JakeQZ
Copy link
Contributor Author

JakeQZ commented Sep 18, 2024

Will be ready for review when I repush. I'm marking as ready for review now, to see if we get a Coveralls comment on repush when it's not in draft status.

Oh. I've just noticed that Coveralls updates its comment, instead of providing a new one.

Surprisingly, coverage has now increased, despite some of the newly-added exception paths not being exercised. Though I suppose the old exception paths weren't being exercised either, and there were more of them, as this PR also now commons-up some of the exception handling logic.

Anyway, it's ready for review now..

@oliverklee
Copy link
Contributor

Oh. I've just noticed that Coveralls updates its comment, instead of providing a new one.

Yeah. I had the same facial expression some time ago when I noticed the same thing … ;-)

Copy link
Contributor

@oliverklee oliverklee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like this refactoring! ❤️

src/CssInliner.php Show resolved Hide resolved
src/CssInliner.php Outdated Show resolved Hide resolved
src/CssInliner.php Outdated Show resolved Hide resolved
src/CssInliner.php Show resolved Hide resolved
@oliverklee oliverklee merged commit 919f5c4 into main Sep 18, 2024
24 checks passed
@oliverklee oliverklee deleted the cleanup/query-selector-all branch September 18, 2024 17:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants