Skip to content

Polyfill Element#getAttributeNames. #393

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

Merged
merged 6 commits into from
Oct 16, 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
50 changes: 50 additions & 0 deletions packages/tests/webcomponentsjs_/get-attribute-names.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!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 getAttributeNames</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 getAttributeNames', function() {
test('Returns an array of attribute names', () => {
const element = document.createElement('div');
assert(element.getAttributeNames().length === 0);

let attributeNames;

attributeNames = element.getAttributeNames();
assert.isArray(attributeNames);
assert.deepEqual(attributeNames, []);

element.setAttribute('name-a', 'value-a');
attributeNames = element.getAttributeNames();
assert.deepEqual(attributeNames, ['name-a']);

element.setAttribute('name-b', 'value-b');
attributeNames = element.getAttributeNames();
assert.deepEqual(attributeNames, ['name-a', 'name-b']);

element.removeAttribute('name-a');
attributeNames = element.getAttributeNames();
assert.deepEqual(attributeNames, ['name-b']);

element.removeAttribute('name-b');
attributeNames = element.getAttributeNames();
assert.deepEqual(attributeNames, []);
});
});
</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 @@ -34,6 +34,7 @@
'loader-after-load.html',
'baseuri.html',
'object-assign.html',
'get-attribute-names.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#getAttributeNames`.
([#393](https://github.com/webcomponents/polyfills/pull/393))
- Add polyfills for ChildNode APIs.
([#390](https://github.com/webcomponents/polyfills/pull/390))
- Add polyfills for select ParentNode APIs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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/parent-node/index.js';
import '../platform/child-node/index.js';
import '../platform/svg-element-class-list.js';
29 changes: 29 additions & 0 deletions packages/webcomponentsjs/ts_src/platform/get-attribute-names.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
@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;
// In IE11, the `attributes` descriptor is on `Node.prototype`.
const attributesDescriptor =
Object.getOwnPropertyDescriptor(Element_prototype, 'attributes') ??
Object.getOwnPropertyDescriptor(Node.prototype, 'attributes');
// In Safari 9, the `attributes` descriptor's getter is undefined. In Chrome 41,
// the `attributes` descriptor is a data descriptor on each Element instance.
const getAttributes = attributesDescriptor?.get ??
function(this: Element) { return this.attributes; };
const map = Array.prototype.map;

if (!Element_prototype.hasOwnProperty('getAttributeNames')) {
Element_prototype.getAttributeNames = function getAttributeNames(this: Element): Array<string> {
return map.call(getAttributes.call(this), attr => attr.name) as Array<string>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of storing the .attributes getter, why not just call this.attributes here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's just so that this won't call into anything else that might wrap the attributes getter after this polyfill is applied.

};
}