HTMLBodyElement.cpp 4.9 KB

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