HTMLTableCellElement.cpp 4.9 KB

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