You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to use the library to evaluate xPath in shadow DOMs in HTML, but have a problem when I use it with absolute xPath.
Per definition, absolute XPath should look for the target in all of the document, but the document itself does not have access to the content of shadow DOM (which are document-fragment). In my case, the case where we use Shadow DOM as a document and have the absolute xpath only inside it, is enough. Is there a way to achieve it ?
import*asxpathfrom'xpath'constevaluate=(path,node)=>{constxPath=xpath.parse(path)constoptions={expressionContextNode: node,isHtml: true,namespaces: '',node: node,}returnxPath.evaluate(options)}consthost=document.querySelector('#host')constshadowRoot=host.attachShadow({mode: 'open'})consttarget=document.createElement('div')target.id='target'shadowRoot.appendChild(target)constnestedTarget=document.createElement('div')nestedTarget.id='nestedTarget'target.appendChild(nestedTarget)// Log to consoleconsole.log('host',host)console.log('shadowRoot',shadowRoot.innerHTML)console.log('Shadow DOM - relative',evaluate('div',shadowRoot).nodes)console.log('Shadow DOM - relative nested',evaluate('div/div',shadowRoot).nodes)console.log('Shadow DOM - absolute',evaluate('//div',shadowRoot).nodes)
The screenshot from the devTools
And the console
The text was updated successfully, but these errors were encountered:
From what I see, in PathExpr.applyLocationPath, we shouldn't have startNodes = [PathExpr.getRoot(xpc, nodes)] if the node is a document-fragment.
Moreover, in PathExpr.applySteps, it seems that for shadow DOM, the reduce() goes "too far", by that I mean that applyStepToNodes was called once to often.
Hello,
I want to use the library to evaluate xPath in shadow DOMs in HTML, but have a problem when I use it with absolute xPath.
Per definition, absolute XPath should look for the target in all of the document, but the document itself does not have access to the content of shadow DOM (which are
document-fragment
). In my case, the case where we use Shadow DOM as a document and have the absolute xpath only inside it, is enough. Is there a way to achieve it ?Here is the HTML and Javascript used:
The screenshot from the devTools
And the console
The text was updated successfully, but these errors were encountered: