LibWeb: Add an engine-internal CSS realm for internal style parsing

This is used for default UA style right now, and we'll expand its use in
the near future.

Note that this required teaching the CSS parser to handle url()
functions when there's no document URL to resolve them against. If we
don't handle that, namespace rules in UA style don't parse correctly.
This commit is contained in:
Andreas Kling 2025-03-25 10:04:42 +00:00 committed by Andreas Kling
parent c12f8b80dc
commit ba030f0363
Notes: github-actions[bot] 2025-03-25 23:58:04 +00:00
4 changed files with 44 additions and 13 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2018-2025, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2020-2023, the SerenityOS developers.
* Copyright (c) 2021-2024, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
@ -8,13 +8,40 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/Bindings/PrincipalHostDefined.h>
#include <LibWeb/CSS/CSSMediaRule.h>
#include <LibWeb/CSS/CSSRuleList.h>
#include <LibWeb/CSS/CSSStyleSheet.h>
#include <LibWeb/CSS/Parser/Parser.h>
#include <LibWeb/HTML/Window.h>
namespace Web {
GC::Ref<JS::Realm> internal_css_realm()
{
static GC::Root<JS::Realm> realm;
static GC::Root<HTML::Window> window;
static OwnPtr<JS::ExecutionContext> execution_context;
if (!realm) {
execution_context = Bindings::create_a_new_javascript_realm(
Bindings::main_thread_vm(),
[&](JS::Realm& realm) -> JS::Object* {
window = HTML::Window::create(realm);
return window;
},
[&](JS::Realm&) -> JS::Object* {
return window;
});
realm = *execution_context->realm;
auto intrinsics = realm->create<Bindings::Intrinsics>(*realm);
auto host_defined = make<Bindings::HostDefined>(intrinsics);
realm->set_host_defined(move(host_defined));
}
return *realm;
}
CSS::CSSStyleSheet* parse_css_stylesheet(CSS::Parser::ParsingParams const& context, StringView css, Optional<URL::URL> location)
{
if (css.is_empty()) {