Skip to content

Commit 2650556

Browse files
committed
Firefox: fixing sendKeys for contentEditable elements, it should append to the existing text.
1 parent 642fbd2 commit 2650556

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

java/client/test/org/openqa/selenium/TypingTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static org.junit.Assume.assumeFalse;
2727
import static org.openqa.selenium.WaitingConditions.elementValueToEqual;
2828
import static org.openqa.selenium.testing.Ignore.Driver.ALL;
29+
import static org.openqa.selenium.testing.Ignore.Driver.CHROME;
2930
import static org.openqa.selenium.testing.Ignore.Driver.HTMLUNIT;
3031
import static org.openqa.selenium.testing.Ignore.Driver.IE;
3132
import static org.openqa.selenium.testing.Ignore.Driver.MARIONETTE;
@@ -720,7 +721,7 @@ public void testShouldBeAbleToTypeIntoEmptyContentEditableElement() {
720721
assertThat(editable.getText(), equalTo("cheese"));
721722
}
722723

723-
@Ignore(value = {ALL}, reason = "Untested except in Firefox", issues = {2825})
724+
@Ignore(value = {CHROME, IE, SAFARI, HTMLUNIT, MARIONETTE})
724725
@Test
725726
public void testShouldBeAbleToTypeIntoContentEditableElementWithExistingValue() {
726727
driver.get(pages.readOnlyPage);

javascript/firefox-driver/js/wrappedElement.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,34 @@ WebElement.sendKeysToElement = function(respond, parameters) {
145145

146146
// We may need a beat for firefox to hand over focus.
147147
this.jsTimer.setTimeout(function() {
148-
// Unless the element already had focus, set the cursor location to the end of the line
149-
// TODO(simon): This seems a little arbitrary.
150148
if (!alreadyFocused && bot.dom.isEditable(element)) {
151-
var length = element.value ? element.value.length : goog.dom.getTextContent(element).length;
149+
var length = element.value ? element.value.length : goog.dom.getTextContent(element).length;
150+
151+
if (bot.dom.isContentEditable(element)) {
152+
var doc = element.ownerDocument || element.document;
153+
var rng = doc.createRange();
154+
var walker = doc.createTreeWalker(element, 4/*NodeFilter.SHOW_TEXT*/, null, null);
155+
var start = length;
156+
var end = length;
157+
var n,pos = 0;
158+
while (n = walker.nextNode()) {
159+
pos += n.nodeValue.length;
160+
if (pos >= start) {
161+
rng.setStart(n, n.nodeValue.length + start - pos);
162+
start = Infinity;
163+
}
164+
if (pos >= end) {
165+
rng.setEnd(n, n.nodeValue.length + end - pos);
166+
break;
167+
}
168+
}
169+
var sel = doc.getSelection();
170+
sel.removeAllRanges();
171+
sel.addRange(rng);
172+
173+
} else {
152174
goog.dom.selection.setCursorPosition(element, length);
175+
}
153176
}
154177

155178
try {

0 commit comments

Comments
 (0)