Bug 742206 part 4. Start using the new Date stuff for HTMLInputElement.valueAsDate. r=smaug
authorBoris Zbarsky <bzbarsky@mit.edu>
Thu, 02 May 2013 14:38:20 -0400
changeset 141591 2f0d5fc274edbc5a8d3f95af11398a58267bf216
parent 141590 3a8c503744ea5f2a14893e4ea385812c7f28c90b
child 141592 e4cc85a0fe1e16db72409eabf5e6b6cf03b0d6c7
push id2579
push userakeybl@mozilla.com
push dateMon, 24 Jun 2013 18:52:47 +0000
treeherdermozilla-beta@b69b7de8a05a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs742206
milestone23.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 742206 part 4. Start using the new Date stuff for HTMLInputElement.valueAsDate. r=smaug
content/html/content/src/HTMLInputElement.cpp
content/html/content/src/HTMLInputElement.h
dom/interfaces/html/nsIDOMHTMLInputElement.idl
dom/webidl/HTMLInputElement.webidl
--- a/content/html/content/src/HTMLInputElement.cpp
+++ b/content/html/content/src/HTMLInputElement.cpp
@@ -92,17 +92,16 @@
 #include "mozilla/Preferences.h"
 #include "mozilla/MathAlgorithms.h"
 
 #include "nsIIDNService.h"
 
 #include <limits>
 
 // input type=date
-#include "jsapi.h"
 #include "js/Date.h"
 
 NS_IMPL_NS_NEW_HTML_ELEMENT_CHECK_PARSER(Input)
 
 // XXX align=left, hspace, vspace, border? other nav4 attrs
 
 static NS_DEFINE_CID(kXULControllersCID,  NS_XULCONTROLLERS_CID);
 
@@ -1395,122 +1394,68 @@ HTMLInputElement::ConvertNumberToString(
         return true;
       }
     default:
       MOZ_ASSERT(false, "Unrecognized input type");
       return false;
   }
 }
 
-JS::Value
-HTMLInputElement::GetValueAsDate(JSContext* aCx, ErrorResult& aRv)
+
+Nullable<Date>
+HTMLInputElement::GetValueAsDate(ErrorResult& aRv)
 {
   if (mType != NS_FORM_INPUT_DATE && mType != NS_FORM_INPUT_TIME) {
-    return JS::NullValue();
+    return Nullable<Date>();
   }
 
   switch (mType) {
     case NS_FORM_INPUT_DATE:
     {
       uint32_t year, month, day;
       nsAutoString value;
       GetValueInternal(value);
       if (!GetValueAsDate(value, &year, &month, &day)) {
-        return JS::NullValue();
-      }
-
-      JSObject* date = JS_NewDateObjectMsec(aCx, 0);
-      if (!date) {
-        JS_ClearPendingException(aCx);
-        return JS::NullValue();
+        return Nullable<Date>();
       }
 
-      JS::Value rval;
-      JS::Value fullYear[3];
-      fullYear[0].setInt32(year);
-      fullYear[1].setInt32(month - 1);
-      fullYear[2].setInt32(day);
-      if (!JS::Call(aCx, date, "setUTCFullYear", 3, fullYear, &rval)) {
-        JS_ClearPendingException(aCx);
-        return JS::NullValue();
-      }
-
-      return JS::ObjectOrNullValue(date);
+      return Nullable<Date>(Date(JS::MakeDate(year, month - 1, day)));
     }
     case NS_FORM_INPUT_TIME:
     {
       uint32_t millisecond;
       nsAutoString value;
       GetValueInternal(value);
       if (!ParseTime(value, &millisecond)) {
-        return JS::NullValue();
+        return Nullable<Date>();
       }
 
-      JSObject* date = JS_NewDateObjectMsec(aCx, millisecond);
-      if (!date) {
-        JS_ClearPendingException(aCx);
-        return JS::NullValue();
-      }
-
-      return JS::ObjectValue(*date);
+      return Nullable<Date>(Date(millisecond));
     }
   }
 
   MOZ_ASSERT(false, "Unrecognized input type");
   aRv.Throw(NS_ERROR_UNEXPECTED);
-  return JS::NullValue();
-}
-
-NS_IMETHODIMP
-HTMLInputElement::GetValueAsDate(JSContext* aCx, JS::Value* aDate)
-{
-  ErrorResult rv;
-  *aDate = GetValueAsDate(aCx, rv);
-  return rv.ErrorCode();
+  return Nullable<Date>();
 }
 
 void
