HTMLTableCellElement.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/CSS/Parser/Parser.h>
  8. #include <LibWeb/CSS/StyleProperties.h>
  9. #include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
  10. #include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
  11. #include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
  12. #include <LibWeb/DOM/Document.h>
  13. #include <LibWeb/HTML/HTMLTableCellElement.h>
  14. #include <LibWeb/HTML/Parser/HTMLParser.h>
  15. namespace Web::HTML {
  16. HTMLTableCellElement::HTMLTableCellElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  17. : HTMLElement(document, move(qualified_name))
  18. {
  19. }
  20. HTMLTableCellElement::~HTMLTableCellElement() = default;
  21. void HTMLTableCellElement::initialize(JS::Realm& realm)
  22. {
  23. Base::initialize(realm);
  24. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLTableCellElementPrototype>(realm, "HTMLTableCellElement"));
  25. }
  26. void HTMLTableCellElement::apply_presentational_hints(CSS::StyleProperties& style) const
  27. {
  28. for_each_attribute([&](auto& name, auto& value) {
  29. if (name == HTML::AttributeNames::bgcolor) {
  30. // https://html.spec.whatwg.org/multipage/rendering.html#tables-2:rules-for-parsing-a-legacy-colour-value
  31. auto color = parse_legacy_color_value(value);
  32. if (color.has_value())
  33. style.set_property(CSS::PropertyID::BackgroundColor, CSS::ColorStyleValue::create(color.value()));
  34. return;
  35. }
  36. if (name == HTML::AttributeNames::valign) {
  37. if (auto parsed_value = parse_css_value(CSS::Parser::ParsingContext { document() }, value.view(), CSS::PropertyID::VerticalAlign))
  38. style.set_property(CSS::PropertyID::VerticalAlign, parsed_value.release_nonnull());
  39. return;
  40. }
  41. if (name == HTML::AttributeNames::align) {
  42. if (value.equals_ignoring_ascii_case("center"sv) || value.equals_ignoring_ascii_case("middle"sv)) {
  43. style.set_property(CSS::PropertyID::TextAlign, CSS::IdentifierStyleValue::create(CSS::ValueID::LibwebCenter));
  44. } else {
  45. if (auto parsed_value = parse_css_value(CSS::Parser::ParsingContext { document() }, value.view(), CSS::PropertyID::TextAlign))
  46. style.set_property(CSS::PropertyID::TextAlign, parsed_value.release_nonnull());
  47. }
  48. return;
  49. }
  50. if (name == HTML::AttributeNames::width) {
  51. if (auto parsed_value = parse_nonzero_dimension_value(value))
  52. style.set_property(CSS::PropertyID::Width, parsed_value.release_nonnull());
  53. return;
  54. } else if (name == HTML::AttributeNames::height) {
  55. if (auto parsed_value = parse_nonzero_dimension_value(value))
  56. style.set_property(CSS::PropertyID::Height, parsed_value.release_nonnull());
  57. return;
  58. } else if (name == HTML::AttributeNames::background) {
  59. if (auto parsed_value = document().parse_url(value); parsed_value.is_valid())
  60. style.set_property(CSS::PropertyID::BackgroundImage, CSS::ImageStyleValue::create(parsed_value));
  61. return;
  62. }
  63. });
  64. }
  65. unsigned int HTMLTableCellElement::col_span() const
  66. {
  67. return attribute(HTML::AttributeNames::colspan).to_uint().value_or(1);
  68. }
  69. WebIDL::ExceptionOr<void> HTMLTableCellElement::set_col_span(unsigned int value)
  70. {
  71. return set_attribute(HTML::AttributeNames::colspan, DeprecatedString::number(value));
  72. }
  73. unsigned int HTMLTableCellElement::row_span() const
  74. {
  75. return attribute(HTML::AttributeNames::rowspan).to_uint().value_or(1);
  76. }
  77. WebIDL::ExceptionOr<void> HTMLTableCellElement::set_row_span(unsigned int value)
  78. {
  79. return set_attribute(HTML::AttributeNames::rowspan, DeprecatedString::number(value));
  80. }
  81. Optional<ARIA::Role> HTMLTableCellElement::default_role() const
  82. {
  83. // TODO: For td:
  84. // role=cell if the ancestor table element is exposed as a role=table
  85. // role=gridcell if the ancestor table element is exposed as a role=grid or treegrid
  86. // No corresponding role if the ancestor table element is not exposed as a role=table, grid or treegrid
  87. // For th:
  88. // role=columnheader, rowheader or cell if the ancestor table element is exposed as a role=table
  89. // role=columnheader, rowheader or gridcell if the ancestor table element is exposed as a role=grid or treegrid
  90. // No corresponding role if the ancestor table element is not exposed as a role=table, grid or treegrid
  91. // https://www.w3.org/TR/html-aria/#el-td
  92. // https://www.w3.org/TR/html-aria/#el-th
  93. return {};
  94. }
  95. }