HTMLBodyElement.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/HTMLBodyElementPrototype.h>
  7. #include <LibWeb/CSS/StyleProperties.h>
  8. #include <LibWeb/CSS/StyleValue.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. set_prototype(&window().ensure_web_prototype<Bindings::HTMLBodyElementPrototype>("HTMLBodyElement"));
  17. }
  18. HTMLBodyElement::~HTMLBodyElement() = default;
  19. void HTMLBodyElement::apply_presentational_hints(CSS::StyleProperties& style) const
  20. {
  21. for_each_attribute([&](auto& name, auto& value) {
  22. if (name.equals_ignoring_case("bgcolor"sv)) {
  23. auto color = Color::from_string(value);
  24. if (color.has_value())
  25. style.set_property(CSS::PropertyID::BackgroundColor, CSS::ColorStyleValue::create(color.value()));
  26. } else if (name.equals_ignoring_case("text"sv)) {
  27. auto color = Color::from_string(value);
  28. if (color.has_value())
  29. style.set_property(CSS::PropertyID::Color, CSS::ColorStyleValue::create(color.value()));
  30. } else if (name.equals_ignoring_case("background"sv)) {
  31. VERIFY(m_background_style_value);
  32. style.set_property(CSS::PropertyID::BackgroundImage, *m_background_style_value);
  33. }
  34. });
  35. }
  36. void HTMLBodyElement::parse_attribute(FlyString const& name, String const& value)
  37. {
  38. HTMLElement::parse_attribute(name, value);
  39. if (name.equals_ignoring_case("link"sv)) {
  40. auto color = Color::from_string(value);
  41. if (color.has_value())
  42. document().set_link_color(color.value());
  43. } else if (name.equals_ignoring_case("alink"sv)) {
  44. auto color = Color::from_string(value);
  45. if (color.has_value())
  46. document().set_active_link_color(color.value());
  47. } else if (name.equals_ignoring_case("vlink"sv)) {
  48. auto color = Color::from_string(value);
  49. if (color.has_value())
  50. document().set_visited_link_color(color.value());
  51. } else if (name.equals_ignoring_case("background"sv)) {
  52. m_background_style_value = CSS::ImageStyleValue::create(document().parse_url(value));
  53. }
  54. #undef __ENUMERATE
  55. #define __ENUMERATE(attribute_name, event_name) \
  56. if (name == HTML::AttributeNames::attribute_name) { \
  57. element_event_handler_attribute_changed(event_name, value); \
  58. }
  59. ENUMERATE_WINDOW_EVENT_HANDLERS(__ENUMERATE)
  60. #undef __ENUMERATE
  61. }
  62. DOM::EventTarget& HTMLBodyElement::global_event_handlers_to_event_target(FlyString const& event_name)
  63. {
  64. // NOTE: This is a little weird, but IIUC document.body.onload actually refers to window.onload
  65. // NOTE: document.body can return either a HTMLBodyElement or HTMLFrameSetElement, so both these elements must support this mapping.
  66. if (DOM::is_window_reflecting_body_element_event_handler(event_name))
  67. return document().window();
  68. return *this;
  69. }
  70. DOM::EventTarget& HTMLBodyElement::window_event_handlers_to_event_target()
  71. {
  72. // All WindowEventHandlers on HTMLFrameSetElement (e.g. document.body.onrejectionhandled) are mapped to window.on{event}.
  73. // NOTE: document.body can return either a HTMLBodyElement or HTMLFrameSetElement, so both these elements must support this mapping.
  74. return document().window();
  75. }
  76. }