HTMLTableCellElement.cpp 2.7 KB

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