dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / dom / element_2.lucb
#==============================================================================================
#
# element_2 - DOM::Element: setting, removing and toggling attributes; layout nodes
#
# DESCRIPTION:
# Port of LibWeb/DOM/Element.cpp (lines 541-918): setAttributeNS, appending and
# setting attribute values, setAttributeNode(NS), removeAttribute(NS|Node),
# hasAttribute(NS), toggleAttribute, getAttributeNames, the attr-associated element(s),
# creating layout nodes for a display type, the attribute change steps and the
# invalidation a style change requires.
#
#==============================================================================================
# mark: Setting attributes ======================================================================
## Ported from Element.cpp:542 Web::DOM::Element::set_attribute_ns_for_bindings.
# https://dom.spec.whatwg.org/#dom-element-setattributens
pub func dom_element_set_attribute_ns_for_bindings(this: DomElement*, namespace_: ak.FlyString?, qualified_name: ak.FlyString, value: TrustedTypesTrustedHtmlOrTrustedTypesTrustedScriptOrTrustedTypesTrustedScriptUrlOrUtf16String) -> !:
# 1. Let (namespace, prefix, localName) be the result of validating and extracting namespace and qualifiedName given "attribute".
var extracted_qualified_name = try dom_validate_and_extract(dom_element_realm(this), namespace_, qualified_name, DomValidationContext.attribute)
# 2. Let verifiedValue be the result of calling get Trusted Types-compliant attribute value
# with localName, namespace, this, and value.
var attribute_ns: ak.Utf16String? = none
if let namespace_value = dom_qualified_name_namespace(&extracted_qualified_name):
attribute_ns = ak.utf16_string_from_utf8_fly_string(&namespace_value)
let verified_value = try trusted_types_get_trusted_types_compliant_attribute_value(dom_qualified_name_local_name(&extracted_qualified_name), attribute_ns, this, value)
# 3. Set an attribute value for this using localName, verifiedValue, and also prefix and namespace.
dom_element_set_attribute_value(this, dom_qualified_name_local_name(&extracted_qualified_name), ak.utf16_string_base_to_utf8_but_should_be_ported_to_utf16(&verified_value.utf16_string_base), dom_qualified_name_prefix(&extracted_qualified_name), dom_qualified_name_namespace(&extracted_qualified_name))
## Ported from Element.cpp:562 Web::DOM::Element::append_attribute.
# https://dom.spec.whatwg.org/#concept-element-attributes-append
pub func dom_element_append_attribute_fly_string(this: DomElement*, name: ak.FlyString, value: ak.String):
let attributes = dom_element_attributes(this) else trap("Element::attributes() is never null")
dom_named_node_map_append_attribute(attributes, dom_attr_create_fly_string(dom_element_document(this), name, value))
## Ported from Element.cpp:568 Web::DOM::Element::append_attribute.
# https://dom.spec.whatwg.org/#concept-element-attributes-append
pub func dom_element_append_attribute_dom_attr(this: DomElement*, attribute: DomAttr*):
let attributes = dom_element_attributes(this) else trap("Element::attributes() is never null")
dom_named_node_map_append_attribute(attributes, attribute)
## Ported from Element.cpp:574 Web::DOM::Element::set_attribute_value.
# https://dom.spec.whatwg.org/#concept-element-attributes-set-value
pub func dom_element_set_attribute_value(this: DomElement*, local_name: ak.FlyString, value: ak.String, prefix: ak.FlyString? = none, namespace_: ak.FlyString? = none):
# 1. Let attribute be the result of getting an attribute given namespace, localName, and element.
let attributes = dom_element_attributes(this) else trap("Element::attributes() is never null")
let attribute = dom_named_node_map_get_attribute_ns(attributes, namespace_, local_name)
# 2. If attribute is null, create an attribute whose namespace is namespace, namespace prefix is prefix, local name
# is localName, value is value, and node document is element’s node document, then append this attribute to element,
# and then return.
if attribute == none:
var name: DomQualifiedName = ---
dom_qualified_name_construct(&name, local_name, prefix, namespace_)
let new_attribute = dom_attr_create_dom_qualified_name(dom_element_document(this), name, value)
dom_named_node_map_append_attribute(attributes, new_attribute)
return
# 3. Change attribute to value.
dom_attr_change_attribute(attribute else trap("unreachable"), value)
## Ported from Element.cpp:596 Web::DOM::Element::set_attribute_node_for_bindings.
# https://dom.spec.whatwg.org/#dom-element-setattributenode
pub func dom_element_set_attribute_node_for_bindings(this: DomElement*, attr: DomAttr*) -> DomAttr*?!:
# The setAttributeNode(attr) and setAttributeNodeNS(attr) methods steps are to return the result of setting an attribute given attr and this.
let attributes = dom_element_attributes(this) else trap("Element::attributes() is never null")
return try dom_named_node_map_set_attribute(attributes, attr)
## Ported from Element.cpp:603 Web::DOM::Element::set_attribute_node_ns_for_bindings.
# https://dom.spec.whatwg.org/#dom-element-setattributenodens
pub func dom_element_set_attribute_node_ns_for_bindings(this: DomElement*, attr: DomAttr*) -> DomAttr*?!:
# The setAttributeNode(attr) and setAttributeNodeNS(attr) methods steps are to return the result of setting an attribute given attr and this.
let attributes = dom_element_attributes(this) else trap("Element::attributes() is never null")
return try dom_named_node_map_set_attribute(attributes, attr)
# mark: Removing, testing and toggling attributes ===============================================
## Ported from Element.cpp:610 Web::DOM::Element::remove_attribute.
# https://dom.spec.whatwg.org/#dom-element-removeattribute
pub func dom_element_remove_attribute(this: DomElement*, name: ak.FlyString):
# The removeAttribute(qualifiedName) method steps are to remove an attribute given qualifiedName and this, and then return undefined.
let attributes = this.m_attributes else return
_ = dom_named_node_map_remove_attribute(attributes, name)
## Ported from Element.cpp:619 Web::DOM::Element::remove_attribute_ns.
# https://dom.spec.whatwg.org/#dom-element-removeattributens
pub func dom_element_remove_attribute_ns(this: DomElement*, namespace_: ak.FlyString?, name: ak.FlyString):
# The removeAttributeNS(namespace, localName) method steps are to remove an attribute given namespace, localName, and this, and then return undefined.
let attributes = this.m_attributes else return
_ = dom_named_node_map_remove_attribute_ns(attributes, namespace_, name)
## Ported from Element.cpp:628 Web::DOM::Element::remove_attribute_node.
# https://dom.spec.whatwg.org/#dom-element-removeattributenode
pub func dom_element_remove_attribute_node(this: DomElement*, attr: DomAttr*) -> DomAttr*!:
let attributes = dom_element_attributes(this) else trap("Element::attributes() is never null")
return try dom_named_node_map_remove_attribute_node(attributes, attr)
## Ported from Element.cpp:634 Web::DOM::Element::has_attribute.
# https://dom.spec.whatwg.org/#dom-element-hasattribute
pub func dom_element_has_attribute(this: DomElement*, name: ak.FlyString) -> bool:
let attributes = this.m_attributes else return false
return dom_named_node_map_get_attribute(attributes, name) != none
## Ported from Element.cpp:642 Web::DOM::Element::has_attribute_ns.
# https://dom.spec.whatwg.org/#dom-element-hasattributens
pub func dom_element_has_attribute_ns(this: DomElement*, namespace_: ak.FlyString?, name: ak.FlyString) -> bool:
let attributes = this.m_attributes else return false
# 1. If namespace is the empty string, then set it to null.
# 2. Return true if this has an attribute whose namespace is namespace and local name is localName; otherwise false.
if dom_node_optional_fly_string_is(namespace_, ak.fly_string_new()):
return dom_named_node_map_get_attribute_ns(attributes, none, name) != none
return dom_named_node_map_get_attribute_ns(attributes, namespace_, name) != none
## Ported from Element.cpp:656 Web::DOM::Element::toggle_attribute.
# https://dom.spec.whatwg.org/#dom-element-toggleattribute
pub func dom_element_toggle_attribute(this: DomElement*, name: ak.FlyString, force: bool?) -> bool!:
# 1. If qualifiedName is not a valid attribute local name, then throw an "InvalidCharacterError" DOMException.
if not dom_is_valid_attribute_local_name(name):
return try idl_throw(.idl_dom_exception(idl_invalid_character_error_create(dom_element_realm(this), ak.utf16_string_from_str("Attribute name must not be empty or contain invalid characters"))))
# 2. If this is in the HTML namespace and its node document is an HTML document, then set qualifiedName to qualifiedName in ASCII lowercase.
let insert_as_lowercase = dom_element_is_html_element_in_html_document(this)
# 3. Let attribute be the first attribute in this’s attribute list whose qualified name is qualifiedName, and null otherwise.
let attributes = dom_element_attributes(this) else trap("Element::attributes() is never null")
let attribute = dom_named_node_map_get_attribute(attributes, name)
# 4. If attribute is null, then:
if attribute == none:
# 1. If force is not given or is true, create an attribute whose local name is qualifiedName, value is the empty
# string, and node document is this’s node document, then append this attribute to this, and then return true.
if force == none or force == true:
let new_name = ak.fly_string_to_ascii_lowercase(&name) if insert_as_lowercase else name
let new_attribute = dom_attr_create_fly_string(dom_element_document(this), new_name, ak.string_new())
dom_named_node_map_append_attribute(attributes, new_attribute)
return true
# 2. Return false.
return false
# 5. Otherwise, if force is not given or is false, remove an attribute given qualifiedName and this, and then return false.
if force == none or force == false:
_ = dom_named_node_map_remove_attribute(attributes, name)
return false
# 6. Return true.
return true
## Ported from Element.cpp:694 Web::DOM::Element::get_attribute_names.
# https://dom.spec.whatwg.org/#dom-element-getattributenames
pub func dom_element_get_attribute_names(this: DomElement*) -> ak.Vector[ak.String]:
# The getAttributeNames() method steps are to return the qualified names of the attributes in this’s attribute list, in order; otherwise a new list.
var names: ak.Vector[ak.String]
let attributes = this.m_attributes else return names
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)
ak.vector_append_t(&names, ak.fly_string_to_string_const(&name))
return names
# mark: Attr-associated elements ================================================================
## Ported from Element.cpp:708 Web::DOM::Element::get_the_attribute_associated_element.
# https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#attr-associated-element
pub func dom_element_get_the_attribute_associated_element(this: DomElement*, content_attribute: ak.FlyString, explicitly_set_attribute_element: DomElement*?) -> DomElement*?:
# 1. Let element be the result of running reflectedTarget's get the element.
let element = this
# 2. Let contentAttributeValue be the result of running reflectedTarget's get the content attribute.
let content_attribute_value = dom_element_get_attribute(element, content_attribute)
# 3. If reflectedTarget's explicitly set attr-element is not null:
if let explicitly_set = explicitly_set_attribute_element:
# 1. If reflectedTarget's explicitly set attr-element is a descendant of any of element's shadow-including
# ancestors, then return reflectedTarget's explicitly set attr-element.
if tree_node_root(explicitly_set.dom_node()) == dom_node_shadow_including_root(element.dom_node()):
return explicitly_set
# 2. Return null.
return none
# 4. Otherwise, if contentAttributeValue is not null, return the first element candidate, in tree order, that meets
# the following criteria:
# * candidate's root is the same as element's root;
# * candidate's ID is contentAttributeValue; and
# * candidate implements T.
if let value = content_attribute_value:
return dom_parent_node_get_element_by_id(dom_element_document(element).dom_parent_node(), ak.fly_string_from_string(value))
# 5. If no such element exists, then return null.
# 6. Return null.
return none
## Ported from Element.cpp:741 Web::DOM::Element::get_the_attribute_associated_elements.
# https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#attr-associated-elements
pub func dom_element_get_the_attribute_associated_elements(this: DomElement*, content_attribute: ak.FlyString, explicitly_set_attribute_elements: const ak.Vector[gc.Weak[DomElement]]*?) -> ak.Vector[DomElement*]?:
# 1. Let elements be an empty list.
var elements: ak.Vector[DomElement*]
# 2. Let element be the result of running reflectedTarget's get the element.
let element = this
# 3. If reflectedTarget's explicitly set attr-elements is not null:
if let explicitly_set = explicitly_set_attribute_elements:
# 1. For each attrElement in reflectedTarget's explicitly set attr-elements:
for i in 0..<ak.vector_size(explicitly_set):
# 1. If attrElement is not a descendant of any of element's shadow-including ancestors, then continue.
let attribute_element = gc.weak_ptr(ak.vector_at_const(explicitly_set, i)) else continue
if tree_node_root(attribute_element.dom_node()) != dom_node_shadow_including_root(element.dom_node()):
continue
# 2. Append attrElement to elements.
ak.vector_append_t(&elements, attribute_element)
# 4. Otherwise:
else:
# 1. Let contentAttributeValue be the result of running reflectedTarget's get the content attribute.
let content_attribute_value = dom_element_get_attribute(element, content_attribute)
# 2. If contentAttributeValue is null, then return null.
let value = content_attribute_value else return none
# 3. Let tokens be contentAttributeValue, split on ASCII whitespace.
let view = ak.string_bytes_as_string_view(&value)
let predicate = DomElementIsAsciiWhitespace(unused = 0)
var tokens = ak.string_view_split_view_if(&view, &predicate)
# 4. For each id of tokens:
for id in ak.vector_span(&tokens):
# 1. Let candidate be the first element, in tree order, that meets the following criteria:
# * candidate's root is the same as element's root;
# * candidate's ID is id; and
# * candidate implements T.
let id_string = ak.fly_string_from_utf8(id) else trap("MUST: FlyString::from_utf8")
let candidate = dom_parent_node_get_element_by_id(dom_element_document(element).dom_parent_node(), id_string)
# 2. If no such element exists, then continue.
let found = candidate else continue
# 3. Append candidate to elements.
ak.vector_append_t(&elements, found)
# 5. Return elements.
return elements
# mark: Layout nodes ============================================================================
## Ported from Element.cpp:794 Web::DOM::Element::create_layout_node (virtual).
pub func dom_element_create_layout_node_impl(this: DomElement*, style: CssComputedProperties*) -> LayoutNode*?:
let local_name = dom_element_local_name(this)
if ak.fly_string_eq_string_view(&local_name, ak.sv("noscript")) and dom_node_is_scripting_enabled(dom_element_document(this).dom_node()):
return none
let display = css_computed_properties_display(style)
let node = dom_element_create_layout_node_for_display_type(dom_element_document(this), display, style, this) else return none
return node.layout_node()
## `document.heap().allocate<T>(document, element, style)` for the layout node classes: the generated
## creation helpers in the document's realm (a layout node's initialize is JS::Cell's, which does nothing).
func dom_element_document_realm(document: DomDocument*) -> Realm*:
return bind_platform_object_realm(document.bind_platform_object())
## Ported from Element.cpp:803 Web::DOM::Element::create_layout_node_for_display_type.
pub func dom_element_create_layout_node_for_display_type(document: DomDocument*, display: CssDisplay, style: CssComputedProperties*, element: DomElement*?) -> LayoutNodeWithStyle*?:
let realm = dom_element_document_realm(document)
let node = (DomNode*?)element
if css_display_is_none(&display):
return none
if css_display_is_table_inside(&display) or css_display_is_table_row_group(&display) or css_display_is_table_header_group(&display) or css_display_is_table_footer_group(&display) or css_display_is_table_row(&display):
return realm_create_layout_box_css_computed_properties(realm, document, node, style).layout_node_with_style()
if css_display_is_list_item(&display):
return realm_create_layout_list_item_box(realm, document, element, style).layout_node_with_style()
if css_display_is_table_cell(&display):
return realm_create_layout_block_container_css_computed_properties(realm, document, node, style).layout_node_with_style()
if css_display_is_table_column(&display) or css_display_is_table_column_group(&display) or css_display_is_table_caption(&display):
# FIXME: This is just an incorrect placeholder until we improve table layout support.
return realm_create_layout_block_container_css_computed_properties(realm, document, node, style).layout_node_with_style()
if css_display_is_math_inside(&display):
# https://w3c.github.io/mathml-core/#new-display-math-value
# MathML elements with a computed display value equal to block math or inline math control box generation
# and layout according to their tag name, as described in the relevant sections.
# FIXME: Figure out what kind of node we should make for them. For now, we'll stick with a generic Box.
return realm_create_layout_block_container_css_computed_properties(realm, document, node, style).layout_node_with_style()
if css_display_is_inline_outside(&display):
if css_display_is_flow_root_inside(&display):
return realm_create_layout_block_container_css_computed_properties(realm, document, node, style).layout_node_with_style()
if css_display_is_flow_inside(&display):
return realm_create_layout_inline_node(realm, document, element, style).layout_node_with_style()
if css_display_is_flex_inside(&display):
return realm_create_layout_box_css_computed_properties(realm, document, node, style).layout_node_with_style()
if css_display_is_grid_inside(&display):
return realm_create_layout_box_css_computed_properties(realm, document, node, style).layout_node_with_style()
# dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Support display: {}", display.to_string()): LIBWEB_CSS_DEBUG is off.
return realm_create_layout_inline_node(realm, document, element, style).layout_node_with_style()
if css_display_is_flex_inside(&display) or css_display_is_grid_inside(&display):
return realm_create_layout_box_css_computed_properties(realm, document, node, style).layout_node_with_style()
if css_display_is_flow_inside(&display) or css_display_is_flow_root_inside(&display) or css_display_is_contents(&display):
return realm_create_layout_block_container_css_computed_properties(realm, document, node, style).layout_node_with_style()
let display_string = css_display_to_string(&display)
ak.dbgln(f"FIXME: CSS display '{ak.string_view_text(ak.string_bytes_as_string_view(&display_string))}' not implemented yet.")
# FIXME: We don't actually support `display: block ruby`, this is just a hack to prevent a crash
if css_display_is_ruby_inside(&display):
return realm_create_layout_block_container_css_computed_properties(realm, document, node, style).layout_node_with_style()
return realm_create_layout_inline_node(realm, document, element, style).layout_node_with_style()
# mark: Attribute changes and style invalidation ================================================
## Ported from Element.cpp:858 Web::DOM::Element::run_attribute_change_steps.
pub func dom_element_run_attribute_change_steps(this: DomElement*, local_name: ak.FlyString, old_value: ak.String?, value: ak.String?, namespace_: ak.FlyString?):
dom_element_attribute_changed(this, local_name, old_value, value, namespace_)
if not dom_node_optional_strings_equal(old_value, value):
css_invalidation_invalidate_style_after_attribute_change(this, local_name, old_value, value)
dom_document_bump_dom_tree_version(dom_element_document(this))
## `ValueComparingRefPtr<CounterStyle const> == ValueComparingRefPtr<CounterStyle const>`: the same
## pointer, or both non-null and equal by value.
func dom_element_counter_styles_equal(a: CssCounterStyle*?, b: CssCounterStyle*?) -> bool:
if a == b:
return true
let a_value = a else return false
let b_value = b else return false
return css_counter_style_equals(a_value, b_value)
## `Vector<ValueComparingRefPtr<CounterStyle const>> == ...`: the same length and pairwise equal.
func dom_element_counter_style_vectors_equal(a: const ak.Vector[CssCounterStyle*?]*, b: const ak.Vector[CssCounterStyle*?]*) -> bool:
if ak.vector_size(a) != ak.vector_size(b):
return false
for i in 0..<ak.vector_size(a):
if not dom_element_counter_styles_equal(*ak.vector_at_const(a, i), *ak.vector_at_const(b, i)):
return false
return true
## Ported from Element.cpp:868 Web::DOM::compute_required_invalidation.
func dom_compute_required_invalidation(old_style: CssComputedProperties*, new_style: CssComputedProperties*, font_computer: CssFontComputer*, old_layout_node: LayoutNodeWithStyle*?, abstract_element: DomAbstractElement*) -> CssRequiredInvalidationAfterStyleChange:
var invalidation: CssRequiredInvalidationAfterStyleChange
# ValueComparingNonnullRefPtr<FontCascadeList const>: the same pointer or equal by value.
let old_font_list = css_computed_properties_computed_font_list(old_style, font_computer)
let new_font_list = css_computed_properties_computed_font_list(new_style, font_computer)
if not (old_font_list == new_font_list or web_fonts.font_cascade_list_equals(old_font_list, new_font_list)):
invalidation.relayout = true
for i in (u16)css_data.first_longhand_property_id..=(u16)css_data.last_longhand_property_id:
let property_id = (css_data.PropertyId)i
let old_value = css_computed_properties_property(old_style, property_id)
let new_value = css_computed_properties_property(new_style, property_id)
if old_value == new_value:
continue
css_required_invalidation_after_style_change_or_assign(&invalidation, css_compute_property_invalidation(property_id, old_value, new_value))
# NB: Even if the computed value hasn't changed the resolved counter style may have (e.g. if the relevant
# @counter-style rule was modified, or a new rule with the same name took precedence over the old one).
if let layout_node = old_layout_node:
let old_computed_values = layout_node_with_style_computed_values(layout_node)
let old_content = css_computed_values_content(&old_computed_values.computed_values)
# NB: We only propagate content to computed values for relevant elements so if the old layout node doesn't
# have a value for content we know it isn't a relevant element and invalidation isn't required.
if let old_content_data = old_content:
let new_content = css_computed_properties_content(new_style, abstract_element, 0)
if not dom_element_counter_style_vectors_equal(&old_content_data.counter_style_dependencies, &new_content.content_data.counter_style_dependencies):
invalidation.rebuild_layout_tree = true
invalidation.relayout = true
invalidation.repaint = true
let old_list_style_type = css_computed_values_list_style_type(&old_computed_values.computed_values)
let (old_has_counter_style, old_counter_style) = dom_element_list_style_type_counter_style(old_list_style_type)
if old_has_counter_style:
let new_list_style_type = css_computed_properties_list_style_type(new_style, dom_abstract_element_style_scope(abstract_element))
let (new_has_counter_style, new_counter_style) = dom_element_list_style_type_counter_style(new_list_style_type)
if new_has_counter_style:
if not dom_element_counter_styles_equal(old_counter_style, new_counter_style):
invalidation.rebuild_layout_tree = true
invalidation.relayout = true
invalidation.repaint = true
return invalidation
## `list_style_type.has<RefPtr<CSS::CounterStyle const>>()` and, when it does, that (possibly null) counter style.
func dom_element_list_style_type_counter_style(list_style_type: CssListStyleType) -> (bool, CssCounterStyle*?):
match list_style_type:
.css_counter_style(counter_style): return (true, counter_style)
_: return (false, none)