HTMLBodyElement.cpp 4.2 KB

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