HTMLBodyElement.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/StyleProperties.h>
  7. #include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
  8. #include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
  9. #include <LibWeb/DOM/Document.h>
  10. #include <LibWeb/HTML/HTMLBodyElement.h>
  11. #include <LibWeb/HTML/Parser/HTMLParser.h>
  12. #include <LibWeb/HTML/Window.h>
  13. #include <LibWeb/Layout/Node.h>
  14. #include <LibWeb/Painting/Paintable.h>
  15. namespace Web::HTML {
  16. JS_DEFINE_ALLOCATOR(HTMLBodyElement);
  17. HTMLBodyElement::HTMLBodyElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  18. : HTMLElement(document, move(qualified_name))
  19. {
  20. }
  21. HTMLBodyElement::~HTMLBodyElement() = default;
  22. void HTMLBodyElement::visit_edges(Visitor& visitor)
  23. {
  24. Base::visit_edges(visitor);
  25. if (m_background_style_value)
  26. m_background_style_value->visit_edges(visitor);
  27. }
  28. void HTMLBodyElement::initialize(JS::Realm& realm)
  29. {
  30. Base::initialize(realm);
  31. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLBodyElementPrototype>(realm, "HTMLBodyElement"_fly_string));
  32. }
  33. void HTMLBodyElement::apply_presentational_hints(CSS::StyleProperties& style) const
  34. {
  35. for_each_attribute([&](auto& name, auto& value) {
  36. if (name.equals_ignoring_ascii_case("bgcolor"sv)) {
  37. // https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value
  38. auto color = parse_legacy_color_value(value);
  39. if (color.has_value())
  40. style.set_property(CSS::PropertyID::BackgroundColor, CSS::ColorStyleValue::create(color.value()), nullptr);
  41. } else if (name.equals_ignoring_ascii_case("text"sv)) {
  42. // https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-2
  43. auto color = parse_legacy_color_value(value);
  44. if (color.has_value())
  45. style.set_property(CSS::PropertyID::Color, CSS::ColorStyleValue::create(color.value()), nullptr);
  46. } else if (name.equals_ignoring_ascii_case("background"sv)) {
  47. VERIFY(m_background_style_value);
  48. style.set_property(CSS::PropertyID::BackgroundImage, *m_background_style_value, nullptr);
  49. }
  50. });
  51. }
  52. void HTMLBodyElement::attribute_changed(FlyString const& name, Optional<String> const& value)
  53. {
  54. HTMLElement::attribute_changed(name, value);
  55. if (name.equals_ignoring_ascii_case("link"sv)) {
  56. // https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-3
  57. auto color = parse_legacy_color_value(value.value_or(String {}));
  58. if (color.has_value())
  59. document().set_link_color(color.value());
  60. } else if (name.equals_ignoring_ascii_case("alink"sv)) {
  61. // https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-5
  62. auto color = parse_legacy_color_value(value.value_or(String {}));
  63. if (color.has_value())
  64. document().set_active_link_color(color.value());
  65. } else if (name.equals_ignoring_ascii_case("vlink"sv)) {
  66. // https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-4
  67. auto color = parse_legacy_color_value(value.value_or(String {}));
  68. if (color.has_value())
  69. document().set_visited_link_color(color.value());
  70. } else if (name.equals_ignoring_ascii_case("background"sv)) {
  71. m_background_style_value = CSS::ImageStyleValue::create(document().parse_url(value.value_or(String {})));
  72. m_background_style_value->on_animate = [this] {
  73. if (paintable()) {
  74. paintable()->set_needs_display();
  75. }
  76. };
  77. }
  78. #undef __ENUMERATE
  79. #define __ENUMERATE(attribute_name, event_name) \
  80. if (name == HTML::AttributeNames::attribute_name) { \
  81. element_event_handler_attribute_changed(event_name, value); \
  82. }
  83. ENUMERATE_WINDOW_EVENT_HANDLERS(__ENUMERATE)
  84. #undef __ENUMERATE
  85. }
  86. JS::GCPtr<DOM::EventTarget> HTMLBodyElement::global_event_handlers_to_event_target(FlyString const& event_name)
  87. {
  88. // NOTE: This is a little weird, but IIUC document.body.onload actually refers to window.onload
  89. // NOTE: document.body can return either a HTMLBodyElement or HTMLFrameSetElement, so both these elements must support this mapping.
  90. if (DOM::is_window_reflecting_body_element_event_handler(event_name))
  91. return document().window();
  92. return *this;
  93. }
  94. JS::GCPtr<DOM::EventTarget> HTMLBodyElement::window_event_handlers_to_event_target()
  95. {
  96. // All WindowEventHandlers on HTMLFrameSetElement (e.g. document.body.onrejectionhandled) are mapped to window.on{event}.
  97. // NOTE: document.body can return either a HTMLBodyElement or HTMLFrameSetElement, so both these elements must support this mapping.
  98. return document().window();
  99. }
  100. }