dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / dom / selectionchange_event_dispatching.lucb
#==============================================================================================
#
# selectionchange_event_dispatching - scheduling and firing selectionchange events
#
# DESCRIPTION:
# Port of LibWeb/DOM/SelectionchangeEventDispatching.h (lines 1-61). The C++ functions are
# templates over a SelectionChangeTarget: a Document, an HTMLInputElement or an
# HTMLTextAreaElement, each with its own "has scheduled selectionchange event" flag. A
# Luce generic body cannot reach those members, so the functions take the target node
# and dom_selection_change_target_* reach the flag of whichever of the three it is.
#
#==============================================================================================
## The text control mixin of an HTMLInputElement or HTMLTextAreaElement target.
func dom_selection_change_target_text_control(target: DomNode*) -> HtmlFormAssociatedTextControlElement*:
if let input = as_if_html_input_element(target):
return &input.form_associated_text_control_element
if let text_area = as_if_html_text_area_element(target):
return &text_area.form_associated_text_control_element
trap("VERIFY: a SelectionChangeTarget is a Document, an HTMLInputElement or an HTMLTextAreaElement")
## `target.has_scheduled_selectionchange_event()`.
func dom_selection_change_target_has_scheduled(target: DomNode*) -> bool:
if let document = as_if_dom_document(target):
return dom_document_has_scheduled_selectionchange_event(document)
return html_form_associated_text_control_element_has_scheduled_selectionchange_event(dom_selection_change_target_text_control(target))
## `target.set_scheduled_selectionchange_event(value)`.
func dom_selection_change_target_set_scheduled(target: DomNode*, value: bool):
if let document = as_if_dom_document(target):
dom_document_set_scheduled_selectionchange_event(document, value)
return
html_form_associated_text_control_element_set_scheduled_selectionchange_event(dom_selection_change_target_text_control(target), value)
## The captures of schedule_a_selectionchange_event's task (C++ captures both by reference).
struct DomScheduleASelectionchangeEventTask:
var target: DomNode*
var document: DomDocument*
## schedule_a_selectionchange_event's task: fire a selectionchange event on target.
func dom_schedule_a_selectionchange_event_task(context: void*?):
let captures = (DomScheduleASelectionchangeEventTask*)(context else trap("no captures"))
dom_fire_a_selectionchange_event(captures.target, captures.document)
## Ported from SelectionchangeEventDispatching.h:25 Web::DOM::schedule_a_selectionchange_event.
# https://w3c.github.io/selection-api/#scheduling-selectionchange-event
pub func dom_schedule_a_selectionchange_event(target: DomNode*, document: DomDocument*):
# 1. If target's has scheduled selectionchange event is true, abort these steps.
if dom_selection_change_target_has_scheduled(target):
return
# AD-HOC: See https://github.com/w3c/selection-api/issues/338
# Set target's has scheduled selectionchange event to true
dom_selection_change_target_set_scheduled(target, true)
# 2. Queue a task on the user interaction task source to fire a selectionchange event on
# target.
let captures = new DomScheduleASelectionchangeEventTask(target = target, document = document) else trap("out of memory")
let steps = gc.create_function0(gc.cell_heap(document.cell()), ak.Function0[unit](call = dom_schedule_a_selectionchange_event_task, context = captures))
_ = html_queue_global_task(HtmlTaskSource.user_interaction, html_relevant_global_object((JsObject*)document), steps)
## Ported from SelectionchangeEventDispatching.h:44 Web::DOM::fire_a_selectionchange_event.
# https://w3c.github.io/selection-api/#firing-selectionchange-event
pub func dom_fire_a_selectionchange_event(target: DomNode*, document: DomDocument*):
# 1. Set target's has scheduled selectionchange event to false.
dom_selection_change_target_set_scheduled(target, false)
# 2. If target is an element, fire an event named selectionchange, which bubbles and not
# cancelable, at target.
# 3. Otherwise, if target is a document, fire an event named selectionchange, which does not
# bubble and not cancelable, at target.
var event_init = DomEventInit()
event_init.bubbles = is_dom_element(target)
event_init.cancelable = false
let event = dom_event_create(bind_platform_object_realm(document.bind_platform_object()), html_event_names_selectionchange, event_init)
_ = dom_event_target_dispatch_event(target.dom_event_target(), event)