/src/serenity/Userland/Libraries/LibWeb/HTML/HTMLBodyElement.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <LibWeb/Bindings/HTMLBodyElementPrototype.h> |
8 | | #include <LibWeb/CSS/StyleProperties.h> |
9 | | #include <LibWeb/CSS/StyleValues/CSSColorValue.h> |
10 | | #include <LibWeb/CSS/StyleValues/ImageStyleValue.h> |
11 | | #include <LibWeb/DOM/Document.h> |
12 | | #include <LibWeb/HTML/HTMLBodyElement.h> |
13 | | #include <LibWeb/HTML/Parser/HTMLParser.h> |
14 | | #include <LibWeb/HTML/Window.h> |
15 | | #include <LibWeb/Layout/Node.h> |
16 | | #include <LibWeb/Painting/Paintable.h> |
17 | | |
18 | | namespace Web::HTML { |
19 | | |
20 | | JS_DEFINE_ALLOCATOR(HTMLBodyElement); |
21 | | |
22 | | HTMLBodyElement::HTMLBodyElement(DOM::Document& document, DOM::QualifiedName qualified_name) |
23 | 0 | : HTMLElement(document, move(qualified_name)) |
24 | 0 | { |
25 | 0 | } |
26 | | |
27 | 0 | HTMLBodyElement::~HTMLBodyElement() = default; |
28 | | |
29 | | void HTMLBodyElement::visit_edges(Visitor& visitor) |
30 | 0 | { |
31 | 0 | Base::visit_edges(visitor); |
32 | 0 | if (m_background_style_value) |
33 | 0 | m_background_style_value->visit_edges(visitor); |
34 | 0 | } |
35 | | |
36 | | void HTMLBodyElement::initialize(JS::Realm& realm) |
37 | 0 | { |
38 | 0 | Base::initialize(realm); |
39 | 0 | WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLBodyElement); |
40 | 0 | } |
41 | | |
42 | | void HTMLBodyElement::apply_presentational_hints(CSS::StyleProperties& style) const |
43 | 0 | { |
44 | 0 | for_each_attribute([&](auto& name, auto& value) { |
45 | 0 | if (name.equals_ignoring_ascii_case("bgcolor"sv)) { |
46 | | // https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value |
47 | 0 | auto color = parse_legacy_color_value(value); |
48 | 0 | if (color.has_value()) |
49 | 0 | style.set_property(CSS::PropertyID::BackgroundColor, CSS::CSSColorValue::create_from_color(color.value())); |
50 | 0 | } else if (name.equals_ignoring_ascii_case("text"sv)) { |
51 | | // https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-2 |
52 | 0 | auto color = parse_legacy_color_value(value); |
53 | 0 | if (color.has_value()) |
54 | 0 | style.set_property(CSS::PropertyID::Color, CSS::CSSColorValue::create_from_color(color.value())); |
55 | 0 | } else if (name.equals_ignoring_ascii_case("background"sv)) { |
56 | 0 | VERIFY(m_background_style_value); |
57 | 0 | style.set_property(CSS::PropertyID::BackgroundImage, *m_background_style_value); |
58 | 0 | } |
59 | 0 | }); |
60 | 0 | } |
61 | | |
62 | | void HTMLBodyElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value) |
63 | 0 | { |
64 | 0 | HTMLElement::attribute_changed(name, old_value, value); |
65 | 0 | if (name.equals_ignoring_ascii_case("link"sv)) { |
66 | | // https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-3 |
67 | 0 | auto color = parse_legacy_color_value(value.value_or(String {})); |
68 | 0 | if (color.has_value()) |
69 | 0 | document().set_normal_link_color(color.value()); |
70 | 0 | } else if (name.equals_ignoring_ascii_case("alink"sv)) { |
71 | | // https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-5 |
72 | 0 | auto color = parse_legacy_color_value(value.value_or(String {})); |
73 | 0 | if (color.has_value()) |
74 | 0 | document().set_active_link_color(color.value()); |
75 | 0 | } else if (name.equals_ignoring_ascii_case("vlink"sv)) { |
76 | | // https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-4 |
77 | 0 | auto color = parse_legacy_color_value(value.value_or(String {})); |
78 | 0 | if (color.has_value()) |
79 | 0 | document().set_visited_link_color(color.value()); |
80 | 0 | } else if (name.equals_ignoring_ascii_case("background"sv)) { |
81 | 0 | m_background_style_value = CSS::ImageStyleValue::create(document().parse_url(value.value_or(String {}))); |
82 | 0 | m_background_style_value->on_animate = [this] { |
83 | 0 | if (paintable()) { |
84 | 0 | paintable()->set_needs_display(); |
85 | 0 | } |
86 | 0 | }; |
87 | 0 | } |
88 | |
|
89 | 0 | #undef __ENUMERATE |
90 | 0 | #define __ENUMERATE(attribute_name, event_name) \ |
91 | 0 | if (name == HTML::AttributeNames::attribute_name) { \ |
92 | 0 | element_event_handler_attribute_changed(event_name, value); \ |
93 | 0 | } |
94 | 0 | ENUMERATE_WINDOW_EVENT_HANDLERS(__ENUMERATE) |
95 | 0 | #undef __ENUMERATE |
96 | 0 | } |
97 | | |
98 | | JS::GCPtr<DOM::EventTarget> HTMLBodyElement::global_event_handlers_to_event_target(FlyString const& event_name) |
99 | 0 | { |
100 | | // NOTE: This is a little weird, but IIUC document.body.onload actually refers to window.onload |
101 | | // NOTE: document.body can return either a HTMLBodyElement or HTMLFrameSetElement, so both these elements must support this mapping. |
102 | 0 | if (DOM::is_window_reflecting_body_element_event_handler(event_name)) |
103 | 0 | return document().window(); |
104 | | |
105 | 0 | return *this; |
106 | 0 | } |
107 | | |
108 | | JS::GCPtr<DOM::EventTarget> HTMLBodyElement::window_event_handlers_to_event_target() |
109 | 0 | { |
110 | | // All WindowEventHandlers on HTMLFrameSetElement (e.g. document.body.onrejectionhandled) are mapped to window.on{event}. |
111 | | // NOTE: document.body can return either a HTMLBodyElement or HTMLFrameSetElement, so both these elements must support this mapping. |
112 | 0 | return document().window(); |
113 | 0 | } |
114 | | |
115 | | } |