HTMLTableCellElement.cpp 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. auto color = Color::from_string(value);
  30. if (color.has_value())
  31. style.set_property(CSS::PropertyID::BackgroundColor, CSS::ColorStyleValue::create(color.value()).release_value_but_fixme_should_propagate_errors());
  32. return;
  33. }
  34. if (name == HTML::AttributeNames::align) {
  35. if (value.equals_ignoring_ascii_case("center"sv) || value.equals_ignoring_ascii_case("middle"sv)) {
  36. style.set_property(CSS::PropertyID::TextAlign, CSS::IdentifierStyleValue::create(CSS::ValueID::LibwebCenter).release_value_but_fixme_should_propagate_errors());
  37. } else {
  38. if (auto parsed_value = parse_css_value(CSS::Parser::ParsingContext { document() }, value.view(), CSS::PropertyID::TextAlign).release_value_but_fixme_should_propagate_errors())
  39. style.set_property(CSS::PropertyID::TextAlign, parsed_value.release_nonnull());
  40. }
  41. return;
  42. }
  43. if (name == HTML::AttributeNames::width) {
  44. if (auto parsed_value = parse_nonzero_dimension_value(value))
  45. style.set_property(CSS::PropertyID::Width, parsed_value.release_nonnull());
  46. return;
  47. } else if (name == HTML::AttributeNames::height) {
  48. if (auto parsed_value = parse_nonzero_dimension_value(value))
  49. style.set_property(CSS::PropertyID::Height, parsed_value.release_nonnull());
  50. return;
  51. }
  52. });
  53. }
  54. unsigned int HTMLTableCellElement::col_span() const
  55. {
  56. return attribute(HTML::AttributeNames::colspan).to_uint().value_or(1);
  57. }
  58. void HTMLTableCellElement::set_col_span(unsigned int value)
  59. {
  60. MUST(set_attribute(HTML::AttributeNames::colspan, DeprecatedString::number(value)));
  61. }
  62. unsigned int HTMLTableCellElement::row_span() const
  63. {
  64. return attribute(HTML::AttributeNames::rowspan).to_uint().value_or(1);
  65. }
  66. void HTMLTableCellElement::set_row_span(unsigned int value)
  67. {
  68. MUST(set_attribute(HTML::AttributeNames::rowspan, DeprecatedString::number(value)));
  69. }
  70. Optional<ARIA::Role> HTMLTableCellElement::default_role() const
  71. {
  72. // TODO: For td:
  73. // role=cell if the ancestor table element is exposed as a role=table
  74. // role=gridcell if the ancestor table element is exposed as a role=grid or treegrid
  75. // No corresponding role if the ancestor table element is not exposed as a role=table, grid or treegrid
  76. // For th:
  77. // role=columnheader, rowheader or cell if the ancestor table element is exposed as a role=table
  78. // role=columnheader, rowheader or gridcell if the ancestor table element is exposed as a role=grid or treegrid
  79. // No corresponding role if the ancestor table element is not exposed as a role=table, grid or treegrid
  80. // https://www.w3.org/TR/html-aria/#el-td
  81. // https://www.w3.org/TR/html-aria/#el-th
  82. return {};
  83. }
  84. }