HTMLTableCellElement.cpp 3.7 KB

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