From 8ac9dfcde602e274d9e04f12ab88bcfa23960177 Mon Sep 17 00:00:00 2001 From: Uthman Yahaya Baba Date: Mon, 19 May 2025 20:59:36 +0100 Subject: [PATCH] Add Helper method for creating new WebFontDocumentContext Signed-off-by: Uthman Yahaya Baba --- components/script/dom/document.rs | 10 +--------- components/script/dom/fontface.rs | 14 +++----------- components/script/dom/window.rs | 17 +++++++++++------ components/script/stylesheet_loader.rs | 12 ++---------- 4 files changed, 17 insertions(+), 36 deletions(-) diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index 9ba4331cfba..9d8029841c4 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -4918,15 +4918,7 @@ impl Document { if self.has_browsing_context() { // Construct WebFontDocumentContext for font fetching - let document_context = WebFontDocumentContext { - policy_container: self.window.global().policy_container(), - document_url: self.window.global().api_base_url(), - has_trustworthy_ancestor_origin: self - .window - .global() - .has_trustworthy_ancestor_origin(), - insecure_requests_policy: self.window.global().insecure_requests_policy(), - }; + let document_context = self.window.new_document_context(); self.window.layout_mut().add_stylesheet( sheet.clone(), diff --git a/components/script/dom/fontface.rs b/components/script/dom/fontface.rs index 896305ebec8..c0ace7b84fb 100644 --- a/components/script/dom/fontface.rs +++ b/components/script/dom/fontface.rs @@ -7,10 +7,7 @@ use std::rc::Rc; use cssparser::{Parser, ParserInput}; use dom_struct::dom_struct; -use fonts::{ - FontContext, FontContextWebFontMethods, FontTemplate, LowercaseFontFamilyName, - WebFontDocumentContext, -}; +use fonts::{FontContext, FontContextWebFontMethods, FontTemplate, LowercaseFontFamilyName}; use js::rust::HandleObject; use style::error_reporting::ParseErrorReporter; use style::font_face::SourceList; @@ -543,13 +540,8 @@ impl FontFaceMethods for FontFace { ) .expect("Parsing shouldn't fail as descriptors are valid by construction"); - //Construct a WebFontDocumentContext object for the current document. - let document_context = WebFontDocumentContext { - policy_container: global.policy_container(), - document_url: global.api_base_url(), - has_trustworthy_ancestor_origin: global.has_trustworthy_ancestor_origin(), - insecure_requests_policy: global.insecure_requests_policy(), - }; + // Construct a WebFontDocumentContext object for the current document. + let document_context = global.as_window().new_document_context(); // Step 4. Using the value of font face’s [[Urls]] slot, attempt to load a font as defined // in [CSS-FONTS-3], as if it was the value of a @font-face rule’s src descriptor. diff --git a/components/script/dom/window.rs b/components/script/dom/window.rs index b2e49a2772f..71c37aaa343 100644 --- a/components/script/dom/window.rs +++ b/components/script/dom/window.rs @@ -769,6 +769,15 @@ impl Window { }) ); } + + pub fn new_document_context(&self) -> WebFontDocumentContext { + WebFontDocumentContext { + policy_container: self.global().policy_container(), + document_url: self.global().api_base_url(), + has_trustworthy_ancestor_origin: self.global().has_trustworthy_ancestor_origin(), + insecure_requests_policy: self.global().insecure_requests_policy(), + } + } } // https://html.spec.whatwg.org/multipage/#atob @@ -2171,12 +2180,8 @@ impl Window { let highlighted_dom_node = document.highlighted_dom_node().map(|node| node.to_opaque()); //Construct a new document context for the reflow. - let document_context = WebFontDocumentContext { - policy_container: self.global().policy_container(), - document_url: self.global().api_base_url(), - has_trustworthy_ancestor_origin: self.global().has_trustworthy_ancestor_origin(), - insecure_requests_policy: self.global().insecure_requests_policy(), - }; + let document_context = self.new_document_context(); + // Send new document and relevant styles to layout. let reflow = ReflowRequest { reflow_info: Reflow { diff --git a/components/script/stylesheet_loader.rs b/components/script/stylesheet_loader.rs index e17319178a6..6f311d19523 100644 --- a/components/script/stylesheet_loader.rs +++ b/components/script/stylesheet_loader.rs @@ -8,7 +8,6 @@ use std::sync::atomic::AtomicBool; use content_security_policy as csp; use cssparser::SourceLocation; use encoding_rs::UTF_8; -use fonts::WebFontDocumentContext; use mime::{self, Mime}; use net_traits::request::{CorsSettings, Destination, RequestId}; use net_traits::{ @@ -233,15 +232,8 @@ impl FetchResponseListener for StylesheetContext { Some(&loader), win.css_error_reporter(), ); - //Construct a new WebFontDocumentContext for the stylesheet - let document_context = WebFontDocumentContext { - policy_container: win.global().policy_container(), - document_url: win.global().api_base_url(), - has_trustworthy_ancestor_origin: win - .global() - .has_trustworthy_ancestor_origin(), - insecure_requests_policy: win.global().insecure_requests_policy(), - }; + // Construct a new WebFontDocumentContext for the stylesheet + let document_context = win.new_document_context(); // Layout knows about this stylesheet, because Stylo added it to the Stylist, // but Layout doesn't know about any new web fonts that it contains.