1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 06:15:07 +00:00

LibWeb: Add input element valueAsDate property

This commit is contained in:
Bastiaan van der Plaat 2023-12-15 18:23:29 +01:00 committed by Andrew Kaster
parent be511fdcf7
commit cf69fd0a09
10 changed files with 314 additions and 9 deletions

View file

@ -128,6 +128,22 @@ bool is_valid_date_string(StringView value)
return day >= 1 && day <= AK::days_in_month(year, month);
}
// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#parse-a-date-string
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Date>> parse_date_string(JS::Realm& realm, StringView value)
{
// FIXME: Implement spec compliant date string parsing
auto parts = value.split_view('-');
if (parts.size() >= 3) {
if (auto year = parts.at(0).to_uint(); year.has_value()) {
if (auto month = parts.at(1).to_uint(); month.has_value()) {
if (auto day_of_month = parts.at(2).to_uint(); day_of_month.has_value())
return JS::Date::create(realm, JS::make_date(JS::make_day(*year, *month - 1, *day_of_month), 0));
}
}
}
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Can't parse date string"sv };
}
// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-local-date-and-time-string
bool is_valid_local_date_and_time_string(StringView value)
{
@ -197,4 +213,23 @@ bool is_valid_time_string(StringView value)
return true;
}
// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#parse-a-time-string
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Date>> parse_time_string(JS::Realm& realm, StringView value)
{
// FIXME: Implement spec compliant time string parsing
auto parts = value.split_view(':');
if (parts.size() >= 2) {
if (auto hours = parts.at(0).to_uint(); hours.has_value()) {
if (auto minutes = parts.at(1).to_uint(); minutes.has_value()) {
if (parts.size() >= 3) {
if (auto seconds = parts.at(2).to_uint(); seconds.has_value())
return JS::Date::create(realm, JS::make_time(*hours, *minutes, *seconds, 0));
}
return JS::Date::create(realm, JS::make_date(0, JS::make_time(*hours, *minutes, 0, 0)));
}
}
}
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Can't parse time string"sv };
}
}