Commit 23b9cb1d authored by Kent Tamura's avatar Kent Tamura Committed by Commit Bot

bindings: Map IDLDate to base::Optional<base::Time> instead of double

* Rename IDLDate to IDLDateOrNull to reflect the reality
* Rename ToCoreDate to ToCoreNullableDate
* Change |double| argument of InputType::SetValueAsDate() to
  base::Optional<base::Time>
* Change |double| argument of |BaseTemporalInputType::
  SerializeWithMilliseconds| to base::Optional<base::Time>
* Rename SerializeWithMilliseconds to SerializeWithDate

This CL has no behavior changes.

Change-Id: I2d8476821317a4577ddcf26a2c88eb9809425ed9
Bug: 988343
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1928512
Commit-Queue: Kent Tamura <tkent@chromium.org>
Reviewed-by: default avatarYuki Shiino <yukishiino@chromium.org>
Reviewed-by: default avatarHitoshi Yoshida <peria@chromium.org>
Cr-Commit-Position: refs/heads/master@{#717609}
parent 5e15eabc
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <type_traits> #include <type_traits>
#include "base/optional.h" #include "base/optional.h"
#include "base/time/time.h"
#include "third_party/blink/renderer/bindings/core/v8/idl_types_base.h" #include "third_party/blink/renderer/bindings/core/v8/idl_types_base.h"
#include "third_party/blink/renderer/bindings/core/v8/native_value_traits.h" #include "third_party/blink/renderer/bindings/core/v8/native_value_traits.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_string_resource.h" #include "third_party/blink/renderer/bindings/core/v8/v8_string_resource.h"
...@@ -87,9 +88,9 @@ struct IDLUnrestrictedDouble final : public IDLBaseHelper<double> {}; ...@@ -87,9 +88,9 @@ struct IDLUnrestrictedDouble final : public IDLBaseHelper<double> {};
struct IDLFloat final : public IDLBaseHelper<float> {}; struct IDLFloat final : public IDLBaseHelper<float> {};
struct IDLUnrestrictedFloat final : public IDLBaseHelper<float> {}; struct IDLUnrestrictedFloat final : public IDLBaseHelper<float> {};
// Date // Nullable Date
// TODO(crbug.com/988343): Change |double| to |base::Time|. struct IDLDateOrNull final : public IDLBaseHelper<base::Optional<base::Time>> {
struct IDLDate final : public IDLBaseHelper<double> {}; };
// Promise // Promise
struct IDLPromise final : public IDLBaseHelper<ScriptPromise> {}; struct IDLPromise final : public IDLBaseHelper<ScriptPromise> {};
......
...@@ -396,12 +396,13 @@ struct CORE_EXPORT NativeValueTraits<IDLPromise> ...@@ -396,12 +396,13 @@ struct CORE_EXPORT NativeValueTraits<IDLPromise>
// Type-specific overloads // Type-specific overloads
template <> template <>
struct CORE_EXPORT NativeValueTraits<IDLDate> struct CORE_EXPORT NativeValueTraits<IDLDateOrNull>
: public NativeValueTraitsBase<IDLDate> { : public NativeValueTraitsBase<IDLDateOrNull> {
static double NativeValue(v8::Isolate* isolate, static base::Optional<base::Time> NativeValue(
v8::Local<v8::Value> value, v8::Isolate* isolate,
ExceptionState& exception_state) { v8::Local<v8::Value> value,
return ToCoreDate(isolate, value, exception_state); ExceptionState& exception_state) {
return ToCoreNullableDate(isolate, value, exception_state);
} }
}; };
......
...@@ -387,16 +387,24 @@ CORE_EXPORT float ToRestrictedFloat(v8::Isolate*, ...@@ -387,16 +387,24 @@ CORE_EXPORT float ToRestrictedFloat(v8::Isolate*,
v8::Local<v8::Value>, v8::Local<v8::Value>,
ExceptionState&); ExceptionState&);
inline double ToCoreDate(v8::Isolate* isolate, inline base::Optional<base::Time> ToCoreNullableDate(
v8::Local<v8::Value> object, v8::Isolate* isolate,
ExceptionState& exception_state) { v8::Local<v8::Value> object,
ExceptionState& exception_state) {
// https://html.spec.whatwg.org/C/#common-input-element-apis:dom-input-valueasdate-2
// ... otherwise if the new value is null or a Date object representing the
// NaN time value, then set the value of the element to the empty string;
// We'd like to return same values for |null| and an invalid Date object.
if (object->IsNull()) if (object->IsNull())
return std::numeric_limits<double>::quiet_NaN(); return base::nullopt;
if (!object->IsDate()) { if (!object->IsDate()) {
exception_state.ThrowTypeError("The provided value is not a Date."); exception_state.ThrowTypeError("The provided value is not a Date.");
return 0; return base::nullopt;
} }
return object.As<v8::Date>()->ValueOf(); double time_value = object.As<v8::Date>()->ValueOf();
if (!std::isfinite(time_value))
return base::nullopt;
return base::Time::FromJsTime(time_value);
} }
// USVString conversion helper. // USVString conversion helper.
......
...@@ -66,9 +66,10 @@ double BaseTemporalInputType::ValueAsDate() const { ...@@ -66,9 +66,10 @@ double BaseTemporalInputType::ValueAsDate() const {
return ValueAsDouble(); return ValueAsDouble();
} }
void BaseTemporalInputType::SetValueAsDate(double value, void BaseTemporalInputType::SetValueAsDate(
ExceptionState&) const { const base::Optional<base::Time>& value,
GetElement().setValue(SerializeWithMilliseconds(value)); ExceptionState&) const {
GetElement().setValue(SerializeWithDate(value));
} }
double BaseTemporalInputType::ValueAsDouble() const { double BaseTemporalInputType::ValueAsDouble() const {
...@@ -154,8 +155,11 @@ String BaseTemporalInputType::SerializeWithComponents( ...@@ -154,8 +155,11 @@ String BaseTemporalInputType::SerializeWithComponents(
return date.ToString(DateComponents::kMillisecond); return date.ToString(DateComponents::kMillisecond);
} }
String BaseTemporalInputType::SerializeWithMilliseconds(double value) const { String BaseTemporalInputType::SerializeWithDate(
return Serialize(Decimal::FromDouble(value)); const base::Optional<base::Time>& value) const {
if (!value)
return g_empty_string;
return Serialize(Decimal::FromDouble(value->ToJsTimeIgnoringNull()));
} }
String BaseTemporalInputType::LocalizeValue( String BaseTemporalInputType::LocalizeValue(
......
...@@ -85,7 +85,8 @@ class BaseTemporalInputType : public InputType { ...@@ -85,7 +85,8 @@ class BaseTemporalInputType : public InputType {
InputTypeView* CreateView() override; InputTypeView* CreateView() override;
ValueMode GetValueMode() const override; ValueMode GetValueMode() const override;
double ValueAsDate() const override; double ValueAsDate() const override;
void SetValueAsDate(double, ExceptionState&) const override; void SetValueAsDate(const base::Optional<base::Time>&,
ExceptionState&) const override;
double ValueAsDouble() const override; double ValueAsDouble() const override;
void SetValueAsDouble(double, void SetValueAsDouble(double,
TextFieldEventBehavior, TextFieldEventBehavior,
...@@ -97,7 +98,7 @@ class BaseTemporalInputType : public InputType { ...@@ -97,7 +98,7 @@ class BaseTemporalInputType : public InputType {
String RangeUnderflowText(const Decimal& minimum) const override; String RangeUnderflowText(const Decimal& minimum) const override;
Decimal DefaultValueForStepUp() const override; Decimal DefaultValueForStepUp() const override;
bool IsSteppable() const override; bool IsSteppable() const override;
virtual String SerializeWithMilliseconds(double) const; virtual String SerializeWithDate(const base::Optional<base::Time>&) const;
String LocalizeValue(const String&) const override; String LocalizeValue(const String&) const override;
bool SupportsReadOnly() const override; bool SupportsReadOnly() const override;
bool ShouldRespectListAttribute() override; bool ShouldRespectListAttribute() override;
......
...@@ -62,7 +62,7 @@ double DateTimeLocalInputType::ValueAsDate() const { ...@@ -62,7 +62,7 @@ double DateTimeLocalInputType::ValueAsDate() const {
} }
void DateTimeLocalInputType::SetValueAsDate( void DateTimeLocalInputType::SetValueAsDate(
double value, const base::Optional<base::Time>& value,
ExceptionState& exception_state) const { ExceptionState& exception_state) const {
// valueAsDate doesn't work for the datetime-local type according to the // valueAsDate doesn't work for the datetime-local type according to the
// standard. // standard.
......
...@@ -46,7 +46,8 @@ class DateTimeLocalInputType final : public BaseTemporalInputType { ...@@ -46,7 +46,8 @@ class DateTimeLocalInputType final : public BaseTemporalInputType {
void CountUsage() override; void CountUsage() override;
const AtomicString& FormControlType() const override; const AtomicString& FormControlType() const override;
double ValueAsDate() const override; double ValueAsDate() const override;
void SetValueAsDate(double, ExceptionState&) const override; void SetValueAsDate(const base::Optional<base::Time>&,
ExceptionState&) const override;
StepRange CreateStepRange(AnyStepHandling) const override; StepRange CreateStepRange(AnyStepHandling) const override;
bool ParseToDateComponentsInternal(const String&, bool ParseToDateComponentsInternal(const String&,
DateComponents*) const override; DateComponents*) const override;
......
...@@ -1211,11 +1211,12 @@ ScriptValue HTMLInputElement::valueAsDate(ScriptState* script_state) const { ...@@ -1211,11 +1211,12 @@ ScriptValue HTMLInputElement::valueAsDate(ScriptState* script_state) const {
void HTMLInputElement::setValueAsDate(ScriptState* script_state, void HTMLInputElement::setValueAsDate(ScriptState* script_state,
const ScriptValue& value, const ScriptValue& value,
ExceptionState& exception_state) { ExceptionState& exception_state) {
double date_ms = NativeValueTraits<IDLDate>::NativeValue( base::Optional<base::Time> date =
script_state->GetIsolate(), value.V8Value(), exception_state); NativeValueTraits<IDLDateOrNull>::NativeValue(
script_state->GetIsolate(), value.V8Value(), exception_state);
if (exception_state.HadException()) if (exception_state.HadException())
return; return;
input_type_->SetValueAsDate(date_ms, exception_state); input_type_->SetValueAsDate(date, exception_state);
} }
double HTMLInputElement::valueAsNumber() const { double HTMLInputElement::valueAsNumber() const {
......
...@@ -226,7 +226,8 @@ double InputType::ValueAsDate() const { ...@@ -226,7 +226,8 @@ double InputType::ValueAsDate() const {
return DateComponents::InvalidMilliseconds(); return DateComponents::InvalidMilliseconds();
} }
void InputType::SetValueAsDate(double, ExceptionState& exception_state) const { void InputType::SetValueAsDate(const base::Optional<base::Time>&,
ExceptionState& exception_state) const {
exception_state.ThrowDOMException( exception_state.ThrowDOMException(
DOMExceptionCode::kInvalidStateError, DOMExceptionCode::kInvalidStateError,
"This input element does not support Date values."); "This input element does not support Date values.");
......
...@@ -95,7 +95,8 @@ class CORE_EXPORT InputType : public GarbageCollected<InputType> { ...@@ -95,7 +95,8 @@ class CORE_EXPORT InputType : public GarbageCollected<InputType> {
virtual ValueMode GetValueMode() const = 0; virtual ValueMode GetValueMode() const = 0;
virtual double ValueAsDate() const; virtual double ValueAsDate() const;
virtual void SetValueAsDate(double, ExceptionState&) const; virtual void SetValueAsDate(const base::Optional<base::Time>&,
ExceptionState&) const;
virtual double ValueAsDouble() const; virtual double ValueAsDouble() const;
virtual void SetValueAsDouble(double, virtual void SetValueAsDouble(double,
TextFieldEventBehavior, TextFieldEventBehavior,
......
...@@ -64,9 +64,11 @@ double MonthInputType::ValueAsDate() const { ...@@ -64,9 +64,11 @@ double MonthInputType::ValueAsDate() const {
return msec; return msec;
} }
String MonthInputType::SerializeWithMilliseconds(double value) const { String MonthInputType::SerializeWithDate(
const base::Optional<base::Time>& value) const {
DateComponents date; DateComponents date;
if (!date.SetMillisecondsSinceEpochForMonth(value)) if (!value ||
!date.SetMillisecondsSinceEpochForMonth(value->ToJsTimeIgnoringNull()))
return String(); return String();
return SerializeWithComponents(date); return SerializeWithComponents(date);
} }
......
...@@ -44,7 +44,7 @@ class MonthInputType final : public BaseTemporalInputType { ...@@ -44,7 +44,7 @@ class MonthInputType final : public BaseTemporalInputType {
void CountUsage() override; void CountUsage() override;
const AtomicString& FormControlType() const override; const AtomicString& FormControlType() const override;
double ValueAsDate() const override; double ValueAsDate() const override;
String SerializeWithMilliseconds(double) const override; String SerializeWithDate(const base::Optional<base::Time>&) const override;
Decimal ParseToNumber(const String&, const Decimal&) const override; Decimal ParseToNumber(const String&, const Decimal&) const override;
Decimal DefaultValueForStepUp() const override; Decimal DefaultValueForStepUp() const override;
StepRange CreateStepRange(AnyStepHandling) const override; StepRange CreateStepRange(AnyStepHandling) const override;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment