Bug 613019 - Track whether the last input/textarea change was done interactively, and enable the commented-out maxLength tracking code. r=mrbkap

--HG--
extra : rebase_source : 59c0986d6184f09f79fcda848de9d822949aa47b
This commit is contained in:
Thomas Wisniewski 2016-08-06 11:59:08 -04:00
parent 0cb80d4c47
commit 30ab766837
8 changed files with 88 additions and 18 deletions

View File

@ -1145,6 +1145,7 @@ HTMLInputElement::HTMLInputElement(already_AddRefed<mozilla::dom::NodeInfo>& aNo
, mAutocompleteAttrState(nsContentUtils::eAutocompleteAttrState_Unknown)
, mDisabledChanged(false)
, mValueChanged(false)
, mLastValueChangeWasInteractive(false)
, mCheckedChanged(false)
, mChecked(false)
, mHandlingSelectEvent(false)
@ -1326,6 +1327,7 @@ HTMLInputElement::Clone(mozilla::dom::NodeInfo* aNodeInfo, nsINode** aResult) co
break;
}
it->mLastValueChangeWasInteractive = mLastValueChangeWasInteractive;
it.forget(aResult);
return NS_OK;
}
@ -3071,7 +3073,8 @@ HTMLInputElement::SetValueInternal(const nsAString& aValue, uint32_t aFlags)
}
}
if (!mParserCreating) {
OnValueChanged(true);
OnValueChanged(/* aNotify = */ true,
/* aWasInteractiveUserChange = */ false);
}
// else DoneCreatingElement calls us again once mParserCreating is false
}
@ -6065,6 +6068,7 @@ HTMLInputElement::Reset()
// We should be able to reset all dirty flags regardless of the type.
SetCheckedChanged(false);
SetValueChanged(false);
mLastValueChangeWasInteractive = false;
switch (GetValueMode()) {
case VALUE_MODE_VALUE:
@ -6917,9 +6921,10 @@ HTMLInputElement::SetCustomValidity(const nsAString& aError)
bool
HTMLInputElement::IsTooLong()
{
if (!MaxLengthApplies() ||
!HasAttr(kNameSpaceID_None, nsGkAtoms::maxlength) ||
!mValueChanged) {
if (!mValueChanged ||
!mLastValueChangeWasInteractive ||
!MaxLengthApplies() ||
!HasAttr(kNameSpaceID_None, nsGkAtoms::maxlength)) {
return false;
}
@ -7192,10 +7197,7 @@ HTMLInputElement::HasBadInput() const
void
HTMLInputElement::UpdateTooLongValidityState()
{
// TODO: this code will be re-enabled with bug 613016 and bug 613019.
#if 0
SetValidityState(VALIDITY_STATE_TOO_LONG, IsTooLong());
#endif
}
void
@ -7735,8 +7737,10 @@ HTMLInputElement::InitializeKeyboardEventListeners()
}
NS_IMETHODIMP_(void)
HTMLInputElement::OnValueChanged(bool aNotify)
HTMLInputElement::OnValueChanged(bool aNotify, bool aWasInteractiveUserChange)
{
mLastValueChangeWasInteractive = aWasInteractiveUserChange;
UpdateAllValidityStates(aNotify);
if (HasDirAuto()) {

View File

@ -229,7 +229,7 @@ public:
NS_IMETHOD_(void) UpdatePlaceholderVisibility(bool aNotify) override;
NS_IMETHOD_(bool) GetPlaceholderVisibility() override;
NS_IMETHOD_(void) InitializeKeyboardEventListeners() override;
NS_IMETHOD_(void) OnValueChanged(bool aNotify) override;
NS_IMETHOD_(void) OnValueChanged(bool aNotify, bool aWasInteractiveUserChange) override;
NS_IMETHOD_(bool) HasCachedSelection() override;
void GetDisplayFileName(nsAString& aFileName) const;
@ -1449,6 +1449,7 @@ protected:
nsContentUtils::AutocompleteAttrState mAutocompleteAttrState;
bool mDisabledChanged : 1;
bool mValueChanged : 1;
bool mLastValueChangeWasInteractive : 1;
bool mCheckedChanged : 1;
bool mChecked : 1;
bool mHandlingSelectEvent : 1;

View File

@ -55,6 +55,7 @@ HTMLTextAreaElement::HTMLTextAreaElement(already_AddRefed<mozilla::dom::NodeInfo
FromParser aFromParser)
: nsGenericHTMLFormElementWithState(aNodeInfo),
mValueChanged(false),
mLastValueChangeWasInteractive(false),
mHandlingSelect(false),
mDoneAddingChildren(!aFromParser),
mInhibitStateRestoration(!!(aFromParser & FROM_PARSER_FRAGMENT)),
@ -1344,7 +1345,9 @@ HTMLTextAreaElement::SetCustomValidity(const nsAString& aError)
bool
HTMLTextAreaElement::IsTooLong()
{
if (!HasAttr(kNameSpaceID_None, nsGkAtoms::maxlength) || !mValueChanged) {
if (!mValueChanged ||
!mLastValueChangeWasInteractive ||
!HasAttr(kNameSpaceID_None, nsGkAtoms::maxlength)) {
return false;
}
@ -1375,10 +1378,7 @@ HTMLTextAreaElement::IsValueMissing() const
void
HTMLTextAreaElement::UpdateTooLongValidityState()
{
// TODO: this code will be re-enabled with bug 613016 and bug 613019.
#if 0
SetValidityState(VALIDITY_STATE_TOO_LONG, IsTooLong());
#endif
}
void
@ -1525,8 +1525,10 @@ HTMLTextAreaElement::InitializeKeyboardEventListeners()
}
NS_IMETHODIMP_(void)
HTMLTextAreaElement::OnValueChanged(bool aNotify)
HTMLTextAreaElement::OnValueChanged(bool aNotify, bool aWasInteractiveUserChange)
{
mLastValueChangeWasInteractive = aWasInteractiveUserChange;
// Update the validity state
bool validBefore = IsValid();
UpdateTooLongValidityState();

View File

@ -106,7 +106,7 @@ public:
NS_IMETHOD_(void) UpdatePlaceholderVisibility(bool aNotify) override;
NS_IMETHOD_(bool) GetPlaceholderVisibility() override;
NS_IMETHOD_(void) InitializeKeyboardEventListeners() override;
NS_IMETHOD_(void) OnValueChanged(bool aNotify) override;
NS_IMETHOD_(void) OnValueChanged(bool aNotify, bool aWasInteractiveUserChange) override;
NS_IMETHOD_(bool) HasCachedSelection() override;
// nsIContent
@ -293,6 +293,8 @@ protected:
nsCOMPtr<nsIControllers> mControllers;
/** Whether or not the value has changed since its default value was given. */
bool mValueChanged;
/** Whether or not the last change to the value was made interactively by the user. */
bool mLastValueChangeWasInteractive;
/** Whether or not we are already handling select event. */
bool mHandlingSelect;
/** Whether or not we are done adding children (always true if not

View File

@ -169,7 +169,7 @@ public:
/**
* Callback called whenever the value is changed.
*/
NS_IMETHOD_(void) OnValueChanged(bool aNotify) = 0;
NS_IMETHOD_(void) OnValueChanged(bool aNotify, bool aWasInteractiveUserChange) = 0;
static const int32_t DEFAULT_COLS = 20;
static const int32_t DEFAULT_ROWS = 1;

View File

@ -978,7 +978,8 @@ nsTextInputListener::EditAction()
}
if (!mSettingValue) {
mTxtCtrlElement->OnValueChanged(true);
mTxtCtrlElement->OnValueChanged(/* aNotify = */ true,
/* aWasInteractiveUserChange = */ true);
}
return NS_OK;
@ -2182,7 +2183,8 @@ nsTextEditorState::SetValue(const nsAString& aValue, uint32_t aFlags)
// can assume that it's safe to notify.
ValueWasChanged(!!mRootNode);
mTextCtrlElement->OnValueChanged(!!mRootNode);
mTextCtrlElement->OnValueChanged(/* aNotify = */ !!mRootNode,
/* aWasInteractiveUserChange = */ false);
return true;
}

View File

@ -368,6 +368,7 @@ skip-if = buildapp == 'mulet' # TC: Bug 1144079 - Re-enable Mulet mochitests and
skip-if = buildapp == 'mulet' || buildapp == 'b2g' || toolkit == 'android' # b2g(form control not selected/checked with synthesizeMouse, also fails on Android) b2g-debug(form control not selected/checked with synthesizeMouse, also fails on Android) b2g-desktop(form control not selected/checked with synthesizeMouse, also fails on Android)
[test_bug613113.html]
skip-if = buildapp == 'b2g' # b2g(bug 587671, need an invalidformsubmit observer) b2g-debug(bug 587671, need an invalidformsubmit observer) b2g-desktop(bug 587671, need an invalidformsubmit observer)
[test_bug613019.html]
[test_bug613722.html]
[test_bug613979.html]
[test_bug615595.html]

View File

@ -0,0 +1,58 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=613019
-->
<head>
<title>Test for Bug 613019</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=613019">Mozilla Bug 613019</a>
<div id="content">
<input type="text" maxlength="2" style="width:200px" value="Test">
<textarea maxlength="2" style="width:200px">Test</textarea>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
/** Test for Bug 613019 **/
function testInteractivityOfValidityStates(elem) {
// verify that user interactivity is necessary for validity state to apply.
is(elem.value, "Test", "Element has incorrect starting value.");
is(elem.validity.tooLong, false, "Element should not be tooLong.");
elem.focus();
synthesizeKey("VK_BACK_SPACE", {});
is(elem.value, "Tes", "Element value was not changed correctly.");
is(elem.validity.tooLong, true, "Element should still be tooLong.");
synthesizeKey("VK_BACK_SPACE", {});
is(elem.value, "Te", "Element value was not changed correctly.");
is(elem.validity.tooLong, false, "Element should no longer be tooLong.");
elem.value = "Test";
is(elem.validity.tooLong, false,
"Element should not be tooLong after non-interactive value change.");
}
function test() {
window.getSelection().removeAllRanges();
testInteractivityOfValidityStates(document.querySelector("input[type=text]"));
testInteractivityOfValidityStates(document.querySelector("textarea"));
SimpleTest.finish();
}
window.onload = function() {
SimpleTest.waitForExplicitFinish();
setTimeout(test, 0);
};
</script>
</pre>
</body>
</html>