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

Polyfill Element#matches. #400

Merged
merged 3 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 34 additions & 0 deletions packages/tests/webcomponentsjs_/matches.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!doctype html>
<!--
@license
Copyright (c) 2020 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html>
<head>
<title>Element matches</title>
<script src="../node_modules/@webcomponents/webcomponentsjs/bundles/webcomponents-pf_dom.js"></script>
<script src="./wct-config.js"></script>
<script src="../node_modules/wct-browser-legacy/browser.js"></script>
</head>
<body>
<script>
suite('Element matches', function() {
test('Returns an array of attribute names', () => {
const element = document.createElement('div');
element.className = 'a b';

assert.isTrue(element.matches('div'));
assert.isTrue(element.matches('.a'));
assert.isTrue(element.matches('.b'));
assert.isTrue(element.matches('div.a.b'));
assert.isFalse(element.matches('.c'));
});
});
</script>
</body>
</html>
1 change: 1 addition & 0 deletions packages/tests/webcomponentsjs_/runner.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
'baseuri.html',
'object-assign.html',
'get-attribute-names.html',
'matches.html',
'parent-node/index.html',
'child-node/index.html',
'svg-element-class-list.html',
Expand Down
2 changes: 2 additions & 0 deletions packages/webcomponentsjs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

- Polyfill `Element#matches`.
([#400](https://github.com/webcomponents/polyfills/pull/400))
- Remove function declarations from platform polyfills to sastisfy internal lint
after import transforms.
([#396](https://github.com/webcomponents/polyfills/pull/396))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
import '../platform/custom-event.js';
import '../platform/baseuri.js';
import '../platform/get-attribute-names.js';
import '../platform/matches.js';
import '../platform/parent-node/index.js';
import '../platform/child-node/index.js';
import '../platform/svg-element-class-list.js';
20 changes: 20 additions & 0 deletions packages/webcomponentsjs/ts_src/platform/matches.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
@license
Copyright (c) 2020 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at
http://polymer.github.io/LICENSE.txt The complete set of authors may be found at
http://polymer.github.io/AUTHORS.txt The complete set of contributors may be
found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by Google as
part of the polymer project is also subject to an additional IP rights grant
found at http://polymer.github.io/PATENTS.txt
*/

export {};

const Element_prototype = Element.prototype;

if (!Element_prototype.hasOwnProperty('matches')) {
Element_prototype.matches =
((Element_prototype as any).webkitMatchesSelector as Element['matches']) ??
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe add mozMatches (but not sure it's relevant at all)?

Copy link
Collaborator Author

@bicknellr bicknellr Oct 21, 2020

Choose a reason for hiding this comment

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

Firefox 34 has the unprefixed version. I think the webkit... one might even be unnecessary now; the MDN page claims that these two should cover IE9+ though.

((Element_prototype as any).msMatchesSelector as Element['matches']);
}