summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-08-31 17:39:13 -0400
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-09-02 11:41:00 -0400
commitd296ebd593d125d131b5bf9974e19d13ca3b3b3f (patch)
treefbae7342e33dfadec1390a19624bc3db4700fb5e
parent548607ea7f45c5f9201f093c56429faf2906b5ad (diff)
Improve definition of caretRangeFromPoint
-rw-r--r--ext/fg/js/document.js38
1 files changed, 26 insertions, 12 deletions
diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js
index 1aa6f83b..a64b6c04 100644
--- a/ext/fg/js/document.js
+++ b/ext/fg/js/document.js
@@ -105,8 +105,8 @@ function docRangeFromPoint({x, y}) {
}
}
- const range = document.caretRangeFromPoint(x, y);
- if (range !== null && isPointInRange(point, range)) {
+ const range = caretRangeFromPoint(x, y);
+ if (range !== null) {
if (imposter !== null) {
docSetImposterStyle(imposterContainer.style, 'z-index', '-2147483646');
docSetImposterStyle(imposter.style, 'pointer-events', 'none');
@@ -235,15 +235,29 @@ function isPointInRect(x, y, rect) {
y >= rect.top && y < rect.bottom);
}
-if (typeof document.caretRangeFromPoint !== 'function') {
- document.caretRangeFromPoint = (x, y) => {
- const position = document.caretPositionFromPoint(x, y);
- if (position && position.offsetNode && position.offsetNode.nodeType === Node.TEXT_NODE) {
+const caretRangeFromPoint = (() => {
+ if (typeof document.caretRangeFromPoint === 'function') {
+ // Chrome, Edge
+ return (x, y) => document.caretRangeFromPoint(x, y);
+ }
+
+ if (typeof document.caretPositionFromPoint === 'function') {
+ // Firefox
+ return (x, y) => {
+ const position = document.caretPositionFromPoint(x, y);
+ const node = position.offsetNode;
+ if (node === null) {
+ return null;
+ }
+
const range = document.createRange();
- range.setStart(position.offsetNode, position.offset);
- range.setEnd(position.offsetNode, position.offset);
+ const offset = (node.nodeType === Node.TEXT_NODE ? position.offset : 0);
+ range.setStart(node, offset);
+ range.setEnd(node, offset);
return range;
- }
- return null;
- };
-}
+ };
+ }
+
+ // No support
+ return () => null;
+})();