UNPKG

@mariusandra/query-selector-shadow-dom

Version:

use querySelector syntax to search for nodes inside of (nested) shadow roots

2 lines (1 loc) 3.65 kB
var querySelectorShadowDom=function(e){"use strict";function o(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function f(e,t){var n;if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator])return(n=e[Symbol.iterator]()).next.bind(n);if(Array.isArray(e)||(n=function(e,t){if(e){if("string"==typeof e)return o(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?o(e,t):void 0}}(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var r=0;return function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function r(e,o,l,u){void 0===u&&(u=null),e=function(e){function t(){r&&(0<u.length&&/^[~+>]$/.test(u[u.length-1])&&u.push(" "),u.push(r))}var n,r,o,l,u=[],a=[0],i=0,s=/(?:[^\\]|(?:^|[^\\])(?:\\\\)+)$/,c=/^\s+$/,h=[/\s+|\/\*|["'>~+\[\(]/g,/\s+|\/\*|["'\[\]\(\)]/g,/\s+|\/\*|["'\[\]\(\)]/g,null,/\*\//g];e=e.trim();for(;;){if(r="",(o=h[a[a.length-1]]).lastIndex=i,!(n=o.exec(e))){r=e.substr(i),t();break}if(l=i,i=o.lastIndex,l<i-n[0].length&&(r=e.substring(l,i-n[0].length)),a[a.length-1]<3){if(t(),"["===n[0])a.push(1);else if("("===n[0])a.push(2);else if(/^["']$/.test(n[0]))a.push(3),h[3]=new RegExp(n[0],"g");else if("/*"===n[0])a.push(4);else if(/^[\]\)]$/.test(n[0])&&0<a.length)a.pop();else if(/^(?:\s+|[~+>])$/.test(n[0])&&(0<u.length&&!c.test(u[u.length-1])&&0===a[a.length-1]&&u.push(" "),1===a[a.length-1]&&5===u.length&&"="===u[2].charAt(u[2].length-1)&&(u[4]=" "+u[4]),c.test(n[0])))continue;u.push(n[0])}else u[u.length-1]+=r,s.test(u[u.length-1])&&(4===a[a.length-1]&&(u.length<2||c.test(u[u.length-2])?u.pop():u[u.length-1]=" ",n[0]=""),a.pop()),u[u.length-1]+=n[0]}return u.join("").trim()}(e);var t=l.querySelector(e);return document.head.createShadowRoot||document.head.attachShadow?!o&&t?t:a(e,",").reduce(function(e,t){if(!o&&e)return e;var s,c,h,n=a(t.replace(/^\s+/g,"").replace(/\s*([>+~]+)\s*/g,"$1")," ").filter(function(e){return!!e}).map(function(e){return a(e,">")}),r=n.length-1,t=i(n[r][n[r].length-1],l,u),r=(s=n,c=r,h=l,function(e){for(var t,n=c,r=e,o=!1;r&&((t=r).nodeType!==Node.DOCUMENT_FRAGMENT_NODE&&t.nodeType!==Node.DOCUMENT_NODE);){var l=!0;if(1===s[n].length)l=r.matches(s[n]);else for(var u=r,a=f([].concat(s[n]).reverse());!(i=a()).done;){var i=i.value;if(!u||!u.matches(i)){l=!1;break}u=g(u,h)}if(l&&0===n){o=!0;break}l&&n--,r=g(r,h)}return o});return o?e=e.concat(t.filter(r)):(e=t.find(r))||null},o?[]:null):o?l.querySelectorAll(e):t}function a(e,n){return e.match(/\\?.|^$/g).reduce(function(e,t){return'"'!==t||e.sQuote?"'"!==t||e.quote?e.quote||e.sQuote||t!==n?e.a[e.a.length-1]+=t:e.a.push(""):(e.sQuote^=1,e.a[e.a.length-1]+=t):(e.quote^=1,e.a[e.a.length-1]+=t),e},{a:[""]}).a}function g(e,t){e=e.parentNode;return e&&e.host&&11===e.nodeType?e.host:e===t?null:e}function i(t,e,n){void 0===t&&(t=null),void 0===n&&(n=null);var o=[];return n?o=n:(n=function e(t){for(var n,r=0;n=t[r];++r)o.push(n),n.shadowRoot&&e(n.shadowRoot.querySelectorAll("*"))},e.shadowRoot&&n(e.shadowRoot.querySelectorAll("*")),n(e.querySelectorAll("*"))),t?o.filter(function(e){return e.matches(t)}):o}return e.querySelectorAllDeep=function(e,t,n){return void 0===t&&(t=document),void 0===n&&(n=null),r(e,!0,t,n)},e.querySelectorDeep=function(e,t,n){return void 0===t&&(t=document),void 0===n&&(n=null),r(e,!1,t,n)},e.collectAllElementsDeep=i,e}({});