@lwc/aria-reflection
Advanced tools
Comparing version 4.0.0-alpha.0 to 4.0.0
@@ -6,8 +6,4 @@ /** | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
var shared = require('@lwc/shared'); | ||
/* | ||
* Copyright (c) 2018, salesforce.com, inc. | ||
* Copyright (c) 2023, salesforce.com, inc. | ||
* All rights reserved. | ||
@@ -17,57 +13,80 @@ * SPDX-License-Identifier: MIT | ||
*/ | ||
function detect(propName, prototype) { | ||
return shared.isUndefined(shared.getOwnPropertyDescriptor(prototype, propName)); | ||
} | ||
/* | ||
* Copyright (c) 2018, salesforce.com, inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: MIT | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT | ||
*/ | ||
function createAriaPropertyPropertyDescriptor(attrName) { | ||
// Note that we need to call this.{get,set,has,remove}Attribute rather than dereferencing | ||
// from Element.prototype, because these methods are overridden in LightningElement. | ||
return { | ||
get() { | ||
// reflect what's in the attribute | ||
return this.hasAttribute(attrName) ? this.getAttribute(attrName) : null; | ||
}, | ||
set(newValue) { | ||
// reflect into the corresponding attribute | ||
if (shared.isNull(newValue)) { | ||
this.removeAttribute(attrName); | ||
} | ||
else { | ||
this.setAttribute(attrName, newValue); | ||
} | ||
}, | ||
configurable: true, | ||
enumerable: true, | ||
}; | ||
} | ||
function patch(propName, prototype) { | ||
const attrName = shared.AriaPropNameToAttrNameMap[propName]; | ||
const descriptor = createAriaPropertyPropertyDescriptor(attrName); | ||
shared.defineProperty(prototype, propName, descriptor); | ||
} | ||
/* | ||
* Copyright (c) 2018, salesforce.com, inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: MIT | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT | ||
*/ | ||
function applyAriaReflection(prototype = Element.prototype) { | ||
const ElementPrototypeAriaPropertyNames = shared.keys(shared.AriaPropNameToAttrNameMap); | ||
for (let i = 0, len = ElementPrototypeAriaPropertyNames.length; i < len; i += 1) { | ||
const propName = ElementPrototypeAriaPropertyNames[i]; | ||
if (detect(propName, prototype)) { | ||
patch(propName, prototype); | ||
} | ||
// Minimal polyfill of ARIA string reflection, plus some non-standard ARIA props | ||
// Taken from https://github.com/salesforce/lwc/blob/44a01ef/packages/%40lwc/shared/src/aria.ts#L22-L70 | ||
// This is designed for maximum backwards compatibility on LEX - it should never change. | ||
// We deliberately don't import from @lwc/shared because that would make this code less portable. | ||
const ARIA_PROPERTIES = [ | ||
'ariaActiveDescendant', | ||
'ariaAtomic', | ||
'ariaAutoComplete', | ||
'ariaBusy', | ||
'ariaChecked', | ||
'ariaColCount', | ||
'ariaColIndex', | ||
'ariaColSpan', | ||
'ariaControls', | ||
'ariaCurrent', | ||
'ariaDescribedBy', | ||
'ariaDetails', | ||
'ariaDisabled', | ||
'ariaErrorMessage', | ||
'ariaExpanded', | ||
'ariaFlowTo', | ||
'ariaHasPopup', | ||
'ariaHidden', | ||
'ariaInvalid', | ||
'ariaKeyShortcuts', | ||
'ariaLabel', | ||
'ariaLabelledBy', | ||
'ariaLevel', | ||
'ariaLive', | ||
'ariaModal', | ||
'ariaMultiLine', | ||
'ariaMultiSelectable', | ||
'ariaOrientation', | ||
'ariaOwns', | ||
'ariaPlaceholder', | ||
'ariaPosInSet', | ||
'ariaPressed', | ||
'ariaReadOnly', | ||
'ariaRelevant', | ||
'ariaRequired', | ||
'ariaRoleDescription', | ||
'ariaRowCount', | ||
'ariaRowIndex', | ||
'ariaRowSpan', | ||
'ariaSelected', | ||
'ariaSetSize', | ||
'ariaSort', | ||
'ariaValueMax', | ||
'ariaValueMin', | ||
'ariaValueNow', | ||
'ariaValueText', | ||
'role', | ||
]; | ||
for (const prop of ARIA_PROPERTIES) { | ||
const attribute = prop.replace(/^aria/, 'aria-').toLowerCase(); // e.g. ariaPosInSet => aria-posinset | ||
if (!Object.getOwnPropertyDescriptor(Element.prototype, prop)) { | ||
Object.defineProperty(Element.prototype, prop, { | ||
get() { | ||
return this.getAttribute(attribute); | ||
}, | ||
set(value) { | ||
// Per the spec, only null is treated as removing the attribute. However, Chromium/WebKit currently | ||
// differ from the spec and allow undefined as well. Here, we follow the spec, as well as | ||
// our historical behavior. See: https://github.com/w3c/aria/issues/1858 | ||
if (value === null) { | ||
this.removeAttribute(attribute); | ||
} | ||
else { | ||
this.setAttribute(attribute, value); | ||
} | ||
}, | ||
// configurable and enumerable to allow it to be overridden – this mimics Safari's/Chrome's behavior | ||
configurable: true, | ||
enumerable: true, | ||
}); | ||
} | ||
} | ||
exports.applyAriaReflection = applyAriaReflection; | ||
/** version: 4.0.0-alpha.0 */ | ||
/** version: 4.0.0 */ | ||
//# sourceMappingURL=index.cjs.js.map |
@@ -1,1 +0,1 @@ | ||
export declare function applyAriaReflection(prototype?: any): void; | ||
declare const ARIA_PROPERTIES: string[]; |
/** | ||
* Copyright (C) 2023 salesforce.com, inc. | ||
*/ | ||
import { isUndefined, getOwnPropertyDescriptor, AriaPropNameToAttrNameMap, defineProperty, isNull, keys } from '@lwc/shared'; | ||
/* | ||
* Copyright (c) 2018, salesforce.com, inc. | ||
* Copyright (c) 2023, salesforce.com, inc. | ||
* All rights reserved. | ||
@@ -12,57 +10,80 @@ * SPDX-License-Identifier: MIT | ||
*/ | ||
function detect(propName, prototype) { | ||
return isUndefined(getOwnPropertyDescriptor(prototype, propName)); | ||
} | ||
/* | ||
* Copyright (c) 2018, salesforce.com, inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: MIT | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT | ||
*/ | ||
function createAriaPropertyPropertyDescriptor(attrName) { | ||
// Note that we need to call this.{get,set,has,remove}Attribute rather than dereferencing | ||
// from Element.prototype, because these methods are overridden in LightningElement. | ||
return { | ||
get() { | ||
// reflect what's in the attribute | ||
return this.hasAttribute(attrName) ? this.getAttribute(attrName) : null; | ||
}, | ||
set(newValue) { | ||
// reflect into the corresponding attribute | ||
if (isNull(newValue)) { | ||
this.removeAttribute(attrName); | ||
} | ||
else { | ||
this.setAttribute(attrName, newValue); | ||
} | ||
}, | ||
configurable: true, | ||
enumerable: true, | ||
}; | ||
} | ||
function patch(propName, prototype) { | ||
const attrName = AriaPropNameToAttrNameMap[propName]; | ||
const descriptor = createAriaPropertyPropertyDescriptor(attrName); | ||
defineProperty(prototype, propName, descriptor); | ||
} | ||
/* | ||
* Copyright (c) 2018, salesforce.com, inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: MIT | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT | ||
*/ | ||
function applyAriaReflection(prototype = Element.prototype) { | ||
const ElementPrototypeAriaPropertyNames = keys(AriaPropNameToAttrNameMap); | ||
for (let i = 0, len = ElementPrototypeAriaPropertyNames.length; i < len; i += 1) { | ||
const propName = ElementPrototypeAriaPropertyNames[i]; | ||
if (detect(propName, prototype)) { | ||
patch(propName, prototype); | ||
} | ||
// Minimal polyfill of ARIA string reflection, plus some non-standard ARIA props | ||
// Taken from https://github.com/salesforce/lwc/blob/44a01ef/packages/%40lwc/shared/src/aria.ts#L22-L70 | ||
// This is designed for maximum backwards compatibility on LEX - it should never change. | ||
// We deliberately don't import from @lwc/shared because that would make this code less portable. | ||
const ARIA_PROPERTIES = [ | ||
'ariaActiveDescendant', | ||
'ariaAtomic', | ||
'ariaAutoComplete', | ||
'ariaBusy', | ||
'ariaChecked', | ||
'ariaColCount', | ||
'ariaColIndex', | ||
'ariaColSpan', | ||
'ariaControls', | ||
'ariaCurrent', | ||
'ariaDescribedBy', | ||
'ariaDetails', | ||
'ariaDisabled', | ||
'ariaErrorMessage', | ||
'ariaExpanded', | ||
'ariaFlowTo', | ||
'ariaHasPopup', | ||
'ariaHidden', | ||
'ariaInvalid', | ||
'ariaKeyShortcuts', | ||
'ariaLabel', | ||
'ariaLabelledBy', | ||
'ariaLevel', | ||
'ariaLive', | ||
'ariaModal', | ||
'ariaMultiLine', | ||
'ariaMultiSelectable', | ||
'ariaOrientation', | ||
'ariaOwns', | ||
'ariaPlaceholder', | ||
'ariaPosInSet', | ||
'ariaPressed', | ||
'ariaReadOnly', | ||
'ariaRelevant', | ||
'ariaRequired', | ||
'ariaRoleDescription', | ||
'ariaRowCount', | ||
'ariaRowIndex', | ||
'ariaRowSpan', | ||
'ariaSelected', | ||
'ariaSetSize', | ||
'ariaSort', | ||
'ariaValueMax', | ||
'ariaValueMin', | ||
'ariaValueNow', | ||
'ariaValueText', | ||
'role', | ||
]; | ||
for (const prop of ARIA_PROPERTIES) { | ||
const attribute = prop.replace(/^aria/, 'aria-').toLowerCase(); // e.g. ariaPosInSet => aria-posinset | ||
if (!Object.getOwnPropertyDescriptor(Element.prototype, prop)) { | ||
Object.defineProperty(Element.prototype, prop, { | ||
get() { | ||
return this.getAttribute(attribute); | ||
}, | ||
set(value) { | ||
// Per the spec, only null is treated as removing the attribute. However, Chromium/WebKit currently | ||
// differ from the spec and allow undefined as well. Here, we follow the spec, as well as | ||
// our historical behavior. See: https://github.com/w3c/aria/issues/1858 | ||
if (value === null) { | ||
this.removeAttribute(attribute); | ||
} | ||
else { | ||
this.setAttribute(attribute, value); | ||
} | ||
}, | ||
// configurable and enumerable to allow it to be overridden – this mimics Safari's/Chrome's behavior | ||
configurable: true, | ||
enumerable: true, | ||
}); | ||
} | ||
} | ||
export { applyAriaReflection }; | ||
/** version: 4.0.0-alpha.0 */ | ||
/** version: 4.0.0 */ | ||
//# sourceMappingURL=index.js.map |
@@ -7,3 +7,3 @@ { | ||
"name": "@lwc/aria-reflection", | ||
"version": "4.0.0-alpha.0", | ||
"version": "4.0.0", | ||
"description": "ARIA element reflection polyfill for strings", | ||
@@ -44,3 +44,3 @@ "keywords": [ | ||
"outputs": [ | ||
"./dist" | ||
"{projectRoot}/dist" | ||
] | ||
@@ -50,5 +50,3 @@ } | ||
}, | ||
"dependencies": { | ||
"@lwc/shared": "4.0.0-alpha.0" | ||
} | ||
} | ||
"dependencies": {} | ||
} |
# @lwc/aria-reflection | ||
> **Note:** use this code at your own risk. It is optimized for backwards-compatibility, not | ||
> as a forward-looking polyfill that keeps up to date with web standards. | ||
Polyfill for [ARIA string reflection](https://wicg.github.io/aom/spec/aria-reflection.html) on Elements. | ||
@@ -24,17 +27,7 @@ This is part of the [Accessibility Object Model](https://wicg.github.io/aom/explainer.html) (AOM). | ||
```js | ||
import { applyAriaReflection } from '@lwc/aria-reflection'; | ||
applyAriaReflection(); | ||
import '@lwc/aria-reflection'; | ||
``` | ||
The polyfill is applied as soon as the function is executed. | ||
The polyfill is applied globally to `Element.prototype` as soon as the module is imported. | ||
Optionally, you can pass in a custom prototype: | ||
```js | ||
applyAriaReflection(MyCustomElement.prototype); | ||
``` | ||
By default, the polyfill is applied to the global `Element.prototype`. | ||
## Implementation | ||
@@ -41,0 +34,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
11637
0
178
0
7
88
- Removed@lwc/shared@4.0.0-alpha.0
- Removed@lwc/shared@4.0.0-alpha.0(transitive)