HTMLMarqueeElement.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/ComputedProperties.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. bool HTMLMarqueeElement::is_presentational_hint(FlyString const& name) const
  27. {
  28. if (Base::is_presentational_hint(name))
  29. return true;
  30. return first_is_one_of(name,
  31. HTML::AttributeNames::bgcolor,
  32. HTML::AttributeNames::height,
  33. HTML::AttributeNames::hspace,
  34. HTML::AttributeNames::vspace,
  35. HTML::AttributeNames::width);
  36. }
  37. void HTMLMarqueeElement::apply_presentational_hints(GC::Ref<CSS::CascadedProperties> cascaded_properties) const
  38. {
  39. HTMLElement::apply_presentational_hints(cascaded_properties);
  40. for_each_attribute([&](auto& name, auto& value) {
  41. if (name == HTML::AttributeNames::bgcolor) {
  42. // https://html.spec.whatwg.org/multipage/rendering.html#the-marquee-element-2:rules-for-parsing-a-legacy-colour-value
  43. auto color = parse_legacy_color_value(value);
  44. if (color.has_value())
  45. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::BackgroundColor, CSS::CSSColorValue::create_from_color(color.value()));
  46. } else if (name == HTML::AttributeNames::height) {
  47. // https://html.spec.whatwg.org/multipage/rendering.html#the-marquee-element-2:maps-to-the-dimension-property
  48. if (auto parsed_value = parse_dimension_value(value)) {
  49. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::Height, *parsed_value);
  50. }
  51. } else if (name == HTML::AttributeNames::hspace) {
  52. if (auto parsed_value = parse_dimension_value(value)) {
  53. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::MarginLeft, *parsed_value);
  54. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::MarginRight, *parsed_value);
  55. }
  56. } else if (name == HTML::AttributeNames::vspace) {
  57. if (auto parsed_value = parse_dimension_value(value)) {
  58. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::MarginTop, *parsed_value);
  59. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::MarginBottom, *parsed_value);
  60. }
  61. } else if (name == HTML::AttributeNames::width) {
  62. if (auto parsed_value = parse_dimension_value(value)) {
  63. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::Width, *parsed_value);
  64. }
  65. }
  66. });
  67. }
  68. // https://html.spec.whatwg.org/multipage/obsolete.html#dom-marquee-scrollamount
  69. WebIDL::UnsignedLong HTMLMarqueeElement::scroll_amount()
  70. {
  71. // The scrollAmount IDL attribute must reflect the scrollamount content attribute. The default value is 6.
  72. if (auto scroll_amount_string = get_attribute(HTML::AttributeNames::scrollamount); scroll_amount_string.has_value()) {
  73. if (auto scroll_amount = parse_non_negative_integer(*scroll_amount_string); scroll_amount.has_value() && *scroll_amount <= 2147483647)
  74. return *scroll_amount;
  75. }
  76. return 6;
  77. }
  78. // https://html.spec.whatwg.org/multipage/obsolete.html#dom-marquee-scrollamount
  79. WebIDL::ExceptionOr<void> HTMLMarqueeElement::set_scroll_amount(WebIDL::UnsignedLong value)
  80. {
  81. if (value > 2147483647)
  82. value = 6;
  83. return set_attribute(HTML::AttributeNames::scrollamount, String::number(value));
  84. }
  85. // https://html.spec.whatwg.org/multipage/obsolete.html#dom-marquee-scrolldelay
  86. WebIDL::UnsignedLong HTMLMarqueeElement::scroll_delay()
  87. {
  88. // The scrollDelay IDL attribute must reflect the scrolldelay content attribute. The default value is 85.
  89. if (auto scroll_delay_string = get_attribute(HTML::AttributeNames::scrolldelay); scroll_delay_string.has_value()) {
  90. if (auto scroll_delay = parse_non_negative_integer(*scroll_delay_string); scroll_delay.has_value() && *scroll_delay <= 2147483647)
  91. return *scroll_delay;
  92. }
  93. return 85;
  94. }
  95. // https://html.spec.whatwg.org/multipage/obsolete.html#dom-marquee-scrolldelay
  96. WebIDL::ExceptionOr<void> HTMLMarqueeElement::set_scroll_delay(WebIDL::UnsignedLong value)
  97. {
  98. if (value > 2147483647)
  99. value = 85;
  100. return set_attribute(HTML::AttributeNames::scrolldelay, String::number(value));
  101. }
  102. }