Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / dom / text.lucb

#==============================================================================================
#
#   text - DOM::Text: text nodes, splitText, wholeText and text directionality
#
#   DESCRIPTION:
#       Port of LibWeb/DOM/Text.cpp (lines 1-177) and the inline functions of Text.h. A Text
#       is CharacterData with the SlottableMixin; CDATASection derives from it.
#
#==============================================================================================

# mark: Construction ============================================================================

## Ported from Text.cpp:21 Web::DOM::Text::Text.
pub func dom_text_construct_utf16_string(this: DomText*, document: DomDocument*, data: ak.Utf16String):
    dom_character_data_construct(this.dom_character_data(), document, DomNodeType.text_node, data)
    # The SlottableMixin base follows the primary base; its vtable is Text's (C++'s vptr of the
    # base subobject, DESIGN.md §2.5) until a derived class's constructor moves it on.
    this.slottable_mixin.vtable = &dom_text_slottable_mixin_vtable

## Ported from Text.cpp:26 Web::DOM::Text::Text.
pub func dom_text_construct_dom_node_type(this: DomText*, document: DomDocument*, type_: DomNodeType, data: ak.Utf16String):
    dom_character_data_construct(this.dom_character_data(), document, type_, data)
    this.slottable_mixin.vtable = &dom_text_slottable_mixin_vtable

## Ported from Text.cpp:31 Web::DOM::Text::initialize (override).
pub func dom_text_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.text)
    dom_character_data_initialize_impl(base, realm)

## Ported from Text.cpp:37 Web::DOM::Text::visit_edges (override).
pub func dom_text_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (DomText*)base
    # Base::visit_edges(visitor): CharacterData inherits Node's.
    dom_node_visit_edges_impl(base, visitor)
    dom_slottable_mixin_visit_edges(&this.slottable_mixin, visitor)

## Ported from Text.cpp:44 Web::DOM::Text::construct_impl.
# https://dom.spec.whatwg.org/#dom-text-text
pub func dom_text_construct_impl(realm: Realm*, data: ak.Utf16String) -> DomText*!:
    # The new Text(data) constructor steps are to set this’s data to data and this’s node document to current global object’s associated Document.
    let window = as_html_window((gc.Cell*)html_current_global_object())
    return realm_create_dom_text_utf16_string(realm, html_window_associated_document(window), data)

# mark: splitText and wholeText =================================================================

## Ported from Text.cpp:53 Web::DOM::Text::split_text.
# https://dom.spec.whatwg.org/#dom-text-splittext
# https://dom.spec.whatwg.org/#concept-text-split
pub func dom_text_split_text(this: DomText*, offset: usize) -> DomText*!:
    let this_node = this.dom_node()
    let realm = bind_platform_object_realm(this.bind_platform_object())

    # 1. Let length be node’s length.
    let length = dom_node_length(this_node)

    # 2. If offset is greater than length, then throw an "IndexSizeError" DOMException.
    if offset > length:
        try idl_throw(.idl_dom_exception(idl_index_size_error_create(realm, ak.utf16_string_from_str("Split offset is greater than length"))))

    # 3. Let count be length minus offset.
    let count = length - offset

    # 4. Let new data be the result of substringing data with node node, offset offset, and count count.
    let new_data = try dom_character_data_substring_data(this.dom_character_data(), offset, count)

    # 5. Let new node be a new Text node, with the same node document as node. Set new node’s data to new data.
    let new_node = realm_create_dom_text_utf16_string(realm, dom_node_document(this_node), new_data)

    # 6. Let parent be node’s parent.
    let parent_node = tree_node_parent(this_node)

    # 7. If parent is not null, then:
    if let parent = parent_node:
        # 1. Insert new node into parent before node’s next sibling.
        dom_node_insert_before(parent, new_node.dom_node(), tree_node_next_sibling(this_node))

        # 2. For each live range whose start node is node and start offset is greater than offset, set its start node
        #    to new node and decrease its start offset by offset.
        for entry in ak.hash_table_begin(dom_range_live_ranges()):
            let range = *entry else continue
            if dom_abstract_range_start_container(range.dom_abstract_range()) == this_node and (usize)dom_abstract_range_start_offset(range.dom_abstract_range()) > offset:
                dom_range_set_start_node(range, new_node.dom_node())
                dom_range_decrease_start_offset(range, (u32)offset)

        # 3. For each live range whose end node is node and end offset is greater than offset, set its end node to new
        #    node and decrease its end offset by offset.
        for entry in ak.hash_table_begin(dom_range_live_ranges()):
            let range = *entry else continue
            if dom_abstract_range_end_container(range.dom_abstract_range()) == this_node and (usize)dom_abstract_range_end_offset(range.dom_abstract_range()) > offset:
                dom_range_set_end_node(range, new_node.dom_node())
                dom_range_decrease_end_offset(range, (u32)offset)

        # 4. For each live range whose start node is parent and start offset is equal to the index of node plus 1,
        #    increase its start offset by 1.
        for entry in ak.hash_table_begin(dom_range_live_ranges()):
            let range = *entry else continue
            if dom_abstract_range_start_container(range.dom_abstract_range()) == parent and (usize)dom_abstract_range_start_offset(range.dom_abstract_range()) == tree_node_index(this_node) + 1:
                dom_range_increase_start_offset(range, 1)

        # 5. For each live range whose end node is parent and end offset is equal to the index of node plus 1, increase
        #    its end offset by 1.
        for entry in ak.hash_table_begin(dom_range_live_ranges()):
            let range = *entry else continue
            if dom_abstract_range_end_container(range.dom_abstract_range()) == parent and (usize)dom_abstract_range_end_offset(range.dom_abstract_range()) == tree_node_index(this_node) + 1:
                dom_range_increase_end_offset(range, 1)

    # 8. Replace data with node node, offset offset, count count, and data the empty string.
    try dom_character_data_replace_data(this.dom_character_data(), offset, count, ak.utf16_view_new())

    # 9. Return new node.
    return new_node

