dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / dom / element_10.lucb
#==============================================================================================
#
# element_10 - DOM::Element: prefixes, attribute walks, style slots, pseudo-elements, scroll()
#
# DESCRIPTION:
# Port of LibWeb/DOM/Element.cpp (lines 3361-3733): set_prefix and locating a namespace
# prefix, for_each_attribute, the layout-node accessors, the attribute-list queries, the
# cascaded/computed properties and custom property data of an element or one of its
# pseudo-elements, getting and ensuring pseudo-elements, scroll()/scrollBy() and
# checkVisibility().
#
#==============================================================================================
# mark: Prefixes and attribute walks ============================================================
## Ported from Element.cpp:3361 Web::DOM::Element::set_prefix.
pub func dom_element_set_prefix(this: DomElement*, value: ak.FlyString?):
dom_qualified_name_set_prefix(&this.m_qualified_name, value)
## Ported from Element.cpp:3367 Web::DOM::Element::locate_a_namespace_prefix.
# https://dom.spec.whatwg.org/#locate-a-namespace-prefix
pub func dom_element_locate_a_namespace_prefix(this: DomElement*, namespace_: ak.String?) -> ak.String?:
# 1. If element’s namespace is namespace and its namespace prefix is non-null, then return its namespace prefix.
if dom_node_optional_fly_string_equals_optional_string(dom_element_namespace_uri(this), namespace_):
if let prefix = dom_element_prefix(this):
return ak.fly_string_to_string_const(&prefix)
# 2. If element has an attribute whose namespace prefix is "xmlns" and value is namespace, then return element’s first such attribute’s local name.
if let attributes = dom_element_attributes(this):
for i in 0..<dom_named_node_map_length(attributes):
let attr = dom_named_node_map_item(attributes, (u32)i) else trap("NamedNodeMap::item in range")
if dom_element_optional_fly_string_is_text(dom_attr_prefix(attr), "xmlns") and dom_node_optional_string_equals(namespace_, dom_attr_value(attr)):
let local_name = dom_attr_local_name(attr)
return ak.fly_string_to_string_const(&local_name)
# 3. If element’s parent element is not null, then return the result of running locate a namespace prefix on that element using namespace.
if let parent = dom_node_parent_element(this.dom_node()):
return dom_element_locate_a_namespace_prefix(parent, namespace_)
# 4. Return null
return none
## Ported from Element.cpp:3390 Web::DOM::Element::for_each_attribute.
pub func dom_element_for_each_attribute_function1(this: DomElement*, callback: ak.Function1[DomAttr*, unit]):
let attributes = this.m_attributes else return
for i in 0..<dom_named_node_map_length(attributes):
ak.function1_call(&callback, dom_named_node_map_item(attributes, (u32)i) else trap("NamedNodeMap::item in range"))
## for_each_attribute(Function<void(FlyString const&, String const&)>)'s adapter lambda: the context is
## the caller's callback.
func dom_element_for_each_attribute_name_and_value(context: void*?, attr: DomAttr*):
let callback = (ak.Function2[const ak.FlyString*, const ak.String*, unit]*)(context else trap("no callback"))
let name = dom_attr_name(attr)
let value = dom_attr_value(attr)
ak.function2_call(callback, &name, &value)
## Ported from Element.cpp:3398 Web::DOM::Element::for_each_attribute.
pub func dom_element_for_each_attribute_function2(this: DomElement*, callback: ak.Function2[const ak.FlyString*, const ak.String*, unit]):
var name_and_value_callback = callback
dom_element_for_each_attribute_function1(this, ak.Function1[DomAttr*, unit](call = dom_element_for_each_attribute_name_and_value, context = &name_and_value_callback))
# mark: Layout nodes and the attribute list =====================================================
## Ported from Element.cpp:3405 Web::DOM::Element::layout_node.
pub func dom_element_layout_node(this: DomElement*) -> LayoutNodeWithStyle*?:
return (LayoutNodeWithStyle*?)dom_node_layout_node(this.dom_node())
## Ported from Element.cpp:3410 Web::DOM::Element::layout_node.
pub func dom_element_layout_node_const(this: DomElement*) -> LayoutNodeWithStyle*?:
return (LayoutNodeWithStyle*?)dom_node_layout_node_const(this.dom_node())
## Ported from Element.cpp:3415 Web::DOM::Element::unsafe_layout_node.
pub func dom_element_unsafe_layout_node(this: DomElement*) -> LayoutNodeWithStyle*?:
return (LayoutNodeWithStyle*?)dom_node_unsafe_layout_node(this.dom_node())
## Ported from Element.cpp:3420 Web::DOM::Element::unsafe_layout_node.
pub func dom_element_unsafe_layout_node_const(this: DomElement*) -> LayoutNodeWithStyle*?:
return (LayoutNodeWithStyle*?)dom_node_unsafe_layout_node_const(this.dom_node())
## Ported from Element.cpp:3425 Web::DOM::Element::has_attributes.
pub func dom_element_has_attributes(this: DomElement*) -> bool:
let attributes = this.m_attributes else return false
return not dom_named_node_map_is_empty(attributes)
## Ported from Element.cpp:3430 Web::DOM::Element::attribute_list_size.
pub func dom_element_attribute_list_size(this: DomElement*) -> usize:
let attributes = this.m_attributes else return 0
return dom_named_node_map_length(attributes)
# mark: Cascaded and computed properties ========================================================
## Ported from Element.cpp:3435 Web::DOM::Element::cascaded_properties.
pub func dom_element_cascaded_properties(this: DomElement*, pseudo_element: css_data.PseudoElement?) -> CssCascadedProperties*?:
if let type_ = pseudo_element:
let pseudo_element_data = dom_element_get_pseudo_element(this, type_)
if let data = pseudo_element_data:
return dom_pseudo_element_cascaded_properties(data)
return none
return this.m_cascaded_properties
## Ported from Element.cpp:3446 Web::DOM::Element::set_cascaded_properties.
pub func dom_element_set_cascaded_properties(this: DomElement*, pseudo_element: css_data.PseudoElement?, cascaded_properties: CssCascadedProperties*?):
if let type_ = pseudo_element:
if (u8)type_ >= (u8)css_data.PseudoElement.known_pseudo_element_count:
return
if cascaded_properties != none:
dom_pseudo_element_set_cascaded_properties(dom_element_ensure_pseudo_element(this, type_), cascaded_properties)
elif let existing_pseudo_element = dom_element_get_pseudo_element(this, type_):
dom_pseudo_element_set_cascaded_properties(existing_pseudo_element, none)
return
this.m_cascaded_properties = cascaded_properties
## Ported from Element.cpp:3460 Web::DOM::Element::computed_properties.
pub func dom_element_computed_properties(this: DomElement*, pseudo_element_type: css_data.PseudoElement? = none) -> CssComputedProperties*?:
if let type_ = pseudo_element_type:
if let pseudo_element = dom_element_get_pseudo_element(this, type_):
return dom_pseudo_element_computed_properties(pseudo_element)
return none
return this.m_computed_properties
## Ported from Element.cpp:3470 Web::DOM::Element::computed_properties.
pub func dom_element_computed_properties_const(this: DomElement*, pseudo_element_type: css_data.PseudoElement? = none) -> CssComputedProperties*?:
if let type_ = pseudo_element_type:
if let pseudo_element = dom_element_get_pseudo_element(this, type_):
return dom_pseudo_element_computed_properties(pseudo_element)
return none
return this.m_computed_properties
## Ported from Element.cpp:3480 Web::DOM::Element::set_computed_properties.
pub func dom_element_set_computed_properties(this: DomElement*, pseudo_element_type: css_data.PseudoElement?, style: CssComputedProperties*?):
if let type_ = pseudo_element_type:
if not css_selector_pseudo_element_selector_is_known_pseudo_element_type(type_):
return
if style != none:
dom_pseudo_element_set_computed_properties(dom_element_ensure_pseudo_element(this, type_), style)
elif let existing_pseudo_element = dom_element_get_pseudo_element(this, type_):
dom_pseudo_element_set_computed_properties(existing_pseudo_element, none)
return
this.m_computed_properties = style
dom_element_computed_properties_changed(this)
# mark: Pseudo-elements =========================================================================
## Ported from Element.cpp:3495 Web::DOM::Element::get_pseudo_element.
pub func dom_element_get_pseudo_element(this: DomElement*, type_: css_data.PseudoElement) -> DomPseudoElement*?:
let pseudo_element_data = this.m_pseudo_element_data else return none
if not css_selector_pseudo_element_selector_is_known_pseudo_element_type(type_):
return none
let pseudo_element = ak.hash_map_get_k(pseudo_element_data, type_) else return none
return *pseudo_element
## Ported from Element.cpp:3511 Web::DOM::Element::ensure_pseudo_element.
func dom_element_ensure_pseudo_element(this: DomElement*, type_: css_data.PseudoElement) -> DomPseudoElement*:
if this.m_pseudo_element_data == none:
this.m_pseudo_element_data = new ak.HashMap[css_data.PseudoElement, DomPseudoElement*, css_data.PseudoElementTraits] else trap("out of memory")
let pseudo_element_data = this.m_pseudo_element_data else trap("unreachable")
assert(css_selector_pseudo_element_selector_is_known_pseudo_element_type(type_))
if ak.hash_map_get_k(pseudo_element_data, type_) == none:
let heap = gc.cell_heap(this.cell())
if css_data.is_pseudo_element_root(type_):
_ = ak.hash_map_set(pseudo_element_data, type_, heap_allocate_dom_pseudo_element_tree_node(heap).dom_pseudo_element())
else:
_ = ak.hash_map_set(pseudo_element_data, type_, heap_allocate_dom_pseudo_element(heap))
return *(ak.hash_map_get_k(pseudo_element_data, type_) else trap("unreachable"))
## Ported from Element.cpp:3528 Web::DOM::Element::set_custom_property_data.
pub func dom_element_set_custom_property_data(this: DomElement*, pseudo_element: css_data.PseudoElement?, data: const CssCustomPropertyData*?):
if pseudo_element == none:
this.m_custom_property_data = data
return
let type_ = pseudo_element else trap("unreachable")
if not css_selector_pseudo_element_selector_is_known_pseudo_element_type(type_):
return
if data != none:
dom_pseudo_element_set_custom_property_data(dom_element_ensure_pseudo_element(this, type_), data)
elif let existing_pseudo_element = dom_element_get_pseudo_element(this, type_):
dom_pseudo_element_set_custom_property_data(existing_pseudo_element, none)
## Ported from Element.cpp:3544 Web::DOM::Element::custom_property_data.
pub func dom_element_custom_property_data(this: DomElement*, pseudo_element: css_data.PseudoElement?) -> const CssCustomPropertyData*?:
let type_ = pseudo_element else return this.m_custom_property_data
if not css_selector_pseudo_element_selector_is_known_pseudo_element_type(type_):
return none
return dom_pseudo_element_custom_property_data(dom_element_ensure_pseudo_element(this, type_))
# mark: scroll() and scrollBy() =================================================================
## Ported from Element.cpp:3556 Web::DOM::Element::scroll.
# https://drafts.csswg.org/cssom-view/#dom-element-scroll
pub func dom_element_scroll_f64(this: DomElement*, x: f64, y: f64) -> JsPromiseCapability*:
# 1. If invoked with one argument, follow these substeps:
# NOTE: Not relevant here.
# 2. If invoked with two arguments, follow these substeps:
# 1. Let options be null converted to a ScrollToOptions dictionary. [WEBIDL]
# 2. Let x and y be the arguments, respectively.
# 3. Normalize non-finite values for x and y.
# 4. Let the left dictionary member of options have the value x.
# 5. Let the top dictionary member of options have the value y.
let normalized_x = html_normalize_non_finite_values_f64(x)
let normalized_y = html_normalize_non_finite_values_f64(y)
# 3. Let document be the element’s node document.
let document = dom_element_document(this)
# 4. If document is not the active document, return a resolved Promise and abort the remaining steps.
if not dom_document_is_active(document):
return idl_create_resolved_promise(dom_element_realm(this), js_undefined())
# 5. Let window be the value of document’s defaultView attribute.
# FIXME: The specification expects defaultView to be a Window object, but defaultView actually returns a WindowProxy object.
let maybe_window = dom_document_window(document)
# 6. If window is null, return a resolved Promise and abort the remaining steps.
let window = maybe_window else return idl_create_resolved_promise(dom_element_realm(this), js_undefined())
# 7. If the element is the root element and document is in quirks mode, return a resolved Promise and abort the
# remaining steps.
if dom_document_document_element(document) == this and dom_document_in_quirks_mode(document):
return idl_create_resolved_promise(dom_element_realm(this), js_undefined())
# OPTIMIZATION: Scrolling an unscrolled element to (0, 0) is a no-op as long
# as the element is not eligible to be the Document.scrollingElement.
let current_scroll_offset = dom_element_scroll_offset(this, none)
if normalized_x == 0.0 and normalized_y == 0.0 and gfx.css_pixel_point_is_zero(¤t_scroll_offset) and not dom_element_is_the_body(this, document) and dom_document_document_element(document) != this:
return idl_create_resolved_promise(dom_element_realm(this), js_undefined())
# NB: Ensure that layout is up-to-date before looking at metrics.
dom_document_update_layout(document, DomUpdateLayoutReason.element_scroll)
# 8. If the element is the root element, return the Promise returned by scroll() on window after the method is
# invoked with scrollX on window as first argument and y as second argument, and abort the remaining steps.
if dom_document_document_element(document) == this:
return html_window_scroll_f64(window, html_window_scroll_x(window), normalized_y)
# 9. If the element is the body element, document is in quirks mode, and the element is not potentially
# scrollable, return the Promise returned by scroll() on window after the method is invoked with options as the
# only argument, and abort the remaining steps.
if dom_element_is_the_body(this, document) and dom_document_in_quirks_mode(document) and not dom_element_is_potentially_scrollable(this):
return html_window_scroll_f64(window, normalized_x, normalized_y)
# 10. If the element does not have any associated box, the element has no associated scrolling box, or the element
# has no overflow, return a resolved Promise and abort the remaining steps.
# FIXME: or the element has no overflow
let paintable_box = dom_node_paintable_box(this.dom_node()) else return idl_create_resolved_promise(dom_element_realm(this), js_undefined())
# 11. Scroll the element to x,y, with the scroll behavior being the value of the behavior dictionary member of
# options. Let scrollPromise be the Promise returned from this step.
# FIXME: Implement this in terms of calling "scroll the element".
var scroll_offset = paint_paintable_box_scroll_offset(paintable_box)
gfx.css_pixel_point_set_x(&scroll_offset, gfx.css_pixels_nearest_value_for_f64(normalized_x))
gfx.css_pixel_point_set_y(&scroll_offset, gfx.css_pixels_nearest_value_for_f64(normalized_y))
_ = paint_paintable_box_set_scroll_offset(paintable_box, scroll_offset)
let scroll_promise = idl_create_resolved_promise(dom_element_realm(this), js_undefined())
# 12. Return scrollPromise.
return scroll_promise
## Ported from Element.cpp:3633 Web::DOM::Element::scroll.
# https://drafts.csswg.org/cssom-view/#dom-element-scroll
pub func dom_element_scroll_html_scroll_to_options(this: DomElement*, options: HtmlScrollToOptions) -> JsPromiseCapability*:
# 1. If invoked with one argument, follow these substeps:
# 1. Let options be the argument.
# 2. Normalize non-finite values for left and top dictionary members of options, if present.
# 3. Let x be the value of the left dictionary member of options, if present, or the element’s current scroll position on the x axis otherwise.
# 4. Let y be the value of the top dictionary member of options, if present, or the element’s current scroll position on the y axis otherwise.
# NOTE: remaining steps performed by Element::scroll(double x, double y)
var x = 0.0
if let left = options.left:
x = html_normalize_non_finite_values_f64(left)
else:
x = dom_element_scroll_left(this)
var y = 0.0
if let top = options.top:
y = html_normalize_non_finite_values_f64(top)
else:
y = dom_element_scroll_top(this)
return dom_element_scroll_f64(this, x, y)
## Ported from Element.cpp:3647 Web::DOM::Element::scroll_by.
# https://drafts.csswg.org/cssom-view/#dom-element-scrollby
pub func dom_element_scroll_by_f64(this: DomElement*, x: f64, y: f64) -> JsPromiseCapability*:
# 2. If invoked with two arguments, follow these substeps:
# 1. Let options be null converted to a ScrollToOptions dictionary. [WEBIDL]
var options: HtmlScrollToOptions
# 2. Let x and y be the arguments, respectively.
# 3. Normalize non-finite values for x and y.
# 4. Let the left dictionary member of options have the value x.
# 5. Let the top dictionary member of options have the value y.
# NOTE: Element::scroll_by(HTML::ScrollToOptions) performs the normalization and following steps.
options.left = x
options.top = y
return dom_element_scroll_by_html_scroll_to_options(this, options)
## Ported from Element.cpp:3664 Web::DOM::Element::scroll_by.
# https://drafts.csswg.org/cssom-view/#dom-element-scrollby
pub func dom_element_scroll_by_html_scroll_to_options(this: DomElement*, options: HtmlScrollToOptions) -> JsPromiseCapability*:
var scrolled_options = options
# 1. If invoked with one argument, follow these substeps:
# 1. Let options be the argument.
# 2. Normalize non-finite values for left and top dictionary members of options, if present.
let left = html_normalize_non_finite_values_optional(options.left)
let top = html_normalize_non_finite_values_optional(options.top)
# NB: Step 2 is implemented by the other overload of scroll_by().
# 3. Add the value of scrollLeft to the left dictionary member.
scrolled_options.left = dom_element_scroll_left(this) + left
# 4. Add the value of scrollTop to the top dictionary member.
scrolled_options.top = dom_element_scroll_top(this) + top
# 5. Return the Promise returned by scroll() after the method is invoked with options as the only argument.
return dom_element_scroll_html_scroll_to_options(this, scrolled_options)
# mark: checkVisibility() =======================================================================
## Ported from Element.cpp:3685 Web::DOM::Element::check_visibility.
# https://drafts.csswg.org/cssom-view-1/#dom-element-checkvisibility
pub func dom_element_check_visibility(this: DomElement*, options: DomCheckVisibilityOptions?) -> bool:
# 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_check_visibility)
# 1. If this does not have an associated box, return false.
if dom_node_paintable_box(this.dom_node()) == none:
return false
# 2. If an ancestor of this in the flat tree has content-visibility: hidden, return false.
var element = dom_node_flat_tree_parent_element(this.dom_node())
while let current = element:
let computed_properties = dom_element_computed_properties(current) else trap("an ancestor with a box has computed properties")
if css_computed_properties_content_visibility(computed_properties) == css_data.ContentVisibility.hidden:
return false
element = dom_node_flat_tree_parent_element(current.dom_node())
# AD-HOC: Since the rest of the steps use the options, we can return early if we haven't been given any options.
let present_options = options else return true
# 3. If either the opacityProperty or the checkOpacity dictionary members of options are true, and this, or an
# ancestor of this in the flat tree, has a computed opacity value of 0, return false.
if present_options.opacity_property or present_options.check_opacity:
var inclusive_ancestor: DomElement*? = this
while let current = inclusive_ancestor:
let computed_properties = dom_element_computed_properties(current) else trap("an element with a box has computed properties")
if css_computed_properties_opacity(computed_properties) == 0.0f32:
return false
inclusive_ancestor = dom_node_flat_tree_parent_element(current.dom_node())
# 4. If either the visibilityProperty or the checkVisibilityCSS dictionary members of options are true, and this
# is invisible, return false.
if present_options.visibility_property or present_options.check_visibility_css:
let computed_properties = dom_element_computed_properties(this) else trap("an element with a box has computed properties")
if css_computed_properties_visibility(computed_properties) == css_data.Visibility.hidden:
return false
# 5. If the contentVisibilityAuto dictionary member of options is true and an ancestor of this in the flat tree
# skips its contents due to content-visibility: auto, return false.
# FIXME: Currently we do not skip any content if content-visibility is auto: https://drafts.csswg.org/css-contain-2/#proximity-to-the-viewport
let skipped_contents_due_to_content_visibility_auto = false
if present_options.content_visibility_auto and skipped_contents_due_to_content_visibility_auto:
var ancestor = dom_node_flat_tree_parent_element(this.dom_node())
while let current = ancestor:
let computed_properties = dom_element_computed_properties(current) else trap("an ancestor with a box has computed properties")
if css_computed_properties_content_visibility(computed_properties) == css_data.ContentVisibility.auto:
return false
ancestor = dom_node_flat_tree_parent_element(current.dom_node())
# 6. Return true.
return true