HTMLBodyElement.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/StyleValue.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/HTML/HTMLBodyElement.h>
  10. #include <LibWeb/HTML/Window.h>
  11. namespace Web::HTML {
  12. HTMLBodyElement::HTMLBodyElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  13. : HTMLElement(document, move(qualified_name))
  14. {
  15. }
  16. HTMLBodyElement::~HTMLBodyElement() = default;
  17. void HTMLBodyElement::initialize(JS::Realm& realm)
  18. {
  19. Base::initialize(realm);
  20. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLBodyElementPrototype>(realm, "HTMLBodyElement"));
  21. }
  22. void HTMLBodyElement::apply_presentational_hints(CSS::StyleProperties& style) const
  23. {
  24. for_each_attribute([&](auto& name, auto& value) {
  25. if (name.equals_ignoring_case("bgcolor"sv)) {
  26. auto color = Color::from_string(value);
  27. if (color.has_value())
  28. style.set_property(CSS::PropertyID::BackgroundColor, CSS::ColorStyleValue::create(color.value()));
  29. } else if (name.equals_ignoring_case("text"sv)) {
  30. auto color = Color::from_string(value);
  31. if (color.has_value())
  32. style.set_property(CSS::PropertyID::Color, CSS::ColorStyleValue::create(color.value()));
  33. } else if (name.equals_ignoring_case("background"sv)) {
  34. VERIFY(m_background_style_value);
  35. style.set_property(CSS::PropertyID::BackgroundImage, *m_background_style_value);
  36. }
  37. });
  38. }
  39. void HTMLBodyElement::parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value)
  40. {
  41. HTMLElement::parse_attribute(name, value);
  42. if (name.equals_ignoring_case("link"sv)) {
  43. auto color = Color::from_string(value);
  44. if (color.has_value())
  45. document().set_link_color(color.value());
  46. } else if (name.equals_ignoring_case("alink"sv)) {
  47. auto color = Color::from_string(value);
  48. if (color.has_value())
  49. document().set_active_link_color(color.value());
  50. } else if (name.equals_ignoring_case("vlink"sv)) {
  51. auto color = Color::from_string(value);
  52. if (color.has_value())
  53. document().set_visited_link_color(color.value());
  54. } else if (name.equals_ignoring_case("background"sv)) {
  55. m_background_style_value = CSS::ImageStyleValue::create(document().parse_url(value));
  56. m_background_style_value->on_animate = [this] {
  57. if (layout_node()) {
  58. layout_node()->set_needs_display();
  59. }
  60. };
  61. }
  62. #undef __ENUMERATE
  63. #define __ENUMERATE(attribute_name, event_name) \
  64. if (name == HTML::AttributeNames::attribute_name) { \
  65. element_event_handler_attribute_changed(event_name, value); \
  66. }
  67. ENUMERATE_WINDOW_EVENT_HANDLERS(__ENUMERATE)
  68. #undef __ENUMERATE
  69. }
  70. DOM::EventTarget& HTMLBodyElement::global_event_handlers_to_event_target(DeprecatedFlyString const& event_name)
  71. {
  72. // NOTE: This is a little weird, but IIUC document.body.onload actually refers to window.onload
  73. // NOTE: document.body can return either a HTMLBodyElement or HTMLFrameSetElement, so both these elements must support this mapping.
  74. if (DOM::is_window_reflecting_body_element_event_handler(event_name))
  75. return document().window();
  76. return *this;
  77. }
  78. DOM::EventTarget& HTMLBodyElement::window_event_handlers_to_event_target()
  79. {
  80. // All WindowEventHandlers on HTMLFrameSetElement (e.g. document.body.onrejectionhandled) are mapped to window.on{event}.
  81. // NOTE: document.body can return either a HTMLBodyElement or HTMLFrameSetElement, so both these elements must support this mapping.
  82. return document().window();
  83. }
  84. }