js: Don't leak to global ns in position_caret.js

This commit is contained in:
Florian Bruhin 2016-08-09 13:36:46 +02:00
parent c8a92a0851
commit b82823d76a

View File

@ -1,6 +1,6 @@
/** /**
* Copyright 2015 Artur Shaik <ashaihullin@gmail.com> * Copyright 2015 Artur Shaik <ashaihullin@gmail.com>
* Copyright 2015 Florian Bruhin (The Compiler) <mail@qutebrowser.org> * Copyright 2015-2016 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
* *
* This file is part of qutebrowser. * This file is part of qutebrowser.
* *
@ -32,79 +32,83 @@
"use strict"; "use strict";
function isElementInViewport(node) { // eslint-disable-line complexity (function() {
var i; function isElementInViewport(node) { // eslint-disable-line complexity
var boundingRect = (node.getClientRects()[0] || var i;
node.getBoundingClientRect()); var boundingRect = (node.getClientRects()[0] ||
node.getBoundingClientRect());
if (boundingRect.width <= 1 && boundingRect.height <= 1) { if (boundingRect.width <= 1 && boundingRect.height <= 1) {
var rects = node.getClientRects(); var rects = node.getClientRects();
for (i = 0; i < rects.length; i++) { for (i = 0; i < rects.length; i++) {
if (rects[i].width > rects[0].height && if (rects[i].width > rects[0].height &&
rects[i].height > rects[0].height) { rects[i].height > rects[0].height) {
boundingRect = rects[i]; boundingRect = rects[i];
}
} }
} }
if (boundingRect === undefined) {
return null;
}
if (boundingRect.top > innerHeight || boundingRect.left > innerWidth) {
return null;
}
if (boundingRect.width <= 1 || boundingRect.height <= 1) {
var children = node.children;
var visibleChildNode = false;
for (i = 0; i < children.length; ++i) {
boundingRect = (children[i].getClientRects()[0] ||
children[i].getBoundingClientRect());
if (boundingRect.width > 1 && boundingRect.height > 1) {
visibleChildNode = true;
break;
}
}
if (visibleChildNode === false) {
return null;
}
}
if (boundingRect.top + boundingRect.height < 10 ||
boundingRect.left + boundingRect.width < -10) {
return null;
}
var computedStyle = window.getComputedStyle(node, null);
if (computedStyle.visibility !== "visible" ||
computedStyle.display === "none" ||
node.hasAttribute("disabled") ||
parseInt(computedStyle.width, 10) === 0 ||
parseInt(computedStyle.height, 10) === 0) {
return null;
}
return boundingRect.top >= -20;
} }
if (boundingRect === undefined) {
return null; function positionCaret() {
} var walker = document.createTreeWalker(document.body, 4, null);
if (boundingRect.top > innerHeight || boundingRect.left > innerWidth) { var node;
return null; var textNodes = [];
} var el;
if (boundingRect.width <= 1 || boundingRect.height <= 1) { while ((node = walker.nextNode())) {
var children = node.children; if (node.nodeType === 3 && node.data.trim() !== "") {
var visibleChildNode = false; textNodes.push(node);
for (i = 0; i < children.length; ++i) { }
boundingRect = (children[i].getClientRects()[0] || }
children[i].getBoundingClientRect()); for (var i = 0; i < textNodes.length; i++) {
if (boundingRect.width > 1 && boundingRect.height > 1) { var element = textNodes[i].parentElement;
visibleChildNode = true; if (isElementInViewport(element.parentElement)) {
el = element;
break; break;
} }
} }
if (visibleChildNode === false) { if (el !== undefined) {
return null; var range = document.createRange();
range.setStart(el, 0);
range.setEnd(el, 0);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} }
} }
if (boundingRect.top + boundingRect.height < 10 ||
boundingRect.left + boundingRect.width < -10) {
return null;
}
var computedStyle = window.getComputedStyle(node, null);
if (computedStyle.visibility !== "visible" ||
computedStyle.display === "none" ||
node.hasAttribute("disabled") ||
parseInt(computedStyle.width, 10) === 0 ||
parseInt(computedStyle.height, 10) === 0) {
return null;
}
return boundingRect.top >= -20;
}
(function() { positionCaret();
var walker = document.createTreeWalker(document.body, 4, null);
var node;
var textNodes = [];
var el;
while ((node = walker.nextNode())) {
if (node.nodeType === 3 && node.data.trim() !== "") {
textNodes.push(node);
}
}
for (var i = 0; i < textNodes.length; i++) {
var element = textNodes[i].parentElement;
if (isElementInViewport(element.parentElement)) {
el = element;
break;
}
}
if (el !== undefined) {
var range = document.createRange();
range.setStart(el, 0);
range.setEnd(el, 0);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
})(); })();