HTMLBodyElement.cpp 3.8 KB

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