2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2020-03-07 06:32:51 -03:00
|
|
|
#include <LibWeb/CSS/StyleProperties.h>
|
2023-03-23 18:12:15 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
|
2023-03-24 12:17:11 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
|
2020-03-07 06:32:51 -03:00
|
|
|
#include <LibWeb/DOM/Document.h>
|
2020-07-26 10:08:16 -03:00
|
|
|
#include <LibWeb/HTML/HTMLBodyElement.h>
|
2023-05-28 00:06:12 -03:00
|
|
|
#include <LibWeb/HTML/Parser/HTMLParser.h>
|
2022-03-07 19:08:26 -03:00
|
|
|
#include <LibWeb/HTML/Window.h>
|
2023-05-08 02:51:03 -03:00
|
|
|
#include <LibWeb/Layout/Node.h>
|
2024-01-14 09:46:52 -03:00
|
|
|
#include <LibWeb/Painting/Paintable.h>
|
2019-10-04 16:05:52 -03:00
|
|
|
|
2020-07-28 13:20:36 -03:00
|
|
|
namespace Web::HTML {
|
2020-03-07 06:27:02 -03:00
|
|
|
|
2023-11-19 15:47:52 -03:00
|
|
|
JS_DEFINE_ALLOCATOR(HTMLBodyElement);
|
|
|
|
|
|
2022-02-18 17:00:52 -03:00
|
|
|
HTMLBodyElement::HTMLBodyElement(DOM::Document& document, DOM::QualifiedName qualified_name)
|
2021-02-07 07:20:15 -03:00
|
|
|
: HTMLElement(document, move(qualified_name))
|
2019-10-04 16:05:52 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-14 16:21:51 -03:00
|
|
|
HTMLBodyElement::~HTMLBodyElement() = default;
|
2019-10-04 16:05:52 -03:00
|
|
|
|
2023-09-27 19:26:02 -03:00
|
|
|
void HTMLBodyElement::visit_edges(Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
if (m_background_style_value)
|
|
|
|
|
m_background_style_value->visit_edges(visitor);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 03:41:28 -03:00
|
|
|
void HTMLBodyElement::initialize(JS::Realm& realm)
|
2023-01-10 08:28:20 -03:00
|
|
|
{
|
2023-08-07 03:41:28 -03:00
|
|
|
Base::initialize(realm);
|
2023-11-21 20:55:21 -03:00
|
|
|
set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLBodyElementPrototype>(realm, "HTMLBodyElement"_fly_string));
|
2023-01-10 08:28:20 -03:00
|
|
|
}
|
|
|
|
|
|
2020-07-26 15:01:35 -03:00
|
|
|
void HTMLBodyElement::apply_presentational_hints(CSS::StyleProperties& style) const
|
2019-10-04 16:05:52 -03:00
|
|
|
{
|
|
|
|
|
for_each_attribute([&](auto& name, auto& value) {
|
2023-03-10 04:48:54 -03:00
|
|
|
if (name.equals_ignoring_ascii_case("bgcolor"sv)) {
|
2023-05-28 00:06:12 -03:00
|
|
|
// https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value
|
|
|
|
|
auto color = parse_legacy_color_value(value);
|
2019-10-04 16:05:52 -03:00
|
|
|
if (color.has_value())
|
2023-08-19 10:00:10 -03:00
|
|
|
style.set_property(CSS::PropertyID::BackgroundColor, CSS::ColorStyleValue::create(color.value()), nullptr);
|
2023-03-10 04:48:54 -03:00
|
|
|
} else if (name.equals_ignoring_ascii_case("text"sv)) {
|
2023-05-28 00:06:12 -03:00
|
|
|
// https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-2
|
|
|
|
|
auto color = parse_legacy_color_value(value);
|
2019-10-04 16:05:52 -03:00
|
|
|
if (color.has_value())
|
2023-08-19 10:00:10 -03:00
|
|
|
style.set_property(CSS::PropertyID::Color, CSS::ColorStyleValue::create(color.value()), nullptr);
|
2023-03-10 04:48:54 -03:00
|
|
|
} else if (name.equals_ignoring_ascii_case("background"sv)) {
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(m_background_style_value);
|
2023-05-26 16:55:25 -03:00
|
|
|
style.set_property(CSS::PropertyID::BackgroundImage, *m_background_style_value, nullptr);
|
2019-10-04 16:05:52 -03:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-10-06 05:11:54 -03:00
|
|
|
|
2023-11-19 02:10:36 -03:00
|
|
|
void HTMLBodyElement::attribute_changed(FlyString const& name, Optional<String> const& value)
|
2019-10-06 05:11:54 -03:00
|
|
|
{
|
2023-07-03 12:08:37 -03:00
|
|
|
HTMLElement::attribute_changed(name, value);
|
2023-03-10 04:48:54 -03:00
|
|
|
if (name.equals_ignoring_ascii_case("link"sv)) {
|
2023-05-28 00:06:12 -03:00
|
|
|
// https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-3
|
2024-01-16 15:04:45 -03:00
|
|
|
auto color = parse_legacy_color_value(value.value_or(String {}));
|
2019-10-06 05:11:54 -03:00
|
|
|
if (color.has_value())
|
|
|
|
|
document().set_link_color(color.value());
|
2023-03-10 04:48:54 -03:00
|
|
|
} else if (name.equals_ignoring_ascii_case("alink"sv)) {
|
2023-05-28 00:06:12 -03:00
|
|
|
// https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-5
|
2024-01-16 15:04:45 -03:00
|
|
|
auto color = parse_legacy_color_value(value.value_or(String {}));
|
2019-10-06 05:11:54 -03:00
|
|
|
if (color.has_value())
|
|
|
|
|
document().set_active_link_color(color.value());
|
2023-03-10 04:48:54 -03:00
|
|
|
} else if (name.equals_ignoring_ascii_case("vlink"sv)) {
|
2023-05-28 00:06:12 -03:00
|
|
|
// https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-4
|
2024-01-16 15:04:45 -03:00
|
|
|
auto color = parse_legacy_color_value(value.value_or(String {}));
|
2019-10-06 05:11:54 -03:00
|
|
|
if (color.has_value())
|
|
|
|
|
document().set_visited_link_color(color.value());
|
2023-03-10 04:48:54 -03:00
|
|
|
} else if (name.equals_ignoring_ascii_case("background"sv)) {
|
2023-11-19 02:10:36 -03:00
|
|
|
m_background_style_value = CSS::ImageStyleValue::create(document().parse_url(value.value_or(String {})));
|
2022-10-30 12:43:42 -03:00
|
|
|
m_background_style_value->on_animate = [this] {
|
2024-01-14 09:46:52 -03:00
|
|
|
if (paintable()) {
|
|
|
|
|
paintable()->set_needs_display();
|
2022-10-30 12:43:42 -03:00
|
|
|
}
|
|
|
|
|
};
|
2019-10-06 05:11:54 -03:00
|
|
|
}
|
2022-06-27 15:48:54 -03:00
|
|
|
|
|
|
|
|
#undef __ENUMERATE
|
2023-11-19 02:10:36 -03:00
|
|
|
#define __ENUMERATE(attribute_name, event_name) \
|
|
|
|
|
if (name == HTML::AttributeNames::attribute_name) { \
|
|
|
|
|
element_event_handler_attribute_changed(event_name, value); \
|
2022-06-27 15:48:54 -03:00
|
|
|
}
|
|
|
|
|
ENUMERATE_WINDOW_EVENT_HANDLERS(__ENUMERATE)
|
|
|
|
|
#undef __ENUMERATE
|
2019-10-06 05:11:54 -03:00
|
|
|
}
|
2020-03-07 06:27:02 -03:00
|
|
|
|
2024-03-10 04:41:18 -03:00
|
|
|
JS::GCPtr<DOM::EventTarget> HTMLBodyElement::global_event_handlers_to_event_target(FlyString const& event_name)
|
2021-02-03 18:47:50 -03:00
|
|
|
{
|
|
|
|
|
// NOTE: This is a little weird, but IIUC document.body.onload actually refers to window.onload
|
2022-06-27 15:20:09 -03:00
|
|
|
// NOTE: document.body can return either a HTMLBodyElement or HTMLFrameSetElement, so both these elements must support this mapping.
|
|
|
|
|
if (DOM::is_window_reflecting_body_element_event_handler(event_name))
|
|
|
|
|
return document().window();
|
|
|
|
|
|
|
|
|
|
return *this;
|
2021-02-03 18:47:50 -03:00
|
|
|
}
|
|
|
|
|
|
2024-03-10 04:41:18 -03:00
|
|
|
JS::GCPtr<DOM::EventTarget> HTMLBodyElement::window_event_handlers_to_event_target()
|
2022-06-27 15:48:54 -03:00
|
|
|
{
|
|
|
|
|
// All WindowEventHandlers on HTMLFrameSetElement (e.g. document.body.onrejectionhandled) are mapped to window.on{event}.
|
|
|
|
|
// NOTE: document.body can return either a HTMLBodyElement or HTMLFrameSetElement, so both these elements must support this mapping.
|
|
|
|
|
return document().window();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 06:27:02 -03:00
|
|
|
}
|