Skip to content

Instantly share code, notes, and snippets.

@sardinecan
Created November 7, 2023 20:01
Show Gist options
  • Save sardinecan/5e5486cb87c472e53d90b08865e1d0a6 to your computer and use it in GitHub Desktop.
Save sardinecan/5e5486cb87c472e53d90b08865e1d0a6 to your computer and use it in GitHub Desktop.
XPointer
<?xml-stylesheet href="../xsltforms/xsltforms.xsl" type="text/xsl"?>
<?xsltforms-options debug="no"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:xf="http://www.w3.org/2002/xforms"
xmlns:ev="http://www.w3.org/2001/xml-events">
<head>
<title>Mouse Events in XForms</title>
<style>
span {
color: orangered;
}
</style>
<model xmlns="http://www.w3.org/2002/xforms" id="index">
<instance>
<data xmlns="">
<xpathStart></xpathStart>
<xpathEnd></xpathEnd>
<text></text>
<index></index>
<length></length>
</data>
</instance>
<xf:action ev:event="getStartXpath">
<xf:setvalue ref="xpathStart" value="event('xpathStart')"/>
</xf:action>
<xf:action ev:event="getEndXpath">
<xf:setvalue ref="xpathEnd" value="event('xpathEnd')"/>
<xf:setvalue ref="text" value="event('text')"/>
<xf:setvalue ref="index" value="event('index')"/>
<xf:setvalue ref="length" value="event('length')"/>
</xf:action>
</model>
<script type="text/javascript">
<![CDATA[
document.onmousedown = function(event) {
if (event===undefined) event = window.event;
var target = 'target' in event? event.target : event.srcElement;
var path= getXPath(target);
XsltForms_xmlevents.dispatch(document.getElementById("index"), "getStartXpath", null, null, null, null, {
xpathStart: path
});
}
function getXPath(element) {
if (element.id!=='')
return "//*[@id='"+element.id+"']";
if (element===document.body)
return element.tagName.toLowerCase();
var ix = 0;
var siblings = element.parentNode.childNodes;
for (var i= 0; i<siblings.length; i++) {
var sibling = siblings[i];
if (sibling===element) return getXPath(element.parentNode) + '/' + element.tagName.toLowerCase() + '[' + (ix + 1) + ']';
if (sibling.nodeType===1 && sibling.tagName === element.tagName) {
ix++;
}
}
}
]]>
</script>
<script type="text/javascript">
<![CDATA[
document.onmouseup = function(event) {
if (event===undefined) event = window.event;
var target = 'target' in event? event.target : event.srcElement;
var path= getXPath(target);
const selection = () => {
if (window.getSelection)
return window.getSelection();
}
XsltForms_xmlevents.dispatch(document.getElementById("index"), "getEndXpath", null, null, null, null, {
xpathEnd: path,
text: selection().toString(),
index: selection().anchorOffset,
length: selection().toString().length
});
}
function getXPath(element) {
if (element.id!=='')
return "//*[@id='"+element.id+"']";
if (element===document.body)
return element.tagName.toLowerCase();
var ix = 0;
var siblings = element.parentNode.childNodes;
for (var i= 0; i<siblings.length; i++) {
var sibling = siblings[i];
if (sibling===element) return getXPath(element.parentNode) + '/' + element.tagName.toLowerCase() + '[' + (ix + 1) + ']';
if (sibling.nodeType===1 && sibling.tagName === element.tagName) {
ix++;
}
}
}
]]>
</script>
</head>
<body>
<div id="transcription">
<h1>Cliquez sur une portion de texte ci-dessous pour obtenir son XPath</h1>
<p id="p1">Lorem ipsum dolor sit amet, <span>consectetur</span> adipisicing elit. <span id="s1">Asperiores</span> cumque dignissimos dolore et, facilis iure iusto laborum neque, officiis quas quis velit? Dolor iure libero non optio repudiandae. Error, nulla.</p>
<!-- ne fonctionne pas avec le contenu mixte… -->
<p>Lorem ipsum dolor sit amet, <span>consectetur</span> adipisicing elit. <span id="s2">Asperiores</span> cumque dignissimos dolore et, facilis iure iusto laborum neque, officiis quas quis velit? Dolor iure libero non optio repudiandae. Error, nulla.</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li><span>Item3</span></li>
</ul>
</div>
<group xmlns="http://www.w3.org/2002/xforms">
Start XPath: <output ref="xpathStart"/><br xmlns="http://www.w3.org/1999/xhtml"/>
End XPath: <output ref="xpathEnd"/><br xmlns="http://www.w3.org/1999/xhtml"/>
Text: <output ref="text"/><br xmlns="http://www.w3.org/1999/xhtml"/>
Index: <output ref="index"/><br xmlns="http://www.w3.org/1999/xhtml"/>
Length: <output ref="length"/><br xmlns="http://www.w3.org/1999/xhtml"/>
</group>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment