HTMLTableCellElement.cpp 4.5 KB

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