2023-12-09 09:50:12 +01:00
|
|
|
const messages=require("./messages.js");
|
|
|
|
|
2023-12-03 17:14:28 +01:00
|
|
|
const initialise = () =>
|
|
|
|
{
|
2023-12-09 09:50:12 +01:00
|
|
|
const myForm=document.getElementById("trainerForm") as HTMLInputElement;
|
|
|
|
const myFile=document.getElementById("myFile") as HTMLInputElement;
|
|
|
|
const myXPath=document.getElementById("myXPath") as HTMLInputElement;
|
|
|
|
const testButton=document.getElementById("runTest") as HTMLButtonElement;
|
|
|
|
const myIframe=document.getElementById("display") as HTMLIFrameElement;
|
2023-12-10 11:07:16 +01:00
|
|
|
const divResult=document.getElementById("results") as HTMLDivElement;
|
2023-12-09 09:50:12 +01:00
|
|
|
const xpath=require('xpath');
|
|
|
|
|
2023-12-03 17:14:28 +01:00
|
|
|
try
|
|
|
|
{
|
2023-12-09 09:50:12 +01:00
|
|
|
// Accueil : doit choisir un fichier avant de pouvoir tester un XPath
|
2023-12-03 17:14:28 +01:00
|
|
|
myXPath.disabled=true;
|
|
|
|
testButton.disabled=true;
|
2023-12-09 09:50:12 +01:00
|
|
|
myXPath.placeholder=messages.needXMLFile;
|
2023-12-03 17:14:28 +01:00
|
|
|
myIframe.style.display="none";
|
2023-12-10 11:07:16 +01:00
|
|
|
divResult.style.display="none";
|
2023-12-03 17:14:28 +01:00
|
|
|
const dom=require('@xmldom/xmldom').DOMParser;
|
|
|
|
|
|
|
|
myFile.addEventListener("change", function(e)
|
|
|
|
{
|
|
|
|
// L'utilisateur a-t-il vraiment sélectionné un fichier ?
|
|
|
|
// Si oui, est-ce un fichier XML ?
|
|
|
|
const selectedFiles=myFile.files;
|
|
|
|
if(selectedFiles !== null && selectedFiles.length === 1)
|
|
|
|
{
|
|
|
|
// selectedFiles[0].type ne fonctionne pas avec certains navigateurs (Fennec), donc... :
|
|
|
|
const extension=selectedFiles[0].name.substring(selectedFiles[0].name.lastIndexOf(".")+1);
|
|
|
|
if(extension !== "xml")
|
2023-12-10 11:07:16 +01:00
|
|
|
divResult.innerHTML=messages.resultsTitle+messages.notXMLError;
|
2023-12-03 17:14:28 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Création d'un fichier XML temporaire que l'on va charger dans l'iframe :
|
|
|
|
const reader=new FileReader();
|
|
|
|
reader.onload=async function(e)
|
|
|
|
{
|
|
|
|
const xmlDatas=reader.result as string;
|
|
|
|
const xmlTmp=new Blob([xmlDatas], {
|
|
|
|
type: "application/xml",
|
|
|
|
});
|
|
|
|
const iframeURL=URL.createObjectURL(xmlTmp);
|
|
|
|
myIframe.src=iframeURL;
|
|
|
|
myIframe.style.display="block";
|
|
|
|
myXPath.disabled=false;
|
2023-12-09 09:50:12 +01:00
|
|
|
myXPath.placeholder=messages.needXPath;
|
2023-12-03 17:14:28 +01:00
|
|
|
testButton.disabled=false;
|
2023-12-09 09:50:12 +01:00
|
|
|
myXPath.value="";
|
|
|
|
myXPath.placeholder=messages.needXPath;
|
2023-12-03 17:14:28 +01:00
|
|
|
|
|
|
|
// Et on peut commencer à jouer avec !
|
|
|
|
const doc=new dom().parseFromString(xmlDatas, 'text/xml');
|
2023-12-09 09:50:12 +01:00
|
|
|
myForm.addEventListener("submit", (e) =>
|
2023-12-03 17:14:28 +01:00
|
|
|
{
|
2023-12-10 11:07:16 +01:00
|
|
|
try
|
2023-12-03 17:14:28 +01:00
|
|
|
{
|
2023-12-10 11:07:16 +01:00
|
|
|
e.preventDefault();
|
|
|
|
divResult.innerHTML="";
|
|
|
|
divResult.style.display="block";
|
|
|
|
let xPathTested=myXPath.value.trim();
|
|
|
|
if(xPathTested == "")
|
|
|
|
divResult.innerHTML+=messages.resultsTitle+messages.emptyXPathError;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
let resultTxt=messages.resultsTitle;
|
|
|
|
const nodes=xpath.select(xPathTested , doc);
|
|
|
|
if(nodes.length === 0)
|
|
|
|
resultTxt+=messages.noResultForXPath;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(let i = 0; i < nodes.length; i++)
|
|
|
|
resultTxt+=`<li>${nodes[i].toString().replaceAll("<", "<").replaceAll(">", ">").replaceAll("\n","<br>")}</li>`;
|
|
|
|
resultTxt=`<ul>${resultTxt}</ul>`;
|
|
|
|
}
|
|
|
|
divResult.innerHTML=resultTxt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(e: unknown)
|
|
|
|
{
|
|
|
|
if (e instanceof Error && e.message=="XPath parse error" )
|
|
|
|
divResult.innerHTML=messages.resultsTitle+messages.xpathError;
|
|
|
|
else
|
|
|
|
divResult.innerHTML=messages.resultsTitle+messages.exceptionError;
|
2023-12-03 17:14:28 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
reader.readAsText(selectedFiles[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch(e)
|
|
|
|
{
|
2023-12-10 11:07:16 +01:00
|
|
|
divResult.innerHTML=messages.resultsTitle.messages.exceptionError;
|
2023-12-03 17:14:28 +01:00
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
}
|
2023-12-09 09:50:12 +01:00
|
|
|
console.log(messages.gitSources);
|
2023-12-03 17:14:28 +01:00
|
|
|
initialise();
|