2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2024-11-23 11:44:06 +01:00
|
|
|
* Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2024-04-27 12:09:58 +12:00
|
|
|
#include <LibWeb/Bindings/HTMLBodyElementPrototype.h>
|
2024-12-20 11:32:17 +01:00
|
|
|
#include <LibWeb/CSS/ComputedProperties.h>
|
2024-08-14 16:37:02 +01:00
|
|
|
#include <LibWeb/CSS/StyleValues/CSSColorValue.h>
|
2023-03-24 15:17:11 +00:00
|
|
|
#include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
|
2024-11-23 11:44:06 +01:00
|
|
|
#include <LibWeb/CSS/StyleValues/LengthStyleValue.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>
|
2024-11-23 11:44:06 +01:00
|
|
|
#include <LibWeb/HTML/Numbers.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>
|
2024-01-14 13:46:52 +01:00
|
|
|
#include <LibWeb/Painting/Paintable.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
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DEFINE_ALLOCATOR(HTMLBodyElement);
|
2023-11-19 19:47:52 +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-09-28 00:26:02 +02: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 08:41:28 +02:00
|
|
|
void HTMLBodyElement::initialize(JS::Realm& realm)
|
2023-01-10 06:28:20 -05:00
|
|
|
{
|
2023-08-07 08:41:28 +02:00
|
|
|
Base::initialize(realm);
|
2024-03-16 13:13:08 +01:00
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLBodyElement);
|
2023-01-10 06:28:20 -05:00
|
|
|
}
|
|
|
|
|
2024-12-23 17:51:10 +01:00
|
|
|
bool HTMLBodyElement::is_presentational_hint(FlyString const& name) const
|
|
|
|
{
|
|
|
|
if (Base::is_presentational_hint(name))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return first_is_one_of(name,
|
|
|
|
HTML::AttributeNames::bgcolor,
|
|
|
|
HTML::AttributeNames::text,
|
2025-02-22 14:54:36 +01:00
|
|
|
HTML::AttributeNames::background,
|
|
|
|
HTML::AttributeNames::marginheight,
|
|
|
|
HTML::AttributeNames::marginwidth,
|
|
|
|
HTML::AttributeNames::topmargin,
|
|
|
|
HTML::AttributeNames::rightmargin,
|
|
|
|
HTML::AttributeNames::bottommargin,
|
|
|
|
HTML::AttributeNames::leftmargin);
|
2024-12-23 17:51:10 +01:00
|
|
|
}
|
|
|
|
|
LibWeb: Split StyleComputer work into two phases with separate outputs
Before this change, StyleComputer would essentially take a DOM element,
find all the CSS rules that apply to it, and resolve the computed value
for each CSS property for that element.
This worked great, but it meant we had to do all the work of selector
matching and cascading every time.
To enable new optimizations, this change introduces a break in the
middle of this process where we've produced a "CascadedProperties".
This object contains the result of the cascade, before we've begun
turning cascaded values into computed values.
The cascaded properties are now stored with each element, which will
later allow us to do partial updates without re-running the full
StyleComputer machine. This will be particularly valuable for
re-implementing CSS inheritance, which is extremely heavy today.
Note that CSS animations and CSS transitions operate entirely on the
computed values, even though the cascade order would have you believe
they happen earlier. I'm not confident we have the right architecture
for this, but that's a separate issue.
2024-12-12 10:06:29 +01:00
|
|
|
void HTMLBodyElement::apply_presentational_hints(GC::Ref<CSS::CascadedProperties> cascaded_properties) 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())
|
2025-02-19 21:02:12 +11:00
|
|
|
cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::BackgroundColor, CSS::CSSColorValue::create_from_color(color.value(), CSS::ColorSyntax::Legacy));
|
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())
|
2025-02-19 21:02:12 +11:00
|
|
|
cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::Color, CSS::CSSColorValue::create_from_color(color.value(), CSS::ColorSyntax::Legacy));
|
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);
|
LibWeb: Split StyleComputer work into two phases with separate outputs
Before this change, StyleComputer would essentially take a DOM element,
find all the CSS rules that apply to it, and resolve the computed value
for each CSS property for that element.
This worked great, but it meant we had to do all the work of selector
matching and cascading every time.
To enable new optimizations, this change introduces a break in the
middle of this process where we've produced a "CascadedProperties".
This object contains the result of the cascade, before we've begun
turning cascaded values into computed values.
The cascaded properties are now stored with each element, which will
later allow us to do partial updates without re-running the full
StyleComputer machine. This will be particularly valuable for
re-implementing CSS inheritance, which is extremely heavy today.
Note that CSS animations and CSS transitions operate entirely on the
computed values, even though the cascade order would have you believe
they happen earlier. I'm not confident we have the right architecture
for this, but that's a separate issue.
2024-12-12 10:06:29 +01:00
|
|
|
cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::BackgroundImage, *m_background_style_value);
|
2019-10-04 21:05:52 +02:00
|
|
|
}
|
|
|
|
});
|
2024-11-23 11:44:06 +01:00
|
|
|
|
|
|
|
auto get_margin_value = [&](auto const& first_body_attr_name, auto const& second_body_attr_name, auto const& container_frame_attr_name) -> Optional<String> {
|
|
|
|
if (auto value = get_attribute(first_body_attr_name); value.has_value())
|
|
|
|
return value.value();
|
|
|
|
if (auto value = get_attribute(second_body_attr_name); value.has_value())
|
|
|
|
return value.value();
|
|
|
|
auto navigable = document().navigable();
|
|
|
|
if (!navigable)
|
|
|
|
return {};
|
|
|
|
auto container = navigable->container();
|
|
|
|
if (!container)
|
|
|
|
return {};
|
|
|
|
if (auto value = container->get_attribute(container_frame_attr_name); value.has_value())
|
|
|
|
return value;
|
|
|
|
return {};
|
|
|
|
};
|
|
|
|
auto margin_top_value = get_margin_value(HTML::AttributeNames::marginheight, HTML::AttributeNames::topmargin, HTML::AttributeNames::marginheight);
|
|
|
|
auto margin_bottom_value = get_margin_value(HTML::AttributeNames::marginheight, HTML::AttributeNames::bottommargin, HTML::AttributeNames::marginheight);
|
|
|
|
auto margin_left_value = get_margin_value(HTML::AttributeNames::marginwidth, HTML::AttributeNames::leftmargin, HTML::AttributeNames::marginwidth);
|
|
|
|
auto margin_right_value = get_margin_value(HTML::AttributeNames::marginwidth, HTML::AttributeNames::rightmargin, HTML::AttributeNames::marginwidth);
|
|
|
|
|
|
|
|
auto apply_margin_value = [&](CSS::PropertyID property_id, Optional<String> const& value) {
|
|
|
|
if (!value.has_value())
|
|
|
|
return;
|
|
|
|
if (auto parsed_value = parse_non_negative_integer(value.value()); parsed_value.has_value())
|
LibWeb: Split StyleComputer work into two phases with separate outputs
Before this change, StyleComputer would essentially take a DOM element,
find all the CSS rules that apply to it, and resolve the computed value
for each CSS property for that element.
This worked great, but it meant we had to do all the work of selector
matching and cascading every time.
To enable new optimizations, this change introduces a break in the
middle of this process where we've produced a "CascadedProperties".
This object contains the result of the cascade, before we've begun
turning cascaded values into computed values.
The cascaded properties are now stored with each element, which will
later allow us to do partial updates without re-running the full
StyleComputer machine. This will be particularly valuable for
re-implementing CSS inheritance, which is extremely heavy today.
Note that CSS animations and CSS transitions operate entirely on the
computed values, even though the cascade order would have you believe
they happen earlier. I'm not confident we have the right architecture
for this, but that's a separate issue.
2024-12-12 10:06:29 +01:00
|
|
|
cascaded_properties->set_property_from_presentational_hint(property_id, CSS::LengthStyleValue::create(CSS::Length::make_px(*parsed_value)));
|
2024-11-23 11:44:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
apply_margin_value(CSS::PropertyID::MarginTop, margin_top_value);
|
|
|
|
apply_margin_value(CSS::PropertyID::MarginBottom, margin_bottom_value);
|
|
|
|
apply_margin_value(CSS::PropertyID::MarginLeft, margin_left_value);
|
|
|
|
apply_margin_value(CSS::PropertyID::MarginRight, margin_right_value);
|
2019-10-04 21:05:52 +02:00
|
|
|
}
|
2019-10-06 10:11:54 +02:00
|
|
|
|
2024-11-14 08:14:16 -05:00
|
|
|
void HTMLBodyElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_)
|
2019-10-06 10:11:54 +02:00
|
|
|
{
|
2024-11-14 08:14:16 -05:00
|
|
|
Base::attribute_changed(name, old_value, value, namespace_);
|
|
|
|
|
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
|
2024-01-16 19:04:45 +01:00
|
|
|
auto color = parse_legacy_color_value(value.value_or(String {}));
|
2019-10-06 10:11:54 +02:00
|
|
|
if (color.has_value())
|
2024-04-16 10:53:09 -06:00
|
|
|
document().set_normal_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
|
2024-01-16 19:04:45 +01:00
|
|
|
auto color = parse_legacy_color_value(value.value_or(String {}));
|
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
|
2024-01-16 19:04:45 +01:00
|
|
|
auto color = parse_legacy_color_value(value.value_or(String {}));
|
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)) {
|
2024-12-06 16:24:08 -05:00
|
|
|
// https://html.spec.whatwg.org/multipage/rendering.html#the-page:attr-background
|
2025-01-23 19:40:57 +13:00
|
|
|
if (auto maybe_background_url = document().encoding_parse_url(value.value_or(String {})); maybe_background_url.has_value()) {
|
|
|
|
m_background_style_value = CSS::ImageStyleValue::create(maybe_background_url.value());
|
|
|
|
m_background_style_value->on_animate = [this] {
|
|
|
|
if (paintable())
|
|
|
|
paintable()->set_needs_display();
|
|
|
|
};
|
|
|
|
}
|
2019-10-06 10:11:54 +02:00
|
|
|
}
|
2022-06-27 19:48:54 +01:00
|
|
|
|
|
|
|
#undef __ENUMERATE
|
2023-11-19 18:10:36 +13:00
|
|
|
#define __ENUMERATE(attribute_name, event_name) \
|
|
|
|
if (name == HTML::AttributeNames::attribute_name) { \
|
|
|
|
element_event_handler_attribute_changed(event_name, 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
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ptr<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
|
|
|
}
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ptr<DOM::EventTarget> HTMLBodyElement::window_event_handlers_to_event_target()
|
2022-06-27 19:48:54 +01: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 10:27:02 +01:00
|
|
|
}
|