HTMLTableCellElement.cpp 3.8 KB

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