{"version":3,"file":"linkedHtmlSelectors.cjs","names":["_domAccessibilityApi","require","_sanityChecks","getControlledElement","element","role","HTMLInputElement","list","getAttribute","ElementError","linkedElementIds","linkedElement","split","map","linkedElementId","ownerDocument","getElementById","filter","Boolean","find","getRole","exports"],"sources":["../../../src/test-selectors/linkedHtmlSelectors.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\nimport { ElementError } from \"./sanityChecks.js\";\n\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    // This can be `.filter(Boolean)` when Inferred Type Predicates is in TypeScript (which should be part of the version we're using): https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-5.html#:~:text=Inferred%20Type%20Predicates,Thanks%20Dan!\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;AAGA,IAAAC,aAAA,GAAAD,OAAA;AAfA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,GAaO,MAAME,oBAAoB,GAAGA,CAAC;EACnCC,OAAO;EACPC;AAKF,CAAC,KAAK;EACJ,IAAID,OAAO,YAAYE,gBAAgB,IAAIF,OAAO,CAACG,IAAI,EAAE;IACvD,OAAOH,OAAO,CAACG,IAAI;EACrB;EAEA,IAAIH,OAAO,CAACI,YAAY,CAAC,eAAe,CAAC,KAAK,OAAO,EAAE;IACrD,MAAM,IAAIC,0BAAY,CACpB,6DAA6D,EAC7DL,OACF,CAAC;EACH;EAEA,MAAMM,gBAAgB,GACpBN,OAAO,CAACI,YAAY,CAAC,eAAe,CAAC,IAAIJ,OAAO,CAACI,YAAY,CAAC,WAAW,CAAC;EAE5E,IAAI,CAACE,gBAAgB,EAAE;IACrB,MAAM,IAAID,0BAAY,CACpB,uDAAuD,EACvDL,OACF,CAAC;EACH;EAEA,MAAMO,aAAa,GAAGD,gBAAgB,CACnCE,KAAK,CAAC,GAAG,CAAC,CACVC,GAAG,CAAEC,eAAe,IACnBV,OAAO,CAACW,aAAa,CAACC,cAAc,CAACF,eAAe,CACtD,CAAC,CAEAG,MAAM,CAAEN,aAAa,IACpBO,OAAO,CAACP,aAAa,CACvB,CAAC,CACAQ,IAAI,CAAER,aAAa,IAClBN,IAAI,GAAG,IAAAe,4BAAO,EAACT,aAAa,CAAC,KAAKN,IAAI,GAAGa,OAAO,CAACP,aAAa,CAChE,CAAC;EAEH,IAAI,CAACA,aAAa,EAAE;IAClB,MAAM,IAAIF,0BAAY,CACpB,uEAAuE,EACvEL,OACF,CAAC;EACH;EAEA,OAAOO,aAAa;AACtB,CAAC;AAACU,OAAA,CAAAlB,oBAAA,GAAAA,oBAAA","ignoreList":[]}