dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / html / html_object_element_1.lucb
#==============================================================================================
#
# html_object_element_1 - HTML::HTMLObjectElement: <object>, its hints and what it represents
#
# DESCRIPTION:
# Port of LibWeb/HTML/HTMLObjectElement.cpp (lines 1-232): construction (which moves the
# mixin tables to HTMLObjectElement's, as C++'s vptrs move, sets up the
# Layout::ImageProvider base and queues the first representation task), initialize,
# visit_edges, the FormAssociatedElement hooks, the presentational hints, data, the layout
# node of each representation and the ancestor test of the representation task.
#
# Phase 1 documents have no browsing context, so the representation task takes the
# fallback steps (the element represents its children); fetching the data resource is
# phase 2 (its Fetch calls go to the closure stubs).
#
#==============================================================================================
# mark: Construction, initialization and visiting ================================================
## Ported from HTMLObjectElement.cpp:44 Web::HTML::HTMLObjectElement::HTMLObjectElement.
pub func html_object_element_construct(this: HtmlObjectElement*, document: DomDocument*, qualified_name: DomQualifiedName):
html_navigable_container_construct(&this.navigable_container, document, qualified_name)
# The Layout::ImageProvider base (no state), then the vptrs of the mixin bases move to
# HTMLObjectElement's tables.
this.image_provider.vtable = &html_object_element_image_provider_vtable
this.image_provider.class_id = this.cell().class_id
let element = this.html_element()
element.element.slottable_mixin.vtable = &html_object_element_slottable_mixin_vtable
element.element.aria_mixin.vtable = &html_object_element_aria_mixin_vtable
element.element.animatable.vtable = &html_object_element_animatable_vtable
element.global_event_handlers.vtable = &html_object_element_global_event_handlers_vtable
element.form_associated_element.vtable = &html_object_element_form_associated_element_vtable
# https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-object-element:potentially-delays-the-load-event
html_navigable_container_set_potentially_delays_the_load_event(&this.navigable_container, true)
# https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-object-element
# Whenever one of the following conditions occur:
# - the element is created,
# ...the user agent must queue an element task on the DOM manipulation task source given the object element to run
# the following steps to (re)determine what the object element represents.
html_object_element_queue_element_task_to_run_object_representation_steps(this)
## The document observer callbacks of initialize: `[this]() { queue_element_task_to_run_object_representation_steps(); }`;
## the context is the object element.
func html_object_element_document_activity_changed(context: void*?):
let this = (HtmlObjectElement*)(context else trap("DocumentObserver callback: no object element"))
html_object_element_queue_element_task_to_run_object_representation_steps(this)
## Ported from HTMLObjectElement.cpp:60 Web::HTML::HTMLObjectElement::initialize (override).
pub func html_object_element_initialize_impl(base: JsCell*, realm: Realm*):
let this = (HtmlObjectElement*)base
web_set_prototype_for_interface(base, realm, BindInterfaceName.html_object_element)
html_element_initialize_impl(base, realm)
let observer = realm_create_dom_document_observer(realm, realm, dom_node_document(this.dom_node()))
this.m_document_observer = observer
# https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-object-element
# Whenever one of the following conditions occur:
# - the element's node document changes whether it is fully active,
# ...the user agent must queue an element task on the DOM manipulation task source given the object element to run
# the following steps to (re)determine what the object element represents.
dom_document_observer_set_document_became_active(observer, ak.Function0[unit](call = html_object_element_document_activity_changed, context = this))
dom_document_observer_set_document_became_inactive(observer, ak.Function0[unit](call = html_object_element_document_activity_changed, context = this))
## Ported from HTMLObjectElement.cpp:80 Web::HTML::HTMLObjectElement::visit_edges (override).
pub func html_object_element_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (HtmlObjectElement*)base
html_navigable_container_visit_edges_impl(base, visitor)
layout_image_provider_image_provider_visit_edges(&this.image_provider, visitor)
gc.cell_visitor_visit_t[HtmlSharedResourceRequest](visitor, this.m_resource_request)
gc.cell_visitor_visit_t[DomDocumentObserver](visitor, this.m_document_observer)
# mark: FormAssociatedElement ====================================================================
## The object element a FormAssociatedElement mixin belongs to.
func html_object_element_from_form_associated_element(base: HtmlFormAssociatedElement*) -> HtmlObjectElement*:
return (HtmlObjectElement*)((u8*)base - memory.offset_of(HtmlElement, form_associated_element))
## Ported from HTMLObjectElement.cpp:88 Web::HTML::HTMLObjectElement::form_associated_element_attribute_changed (override).
pub func html_object_element_form_associated_element_attribute_changed_impl(base: HtmlFormAssociatedElement*, name: ak.FlyString, old_value: ak.String?, value: ak.String?, namespace_: ak.FlyString?):
let this = html_object_element_from_form_associated_element(base)
let element = this.dom_element()
# https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-object-element
# Whenever one of the following conditions occur:
# - the element's classid attribute is set, changed, or removed,
let classid_changed = html_name_is(name, html_attribute_names_classid)
# - the element's classid attribute is not present, and its data attribute is set, changed, or removed,
let data_changed = not dom_element_has_attribute(element, html_attribute_names_classid) and html_name_is(name, html_attribute_names_data)
# - neither the element's classid attribute nor its data attribute are present, and its type attribute is set, changed, or removed,
let type_changed = not dom_element_has_attribute(element, html_attribute_names_classid) and not dom_element_has_attribute(element, html_attribute_names_data) and html_name_is(name, html_attribute_names_type)
if classid_changed or data_changed or type_changed:
# ...the user agent must queue an element task on the DOM manipulation task source given the object element to run
# the following steps to (re)determine what the object element represents.
html_object_element_queue_element_task_to_run_object_representation_steps(this)
## Ported from HTMLObjectElement.cpp:105 Web::HTML::HTMLObjectElement::form_associated_element_was_removed (override).
pub func html_object_element_form_associated_element_was_removed_impl(base: HtmlFormAssociatedElement*, arg0: DomNode*?):
let this = html_object_element_from_form_associated_element(base)
html_navigable_container_destroy_the_child_navigable(&this.navigable_container)
# mark: Presentational hints, data and the layout node ===========================================
## Ported from HTMLObjectElement.cpp:110 Web::HTML::HTMLObjectElement::is_presentational_hint (override).
pub func html_object_element_is_presentational_hint_impl(base: DomElement*, name: ak.FlyString) -> bool:
if dom_element_is_presentational_hint_impl(base, name):
return true
let names: ak.FlyString[6] = [
html_attribute_names_align,
html_attribute_names_border,
html_attribute_names_height,
html_attribute_names_hspace,
html_attribute_names_vspace,
html_attribute_names_width]
return html_name_is_one_of(name, names)
## Ported from HTMLObjectElement.cpp:124 Web::HTML::HTMLObjectElement::apply_presentational_hints (override).
pub func html_object_element_apply_presentational_hints_impl(base: DomElement*, properties: ak.Vector[CssStyleProperty]*):
dom_element_apply_presentational_hints_impl(base, properties)
# for_each_attribute([&](auto& name, auto& value) { ... })
let attributes = base.m_attributes else return
for i in 0..<dom_named_node_map_length(attributes):
let attribute = dom_named_node_map_item(attributes, (u32)i) else trap("NamedNodeMap::item in range")
let name = dom_attr_name(attribute)
let value = dom_attr_value(attribute)
if html_name_is(name, html_attribute_names_align):
if html_value_is_ignoring_case(value, "center"):
html_append_keyword_hint(properties, css_data.PropertyId.text_align, css_data.Keyword.center)
elif html_value_is_ignoring_case(value, "middle"):
html_append_keyword_hint(properties, css_data.PropertyId.text_align, css_data.Keyword.middle)
elif html_name_is(name, html_attribute_names_border):
if let parsed_value = html_parse_non_negative_integer(ak.string_bytes_as_string_view(&value)):
let width_style_value = css_length_style_value_create(css_length_make_px_f64(f64(parsed_value))).css_style_value()
html_append_presentational_hint(properties, css_data.PropertyId.border_top_width, width_style_value)
html_append_presentational_hint(properties, css_data.PropertyId.border_right_width, width_style_value)
html_append_presentational_hint(properties, css_data.PropertyId.border_bottom_width, width_style_value)
html_append_presentational_hint(properties, css_data.PropertyId.border_left_width, width_style_value)
let border_style_value = css_keyword_style_value_create(css_data.Keyword.solid).css_style_value()
html_append_presentational_hint(properties, css_data.PropertyId.border_top_style, border_style_value)
html_append_presentational_hint(properties, css_data.PropertyId.border_right_style, border_style_value)
html_append_presentational_hint(properties, css_data.PropertyId.border_bottom_style, border_style_value)
html_append_presentational_hint(properties, css_data.PropertyId.border_left_style, border_style_value)
# https://html.spec.whatwg.org/multipage/rendering.html#attributes-for-embedded-content-and-images:maps-to-the-dimension-property-3
elif html_name_is(name, html_attribute_names_height):
if let parsed_value = html_parse_dimension_value(ak.string_bytes_as_string_view(&value)):
html_append_presentational_hint(properties, css_data.PropertyId.height, parsed_value)
# https://html.spec.whatwg.org/multipage/rendering.html#attributes-for-embedded-content-and-images:maps-to-the-dimension-property
elif html_name_is(name, html_attribute_names_hspace):
if let parsed_value = html_parse_dimension_value(ak.string_bytes_as_string_view(&value)):
html_append_presentational_hint(properties, css_data.PropertyId.margin_left, parsed_value)
html_append_presentational_hint(properties, css_data.PropertyId.margin_right, parsed_value)
elif html_name_is(name, html_attribute_names_vspace):
if let parsed_value = html_parse_dimension_value(ak.string_bytes_as_string_view(&value)):
html_append_presentational_hint(properties, css_data.PropertyId.margin_top, parsed_value)
html_append_presentational_hint(properties, css_data.PropertyId.margin_bottom, parsed_value)
elif html_name_is(name, html_attribute_names_width):
if let parsed_value = html_parse_dimension_value(ak.string_bytes_as_string_view(&value)):
html_append_presentational_hint(properties, css_data.PropertyId.width, parsed_value)
## Ported from HTMLObjectElement.cpp:174 Web::HTML::HTMLObjectElement::data.
# https://html.spec.whatwg.org/multipage/iframe-embed-object.html#attr-object-data
pub func html_object_element_data(this: HtmlObjectElement*) -> ak.String:
let data = dom_element_get_attribute(this.dom_element(), html_attribute_names_data) else return ak.string_new()
let url = dom_document_encoding_parse_url(dom_node_document(this.dom_node()), ak.string_bytes_as_string_view(&data)) else return ak.string_new()
return web_url.url_to_string(&url)
## Ported from HTMLObjectElement.cpp:187 Web::HTML::HTMLObjectElement::set_data.
pub func html_object_element_set_data(this: HtmlObjectElement*, data: ak.String):
dom_element_set_attribute_value(this.dom_element(), html_attribute_names_data, data)
## Ported from HTMLObjectElement.cpp:192 Web::HTML::HTMLObjectElement::create_layout_node (override).
pub func html_object_element_create_layout_node_impl(base: DomElement*, style: CssComputedProperties*) -> LayoutNode*?:
let this = (HtmlObjectElement*)base
let realm = dom_element_realm(base)
let document = dom_node_document(base.dom_node())
match this.m_representation:
.children:
# NavigableContainer::create_layout_node: NavigableContainer does not override it, so this is Element's.
return dom_element_create_layout_node_impl(base, style)
.content_navigable:
# heap().allocate<Layout::NavigableContainerViewport>(...): a layout node's initialize is JS::Cell's, which does nothing.
return realm_create_layout_navigable_container_viewport(realm, document, &this.navigable_container, style).layout_node()
.image:
if html_object_element_image_data(this) != none:
return realm_create_layout_image_box(realm, document, base, style, &this.image_provider).layout_node()
_:
return none
return none
## Ported from HTMLObjectElement.cpp:210 Web::HTML::HTMLObjectElement::adjust_computed_style (override).
pub func html_object_element_adjust_computed_style_impl(base: DomElement*, style: CssComputedProperties*):
# https://drafts.csswg.org/css-display-3/#unbox
let display = css_computed_properties_display(style)
if css_display_is_contents(&display):
css_computed_properties_set_property(style, css_data.PropertyId.display, css_display_style_value_create(css_display_from_short(CssDisplayShort.none_)).css_style_value())
## Ported from HTMLObjectElement.cpp:217 Web::HTML::HTMLObjectElement::has_ancestor_media_element_or_object_element_not_showing_fallback_content.
pub func html_object_element_has_ancestor_media_element_or_object_element_not_showing_fallback_content(this: HtmlObjectElement*) -> bool:
var ancestor = tree_node_parent(this.dom_node())
while let current = ancestor:
if is_html_media_element(current):
return true
if let ancestor_object = as_if_html_object_element(current):
if ancestor_object.m_representation != HtmlObjectElementRepresentation.children:
return true
ancestor = tree_node_parent(current)
return false