Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
panaC committed Nov 6, 2024
1 parent dc9c1ea commit 39bd2ca
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 26 deletions.
14 changes: 6 additions & 8 deletions build/demo/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { processFragmentDirective } from "../vendor/treora-text-fragment/index.j
function copyPreContent(event) {
// Get the pre element containing the text to be copied
var preElement = event.target.nextSibling;
while (preElement && preElement.nodeName !== 'PRE') {
while (preElement && preElement.nodeName !== 'pre') {
preElement = preElement.nextSibling;
}
if (!preElement)
Expand All @@ -40,7 +40,7 @@ preElements.forEach(function (preElement) {
// Create a button element
var button = document.createElement('button');
button.innerHTML = '<i class="fa fa-clipboard" aria-hidden="true"></i> Copy';
button.onclick = copyPreContent;
button.addEventListener('click', copyPreContent);
// Add the button just before the pre element
preElement.parentNode.insertBefore(button, preElement);
});
Expand All @@ -58,8 +58,7 @@ function createXPathSelectorMatcher(selector) {
return async function* matchAll(scope) {
const scopeRange = toRange(scope);
const document = ownerDocument(scopeRange);
const scopeRangeElement = scopeRange.commonAncestorContainer;
const element = nodeFromXPath(selector.value, scopeRangeElement);
const element = nodeFromXPath(selector.value);
console.log("XPath node found :", element);
if (!element)
throw new Error("XPath node not found !:");
Expand Down Expand Up @@ -196,10 +195,9 @@ const describeRangeXPathSelector = async (range) => {
if (!commonAncestorHTMLElement) {
return undefined;
}
const source = document.getElementById("source");
return {
type: "XPathSelector",
value: xpathFromNode(commonAncestorHTMLElement, source),
value: xpathFromNode(commonAncestorHTMLElement),
refinedBy: await describeTextPosition(rangeNormalize, commonAncestorHTMLElement),
};
};
Expand Down Expand Up @@ -408,5 +406,5 @@ const debounceInputChange = debounce(async (e) => {
}, 500);
inputTextArea.addEventListener("change", debounceInputChange);
const inputButton = document.getElementById("inputButton");
inputButton.onclick = () => debounceInputChange();
;
inputButton.addEventListener("click", debounceInputChange);
// inputButton.onclick = () => debounceInputChange();;
2 changes: 1 addition & 1 deletion build/vendor/anchoring/xpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function getPathSegment(node) {
* @param node - The node to generate a path to
* @param root - Root node to which the returned path is relative
*/
export function xpathFromNode(node, root) {
export function xpathFromNode(node, root = document.body) {
let xpath = '';
let elem = node;
while (elem !== root) {
Expand Down
14 changes: 6 additions & 8 deletions demo/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ interface TextFragmentSelector<T = undefined> extends Selector<T> {
function copyPreContent(event: any) {
// Get the pre element containing the text to be copied
var preElement = event.target.nextSibling;
while (preElement && preElement.nodeName !== 'PRE') {
while (preElement && preElement.nodeName !== 'pre') {
preElement = preElement.nextSibling;
}
if (!preElement) return;
Expand All @@ -63,7 +63,7 @@ preElements.forEach(function (preElement) {
// Create a button element
var button = document.createElement('button');
button.innerHTML = '<i class="fa fa-clipboard" aria-hidden="true"></i> Copy';
button.onclick = copyPreContent;
button.addEventListener('click', copyPreContent);
// Add the button just before the pre element
preElement.parentNode!.insertBefore(button, preElement);
});
Expand All @@ -85,8 +85,7 @@ function createXPathSelectorMatcher(
return async function* matchAll(scope) {
const scopeRange = toRange(scope);
const document = ownerDocument(scopeRange);
const scopeRangeElement = scopeRange.commonAncestorContainer as HTMLElement;
const element = nodeFromXPath(selector.value, scopeRangeElement);
const element = nodeFromXPath(selector.value);
console.log("XPath node found :", element);
if (!element) throw new Error("XPath node not found !:");
const range = document.createRange();
Expand Down Expand Up @@ -261,10 +260,9 @@ const describeRangeXPathSelector = async (range: Range): Promise<XPathSelector |
return undefined;
}

const source = document.getElementById("source") as HTMLElement;
return {
type: "XPathSelector",
value: xpathFromNode(commonAncestorHTMLElement, source),
value: xpathFromNode(commonAncestorHTMLElement),
refinedBy: await describeTextPosition(
rangeNormalize,
commonAncestorHTMLElement,
Expand Down Expand Up @@ -488,7 +486,7 @@ const debounceInputChange = debounce(async (e: any) => {
inputTextArea.addEventListener("change", debounceInputChange);

const inputButton = document.getElementById("inputButton") as HTMLElement;
inputButton.addEventListener("click", debounceInputChange);


inputButton.onclick = () => debounceInputChange();;
// inputButton.onclick = () => debounceInputChange();;

2 changes: 1 addition & 1 deletion vendor/anchoring/xpath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function getPathSegment(node: Node): string {
* @param node - The node to generate a path to
* @param root - Root node to which the returned path is relative
*/
export function xpathFromNode(node: Node, root: Node) {
export function xpathFromNode(node: Node, root: Node = document.body) {
let xpath = '';

let elem: Node | null = node;
Expand Down
14 changes: 6 additions & 8 deletions web/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1674,7 +1674,7 @@ function getPathSegment(node) {
const pos = getNodePosition(node);
return `${name}[${pos}]`;
}
function xpathFromNode(node, root) {
function xpathFromNode(node, root = document.body) {
let xpath = "";
let elem = node;
while (elem !== root) {
Expand Down Expand Up @@ -2321,7 +2321,7 @@ function firstBoundaryPointAfter([node, offset]) {
// build/demo/index.mjs
function copyPreContent(event) {
var preElement = event.target.nextSibling;
while (preElement && preElement.nodeName !== "PRE") {
while (preElement && preElement.nodeName !== "pre") {
preElement = preElement.nextSibling;
}
if (!preElement)
Expand All @@ -2337,7 +2337,7 @@ var preElements = document.body.querySelectorAll("pre");
preElements.forEach(function(preElement) {
var button = document.createElement("button");
button.innerHTML = '<i class="fa fa-clipboard" aria-hidden="true"></i> Copy';
button.onclick = copyPreContent;
button.addEventListener("click", copyPreContent);
preElement.parentNode.insertBefore(button, preElement);
});
function debounce(func, timeout) {
Expand All @@ -2353,8 +2353,7 @@ function createXPathSelectorMatcher(selector2) {
return async function* matchAll(scope) {
const scopeRange = toRange(scope);
const document2 = ownerDocument(scopeRange);
const scopeRangeElement = scopeRange.commonAncestorContainer;
const element = nodeFromXPath(selector2.value, scopeRangeElement);
const element = nodeFromXPath(selector2.value);
console.log("XPath node found :", element);
if (!element)
throw new Error("XPath node not found !:");
Expand Down Expand Up @@ -2474,10 +2473,9 @@ var describeRangeXPathSelector = async (range) => {
if (!commonAncestorHTMLElement) {
return void 0;
}
const source = document.getElementById("source");
return {
type: "XPathSelector",
value: xpathFromNode(commonAncestorHTMLElement, source),
value: xpathFromNode(commonAncestorHTMLElement),
refinedBy: await describeTextPosition2(rangeNormalize, commonAncestorHTMLElement)
};
};
Expand Down Expand Up @@ -2671,7 +2669,7 @@ var debounceInputChange = debounce(async (e) => {
}, 500);
inputTextArea.addEventListener("change", debounceInputChange);
var inputButton = document.getElementById("inputButton");
inputButton.onclick = () => debounceInputChange();
inputButton.addEventListener("click", debounceInputChange);
/**
* @license
* Licensed to the Apache Software Foundation (ASF) under one
Expand Down

0 comments on commit 39bd2ca

Please sign in to comment.