diff options
| author | Jez Ng | 2012-01-25 23:55:40 -0500 |
|---|---|---|
| committer | Jez Ng | 2012-01-26 02:47:56 -0500 |
| commit | 42bb33d427e3d8c36c31753bd0c0a81bf330e4ce (patch) | |
| tree | dc06ef0827cbce8ffab16ad39aa3447f8bb99d06 /lib | |
| parent | c9db525f07b4ac1ff1293e39d2944e01ebf3651e (diff) | |
| download | vimium-42bb33d427e3d8c36c31753bd0c0a81bf330e4ce.tar.bz2 | |
Factor out common DOM functions.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/domUtils.js | 92 | ||||
| -rw-r--r-- | lib/utils.js | 63 |
2 files changed, 92 insertions, 63 deletions
diff --git a/lib/domUtils.js b/lib/domUtils.js new file mode 100644 index 00000000..700e1920 --- /dev/null +++ b/lib/domUtils.js @@ -0,0 +1,92 @@ +var domUtils = { + /* + * Takes an array of XPath selectors, adds the necessary namespaces (currently only XHTML), and applies them + * to the document root. The namespaceResolver in evaluateXPath should be kept in sync with the namespaces + * here. + */ + makeXPath: function(elementArray) { + var xpath = []; + for (var i in elementArray) + xpath.push("//" + elementArray[i], "//xhtml:" + elementArray[i]); + return xpath.join(" | "); + }, + + evaluateXPath: function(xpath, resultType) { + function namespaceResolver(namespace) { + return namespace == "xhtml" ? "http://www.w3.org/1999/xhtml" : null; + } + return document.evaluate(xpath, document.documentElement, namespaceResolver, resultType, null); + }, + + /** + * Returns the first visible clientRect of an element if it exists. Otherwise it returns null. + */ + getVisibleClientRect: function(element) { + // Note: this call will be expensive if we modify the DOM in between calls. + var clientRects = element.getClientRects(); + var clientRectsLength = clientRects.length; + + for (var i = 0; i < clientRectsLength; i++) { + if (clientRects[i].top < 0 || clientRects[i].top >= window.innerHeight - 4 || + clientRects[i].left < 0 || clientRects[i].left >= window.innerWidth - 4) + continue; + + if (clientRects[i].width < 3 || clientRects[i].height < 3) + continue; + + // eliminate invisible elements (see test_harnesses/visibility_test.html) + var computedStyle = window.getComputedStyle(element, null); + if (computedStyle.getPropertyValue('visibility') != 'visible' || + computedStyle.getPropertyValue('display') == 'none') + continue; + + return clientRects[i]; + } + + for (var i = 0; i < clientRectsLength; i++) { + // If the link has zero dimensions, it may be wrapping visible + // but floated elements. Check for this. + if (clientRects[i].width == 0 || clientRects[i].height == 0) { + for (var j = 0, childrenCount = element.children.length; j < childrenCount; j++) { + var computedStyle = window.getComputedStyle(element.children[j], null); + // Ignore child elements which are not floated and not absolutely positioned for parent elements with zero width/height + if (computedStyle.getPropertyValue('float') == 'none' && computedStyle.getPropertyValue('position') != 'absolute') + continue; + var childClientRect = this.getVisibleClientRect(element.children[j]); + if (childClientRect === null) + continue; + return childClientRect; + } + } + }; + return null; + }, + + /* + * Selectable means the element has a text caret; this is not the same as "focusable". + */ + isSelectable: function(element) { + var selectableTypes = ["search", "text", "password"]; + return (element.nodeName.toLowerCase() == "input" && selectableTypes.indexOf(element.type) >= 0) || + element.nodeName.toLowerCase() == "textarea"; + }, + + simulateSelect: function(element) { + element.focus(); + // When focusing a textbox, put the selection caret at the end of the textbox's contents. + element.setSelectionRange(element.value.length, element.value.length); + }, + + simulateClick: function(element, modifiers) { + modifiers = modifiers || {}; + + var event = document.createEvent("MouseEvents"); + event.initMouseEvent("click", true, true, window, 1, 0, 0, 0, 0, modifiers.ctrlKey, false, false, + modifiers.metaKey, 0, null); + + // Debugging note: Firefox will not execute the element's default action if we dispatch this click event, + // but Webkit will. Dispatching a click on an input box does not seem to focus it; we do that separately + element.dispatchEvent(event); + }, + +}; diff --git a/lib/utils.js b/lib/utils.js index 6526fe89..df325138 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -12,69 +12,6 @@ var utils = { return func.apply(obj, argArray); }, - /* - * Takes an array of XPath selectors, adds the necessary namespaces (currently only XHTML), and applies them - * to the document root. The namespaceResolver in evaluateXPath should be kept in sync with the namespaces - * here. - */ - makeXPath: function(elementArray) { - var xpath = []; - for (var i in elementArray) - xpath.push("//" + elementArray[i], "//xhtml:" + elementArray[i]); - return xpath.join(" | "); - }, - - evaluateXPath: function(xpath, resultType) { - function namespaceResolver(namespace) { - return namespace == "xhtml" ? "http://www.w3.org/1999/xhtml" : null; - } - return document.evaluate(xpath, document.documentElement, namespaceResolver, resultType, null); - }, - - /** - * Returns the first visible clientRect of an element if it exists. Otherwise it returns null. - */ - getVisibleClientRect: function(element) { - // Note: this call will be expensive if we modify the DOM in between calls. - var clientRects = element.getClientRects(); - var clientRectsLength = clientRects.length; - - for (var i = 0; i < clientRectsLength; i++) { - if (clientRects[i].top < 0 || clientRects[i].top >= window.innerHeight - 4 || - clientRects[i].left < 0 || clientRects[i].left >= window.innerWidth - 4) - continue; - - if (clientRects[i].width < 3 || clientRects[i].height < 3) - continue; - - // eliminate invisible elements (see test_harnesses/visibility_test.html) - var computedStyle = window.getComputedStyle(element, null); - if (computedStyle.getPropertyValue('visibility') != 'visible' || - computedStyle.getPropertyValue('display') == 'none') - continue; - - return clientRects[i]; - } - - for (var i = 0; i < clientRectsLength; i++) { - // If the link has zero dimensions, it may be wrapping visible - // but floated elements. Check for this. - if (clientRects[i].width == 0 || clientRects[i].height == 0) { - for (var j = 0, childrenCount = element.children.length; j < childrenCount; j++) { - var computedStyle = window.getComputedStyle(element.children[j], null); - // Ignore child elements which are not floated and not absolutely positioned for parent elements with zero width/height - if (computedStyle.getPropertyValue('float') == 'none' && computedStyle.getPropertyValue('position') != 'absolute') - continue; - var childClientRect = this.getVisibleClientRect(element.children[j]); - if (childClientRect === null) - continue; - return childClientRect; - } - } - }; - return null; - }, - /** * Creates a search URL from the given :query. */ |