-HTMLInputElement::SetValueAsDate(JSContext* aCx, JS::Value aDate, ErrorResult& aRv)
+HTMLInputElement::SetValueAsDate(Nullable<Date> aDate, ErrorResult& aRv)
 {
   if (mType != NS_FORM_INPUT_DATE && mType != NS_FORM_INPUT_TIME) {
     aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
     return;
   }
 
-  if (aDate.isNullOrUndefined()) {
+  if (aDate.IsNull() || aDate.Value().IsUndefined()) {
     aRv = SetValue(EmptyString());
     return;
   }
 
-  // TODO: return TypeError when HTMLInputElement is converted to WebIDL, see
-  // bug 826302.
-  if (!aDate.isObject() || !JS_ObjectIsDate(aCx, &aDate.toObject())) {
-    SetValue(EmptyString());
-    aRv.Throw(NS_ERROR_INVALID_ARG);
-    return;
-  }
-
-  JSObject& date = aDate.toObject();
-  JS::Value timestamp;
-  if (!JS::Call(aCx, &date, "getTime", 0, nullptr, &timestamp) ||
-      !timestamp.isNumber() || MOZ_DOUBLE_IS_NaN(timestamp.toNumber())) {
-    JS_ClearPendingException(aCx);
-    SetValue(EmptyString());
-    return;
-  }
-
-  SetValue(timestamp.toNumber());
-}
-
-NS_IMETHODIMP
-HTMLInputElement::SetValueAsDate(JSContext* aCx, const JS::Value& aDate)
-{
-  ErrorResult rv;
-  SetValueAsDate(aCx, aDate, rv);
-  return rv.ErrorCode();
+  SetValue(aDate.Value().TimeStamp());
 }
 
 NS_IMETHODIMP
 HTMLInputElement::GetValueAsNumber(double* aValueAsNumber)
 {
   *aValueAsNumber = ValueAsNumber();
   return NS_OK;
 }
--- a/content/html/content/src/HTMLInputElement.h
+++ b/content/html/content/src/HTMLInputElement.h
@@ -568,19 +568,19 @@ public:
   void SetDefaultValue(const nsAString& aValue, ErrorResult& aRv)
   {
     SetHTMLAttr(nsGkAtoms::value, aValue, aRv);
   }
 
   // XPCOM GetValue() is OK
   void SetValue(const nsAString& aValue, ErrorResult& aRv);
 
-  JS::Value GetValueAsDate(JSContext* aCx, ErrorResult& aRv);
+  Nullable<Date> GetValueAsDate(ErrorResult& aRv);
 
-  void SetValueAsDate(JSContext* aCx, JS::Value aValue, ErrorResult& aRv);
+  void SetValueAsDate(Nullable<Date>, ErrorResult& aRv);
 
   double ValueAsNumber() const
   {
     return DoesValueAsNumberApply() ? GetValueAsDouble()
                                     : MOZ_DOUBLE_NaN();
   }
 
   void SetValueAsNumber(double aValue, ErrorResult& aRv);
--- a/dom/interfaces/html/nsIDOMHTMLInputElement.idl
+++ b/dom/interfaces/html/nsIDOMHTMLInputElement.idl
@@ -15,17 +15,17 @@ interface nsIDOMValidityState;
   *
   * This interface is trying to follow the DOM Level 2 HTML specification:
   * http://www.w3.org/TR/DOM-Level-2-HTML/
   *
   * with changes from the work-in-progress WHATWG HTML specification:
   * http://www.whatwg.org/specs/web-apps/current-work/
   */
 
-[scriptable, uuid(79474f55-a561-44d6-a80f-8e57dba634dd)]
+[scriptable, uuid(d57537ed-39d0-46ea-8516-0ce0a5bfb805)]
 interface nsIDOMHTMLInputElement : nsIDOMHTMLElement
 {
            attribute DOMString             accept;
            attribute DOMString             alt;
 
            attribute DOMString             autocomplete;
            attribute boolean               autofocus;
            attribute boolean               defaultChecked;
@@ -64,17 +64,19 @@ interface nsIDOMHTMLInputElement : nsIDO
            attribute unsigned long         size;
            attribute unsigned long width;
            attribute DOMString             src;
 
            attribute DOMString             type;
            attribute DOMString             defaultValue;
            attribute DOMString             value;
            attribute double                valueAsNumber;
-  [implicit_jscontext] attribute jsval     valueAsDate;
+          // valustAsDate is only supported via WebIDL, because it's intimately
+          // tied to JS Date objects and xpidl support for that sort of thing is
+          // terrible.
 
   [optional_argc] void stepDown([optional] in long n);
   [optional_argc] void stepUp([optional] in long n);
 
   // The following lines are part of the constraint validation API, see:
   // http://www.whatwg.org/specs/web-apps/current-work/#the-constraint-validation-api
   readonly attribute boolean             willValidate;
   readonly attribute nsIDOMValidityState validity;
--- a/dom/webidl/HTMLInputElement.webidl
+++ b/dom/webidl/HTMLInputElement.webidl
@@ -76,19 +76,18 @@ interface HTMLInputElement : HTMLElement
   [Pure, SetterThrows]
            attribute DOMString step;
   [Pure, SetterThrows]
            attribute DOMString type;
   [Pure, SetterThrows]
            attribute DOMString defaultValue;
   [Pure, TreatNullAs=EmptyString, SetterThrows]
            attribute DOMString value;
-           // Bug 742206 - any to Date?
   [Throws]
-           attribute any valueAsDate;
+           attribute Date? valueAsDate;
   [Pure, SetterThrows]
            attribute unrestricted double valueAsNumber;
            attribute unsigned long width;
 
   [Throws]
   void stepUp(optional long n = 1);
   [Throws]
   void stepDown(optional long n = 1);