Skip to content

Commit

Permalink
Merge pull request #1092 from Polymer/fix-queryAssignedNodes
Browse files Browse the repository at this point in the history
Fixes queryAssignedNodes when querying for nodes on browsers that do …
  • Loading branch information
Steve Orvell authored Oct 20, 2020
2 parents 79fac08 + 6573419 commit db153a5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
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.

7 changes: 4 additions & 3 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 ?
(node as Element).matches(selector) :
legacyMatches.call(node as Element, selector));
// tslint:disable-next-line:no-any testing existence on older browsers
((node as any).matches ?
(node as Element).matches(selector) :
legacyMatches.call(node as Element, selector)));
}
return nodes;
},
Expand Down
38 changes: 38 additions & 0 deletions src/test/lib/decorators_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,44 @@ 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');
let definedMatches = true;
try {
Object.defineProperty(Element.prototype, 'matches', {
value: undefined,
configurable: true,
});
} catch (e) {
definedMatches = false;
}
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 (definedMatches && descriptor !== undefined) {
Object.defineProperty(Element.prototype, 'matches', descriptor);
}
});
});

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

0 comments on commit db153a5

Please sign in to comment.