HTMLTableCellElement.cpp 3.7 KB

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