1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-09 09:34:57 +09:00

LibWeb: Set the initiator type for script-initiated CSS requests

Similar to other ad-hoc behavior in this method, we need to handle
requests which are not associated with a style sheet. For example:

    <script>
        const element = document.createElement('div');
        element.style['background'] = 'url(https://foo.com/img.png)';
        document.body.appendChild(element);
    </script>

Will not be associated with a style sheet.

This is needed to ensure we fire a PerformanceResourceTiming event for
this resource load. This is not currently testable in CI, as this event
is also gated by HTTP/S requests.
This commit is contained in:
Timothy Flynn 2025-05-06 10:22:33 -04:00 committed by Sam Atkins
parent 1d63398cbd
commit 0da7441b9b
Notes: github-actions[bot] 2025-05-06 16:38:32 +00:00

View file

@ -27,12 +27,12 @@ static WebIDL::ExceptionOr<GC::Ref<Fetch::Infrastructure::Request>> fetch_a_styl
// 2. Let base be sheets stylesheet base URL if it is not null, otherwise environmentSettingss API base URL. [CSSOM]
// AD-HOC: We use the sheet's location if it has no base url. https://github.com/w3c/csswg-drafts/issues/12068
auto base = sheet_or_document.visit(
[&](GC::Ref<CSSStyleSheet> const& sheet) {
[&](GC::Ref<CSSStyleSheet> sheet) {
return sheet->base_url()
.value_or_lazy_evaluated_optional([&sheet] { return sheet->location(); })
.value_or_lazy_evaluated([&environment_settings] { return environment_settings.api_base_url(); });
},
[](GC::Ref<DOM::Document> const& document) { return document->base_url(); });
[](GC::Ref<DOM::Document> document) { return document->base_url(); });
// 3. Let parsedUrl be the result of the URL parser steps with urlValues url and base. If the algorithm returns an error, return.
auto url_string = url_value.visit(
@ -66,14 +66,22 @@ static WebIDL::ExceptionOr<GC::Ref<Fetch::Infrastructure::Request>> fetch_a_styl
// 7. If reqs mode is "cors", set reqs referrer to sheets location. [CSSOM]
if (request->mode() == Fetch::Infrastructure::Request::Mode::CORS) {
auto location = sheet_or_document.visit(
[](GC::Ref<CSSStyleSheet> const& sheet) { return sheet->location().value(); },
[](GC::Ref<DOM::Document> const& document) { return document->url(); });
[](GC::Ref<CSSStyleSheet> sheet) { return sheet->location().value(); },
[](GC::Ref<DOM::Document> document) { return document->url(); });
request->set_referrer(move(location));
}
// 8. If sheets origin-clean flag is set, set reqs initiator type to "css". [CSSOM]
if (auto* sheet = sheet_or_document.get_pointer<GC::Ref<CSSStyleSheet>>(); sheet && (*sheet)->is_origin_clean())
request->set_initiator_type(Fetch::Infrastructure::Request::InitiatorType::CSS);
sheet_or_document.visit(
[&](GC::Ref<CSSStyleSheet> sheet) {
// 8. If sheets origin-clean flag is set, set reqs initiator type to "css". [CSSOM]
if (sheet->is_origin_clean())
request->set_initiator_type(Fetch::Infrastructure::Request::InitiatorType::CSS);
},
[&](GC::Ref<DOM::Document>) {
// AD-HOC: If the resource is not associated with a stylesheet, we must still set an initiator type in order
// for this resource to be observable through a PerformanceObserver. WPT relies on this.
request->set_initiator_type(Fetch::Infrastructure::Request::InitiatorType::Script);
});
// 9. Fetch req, with processresponseconsumebody set to processResponse.
// NB: Implemented by caller.