Skip to content

Truly independent web browser

19.7 KB
Raw Blame
/*
 * Copyright (c) 2023, Preston Taylor <[email protected]>
 * Copyright (c) 2025, Lorenz Ackermann <[email protected]>
 * Copyright (c) 2026, Sam Atkins <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/ScopeGuard.h>
#include <LibWeb/CSS/Parser/Parser.h>
#include <LibWeb/CSS/StyleComputer.h>
#include <LibWeb/CSS/StyleEngineInput.h>
#include <LibWeb/CSS/StyleScope.h>
#include <LibWeb/ContentSecurityPolicy/BlockingAlgorithms.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/DOM/ShadowRoot.h>
#include <LibWeb/DOM/StyleElementBase.h>
#include <LibWeb/HTML/AttributeNames.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/HTMLStyleElement.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
#include <LibWeb/SVG/SVGStyleElement.h>
#include <LibWebCommon/Infra/Strings.h>

namespace Web::DOM {

void StyleElementBase::set_parser_document(Badge<HTML::HTMLParser>, GC::Ref<Document> document)
{
    m_parser_document = document;
    m_is_on_parser_stack_of_open_elements = true;
}

void StyleElementBase::did_pop_off_parser_stack_of_open_elements()
{
    m_is_on_parser_stack_of_open_elements = false;
    update_a_style_block(UpdateSource::ParserPop);
}

void StyleElementBase::update_a_style_block_for_dynamic_change()
{
    if (m_is_on_parser_stack_of_open_elements)
        return;
    update_a_style_block();
}

void StyleElementBase::style_element_attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& value)
{
    if (name == HTML::AttributeNames::media) {
        if (auto* sheet = this->sheet()) {
            sheet->set_media(value.has_value() ? value->utf16_view() : u""sv);
            associated_style_sheet_media_attribute_changed();
        }
    } else if (name == HTML::AttributeNames::type) {
        update_a_style_block_for_dynamic_change();
    }
}

void StyleElementBase::style_element_moved()
{
    if (!m_associated_css_style_sheet)
        return;

    VERIFY(m_style_sheet_scope);
    auto& destination = as_element().document_or_shadow_root_style_scope();
    m_style_sheet_scope->move_sheet(*m_associated_css_style_sheet, destination);
    m_style_sheet_scope = &destination;
}

bool StyleElementBase::style_element_was_created_by_parser() const
{
    return m_parser_document == &as_element().document();
}

// The user agent must run the "update a style block" algorithm whenever one of the following conditions occur:
// The element is popped off the stack of open elements of an HTML parser or XML parser.
//
// NOTE: This is basically done by children_changed() today:
// The element's children changed steps run.
//
// NOTE: This is basically done by inserted() and removed_from() today:
// The element is not on the stack of open elements of an HTML parser or XML parser, and it becomes connected or disconnected.
//
// https://html.spec.whatwg.org/multipage/semantics.html#update-a-style-block
void StyleElementBase::update_a_style_block(UpdateSource update_source)
{
    auto& style_element = as_element();

    // OPTIMIZATION: Skip parsing CSS if we're in the middle of parsing a HTML fragment.
    //               The style block will be parsed upon insertion into a proper document.
    if (style_element.document().is_temporary_document_for_fragment_parsing())
        return;

    // 1. Let element be the style element.
    // 2. If element has an associated CSS style sheet, remove the CSS style sheet in question.
    Optional<RefPtr<CSS::StyleSheetState>> replaced_sheet;
    Optional<GC::Root<Node>> replaced_sheet_root;
    bool defer_style_engine_update = false;
    if (m_associated_css_style_sheet) {
        defer_style_engine_update = m_associated_css_style_sheet->style_engine_sheet_id() != 0 && !m_associated_css_style_sheet->shared_compiled_style_sheet();
        replaced_sheet = m_associated_css_style_sheet;
        replaced_sheet_root = GC::Root { m_style_sheet_scope->node() };
        m_style_sheet_scope->remove_a_css_style_sheet(
            *m_associated_css_style_sheet,
            defer_style_engine_update ? CSS::StyleScope::StyleEngineUpdate::Defer : CSS::StyleScope::StyleEngineUpdate::Record);
        m_style_sheet_scope = nullptr;

        // FIXME: This should probably be handled by StyleSheet::set_owner_node().
        m_associated_css_style_sheet = nullptr;
    }
    auto record_deferred_detachment = [&] {
        if (!defer_style_engine_update)
            return;
        CSS::record_stylesheet_detached(**replaced_sheet, **replaced_sheet_root);
        defer_style_engine_update = false;
    };

    // AD-HOC: The script-blocking style sheet set tracks elements, but a style element's associated sheet can be
    //         replaced before that sheet's critical subresources finish loading. Keep that set and any queued
    //         completions in sync with the sheet removed above.
    ++m_style_sheet_update_generation;
    // NB: Keep an existing render-blocking entry while replacing its sheet. Once the body exists,
    //     removing the entry would prevent the new sheet from blocking rendering again.
    bool keep_render_blocking = false;
    ScopeGuard unblock_replaced_sheet = [&] {
        if (!keep_render_blocking && style_element.document().is_render_blocking_element(style_element))
            style_element.unblock_rendering();
    };
    remove_from_script_blocking_style_sheet_set_if_needed();
    clear_associated_css_style_sheet_parser_blocking_state();

    // 3. If element is not connected, then return.
    if (!style_element.is_connected()) {
        record_deferred_detachment();
        return;
    }

    // 4. If element's type attribute is present and its value is neither the empty string nor an ASCII case-insensitive match for "text/css", then return.
    auto type_attribute = style_element.attribute(HTML::AttributeNames::type);
    if (type_attribute.has_value() && !type_attribute->is_empty() && !type_attribute->equals_ignoring_ascii_case(u"text/css"sv)) {
        record_deferred_detachment();
        return;
    }

    // 5. If the Should element's inline behavior be blocked by Content Security Policy? algorithm returns "Blocked" when executed upon the style element, "style", and the style element's child text content, then return. [CSP]
    auto child_text = style_element.child_text_content();
    if (ContentSecurityPolicy::should_elements_inline_type_behavior_be_blocked_by_content_security_policy(style_element, ContentSecurityPolicy::Directives::Directive::InlineType::Style, child_text.utf16_view()) == ContentSecurityPolicy::Directives::Directive::Result::Blocked) {
        record_deferred_detachment();
        return;
    }

    // 6. Create a CSS style sheet with the following properties:
    //        type
    //            text/css
    //        owner node
    //            element
    //        media
    //            The media attribute of element.
    //        title
    //            The title attribute of element, if element is in a document tree, or the empty string otherwise.
    //        alternate flag
    //            Unset.
    //        origin-clean flag
    //            Set.
    //        location
    //        parent CSS style sheet
    //        owner CSS rule
    //            null
    //        disabled flag
    //            Left at its default value.
    //        CSS rules
    //          Left uninitialized.
    auto text_content = style_element.text_content();
    auto text_content_view = text_content.has_value() ? text_content->utf16_view() : u""sv;
    auto media_attribute = style_element.attribute(HTML::AttributeNames::media);
    auto media_attribute_view = media_attribute.has_value() ? media_attribute->utf16_view() : u""sv;
    auto title_attribute = style_element.in_a_document_tree() ? style_element.attribute(HTML::AttributeNames::title) : Optional<Utf16String> {};
    m_style_sheet_scope = &style_element.document_or_shadow_root_style_scope();
    m_associated_css_style_sheet = m_style_sheet_scope->create_a_css_style_sheet(
        text_content_view,
        &style_element,
        media_attribute_view,
        title_attribute.has_value() ? title_attribute.release_value() : Utf16String {},
        CSS::StyleScope::Alternate::No,
        CSS::StyleScope::OriginClean::Yes,
        {},
        nullptr,
        nullptr,
        defer_style_engine_update ? CSS::StyleScope::StyleEngineUpdate::Defer : CSS::StyleScope::StyleEngineUpdate::Record);

    if (defer_style_engine_update) {
        m_associated_css_style_sheet->set_style_engine_sheet_id((*replaced_sheet)->style_engine_sheet_id());
        m_associated_css_style_sheet->set_style_engine_occurrence_id((*replaced_sheet)->style_engine_occurrence_id());
        CSS::record_stylesheet_rules_replaced(*m_associated_css_style_sheet);
        m_associated_css_style_sheet->evaluate_media_queries(style_element.document());
        CSS::record_stylesheet_conditions(
            *m_associated_css_style_sheet,
            m_style_sheet_scope->node(),
            !m_associated_css_style_sheet->disabled() && m_associated_css_style_sheet->native_media_list().matches());
        defer_style_engine_update = false;
    }

    evaluate_associated_style_sheet_media_queries();
    if (update_source == UpdateSource::ParserPop) {
        m_associated_css_style_sheet_was_created_by_parser = true;
        m_associated_css_style_sheet_was_enabled_when_created_by_parser = !m_associated_css_style_sheet->disabled();
        m_associated_css_style_sheet_load_is_pending_for_script_blocking = m_associated_css_style_sheet_was_enabled_when_created_by_parser
            && m_associated_css_style_sheet_media_matches_environment
            && m_associated_css_style_sheet->loading_state() == CSS::StyleSheetState::LoadingState::Loading;
    }

    // 7. If element contributes a script-blocking style sheet, append element to its node document's script-blocking style sheet set.
    if (style_element.contributes_a_script_blocking_style_sheet()) {
        m_document_load_event_delayer.emplace(style_element.document());
        style_element.document().script_blocking_style_sheet_set().set(style_element);
        m_associated_css_style_sheet_is_blocking_scripts = true;
    }

    // 8. If element's media attribute's value matches the environment and element is potentially render-blocking, then block rendering on element.
    if (m_associated_css_style_sheet_media_matches_environment && style_element.is_potentially_render_blocking()) {
        style_element.block_rendering();
        keep_render_blocking = true;
    }

    // AD-HOC: Check if we have already loaded the sheet's resources.
    auto loading_state = m_associated_css_style_sheet->loading_state();
    if (loading_state == CSS::StyleSheetState::LoadingState::Loaded || loading_state == CSS::StyleSheetState::LoadingState::Error) {
        finished_loading_critical_subresources(loading_state == CSS::StyleSheetState::LoadingState::Error ? AnyFailed::Yes : AnyFailed::No);
    }
}

// https://html.spec.whatwg.org/multipage/semantics.html#the-style-element:critical-subresources
void StyleElementBase::finished_loading_critical_subresources(AnyFailed any_failed)
{
    // 1. Let element be the style element associated with the style sheet in question.
    auto& element = as_element();
    auto const style_sheet_update_generation = m_style_sheet_update_generation;

    // 2. Let success be true.
    auto success = true;

    // 3. If the attempts to obtain any of the style sheet's critical subresources failed for any reason
    //    (e.g., DNS error, HTTP 404 response, a connection being prematurely closed, unsupported Content-Type),
    //    set success to false.
    //    Note that content-specific errors, e.g., CSS parse errors or PNG decoding errors, do not affect success.
    if (any_failed == AnyFailed::Yes)
        success = false;

    // 4. Queue an element task on the networking task source given element and the following steps:
    element.queue_an_element_task(HTML::Task::Source::Networking, GC::create_function<void()>(GC::Heap::the(), GC::captures(element, success, style_sheet_update_generation), [](Element& element, auto& success, auto& style_sheet_update_generation) {
        // AD-HOC: If the associated stylesheet has been replaced or removed since this task was queued, ignore it.
        auto* style_element_base = as_if<StyleElementBase>(element);
        if (!style_element_base || style_element_base->m_style_sheet_update_generation != style_sheet_update_generation)
            return;

        // 1. If success is true, fire an event named load at element.
        // AD-HOC: these should call fire an event this is not implemented anywhere so we dispatch it ourselves
        if (success)
            element.dispatch_event(DOM::Event::create(
                HTML::EventNames::load,
                HighResolutionTime::current_high_resolution_time(HTML::relevant_global_object(element))));
        // 2. Otherwise, fire an event named error at element.
        else
            element.dispatch_event(DOM::Event::create(
                HTML::EventNames::error,
                HighResolutionTime::current_high_resolution_time(HTML::relevant_global_object(element))));
        // NB: A load or error handler can replace the sheet and start blocking for its new resources.
        if (style_element_base->m_style_sheet_update_generation != style_sheet_update_generation)
            return;

        // 3. If element contributes a script-blocking style sheet:
        if (element.contributes_a_script_blocking_style_sheet()) {
            // 1. Assert: element's node document's script-blocking style sheet set contains element.
            VERIFY(element.document().script_blocking_style_sheet_set().contains(element));
            VERIFY(style_element_base->m_associated_css_style_sheet_is_blocking_scripts);
            // 2. Remove element from its node document's script-blocking style sheet set.
            element.document().remove_from_script_blocking_style_sheet_set(element);
            style_element_base->m_associated_css_style_sheet_is_blocking_scripts = false;
        }
        // 4. Unblock rendering on element.
        element.unblock_rendering();
        style_element_base->m_associated_css_style_sheet_load_is_pending_for_script_blocking = false;
    }));
    m_document_load_event_delayer.clear();
}

void StyleElementBase::associated_style_sheet_media_attribute_changed()
{
    evaluate_associated_style_sheet_media_queries();
    if (!as_element().contributes_a_script_blocking_style_sheet()) {
        remove_from_script_blocking_style_sheet_set_if_needed();
        m_associated_css_style_sheet_load_is_pending_for_script_blocking = false;
    }
}

// https://html.spec.whatwg.org/multipage/semantics.html#contributes-a-script-blocking-style-sheet
bool StyleElementBase::style_element_contributes_a_script_blocking_style_sheet() const
{
    // An element el in the context of a Document of an HTML parser or XML parser contributes a script-blocking style
    // sheet if all of the following are true:
    auto& element = as_element();

    // el was created by that Document's parser.
    if (m_parser_document != &element.document())
        return false;

    if (!m_associated_css_style_sheet_was_created_by_parser)
        return false;

    // el is either a style element or a link element that was an external resource link that contributes to the
    // styling processing model when the el was created by the parser.
    // NB: This is a style element, so all good!

    // el's media attribute's value matches the environment.
    if (!m_associated_css_style_sheet_media_matches_environment)
        return false;

    // el's style sheet was enabled when the element was created by the parser.
    if (!m_associated_css_style_sheet_was_enabled_when_created_by_parser)
        return false;

    // FIXME: The last time the event loop reached step 1, el's root was that Document.

    // FIXME: The user agent hasn't given up on loading that particular style sheet yet.
    //        A user agent may give up on loading a style sheet at any time.

    // AD-HOC: Once this parser-created stylesheet has finished loading or stopped blocking scripts, it no longer
    //         contributes a script-blocking stylesheet even though it is still the current associated stylesheet.
    // FIXME: May not be needed once the other clauses above are implemented.
    if (!m_associated_css_style_sheet_load_is_pending_for_script_blocking)
        return false;

    return true;
}

void StyleElementBase::evaluate_associated_style_sheet_media_queries()
{
    if (!m_associated_css_style_sheet) {
        m_associated_css_style_sheet_media_matches_environment = false;
        return;
    }

    m_associated_css_style_sheet->evaluate_media_queries(as_element().document());
    m_associated_css_style_sheet_media_matches_environment = m_associated_css_style_sheet->native_media_list().matches();
}

void StyleElementBase::remove_from_script_blocking_style_sheet_set_if_needed()
{
    if (!m_associated_css_style_sheet_is_blocking_scripts)
        return;

    auto& element = as_element();
    if (element.document().script_blocking_style_sheet_set().contains(element))
        element.document().remove_from_script_blocking_style_sheet_set(element);

    m_associated_css_style_sheet_is_blocking_scripts = false;
    m_document_load_event_delayer.clear();
}

void StyleElementBase::clear_associated_css_style_sheet_parser_blocking_state()
{
    m_associated_css_style_sheet_was_created_by_parser = false;
    m_associated_css_style_sheet_was_enabled_when_created_by_parser = false;
    m_associated_css_style_sheet_media_matches_environment = false;
    m_associated_css_style_sheet_load_is_pending_for_script_blocking = false;
    m_associated_css_style_sheet_is_blocking_scripts = false;
}

// https://www.w3.org/TR/cssom/#dom-linkstyle-sheet
CSS::CSSStyleSheet* StyleElementBase::cssom_sheet() const
{
    return m_associated_css_style_sheet ? &m_associated_css_style_sheet->cssom_sheet() : nullptr;
}

CSS::StyleSheetState* StyleElementBase::sheet()
{
    // The sheet attribute must return the associated CSS style sheet for the node or null if there is no associated CSS style sheet.
    return m_associated_css_style_sheet.ptr();
}

// https://www.w3.org/TR/cssom/#dom-linkstyle-sheet
CSS::StyleSheetState const* StyleElementBase::sheet() const
{
    // The sheet attribute must return the associated CSS style sheet for the node or null if there is no associated CSS style sheet.
    return m_associated_css_style_sheet.ptr();
}

// https://html.spec.whatwg.org/multipage/semantics.html#dom-style-disabled
bool StyleElementBase::disabled()
{
    // 1. If this does not have an associated CSS style sheet, return false.
    if (!sheet())
        return false;

    // 2. If this's associated CSS style sheet's disabled flag is set, return true.
    if (sheet()->disabled())
        return true;

    // 3. Return false.
    return false;
}

// https://html.spec.whatwg.org/multipage/semantics.html#dom-style-disabled
void StyleElementBase::set_disabled(bool disabled)
{
    // 1. If this does not have an associated CSS style sheet, return.
    if (!sheet())
        return;

    // 2. If the given value is true, set this's associated CSS style sheet's disabled flag.
    //    Otherwise, unset this's associated CSS style sheet's disabled flag.
    sheet()->set_disabled(disabled);
}

void StyleElementBase::visit_style_element_edges(JS::Cell::Visitor& visitor)
{
    visitor.visit(m_associated_css_style_sheet);
    if (m_style_sheet_scope)
        visitor.visit(m_style_sheet_scope->node());
}

template<>
StyleElementBase* Node::fast_as<StyleElementBase>()
{
    if (auto* html_style_element = as_if<HTML::HTMLStyleElement>(*this))
        return html_style_element;
    if (auto* svg_style_element = as_if<SVG::SVGStyleElement>(*this))
        return svg_style_element;
    return nullptr;
}

template<>
StyleElementBase const* Node::fast_as<StyleElementBase>() const
{
    return const_cast<Node&>(*this).fast_as<StyleElementBase>();
}

}