Fix a couple eslint errors
Restructure serialize_elem into a bunch of smaller functions
This commit is contained in:
parent
2898c416aa
commit
7f9d4888fd
@ -42,3 +42,4 @@ rules:
|
|||||||
function-paren-newline: "off"
|
function-paren-newline: "off"
|
||||||
multiline-comment-style: "off"
|
multiline-comment-style: "off"
|
||||||
no-bitwise: "off"
|
no-bitwise: "off"
|
||||||
|
no-ternary: "off"
|
||||||
|
@ -40,6 +40,41 @@ window._qutebrowser.webelem = (function() {
|
|||||||
const funcs = {};
|
const funcs = {};
|
||||||
const elements = [];
|
const elements = [];
|
||||||
|
|
||||||
|
function get_frame_offset(frame) {
|
||||||
|
if (frame === null) {
|
||||||
|
// Dummy object with zero offset
|
||||||
|
return {
|
||||||
|
"top": 0,
|
||||||
|
"right": 0,
|
||||||
|
"bottom": 0,
|
||||||
|
"left": 0,
|
||||||
|
"height": 0,
|
||||||
|
"width": 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return frame.frameElement.getBoundingClientRect();
|
||||||
|
}
|
||||||
|
|
||||||
|
function get_caret_position(elem, frame) {
|
||||||
|
// With older Chromium versions (and QtWebKit), InvalidStateError will
|
||||||
|
// be thrown if elem doesn't have selectionStart.
|
||||||
|
// With newer Chromium versions (>= Qt 5.10), we get null.
|
||||||
|
try {
|
||||||
|
return elem.selectionStart;
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof (frame
|
||||||
|
? frame.DOMException
|
||||||
|
: DOMException) &&
|
||||||
|
err.name === "InvalidStateError") {
|
||||||
|
// nothing to do, caret_position is already null
|
||||||
|
} else {
|
||||||
|
// not the droid we're looking for
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
function serialize_elem(elem, frame = null) {
|
function serialize_elem(elem, frame = null) {
|
||||||
if (!elem) {
|
if (!elem) {
|
||||||
return null;
|
return null;
|
||||||
@ -48,23 +83,7 @@ window._qutebrowser.webelem = (function() {
|
|||||||
const id = elements.length;
|
const id = elements.length;
|
||||||
elements[id] = elem;
|
elements[id] = elem;
|
||||||
|
|
||||||
// With older Chromium versions (and QtWebKit), InvalidStateError will
|
const caret_position = get_caret_position(elem, frame);
|
||||||
// be thrown if elem doesn't have selectionStart.
|
|
||||||
// With newer Chromium versions (>= Qt 5.10), we get null.
|
|
||||||
let caret_position = null;
|
|
||||||
try {
|
|
||||||
caret_position = elem.selectionStart;
|
|
||||||
} catch (err) {
|
|
||||||
if ((err instanceof DOMException ||
|
|
||||||
(frame !== null &&
|
|
||||||
err instanceof frame.DOMException)) &&
|
|
||||||
err.name === "InvalidStateError") {
|
|
||||||
// nothing to do, caret_position is already null
|
|
||||||
} else {
|
|
||||||
// not the droid we're looking for
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const out = {
|
const out = {
|
||||||
"id": id,
|
"id": id,
|
||||||
@ -73,7 +92,6 @@ window._qutebrowser.webelem = (function() {
|
|||||||
"rects": [], // Gets filled up later
|
"rects": [], // Gets filled up later
|
||||||
"caret_position": caret_position,
|
"caret_position": caret_position,
|
||||||
};
|
};
|
||||||
|
|
||||||
// https://github.com/qutebrowser/qutebrowser/issues/2569
|
// https://github.com/qutebrowser/qutebrowser/issues/2569
|
||||||
if (typeof elem.tagName === "string") {
|
if (typeof elem.tagName === "string") {
|
||||||
out.tag_name = elem.tagName;
|
out.tag_name = elem.tagName;
|
||||||
@ -104,22 +122,7 @@ window._qutebrowser.webelem = (function() {
|
|||||||
out.attributes = attributes;
|
out.attributes = attributes;
|
||||||
|
|
||||||
const client_rects = elem.getClientRects();
|
const client_rects = elem.getClientRects();
|
||||||
|
const frame_offset_rect = get_frame_offset(frame);
|
||||||
// Get location of frame if it exists and add it to element
|
|
||||||
let frame_offset_rect = null;
|
|
||||||
if (frame === null) {
|
|
||||||
// Dummy object with zero offset
|
|
||||||
frame_offset_rect = {
|
|
||||||
"top": 0,
|
|
||||||
"right": 0,
|
|
||||||
"bottom": 0,
|
|
||||||
"left": 0,
|
|
||||||
"height": 0,
|
|
||||||
"width": 0,
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
frame_offset_rect = frame.frameElement.getBoundingClientRect();
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let k = 0; k < client_rects.length; ++k) {
|
for (let k = 0; k < client_rects.length; ++k) {
|
||||||
const rect = client_rects[k];
|
const rect = client_rects[k];
|
||||||
@ -184,7 +187,7 @@ window._qutebrowser.webelem = (function() {
|
|||||||
// cross domain errors, else false.
|
// cross domain errors, else false.
|
||||||
function iframe_same_domain(frame) {
|
function iframe_same_domain(frame) {
|
||||||
try {
|
try {
|
||||||
frame.document;
|
frame.document; // eslint-disable-line no-unused-expressions
|
||||||
return true;
|
return true;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
Reference in New Issue
Block a user