2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2020-03-07 10:32:51 +01:00
|
|
|
#include <LibWeb/CSS/StyleProperties.h>
|
2023-03-23 21:12:15 +00:00
|
|
|
#include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
|
2023-03-24 15:17:11 +00:00
|
|
|
#include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
|
2020-03-07 10:32:51 +01:00
|
|
|
#include <LibWeb/DOM/Document.h>
|
2020-07-26 15:08:16 +02:00
|
|
|
#include <LibWeb/HTML/HTMLBodyElement.h>
|
2023-05-28 15:06:12 +12:00
|
|
|
#include <LibWeb/HTML/Parser/HTMLParser.h>
|
2022-03-07 23:08:26 +01:00
|
|
|
#include <LibWeb/HTML/Window.h>
|
2023-05-08 07:51:03 +02:00
|
|
|
#include <LibWeb/Layout/Node.h>
|
2019-10-04 21:05:52 +02:00
|
|
|
|
2020-07-28 18:20:36 +02:00
|
|
|
namespace Web::HTML {
|
2020-03-07 10:27:02 +01:00
|
|
|
|
2022-02-18 21:00:52 +01:00
|
|
|
HTMLBodyElement::HTMLBodyElement(DOM::Document& document, DOM::QualifiedName qualified_name)
|
2021-02-07 11:20:15 +01:00
|
|
|
: HTMLElement(document, move(qualified_name))
|
2019-10-04 21:05:52 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:21:51 -06:00
|
|
|
HTMLBodyElement::~HTMLBodyElement() = default;
|
2019-10-04 21:05:52 +02:00
|
|
|
|
2023-01-28 12:33:35 -05:00
|
|
|
JS::ThrowCompletionOr<void> HTMLBodyElement::initialize(JS::Realm& realm)
|
2023-01-10 06:28:20 -05:00
|
|
|
{
|
2023-01-28 12:33:35 -05:00
|
|
|
MUST_OR_THROW_OOM(Base::initialize(realm));
|
2023-01-10 06:28:20 -05:00
|
|
|
set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLBodyElementPrototype>(realm, "HTMLBodyElement"));
|
2023-01-28 12:33:35 -05:00
|
|
|
|
|
|
|
return {};
|
2023-01-10 06:28:20 -05:00
|
|
|
}
|
|
|
|
|
2020-07-26 20:01:35 +02:00
|
|
|
void HTMLBodyElement::apply_presentational_hints(CSS::StyleProperties& style) const
|
2019-10-04 21:05:52 +02:00
|
|
|
{
|
|
|
|
for_each_attribute([&](auto& name, auto& value) {
|
2023-03-10 08:48:54 +01:00
|
|
|
if (name.equals_ignoring_ascii_case("bgcolor"sv)) {
|
2023-05-28 15:06:12 +12: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 21:05:52 +02:00
|
|
|
if (color.has_value())
|
2023-05-05 15:02:03 +01:00
|
|
|
style.set_property(CSS::PropertyID::BackgroundColor, CSS::ColorStyleValue::create(color.value()).release_value_but_fixme_should_propagate_errors());
|
2023-03-10 08:48:54 +01:00
|
|
|
} else if (name.equals_ignoring_ascii_case("text"sv)) {
|
2023-05-28 15:06:12 +12: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 21:05:52 +02:00
|
|
|
if (color.has_value())
|
2023-05-05 15:02:03 +01:00
|
|
|
style.set_property(CSS::PropertyID::Color, CSS::ColorStyleValue::create(color.value()).release_value_but_fixme_should_propagate_errors());
|
2023-03-10 08:48:54 +01:00
|
|
|
} else if (name.equals_ignoring_ascii_case("background"sv)) {
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_background_style_value);
|
2020-03-07 11:17:18 +01:00
|
|
|
style.set_property(CSS::PropertyID::BackgroundImage, *m_background_style_value);
|
2019-10-04 21:05:52 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-10-06 10:11:54 +02:00
|
|
|
|
2023-01-08 19:23:00 -05:00
|
|
|
void HTMLBodyElement::parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value)
|
2019-10-06 10:11:54 +02:00
|
|
|
{
|
2020-06-04 16:04:52 +02:00
|
|
|
HTMLElement::parse_attribute(name, value);
|
2023-03-10 08:48:54 +01:00
|
|
|
if (name.equals_ignoring_ascii_case("link"sv)) {
|
2023-05-28 15:06:12 +12:00
|
|
|
// https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-3
|
|
|
|
auto color = parse_legacy_color_value(value);
|
2019-10-06 10:11:54 +02:00
|
|
|
if (color.has_value())
|
|
|
|
document().set_link_color(color.value());
|
2023-03-10 08:48:54 +01:00
|
|
|
} else if (name.equals_ignoring_ascii_case("alink"sv)) {
|
2023-05-28 15:06:12 +12:00
|
|
|
// https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-5
|
|
|
|
auto color = parse_legacy_color_value(value);
|
2019-10-06 10:11:54 +02:00
|
|
|
if (color.has_value())
|
|
|
|
document().set_active_link_color(color.value());
|
2023-03-10 08:48:54 +01:00
|
|
|
} else if (name.equals_ignoring_ascii_case("vlink"sv)) {
|
2023-05-28 15:06:12 +12:00
|
|
|
// https://html.spec.whatwg.org/multipage/rendering.html#the-page:rules-for-parsing-a-legacy-colour-value-4
|
|
|
|
auto color = parse_legacy_color_value(value);
|
2019-10-06 10:11:54 +02:00
|
|
|
if (color.has_value())
|
|
|
|
document().set_visited_link_color(color.value());
|
2023-03-10 08:48:54 +01:00
|
|
|
} else if (name.equals_ignoring_ascii_case("background"sv)) {
|
2023-05-05 15:02:03 +01:00
|
|
|
m_background_style_value = CSS::ImageStyleValue::create(document().parse_url(value)).release_value_but_fixme_should_propagate_errors();
|
2022-10-30 15:43:42 +00:00
|
|
|
m_background_style_value->on_animate = [this] {
|
|
|
|
if (layout_node()) {
|
|
|
|
layout_node()->set_needs_display();
|
|
|
|
}
|
|
|
|
};
|
2019-10-06 10:11:54 +02:00
|
|
|
}
|
2022-06-27 19:48:54 +01:00
|
|
|
|
|
|
|
#undef __ENUMERATE
|
2023-04-09 09:52:27 +02:00
|
|
|
#define __ENUMERATE(attribute_name, event_name) \
|
|
|
|
if (name == HTML::AttributeNames::attribute_name) { \
|
|
|
|
element_event_handler_attribute_changed(event_name, String::from_deprecated_string(value).release_value()); \
|
2022-06-27 19:48:54 +01:00
|
|
|
}
|
|
|
|
ENUMERATE_WINDOW_EVENT_HANDLERS(__ENUMERATE)
|
|
|
|
#undef __ENUMERATE
|
2019-10-06 10:11:54 +02:00
|
|
|
}
|
2020-03-07 10:27:02 +01:00
|
|
|
|
2023-04-06 07:25:18 +02:00
|
|
|
DOM::EventTarget& HTMLBodyElement::global_event_handlers_to_event_target(FlyString const& event_name)
|
2021-02-03 22:47:50 +01:00
|
|
|
{
|
|
|
|
// NOTE: This is a little weird, but IIUC document.body.onload actually refers to window.onload
|
2022-06-27 19:20:09 +01: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 22:47:50 +01:00
|
|
|
}
|
|
|
|
|
2022-06-27 19:48:54 +01:00
|
|
|
DOM::EventTarget& HTMLBodyElement::window_event_handlers_to_event_target()
|
|
|
|
{
|
|
|
|
// 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 10:27:02 +01:00
|
|
|
}
|