HTMLTableCellElement.cpp 3.6 KB

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