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