{"version":3,"file":"getControlledElement.cjs","names":["_domAccessibilityApi","require","ElementError","Error","constructor","message","element","name","console","error","getControlledElement","role","HTMLInputElement","list","getAttribute","linkedElementIds","linkedElement","split","map","linkedElementId","ownerDocument","getElementById","filter","Boolean","find","getRole","exports"],"sources":["../../../src/test-utils/getControlledElement.ts"],"sourcesContent":["/*!\n * Copyright (c) 2024-present, Okta, Inc. and/or its affiliates. All rights reserved.\n * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the \"License.\")\n *\n * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n *\n * See the License for the specific language governing permissions and limitations under the License.\n */\n\nimport { getRole } from \"dom-accessibility-api\";\nimport { type AriaRole } from \"react\";\n\nclass ElementError extends Error {\n  constructor(message: string, element: HTMLElement) {\n    super(message);\n    this.name = \"ElementError\";\n    console.error(\"ElementError\", element);\n  }\n}\n\n/**\n * Resolves the element controlled by a trigger via `aria-controls`, `aria-owns`, or an `<input list>` association.\n *\n * For `aria-haspopup`:\n * @see https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-haspopup\n * For `datalist`:\n * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/datalist\n */\nexport const getControlledElement = ({\n  element,\n  role,\n}: {\n  element: HTMLElement;\n  /** If this element controls multiple items, it might be valuable to help narrow down the specific item's `role`. */\n  role?: AriaRole;\n}) => {\n  if (element instanceof HTMLInputElement && element.list) {\n    return element.list;\n  }\n\n  if (element.getAttribute(\"aria-expanded\") === \"false\") {\n    throw new ElementError(\n      \"Popup isn't open in ARIA; therefore, it cannot be captured.\",\n      element,\n    );\n  }\n\n  const linkedElementIds =\n    element.getAttribute(\"aria-controls\") || element.getAttribute(\"aria-owns\");\n\n  if (!linkedElementIds) {\n    throw new ElementError(\n      \"Popup isn't linked; therefore, it cannot be captured.\",\n      element,\n    );\n  }\n\n  const linkedElement = linkedElementIds\n    .split(\" \")\n    .map((linkedElementId) =>\n      element.ownerDocument.getElementById(linkedElementId),\n    )\n    .filter((linkedElement): linkedElement is HTMLElement =>\n      Boolean(linkedElement),\n    )\n    .find((linkedElement) =>\n      role ? getRole(linkedElement) === role : Boolean(linkedElement),\n    );\n\n  if (!linkedElement) {\n    throw new ElementError(\n      \"Controlled element isn't available; therefore, it cannot be captured.\",\n      element,\n    );\n  }\n\n  return linkedElement;\n};\n"],"mappings":";;;;;;AAYA,IAAAA,oBAAA,GAAAC,OAAA;AAZA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAKA,MAAMC,YAAY,SAASC,KAAK,CAAC;EAC/BC,WAAWA,CAACC,OAAe,EAAEC,OAAoB,EAAE;IACjD,KAAK,CAACD,OAAO,CAAC;IACd,IAAI,CAACE,IAAI,GAAG,cAAc;IAC1BC,OAAO,CAACC,KAAK,CAAC,cAAc,EAAEH,OAAO,CAAC;EACxC;AACF;AAUO,MAAMI,oBAAoB,GAAGA,CAAC;EACnCJ,OAAO;EACPK;AAKF,CAAC,KAAK;EACJ,IAAIL,OAAO,YAAYM,gBAAgB,IAAIN,OAAO,CAACO,IAAI,EAAE;IACvD,OAAOP,OAAO,CAACO,IAAI;EACrB;EAEA,IAAIP,OAAO,CAACQ,YAAY,CAAC,eAAe,CAAC,KAAK,OAAO,EAAE;IACrD,MAAM,IAAIZ,YAAY,CACpB,6DAA6D,EAC7DI,OACF,CAAC;EACH;EAEA,MAAMS,gBAAgB,GACpBT,OAAO,CAACQ,YAAY,CAAC,eAAe,CAAC,IAAIR,OAAO,CAACQ,YAAY,CAAC,WAAW,CAAC;EAE5E,IAAI,CAACC,gBAAgB,EAAE;IACrB,MAAM,IAAIb,YAAY,CACpB,uDAAuD,EACvDI,OACF,CAAC;EACH;EAEA,MAAMU,aAAa,GAAGD,gBAAgB,CACnCE,KAAK,CA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