dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / dom / element_4.lucb
#==============================================================================================
#
# element_4 - DOM::Element: innerHTML, activation, shadow host, inline style, client rects
#
# DESCRIPTION:
# Port of LibWeb/DOM/Element.cpp (lines 1336-1683): the innerHTML setter and getter,
# focus and activation state, the document element and shadow host queries, the
# inline style and its StylePropertyMap, the HTML-uppercased qualified name, queueing
# an element task, void elements, getBoundingClientRect/getClientRects and the
# client* metrics.
#
#==============================================================================================
# mark: innerHTML ===============================================================================
## `TrustedTypes::TrustedHTMLOrString` widened to the `Variant<TrustedHTML, TrustedScript,
## TrustedScriptURL, Utf16String>` the Trusted Types algorithms take.
func dom_element_trusted_html_or_string_as_trusted_type(value: TrustedTypesTrustedHtmlOrString) -> TrustedTypesTrustedHtmlOrTrustedTypesTrustedScriptOrTrustedTypesTrustedScriptUrlOrUtf16String:
match value:
.trusted_types_trusted_html(trusted_html): return .trusted_types_trusted_html(trusted_html)
.utf16_string(string): return .utf16_string(string)
## Ported from Element.cpp:1337 Web::DOM::Element::set_inner_html.
# https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-element-innerhtml
pub func dom_element_set_inner_html(this: DomElement*, value: TrustedTypesTrustedHtmlOrString) -> !:
# 1. Let compliantString be the result of invoking the Get Trusted Type compliant string algorithm with
# TrustedHTML, this's relevant global object, the given value, "Element innerHTML", and "script".
# (TrustedTypes::Script.to_string() is "'script'".)
let compliant_string = try trusted_types_get_trusted_type_compliant_string(TrustedTypesTrustedTypeName.trusted_html, html_relevant_global_object((JsObject*)this), dom_element_trusted_html_or_string_as_trusted_type(value), TrustedTypesInjectionSink.element_inner_html, ak.string_from_str("'script'"))
# 2. Let context be this.
var context = this.dom_node()
# 3. Let fragment be the result of invoking the fragment parsing algorithm steps with context and compliantString.
let compliant_utf8 = ak.utf16_string_base_to_utf8_but_should_be_ported_to_utf16(&compliant_string.utf16_string_base)
let fragment = try dom_element_parse_fragment(as_dom_element(context), ak.string_bytes_as_string_view(&compliant_utf8))
# 4. If context is a template element, then set context to the template element's template contents (a DocumentFragment).
let template_element = as_if_html_template_element(context)
if let template = template_element:
context = html_template_element_content(template).dom_node()
# 5. Replace all with fragment within context.
dom_node_replace_all(context, fragment.dom_node())
# NOTE: We don't invalidate style & layout for <template> elements since they don't affect rendering.
if template_element == none:
dom_node_set_needs_style_update(context, true)
if dom_node_is_connected(context):
# NOTE: Since the DOM has changed, we have to rebuild the layout tree.
dom_node_set_needs_layout_tree_update(context, true, DomSetNeedsLayoutTreeUpdateReason.element_set_inner_html)
## Ported from Element.cpp:1376 Web::DOM::Element::inner_html.
# https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-element-innerhtml
pub func dom_element_inner_html(this: DomElement*) -> TrustedTypesTrustedHtmlOrString!:
return .utf16_string(try dom_node_serialize_fragment(this.dom_node(), HtmlRequireWellFormed.yes))
# mark: Focus, activation and the tree position =================================================
## Ported from Element.cpp:1381 Web::DOM::Element::is_focused.
pub func dom_element_is_focused(this: DomElement*) -> bool:
return dom_document_focused_area(dom_element_document(this)) == this.dom_node()
## Ported from Element.cpp:1386 Web::DOM::Element::is_the_active_element.
pub func dom_element_is_the_active_element(this: DomElement*) -> bool:
return dom_document_active_element(dom_element_document(this)) == this
## Ported from Element.cpp:1391 Web::DOM::Element::is_being_activated.
pub func dom_element_is_being_activated(this: DomElement*) -> bool:
return this.m_is_being_activated
## Ported from Element.cpp:1396 Web::DOM::Element::set_being_activated (virtual).
pub func dom_element_set_being_activated_impl(this: DomElement*, active: bool):
if this.m_is_being_activated == active:
return
this.m_is_being_activated = active
css_invalidation_invalidate_style_after_active_state_change(this)
## Ported from Element.cpp:1404 Web::DOM::Element::is_target.
pub func dom_element_is_target(this: DomElement*) -> bool:
return dom_document_target_element(dom_element_document(this)) == this
## Ported from Element.cpp:1410 Web::DOM::Element::is_document_element.
# https://dom.spec.whatwg.org/#document-element
pub func dom_element_is_document_element(this: DomElement*) -> bool:
# The document element of a document is the element whose parent is that document, if it exists; otherwise null.
return tree_node_parent(this.dom_node()) == dom_element_document(this).dom_node()
## Ported from Element.cpp:1417 Web::DOM::Element::is_shadow_host.
# https://dom.spec.whatwg.org/#element-shadow-host
pub func dom_element_is_shadow_host(this: DomElement*) -> bool:
# An element is a shadow host if its shadow root is non-null.
return this.m_shadow_root != none
## Ported from Element.cpp:1423 Web::DOM::Element::set_shadow_root.
pub func dom_element_set_shadow_root(this: DomElement*, shadow_root: DomShadowRoot*?):
if this.m_shadow_root == shadow_root:
return
if let old_shadow_root = this.m_shadow_root:
dom_document_fragment_set_host(old_shadow_root.dom_document_fragment(), none)
dom_node_set_is_connected(old_shadow_root.dom_node(), false)
this.m_shadow_root = shadow_root
if let new_shadow_root = this.m_shadow_root:
dom_document_fragment_set_host(new_shadow_root.dom_document_fragment(), this)
dom_node_set_is_connected(new_shadow_root.dom_node(), dom_node_is_connected(this.dom_node()))
css_invalidation_invalidate_style_after_shadow_root_change(this)
dom_node_set_needs_layout_tree_update(this.dom_node(), true, DomSetNeedsLayoutTreeUpdateReason.element_set_shadow_root)
# mark: Inline style ============================================================================
## Ported from Element.cpp:1440 Web::DOM::Element::style_for_bindings.
pub func dom_element_style_for_bindings(this: DomElement*) -> CssStyleProperties*:
if this.m_inline_style == none:
var properties: ak.Vector[CssStyleProperty]
var custom_properties: ak.OrderedHashMap[ak.FlyString, CssStyleProperty, ak.FlyStringTraits]
this.m_inline_style = css_style_properties_create_element_inline_style(dom_element_abstract_element(this), properties, custom_properties)
return this.m_inline_style else trap("unreachable")
## Ported from Element.cpp:1447 Web::DOM::Element::attribute_style_map.
pub func dom_element_attribute_style_map(this: DomElement*) -> CssStylePropertyMap*:
if this.m_attribute_style_map == none:
this.m_attribute_style_map = css_style_property_map_create(dom_element_realm(this), dom_element_style_for_bindings(this).css_style_declaration())
return this.m_attribute_style_map else trap("unreachable")
## Ported from Element.cpp:1454 Web::DOM::Element::set_inline_style.
pub func dom_element_set_inline_style(this: DomElement*, style: CssStyleProperties*?):
this.m_inline_style = style
if this.m_attribute_style_map != none:
this.m_attribute_style_map = none
dom_node_set_needs_style_update(this.dom_node(), true)
## Ported from Element.cpp:1463 Web::DOM::Element::make_html_uppercased_qualified_name.
# https://dom.spec.whatwg.org/#element-html-uppercased-qualified-name
func dom_element_make_html_uppercased_qualified_name(this: DomElement*) -> ak.FlyString:
# This is allowed by the spec: "User agents could optimize qualified name and HTML-uppercased qualified name by storing them in internal slots."
let qualified_name = dom_element_qualified_name(this)
if dom_element_is_html_element_in_html_document(this):
return ak.fly_string_to_ascii_uppercase(&qualified_name)
return qualified_name
# mark: Tasks and void elements =================================================================
## Ported from Element.cpp:1472 Web::DOM::Element::queue_an_element_task.
# https://html.spec.whatwg.org/multipage/webappapis.html#queue-an-element-task
pub func dom_element_queue_an_element_task(this: DomElement*, source: HtmlTaskSource, steps: ak.Function0[unit]) -> u64:
return html_queue_a_task(source, html_main_thread_event_loop(), dom_element_document(this), gc.create_function0(gc.cell_heap(this.cell()), steps))
## `local_name().is_one_of(...)` over tag-name FlyStrings.
func dom_element_local_name_is_one_of(this: DomElement*, names: const ak.FlyString[]) -> bool:
let local_name = dom_element_local_name(this)
for name in names:
if ak.fly_string_eq_fly_string(&local_name, &name):
return true
return false
## Ported from Element.cpp:1478 Web::DOM::Element::is_void_element.
# https://html.spec.whatwg.org/multipage/syntax.html#void-elements
pub func dom_element_is_void_element(this: DomElement*) -> bool:
let names: ak.FlyString[14] = [html_tag_names_area, html_tag_names_base, html_tag_names_br, html_tag_names_col, html_tag_names_embed, html_tag_names_hr, html_tag_names_img, html_tag_names_input, html_tag_names_link, html_tag_names_meta, html_tag_names_param, html_tag_names_source, html_tag_names_track, html_tag_names_wbr]
return dom_element_local_name_is_one_of(this, names)
## Ported from Element.cpp:1484 Web::DOM::Element::serializes_as_void.
# https://html.spec.whatwg.org/multipage/parsing.html#serializes-as-void
pub func dom_element_serializes_as_void(this: DomElement*) -> bool:
let names: ak.FlyString[4] = [html_tag_names_basefont, html_tag_names_bgsound, html_tag_names_frame, html_tag_names_keygen]
return dom_element_is_void_element(this) or dom_element_local_name_is_one_of(this, names)
# mark: Client rects ============================================================================
## Ported from Element.cpp:1490 Web::DOM::Element::get_bounding_client_rect_for_bindings.
# https://drafts.csswg.org/cssom-view/#dom-element-getboundingclientrect
pub func dom_element_get_bounding_client_rect_for_bindings(this: DomElement*) -> GeometryDomRect*:
let rect = dom_element_get_bounding_client_rect(this)
let x = gfx.css_pixel_rect_x(&rect)
let y = gfx.css_pixel_rect_y(&rect)
let width = gfx.css_pixel_rect_width(&rect)
let height = gfx.css_pixel_rect_height(&rect)
return geometry_dom_rect_construct_impl(dom_element_realm(this), gfx.css_pixels_to_double_const(&x), gfx.css_pixels_to_double_const(&y), gfx.css_pixels_to_double_const(&width), gfx.css_pixels_to_double_const(&height)) else trap("MUST: DOMRect::construct_impl")
## `rect.width() != 0 && rect.height() != 0`.
func dom_element_rect_has_area(rect: const gfx.CssPixelRect*) -> bool:
let width = gfx.css_pixel_rect_width(rect)
let height = gfx.css_pixel_rect_height(rect)
return width.m_value != 0 and height.m_value != 0
## Ported from Element.cpp:1497 Web::DOM::Element::get_bounding_client_rect.
# https://drafts.csswg.org/cssom-view/#dom-element-getboundingclientrect
pub func dom_element_get_bounding_client_rect(this: DomElement*) -> gfx.CssPixelRect:
# 1. Let list be the result of invoking getClientRects() on element.
var list = dom_element_get_client_rects(this)
# 2. If the list is empty return a DOMRect object whose x, y, width and height members are zero.
if ak.vector_size(&list) == 0:
var empty: gfx.CssPixelRect
return empty
# 3. If all rectangles in list have zero width or height, return the first rectangle in list.
var all_rectangle_has_zero_width_or_height = true
for i in 0..<ak.vector_size(&list):
let rect = ak.vector_at(&list, i)
if dom_element_rect_has_area(rect):
all_rectangle_has_zero_width_or_height = false
break
if all_rectangle_has_zero_width_or_height:
return *ak.vector_at(&list, 0)
# 4. Otherwise, return a DOMRect object describing the smallest rectangle that includes all of the rectangles in
# list of which the height or width is not zero.
var bounding_rect = *ak.vector_at(&list, 0)
for i in 1..<ak.vector_size(&list):
let rect = ak.vector_at(&list, i)
if not dom_element_rect_has_area(rect):
continue
gfx.css_pixel_rect_unite(&bounding_rect, *rect)
return bounding_rect
## Ported from Element.cpp:1531 Web::DOM::Element::get_client_rects_for_bindings.
# https://drafts.csswg.org/cssom-view/#dom-element-getclientrects
pub func dom_element_get_client_rects_for_bindings(this: DomElement*) -> GeometryDomRectList*:
var rects: ak.Vector[GeometryDomRect*?]
var client_rects = dom_element_get_client_rects(this)
for rect in ak.vector_span(&client_rects):
let x = gfx.css_pixel_rect_x(&rect)
let y = gfx.css_pixel_rect_y(&rect)
let width = gfx.css_pixel_rect_width(&rect)
let height = gfx.css_pixel_rect_height(&rect)
ak.vector_append_t(&rects, geometry_dom_rect_construct_impl(dom_element_realm(this), gfx.css_pixels_to_double_const(&x), gfx.css_pixels_to_double_const(&y), gfx.css_pixels_to_double_const(&width), gfx.css_pixels_to_double_const(&height)) else trap("MUST: DOMRect::construct_impl"))
return geometry_dom_rect_list_create(dom_element_realm(this), rects)
## Ported from Element.cpp:1541 Web::DOM::Element::get_client_rects.
# https://drafts.csswg.org/cssom-view/#dom-element-getclientrects
pub func dom_element_get_client_rects(this: DomElement*) -> ak.Vector[gfx.CssPixelRect]:
var rects: ak.Vector[gfx.CssPixelRect]
let document = dom_element_document(this)
if dom_document_navigable(document) == none:
return rects
# NOTE: Ensure that layout is up-to-date before looking at metrics.
dom_document_update_layout_if_needed_for_node(document, this.dom_node(), DomUpdateLayoutReason.element_get_client_rects)
# 1. If the element on which it was invoked does not have an associated layout box return an empty DOMRectList
# object and stop this algorithm.
if dom_element_layout_node(this) == none:
return rects
# FIXME: 2. If the element has an associated SVG layout box return a DOMRectList object containing a single
# DOMRect object that describes the bounding box of the element as defined by the SVG specification,
# applying the transforms that apply to the element and its ancestors.
# 3. Return a DOMRectList object containing DOMRect objects in content order, one for each box fragment,
# describing its border area (including those with a height or width of zero) with the following constraints:
# - Apply the transforms that apply to the element and its ancestors.
# FIXME: - If the element on which the method was invoked has a computed value for the display property of table
# or inline-table include both the table box and the caption box, if any, but not the anonymous container box.
# FIXME: - Replace each anonymous block box with its child box(es) and repeat this until no anonymous block boxes
# are left in the final list.
# NOTE: Make sure CSS transforms are resolved before it is used to calculate the rect position.
dom_document_update_paint_and_hit_testing_properties_if_needed(document)
if let paintable_box = dom_node_paintable_box(this.dom_node()):
let absolute_rect = paint_paintable_box_absolute_border_box_rect(paintable_box)
ak.vector_append_t(&rects, paint_paintable_box_transform_rect_to_viewport(paintable_box, absolute_rect))
elif dom_node_paintable(this.dom_node()) != none:
let description = dom_node_debug_description(this.dom_node())
ak.dbgln(f"FIXME: Failed to get client rects for element ({ak.string_view_text(ak.string_bytes_as_string_view(&description))})")
return rects
## Ported from Element.cpp:1581 Web::DOM::Element::client_top.
pub func dom_element_client_top(this: DomElement*) -> i32:
# NOTE: We only need style information here, not layout metrics.
dom_document_update_style_if_needed_for_element(dom_element_document(this), dom_element_abstract_element(this))
# 1. If the element has no associated CSS layout box or if the CSS layout box is inline, return zero.
let computed_properties = dom_element_computed_properties(this) else return 0
let display = css_computed_properties_display(computed_properties)
if css_display_is_none(&display) or css_display_is_contents(&display):
return 0
if css_display_is_inline_outside(&display) and css_display_is_flow_inside(&display):
return 0
# 2. Return the computed value of the border-top-width property
# plus the height of any scrollbar rendered between the top padding edge and the top border edge,
# ignoring any transforms that apply to the element and its ancestors.
let border_top_style = css_computed_properties_line_style(computed_properties, css_data.PropertyId.border_top_style)
if border_top_style == css_data.LineStyle.none_ or border_top_style == css_data.LineStyle.hidden:
return 0
let border_top_width = css_computed_properties_length(computed_properties, css_data.PropertyId.border_top_width)
let result = ak.max(gfx.css_pixels_from_int(0), css_length_absolute_length_to_px(&border_top_width))
return gfx.css_pixels_to_int_const(&result)
## Ported from Element.cpp:1605 Web::DOM::Element::client_left.
# https://drafts.csswg.org/cssom-view/#dom-element-clientleft
pub func dom_element_client_left(this: DomElement*) -> i32:
# NOTE: We only need style information here, not layout metrics.
dom_document_update_style_if_needed_for_element(dom_element_document(this), dom_element_abstract_element(this))
# 1. If the element has no associated CSS layout box or if the CSS layout box is inline, return zero.
let computed_properties = dom_element_computed_properties(this) else return 0
let display = css_computed_properties_display(computed_properties)
if css_display_is_none(&display) or css_display_is_contents(&display):
return 0
if css_display_is_inline_outside(&display) and css_display_is_flow_inside(&display):
return 0
# 2. Return the computed value of the border-left-width property
# plus the width of any scrollbar rendered between the left padding edge and the left border edge,
# ignoring any transforms that apply to the element and its ancestors.
let border_left_style = css_computed_properties_line_style(computed_properties, css_data.PropertyId.border_left_style)
if border_left_style == css_data.LineStyle.none_ or border_left_style == css_data.LineStyle.hidden:
return 0
let border_left_width = css_computed_properties_length(computed_properties, css_data.PropertyId.border_left_width)
let result = ak.max(gfx.css_pixels_from_int(0), css_length_absolute_length_to_px(&border_left_width))
return gfx.css_pixels_to_int_const(&result)
## `(is<HTMLHtmlElement>(*this) && !document().in_quirks_mode()) || (is<HTMLBodyElement>(*this) && document().in_quirks_mode())`.
func dom_element_is_viewport_sized_for_client_metrics(this: DomElement*) -> bool:
let in_quirks_mode = dom_document_in_quirks_mode(dom_element_document(this))
return (is_html_html_element(this) and not in_quirks_mode) or (is_html_body_element(this) and in_quirks_mode)
## Ported from Element.cpp:1629 Web::DOM::Element::client_width.
# https://drafts.csswg.org/cssom-view/#dom-element-clientwidth
pub func dom_element_client_width(this: DomElement*) -> i32:
# NOTE: We do step 2 before step 1 here since step 2 can exit early without needing to perform layout.
# 2. If the element is the root element and the element’s node document is not in quirks mode,
# or if the element is the HTML body element and the element’s node document is in quirks mode,
# return the viewport width excluding the size of a rendered scroll bar (if any).
if dom_element_is_viewport_sized_for_client_metrics(this):
let viewport_rect = dom_document_viewport_rect(dom_element_document(this))
let width = gfx.css_pixel_rect_width(&viewport_rect)
return gfx.css_pixels_to_int_const(&width)
# NOTE: Ensure that layout is up-to-date before looking at metrics.
dom_document_update_layout_if_needed_for_node(dom_element_document(this), this.dom_node(), DomUpdateLayoutReason.element_client_width)
# 1. If the element has no associated CSS layout box or if the CSS layout box is inline, return zero.
let paintable_box = dom_node_paintable_box(this.dom_node()) else return 0
# 3. Return the width of the padding edge excluding the width of any rendered scrollbar between the padding edge and the border edge,
# ignoring any transforms that apply to the element and its ancestors.
let padding_box = paint_paintable_box_absolute_padding_box_rect(paintable_box)
let width = gfx.css_pixel_rect_width(&padding_box)
return gfx.css_pixels_to_int_const(&width)
## Ported from Element.cpp:1654 Web::DOM::Element::client_height.
# https://drafts.csswg.org/cssom-view/#dom-element-clientheight
pub func dom_element_client_height(this: DomElement*) -> i32:
# NOTE: We do step 2 before step 1 here since step 2 can exit early without needing to perform layout.
# 2. If the element is the root element and the element’s node document is not in quirks mode,
# or if the element is the HTML body element and the element’s node document is in quirks mode,
# return the viewport height excluding the size of a rendered scroll bar (if any).
if dom_element_is_viewport_sized_for_client_metrics(this):
let viewport_rect = dom_document_viewport_rect(dom_element_document(this))
let height = gfx.css_pixel_rect_height(&viewport_rect)
return gfx.css_pixels_to_int_const(&height)
# NOTE: Ensure that layout is up-to-date before looking at metrics.
dom_document_update_layout_if_needed_for_node(dom_element_document(this), this.dom_node(), DomUpdateLayoutReason.element_client_height)
# 1. If the element has no associated CSS layout box or if the CSS layout box is inline, return zero.
let paintable_box = dom_node_paintable_box(this.dom_node()) else return 0
# 3. Return the height of the padding edge excluding the height of any rendered scrollbar between the padding edge and the border edge,
# ignoring any transforms that apply to the element and its ancestors.
let padding_box = paint_paintable_box_absolute_padding_box_rect(paintable_box)
let height = gfx.css_pixel_rect_height(&padding_box)
return gfx.css_pixels_to_int_const(&height)
## Ported from Element.cpp:1679 Web::DOM::Element::current_css_zoom.
# https://drafts.csswg.org/cssom-view/#dom-element-currentcsszoom
pub func dom_element_current_css_zoom(this: DomElement*) -> f64:
ak.dbgln("FIXME: Implement Element::current_css_zoom()")
return 1.0