easy-aria
Version:
The easiest (and safest) way to manipulate ARIA attributes in HTML.
2 lines (1 loc) • 13.2 kB
JavaScript
(function(a,i){typeof exports=="object"&&typeof module<"u"?module.exports=i():typeof define=="function"&&define.amd?define(i):(a=typeof globalThis<"u"?globalThis:a||self,a.aria=i())})(this,function(){"use strict";var T=Object.defineProperty;var q=(a,i,n)=>i in a?T(a,i,{enumerable:!0,configurable:!0,writable:!0,value:n}):a[i]=n;var m=(a,i,n)=>(q(a,typeof i!="symbol"?i+"":i,n),n),g=(a,i,n)=>{if(!i.has(a))throw TypeError("Cannot "+n)};var A=(a,i,n)=>(g(a,i,"read from private field"),n?n.call(a):i.get(a)),p=(a,i,n)=>{if(i.has(a))throw TypeError("Cannot add the same private member more than once");i instanceof WeakSet?i.add(a):i.set(a,n)},$=(a,i,n,o)=>(g(a,i,"write to private field"),o?o.call(a,n):i.set(a,n),n);var k=(a,i,n,o)=>({set _(E){$(a,i,E,n)},get _(){return A(a,i,o)}}),r=(a,i,n)=>(g(a,i,"access private method"),n);var n,o,x,h,w,t,c;const l=class l{constructor(e){p(this,t);m(this,"el");this.el=e}getRole(){return this.el.getAttribute("role")}setRole(e){if(!arguments.length)throw new TypeError("EasyAria.setRole() requires 1 argument, but received 0.");if(typeof e!="string")throw new TypeError('Please specify an ARIA role (e.g., "group").');return this.el.setAttribute("role",e),this}call(e){if(!arguments.length)throw new TypeError("EasyAria.call() requires 1 argument, but received 0.");if(typeof e!="function")throw new TypeError("EasyAria.call() requires a function.");return e.call(this,this.el),this}set(e,s){var d,y;if(!arguments.length)throw new TypeError("EasyAria.set() requires at least 1 argument, but received 0.");if(typeof e!="string")throw new TypeError('Please specify an un-prefixed ARIA 1.2 attribute (e.g., "checked").');switch(e=e.toLowerCase(),e){case"keyshortcuts":case"label":case"placeholder":case"roledescription":case"valuetext":if(typeof s!="string")throw new TypeError(`"aria-${e}" requires a string.`);r(this,t,c).call(this,e,String(s));break;case"activedescendant":case"details":case"errormessage":if(s instanceof Element)r(this,t,c).call(this,e,r(d=l,h,w).call(d,s));else if(typeof s=="string")r(this,t,c).call(this,e,s);else throw new TypeError(`"aria-${e}" requires a string, or pass an Element to use its ID.`);break;case"controls":case"describedby":case"flowto":case"labelledby":case"owns":if(s instanceof Element)r(this,t,c).call(this,e,r(y=l,h,w).call(y,s));else if(Array.isArray(s)||s instanceof NodeList)r(this,t,c).call(this,e,Array.prototype.map.call(s,u=>{var b;return r(b=l,h,w).call(b,u)}).filter(Boolean).join(" "));else if(typeof s=="string")r(this,t,c).call(this,e,s);else throw new TypeError(`"aria-${e}" requires a string, or pass an Element or Array/NodeList of Elements to use their IDs.`);break;case"atomic":case"busy":case"disabled":case"modal":case"multiline":case"multiselectable":case"readonly":case"required":if(typeof s>"u")r(this,t,c).call(this,e,String(!0));else switch(s){case!0:case!1:r(this,t,c).call(this,e,String(s));break;default:throw new TypeError(`"aria-${e}" requires a boolean value.`)}break;case"expanded":case"grabbed":case"hidden":case"selected":if(typeof s>"u")r(this,t,c).call(this,e,String(!0));else switch(s){case!0:case!1:r(this,t,c).call(this,e,String(s));break;case"undefined":r(this,t,c).call(this,e,s);break;default:throw new TypeError(`"aria-${e}" requires a boolean value, or the string "undefined".`)}break;case"checked":case"pressed":if(typeof s>"u")r(this,t,c).call(this,e,String(!0));else switch(s){case!0:case!1:r(this,t,c).call(this,e,String(s));break;case"mixed":case"undefined":r(this,t,c).call(this,e,s);break;default:throw new TypeError(`"aria-${e}" requires a boolean value, or any of the following strings: "mixed", "undefined".`)}break;case"current":if(typeof s>"u")r(this,t,c).call(this,e,String(!0));else switch(s){case!0:case!1:r(this,t,c).call(this,e,String(s));break;case"page":case"step":case"location":case"date":case"time":r(this,t,c).call(this,e,s);break;default:throw new TypeError(`"aria-${e}" requires a boolean value, or any of the following strings: "page", "step", "location", "date", "time".`)}break;case"haspopup":if(typeof s>"u")r(this,t,c).call(this,e,String(!0));else switch(s){case!0:case!1:r(this,t,c).call(this,e,String(s));break;case"menu":case"listbox":case"tree":case"grid":case"dialog":r(this,t,c).call(this,e,s);break;default:throw new TypeError(`"aria-${e}" requires a boolean value, or any of the following strings: "menu", "listbox", "tree", "grid", "dialog".`)}break;case"invalid":if(typeof s>"u")r(this,t,c).call(this,e,String(!0));else switch(s){case!0:case!1:r(this,t,c).call(this,e,String(s));break;case"grammar":case"spelling":r(this,t,c).call(this,e,s);break;default:throw new TypeError(`"aria-${e}" requires a boolean value, or any of the following strings: "grammar", "spelling".`)}break;case"autocomplete":switch(s){case"both":case"inline":case"list":case"none":r(this,t,c).call(this,e,s);break;default:throw new TypeError(`"aria-${e}" only accepts the values specified at: https://www.w3.org/TR/wai-aria-1.2/#aria-${e}`)}break;case"dropeffect":switch(s){case"copy":case"execute":case"link":case"move":case"none":case"popup":r(this,t,c).call(this,e,s);break;default:throw new TypeError(`"aria-${e}" only accepts the values specified at: https://www.w3.org/TR/wai-aria-1.2/#aria-${e}`)}break;case"live":switch(s){case"assertive":case"off":case"polite":r(this,t,c).call(this,e,s);break;default:throw new TypeError(`"aria-${e}" only accepts the values specified at: https://www.w3.org/TR/wai-aria-1.2/#aria-${e}`)}break;case"orientation":switch(s){case"horizontal":case"undefined":case"vertical":r(this,t,c).call(this,e,s);break;default:throw new TypeError(`"aria-${e}" only accepts the values specified at: https://www.w3.org/TR/wai-aria-1.2/#aria-${e}`)}break;case"relevant":switch(s){case"additions":case"additions text":case"all":case"removals":case"text":r(this,t,c).call(this,e,s);break;default:throw new TypeError(`"aria-${e}" only accepts the values specified at: https://www.w3.org/TR/wai-aria-1.2/#aria-${e}`)}break;case"sort":switch(s){case"ascending":case"descending":case"none":case"other":r(this,t,c).call(this,e,s);break;default:throw new TypeError(`"aria-${e}" only accepts the values specified at: https://www.w3.org/TR/wai-aria-1.2/#aria-${e}`)}break;case"colcount":case"colindex":case"colspan":case"level":case"posinset":case"rowcount":case"rowindex":case"rowspan":case"setsize":case"valuemax":case"valuemin":case"valuenow":{if(typeof s=="bigint"||typeof s=="number"||typeof s=="string"){const u=Number(s);if(Number.isNaN(u))throw new TypeError(`"aria-${e}" requires a number or numerical string.`);r(this,t,c).call(this,e,String(u))}else throw new TypeError(`"aria-${e}" requires a number or numerical string.`);break}default:{const u=e;throw new TypeError(`Invalid attribute: "aria-${u}". Please specify an un-prefixed ARIA 1.2 attribute (e.g., "checked").`)}}return this}unset(e){if(!arguments.length)throw new TypeError("EasyAria.unset() requires 1 argument, but received 0.");if(typeof e!="string")throw new TypeError('Please specify an un-prefixed ARIA 1.2 attribute (e.g., "checked").');switch(e=e.toLowerCase(),e){case"keyshortcuts":case"label":case"placeholder":case"roledescription":case"valuetext":case"activedescendant":case"details":case"errormessage":case"controls":case"describedby":case"flowto":case"labelledby":case"owns":case"atomic":case"busy":case"disabled":case"modal":case"multiline":case"multiselectable":case"readonly":case"required":case"expanded":case"grabbed":case"hidden":case"selected":case"checked":case"current":case"haspopup":case"invalid":case"pressed":case"autocomplete":case"dropeffect":case"live":case"orientation":case"relevant":case"sort":case"colcount":case"colindex":case"colspan":case"level":case"posinset":case"rowcount":case"rowindex":case"rowspan":case"setsize":case"valuemax":case"valuemin":case"valuenow":this.el.removeAttribute(`aria-${e}`);break;default:{const s=e;throw new TypeError(`Invalid attribute: "aria-${s}". Please specify an un-prefixed ARIA 1.2 attribute (e.g., "checked").`)}}return this}get(e){if(!arguments.length)throw new TypeError("EasyAria.get() requires 1 argument, but received 0.");if(typeof e!="string")throw new TypeError('Please specify an un-prefixed ARIA 1.2 attribute (e.g., "checked").');e=e.toLowerCase();const s=this.el.getAttribute(`aria-${String(e)}`);switch(e){case"activedescendant":case"controls":case"describedby":case"details":case"errormessage":case"flowto":case"keyshortcuts":case"label":case"labelledby":case"owns":case"placeholder":case"roledescription":case"valuetext":return s;case"autocomplete":switch(s){case"both":case"inline":case"list":case"none":return s}break;case"dropeffect":switch(s){case"copy":case"execute":case"link":case"move":case"none":case"popup":return s}break;case"live":switch(s){case"assertive":case"off":case"polite":return s}break;case"orientation":switch(s){case"horizontal":case"undefined":case"vertical":return s}break;case"relevant":switch(s){case"additions":case"additions text":case"all":case"removals":case"text":return s}break;case"sort":switch(s){case"ascending":case"descending":case"none":case"other":return s}break;case"expanded":case"grabbed":case"hidden":case"selected":switch(s){case"true":return!0;case"false":return!1;case"undefined":return s}break;case"checked":case"pressed":switch(s){case"true":return!0;case"false":return!1;case"mixed":case"undefined":return s}break;case"current":switch(s){case"true":return!0;case"false":return!1;case"page":case"step":case"location":case"date":case"time":return s}break;case"haspopup":switch(s){case"true":return!0;case"false":return!1;case"menu":case"listbox":case"tree":case"grid":case"dialog":return s}break;case"invalid":switch(s){case"true":return!0;case"false":return!1;case"grammar":case"spelling":return s}break;case"atomic":case"busy":case"disabled":case"modal":case"multiline":case"multiselectable":case"readonly":case"required":switch(s){case"true":return!0;case"false":return!1}break;case"colcount":case"colindex":case"colspan":case"level":case"posinset":case"rowcount":case"rowindex":case"rowspan":case"setsize":case"valuemax":case"valuemin":case"valuenow":{const d=Number(s);if(!Number.isNaN(d))return d;break}default:{const d=e;throw new TypeError(`Invalid attribute: "aria-${d}". Please specify an un-prefixed ARIA 1.2 attribute (e.g., "checked").`)}}return null}check(){return this.set("checked")}uncheck(){return this.set("checked",!1)}toggleChecked(){return this.set("checked",!this.isChecked())}control(e){return this.set("controls",e)}describeWith(e){return this.set("describedby",e)}disable(){return this.set("disabled")}enable(){return this.set("disabled",!1)}toggleDisabled(){return this.set("disabled",!this.isDisabled())}expand(){return this.set("expanded")}collapse(){return this.set("expanded",!1)}toggleExpanded(){return this.set("expanded",!this.isExpanded())}flowTo(e){return this.set("flowto",e)}grab(){return this.set("grabbed")}ungrab(){return this.set("grabbed",!1)}toggleGrabbed(){return this.set("grabbed",!this.isGrabbed())}hide(){return this.set("hidden")}unhide(){return this.set("hidden",!1)}toggleHidden(){return this.set("hidden",!this.isHidden())}label(e){return this.set("label",e)}labelWith(e){return this.set("labelledby",e)}own(e){return this.set("owns",e)}press(){return this.set("pressed")}unpress(){return this.set("pressed",!1)}togglePressed(){return this.set("pressed",!this.isPressed())}require(){return this.set("required")}unrequire(){return this.set("required",!1)}toggleRequired(){return this.set("required",!this.isRequired())}describeRole(e){return this.set("roledescription",e)}select(){return this.set("selected")}unselect(){return this.set("selected",!1)}toggleSelected(){return this.set("selected",!this.isSelected())}isAtomic(){return this.get("atomic")===!0}isBusy(){return this.get("busy")===!0}isChecked(){return this.get("checked")===!0}isCurrent(e){const s=this.get("current");return e?s===e:s===!0||s==="page"||s==="step"||s==="location"||s==="date"||s==="time"}isDisabled(){return this.get("disabled")===!0}isExpanded(){return this.get("expanded")===!0}isGrabbed(){return this.get("grabbed")===!0}hasPopup(e){const s=this.get("haspopup");return e?s===e:s===!0||s==="menu"||s==="listbox"||s==="tree"||s==="grid"||s==="dialog"}isHidden(){return this.get("hidden")===!0}isInvalid(e){const s=this.get("invalid");return e?s===e:s===!0||s==="grammar"||s==="spelling"}isModal(){return this.get("modal")===!0}isMultiline(){return this.get("multiline")===!0}isMultiselectable(){return this.get("multiselectable")===!0}isPressed(){return this.get("pressed")===!0}isReadonly(){return this.get("readonly")===!0}isRequired(){return this.get("required")===!0}isSelected(){return this.get("selected")===!0}};n=new WeakMap,o=new WeakSet,x=function(){return`easy-aria-id-${k(this,n)._++}`},h=new WeakSet,w=function(e){var s;return e instanceof Element?(e.id||(e.id=r(s=l,o,x).call(s)),e.id):""},t=new WeakSet,c=function(e,s){this.el.setAttribute(`aria-${e}`,s)},p(l,o),p(l,h),p(l,n,1);let a=l;function i(f){if(typeof f=="string"){const e=document.querySelector(f);if(e)return new a(e)}else if(f instanceof Element)return new a(f);return null}return i});