dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / html / html_table_cell_element.lucb
#==============================================================================================
#
# html_table_cell_element - HTML::HTMLTableCellElement: <td> and <th>
#
# DESCRIPTION:
# Port of LibWeb/HTML/HTMLTableCellElement.cpp (lines 1-241) and HTMLTableCellElement.h:
# the constructor (which moves the mixin tables to HTMLTableCellElement's, as C++'s vptrs
# move), initialize, the presentational hints (including the table's cellpadding and
# border, which apply to its cells), colSpan/rowSpan, cellIndex and the default role.
#
#==============================================================================================
## Ported from HTMLTableCellElement.cpp:28 Web::HTML::HTMLTableCellElement::HTMLTableCellElement.
pub func html_table_cell_element_construct(this: HtmlTableCellElement*, document: DomDocument*, qualified_name: DomQualifiedName):
html_element_construct(this.html_element(), document, qualified_name)
# The vptrs of the mixin bases move to HTMLTableCellElement's tables.
let element = this.html_element()
element.element.slottable_mixin.vtable = &html_table_cell_element_slottable_mixin_vtable
element.element.aria_mixin.vtable = &html_table_cell_element_aria_mixin_vtable
element.element.animatable.vtable = &html_table_cell_element_animatable_vtable
element.global_event_handlers.vtable = &html_table_cell_element_global_event_handlers_vtable
element.form_associated_element.vtable = &html_table_cell_element_form_associated_element_vtable
## Ported from HTMLTableCellElement.cpp:35 Web::HTML::HTMLTableCellElement::initialize (override).
pub func html_table_cell_element_initialize_impl(base: JsCell*, realm: Realm*):
web_set_prototype_for_interface(base, realm, BindInterfaceName.html_table_cell_element)
html_element_initialize_impl(base, realm)
## Ported from HTMLTableCellElement.cpp:41 Web::HTML::HTMLTableCellElement::is_presentational_hint (override).
pub func html_table_cell_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[7] = [html_attribute_names_align, html_attribute_names_background, html_attribute_names_bgcolor, html_attribute_names_height, html_attribute_names_nowrap, html_attribute_names_valign, html_attribute_names_width]
for candidate in names:
if ak.fly_string_eq_fly_string(&name, &candidate):
return true
return false
## The background attribute's presentational hint, shared by the table elements:
## `CSS::StyleValueList::create({ CSS::ImageStyleValue::create(url) }, CSS::StyleValueList::Separator::Comma)`.
pub func html_table_background_image_value(url: web_url.Url) -> CssStyleValue*:
var values: ak.Vector[CssStyleValue*]
ak.vector_append_t(&values, css_image_style_value_create_url(url).css_style_value())
return css_style_value_list_create(values, CssStyleValueListSeparator.comma).css_style_value()
## `CSS::Parser::ParsingParams { document }`.
pub func html_document_parsing_params(document: DomDocument*) -> CssParsingParams:
var parsing_params: CssParsingParams = ---
css_parsing_params_construct_dom_document(&parsing_params, document)
return parsing_params
## Ported from HTMLTableCellElement.cpp:56 Web::HTML::HTMLTableCellElement::apply_presentational_hints (override).
pub func html_table_cell_element_apply_presentational_hints_impl(base: DomElement*, properties: ak.Vector[CssStyleProperty]*):
dom_element_apply_presentational_hints_impl(base, properties)
let document = dom_node_document(base.dom_node())
for attribute in dom_element_attribute_span(base):
let name = dom_attr_name(attribute)
let value_string = dom_attr_value(attribute)
let value = ak.string_bytes_as_string_view(&value_string)
if ak.fly_string_eq_fly_string(&name, &html_attribute_names_bgcolor):
# https://html.spec.whatwg.org/multipage/rendering.html#tables-2:rules-for-parsing-a-legacy-colour-value
if let color = html_parse_legacy_color_value(value):
ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.background_color, value = css_color_style_value_create_from_color(color, CssColorSyntax.legacy).css_style_value()))
continue
if ak.fly_string_eq_fly_string(&name, &html_attribute_names_valign):
if let parsed_value = parse_css_value(html_document_parsing_params(document), value, css_data.PropertyId.vertical_align):
ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.vertical_align, value = parsed_value))
continue
if ak.fly_string_eq_fly_string(&name, &html_attribute_names_align):
if let parsed_value = html_parse_table_child_element_align_value(value):
ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.text_align, value = parsed_value))
continue
if ak.fly_string_eq_fly_string(&name, &html_attribute_names_width):
if let parsed_value = html_parse_nonzero_dimension_value(value):
ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.width, value = parsed_value))
continue
elif ak.fly_string_eq_fly_string(&name, &html_attribute_names_height):
if let parsed_value = html_parse_nonzero_dimension_value(value):
ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.height, value = parsed_value))
continue
elif ak.fly_string_eq_fly_string(&name, &html_attribute_names_background):
# https://html.spec.whatwg.org/multipage/rendering.html#tables-2:encoding-parsing-and-serializing-a-url
if let parsed_value = dom_document_encoding_parse_url(document, value):
ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.background_image, value = html_table_background_image_value(parsed_value)))
continue
elif ak.fly_string_eq_fly_string(&name, &html_attribute_names_nowrap):
ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.text_wrap_mode, value = css_keyword_style_value_create(css_data.Keyword.nowrap).css_style_value()))
continue
let table_node = tree_node_first_ancestor_of_type(base.dom_node(), &html_table_element_class) else return
let table_element = (HtmlTableElement*)table_node
if let padding = html_table_element_cellpadding(table_element):
let length = css_length_make_px_f64(f64(padding))
ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.padding_top, value = css_length_style_value_create(length).css_style_value()))
ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.padding_bottom, value = css_length_style_value_create(length).css_style_value()))
ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.padding_left, value = css_length_style_value_create(length).css_style_value()))
ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.padding_right, value = css_length_style_value_create(length).css_style_value()))
let border = html_table_element_border(table_element)
if border == 0:
return
html_table_cell_element_apply_border_style(properties, table_element, css_data.PropertyId.border_left_style, css_data.PropertyId.border_left_width, css_data.PropertyId.border_left_color)
html_table_cell_element_apply_border_style(properties, table_element, css_data.PropertyId.border_top_style, css_data.PropertyId.border_top_width, css_data.PropertyId.border_top_color)
html_table_cell_element_apply_border_style(properties, table_element, css_data.PropertyId.border_right_style, css_data.PropertyId.border_right_width, css_data.PropertyId.border_right_color)
html_table_cell_element_apply_border_style(properties, table_element, css_data.PropertyId.border_bottom_style, css_data.PropertyId.border_bottom_width, css_data.PropertyId.border_bottom_color)
## apply_presentational_hints' apply_border_style lambda: an inset 1px border in the table's
## computed border color.
func html_table_cell_element_apply_border_style(properties: ak.Vector[CssStyleProperty]*, table_element: HtmlTableElement*, style_property: css_data.PropertyId, width_property: css_data.PropertyId, color_property: css_data.PropertyId):
ak.vector_append_t(properties, CssStyleProperty(property_id = style_property, value = css_keyword_style_value_create(css_data.Keyword.inset).css_style_value()))
ak.vector_append_t(properties, CssStyleProperty(property_id = width_property, value = css_length_style_value_create(css_length_make_px_f64(1.0)).css_style_value()))
let computed_properties = dom_element_computed_properties(table_element.dom_element()) else trap("the table element has computed properties")
ak.vector_append_t(properties, CssStyleProperty(property_id = color_property, value = css_computed_properties_property(computed_properties, color_property)))
## Ported from HTMLTableCellElement.cpp:124 Web::HTML::HTMLTableCellElement::col_span.
# This implements step 8 in the spec here:
# https://html.spec.whatwg.org/multipage/tables.html#algorithm-for-processing-rows
pub func html_table_cell_element_col_span(this: HtmlTableCellElement*) -> u32:
# If the current cell has a colspan attribute, then parse that attribute's value, and let colspan be the result.
# If parsing that value failed, or returned zero, or if the attribute is absent, then let colspan be 1, instead.
let col_span_attribute = dom_element_get_attribute(this.dom_element(), html_attribute_names_colspan) else return 1
let optional_value_digits = html_parse_non_negative_integer_digits(ak.string_bytes_as_string_view(&col_span_attribute))
let value_digits = optional_value_digits else return 1
let optional_value = ak.string_view_to_number_i64(&value_digits, ak.TrimWhitespace.no)
if optional_value == 0:
return 1
# NB: If there is no value at this point the value must be larger than NumericLimits<i64>::max(), so return the maximum value of 1000.
let value = optional_value else return 1000
# If colspan is greater than 1000, let it be 1000 instead.
if value > 1000:
return 1000
return (u32)value
## Ported from HTMLTableCellElement.cpp:155 Web::HTML::HTMLTableCellElement::set_col_span.
pub func html_table_cell_element_set_col_span(this: HtmlTableCellElement*, value: u32):
var span = value
if span > 2147483647:
span = 1
dom_element_set_attribute_value(this.dom_element(), html_attribute_names_colspan, ak.string_number_u64(span))
## Ported from HTMLTableCellElement.cpp:164 Web::HTML::HTMLTableCellElement::row_span.
# This implements step 9 in the spec here:
# https://html.spec.whatwg.org/multipage/tables.html#algorithm-for-processing-rows
pub func html_table_cell_element_row_span(this: HtmlTableCellElement*) -> u32:
# If the current cell has a rowspan attribute, then parse that attribute's value, and let rowspan be the result.
# If parsing that value failed or if the attribute is absent, then let rowspan be 1, instead.
let row_span_attribute = dom_element_get_attribute(this.dom_element(), html_attribute_names_rowspan) else return 1
let optional_value_digits = html_parse_non_negative_integer_digits(ak.string_bytes_as_string_view(&row_span_attribute))
let value_digits = optional_value_digits else return 1
let optional_value = ak.string_view_to_number_i64(&value_digits, ak.TrimWhitespace.no)
# If rowspan is greater than 65534, let it be 65534 instead.
# NB: If there is no value at this point the value must be larger than NumericLimits<i64>::max(), so return the maximum value of 65534.
let value = optional_value else return 65534
if value > 65534:
return 65534
return (u32)value
## Ported from HTMLTableCellElement.cpp:186 Web::HTML::HTMLTableCellElement::set_row_span.
pub func html_table_cell_element_set_row_span(this: HtmlTableCellElement*, value: u32):
var span = value
if span > 2147483647:
span = 1
dom_element_set_attribute_value(this.dom_element(), html_attribute_names_rowspan, ak.string_number_u64(span))
## Ported from HTMLTableCellElement.cpp:194 Web::HTML::HTMLTableCellElement::cell_index.
# https://html.spec.whatwg.org/multipage/tables.html#dom-tdth-cellindex
pub func html_table_cell_element_cell_index(this: HtmlTableCellElement*) -> i32:
# The cellIndex IDL attribute must, if the element has a parent tr element, return the index of the cell's
# element in the parent element's cells collection. If there is no such parent element, then the attribute
# must return −1.
let parent = tree_node_first_ancestor_of_type(this.dom_node(), &html_table_row_element_class) else return -1
var rows = dom_html_collection_collect_matching_elements(html_table_row_element_cells((HtmlTableRowElement*)parent))
for (i, row) in ak.vector_span(&rows).indexed():
if row == this.dom_element():
return (i32)i
return -1
## Ported from HTMLTableCellElement.cpp:211 Web::HTML::HTMLTableCellElement::default_role (override).
pub func html_table_cell_element_default_role_impl(base: AriaMixin*) -> AriaRole?:
let this = dom_element_from_aria_mixin(base)
let local_name = dom_element_local_name(this)
if ak.fly_string_eq_fly_string(&local_name, &html_tag_names_th):
var ancestor = dom_node_parent_element(this.dom_node())
while let element = ancestor:
# AD-HOC: The ancestor checks here aren’t explicitly defined in the spec, but implicitly follow from what
# the spec does state, and from the physical placement/layout of elements. Also, the el-th and el-th-in-row
# tests at https://wpt.fyi/results/html-aam/table-roles.html require doing these ancestor checks — and
# implementing them causes the behavior to match that of other engines.
let scope = dom_element_get_attribute(this, html_attribute_names_scope)
let ancestor_name = dom_element_local_name(element)
# https://w3c.github.io/html-aam/#el-th-columnheader
if html_table_cell_element_optional_string_is(scope, "columnheader") or ak.fly_string_eq_fly_string(&ancestor_name, &html_tag_names_thead):
return AriaRole.columnheader
# https://w3c.github.io/html-aam/#el-th-rowheader
if html_table_cell_element_optional_string_is(scope, "rowheader") or ak.fly_string_eq_fly_string(&ancestor_name, &html_tag_names_tbody):
return AriaRole.rowheader
ancestor = dom_node_parent_element(element.dom_node())
let table_node = tree_node_first_ancestor_of_type(this.dom_node(), &html_table_element_class) else trap("a table cell's default role needs an ancestor table element")
let table_element = (DomElement*)table_node
# https://w3c.github.io/html-aam/#el-td
# https://w3c.github.io/html-aam/#el-th/
# (ancestor table element has table role)
if aria_mixin_role_or_default(&table_element.aria_mixin) == AriaRole.table:
return AriaRole.cell
# https://w3c.github.io/html-aam/#el-td-gridcell
# https://w3c.github.io/html-aam/#el-th-gridcell
# (ancestor table element has grid or treegrid role)
let table_role = aria_mixin_role_or_default(&table_element.aria_mixin)
if table_role == AriaRole.grid or table_role == AriaRole.gridcell:
return AriaRole.gridcell
return none
## `Optional<String> == "literal"`: present and equal.
func html_table_cell_element_optional_string_is(value: ak.String?, text: str) -> bool:
let string = value else return false
return ak.string_eq_string_view(&string, ak.sv(text))
## Ported from HTMLTableCellElement.h:34 Web::HTML::HTMLTableCellElement::is_html_table_cell_element (override).
pub func html_table_cell_element_is_html_table_cell_element_impl(base: DomNode*) -> bool:
return true