Bug 742206 part 4. Start using the new Date stuff for HTMLInputElement.valueAsDate. r=smaug

This commit is contained in:
Boris Zbarsky 2013-05-02 14:38:20 -04:00
parent 89ac81bbf5
commit a0eaaa1a6a
4 changed files with 19 additions and 73 deletions

View File

@ -97,7 +97,6 @@
#include <limits>
// 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<Date>
HTMLInputElement::GetValueAsDate(ErrorResult& aRv)
{
if (mType != NS_FORM_INPUT_DATE && mType != NS_FORM_INPUT_TIME) {
return JS::NullValue();
return Nullable<Date>();
}
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<Date>();
}
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>(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<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

View File

@ -573,9 +573,9 @@ public:
// 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
{

View File

@ -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);

View File

@ -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;