HTMLBodyElement.cpp 3.3 KB

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