Bug 769355 - Implements max attribute for <input type='date'>. r=mounir
authorRaphael Catolino <rcatolino@mozilla.com>
Fri, 21 Dec 2012 18:26:56 +0100
changeset 126218 739b8e307069d3f8f1a23e3cab2887c4e98507ac
parent 126217 78970aaa8008f602dcbb622b8c573fdc5d1036d1
child 126219 5705bb322879b3f0ca5b047ce84bb0f63e4732bb
push id2151
push userlsblakk@mozilla.com
push dateTue, 19 Feb 2013 18:06:57 +0000
treeherdermozilla-beta@4952e88741ec [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmounir
bugs769355
milestone20.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 769355 - Implements max attribute for <input type='date'>. r=mounir
content/html/content/src/nsHTMLInputElement.cpp
content/html/content/test/forms/test_max_attribute.html
--- a/content/html/content/src/nsHTMLInputElement.cpp
+++ b/content/html/content/src/nsHTMLInputElement.cpp
@@ -1330,29 +1330,28 @@ nsHTMLInputElement::GetMinAsDouble() con
   nsresult ec;
   double min = minStr.ToDouble(&ec);
   return NS_SUCCEEDED(ec) ? min : MOZ_DOUBLE_NaN();
 }
 
 double
 nsHTMLInputElement::GetMaxAsDouble() const
 {
-  // Should only be used for <input type='number'> for the moment.
-  MOZ_ASSERT(mType == NS_FORM_INPUT_NUMBER);
+  // Should only be used for <input type='number'/'date'> for the moment.
+  MOZ_ASSERT(mType == NS_FORM_INPUT_NUMBER || mType == NS_FORM_INPUT_DATE);
 
   if (!HasAttr(kNameSpaceID_None, nsGkAtoms::max)) {
     return MOZ_DOUBLE_NaN();
   }
 
   nsAutoString maxStr;
   GetAttr(kNameSpaceID_None, nsGkAtoms::max, maxStr);
 
-  nsresult ec;
-  double max = maxStr.ToDouble(&ec);
-  return NS_SUCCEEDED(ec) ? max : MOZ_DOUBLE_NaN();
+  double max;
+  return ConvertStringToNumber(maxStr, max) ? max : MOZ_DOUBLE_NaN();
 }
 
 double
 nsHTMLInputElement::GetStepBase() const
 {
   double stepBase = GetMinAsDouble();
 
   // If @min is not a double, we should use defaultValue.
@@ -4415,18 +4414,17 @@ nsHTMLInputElement::HasPatternMismatch()
   nsIDocument* doc = OwnerDoc();
 
   return !nsContentUtils::IsPatternMatching(value, pattern, doc);
 }
 
 bool
 nsHTMLInputElement::IsRangeOverflow() const
 {
-  // Ignore <input type=date> until bug 769355 is fixed.
-  if (!DoesMinMaxApply() || mType == NS_FORM_INPUT_DATE) {
+  if (!DoesMinMaxApply()) {
     return false;
   }
 
   double max = GetMaxAsDouble();
   if (MOZ_DOUBLE_IS_NaN(max)) {
     return false;
   }
 
@@ -4694,21 +4692,28 @@ nsHTMLInputElement::GetValidationMessage
       }
       aValidationMessage = message;
       break;
     }
     case VALIDITY_STATE_RANGE_OVERFLOW:
     {
       nsXPIDLString message;
 
-      double max = GetMaxAsDouble();
-      MOZ_ASSERT(!MOZ_DOUBLE_IS_NaN(max));
-
       nsAutoString maxStr;
-      maxStr.AppendFloat(max);
+      if (mType == NS_FORM_INPUT_NUMBER) {
+        //We want to show the value as parsed when it's a number
+        double max = GetMaxAsDouble();
+        MOZ_ASSERT(!MOZ_DOUBLE_IS_NaN(max));
+
+        maxStr.AppendFloat(max);
+      } else if (mType == NS_FORM_INPUT_DATE) {
+        GetAttr(kNameSpaceID_None, nsGkAtoms::max, maxStr);
+      } else {
+        NS_NOTREACHED("Unexpected input type");
+      }
 
       const PRUnichar* params[] = { maxStr.get() };
       rv = nsContentUtils::FormatLocalizedString(nsContentUtils::eDOM_PROPERTIES,
                                                  "FormValidationRangeOverflow",
                                                  params, message);
       aValidationMessage = message;
       break;
     }
