Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / html / html_source_element.lucb

#==============================================================================================
#
#   html_source_element - HTML::HTMLSourceElement: <source>
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/HTMLSourceElement.cpp (lines 1-122): the constructor (which moves
#       the mixin tables to HTMLSourceElement's, as C++'s vptrs move), initialize, and the
#       insertion, moving, removing and attribute change steps, which run a media element's
#       resource selection or count as relevant mutations of a picture's img children.
#
#==============================================================================================

## Ported from HTMLSourceElement.cpp:19 Web::HTML::HTMLSourceElement::HTMLSourceElement.
pub func html_source_element_construct(this: HtmlSourceElement*, document: DomDocument*, qualified_name: DomQualifiedName):
    html_element_construct(this.html_element(), document, qualified_name)
    # The vptrs of the mixin bases move to HTMLSourceElement's tables.
    let element = this.html_element()
    element.element.slottable_mixin.vtable = &html_source_element_slottable_mixin_vtable
    element.element.aria_mixin.vtable = &html_source_element_aria_mixin_vtable
    element.element.animatable.vtable = &html_source_element_animatable_vtable
    element.global_event_handlers.vtable = &html_source_element_global_event_handlers_vtable
    element.form_associated_element.vtable = &html_source_element_form_associated_element_vtable

## Ported from HTMLSourceElement.cpp:26 Web::HTML::HTMLSourceElement::initialize (override).
pub func html_source_element_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.html_source_element)
    html_element_initialize_impl(base, realm)

## Ported from HTMLSourceElement.cpp:32 Web::HTML::update_image_children_of_picture.
func html_update_image_children_of_picture(picture: DomNode*):
    for child in dom_node_children(picture):
        if let img = as_if_html_image_element(child):
            html_image_element_update_the_image_data(img, true)

## Ported from HTMLSourceElement.cpp:41 Web::HTML::HTMLSourceElement::inserted (override).
# https://html.spec.whatwg.org/multipage/embedded-content.html#the-source-element:html-element-insertion-steps
pub func html_source_element_inserted_impl(base: DomNode*):
    # The source HTML element insertion steps, given insertedNode, are:
    html_element_inserted_impl(base)

    # 1. Let parent be insertedNode's parent.
    let parent = tree_node_parent(base) else return

    # 2. If parent is a media element that has no src attribute and whose networkState has the value NETWORK_EMPTY,
    #    then invoke that media element's resource selection algorithm.
    if let media_element = as_if_html_media_element(parent):
        if not dom_element_has_attribute(media_element.dom_element(), html_attribute_names_src) and html_media_element_network_state(media_element) == HtmlMediaElementNetworkState.empty:
            html_media_element_select_resource(media_element)

    # 3. If parent is a picture element, then for each child of parent's children, if child is an img element, then
    #    count this as a relevant mutation for child.
    if is_html_picture_element(parent):
        html_update_image_children_of_picture(parent)

## Ported from HTMLSourceElement.cpp:64 Web::HTML::HTMLSourceElement::moved_from (override).
# https://html.spec.whatwg.org/multipage/embedded-content.html#the-source-element:the-source-element-17
pub func html_source_element_moved_from_impl(base: DomNode*, is_subtree_root: DomNodeIsSubtreeRoot, old_ancestor: DomNode*?):
    # The source HTML element moving steps, given movedNode, isSubtreeRoot, and oldAncestor are:
    html_element_moved_from_impl(base, is_subtree_root, old_ancestor)

    # 1. If isSubtreeRoot is true and oldAncestor is a picture element, then for each child of oldAncestor's
    #    children: if child is an img element, then count this as a relevant mutation for child.
    if is_subtree_root == DomNodeIsSubtreeRoot.yes:
        if let ancestor = old_ancestor:
            if is_html_picture_element(ancestor):
                html_update_image_children_of_picture(ancestor)

    # The img element may also have moved into a (new) picture parent; the "picture's children
    # changed" mutation covers that for the new ancestor too.
    if let parent = tree_node_parent(base):
        if is_html_picture_element(parent):
            html_update_image_children_of_picture(parent)

## Ported from HTMLSourceElement.cpp:83 Web::HTML::HTMLSourceElement::removed_from (override).
# https://html.spec.whatwg.org/multipage/embedded-content.html#the-source-element:the-source-element-18
pub func html_source_element_removed_from_impl(base: DomNode*, is_subtree_root: DomNodeIsSubtreeRoot, old_ancestor: DomNode*?, old_root: DomNode*):
    # The source HTML element removing steps, given removedNode, isSubtreeRoot, and oldAncestor are:
    html_element_removed_from_impl(base, is_subtree_root, old_ancestor, old_root)

    # 1. If isSubtreeRoot is true and oldAncestor is a picture element, then for each child of oldAncestor's
    #    children: if child is an img element, then count this as a relevant mutation for child.
    if is_subtree_root == DomNodeIsSubtreeRoot.yes:
        if let ancestor = old_ancestor:
            if is_html_picture_element(ancestor):
                html_update_image_children_of_picture(ancestor)

## Ported from HTMLSourceElement.cpp:99 Web::HTML::HTMLSourceElement::attribute_changed (override).
# https://html.spec.whatwg.org/multipage/images.html#relevant-mutations
# "The element's parent is a picture element and a source element that is a previous sibling has
#  its srcset, sizes, media, type, width or height attributes set, changed, or removed."
pub func html_source_element_attribute_changed_impl(base: DomElement*, name: ak.FlyString, old_value: ak.String?, value: ak.String?, namespace_: ak.FlyString?):
    html_element_attribute_changed_impl(base, name, old_value, value, namespace_)

    let names: ak.FlyString[6] = [html_attribute_names_srcset, html_attribute_names_sizes, html_attribute_names_media, html_attribute_names_type, html_attribute_names_width, html_attribute_names_height]
    var is_one_of = false
    for candidate in names:
        if ak.fly_string_eq_fly_string(&name, &candidate):
            is_one_of = true
    if not is_one_of:
        return

    let parent = tree_node_parent(base.dom_node()) else return
    if not is_html_picture_element(parent):
        return

    # Only following img siblings consider this source a "previous sibling".
    var sibling = tree_node_next_sibling(base.dom_node())
    while let node = sibling:
        if let img = as_if_html_image_element(node):
            html_image_element_update_the_image_data(img, true)
        sibling = tree_node_next_sibling(node)