HTMLBodyElement.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/HTMLBodyElementPrototype.h>
  7. #include <LibWeb/CSS/StyleProperties.h>
  8. #include <LibWeb/CSS/StyleValues/CSSColorValue.h>
  9. #include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
  10. #include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
  11. #include <LibWeb/DOM/Document.h>
  12. #include <LibWeb/HTML/HTMLBodyElement.h>
  13. #include <LibWeb/HTML/Numbers.h>
  14. #include <LibWeb/HTML/Parser/HTMLParser.h>
  15. #include <LibWeb/HTML/Window.h>
  16. #include <LibWeb/Layout/Node.h>
  17. #include <LibWeb/Painting/Paintable.h>
  18. namespace Web::HTML {
  19. GC_DEFINE_ALLOCATOR(HTMLBodyElement);
  20. HTMLBodyElement::HTMLBodyElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  21. : HTMLElement(document, move(qualified_name))
  22. {
  23. }
  24. HTMLBodyElement::~HTMLBodyElement() = default;
  25. void HTMLBodyElement::visit_edges(Visitor& visitor)
  26. {
  27. Base::visit_edges(visitor);
  28. if (m_background_style_value)
  29. m_background_style_value->visit_edges(visitor);
  30. }
  31. void HTMLBodyElement::initialize(JS::Realm& realm)
  32. {
  33. Base::initialize(realm);
  34. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLBodyElement);
  35. }
  36. void HTMLBodyElement::apply_presentational_hints(CSS::StyleProperties& style) const
  37. {
  38. for_each_attribute([&](auto& name, auto& value) {
  39. if (name.equals_ignoring_ascii_case("bgcolor"sv)) {
  40. // https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value
  41. auto color = parse_legacy_color_value(value);
  42. if (color.has_value())
  43. style.set_property(CSS::PropertyID::BackgroundColor, CSS::CSSColorValue::create_from_color(color.value()));
  44. } else if (name.equals_ignoring_ascii_case("text"sv)) {
  45. // https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-2
  46. auto color = parse_legacy_color_value(value);
  47. if (color.has_value())
  48. style.set_property(CSS::PropertyID::Color, CSS::CSSColorValue::create_from_color(color.value()));
  49. } else if (name.equals_ignoring_ascii_case("background"sv)) {
  50. VERIFY(m_background_style_value);
  51. style.set_property(CSS::PropertyID::BackgroundImage, *m_background_style_value);
  52. }
  53. });
  54. auto get_margin_value = [&](auto const& first_body_attr_name, auto const& second_body_attr_name, auto const& container_frame_attr_name) -> Optional<String> {
  55. if (auto value = get_attribute(first_body_attr_name); value.has_value())
  56. return value.value();
  57. if (auto value = get_attribute(second_body_attr_name); value.has_value())
  58. return value.value();
  59. auto navigable = document().navigable();
  60. if (!navigable)
  61. return {};
  62. auto container = navigable->container();
  63. if (!container)
  64. return {};
  65. if (auto value = container->get_attribute(container_frame_attr_name); value.has_value())
  66. return value;
  67. return {};
  68. };
  69. auto margin_top_value = get_margin_value(HTML::AttributeNames::marginheight, HTML::AttributeNames::topmargin, HTML::AttributeNames::marginheight);
  70. auto margin_bottom_value = get_margin_value(HTML::AttributeNames::marginheight, HTML::AttributeNames::bottommargin, HTML::AttributeNames::marginheight);
  71. auto margin_left_value = get_margin_value(HTML::AttributeNames::marginwidth, HTML::AttributeNames::leftmargin, HTML::AttributeNames::marginwidth);
  72. auto margin_right_value = get_margin_value(HTML::AttributeNames::marginwidth, HTML::AttributeNames::rightmargin, HTML::AttributeNames::marginwidth);
  73. auto apply_margin_value = [&](CSS::PropertyID property_id, Optional<String> const& value) {
  74. if (!value.has_value())
  75. return;
  76. if (auto parsed_value = parse_non_negative_integer(value.value()); parsed_value.has_value())
  77. style.set_property(property_id, CSS::LengthStyleValue::create(CSS::Length::make_px(*parsed_value)));
  78. };
  79. apply_margin_value(CSS::PropertyID::MarginTop, margin_top_value);
  80. apply_margin_value(CSS::PropertyID::MarginBottom, margin_bottom_value);
  81. apply_margin_value(CSS::PropertyID::MarginLeft, margin_left_value);
  82. apply_margin_value(CSS::PropertyID::MarginRight, margin_right_value);
  83. }
  84. void HTMLBodyElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_)
  85. {
  86. Base::attribute_changed(name, old_value, value, namespace_);
  87. if (name.equals_ignoring_ascii_case("link"sv)) {
  88. // https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-3
  89. auto color = parse_legacy_color_value(value.value_or(String {}));
  90. if (color.has_value())
  91. document().set_normal_link_color(color.value());
  92. } else if (name.equals_ignoring_ascii_case("alink"sv)) {
  93. // https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-5
  94. auto color = parse_legacy_color_value(value.value_or(String {}));
  95. if (color.has_value())
  96. document().set_active_link_color(color.value());
  97. } else if (name.equals_ignoring_ascii_case("vlink"sv)) {
  98. // https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-4
  99. auto color = parse_legacy_color_value(value.value_or(String {}));
  100. if (color.has_value())
  101. document().set_visited_link_color(color.value());
  102. } else if (name.equals_ignoring_ascii_case("background"sv)) {
  103. // https://html.spec.whatwg.org/multipage/rendering.html#the-page:attr-background
  104. m_background_style_value = CSS::ImageStyleValue::create(document().encoding_parse_url(value.value_or(String {})));
  105. m_background_style_value->on_animate = [this] {
  106. if (paintable()) {
  107. paintable()->set_needs_display();
  108. }
  109. };
  110. }
  111. #undef __ENUMERATE
  112. #define __ENUMERATE(attribute_name, event_name) \
  113. if (name == HTML::AttributeNames::attribute_name) { \
  114. element_event_handler_attribute_changed(event_name, value); \
  115. }
  116. ENUMERATE_WINDOW_EVENT_HANDLERS(__ENUMERATE)
  117. #undef __ENUMERATE
  118. }
  119. GC::Ptr<DOM::EventTarget> HTMLBodyElement::global_event_handlers_to_event_target(FlyString const& event_name)
  120. {
  121. // NOTE: This is a little weird, but IIUC document.body.onload actually refers to window.onload
  122. // NOTE: document.body can return either a HTMLBodyElement or HTMLFrameSetElement, so both these elements must support this mapping.
  123. if (DOM::is_window_reflecting_body_element_event_handler(event_name))
  124. return document().window();
  125. return *this;
  126. }
  127. GC::Ptr<DOM::EventTarget> HTMLBodyElement::window_event_handlers_to_event_target()
  128. {
  129. // All WindowEventHandlers on HTMLFrameSetElement (e.g. document.body.onrejectionhandled) are mapped to window.on{event}.
  130. // NOTE: document.body can return either a HTMLBodyElement or HTMLFrameSetElement, so both these elements must support this mapping.
  131. return document().window();
  132. }
  133. }