HTMLMarqueeElement.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/ColorStyleValue.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. JS_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::ColorStyleValue::create(color.value()));
  35. }
  36. });
  37. }
  38. // https://html.spec.whatwg.org/multipage/obsolete.html#dom-marquee-scrollamount
  39. WebIDL::UnsignedLong HTMLMarqueeElement::scroll_amount()
  40. {
  41. // The scrollAmount IDL attribute must reflect the scrollamount content attribute. The default value is 6.
  42. if (auto scroll_amount_string = get_attribute(HTML::AttributeNames::scrollamount); scroll_amount_string.has_value()) {
  43. if (auto scroll_amount = parse_non_negative_integer(*scroll_amount_string); scroll_amount.has_value())
  44. return *scroll_amount;
  45. }
  46. return 6;
  47. }
  48. // https://html.spec.whatwg.org/multipage/obsolete.html#dom-marquee-scrollamount
  49. WebIDL::ExceptionOr<void> HTMLMarqueeElement::set_scroll_amount(WebIDL::UnsignedLong value)
  50. {
  51. return set_attribute(HTML::AttributeNames::scrollamount, MUST(String::number(value)));
  52. }
  53. // https://html.spec.whatwg.org/multipage/obsolete.html#dom-marquee-scrolldelay
  54. WebIDL::UnsignedLong HTMLMarqueeElement::scroll_delay()
  55. {
  56. // The scrollDelay IDL attribute must reflect the scrolldelay content attribute. The default value is 85.
  57. if (auto scroll_delay_string = get_attribute(HTML::AttributeNames::scrolldelay); scroll_delay_string.has_value()) {
  58. if (auto scroll_delay = parse_non_negative_integer(*scroll_delay_string); scroll_delay.has_value())
  59. return *scroll_delay;
  60. }
  61. return 85;
  62. }
  63. // https://html.spec.whatwg.org/multipage/obsolete.html#dom-marquee-scrolldelay
  64. WebIDL::ExceptionOr<void> HTMLMarqueeElement::set_scroll_delay(WebIDL::UnsignedLong value)
  65. {
  66. return set_attribute(HTML::AttributeNames::scrolldelay, MUST(String::number(value)));
  67. }
  68. }