aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authormrmr19932014-12-17 10:15:54 +0000
committermrmr19932014-12-17 10:15:54 +0000
commita199335790aec50cf3ed7cc27c5b407875c37107 (patch)
treea00e8307d50204778d52961b78fab20db17b8f31 /lib
parent09f8527915eae8067072277e2b161493ede359cd (diff)
downloadvimium-a199335790aec50cf3ed7cc27c5b407875c37107.tar.bz2
Use the DOM rather than XPath to detect clickable elements
Diffstat (limited to 'lib')
-rw-r--r--lib/dom_utils.coffee36
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee
index a0ac0bd3..26fa9b81 100644
--- a/lib/dom_utils.coffee
+++ b/lib/dom_utils.coffee
@@ -42,6 +42,42 @@ DomUtils =
document.evaluate(xpath, document.documentElement, namespaceResolver, resultType, null)
#
+ # Returns all the clickable element children of contextNode. This also can include contextNode itself.
+ #
+ getClickableElements: (contextNode = document.documentElement) ->
+ elements = Array::slice.call(contextNode?.getElementsByTagName "*")
+ elements.unshift contextNode # Check the contextNode as well.
+ clickableElements = []
+ for element in elements
+ isClickable = false
+ tagName = element.tagName.toLowerCase()
+ isClickable = (->
+ if element.hasAttribute "onclick"
+ true
+ else if element.hasAttribute "tabindex"
+ true
+ else if element.getAttribute "role" in ["button", "link"]
+ true
+ else if element.getAttribute("class")?.toLowerCase().indexOf("button") >= 0
+ true
+ else if element.getAttribute("contentEditable")?.toLowerCase() in ["", "contentEditable", "true"]
+ true
+ else if tagName == "a"
+ true
+ else if tagName == "area"
+ element.hasAttribute "href"
+ else if (tagName == "input" and DomUtils.isSelectable element) or tagName == "textarea"
+ not (element.disabled or element.hasAttribute "readonly")
+ else if (tagName == "input" and element.getAttribute("type")?.toLowerCase() != "hidden") or
+ tagName in ["button", "select"]
+ not element.disabled
+ else
+ false
+ )()
+ clickableElements.push element if isClickable
+ clickableElements
+
+ #
# Returns the first visible clientRect of an element if it exists. Otherwise it returns null.
#
getVisibleClientRect: (element) ->