Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / svg / svg_list.lucb

#==============================================================================================
#
#   svg_list - SVG::SVGList<T>: the list interface of the SVG DOM
#
#   DESCRIPTION:
#       Port of LibWeb/SVG/SVGList.cpp (lines 1-212) with the inline members of SVGList.h: the
#       operations of https://www.w3.org/TR/SVG2/types.html#TermListInterface, generic over
#       the item type as the C++ template is (instantiated for SVGLength, SVGNumber and
#       SVGTransform).
#
#       visit_edges takes the function that visits one item: a generic body cannot visit a
#       `T` (GC::Ref<T>) without knowing it is a cell, so each list passes its item visitor.
#
#==============================================================================================

## Ported from Libraries/LibWeb/SVG/SVGList.cpp:15 Web::SVG::SVGList::SVGList<T>.
pub func svg_list_construct_vector[T](this: SvgList[T]*, realm: Realm*, items: ak.Vector[T], read_only: SvgReadOnlyList):
    *this = SvgList[T](m_realm = realm, m_items = items, m_read_only = read_only)

## Ported from Libraries/LibWeb/SVG/SVGList.cpp:23 Web::SVG::SVGList::SVGList<T>.
pub func svg_list_construct_svg_read_only_list[T](this: SvgList[T]*, realm: Realm*, read_only: SvgReadOnlyList):
    *this = SvgList[T](m_realm = realm, m_items = ak.Vector[T](), m_read_only = read_only)

## Ported from Libraries/LibWeb/SVG/SVGList.cpp:30 Web::SVG::SVGList::visit_edges.
pub func svg_list_visit_edges[T](this: SvgList[T]*, visitor: gc.CellVisitor*, visit_item: func(gc.CellVisitor*, T) -> unit):
    gc.cell_visitor_visit_t_2(visitor, this.m_realm)
    for item in ak.vector_span(&this.m_items):
        visit_item(visitor, item)

## Ported from Libraries/LibWeb/SVG/SVGList.cpp:38 Web::SVG::SVGList::length.
# https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__length
pub func svg_list_length[T](this: const SvgList[T]*) -> u32:
    # The length and numberOfItems IDL attributes represents the length of the list, and on getting simply return the
    # length of the list.
    return (u32)ak.vector_size(&this.m_items)

## Ported from Libraries/LibWeb/SVG/SVGList.h:28 Web::SVG::SVGList::number_of_items.
pub func svg_list_number_of_items[T](this: const SvgList[T]*) -> u32:
    return svg_list_length(this)

## `WebIDL::NoModificationAllowedError::create(m_realm, "Cannot modify a read-only list"_utf16)`, thrown.
func svg_list_throw_read_only[T](this: SvgList[T]*) -> never!:
    return try idl_throw(.idl_dom_exception(idl_no_modification_allowed_error_create(this.m_realm, ak.utf16_string_from_str("Cannot modify a read-only list"))))

## `WebIDL::IndexSizeError::create(m_realm, "List index out of bounds"_utf16)`, thrown.
func svg_list_throw_index_size[T](this: SvgList[T]*) -> never!:
    return try idl_throw(.idl_dom_exception(idl_index_size_error_create(this.m_realm, ak.utf16_string_from_str("List index out of bounds"))))

## Ported from Libraries/LibWeb/SVG/SVGList.cpp:47 Web::SVG::SVGList::clear.
# https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__clear
pub func svg_list_clear[T](this: SvgList[T]*) -> !:
    # 1. If the list is read only, then throw a NoModificationAllowedError.
    if this.m_read_only == SvgReadOnlyList.yes:
        return try svg_list_throw_read_only(this)

    # 2. Detach and then remove all elements in the list.
    # FIXME: Detach items.
    ak.vector_clear(&this.m_items)

    # FIXME: 3. If the list reflects an attribute, or represents the base value of an object that reflects an attribute, then
    #    reserialize the reflected attribute.

    return

## Ported from Libraries/LibWeb/SVG/SVGList.cpp:65 Web::SVG::SVGList::initialize_.
# https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__initialize
pub func svg_list_initialize[T](this: SvgList[T]*, new_item: T) -> T!:
    # 1. If the list is read only, then throw a NoModificationAllowedError.
    if this.m_read_only == SvgReadOnlyList.yes:
        return try svg_list_throw_read_only(this)

    # 2. Detach and then remove all elements in the list.
    # FIXME: Detach items.
    ak.vector_clear(&this.m_items)

    # FIXME: 3. If newItem is an object type, and newItem is not a detached object, then set newItem to be a newly created
    #    object of the same type as newItem and which has the same (number or length) value.

    # FIXME: 4. Attach newItem to the list interface object.

    # 5. Append newItem to this list.
    ak.vector_append_t(&this.m_items, new_item)

    # FIXME: 6. If the list reflects an attribute, or represents the base value of an object that reflects an attribute, then
    #    reserialize the reflected attribute.

    # 7. Return newItem.
    return new_item

