@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 3.72 kB
JavaScript
import*as SDK from"../../core/sdk/sdk.js";export const fullQualifiedSelector=function(e,t){return e.nodeType()!==Node.ELEMENT_NODE?e.localName()||e.nodeName().toLowerCase():cssPath(e,t)};export const cssPath=function(e,t){if(e.nodeType()!==Node.ELEMENT_NODE)return"";const o=[];let n=e;for(;n;){const r=cssPathStep(n,Boolean(t),n===e);if(!r)break;if(o.push(r),r.optimized)break;n=n.parentNode}return o.reverse(),o.join(" > ")};export const canGetJSPath=function(e){let t=e;for(;t;){const e=t.ancestorShadowRoot();if(e&&e.shadowRootType()!==SDK.DOMModel.DOMNode.ShadowRootTypes.Open)return!1;t=t.ancestorShadowHost()}return!0};export const jsPath=function(e,t){if(e.nodeType()!==Node.ELEMENT_NODE)return"";const o=[];let n=e;for(;n;)o.push(cssPath(n,t)),n=n.ancestorShadowHost();o.reverse();let r="";for(let e=0;e<o.length;++e){const t=JSON.stringify(o[e]);r+=e?`.shadowRoot.querySelector(${t})`:`document.querySelector(${t})`}return r};const cssPathStep=function(e,t,o){if(e.nodeType()!==Node.ELEMENT_NODE)return null;const n=e.getAttribute("id");if(t){if(n)return new Step(c(n),!0);const t=e.nodeName().toLowerCase();if("body"===t||"head"===t||"html"===t)return new Step(e.nodeNameInCorrectCase(),!0)}const r=e.nodeNameInCorrectCase();if(n)return new Step(r+c(n),!0);const i=e.parentNode;if(!i||i.nodeType()===Node.DOCUMENT_NODE)return new Step(r,!0);function s(e){const t=e.getAttribute("class");return t?t.split(/\s+/g).filter(Boolean).map((function(e){return"$"+e})):[]}function c(e){return"#"+CSS.escape(e)}const a=s(e);let d=!1,N=!1,u=-1,l=-1;const f=i.children();for(let t=0;f&&(-1===u||!N)&&t<f.length;++t){const o=f[t];if(o.nodeType()!==Node.ELEMENT_NODE)continue;if(l+=1,o===e){u=l;continue}if(N)continue;if(o.nodeNameInCorrectCase()!==r)continue;d=!0;const n=new Set(a);if(!n.size){N=!0;continue}const i=s(o);for(let e=0;e<i.length;++e){const t=i[e];if(n.has(t)&&(n.delete(t),!n.size)){N=!0;break}}}let p=r;if(o&&"input"===r.toLowerCase()&&e.getAttribute("type")&&!e.getAttribute("id")&&!e.getAttribute("class")&&(p+="[type="+CSS.escape(e.getAttribute("type")||"")+"]"),N)p+=":nth-child("+(u+1)+")";else if(d)for(const e of a)p+="."+CSS.escape(e.slice(1));return new Step(p,!1)};export const xPath=function(e,t){if(e.nodeType()===Node.DOCUMENT_NODE)return"/";const o=[];let n=e;for(;n;){const e=xPathValue(n,t);if(!e)break;if(o.push(e),e.optimized)break;n=n.parentNode}return o.reverse(),(o.length&&o[0].optimized?"":"/")+o.join("/")};const xPathValue=function(e,t){let o;const n=xPathIndex(e);if(-1===n)return null;switch(e.nodeType()){case Node.ELEMENT_NODE:if(t&&e.getAttribute("id"))return new Step('//*[@id="'+e.getAttribute("id")+'"]',!0);o=e.localName();break;case Node.ATTRIBUTE_NODE:o="@"+e.nodeName();break;case Node.TEXT_NODE:case Node.CDATA_SECTION_NODE:o="text()";break;case Node.PROCESSING_INSTRUCTION_NODE:o="processing-instruction()";break;case Node.COMMENT_NODE:o="comment()";break;case Node.DOCUMENT_NODE:default:o=""}return n>0&&(o+="["+n+"]"),new Step(o,e.nodeType()===Node.DOCUMENT_NODE)},xPathIndex=function(e){function t(e,t){if(e===t)return!0;if(e.nodeType()===Node.ELEMENT_NODE&&t.nodeType()===Node.ELEMENT_NODE)return e.localName()===t.localName();if(e.nodeType()===t.nodeType())return!0;return(e.nodeType()===Node.CDATA_SECTION_NODE?Node.TEXT_NODE:e.nodeType())===(t.nodeType()===Node.CDATA_SECTION_NODE?Node.TEXT_NODE:t.nodeType())}const o=e.parentNode?e.parentNode.children():null;if(!o)return 0;let n;for(let r=0;r<o.length;++r)if(t(e,o[r])&&o[r]!==e){n=!0;break}if(!n)return 0;let r=1;for(let n=0;n<o.length;++n)if(t(e,o[n])){if(o[n]===e)return r;++r}return-1};export class Step{value;optimized;constructor(e,t){this.value=e,this.optimized=t||!1}toString(){return this.value}}