HTMLTableCellElement.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Bindings/HTMLTableCellElementPrototype.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/CSS/ComputedProperties.h>
  10. #include <LibWeb/CSS/Parser/Parser.h>
  11. #include <LibWeb/CSS/StyleValues/CSSColorValue.h>
  12. #include <LibWeb/CSS/StyleValues/CSSKeywordValue.h>
  13. #include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
  14. #include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
  15. #include <LibWeb/DOM/Document.h>
  16. #include <LibWeb/DOM/HTMLCollection.h>
  17. #include <LibWeb/HTML/HTMLTableCellElement.h>
  18. #include <LibWeb/HTML/HTMLTableElement.h>
  19. #include <LibWeb/HTML/Numbers.h>
  20. #include <LibWeb/HTML/Parser/HTMLParser.h>
  21. namespace Web::HTML {
  22. GC_DEFINE_ALLOCATOR(HTMLTableCellElement);
  23. HTMLTableCellElement::HTMLTableCellElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  24. : HTMLElement(document, move(qualified_name))
  25. {
  26. }
  27. HTMLTableCellElement::~HTMLTableCellElement() = default;
  28. void HTMLTableCellElement::initialize(JS::Realm& realm)
  29. {
  30. Base::initialize(realm);
  31. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLTableCellElement);
  32. }
  33. bool HTMLTableCellElement::is_presentational_hint(FlyString const& name) const
  34. {
  35. if (Base::is_presentational_hint(name))
  36. return true;
  37. return first_is_one_of(name,
  38. HTML::AttributeNames::align,
  39. HTML::AttributeNames::background,
  40. HTML::AttributeNames::bgcolor,
  41. HTML::AttributeNames::height,
  42. HTML::AttributeNames::valign,
  43. HTML::AttributeNames::width);
  44. }
  45. void HTMLTableCellElement::apply_presentational_hints(GC::Ref<CSS::CascadedProperties> cascaded_properties) const
  46. {
  47. for_each_attribute([&](auto& name, auto& value) {
  48. if (name == HTML::AttributeNames::bgcolor) {
  49. // https://html.spec.whatwg.org/multipage/rendering.html#tables-2:rules-for-parsing-a-legacy-colour-value
  50. auto color = parse_legacy_color_value(value);
  51. if (color.has_value())
  52. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::BackgroundColor, CSS::CSSColorValue::create_from_color(color.value()));
  53. return;
  54. }
  55. if (name == HTML::AttributeNames::valign) {
  56. if (auto parsed_value = parse_css_value(CSS::Parser::ParsingContext { document() }, value, CSS::PropertyID::VerticalAlign))
  57. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::VerticalAlign, parsed_value.release_nonnull());
  58. return;
  59. }
  60. if (name == HTML::AttributeNames::align) {
  61. if (value.equals_ignoring_ascii_case("center"sv) || value.equals_ignoring_ascii_case("middle"sv)) {
  62. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::TextAlign, CSS::CSSKeywordValue::create(CSS::Keyword::LibwebCenter));
  63. } else if (value.equals_ignoring_ascii_case("left"sv)) {
  64. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::TextAlign, CSS::CSSKeywordValue::create(CSS::Keyword::LibwebLeft));
  65. } else if (value.equals_ignoring_ascii_case("right"sv)) {
  66. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::TextAlign, CSS::CSSKeywordValue::create(CSS::Keyword::LibwebRight));
  67. } else {
  68. if (auto parsed_value = parse_css_value(CSS::Parser::ParsingContext { document() }, value, CSS::PropertyID::TextAlign))
  69. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::TextAlign, parsed_value.release_nonnull());
  70. }
  71. return;
  72. }
  73. if (name == HTML::AttributeNames::width) {
  74. if (auto parsed_value = parse_nonzero_dimension_value(value))
  75. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::Width, parsed_value.release_nonnull());
  76. return;
  77. } else if (name == HTML::AttributeNames::height) {
  78. if (auto parsed_value = parse_nonzero_dimension_value(value))
  79. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::Height, parsed_value.release_nonnull());
  80. return;
  81. } else if (name == HTML::AttributeNames::background) {
  82. // https://html.spec.whatwg.org/multipage/rendering.html#tables-2:encoding-parsing-and-serializing-a-url
  83. if (auto parsed_value = document().encoding_parse_url(value); parsed_value.is_valid())
  84. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::BackgroundImage, CSS::ImageStyleValue::create(parsed_value));
  85. return;
  86. }
  87. });
  88. auto const table_element = first_ancestor_of_type<HTMLTableElement>();
  89. if (!table_element)
  90. return;
  91. if (auto padding = table_element->padding()) {
  92. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::PaddingTop, CSS::LengthStyleValue::create(CSS::Length::make_px(padding)));
  93. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::PaddingBottom, CSS::LengthStyleValue::create(CSS::Length::make_px(padding)));
  94. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::PaddingLeft, CSS::LengthStyleValue::create(CSS::Length::make_px(padding)));
  95. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::PaddingRight, CSS::LengthStyleValue::create(CSS::Length::make_px(padding)));
  96. }
  97. auto border = table_element->border();
  98. if (!border)
  99. return;
  100. auto apply_border_style = [&](CSS::PropertyID style_property, CSS::PropertyID width_property, CSS::PropertyID color_property) {
  101. cascaded_properties->set_property_from_presentational_hint(style_property, CSS::CSSKeywordValue::create(CSS::Keyword::Inset));
  102. cascaded_properties->set_property_from_presentational_hint(width_property, CSS::LengthStyleValue::create(CSS::Length::make_px(1)));
  103. cascaded_properties->set_property_from_presentational_hint(color_property, table_element->computed_properties()->property(color_property));
  104. };
  105. apply_border_style(CSS::PropertyID::BorderLeftStyle, CSS::PropertyID::BorderLeftWidth, CSS::PropertyID::BorderLeftColor);
  106. apply_border_style(CSS::PropertyID::BorderTopStyle, CSS::PropertyID::BorderTopWidth, CSS::PropertyID::BorderTopColor);
  107. apply_border_style(CSS::PropertyID::BorderRightStyle, CSS::PropertyID::BorderRightWidth, CSS::PropertyID::BorderRightColor);
  108. apply_border_style(CSS::PropertyID::BorderBottomStyle, CSS::PropertyID::BorderBottomWidth, CSS::PropertyID::BorderBottomColor);
  109. }
  110. // This implements step 8 in the spec here:
  111. // https://html.spec.whatwg.org/multipage/tables.html#algorithm-for-processing-rows
  112. WebIDL::UnsignedLong HTMLTableCellElement::col_span() const
  113. {
  114. auto col_span_attribute = get_attribute(HTML::AttributeNames::colspan);
  115. if (!col_span_attribute.has_value())
  116. return 1;
  117. auto optional_value_digits = Web::HTML::parse_non_negative_integer_digits(*col_span_attribute);
  118. // If parsing that value failed, or returned zero, or if the attribute is absent, then let colspan be 1, instead.
  119. if (!optional_value_digits.has_value())
  120. return 1;
  121. auto optional_value = optional_value_digits->to_number<i64>(TrimWhitespace::No);
  122. if (optional_value == 0)
  123. return 1;
  124. // NOTE: If there is no value at this point the value must be larger than NumericLimits<i64>::max(), so return the maximum value of 1000.
  125. if (!optional_value.has_value())
  126. return 1000;
  127. auto value = optional_value.value();
  128. // If colspan is greater than 1000, let it be 1000 instead.
  129. if (value > 1000) {
  130. return 1000;
  131. }
  132. return value;
  133. }
  134. WebIDL::ExceptionOr<void> HTMLTableCellElement::set_col_span(WebIDL::UnsignedLong value)
  135. {
  136. if (value > 2147483647)
  137. value = 1;
  138. return set_attribute(HTML::AttributeNames::colspan, String::number(value));
  139. }
  140. // This implements step 9 in the spec here:
  141. // https://html.spec.whatwg.org/multipage/tables.html#algorithm-for-processing-rows
  142. WebIDL::UnsignedLong HTMLTableCellElement::row_span() const
  143. {
  144. auto row_span_attribute = get_attribute(HTML::AttributeNames::rowspan);
  145. if (!row_span_attribute.has_value())
  146. return 1;
  147. // If parsing that value failed or if the attribute is absent, then let rowspan be 1, instead.
  148. auto optional_value_digits = Web::HTML::parse_non_negative_integer_digits(*row_span_attribute);
  149. if (!optional_value_digits.has_value())
  150. return 1;
  151. auto optional_value = optional_value_digits->to_number<i64>(TrimWhitespace::No);
  152. // If rowspan is greater than 65534, let it be 65534 instead.
  153. // NOTE: If there is no value at this point the value must be larger than NumericLimits<i64>::max(), so return the maximum value of 65534.
  154. if (!optional_value.has_value() || *optional_value > 65534)
  155. return 65534;
  156. return *optional_value;
  157. }
  158. WebIDL::ExceptionOr<void> HTMLTableCellElement::set_row_span(WebIDL::UnsignedLong value)
  159. {
  160. if (value > 2147483647)
  161. value = 1;
  162. return set_attribute(HTML::AttributeNames::rowspan, String::number(value));
  163. }
  164. // https://html.spec.whatwg.org/multipage/tables.html#dom-tdth-cellindex
  165. WebIDL::Long HTMLTableCellElement::cell_index() const
  166. {
  167. // The cellIndex IDL attribute must, if the element has a parent tr element, return the index of the cell's
  168. // element in the parent element's cells collection. If there is no such parent element, then the attribute
  169. // must return −1.
  170. auto const* parent = first_ancestor_of_type<HTMLTableRowElement>();
  171. if (!parent)
  172. return -1;
  173. auto rows = parent->cells()->collect_matching_elements();
  174. for (size_t i = 0; i < rows.size(); ++i) {
  175. if (rows[i] == this)
  176. return i;
  177. }
  178. return -1;
  179. }
  180. Optional<ARIA::Role> HTMLTableCellElement::default_role() const
  181. {
  182. if (local_name() == TagNames::th) {
  183. for (auto const* ancestor = parent_element(); ancestor; ancestor = ancestor->parent_element()) {
  184. // AD-HOC: The ancestor checks here aren’t explicitly defined in the spec, but implicitly follow from what
  185. // the spec does state, and from the physical placement/layout of elements. Also, the el-th and el-th-in-row
  186. // tests at https://wpt.fyi/results/html-aam/table-roles.html require doing these ancestor checks — and
  187. // implementing them causes the behavior to match that of other engines.
  188. // https://w3c.github.io/html-aam/#el-th-columnheader
  189. if (get_attribute(HTML::AttributeNames::scope) == "columnheader" || ancestor->local_name() == TagNames::thead)
  190. return ARIA::Role::columnheader;
  191. // https://w3c.github.io/html-aam/#el-th-rowheader
  192. if (get_attribute(HTML::AttributeNames::scope) == "rowheader" || ancestor->local_name() == TagNames::tbody)
  193. return ARIA::Role::rowheader;
  194. }
  195. }
  196. auto const* table_element = first_ancestor_of_type<HTMLTableElement>();
  197. // https://w3c.github.io/html-aam/#el-td
  198. // https://w3c.github.io/html-aam/#el-th/
  199. // (ancestor table element has table role)
  200. if (table_element->role_or_default() == ARIA::Role::table)
  201. return ARIA::Role::cell;
  202. // https://w3c.github.io/html-aam/#el-td-gridcell
  203. // https://w3c.github.io/html-aam/#el-th-gridcell
  204. // (ancestor table element has grid or treegrid role)
  205. if (first_is_one_of(table_element->role_or_default(), ARIA::Role::grid, ARIA::Role::gridcell))
  206. return ARIA::Role::gridcell;
  207. return {};
  208. }
  209. }