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

Add a querySelectorAll helper function to use :scope when possible #3778

Closed
wants to merge 2 commits into from

Conversation

mkhatib
Copy link
Contributor

@mkhatib mkhatib commented Jun 24, 2016

A prequel to #3734

ITI: #3343

* A wrapper around native querySelectorAll to use :scope when possible.
* @param {!Element} parent
* @param {string} selector
* @returns {!Array.<!Element>}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: no "period"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@dvoytenko
Copy link
Contributor

@mkhatib please also touch base with @cramforce on review, since he's been pushing the wider :scope use

* @param {string} selector
* @returns {!Array.<!Element>}
*/
export function querySelectorAll(parent, selector) {
Copy link
Member

Choose a reason for hiding this comment

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

Definitely would call this scopedQuerySelectorAll

What is your concrete use case? Do you need a polyfill?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No not a polyfill, @dvoytenko suggested instead of making the call to the native querySelectorAll is to try to move it to a helper method that would use :scope when available.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed.

export function querySelectorAll(parent, selector) {
if (scopeSelectorSupported) {
const scopedSelectors = selector.split(',').map(sel => `:scope ${sel}`);
return toArray(parent.querySelectorAll(scopedSelectors.join(',')));
Copy link
Member

Choose a reason for hiding this comment

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

Why toArray?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess I was trying to stick to the same return signature to other methods in dom.js, but I think for this one, we can go with the NodeList return instead since that's always what is to be returned. Will change.

@cramforce
Copy link
Member

I'm still struggling to understand why this change is needed. What is the use case?

@mkhatib
Copy link
Contributor Author

mkhatib commented Jun 25, 2016

@cramforce in a preivous PR I was doing the following:

const inputs = toArray(form.querySelectorAll('input,select,textarea'));

And @dvoytenko suggested that this should be moved to a dom.js helper method to use :scope when available for performance reasons I believe. Is that not necessary?

@cramforce
Copy link
Member

I don't think it is actually faster

Does scope make sense in querySelectorAll if not combined with something like > as in :scope > .foo?

We use this elsewhere in dom.js to match immediate children.

@mkhatib
Copy link
Contributor Author

mkhatib commented Jun 27, 2016

Ah ok, I am not sure tbh. Didn't realize the :scope win would be only when for matching immediate children. In this case I don't think we need this change.

@mkhatib mkhatib closed this Jun 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants