Visual Selector - xPath handling misc fixes (#1976)

pull/1748/merge
dgtlmoon 1 year ago committed by GitHub
parent 5e2049c538
commit f7f98945a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -351,7 +351,7 @@ class ValidateCSSJSONXPATHInput(object):
raise ValidationError("XPath not permitted in this field!") raise ValidationError("XPath not permitted in this field!")
from lxml import etree, html from lxml import etree, html
tree = html.fromstring("<html></html>") tree = html.fromstring("<html></html>")
line = line.replace('xpath1:', '') line = re.sub(r'^xpath1:', '', line)
try: try:
tree.xpath(line.strip()) tree.xpath(line.strip())

@ -170,9 +170,12 @@ if (include_filters.length) {
try { try {
// is it xpath? // is it xpath?
if (f.startsWith('/') || f.startsWith('xpath:')) { if (f.startsWith('/') || f.startsWith('xpath')) {
q = document.evaluate(f.replace('xpath:', ''), document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; var qry_f = f.replace(/xpath(:|\d:)/, '')
console.log("[xpath] Scanning for included filter " + qry_f)
q = document.evaluate(qry_f, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
} else { } else {
console.log("[css] Scanning for included filter " + f)
q = document.querySelector(f); q = document.querySelector(f);
} }
} catch (e) { } catch (e) {
@ -182,8 +185,18 @@ if (include_filters.length) {
} }
if (q) { if (q) {
// Try to resolve //something/text() back to its /something so we can atleast get the bounding box
try {
if (typeof q.nodeName == 'string' && q.nodeName === '#text') {
q = q.parentElement
}
} catch (e) {
console.log(e)
console.log("xpath_element_scraper: #text resolver")
}
// #1231 - IN the case XPath attribute filter is applied, we will have to traverse up and find the element. // #1231 - IN the case XPath attribute filter is applied, we will have to traverse up and find the element.
if (q.hasOwnProperty('getBoundingClientRect')) { if (typeof q.getBoundingClientRect == 'function') {
bbox = q.getBoundingClientRect(); bbox = q.getBoundingClientRect();
console.log("xpath_element_scraper: Got filter element, scroll from top was " + scroll_y) console.log("xpath_element_scraper: Got filter element, scroll from top was " + scroll_y)
} else { } else {
@ -192,7 +205,8 @@ if (include_filters.length) {
bbox = q.ownerElement.getBoundingClientRect(); bbox = q.ownerElement.getBoundingClientRect();
console.log("xpath_element_scraper: Got filter by ownerElement element, scroll from top was " + scroll_y) console.log("xpath_element_scraper: Got filter by ownerElement element, scroll from top was " + scroll_y)
} catch (e) { } catch (e) {
console.log("xpath_element_scraper: error looking up ownerElement") console.log(e)
console.log("xpath_element_scraper: error looking up q.ownerElement")
} }
} }
} }

@ -149,7 +149,7 @@ $(document).ready(function () {
// @todo In the future paint all that match // @todo In the future paint all that match
for (const c of current_default_xpath) { for (const c of current_default_xpath) {
for (var i = selector_data['size_pos'].length; i !== 0; i--) { for (var i = selector_data['size_pos'].length; i !== 0; i--) {
if (selector_data['size_pos'][i - 1].xpath === c) { if (selector_data['size_pos'][i - 1].xpath.trim() === c.trim()) {
console.log("highlighting " + c); console.log("highlighting " + c);
current_selected_i = i - 1; current_selected_i = i - 1;
highlight_current_selected_i(); highlight_current_selected_i();

@ -227,9 +227,6 @@ def test_regex_error_handling(client, live_server):
follow_redirects=True follow_redirects=True
) )
with open('/tmp/fuck.html', 'wb') as f:
f.write(res.data)
assert b'is not a valid regular expression.' in res.data assert b'is not a valid regular expression.' in res.data
res = client.get(url_for("form_delete", uuid="all"), follow_redirects=True) res = client.get(url_for("form_delete", uuid="all"), follow_redirects=True)

Loading…
Cancel
Save