HTMLBodyElement.cpp 4.0 KB

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