@@ -5191,17 +5196,17 @@ nsHTMLInputElement::UpdateValidityUIBits
   }
 }
 
 void
 nsHTMLInputElement::UpdateHasRange()
 {
   mHasRange = false;
 
-  if (mType != NS_FORM_INPUT_NUMBER) {
+  if (mType != NS_FORM_INPUT_NUMBER && mType != NS_FORM_INPUT_DATE) {
     return;
   }
 
   // <input type=number> has a range if min or max is a valid double.
 
   double min = GetMinAsDouble();
   if (!MOZ_DOUBLE_IS_NaN(min)) {
     mHasRange = true;
--- a/content/html/content/test/forms/test_max_attribute.html
+++ b/content/html/content/test/forms/test_max_attribute.html
@@ -23,17 +23,17 @@ var types = [
   [ 'hidden',         false ],
   [ 'text',           false ],
   [ 'search',         false ],
   [ 'tel',            false ],
   [ 'url',            false ],
   [ 'email',          false ],
   [ 'password',       false ],
   [ 'datetime',       true,  true ],
-  [ 'date',           true,  true ],
+  [ 'date',           true ],
   [ 'month',          true,  true ],
   [ 'week',           true,  true ],
   [ 'time',           true,  true ],
   [ 'datetime-local', true,  true ],
   [ 'number',         true ],
   [ 'range',          true,  true ],
   [ 'color',          false, true ],
   [ 'checkbox',       false ],
@@ -85,17 +85,22 @@ for (var data of types) {
 
   if (data[2]) {
     todo_is(input.type, data[0], data[0] + " isn't implemented yet");
     continue;
   }
 
   checkValidity(input, true, apply, false);
 
-  input.max = '2';
+  if (input.type == 'date') {
+    input.max = '2012-06-27';
+  } else {
+    input.max = '2';
+  }
+
   checkValidity(input, true, apply, apply);
 
   if (input.type == 'url') {
     input.value = 'http://mozilla.org';
     checkValidity(input, true, apply, apply);
   } else if (input.type == 'email') {
     input.value = 'foo@bar.com';
     checkValidity(input, true, apply, apply);
@@ -113,16 +118,46 @@ for (var data of types) {
                    0666, 0);
     outStream.write("foo", 3);
     outStream.close();
 
     input.value = file.path;
     checkValidity(input, true, apply, apply);
 
     file.remove(false);
+  } else if (input.type == 'date') {
+    input.value = '2012-06-26';
+    checkValidity(input, true, apply, apply);
+
+    input.value = '2012-06-27';
+    checkValidity(input, true, apply, apply);
+
+    input.value = 'foo';
+    checkValidity(input, true, apply, apply);
+
+    input.value = '2012-06-28';
+    checkValidity(input, false, apply, apply);
+
+    input.max = '2012-06-30';
+    checkValidity(input, true, apply, apply);
+
+    input.value = '2012-07-05';
+    checkValidity(input, false, apply, apply);
+
+    input.value = '1000-01-01';
+    checkValidity(input, true, apply, apply);
+
+    input.value = '20120-01-01';
+    checkValidity(input, false, apply, apply);
+
+    input.max = '0050-01-01';
+    checkValidity(input, false, apply, apply);
+
+    input.value = '0049-01-01';
+    checkValidity(input, true, apply, apply);
   } else {
     input.value = '1';
     checkValidity(input, true, apply, apply);
 
     input.value = '2';
     checkValidity(input, true, apply, apply);
 
     input.value = 'foo';