{"version":3,"file":"focus-within-polyfill.js","sources":["../src/focus-within.js"],"sourcesContent":["/**\n * :focus-within Polyfill\n * @return {boolean}\n */\nfunction polyfill() {\n /** @const */ var CLASS_NAME = 'focus-within';\n /** @const */ var WHITE_SPACE = ['\\n', '\\t', ' ', '\\r'];\n\n /**\n * Calculate the entire event path.\n *\n * @param {Element} node\n * @return {Array} computedPath\n */\n function computeEventPath(node) {\n var path = [node];\n var parent = null;\n\n while ((parent = node.parentNode || node.host || node.defaultView)) {\n path.push(parent);\n node = parent;\n }\n\n return path;\n }\n\n /**\n * Add user defined attribute to element retaining any previously\n * applied attributes. Attribute can be the 'class' attribute for\n * compatibility reasons.\n *\n * @param {string} value\n * @return {function(Element)} callback\n */\n function addClass(value) {\n return function(el) {\n var attributes =\n typeof el.getAttribute !== 'undefined' ?\n el.getAttribute('class') || '' :\n undefined;\n\n if (\n typeof attributes !== 'undefined' &&\n attributes.indexOf(value) === -1\n ) {\n el.setAttribute('class', attributes.concat(' ', value).trim());\n }\n };\n }\n\n /**\n * Remove user defined attribute value or entire attribute if last one.\n * Attribute can be the 'class' attribute for compatibility reasons.\n *\n * @param {string} value\n * @return {function(Element)} callback\n */\n function removeClass(value) {\n return function(el) {\n var attributes =\n typeof el.getAttribute !== 'undefined' ?\n el.getAttribute('class') || '' :\n undefined;\n\n if (attributes) {\n var index = attributes.indexOf(value);\n // Check if `value` exists in `attributes` and it is either\n // at the start or after a whitespace character. This stops\n // \"focus-within\" being remove from \"js-focus-within\".\n if (\n index >= 0 &&\n (index === 0 ||\n WHITE_SPACE.indexOf(attributes.charAt(index - 1)) >= 0)\n ) {\n var newAttributes = attributes.replace(value, '').trim();\n newAttributes === '' ?\n el.removeAttribute('class') :\n el.setAttribute('class', newAttributes);\n }\n }\n };\n }\n\n /**\n * Attach event listerns to initiate polyfill\n * @return {boolean}\n */\n function load() {\n var handler = function(e) {\n var running;\n\n /**\n * Request animation frame callback.\n * Remove previously applied attributes.\n * Add new attributes.\n */\n function action() {\n running = false;\n\n if ('blur' === e.type) {\n Array.prototype.slice\n .call(computeEventPath(e.target))\n .forEach(removeClass(CLASS_NAME));\n }\n\n if ('focus' === e.type) {\n Array.prototype.slice\n .call(computeEventPath(e.target))\n .forEach(addClass(CLASS_NAME));\n }\n }\n\n if (!running) {\n window.requestAnimationFrame(action);\n running = true;\n }\n };\n\n document.addEventListener('focus', handler, true);\n document.addEventListener('blur', handler, true);\n addClass('js-focus-within')(document.body);\n return true;\n }\n\n try {\n return typeof window !== 'undefined' &&\n !document.querySelector(':' + CLASS_NAME);\n } catch (error) {\n return load();\n }\n}\n\npolyfill();\n"],"names":["polyfill","computeEventPath","node","path","parent","parentNode","host","defaultView","push","addClass","value","el","attributes","getAttribute","undefined","indexOf","setAttribute","concat","trim","removeClass","index","WHITE_SPACE","charAt","newAttributes","replace","removeAttribute","load","handler","e","action","running","type","Array","prototype","slice","call","target","forEach","CLASS_NAME","window","requestAnimationFrame","document","addEventListener","body","querySelector","error"],"mappings":"yBAIAA,SAAiB,EAAG,CAUlBC,QAASA,EAAgB,CAACC,CAAD,CAAO,CAI9B,IAHA,IAAIC,EAAO,CAACD,CAAD,CAGX,CAAQE,CAAR,CAAiBF,CAAAG,WAAjB,EAAoCH,CAAAI,KAApC,EAAiDJ,CAAAK,YAAjD,CAAA,CACEJ,CAAAK,KAAA,CAAUJ,CAAV,CAIF,OAAOD,EATuB,CAoBhCM,QAASA,EAAQ,CAACC,CAAD,CAAQ,CACvB,MAAO,UAASC,EAAI,CAClB,IAAIC,EACyB,WAA3B,GAAA,MAAOD,EAAAE,aAAP,CACEF,CAAAE,aAAA,CAAgB,OAAhB,CADF,EAC8B,EAD9B,CAEEC,IAAAA,EAGoB,YADxB,GACE,MAAOF,EADT,EAEiC,EAFjC,GAEEA,CAAAG,QAAA,CAAmBL,CAAnB,CAFF,EAIEC,CAAAK,aAAA,CAAgB,OAAhB,CAAyBJ,CAAAK,OAAA,CAAkB,GAAlB,CAAuBP,CAAvB,CAAAQ,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