From 6c076e3c12f387726316db74190dac97cf886612 Mon Sep 17 00:00:00 2001 From: Tapio Aali Date: Tue, 7 Jan 2014 13:31:51 +0200 Subject: [PATCH] Workaround for input prompt used as value on ESC in FF (#8051) Fixed situation in which input prompt appeared as the value of an empty TextField in FF if ESC was pressed. Change-Id: I41c8ec4845b43b483165c10f78443f806d45a623 --- .../src/com/vaadin/client/ui/VTextField.java | 46 +++++++++++++++++-- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/client/src/com/vaadin/client/ui/VTextField.java b/client/src/com/vaadin/client/ui/VTextField.java index da9445c811..9360a6e172 100644 --- a/client/src/com/vaadin/client/ui/VTextField.java +++ b/client/src/com/vaadin/client/ui/VTextField.java @@ -80,6 +80,9 @@ public class VTextField extends TextBoxBase implements Field, ChangeHandler, private boolean prompting = false; private int lastCursorPos = -1; + // used while checking if FF has set input prompt as value + private boolean possibleInputError = false; + public VTextField() { this(DOM.createInputText()); } @@ -88,9 +91,7 @@ public class VTextField extends TextBoxBase implements Field, ChangeHandler, super(node); setStyleName(CLASSNAME); addChangeHandler(this); - if (BrowserInfo.get().isIE()) { - // IE does not send change events when pressing enter in a text - // input so we handle it using a key listener instead + if (BrowserInfo.get().isIE() || BrowserInfo.get().isFirefox()) { addKeyDownHandler(this); } addFocusHandler(this); @@ -260,6 +261,9 @@ public class VTextField extends TextBoxBase implements Field, ChangeHandler, if (focusedTextField == this) { focusedTextField = null; } + if (BrowserInfo.get().isFirefox()) { + removeOnInputListener(getElement()); + } } @Override @@ -268,6 +272,11 @@ public class VTextField extends TextBoxBase implements Field, ChangeHandler, if (listenTextChangeEvents) { detachCutEventListener(getElement()); } + if (BrowserInfo.get().isFirefox()) { + // Workaround for FF setting input prompt as the value if esc is + // pressed while the field is focused and empty (#8051). + addOnInputListener(getElement()); + } } /** For internal use only. May be removed or replaced in the future. */ @@ -433,8 +442,17 @@ public class VTextField extends TextBoxBase implements Field, ChangeHandler, @Override public void onKeyDown(KeyDownEvent event) { - if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) { + if (BrowserInfo.get().isIE() + && event.getNativeKeyCode() == KeyCodes.KEY_ENTER) { + // IE does not send change events when pressing enter in a text + // input so we handle it using a key listener instead valueChange(false); + } else if (BrowserInfo.get().isFirefox() + && event.getNativeKeyCode() == KeyCodes.KEY_ESCAPE + && getText().equals("")) { + // check after onInput event if inputPrompt has appeared as the + // value of the field + possibleInputError = true; } } @@ -450,4 +468,24 @@ public class VTextField extends TextBoxBase implements Field, ChangeHandler, String wrap = getElement().getAttribute("wrap"); return !"off".equals(wrap); } + + private native void addOnInputListener(Element el) + /*-{ + var self = this; + el.oninput = $entry(function() { + self.@com.vaadin.client.ui.VTextField::checkForInputError()(); + }); + }-*/; + + private native void removeOnInputListener(Element el) + /*-{ + el.oninput = null; + }-*/; + + private void checkForInputError() { + if (possibleInputError && getText().equals(inputPrompt)) { + setText(""); + } + possibleInputError = false; + } } -- 2.39.5