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

Fixes queryAssignedNodes when querying for nodes on browsers that do … #1092

Merged
merged 6 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
<!-- ### Removed -->
<!-- ### Fixed -->

## Unreleased

### Fixed
* Fixes an issue with `queryAssignedNodes` when applying a selector on a slot that included text nodes on older browsers not supporting Element.matches [#1088](https://github.com/Polymer/lit-element/issues/1088).

## [2.4.0] - 2020-08-19

### Changed
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/lib/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,10 @@ export function queryAssignedNodes(
if (nodes && selector) {
nodes = nodes.filter(
(node) => node.nodeType === Node.ELEMENT_NODE &&
(node as Element).matches ?
// tslint:disable-next-line:no-any testing existence on older browsers
((node as any).matches ?
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the DOM types include .matches you shouldn't need the as any cast. Did the old code not type-check?

(node as Element).matches(selector) :
legacyMatches.call(node as Element, selector));
legacyMatches.call(node as Element, selector)));
}
return nodes;
},
Expand Down
33 changes: 33 additions & 0 deletions src/test/lib/decorators_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,39 @@ suite('decorators', () => {
flush();
assert.deepEqual(c.assignedNodesEl.footerAssignedItems, []);
});

test('returns assignedNodes for slot that contains text nodes filtered by selector when Element.matches does not exist', async () => {
const descriptor = Object.getOwnPropertyDescriptor(
Element.prototype,
'matches'
);
Object.defineProperty(Element.prototype, 'matches', {
value: undefined,
configurable: true,
});
const c = new C();
container.appendChild(c);
await c.updateComplete;
await c.assignedNodesEl.updateComplete;
assert.deepEqual(c.assignedNodesEl.footerAssignedItems, []);
const child1 = document.createElement('div');
const child2 = document.createElement('div');
const text1 = document.createTextNode('');
const text2 = document.createTextNode('');
child2.classList.add('item');
c.appendChild(child1);
c.appendChild(text1);
c.appendChild(child2);
c.appendChild(text2);
flush();
assert.deepEqual(c.assignedNodesEl.footerAssignedItems, [child2]);
c.removeChild(child2);
flush();
assert.deepEqual(c.assignedNodesEl.footerAssignedItems, []);
if (descriptor !== undefined) {
Object.defineProperty(Element.prototype, 'matches', descriptor);
}
});
});

suite('@queryAsync', () => {
Expand Down