dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / dom / editing_host_manager.lucb
#==============================================================================================
#
# editing_host_manager - DOM::EditingHostManager: input into contenteditable editing hosts
#
# DESCRIPTION:
# Port of LibWeb/DOM/EditingHostManager.cpp (lines 1-220) and the inline functions of
# EditingHostManager.h. It is the InputEventsTarget of a document's editing hosts: text
# input goes through the document's execCommand, caret movement through its Selection.
#
#==============================================================================================
# mark: Construction ============================================================================
## Ported from EditingHostManager.cpp:20 Web::DOM::EditingHostManager::create.
pub func dom_editing_host_manager_create(realm: Realm*, document: DomDocument*) -> DomEditingHostManager*:
return realm_create_dom_editing_host_manager(realm, document)
## Ported from EditingHostManager.cpp:25 Web::DOM::EditingHostManager::EditingHostManager.
pub func dom_editing_host_manager_construct(this: DomEditingHostManager*, document: DomDocument*):
# The InputEventsTarget base subobject's vtable is EditingHostManager's (DESIGN.md ยง2.5).
this.input_events_target.vtable = &dom_editing_host_manager_input_events_target_vtable
this.m_document = document
## Ported from EditingHostManager.cpp:30 Web::DOM::EditingHostManager::visit_edges (override).
pub func dom_editing_host_manager_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (DomEditingHostManager*)base
# Base::visit_edges(visitor): JS::Cell's visits nothing.
gc.cell_visitor_visit_t_2(visitor, this.m_document)
gc.cell_visitor_visit_t(visitor, this.m_active_contenteditable_element)
## The EditingHostManager an InputEventsTarget override receives (C++'s `this` adjustment).
func dom_editing_host_manager_from_input_events_target(base: InputEventsTarget*) -> DomEditingHostManager*:
return (DomEditingHostManager*)((u8*)base - memory.offset_of(DomEditingHostManager, input_events_target))
## `dbgln("<function>(): editing resulted in exception: {}", editing_result.exception())`.
func dom_editing_host_manager_log_exception(function: str):
let exception = idl_exception_to_string(idl_take_pending_exception())
let exception_view = ak.string_bytes_as_string_view(&exception)
ak.dbgln(f"{function}(): editing resulted in exception: {ak.string_view_text(exception_view)}")
# mark: Input ===================================================================================
## Ported from EditingHostManager.cpp:37 Web::DOM::EditingHostManager::handle_insert (override).
pub func dom_editing_host_manager_handle_insert_impl(base: InputEventsTarget*, input_type: ak.FlyString, value: ak.Utf16String):
let this = dom_editing_host_manager_from_input_events_target(base)
# https://w3c.github.io/editing/docs/execCommand/#additional-requirements
# When the user instructs the user agent to insert text inside an editing host, such as by typing on the keyboard
# while the cursor is in an editable node, the user agent must call execCommand("inserttext", false, value) on the
# relevant document, with value equal to the text the user provided. If the user inserts multiple characters at
# once or in quick succession, this specification does not define whether it is treated as one insertion or several
# consecutive insertions.
_ = dom_document_exec_command(this.m_document, editing_command_names_insert_text, false, value) catch:
dom_editing_host_manager_log_exception("handle_insert")
recover false
## Ported from EditingHostManager.cpp:50 Web::DOM::EditingHostManager::select_all (override).
pub func dom_editing_host_manager_select_all_impl(base: InputEventsTarget*):
let this = dom_editing_host_manager_from_input_events_target(base)
if this.m_active_contenteditable_element == none:
return
let selection = dom_document_get_selection(this.m_document) else trap("VERIFY: selection")
let anchor_node = selection_selection_anchor_node(selection) else return
let focus_node = selection_selection_focus_node(selection) else return
selection_selection_set_base_and_extent(selection, anchor_node, 0, focus_node, (u32)dom_node_length(focus_node)) else trap("MUST: set_base_and_extent")
## Ported from EditingHostManager.cpp:62 Web::DOM::EditingHostManager::set_selection_anchor (override).
pub func dom_editing_host_manager_set_selection_anchor_impl(base: InputEventsTarget*, anchor_node: DomNode*, anchor_offset: usize):
let this = dom_editing_host_manager_from_input_events_target(base)
let selection = dom_document_get_selection(this.m_document) else trap("VERIFY: selection")
selection_selection_collapse(selection, anchor_node, (u32)anchor_offset) else trap("MUST: collapse")
dom_document_reset_cursor_blink_cycle(this.m_document)
## Ported from EditingHostManager.cpp:69 Web::DOM::EditingHostManager::set_selection_focus (override).
pub func dom_editing_host_manager_set_selection_focus_impl(base: InputEventsTarget*, focus_node: DomNode*, focus_offset: usize):
let this = dom_editing_host_manager_from_input_events_target(base)
let active = this.m_active_contenteditable_element else return
if not tree_node_is_ancestor_of(active, focus_node):
return
let selection = dom_document_get_selection(this.m_document) else trap("VERIFY: selection")
let anchor_node = selection_selection_anchor_node(selection) else return
selection_selection_set_base_and_extent(selection, anchor_node, selection_selection_anchor_offset(selection), focus_node, (u32)focus_offset) else trap("MUST: set_base_and_extent")
dom_document_reset_cursor_blink_cycle(this.m_document)
## Ported from EditingHostManager.cpp:80 Web::DOM::EditingHostManager::get_selection_for_navigation.
func dom_editing_host_manager_get_selection_for_navigation(this: DomEditingHostManager*, collapse: InputEventsTargetCollapseSelection) -> SelectionSelection*?:
# In order for navigation to happen inside an editing host, the document must have a selection,
let selection = dom_document_get_selection(this.m_document) else return none
# and the focus node must be inside a text node,
let focus = selection_selection_focus_node(selection) else return none
if not is_dom_text(focus):
return none
# and if we're performing collapsed navigation (i.e. moving the caret), the focus node must be editable.
if collapse == InputEventsTargetCollapseSelection.yes and not dom_node_is_editable(focus):
return none
return selection
# mark: Caret movement ==========================================================================
## Ported from EditingHostManager.cpp:99 Web::DOM::EditingHostManager::move_cursor_to_start (override).
pub func dom_editing_host_manager_move_cursor_to_start_impl(base: InputEventsTarget*, collapse: InputEventsTargetCollapseSelection):
let this = dom_editing_host_manager_from_input_events_target(base)
let selection = dom_editing_host_manager_get_selection_for_navigation(this, collapse) else return
let node = selection_selection_focus_node(selection)
if collapse == InputEventsTargetCollapseSelection.yes:
selection_selection_collapse(selection, node, 0) else trap("MUST: collapse")
dom_document_reset_cursor_blink_cycle(this.m_document)
else:
let anchor_node = selection_selection_anchor_node(selection) else trap("VERIFY: anchor node")
selection_selection_set_base_and_extent(selection, anchor_node, selection_selection_anchor_offset(selection), node else trap("VERIFY: focus node"), 0) else trap("MUST: set_base_and_extent")
selection_selection_scroll_focus_into_view(selection)
## Ported from EditingHostManager.cpp:115 Web::DOM::EditingHostManager::move_cursor_to_end (override).
pub func dom_editing_host_manager_move_cursor_to_end_impl(base: InputEventsTarget*, collapse: InputEventsTargetCollapseSelection):
let this = dom_editing_host_manager_from_input_events_target(base)
let selection = dom_editing_host_manager_get_selection_for_navigation(this, collapse) else return
let node = selection_selection_focus_node(selection) else trap("VERIFY: focus node")
if collapse == InputEventsTargetCollapseSelection.yes:
dom_document_reset_cursor_blink_cycle(this.m_document)
selection_selection_collapse(selection, node, (u32)dom_node_length(node)) else trap("MUST: collapse")
else:
let anchor_node = selection_selection_anchor_node(selection) else trap("VERIFY: anchor node")
selection_selection_set_base_and_extent(selection, anchor_node, selection_selection_anchor_offset(selection), node, (u32)dom_node_length(node)) else trap("MUST: set_base_and_extent")
selection_selection_scroll_focus_into_view(selection)
## Ported from EditingHostManager.cpp:131 Web::DOM::EditingHostManager::increment_cursor_position_offset (override).
pub func dom_editing_host_manager_increment_cursor_position_offset_impl(base: InputEventsTarget*, collapse: InputEventsTargetCollapseSelection):
let this = dom_editing_host_manager_from_input_events_target(base)
let selection = dom_editing_host_manager_get_selection_for_navigation(this, collapse) else return
selection_selection_move_offset_to_next_character(selection, collapse == InputEventsTargetCollapseSelection.yes)
## Ported from EditingHostManager.cpp:139 Web::DOM::EditingHostManager::decrement_cursor_position_offset (override).
pub func dom_editing_host_manager_decrement_cursor_position_offset_impl(base: InputEventsTarget*, collapse: InputEventsTargetCollapseSelection):
let this = dom_editing_host_manager_from_input_events_target(base)
let selection = dom_editing_host_manager_get_selection_for_navigation(this, collapse) else return
selection_selection_move_offset_to_previous_character(selection, collapse == InputEventsTargetCollapseSelection.yes)
## Ported from EditingHostManager.cpp:147 Web::DOM::EditingHostManager::increment_cursor_position_to_next_word (override).
pub func dom_editing_host_manager_increment_cursor_position_to_next_word_impl(base: InputEventsTarget*, collapse: InputEventsTargetCollapseSelection):
let this = dom_editing_host_manager_from_input_events_target(base)
let selection = dom_editing_host_manager_get_selection_for_navigation(this, collapse) else return
selection_selection_move_offset_to_next_word(selection, collapse == InputEventsTargetCollapseSelection.yes)
## Ported from EditingHostManager.cpp:155 Web::DOM::EditingHostManager::decrement_cursor_position_to_previous_word (override).
pub func dom_editing_host_manager_decrement_cursor_position_to_previous_word_impl(base: InputEventsTarget*, collapse: InputEventsTargetCollapseSelection):
let this = dom_editing_host_manager_from_input_events_target(base)
let selection = dom_editing_host_manager_get_selection_for_navigation(this, collapse) else return
selection_selection_move_offset_to_previous_word(selection, collapse == InputEventsTargetCollapseSelection.yes)
## Ported from EditingHostManager.cpp:163 Web::DOM::EditingHostManager::increment_cursor_position_to_next_line (override).
pub func dom_editing_host_manager_increment_cursor_position_to_next_line_impl(base: InputEventsTarget*, collapse: InputEventsTargetCollapseSelection):
let this = dom_editing_host_manager_from_input_events_target(base)
if let selection = dom_document_get_selection(this.m_document):
selection_selection_move_offset_to_next_line(selection, collapse == InputEventsTargetCollapseSelection.yes)
## Ported from EditingHostManager.cpp:169 Web::DOM::EditingHostManager::decrement_cursor_position_to_previous_line (override).
pub func dom_editing_host_manager_decrement_cursor_position_to_previous_line_impl(base: InputEventsTarget*, collapse: InputEventsTargetCollapseSelection):
let this = dom_editing_host_manager_from_input_events_target(base)
if let selection = dom_document_get_selection(this.m_document):
selection_selection_move_offset_to_previous_line(selection, collapse == InputEventsTargetCollapseSelection.yes)
# mark: Deleting and line breaks ================================================================
## Ported from EditingHostManager.cpp:175 Web::DOM::EditingHostManager::handle_delete (override).
pub func dom_editing_host_manager_handle_delete_impl(base: InputEventsTarget*, input_type: ak.FlyString):
let this = dom_editing_host_manager_from_input_events_target(base)
# https://w3c.github.io/editing/docs/execCommand/#additional-requirements
# When the user instructs the user agent to delete the previous character inside an editing host, such as by
# pressing the Backspace key while the cursor is in an editable node, the user agent must call
# execCommand("delete") on the relevant document.
# When the user instructs the user agent to delete the next character inside an editing host, such as by pressing
# the Delete key while the cursor is in an editable node, the user agent must call execCommand("forwarddelete") on
# the relevant document.
let command = editing_command_names_delete if ak.fly_string_eq_fly_string(&input_type, &ui_events_input_types_delete_content_backward) else editing_command_names_forward_delete
_ = dom_document_exec_command(this.m_document, command, false, ak.utf16_string_new()) catch:
dom_editing_host_manager_log_exception("handle_delete")
recover false
## Ported from EditingHostManager.cpp:190 Web::DOM::EditingHostManager::handle_return_key (override).
pub func dom_editing_host_manager_handle_return_key_impl(base: InputEventsTarget*, ui_input_type: ak.FlyString) -> EventResult:
let this = dom_editing_host_manager_from_input_events_target(base)
assert(ak.fly_string_eq_fly_string(&ui_input_type, &ui_events_input_types_insert_paragraph) or ak.fly_string_eq_fly_string(&ui_input_type, &ui_events_input_types_insert_line_break))
# https://w3c.github.io/editing/docs/execCommand/#additional-requirements
# When the user instructs the user agent to insert a line break inside an editing host, such as by pressing the
# Enter key while the cursor is in an editable node, the user agent must call execCommand("insertparagraph") on the
# relevant document.
# When the user instructs the user agent to insert a line break inside an editing host without breaking out of the
# current block, such as by pressing Shift-Enter or Option-Enter while the cursor is in an editable node, the user
# agent must call execCommand("insertlinebreak") on the relevant document.
let command = editing_command_names_insert_paragraph if ak.fly_string_eq_fly_string(&ui_input_type, &ui_events_input_types_insert_paragraph) else editing_command_names_insert_line_break
let editing_result = dom_document_exec_command(this.m_document, command, false, ak.utf16_string_new()) catch:
dom_editing_host_manager_log_exception("handle_return_key")
return EventResult.dropped
return EventResult.handled if editing_result else EventResult.dropped
## The predicate of is_within_active_contenteditable: `&it == active`.
func dom_editing_host_manager_is_active(context: void*?, it: DomNode*) -> bool:
let active = (DomNode*)(context else trap("no node"))
return it == active
## Ported from EditingHostManager.cpp:212 Web::DOM::EditingHostManager::is_within_active_contenteditable.
pub func dom_editing_host_manager_is_within_active_contenteditable(this: DomEditingHostManager*, node: DomNode*) -> bool:
let active = this.m_active_contenteditable_element else return false
return dom_node_find_in_shadow_including_ancestry(node, ak.Function1[DomNode*, bool](call = dom_editing_host_manager_is_active, context = active)) != none
# mark: EditingHostManager.h ====================================================================
## Ported from EditingHostManager.h:42 Web::DOM::EditingHostManager::set_active_contenteditable_element.
pub func dom_editing_host_manager_set_active_contenteditable_element(this: DomEditingHostManager*, element: DomNode*?):
this.m_active_contenteditable_element = element
## Ported from EditingHostManager.h:50 Web::DOM::EditingHostManager::as_cell (override).
pub func dom_editing_host_manager_as_cell_impl(base: InputEventsTarget*) -> JsCell*:
return dom_editing_host_manager_from_input_events_target(base).js_cell()