HTMLTableCellElement.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/Parser/Parser.h>
  7. #include <LibWeb/HTML/HTMLTableCellElement.h>
  8. #include <LibWeb/HTML/Parser/HTMLParser.h>
  9. #include <LibWeb/HTML/Window.h>
  10. namespace Web::HTML {
  11. HTMLTableCellElement::HTMLTableCellElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  12. : HTMLElement(document, move(qualified_name))
  13. {
  14. set_prototype(&window().cached_web_prototype("HTMLTableCellElement"));
  15. }
  16. HTMLTableCellElement::~HTMLTableCellElement() = default;
  17. void HTMLTableCellElement::apply_presentational_hints(CSS::StyleProperties& style) const
  18. {
  19. for_each_attribute([&](auto& name, auto& value) {
  20. if (name == HTML::AttributeNames::bgcolor) {
  21. auto color = Color::from_string(value);
  22. if (color.has_value())
  23. style.set_property(CSS::PropertyID::BackgroundColor, CSS::ColorStyleValue::create(color.value()));
  24. return;
  25. }
  26. if (name == HTML::AttributeNames::align) {
  27. if (value.equals_ignoring_case("center"sv) || value.equals_ignoring_case("middle"sv)) {
  28. style.set_property(CSS::PropertyID::TextAlign, CSS::IdentifierStyleValue::create(CSS::ValueID::LibwebCenter));
  29. } else {
  30. if (auto parsed_value = parse_css_value(CSS::Parser::ParsingContext { document() }, value.view(), CSS::PropertyID::TextAlign))
  31. style.set_property(CSS::PropertyID::TextAlign, parsed_value.release_nonnull());
  32. }
  33. return;
  34. }
  35. if (name == HTML::AttributeNames::width) {
  36. if (auto parsed_value = parse_nonzero_dimension_value(value))
  37. style.set_property(CSS::PropertyID::Width, parsed_value.release_nonnull());
  38. return;
  39. } else if (name == HTML::AttributeNames::height) {
  40. if (auto parsed_value = parse_nonzero_dimension_value(value))
  41. style.set_property(CSS::PropertyID::Height, parsed_value.release_nonnull());
  42. return;
  43. }
  44. });
  45. }
  46. unsigned int HTMLTableCellElement::col_span() const
  47. {
  48. return attribute(HTML::AttributeNames::colspan).to_uint().value_or(1);
  49. }
  50. void HTMLTableCellElement::set_col_span(unsigned int value)
  51. {
  52. set_attribute(HTML::AttributeNames::colspan, String::number(value));
  53. }
  54. unsigned int HTMLTableCellElement::row_span() const
  55. {
  56. return attribute(HTML::AttributeNames::rowspan).to_uint().value_or(1);
  57. }
  58. void HTMLTableCellElement::set_row_span(unsigned int value)
  59. {
  60. set_attribute(HTML::AttributeNames::rowspan, String::number(value));
  61. }
  62. }