Truly independent web browser
# Copyright (c) 2026-present, the Ladybird developers.
#
# SPDX-License-Identifier: BSD-2-Clause
from __future__ import annotations
from Generators.libweb_bindings.includes import GeneratedIncludes
def wrap_with_extended_attribute_exposure_checks(
includes: GeneratedIncludes,
extended_attributes: dict[str, str],
text: str,
experimental_name: str | None = None,
) -> str:
if "SecureContext" in extended_attributes:
includes.add("LibWeb/Bindings/PrincipalHostDefined.h")
text = text.replace("\n", "\n ")
text = f""" if (HTML::is_secure_context(Bindings::principal_host_defined_environment_settings_object(realm))) {{
{text} }}
"""
if extended_attributes.get("Exposed") == "Window":
includes.add("LibWeb/HTML/Scripting/Environments.h")
text = text.replace("\n", "\n ")
text = f""" if (HTML::window_from_global_object(realm.global_object())) {{
{text} }}
"""
if "NavigatorCompatibilityMode" in extended_attributes:
# Only the named engine has this member, so it exists only while the navigator claims to be that engine.
includes.add("LibWeb/Loader/ResourceLoader.h")
includes.add("LibWebCommon/Loader/NavigatorCompatibilityMode.h")
mode = extended_attributes["NavigatorCompatibilityMode"]
text = text.replace("\n", "\n ")
text = f""" if (ResourceLoader::navigator_compatibility_mode() == NavigatorCompatibilityMode::{mode}) {{
{text} }}
"""
if "Experimental" in extended_attributes:
# The name is what a site-compatibility rule lists to expose this member for the sites it matches.
if experimental_name is None:
raise ValueError("[Experimental] members need a name for site-compatibility rules")
includes.add("LibWeb/Bindings/PrincipalHostDefined.h")
includes.add("LibWeb/HTML/WindowOrWorkerGlobalScope.h")
text = text.replace("\n", "\n ")
text = f""" if (HTML::WindowOrWorkerGlobalScopeMixin::expose_experimental_interface(Bindings::principal_host_defined_environment_settings_object(realm), "{experimental_name}"sv)) {{
{text} }}
"""
return text
def wrap_with_ce_reactions(includes: GeneratedIncludes, expression: str, this_object_realm: str) -> str:
includes.add("LibWeb/HTML/CustomElements/CustomElementReactions.h")
includes.add("LibWeb/HTML/Scripting/SimilarOriginWindowAgent.h")
return f"""[&]() -> decltype({expression}) {{
// For [CEReactions]: https://html.spec.whatwg.org/multipage/custom-elements.html#cereactions
// 1. Push a new element queue onto this object's relevant agent's custom element reactions stack.
auto& reactions_stack = Bindings::main_thread_similar_origin_window_agent().custom_element_reactions_stack;
reactions_stack.element_queue_stack.append({{}});
// 2. Run the originally-specified steps for this construct, catching any exceptions. If the steps return a value, let value be the returned value. If they throw an exception, let exception be the thrown exception.
auto value_or_exception = {expression};
// 3. Let queue be the result of popping from this object's relevant agent's custom element reactions stack.
// 4. Invoke custom element reactions in queue.
auto queue = reactions_stack.element_queue_stack.take_last();
HTML::invoke_custom_element_reactions(queue);
// 5. If an exception exception was thrown by the original steps, rethrow exception.
if (value_or_exception.is_error())
return value_or_exception.release_error();
// 6. If a value value was returned from the original steps, return value.
return value_or_exception.release_value();
}}()"""