HTMLMarqueeElement.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Bindings/HTMLMarqueeElementPrototype.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/CSS/StyleProperties.h>
  10. #include <LibWeb/CSS/StyleValues/CSSColorValue.h>
  11. #include <LibWeb/HTML/HTMLMarqueeElement.h>
  12. #include <LibWeb/HTML/Numbers.h>
  13. #include <LibWeb/HTML/Parser/HTMLParser.h>
  14. namespace Web::HTML {
  15. GC_DEFINE_ALLOCATOR(HTMLMarqueeElement);
  16. HTMLMarqueeElement::HTMLMarqueeElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  17. : HTMLElement(document, move(qualified_name))
  18. {
  19. }
  20. HTMLMarqueeElement::~HTMLMarqueeElement() = default;
  21. void HTMLMarqueeElement::initialize(JS::Realm& realm)
  22. {
  23. Base::initialize(realm);
  24. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLMarqueeElement);
  25. }
  26. void HTMLMarqueeElement::apply_presentational_hints(CSS::StyleProperties& style) const
  27. {
  28. HTMLElement::apply_presentational_hints(style);
  29. for_each_attribute([&](auto& name, auto& value) {
  30. if (name == HTML::AttributeNames::bgcolor) {
  31. // https://html.spec.whatwg.org/multipage/rendering.html#the-marquee-element-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::CSSColorValue::create_from_color(color.value()));
  35. } else if (name == HTML::AttributeNames::height) {
  36. // https://html.spec.whatwg.org/multipage/rendering.html#the-marquee-element-2:maps-to-the-dimension-property
  37. if (auto parsed_value = parse_dimension_value(value)) {
  38. style.set_property(CSS::PropertyID::Height, *parsed_value);
  39. }
  40. } else if (name == HTML::AttributeNames::hspace) {
  41. if (auto parsed_value = parse_dimension_value(value)) {
  42. style.set_property(CSS::PropertyID::MarginLeft, *parsed_value);
  43. style.set_property(CSS::PropertyID::MarginRight, *parsed_value);
  44. }
  45. } else if (name == HTML::AttributeNames::vspace) {
  46. if (auto parsed_value = parse_dimension_value(value)) {
  47. style.set_property(CSS::PropertyID::MarginTop, *parsed_value);
  48. style.set_property(CSS::PropertyID::MarginBottom, *parsed_value);
  49. }
  50. } else if (name == HTML::AttributeNames::width) {
  51. if (auto parsed_value = parse_dimension_value(value)) {
  52. style.set_property(CSS::PropertyID::Width, *parsed_value);
  53. }
  54. }
  55. });
  56. }
  57. // https://html.spec.whatwg.org/multipage/obsolete.html#dom-marquee-scrollamount
  58. WebIDL::UnsignedLong HTMLMarqueeElement::scroll_amount()
  59. {
  60. // The scrollAmount IDL attribute must reflect the scrollamount content attribute. The default value is 6.
  61. if (auto scroll_amount_string = get_attribute(HTML::AttributeNames::scrollamount); scroll_amount_string.has_value()) {
  62. if (auto scroll_amount = parse_non_negative_integer(*scroll_amount_string); scroll_amount.has_value())
  63. return *scroll_amount;
  64. }
  65. return 6;
  66. }
  67. // https://html.spec.whatwg.org/multipage/obsolete.html#dom-marquee-scrollamount
  68. WebIDL::ExceptionOr<void> HTMLMarqueeElement::set_scroll_amount(WebIDL::UnsignedLong value)
  69. {
  70. return set_attribute(HTML::AttributeNames::scrollamount, String::number(value));
  71. }
  72. // https://html.spec.whatwg.org/multipage/obsolete.html#dom-marquee-scrolldelay
  73. WebIDL::UnsignedLong HTMLMarqueeElement::scroll_delay()
  74. {
  75. // The scrollDelay IDL attribute must reflect the scrolldelay content attribute. The default value is 85.
  76. if (auto scroll_delay_string = get_attribute(HTML::AttributeNames::scrolldelay); scroll_delay_string.has_value()) {
  77. if (auto scroll_delay = parse_non_negative_integer(*scroll_delay_string); scroll_delay.has_value())
  78. return *scroll_delay;
  79. }
  80. return 85;
  81. }
  82. // https://html.spec.whatwg.org/multipage/obsolete.html#dom-marquee-scrolldelay
  83. WebIDL::ExceptionOr<void> HTMLMarqueeElement::set_scroll_delay(WebIDL::UnsignedLong value)
  84. {
  85. return set_attribute(HTML::AttributeNames::scrolldelay, String::number(value));
  86. }
  87. }