dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / html / html_label_element.lucb
#==============================================================================================
#
# html_label_element - HTML::HTMLLabelElement: <label>
#
# DESCRIPTION:
# Port of LibWeb/HTML/HTMLLabelElement.cpp (lines 1-161) and the inline functions of
# HTMLLabelElement.h: construction (which moves the mixin tables to HTMLLabelElement's,
# as C++'s vptrs move), being activated, the activation behavior (a click forwarded to
# the labeled control, then its focusing steps), the labeled control and the form IDL
# attribute.
#
# The activation behavior reads UIEvents::MouseEvent, which is phase 4 (input events);
# its members are closure stubs until then.
#
#==============================================================================================
# mark: Construction ============================================================================
## Ported from HTMLLabelElement.cpp:22 Web::HTML::HTMLLabelElement::HTMLLabelElement.
pub func html_label_element_construct(this: HtmlLabelElement*, document: DomDocument*, qualified_name: DomQualifiedName):
html_element_construct(this.html_element(), document, qualified_name)
# The vptrs of the mixin bases move to HTMLLabelElement's tables.
let element = this.html_element()
element.element.slottable_mixin.vtable = &html_label_element_slottable_mixin_vtable
element.element.aria_mixin.vtable = &html_label_element_aria_mixin_vtable
element.element.animatable.vtable = &html_label_element_animatable_vtable
element.global_event_handlers.vtable = &html_label_element_global_event_handlers_vtable
element.form_associated_element.vtable = &html_label_element_form_associated_element_vtable
## Ported from HTMLLabelElement.cpp:29 Web::HTML::HTMLLabelElement::initialize (override).
pub func html_label_element_initialize_impl(base: JsCell*, realm: Realm*):
web_set_prototype_for_interface(base, realm, BindInterfaceName.html_label_element)
html_element_initialize_impl(base, realm)
# mark: Activation ==============================================================================
## Ported from HTMLLabelElement.cpp:35 Web::HTML::HTMLLabelElement::set_being_activated (override).
pub func html_label_element_set_being_activated_impl(base: DomElement*, activated: bool):
let this = (HtmlLabelElement*)base
dom_element_set_being_activated_impl(base, activated)
if let labeled_control = html_label_element_control(this):
dom_element_set_being_activated(labeled_control.dom_element(), activated)
## Ported from HTMLLabelElement.cpp:42 Web::HTML::HTMLLabelElement::has_activation_behavior (override).
pub func html_label_element_has_activation_behavior_impl(base: DomEventTarget*) -> bool:
return true
## Ported from HTMLLabelElement.cpp:48 Web::HTML::HTMLLabelElement::activation_behavior (override).
# https://html.spec.whatwg.org/multipage/forms.html#the-label-element:activation-behaviour
pub func html_label_element_activation_behavior_impl(base: DomEventTarget*, event: DomEvent*):
let this = (HtmlLabelElement*)base
let document = dom_node_document(this.dom_node())
# The label element's exact default presentation and behavior, in particular what its activation behavior might be,
# if anything, should match the platform's label behavior. The activation behavior of a label element for events
# targeted at interactive content descendants of a label element, and any descendants of those interactive content
# descendants, must be to do nothing.
# AD-HOC: Click and focus the control, matching typical platform behavior.
# This matches the behavior of HTMLElement::click(), but the original event properties are preserved.
if this.m_click_in_progress:
return
let control_element = html_label_element_control(this) else return
# NB: If a click was fired after a drag selection was made on the label element, do not propagate the click event
# to the input element. This allows the user to e.g. copy the label's text.
let event_type = dom_event_type(event)
if ak.fly_string_eq_fly_string(&event_type, &html_event_names_click):
let mouse_event = as_ui_events_mouse_event(event)
let selection = dom_document_get_selection(document)
if ui_events_ui_event_detail(mouse_event.ui_events_ui_event()) == 1:
if let present_selection = selection:
if not selection_selection_is_collapsed(present_selection):
return
if let form_control = as_if_form_associated_element(control_element):
if not html_form_associated_element_enabled(form_control):
return
# (C++ scopes the click in a block whose ScopeGuard unsets the flag on leaving it; the block
# has no early exit, so the flag is unset at its end.)
this.m_click_in_progress = true
let mouse_event = as_ui_events_mouse_event(event)
let click_event = ui_events_mouse_event_clone(mouse_event)
# NB: Ensure layout is up to date before accessing the control's paintable.
dom_document_update_layout(document, DomUpdateLayoutReason.html_label_element_activation_behavior)
# Recompute offsetX/offsetY relative to the control element, since the original values are relative to the label.
if let paintable = dom_node_paintable(control_element.dom_node()):
if let navigable = dom_document_navigable(document):
let scroll_offset = html_navigable_viewport_scroll_offset(navigable)
let page_position = gfx.CssPixelPoint(m_x = gfx.CssPixels.from_f64(ui_events_mouse_event_client_x(mouse_event)).add(scroll_offset.m_x), m_y = gfx.CssPixels.from_f64(ui_events_mouse_event_client_y(mouse_event)).add(scroll_offset.m_y))
let box_position = paint_paintable_box_type_agnostic_position(paintable)
ui_events_mouse_event_set_offset_x(click_event, ak.rounding_round_f64(page_position.m_x.sub(box_position.m_x).to_double()))
ui_events_mouse_event_set_offset_y(click_event, ak.rounding_round_f64(page_position.m_y.sub(box_position.m_y).to_double()))
# UIEvents::MouseEvent derives from DOM::Event first (opaque until phase 4).
let click_dom_event = (DomEvent*)click_event
dom_event_set_bubbles(click_dom_event, true)
dom_event_set_cancelable(click_dom_event, true)
dom_event_set_composed(click_dom_event, true)
dom_event_set_is_trusted(click_dom_event, dom_event_is_trusted(event))
_ = dom_event_target_dispatch_event(control_element.dom_event_target(), click_dom_event)
this.m_click_in_progress = false
if dom_event_target_is_focusable(control_element.dom_event_target()):
html_run_focusing_steps(control_element.dom_node())
# mark: The labeled control =====================================================================
## Ported from HTMLLabelElement.h:20 Web::HTML::HTMLLabelElement::for_.
pub func html_label_element_for(this: HtmlLabelElement*) -> ak.String?:
return dom_element_attribute(this.dom_element(), html_attribute_names_for)
## Ported from HTMLLabelElement.cpp:109 Web::HTML::HTMLLabelElement::control.
# https://html.spec.whatwg.org/multipage/forms.html#labeled-control
pub func html_label_element_control(this: HtmlLabelElement*) -> HtmlElement*?:
var control: HtmlElement*? = none
# The for attribute may be specified to indicate a form control with which the caption is
# to be associated. If the attribute is specified, the attribute's value must be the ID of
# a labelable element in the same tree as the label element. If the attribute is specified
# and there is an element in the tree whose ID is equal to the value of the for attribute,
# and the first such element in tree order is a labelable element, then that element is the
# label element's labeled control.
if html_label_element_for(this) != none:
for node in dom_node_inclusive_subtree(tree_node_root(this.dom_node())):
let element = as_if_html_element(node) else continue
let for_value = html_label_element_for(this) else trap("for_() has a value")
if html_optional_fly_string_eq_optional_string(dom_element_id(element.dom_element()), for_value) and html_element_is_labelable(element):
control = element
break
return control
# If the for attribute is not specified, but the label element has a labelable element descendant,
# then the first such descendant in tree order is the label element's labeled control.
for node in dom_node_subtree(this.dom_node()):
let element = as_if_html_element(node) else continue
if html_element_is_labelable(element):
control = element
break
return control
## Ported from HTMLLabelElement.cpp:144 Web::HTML::HTMLLabelElement::form.
# https://html.spec.whatwg.org/multipage/forms.html#dom-label-form
pub func html_label_element_form(this: HtmlLabelElement*) -> HtmlFormElement*?:
let labeled_control = html_label_element_control(this)
# 1. If the label element has no labeled control, then return null.
let control = labeled_control else return none
# 2. If the label element's labeled control is not a form-associated element, then return null.
let form_associated_element = as_if_form_associated_element(control) else return none
# 3. Return the label element's labeled control's form owner (which can still be null).
return html_form_associated_element_form(form_associated_element)