From a0eaaa1a6ab22e626b0d1a3d807fb8b9837fb4dd Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Thu, 2 May 2013 14:38:20 -0400 Subject: [PATCH] Bug 742206 part 4. Start using the new Date stuff for HTMLInputElement.valueAsDate. r=smaug --- content/html/content/src/HTMLInputElement.cpp | 79 +++---------------- content/html/content/src/HTMLInputElement.h | 4 +- .../html/nsIDOMHTMLInputElement.idl | 6 +- dom/webidl/HTMLInputElement.webidl | 3 +- 4 files changed, 19 insertions(+), 73 deletions(-) diff --git a/content/html/content/src/HTMLInputElement.cpp b/content/html/content/src/HTMLInputElement.cpp index c63a9751736..669d4801897 100644 --- a/content/html/content/src/HTMLInputElement.cpp +++ b/content/html/content/src/HTMLInputElement.cpp @@ -97,7 +97,6 @@ #include // input type=date -#include "jsapi.h" #include "js/Date.h" NS_IMPL_NS_NEW_HTML_ELEMENT_CHECK_PARSER(Input) @@ -1400,11 +1399,12 @@ HTMLInputElement::ConvertNumberToString(double aValue, } } -JS::Value -HTMLInputElement::GetValueAsDate(JSContext* aCx, ErrorResult& aRv) + +Nullable +HTMLInputElement::GetValueAsDate(ErrorResult& aRv) { if (mType != NS_FORM_INPUT_DATE && mType != NS_FORM_INPUT_TIME) { - return JS::NullValue(); + return Nullable(); } switch (mType) { @@ -1414,26 +1414,10 @@ HTMLInputElement::GetValueAsDate(JSContext* aCx, ErrorResult& aRv) nsAutoString value; GetValueInternal(value); if (!GetValueAsDate(value, &year, &month, &day)) { - return JS::NullValue(); + return Nullable(); } - JSObject* date = JS_NewDateObjectMsec(aCx, 0); - if (!date) { - JS_ClearPendingException(aCx); - return JS::NullValue(); - } - - 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(JS::MakeDate(year, month - 1, day))); } case NS_FORM_INPUT_TIME: { @@ -1441,71 +1425,32 @@ HTMLInputElement::GetValueAsDate(JSContext* aCx, ErrorResult& aRv) nsAutoString value; GetValueInternal(value); if (!ParseTime(value, &millisecond)) { - return JS::NullValue(); + return Nullable(); } - JSObject* date = JS_NewDateObjectMsec(aCx, millisecond); - if (!date) { - JS_ClearPendingException(aCx); - return JS::NullValue(); - } - - return JS::ObjectValue(*date); + return Nullable(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(); } void -HTMLInputElement::SetValueAsDate(JSContext* aCx, JS::Value aDate, ErrorResult& aRv) +HTMLInputElement::SetValueAsDate(Nullable 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, ×tamp) || - !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 diff --git a/content/html/content/src/HTMLInputElement.h b/content/html/content/src/HTMLInputElement.h index fcef2520742..6daea9ba0e3 100644 --- a/content/html/content/src/HTMLInputElement.h +++ b/content/html/content/src/HTMLInputElement.h @@ -573,9 +573,9 @@ public: // XPCOM GetValue() is OK void SetValue(const nsAString& aValue, ErrorResult& aRv); - JS::Value GetValueAsDate(JSContext* aCx, ErrorResult& aRv); + Nullable GetValueAsDate(ErrorResult& aRv); - void SetValueAsDate(JSContext* aCx, JS::Value aValue, ErrorResult& aRv); + void SetValueAsDate(Nullable, ErrorResult& aRv); double ValueAsNumber() const { diff --git a/dom/interfaces/html/nsIDOMHTMLInputElement.idl b/dom/interfaces/html/nsIDOMHTMLInputElement.idl index 6905185016a..10ecec09376 100644 --- a/dom/interfaces/html/nsIDOMHTMLInputElement.idl +++ b/dom/interfaces/html/nsIDOMHTMLInputElement.idl @@ -20,7 +20,7 @@ interface nsIDOMValidityState; * 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; @@ -69,7 +69,9 @@ interface nsIDOMHTMLInputElement : nsIDOMHTMLElement 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); diff --git a/dom/webidl/HTMLInputElement.webidl b/dom/webidl/HTMLInputElement.webidl index 66886d815f5..1b83ce5bc89 100644 --- a/dom/webidl/HTMLInputElement.webidl +++ b/dom/webidl/HTMLInputElement.webidl @@ -81,9 +81,8 @@ interface HTMLInputElement : HTMLElement { 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;