HTMLBodyElement.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/ComputedProperties.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. bool HTMLBodyElement::is_presentational_hint(FlyString const& name) const
  37. {
  38. if (Base::is_presentational_hint(name))
  39. return true;
  40. return first_is_one_of(name,
  41. HTML::AttributeNames::bgcolor,
  42. HTML::AttributeNames::text,
  43. HTML::AttributeNames::background);
  44. }
  45. void HTMLBodyElement::apply_presentational_hints(GC::Ref<CSS::CascadedProperties> cascaded_properties) const
  46. {
  47. for_each_attribute([&](auto& name, auto& value) {
  48. if (name.equals_ignoring_ascii_case("bgcolor"sv)) {
  49. // https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value
  50. auto color = parse_legacy_color_value(value);
  51. if (color.has_value())
  52. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::BackgroundColor, CSS::CSSColorValue::create_from_color(color.value()));
  53. } else if (name.equals_ignoring_ascii_case("text"sv)) {
  54. // https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-2
  55. auto color = parse_legacy_color_value(value);
  56. if (color.has_value())
  57. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::Color, CSS::CSSColorValue::create_from_color(color.value()));
  58. } else if (name.equals_ignoring_ascii_case("background"sv)) {
  59. VERIFY(m_background_style_value);
  60. cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::BackgroundImage, *m_background_style_value);
  61. }
  62. });
  63. auto get_margin_value = [&](auto const& first_body_attr_name, auto const& second_body_attr_name, auto const& container_frame_attr_name) -> Optional<String> {
  64. if (auto value = get_attribute(first_body_attr_name); value.has_value())
  65. return value.value();
  66. if (auto value = get_attribute(second_body_attr_name); value.has_value())
  67. return value.value();
  68. auto navigable = document().navigable();
  69. if (!navigable)
  70. return {};
  71. auto container = navigable->container();
  72. if (!container)
  73. return {};
  74. if (auto value = container->get_attribute(container_frame_attr_name); value.has_value())
  75. return value;
  76. return {};
  77. };
  78. auto margin_top_value = get_margin_value(HTML::AttributeNames::marginheight, HTML::AttributeNames::topmargin, HTML::AttributeNames::marginheight);
  79. auto margin_bottom_value = get_margin_value(HTML::AttributeNames::marginheight, HTML::AttributeNames::bottommargin, HTML::AttributeNames::marginheight);
  80. auto margin_left_value = get_margin_value(HTML::AttributeNames::marginwidth, HTML::AttributeNames::leftmargin, HTML::AttributeNames::marginwidth);
  81. auto margin_right_value = get_margin_value(HTML::AttributeNames::marginwidth, HTML::AttributeNames::rightmargin, HTML::AttributeNames::marginwidth);
  82. auto apply_margin_value = [&](CSS::PropertyID property_id, Optional<String> const& value) {
  83. if (!value.has_value())
  84. return;
  85. if (auto parsed_value = parse_non_negative_integer(value.value()); parsed_value.has_value())
  86. cascaded_properties->set_property_from_presentational_hint(property_id, CSS::LengthStyleValue::create(CSS::Length::make_px(*parsed_value)));
  87. };
  88. apply_margin_value(CSS::PropertyID::MarginTop, margin_top_value);
  89. apply_margin_value(CSS::PropertyID::MarginBottom, margin_bottom_value);
  90. apply_margin_value(CSS::PropertyID::MarginLeft, margin_left_value);
  91. apply_margin_value(CSS::PropertyID::MarginRight, margin_right_value);
  92. }
  93. void HTMLBodyElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_)
  94. {
  95. Base::attribute_changed(name, old_value, value, namespace_);
  96. if (name.equals_ignoring_ascii_case("link"sv)) {
  97. // https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-3
  98. auto color = parse_legacy_color_value(value.value_or(String {}));
  99. if (color.has_value())
  100. document().set_normal_link_color(color.value());
  101. } else if (name.equals_ignoring_ascii_case("alink"sv)) {
  102. // https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-5
  103. auto color = parse_legacy_color_value(value.value_or(String {}));
  104. if (color.has_value())
  105. document().set_active_link_color(color.value());
  106. } else if (name.equals_ignoring_ascii_case("vlink"sv)) {
  107. // https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-4
  108. auto color = parse_legacy_color_value(value.value_or(String {}));
  109. if (color.has_value())
  110. document().set_visited_link_color(color.value());
  111. } else if (name.equals_ignoring_ascii_case("background"sv)) {
  112. // https://html.spec.whatwg.org/multipage/rendering.html#the-page:attr-background
  113. m_background_style_value = CSS::ImageStyleValue::create(document().encoding_parse_url(value.value_or(String {})));
  114. m_background_style_value->on_animate = [this] {
  115. if (paintable()) {
  116. paintable()->set_needs_display();
  117. }
  118. };
  119. }
  120. #undef __ENUMERATE
  121. #define __ENUMERATE(attribute_name, event_name) \
  122. if (name == HTML::AttributeNames::attribute_name) { \
  123. element_event_handler_attribute_changed(event_name, value); \
  124. }
  125. ENUMERATE_WINDOW_EVENT_HANDLERS(__ENUMERATE)
  126. #undef __ENUMERATE
  127. }
  128. GC::Ptr<DOM::EventTarget> HTMLBodyElement::global_event_handlers_to_event_target(FlyString const& event_name)
  129. {
  130. // NOTE: This is a little weird, but IIUC document.body.onload actually refers to window.onload
  131. // NOTE: document.body can return either a HTMLBodyElement or HTMLFrameSetElement, so both these elements must support this mapping.
  132. if (DOM::is_window_reflecting_body_element_event_handler(event_name))
  133. return document().window();
  134. return *this;
  135. }
  136. GC::Ptr<DOM::EventTarget> HTMLBodyElement::window_event_handlers_to_event_target()
  137. {
  138. // All WindowEventHandlers on HTMLFrameSetElement (e.g. document.body.onrejectionhandled) are mapped to window.on{event}.
  139. // NOTE: document.body can return either a HTMLBodyElement or HTMLFrameSetElement, so both these elements must support this mapping.
  140. return document().window();
  141. }
  142. }