## Ported from Libraries/LibWeb/SVG/SVGList.cpp:92 Web::SVG::SVGList::get_item.
# https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__getItem
pub func svg_list_get_item[T](this: SvgList[T]*, index: u32) -> T!:
    # 1. If index is greater than or equal to the length of the list, then throw an IndexSizeError.
    if (usize)index >= ak.vector_size(&this.m_items):
        return try svg_list_throw_index_size(this)

    # 2. Return the element in the list at position index.
    return *ak.vector_at(&this.m_items, (usize)index)

## Ported from Libraries/LibWeb/SVG/SVGList.cpp:104 Web::SVG::SVGList::insert_item_before.
# https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__insertItemBefore
pub func svg_list_insert_item_before[T](this: SvgList[T]*, new_item: T, index: u32) -> T!:
    # 1. If the list is read only, then throw a NoModificationAllowedError.
    if this.m_read_only == SvgReadOnlyList.yes:
        return try svg_list_throw_read_only(this)

    # FIXME: 2. If newItem is an object type, and newItem is not a detached object, then set newItem to be a newly created
    #    object of the same type as newItem and which has the same (number or length) value.

    # 3. If index is greater than the length of the list, then set index to be the list length.
    var used_index = index
    if (usize)used_index > ak.vector_size(&this.m_items):
        used_index = (u32)ak.vector_size(&this.m_items)

    # 4. Insert newItem into the list at index index.
    ak.vector_insert(&this.m_items, (usize)used_index, new_item)

    # FIXME: 5. Attach newItem to the list interface object.

    # FIXME: 6. If the list reflects an attribute, or represents the base value of an object that reflects an attribute, then
    #    reserialize the reflected attribute.

    # 7. Return newItem.
    return new_item

## Ported from Libraries/LibWeb/SVG/SVGList.cpp:131 Web::SVG::SVGList::replace_item.
# https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__replaceItem
pub func svg_list_replace_item[T](this: SvgList[T]*, new_item: T, index: u32) -> T!:
    # 1. If the list is read only, then throw a NoModificationAllowedError.
    if this.m_read_only == SvgReadOnlyList.yes:
        return try svg_list_throw_read_only(this)

    # 2. If index is greater than or equal to the length of the list, then throw an IndexSizeError.
    if (usize)index >= ak.vector_size(&this.m_items):
        return try svg_list_throw_index_size(this)

    # FIXME: 3. If newItem is an object type, and newItem is not a detached object, then set newItem to be a newly created
    #    object of the same type as newItem and which has the same (number or length) value.

    # FIXME: 4. Detach the element in the list at index index.

    # 5. Replace the element in the list at index index with newItem.
    *ak.vector_at(&this.m_items, (usize)index) = new_item

    # FIXME: 6. Attach newItem to the list interface object.

    # FIXME: 7. If the list reflects an attribute, or represents the base value of an object that reflects an attribute, then
    #    reserialize the reflected attribute.

    # 8. Return newItem.
    return new_item

## Ported from Libraries/LibWeb/SVG/SVGList.cpp:160 Web::SVG::SVGList::remove_item.
# https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__removeItem
pub func svg_list_remove_item[T](this: SvgList[T]*, index: u32) -> T!:
    # 1. If the list is read only, then throw a NoModificationAllowedError.
    if this.m_read_only == SvgReadOnlyList.yes:
        return try svg_list_throw_read_only(this)

    # 2. If index is greater than or equal to the length of the list, then throw an IndexSizeError with code.
    if (usize)index >= ak.vector_size(&this.m_items):
        return try svg_list_throw_index_size(this)

    # 3. Let item be the list element at index index.
    let item = *ak.vector_at(&this.m_items, (usize)index)

    # FIXME: 4. Detach item.

    # 5. Remove the list element at index index.
    ak.vector_remove(&this.m_items, (usize)index)

    # 6. Return item.
    return item

## Ported from Libraries/LibWeb/SVG/SVGList.cpp:184 Web::SVG::SVGList::append_item.
# https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__appendItem
pub func svg_list_append_item[T](this: SvgList[T]*, new_item: T) -> T!:
    # 1. If the list is read only, then throw a NoModificationAllowedError.
    if this.m_read_only == SvgReadOnlyList.yes:
        return try svg_list_throw_read_only(this)

    # FIXME: 2. If newItem is an object type, and newItem is not a detached object, then set newItem to be a newly created
    #    object of the same type as newItem and which has the same (number or length) value.

    # 3. Let index be the length of the list.
    # AD-HOC: No, this is unused.

    # 4. Append newItem to the end of the list.
    ak.vector_append_t(&this.m_items, new_item)

    # FIXME: 5. Attach newItem to the list interface object.

    # FIXME: 6. If the list reflects an attribute, or represents the base value of an object that reflects an attribute, then
    #    reserialize the reflected attribute.

    # 7. Return newItem.
    return new_item

## Ported from Libraries/LibWeb/SVG/SVGList.h:38 Web::SVG::SVGList::items.
pub func svg_list_items[T](this: SvgList[T]*) -> const T[]:
    return ak.vector_span_const(&this.m_items)

## Ported from Libraries/LibWeb/SVG/SVGList.h:46 Web::SVG::SVGList::read_only.
pub func svg_list_read_only[T](this: const SvgList[T]*) -> SvgReadOnlyList:
    return this.m_read_only