## Ported from Text.cpp:120 Web::DOM::Text::whole_text.
# https://dom.spec.whatwg.org/#dom-text-wholetext
pub func dom_text_whole_text(this: DomText*) -> ak.Utf16String:
    # https://dom.spec.whatwg.org/#contiguous-text-nodes
    # The contiguous Text nodes of a node node are node, node’s previous sibling Text node, if any, and its contiguous
    # Text nodes, and node’s next sibling Text node, if any, and its contiguous Text nodes, avoiding any duplicates.
    var nodes: ak.Vector[DomText*]

    ak.vector_append_t(&nodes, this)

    var current_node = tree_node_previous_sibling(this.dom_node())
    while let current = current_node:
        if not (dom_node_is_text(current) or dom_node_is_cdata_section(current)):
            break
        ak.vector_append_t(&nodes, (DomText*)current)
        current_node = tree_node_previous_sibling(current)

    # Reverse nodes so they are in tree order
    ak.vector_reverse(&nodes)

    current_node = tree_node_next_sibling(this.dom_node())
    while let current = current_node:
        if not (dom_node_is_text(current) or dom_node_is_cdata_section(current)):
            break
        ak.vector_append_t(&nodes, (DomText*)current)
        current_node = tree_node_next_sibling(current)

    var builder = ak.string_builder_new_mode(ak.StringBuilderMode.utf16)
    for text_node in ak.vector_span(&nodes):
        let data = dom_character_data_data(text_node.dom_character_data())
        ak.string_builder_append_utf16_view(&builder, ak.utf16_string_base_utf16_view(&data.utf16_string_base))

    return ak.string_builder_to_utf16_string(&builder)

# mark: Directionality ==========================================================================

## Ported from Text.cpp:152 Web::DOM::Text::directionality.
# https://html.spec.whatwg.org/multipage/dom.html#text-node-directionality
pub func dom_text_directionality(this: DomText*) -> DomElementDirectionality?:
    # 1. If text's data does not contain a code point whose bidirectional character type is L, AL, or R, then return null.
    # 2. Let codePoint be the first code point in text's data whose bidirectional character type is L, AL, or R.
    var found_character_bidi_class: web_unicode.BidiClass? = none
    let data = dom_character_data_data(this.dom_character_data())
    let view = ak.utf16_string_base_utf16_view(&data.utf16_string_base)
    var iterator = ak.utf16_view_begin(&view)
    while not ak.utf16_code_point_iterator_done(&iterator):
        let code_point = ak.utf16_code_point_iterator_deref(&iterator)
        _ = ak.utf16_code_point_iterator_inc(&iterator)
        let bidi_class = web_unicode.bidirectional_class(code_point)
        if bidi_class == web_unicode.BidiClass.left_to_right or bidi_class == web_unicode.BidiClass.right_to_left_arabic or bidi_class == web_unicode.BidiClass.right_to_left:
            found_character_bidi_class = bidi_class
            break
    let found = found_character_bidi_class else return none

    # 3. If codePoint is of bidirectional character type AL or R, then return 'rtl'.
    if found == web_unicode.BidiClass.right_to_left_arabic or found == web_unicode.BidiClass.right_to_left:
        return DomElementDirectionality.rtl

    # 4. If codePoint is of bidirectional character type L, then return 'ltr'.
    # NOTE: codePoint should always be of bidirectional character type L by this point, so we can just return 'ltr' here.
    assert(found == web_unicode.BidiClass.left_to_right)
    return DomElementDirectionality.ltr

# mark: Text.h ==================================================================================

## Ported from Text.h:29 Web::DOM::Text::node_name (override).
pub func dom_text_node_name_impl(base: DomNode*) -> ak.FlyString:
    return ak.fly_string_from_str("#text")

## Ported from Text.h:31 Web::DOM::Text::slottable_as_node (override).
pub func dom_text_slottable_as_node_impl(base: DomSlottableMixin*) -> DomNode*:
    return ((DomText*)((u8*)base - memory.offset_of(DomText, slottable_mixin))).dom_node()

## Ported from Text.h:33 Web::DOM::Text::max_length.
pub func dom_text_max_length(this: DomText*) -> u64?:
    return this.m_max_length

## Ported from Text.h:34 Web::DOM::Text::set_max_length.
pub func dom_text_set_max_length(this: DomText*, max_length: u64?):
    this.m_max_length = max_length

## Ported from Text.h:39 Web::DOM::Text::is_password_input.
pub func dom_text_is_password_input(this: DomText*) -> bool:
    return this.m_is_password_input

## Ported from Text.h:40 Web::DOM::Text::set_is_password_input.
pub func dom_text_set_is_password_input(this: DomText*, b: bool):
    this.m_is_password_input = b