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
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,21 @@ export function childElementByTag(parent, tagName) {
}


/**
* 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.

*/
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.

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.

}
return toArray(parent.querySelectorAll(selector));
}


/**
* Returns element data-param- attributes as url parameters key-value pairs.
* e.g. data-param-some-attr=value -> {someAttr: value}.
Expand Down
25 changes: 25 additions & 0 deletions test/functional/test-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,31 @@ describe('DOM', () => {
expect(dom.lastChildElementByAttr(parent, 'on-child')).to.be.null;
});

function testQuerySelectorAll() {
const parent = document.createElement('parent');
const element1 = document.createElement('element1');
parent.appendChild(element1);
const element2 = document.createElement('element2');
parent.appendChild(element2);
const element3 = document.createElement('element3');
element1.appendChild(element3);

const element1Nested = document.createElement('element1');
element2.appendChild(element1Nested);

expect(dom.querySelectorAll(parent, 'element1, element3').length)
.to.equal(3);
expect(dom.querySelectorAll(parent, 'element2').length).to.equal(1);
expect(dom.querySelectorAll(parent, 'element4').length).to.equal(0);
}

it('querySelectorAll should return all matches', testQuerySelectorAll);

it('querySelectorAll should return all matches (polyfill)', () => {
dom.setScopeSelectorSupportedForTesting(false);
testQuerySelectorAll();
});

describe('contains', () => {
let connectedElement;
let connectedChild;
Expand Down