Skip to content

Instantly share code, notes, and snippets.

@caisui
Created June 4, 2010 15:03
Show Gist options
  • Save caisui/425514 to your computer and use it in GitHub Desktop.
Save caisui/425514 to your computer and use it in GitHub Desktop.
findScrollable: function findScrollable(dir, horizontal) {
let pos = "scrollTop", size = "clientHeight", max = "scrollHeight", layoutSize = "offsetHeight",
overflow = "overflowX", border1 = "borderTopWidth", border2 = "borderBottomWidth";
if (horizontal)
pos = "scrollLeft", size = "clientWidth", max = "scrollWidth", layoutSize = "offsetWidth",
overflow = "overflowX", border1 = "borderLeftWidth", border2 = "borderRightWidth";
function find(elem) {
for (; elem && elem.ownerDocument; elem = elem.parentNode) {
let style = util.computedStyle(elem);
let realSize = elem[size];
// Stupid Gecko eccentricities. May fail for quirks mode documents.
if (style.overflow === "visible") // Stupid, fallible heuristic.
continue;
if (dir < 0 && elem[pos] > 0 || dir > 0 && elem[pos] + realSize < elem[max])
break;
}
return elem;
}
let win = this.focusedWindow;
if (win.getSelection().rangeCount)
var elem = find(win.getSelection().getRangeAt(0).startContainer);
if (!(elem instanceof Element)) {
let doc = Buffer.findScrollableWindow().document;
elem = (doc.compatMode === "CSS1Compat" ? doc.documentElement : doc.body) || doc.getElementsByTagName("body")[0];
}
return elem;
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment