LibWeb: Declare optional parse results inlined in if statements

This commit is contained in:
Bastiaan van der Plaat 2023-12-07 18:40:54 +01:00 committed by Tim Flynn
parent 1b9a961fb0
commit 3bfc2f5e95
Notes: sideshowbarker 2024-07-18 03:20:18 +09:00
5 changed files with 40 additions and 65 deletions

View File

@ -1166,11 +1166,9 @@ i32 HTMLInputElement::default_tab_index_value() const
unsigned HTMLInputElement::size() const unsigned HTMLInputElement::size() const
{ {
// The size IDL attribute is limited to only positive numbers and has a default value of 20. // The size IDL attribute is limited to only positive numbers and has a default value of 20.
auto maybe_size_string = get_attribute(HTML::AttributeNames::size); if (auto size_string = get_attribute(HTML::AttributeNames::size); size_string.has_value()) {
if (maybe_size_string.has_value()) { if (auto size = parse_non_negative_integer(*size_string); size.has_value())
auto maybe_size = parse_non_negative_integer(maybe_size_string.value()); return *size;
if (maybe_size.has_value())
return maybe_size.value();
} }
return 20; return 20;
} }

View File

@ -38,11 +38,9 @@ double HTMLMeterElement::value() const
{ {
// If the value attribute is specified and a value could be parsed out of it, then that value is the candidate actual value. Otherwise, the candidate actual value is zero. // If the value attribute is specified and a value could be parsed out of it, then that value is the candidate actual value. Otherwise, the candidate actual value is zero.
double candidate_value = 0.0; double candidate_value = 0.0;
auto maybe_value_string = get_attribute(HTML::AttributeNames::value); if (auto value_string = get_attribute(HTML::AttributeNames::value); value_string.has_value()) {
if (maybe_value_string.has_value()) { if (auto value = parse_floating_point_number(*value_string); value.has_value())
auto maybe_value = parse_floating_point_number(maybe_value_string.value()); candidate_value = *value;
if (maybe_value.has_value())
candidate_value = maybe_value.value();
} }
// If the candidate actual value is less than the minimum value, then the actual value is the minimum value. // If the candidate actual value is less than the minimum value, then the actual value is the minimum value.
@ -63,11 +61,9 @@ WebIDL::ExceptionOr<void> HTMLMeterElement::set_value(double value)
double HTMLMeterElement::min() const double HTMLMeterElement::min() const
{ {
// If the min attribute is specified and a value could be parsed out of it, then the minimum value is that value. Otherwise, the minimum value is zero. // If the min attribute is specified and a value could be parsed out of it, then the minimum value is that value. Otherwise, the minimum value is zero.
auto maybe_min_string = get_attribute(HTML::AttributeNames::min); if (auto min_string = get_attribute(HTML::AttributeNames::min); min_string.has_value()) {
if (maybe_min_string.has_value()) { if (auto min = parse_floating_point_number(*min_string); min.has_value())
auto maybe_min = parse_floating_point_number(maybe_min_string.value()); return *min;
if (maybe_min.has_value())
return maybe_min.value();
} }
return 0; return 0;
} }
@ -85,11 +81,9 @@ double HTMLMeterElement::max() const
{ {
// If the max attribute is specified and a value could be parsed out of it, then the candidate maximum value is that value. Otherwise, the candidate maximum value is 1.0. // If the max attribute is specified and a value could be parsed out of it, then the candidate maximum value is that value. Otherwise, the candidate maximum value is 1.0.
double candidate_max = 1.0; double candidate_max = 1.0;
auto maybe_max_string = get_attribute(HTML::AttributeNames::max); if (auto max_string = get_attribute(HTML::AttributeNames::max); max_string.has_value()) {
if (maybe_max_string.has_value()) { if (auto max = parse_floating_point_number(*max_string); max.has_value())
auto maybe_max = parse_floating_point_number(maybe_max_string.value()); candidate_max = *max;
if (maybe_max.has_value())
candidate_max = maybe_max.value();
} }
// If the candidate maximum value is greater than or equal to the minimum value, then the maximum value is the candidate maximum value. Otherwise, the maximum value is the same as the minimum value. // If the candidate maximum value is greater than or equal to the minimum value, then the maximum value is the candidate maximum value. Otherwise, the maximum value is the same as the minimum value.
@ -109,11 +103,9 @@ double HTMLMeterElement::low() const
{ {
// If the low attribute is specified and a value could be parsed out of it, then the candidate low boundary is that value. Otherwise, the candidate low boundary is the same as the minimum value. // If the low attribute is specified and a value could be parsed out of it, then the candidate low boundary is that value. Otherwise, the candidate low boundary is the same as the minimum value.
double candidate_low = min(); double candidate_low = min();
auto maybe_low_string = get_attribute(HTML::AttributeNames::low); if (auto low_string = get_attribute(HTML::AttributeNames::low); low_string.has_value()) {
if (maybe_low_string.has_value()) { if (auto low = parse_floating_point_number(*low_string); low.has_value())
auto maybe_low = parse_floating_point_number(maybe_low_string.value()); candidate_low = *low;
if (maybe_low.has_value())
candidate_low = maybe_low.value();
} }
// If the candidate low boundary is less than the minimum value, then the low boundary is the minimum value. // If the candidate low boundary is less than the minimum value, then the low boundary is the minimum value.
@ -135,11 +127,9 @@ double HTMLMeterElement::high() const
{ {
// If the high attribute is specified and a value could be parsed out of it, then the candidate high boundary is that value. Otherwise, the candidate high boundary is the same as the maximum value. // If the high attribute is specified and a value could be parsed out of it, then the candidate high boundary is that value. Otherwise, the candidate high boundary is the same as the maximum value.
double candidate_high = max(); double candidate_high = max();
auto maybe_high_string = get_attribute(HTML::AttributeNames::high); if (auto high_string = get_attribute(HTML::AttributeNames::high); high_string.has_value()) {
if (maybe_high_string.has_value()) { if (auto high = parse_floating_point_number(*high_string); high.has_value())
auto maybe_high = parse_floating_point_number(maybe_high_string.value()); candidate_high = *high;
if (maybe_high.has_value())
candidate_high = maybe_high.value();
} }
// If the candidate high boundary is less than the low boundary, then the high boundary is the low boundary. // If the candidate high boundary is less than the low boundary, then the high boundary is the low boundary.
@ -161,11 +151,9 @@ double HTMLMeterElement::optimum() const
{ {
// If the optimum attribute is specified and a value could be parsed out of it, then the candidate optimum point is that value. Otherwise, the candidate optimum point is the midpoint between the minimum value and the maximum value. // If the optimum attribute is specified and a value could be parsed out of it, then the candidate optimum point is that value. Otherwise, the candidate optimum point is the midpoint between the minimum value and the maximum value.
double candidate_optimum = (max() + min()) / 2; double candidate_optimum = (max() + min()) / 2;
auto maybe_optimum_string = get_attribute(HTML::AttributeNames::optimum); if (auto optimum_string = get_attribute(HTML::AttributeNames::optimum); optimum_string.has_value()) {
if (maybe_optimum_string.has_value()) { if (auto optimum = parse_floating_point_number(*optimum_string); optimum.has_value())
auto maybe_optimum = parse_floating_point_number(maybe_optimum_string.value()); candidate_optimum = *optimum;
if (maybe_optimum.has_value())
candidate_optimum = maybe_optimum.value();
} }
// If the candidate optimum point is less than the minimum value, then the optimum point is the minimum value. // If the candidate optimum point is less than the minimum value, then the optimum point is the minimum value.
@ -246,5 +234,4 @@ void HTMLMeterElement::update_meter_value_element()
double position = (value - min) / (max - min) * 100; double position = (value - min) / (max - min) * 100;
MUST(m_meter_value_element->set_attribute(HTML::AttributeNames::style, MUST(String::formatted("width: {}%;", position)))); MUST(m_meter_value_element->set_attribute(HTML::AttributeNames::style, MUST(String::formatted("width: {}%;", position))));
} }
} }

View File

@ -37,13 +37,11 @@ void HTMLProgressElement::visit_edges(Cell::Visitor& visitor)
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-progress-value // https://html.spec.whatwg.org/multipage/form-elements.html#dom-progress-value
double HTMLProgressElement::value() const double HTMLProgressElement::value() const
{ {
auto maybe_value_string = get_attribute(HTML::AttributeNames::value); if (auto value_string = get_attribute(HTML::AttributeNames::value); value_string.has_value()) {
if (!maybe_value_string.has_value()) if (auto value = parse_floating_point_number(*value_string); value.has_value())
return 0; return clamp(*value, 0, max());
auto maybe_value = parse_floating_point_number(maybe_value_string.value()); }
if (!maybe_value.has_value()) return 0;
return 0;
return clamp(maybe_value.value(), 0, max());
} }
WebIDL::ExceptionOr<void> HTMLProgressElement::set_value(double value) WebIDL::ExceptionOr<void> HTMLProgressElement::set_value(double value)
@ -60,13 +58,11 @@ WebIDL::ExceptionOr<void> HTMLProgressElement::set_value(double value)
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-progress-max // https://html.spec.whatwg.org/multipage/form-elements.html#dom-progress-max
double HTMLProgressElement::max() const double HTMLProgressElement::max() const
{ {
auto maybe_max_string = get_attribute(HTML::AttributeNames::max); if (auto max_string = get_attribute(HTML::AttributeNames::max); max_string.has_value()) {
if (!maybe_max_string.has_value()) if (auto max = parse_floating_point_number(*max_string); max.has_value())
return 1; return AK::max(*max, 0);
auto maybe_max = parse_floating_point_number(maybe_max_string.value()); }
if (!maybe_max.has_value()) return 1;
return 1;
return AK::max(maybe_max.value(), 0);
} }
WebIDL::ExceptionOr<void> HTMLProgressElement::set_max(double value) WebIDL::ExceptionOr<void> HTMLProgressElement::set_max(double value)

View File

@ -29,11 +29,9 @@ void HTMLTableColElement::initialize(JS::Realm& realm)
unsigned int HTMLTableColElement::span() const unsigned int HTMLTableColElement::span() const
{ {
// The span IDL attribute must reflect the content attribute of the same name. It is clamped to the range [1, 1000], and its default value is 1. // The span IDL attribute must reflect the content attribute of the same name. It is clamped to the range [1, 1000], and its default value is 1.
auto maybe_span_string = get_attribute(HTML::AttributeNames::span); if (auto span_string = get_attribute(HTML::AttributeNames::span); span_string.has_value()) {
if (maybe_span_string.has_value()) { if (auto span = parse_non_negative_integer(*span_string); span.has_value())
auto maybe_span = parse_non_negative_integer(maybe_span_string.value()); return clamp(*span, 1, 1000);
if (maybe_span.has_value())
return clamp(maybe_span.value(), 1, 1000);
} }
return 1; return 1;
} }

View File

@ -100,11 +100,9 @@ void HTMLTextAreaElement::form_associated_element_was_removed(DOM::Node*)
unsigned HTMLTextAreaElement::cols() const unsigned HTMLTextAreaElement::cols() const
{ {
// The cols and rows attributes are limited to only positive numbers with fallback. The cols IDL attribute's default value is 20. // The cols and rows attributes are limited to only positive numbers with fallback. The cols IDL attribute's default value is 20.
auto maybe_cols_string = get_attribute(HTML::AttributeNames::cols); if (auto cols_string = get_attribute(HTML::AttributeNames::cols); cols_string.has_value()) {
if (maybe_cols_string.has_value()) { if (auto cols = parse_non_negative_integer(*cols_string); cols.has_value())
auto maybe_cols = parse_non_negative_integer(maybe_cols_string.value()); return *cols;
if (maybe_cols.has_value())
return maybe_cols.value();
} }
return 20; return 20;
} }
@ -118,11 +116,9 @@ WebIDL::ExceptionOr<void> HTMLTextAreaElement::set_cols(unsigned value)
unsigned HTMLTextAreaElement::rows() const unsigned HTMLTextAreaElement::rows() const
{ {
// The cols and rows attributes are limited to only positive numbers with fallback. The rows IDL attribute's default value is 2. // The cols and rows attributes are limited to only positive numbers with fallback. The rows IDL attribute's default value is 2.
auto maybe_rows_string = get_attribute(HTML::AttributeNames::rows); if (auto rows_string = get_attribute(HTML::AttributeNames::rows); rows_string.has_value()) {
if (maybe_rows_string.has_value()) { if (auto rows = parse_non_negative_integer(*rows_string); rows.has_value())
auto maybe_rows = parse_non_negative_integer(maybe_rows_string.value()); return *rows;
if (maybe_rows.has_value())
return maybe_rows.value();
} }
return 2; return 2;
} }