dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / html / html_meter_element.lucb
#==============================================================================================
#
# html_meter_element - HTML::HTMLMeterElement: <meter>
#
# DESCRIPTION:
# Port of LibWeb/HTML/HTMLMeterElement.cpp (lines 1-240) and the inline functions of
# HTMLMeterElement.h: construction (which moves the mixin tables to HTMLMeterElement's,
# as C++'s vptrs move), the actual value, minimum, maximum, low and high boundaries and
# optimum point with their setters, the insertion steps, the unboxed display, the
# user-agent shadow tree (a slider track and fill) and the value state of the gauge.
#
# Copyright (c) 2020, the SerenityOS developers.
# Copyright (c) 2023, Bastiaan van der Plaat <bastiaan.v.d.plaat@gmail.com>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Construction ============================================================================
## Ported from HTMLMeterElement.cpp:23 Web::HTML::HTMLMeterElement::HTMLMeterElement.
pub func html_meter_element_construct(this: HtmlMeterElement*, document: DomDocument*, qualified_name: DomQualifiedName):
html_element_construct(this.html_element(), document, qualified_name)
# The vptrs of the mixin bases move to HTMLMeterElement's tables.
let element = this.html_element()
element.element.slottable_mixin.vtable = &html_meter_element_slottable_mixin_vtable
element.element.aria_mixin.vtable = &html_meter_element_aria_mixin_vtable
element.element.animatable.vtable = &html_meter_element_animatable_vtable
element.global_event_handlers.vtable = &html_meter_element_global_event_handlers_vtable
element.form_associated_element.vtable = &html_meter_element_form_associated_element_vtable
## Ported from HTMLMeterElement.cpp:30 Web::HTML::HTMLMeterElement::initialize (override).
pub func html_meter_element_initialize_impl(base: JsCell*, realm: Realm*):
web_set_prototype_for_interface(base, realm, BindInterfaceName.html_meter_element)
html_element_initialize_impl(base, realm)
## Ported from HTMLMeterElement.cpp:36 Web::HTML::HTMLMeterElement::visit_edges (override).
pub func html_meter_element_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (HtmlMeterElement*)base
html_element_visit_edges_impl(base, visitor)
gc.cell_visitor_visit_t(visitor, this.m_meter_value_element)
# mark: Values ==================================================================================
## The value of the floating-point attribute `name`, if it is specified and a value could be parsed out of it.
func html_meter_element_parsed_attribute(this: HtmlMeterElement*, name: ak.FlyString) -> f64?:
let string = dom_element_get_attribute(this.dom_element(), name) else return none
return html_parse_floating_point_number_string_view(ak.string_bytes_as_string_view(&string))
## Ported from HTMLMeterElement.cpp:43 Web::HTML::HTMLMeterElement::value.
# https://html.spec.whatwg.org/multipage/form-elements.html#concept-meter-actual
pub func html_meter_element_value(this: HtmlMeterElement*) -> f64:
# 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.
var candidate_value = 0.0
if let value = html_meter_element_parsed_attribute(this, html_attribute_names_value):
candidate_value = value
# If the candidate actual value is less than the minimum value, then the actual value is the minimum value.
# Otherwise, if the candidate actual value is greater than the maximum value, then the actual value is the maximum value.
# Otherwise, the actual value is the candidate actual value.
return ak.clamp_f64(candidate_value, html_meter_element_min(this), html_meter_element_max(this))
## Ported from HTMLMeterElement.cpp:58 Web::HTML::HTMLMeterElement::set_value.
pub func html_meter_element_set_value(this: HtmlMeterElement*, value: f64):
dom_element_set_attribute_value(this.dom_element(), html_attribute_names_value, ak.string_number_f64(value))
html_meter_element_update_meter_value_element(this)
## Ported from HTMLMeterElement.cpp:65 Web::HTML::HTMLMeterElement::min.
# https://html.spec.whatwg.org/multipage/form-elements.html#concept-meter-minimum
pub func html_meter_element_min(this: HtmlMeterElement*) -> f64:
# 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 let min = html_meter_element_parsed_attribute(this, html_attribute_names_min):
return min
return 0.0
## Ported from HTMLMeterElement.cpp:75 Web::HTML::HTMLMeterElement::set_min.
pub func html_meter_element_set_min(this: HtmlMeterElement*, value: f64):
dom_element_set_attribute_value(this.dom_element(), html_attribute_names_min, ak.string_number_f64(value))
html_meter_element_update_meter_value_element(this)
## Ported from HTMLMeterElement.cpp:82 Web::HTML::HTMLMeterElement::max.
# https://html.spec.whatwg.org/multipage/form-elements.html#concept-meter-maximum
pub func html_meter_element_max(this: HtmlMeterElement*) -> f64:
# 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.
var candidate_max = 1.0
if let max = html_meter_element_parsed_attribute(this, html_attribute_names_max):
candidate_max = max
# 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.
return ak.max_f64(candidate_max, html_meter_element_min(this))
## Ported from HTMLMeterElement.cpp:95 Web::HTML::HTMLMeterElement::set_max.
pub func html_meter_element_set_max(this: HtmlMeterElement*, value: f64):
dom_element_set_attribute_value(this.dom_element(), html_attribute_names_max, ak.string_number_f64(value))
html_meter_element_update_meter_value_element(this)
## Ported from HTMLMeterElement.cpp:102 Web::HTML::HTMLMeterElement::low.
# https://html.spec.whatwg.org/multipage/form-elements.html#concept-meter-low
pub func html_meter_element_low(this: HtmlMeterElement*) -> f64:
# 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.
var candidate_low = html_meter_element_min(this)
if let low = html_meter_element_parsed_attribute(this, html_attribute_names_low):
candidate_low = low
# If the candidate low boundary is less than the minimum value, then the low boundary is the minimum value.
# Otherwise, if the candidate low boundary is greater than the maximum value, then the low boundary is the maximum value.
# Otherwise, the low boundary is the candidate low boundary.
return ak.clamp_f64(candidate_low, html_meter_element_min(this), html_meter_element_max(this))
## Ported from HTMLMeterElement.cpp:117 Web::HTML::HTMLMeterElement::set_low.
pub func html_meter_element_set_low(this: HtmlMeterElement*, value: f64):
dom_element_set_attribute_value(this.dom_element(), html_attribute_names_low, ak.string_number_f64(value))
html_meter_element_update_meter_value_element(this)
## Ported from HTMLMeterElement.cpp:124 Web::HTML::HTMLMeterElement::high.
# https://html.spec.whatwg.org/multipage/form-elements.html#concept-meter-high
pub func html_meter_element_high(this: HtmlMeterElement*) -> f64:
# 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.
var candidate_high = html_meter_element_max(this)
if let high = html_meter_element_parsed_attribute(this, html_attribute_names_high):
candidate_high = high
# If the candidate high boundary is less than the low boundary, then the high boundary is the low boundary.
# Otherwise, if the candidate high boundary is greater than the maximum value, then the high boundary is the maximum value.
# Otherwise, the high boundary is the candidate high boundary.
return ak.clamp_f64(candidate_high, html_meter_element_low(this), html_meter_element_max(this))
## Ported from HTMLMeterElement.cpp:139 Web::HTML::HTMLMeterElement::set_high.
pub func html_meter_element_set_high(this: HtmlMeterElement*, value: f64):
dom_element_set_attribute_value(this.dom_element(), html_attribute_names_high, ak.string_number_f64(value))
html_meter_element_update_meter_value_element(this)
## Ported from HTMLMeterElement.cpp:146 Web::HTML::HTMLMeterElement::optimum.
# https://html.spec.whatwg.org/multipage/form-elements.html#concept-meter-optimum
pub func html_meter_element_optimum(this: HtmlMeterElement*) -> f64:
# 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.
var candidate_optimum = (html_meter_element_max(this) + html_meter_element_min(this)) / 2.0
if let optimum = html_meter_element_parsed_attribute(this, html_attribute_names_optimum):
candidate_optimum = optimum
# If the candidate optimum point is less than the minimum value, then the optimum point is the minimum value.
# Otherwise, if the candidate optimum point is greater than the maximum value, then the optimum point is the maximum value.
# Otherwise, the optimum point is the candidate optimum point.
return ak.clamp_f64(candidate_optimum, html_meter_element_min(this), html_meter_element_max(this))
## Ported from HTMLMeterElement.cpp:161 Web::HTML::HTMLMeterElement::set_optimum.
pub func html_meter_element_set_optimum(this: HtmlMeterElement*, value: f64):
dom_element_set_attribute_value(this.dom_element(), html_attribute_names_optimum, ak.string_number_f64(value))
html_meter_element_update_meter_value_element(this)
# mark: Rendering ===============================================================================
## Ported from HTMLMeterElement.cpp:167 Web::HTML::HTMLMeterElement::inserted (override).
pub func html_meter_element_inserted_impl(base: DomNode*):
html_element_inserted_impl(base)
html_meter_element_create_shadow_tree_if_needed((HtmlMeterElement*)base)
## Ported from HTMLMeterElement.cpp:173 Web::HTML::HTMLMeterElement::adjust_computed_style (override).
pub func html_meter_element_adjust_computed_style_impl(base: DomElement*, style: CssComputedProperties*):
_ = base
# https://drafts.csswg.org/css-display-3/#unbox
let display = css_computed_properties_display(style)
if css_display_is_contents(&display):
css_computed_properties_set_property(style, css_data.PropertyId.display, css_display_style_value_create(css_display_from_short(CssDisplayShort.none_)).css_style_value())
## Ported from HTMLMeterElement.cpp:180 Web::HTML::HTMLMeterElement::create_shadow_tree_if_needed.
func html_meter_element_create_shadow_tree_if_needed(this: HtmlMeterElement*):
if dom_element_shadow_root(this.dom_element()) != none:
return
let document = dom_node_document(this.dom_node())
let shadow_root = realm_create_dom_shadow_root(html_element_realm(this.html_element()), document, this.dom_element(), BindShadowRootMode.closed)
dom_shadow_root_set_user_agent_internal(shadow_root, true)
dom_element_set_shadow_root(this.dom_element(), shadow_root)
let meter_bar_element = dom_create_element(document, html_tag_names_div, namespace_html) else trap("MUST: DOM::create_element")
dom_element_set_use_pseudo_element(meter_bar_element, css_data.PseudoElement.slider_track)
html_append_child(shadow_root.dom_node(), meter_bar_element.dom_node())
let meter_value_element = dom_create_element(document, html_tag_names_div, namespace_html) else trap("MUST: DOM::create_element")
this.m_meter_value_element = meter_value_element
dom_element_set_use_pseudo_element(meter_value_element, css_data.PseudoElement.slider_fill)
html_append_child(meter_bar_element.dom_node(), meter_value_element.dom_node())
html_meter_element_update_meter_value_element(this)
## Ported from HTMLMeterElement.cpp:199 Web::HTML::HTMLMeterElement::update_meter_value_element.
func html_meter_element_update_meter_value_element(this: HtmlMeterElement*):
# UA requirements for regions of the gauge:
let value = html_meter_element_value(this)
let min = html_meter_element_min(this)
let max = html_meter_element_max(this)
let low = html_meter_element_low(this)
let high = html_meter_element_high(this)
let optimum = html_meter_element_optimum(this)
# If the optimum point is equal to the low boundary or the high boundary, or anywhere in between them, then the region between the low and high boundaries of the gauge must be treated as the optimum region, and the low and high parts, if any, must be treated as suboptimal.
if optimum >= low and optimum <= high:
if value >= low and value <= high:
this.m_cached_value_state = HtmlMeterElementValueState.optimal
else:
this.m_cached_value_state = HtmlMeterElementValueState.suboptimal
# Otherwise, if the optimum point is less than the low boundary, then the region between the minimum value and the low boundary must be treated as the optimum region, the region from the low boundary up to the high boundary must be treated as a suboptimal region, and the remaining region must be treated as an even less good region.
elif optimum < low:
if value >= low and value <= high:
this.m_cached_value_state = HtmlMeterElementValueState.suboptimal
else:
this.m_cached_value_state = HtmlMeterElementValueState.even_less_good
# Finally, if the optimum point is higher than the high boundary, then the situation is reversed; the region between the high boundary and the maximum value must be treated as the optimum region, the region from the high boundary down to the low boundary must be treated as a suboptimal region, and the remaining region must be treated as an even less good region.
else:
if value >= high and value <= max:
this.m_cached_value_state = HtmlMeterElementValueState.optimal
elif value >= low and value <= high:
this.m_cached_value_state = HtmlMeterElementValueState.suboptimal
else:
this.m_cached_value_state = HtmlMeterElementValueState.even_less_good
let meter_value_element = this.m_meter_value_element else return
let position = (value - min) / (max - min) * 100.0
let width = html_string_formatted("{}%", [ak.arg_f64(position)])
css_style_properties_set_property_property_id(dom_element_style_for_bindings(meter_value_element), css_data.PropertyId.width, ak.string_bytes_as_string_view(&width), ak.sv("")) else trap("MUST: set_property")
# mark: Inline functions of HTMLMeterElement.h ==================================================
## Ported from HTMLMeterElement.h:42 Web::HTML::HTMLMeterElement::is_labelable (override).
# https://html.spec.whatwg.org/multipage/forms.html#category-label
pub func html_meter_element_is_labelable_impl(base: HtmlElement*) -> bool:
_ = base
return true
## Ported from HTMLMeterElement.h:45 Web::HTML::HTMLMeterElement::default_role (override).
# https://www.w3.org/TR/html-aria/#el-meter
pub func html_meter_element_default_role_impl(base: AriaMixin*) -> AriaRole?:
_ = base
return AriaRole.meter
## Ported from HTMLMeterElement.h:52 Web::HTML::HTMLMeterElement::value_state.
pub func html_meter_element_value_state(this: HtmlMeterElement*) -> HtmlMeterElementValueState:
return this.m_cached_value_state