Skip to content

Commit

Permalink
Scroll into view only when out of view. Fixes hollingsworthd#157
Browse files Browse the repository at this point in the history
  • Loading branch information
hollingsworthd committed Dec 7, 2016
1 parent 808e337 commit 94876e9
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/com/machinepublishers/jbrowserdriver/ElementServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,24 @@ class ElementServer extends RemoteObject implements ElementRemote, WebElement,
IS_VISIBLE = builder.toString();
}

private static final String SCROLL_INTO_VIEW;
static {
StringBuilder builder = new StringBuilder();
builder.append("var me = this;");
builder.append("(function(){");
builder.append(" var rect = me.getBoundingClientRect();");
builder.append(" if(rect");
builder.append(" && (rect.top < 0");
builder.append(" || rect.left < 0");
builder.append(" || rect.bottom > window.innerHeight");
builder.append(" || rect.right > window.innerWidth");
builder.append(" || rect.bottom > document.documentElement.clientHeight");
builder.append(" || rect.right > document.documentElement.clientWidth)) {");
builder.append(" me.scrollIntoView();");
builder.append(" }");
builder.append("})();");
SCROLL_INTO_VIEW = builder.toString();
}
private static final Pattern rgb = Pattern.compile(
"rgb\\(([0-9]{1,3}), ([0-9]{1,3}), ([0-9]{1,3})\\)");
private static final Map<ElementId, ElementServer> map = new HashMap<ElementId, ElementServer>();
Expand Down Expand Up @@ -219,7 +237,7 @@ public void click() {
@Override
public Object perform() {
validate(false);
node.call("scrollIntoView");
node.eval(SCROLL_INTO_VIEW);
if (contextItem.context.get().keyboard.get().isShiftPressed()) {
node.eval(
new StringBuilder()
Expand Down Expand Up @@ -339,7 +357,7 @@ public void sendKeys(final CharSequence... keys) {
@Override
public Object perform() {
validate(true);
node.call("scrollIntoView");
node.eval(SCROLL_INTO_VIEW);
node.call("focus");
return null;
}
Expand All @@ -365,7 +383,7 @@ public void clear() {
public Object perform() {
validate(false);
contextItem.httpListener.get().resetStatusCode();
node.call("scrollIntoView");
node.eval(SCROLL_INTO_VIEW);
node.call("focus");
node.eval("this.value='';");
return null;
Expand Down Expand Up @@ -1060,7 +1078,7 @@ public Point locate() {
@Override
public Point perform() {
validate(false);
node.call("scrollIntoView");
node.eval(SCROLL_INTO_VIEW);
return null;
}
});
Expand Down

0 comments on commit 94876e9

Please sign in to comment.