Skip to content

Commit

Permalink
Improving the detection of valid/invalid elements
Browse files Browse the repository at this point in the history
  • Loading branch information
Pauan committed Sep 28, 2015
1 parent 360168d commit c232b36
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 15 deletions.
41 changes: 26 additions & 15 deletions src/data/AutoScroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,25 +274,36 @@ chrome.storage.local.get(defaults, function (options) {
}


// TODO maybe handle `contentEditable` ?
function isInvalid(elem) {
return (elem.localName === "a" && elem.href) ||
(elem.localName === "textarea") ||
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input
(elem.localName === "input" &&
!(elem.type === "button" ||
elem.type === "checkbox" ||
elem.type === "file" ||
elem.type === "hidden" ||
elem.type === "image" ||
elem.type === "radio" ||
elem.type === "reset" ||
elem.type === "submit"));
}

function isValid(elem) {
if (options["scrollOnLinks"]) {
return false
// <input> tags can't have children, so the only time it will ever occur is as event.target
} else if (elem.localName === "input") {
return !(elem.type === "button" ||
elem.type === "checkbox" ||
elem.type === "file" ||
elem.type === "hidden" ||
elem.type === "image" ||
elem.type === "radio" ||
elem.type === "reset" ||
elem.type === "submit")
return true

} else {
while (true) {
if (elem === document || elem === document.body || elem === htmlNode) {
return false
} else if (elem.localName === "a" && elem.href || elem.localName === "textarea") {
if (elem === document ||
elem === document.body ||
elem === htmlNode) {
return true

} else if (isInvalid(elem)) {
return false

} else {
elem = elem.parentNode
}
Expand Down Expand Up @@ -446,7 +457,7 @@ chrome.storage.local.get(defaults, function (options) {
if (((e.button === 1 && options.middleClick) ||
(e.button === 0 && (e.ctrlKey || e.metaKey) && options.ctrlClick)) &&
e.clientX < htmlNode.clientWidth &&
!isInvalid(e.target)) {
isValid(e.target)) {

var elem = findScroll(e.target)

Expand Down
34 changes: 34 additions & 0 deletions test/Valid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<style>
body {
margin: 0px;
}

#page {
height: 9999px;
padding: 30px;
}
</style>
</head>
<body>
<div id="page">
<p>
<a href="http://example.com">Example</a>
</p>
<p>
<a href="http://example.com"><button>Example</button></a>
</p>
<p>
<a href="http://example.com"><input type="button" value="Example"></input></a>
</p>

<p>
<input type="button" value="Example"></input>
</p>
</div>
</body>
</html>

0 comments on commit c232b36

Please sign in to comment.