script/dom/html/
htmlstyleelement.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::cell::Cell;
6use std::sync::atomic::{AtomicBool, Ordering};
7
8use dom_struct::dom_struct;
9use html5ever::{LocalName, Prefix, local_name};
10use js::context::JSContext;
11use js::rust::HandleObject;
12use net_traits::ReferrerPolicy;
13use script_bindings::root::Dom;
14use servo_arc::Arc;
15use style::media_queries::MediaList as StyleMediaList;
16use style::stylesheets::{Stylesheet, StylesheetInDocument, UrlExtraData};
17use stylo_atoms::Atom;
18
19use crate::dom::attr::Attr;
20use crate::dom::bindings::cell::DomRefCell;
21use crate::dom::bindings::codegen::Bindings::DOMTokenListBinding::DOMTokenList_Binding::DOMTokenListMethods;
22use crate::dom::bindings::codegen::Bindings::HTMLStyleElementBinding::HTMLStyleElementMethods;
23use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
24use crate::dom::bindings::inheritance::Castable;
25use crate::dom::bindings::root::{DomRoot, MutNullableDom};
26use crate::dom::bindings::str::DOMString;
27use crate::dom::csp::{CspReporting, InlineCheckType};
28use crate::dom::css::cssstylesheet::CSSStyleSheet;
29use crate::dom::css::stylesheet::StyleSheet as DOMStyleSheet;
30use crate::dom::css::stylesheetcontentscache::{
31    StylesheetContentsCache, StylesheetContentsCacheKey,
32};
33use crate::dom::document::Document;
34use crate::dom::documentorshadowroot::StylesheetSource;
35use crate::dom::domtokenlist::DOMTokenList;
36use crate::dom::element::{AttributeMutation, Element, ElementCreator};
37use crate::dom::html::htmlelement::HTMLElement;
38use crate::dom::medialist::MediaList;
39use crate::dom::node::{BindContext, ChildrenMutation, Node, NodeTraits, UnbindContext};
40use crate::dom::virtualmethods::VirtualMethods;
41use crate::script_runtime::CanGc;
42use crate::stylesheet_loader::StylesheetOwner;
43
44#[dom_struct]
45pub(crate) struct HTMLStyleElement {
46    htmlelement: HTMLElement,
47    #[conditional_malloc_size_of]
48    #[no_trace]
49    stylesheet: DomRefCell<Option<Arc<Stylesheet>>>,
50    #[no_trace]
51    stylesheetcontents_cache_key: DomRefCell<Option<StylesheetContentsCacheKey>>,
52    cssom_stylesheet: MutNullableDom<CSSStyleSheet>,
53    /// <https://html.spec.whatwg.org/multipage/#a-style-sheet-that-is-blocking-scripts>
54    parser_inserted: Cell<bool>,
55    in_stack_of_open_elements: Cell<bool>,
56    pending_loads: Cell<u32>,
57    any_failed_load: Cell<bool>,
58    /// <https://html.spec.whatwg.org/multipage/#dom-style-blocking>
59    blocking: MutNullableDom<DOMTokenList>,
60}
61
62impl HTMLStyleElement {
63    fn new_inherited(
64        local_name: LocalName,
65        prefix: Option<Prefix>,
66        document: &Document,
67        creator: ElementCreator,
68    ) -> HTMLStyleElement {
69        HTMLStyleElement {
70            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
71            stylesheet: DomRefCell::new(None),
72            stylesheetcontents_cache_key: DomRefCell::new(None),
73            cssom_stylesheet: MutNullableDom::new(None),
74            parser_inserted: Cell::new(creator.is_parser_created()),
75            in_stack_of_open_elements: Cell::new(creator.is_parser_created()),
76            pending_loads: Cell::new(0),
77            any_failed_load: Cell::new(false),
78            blocking: Default::default(),
79        }
80    }
81
82    pub(crate) fn new(
83        cx: &mut js::context::JSContext,
84        local_name: LocalName,
85        prefix: Option<Prefix>,
86        document: &Document,
87        proto: Option<HandleObject>,
88        creator: ElementCreator,
89    ) -> DomRoot<HTMLStyleElement> {
90        Node::reflect_node_with_proto(
91            cx,
92            Box::new(HTMLStyleElement::new_inherited(
93                local_name, prefix, document, creator,
94            )),
95            document,
96            proto,
97        )
98    }
99
100    #[inline]
101    fn create_media_list(&self, mq_str: &str) -> StyleMediaList {
102        MediaList::parse_media_list(mq_str, &self.owner_window())
103    }
104
105    /// <https://html.spec.whatwg.org/multipage/#update-a-style-block>
106    pub(crate) fn update_a_style_block(&self) {
107        // Step 1. Let element be the style element.
108        //
109        // That's self
110
111        // Step 2. If element has an associated CSS style sheet, remove the CSS style sheet in question.
112        self.remove_stylesheet();
113
114        // Step 3. If element is not connected, then return.
115        let node = self.upcast::<Node>();
116        if !node.is_connected() {
117            return;
118        }
119        assert!(
120            node.is_in_a_document_tree() || node.is_in_a_shadow_tree(),
121            "This stylesheet does not have an owner, so there's no reason to parse its contents"
122        );
123
124        // Step 4. If element's type attribute is present and its value is neither the empty string
125        // nor an ASCII case-insensitive match for "text/css", then return.
126        let mut type_attribute = self.Type();
127        type_attribute.make_ascii_lowercase();
128        if !type_attribute.is_empty() && type_attribute != "text/css" {
129            return;
130        }
131
132        // Step 5: If the Should element's inline behavior be blocked by Content Security Policy? algorithm
133        // returns "Blocked" when executed upon the style element, "style",
134        // and the style element's child text content, then return. [CSP]
135        let doc = self.owner_document();
136        let global = &self.owner_global();
137        if global
138            .get_csp_list()
139            .should_elements_inline_type_behavior_be_blocked(
140                global,
141                self.upcast(),
142                InlineCheckType::Style,
143                &node.child_text_content().str(),
144                doc.get_current_parser_line(),
145            )
146        {
147            return;
148        }
149
150        // Step 6. Create a CSS style sheet with the following properties:
151        let data = node
152            .GetTextContent()
153            .expect("Element.textContent must be a string");
154        let shared_lock = node.owner_doc().style_shared_lock().clone();
155        let mq = Arc::new(shared_lock.wrap(self.create_media_list(&self.Media().str())));
156
157        // For duplicate style sheets with identical content, `StylesheetContents` can be reused
158        // to avoid reedundant parsing of the style sheets. Additionally, the cache hit rate of
159        // stylo's `CascadeDataCache` can now be significantly improved. When shared `StylesheetContents`
160        // is modified, copy-on-write will occur, see `CSSStyleSheet::will_modify`.
161        let (cache_key, contents) = StylesheetContentsCache::get_or_insert_with(
162            &data.str(),
163            &shared_lock,
164            UrlExtraData(doc.base_url().get_arc()),
165            doc.quirks_mode(),
166            self.upcast(),
167        );
168
169        let sheet = Arc::new(Stylesheet {
170            contents: shared_lock.wrap(contents),
171            shared_lock,
172            media: mq,
173            disabled: AtomicBool::new(false),
174        });
175
176        // From here we differ from the spec. Since we have a cache,
177        // we have two situations:
178        //
179        // 1. We hit the cache. No fetch ever runs, hence we don't
180        // need to muddy with render-/load-blocking
181        // 2. We don't hit the cache. In this scenario, we once again
182        // have two sub-scenarios:
183        //   a. We synchronously parse the contents without any `@import`.
184        //      This means we can proceed as in situation 1
185        //   b. We synchronously parse the contents and we encounter 1+
186        //      `@import` rules. In that case, we do start to load urls.
187        //
188        // For situation 1 and 2a, we can immediately fire the load event
189        // since we are done.
190        if self.pending_loads.get() == 0 {
191            // Step 4 of https://html.spec.whatwg.org/multipage/#the-style-element:critical-subresources
192            //
193            // Step 4. Queue an element task on the networking task source given element and the following steps:
194            // Step 4.1. If success is true, fire an event named load at element.
195            self.owner_global()
196                .task_manager()
197                .networking_task_source()
198                .queue_simple_event(self.upcast(), atom!("load"));
199        }
200
201        // For situation 2b, we need to do more work.
202        // Therefore, the following steps are actually implemented in
203        // `ElementStylesheetLoader::load_with_element`.
204        //
205        //     Step 7. If element contributes a script-blocking style sheet,
206        //     append element to its node document's script-blocking style sheet set.
207        //
208        //     Step 8. If element's media attribute's value matches the environment
209        //     and element is potentially render-blocking, then block rendering on element.
210
211        // Finally, update our stylesheet, regardless of which scenario we ran into
212        self.clean_stylesheet_ownership();
213        self.set_stylesheet(sheet, cache_key);
214    }
215
216    // FIXME(emilio): This is duplicated with HTMLLinkElement::set_stylesheet.
217    //
218    // With the reuse of `StylesheetContent` for same stylesheet string content,
219    // this function has a bit difference with `HTMLLinkElement::set_stylesheet` now.
220    pub(crate) fn set_stylesheet(
221        &self,
222        s: Arc<Stylesheet>,
223        cache_key: Option<StylesheetContentsCacheKey>,
224    ) {
225        *self.stylesheet.borrow_mut() = Some(s.clone());
226        *self.stylesheetcontents_cache_key.borrow_mut() = cache_key;
227        self.stylesheet_list_owner()
228            .add_owned_stylesheet(self.upcast(), s);
229    }
230
231    pub(crate) fn will_modify_stylesheet(&self) {
232        if let Some(stylesheet_with_owned_contents) = self.create_owned_contents_stylesheet() {
233            self.remove_stylesheet();
234            if let Some(cssom_stylesheet) = self.cssom_stylesheet.get() {
235                let guard = stylesheet_with_owned_contents.shared_lock.read();
236                cssom_stylesheet.update_style_stylesheet(&stylesheet_with_owned_contents, &guard);
237            }
238            self.set_stylesheet(stylesheet_with_owned_contents, None);
239        }
240    }
241
242    pub(crate) fn get_stylesheet(&self) -> Option<Arc<Stylesheet>> {
243        self.stylesheet.borrow().clone()
244    }
245
246    pub(crate) fn get_cssom_stylesheet(&self) -> Option<DomRoot<CSSStyleSheet>> {
247        self.get_stylesheet().map(|sheet| {
248            self.cssom_stylesheet.or_init(|| {
249                CSSStyleSheet::new(
250                    &self.owner_window(),
251                    Some(self.upcast::<Element>()),
252                    "text/css".into(),
253                    None, // todo handle location
254                    None, // todo handle title
255                    sheet,
256                    None, // constructor_document
257                    CanGc::note(),
258                )
259            })
260        })
261    }
262
263    fn create_owned_contents_stylesheet(&self) -> Option<Arc<Stylesheet>> {
264        let cache_key = self.stylesheetcontents_cache_key.borrow_mut().take()?;
265        if cache_key.is_uniquely_owned() {
266            StylesheetContentsCache::remove(cache_key);
267            return None;
268        }
269
270        let stylesheet_with_shared_contents = self.stylesheet.borrow().clone()?;
271        let lock = stylesheet_with_shared_contents.shared_lock.clone();
272        let guard = stylesheet_with_shared_contents.shared_lock.read();
273        let stylesheet_with_owned_contents = Arc::new(Stylesheet {
274            contents: lock.wrap(
275                stylesheet_with_shared_contents
276                    .contents(&guard)
277                    .deep_clone(&lock, None, &guard),
278            ),
279            shared_lock: lock,
280            media: stylesheet_with_shared_contents.media.clone(),
281            disabled: AtomicBool::new(
282                stylesheet_with_shared_contents
283                    .disabled
284                    .load(Ordering::SeqCst),
285            ),
286        });
287
288        Some(stylesheet_with_owned_contents)
289    }
290
291    fn clean_stylesheet_ownership(&self) {
292        if let Some(cssom_stylesheet) = self.cssom_stylesheet.get() {
293            // If the CSSOMs change from having an owner node to being ownerless, they may still
294            // potentially modify shared stylesheets. Thus, create an new `Stylesheet` with owned
295            // `StylesheetContents` to ensure that the potentially modifications are only made on
296            // the owned `StylesheetContents`.
297            if let Some(stylesheet) = self.create_owned_contents_stylesheet() {
298                let guard = stylesheet.shared_lock.read();
299                cssom_stylesheet.update_style_stylesheet(&stylesheet, &guard);
300            }
301            cssom_stylesheet.set_owner_node(None);
302        }
303        self.cssom_stylesheet.set(None);
304    }
305
306    fn remove_stylesheet(&self) {
307        self.clean_stylesheet_ownership();
308        if let Some(s) = self.stylesheet.borrow_mut().take() {
309            self.stylesheet_list_owner()
310                .remove_stylesheet(StylesheetSource::Element(Dom::from_ref(self.upcast())), &s);
311            let _ = self.stylesheetcontents_cache_key.borrow_mut().take();
312        }
313    }
314}
315
316impl VirtualMethods for HTMLStyleElement {
317    fn super_type(&self) -> Option<&dyn VirtualMethods> {
318        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
319    }
320
321    fn children_changed(&self, cx: &mut JSContext, mutation: &ChildrenMutation) {
322        self.super_type().unwrap().children_changed(cx, mutation);
323
324        // https://html.spec.whatwg.org/multipage/#update-a-style-block
325        // > The element is not on the stack of open elements of an HTML parser or XML parser, and its children changed steps run.
326        if !self.in_stack_of_open_elements.get() {
327            self.update_a_style_block();
328        }
329    }
330
331    fn bind_to_tree(&self, cx: &mut JSContext, context: &BindContext) {
332        self.super_type().unwrap().bind_to_tree(cx, context);
333
334        // https://html.spec.whatwg.org/multipage/#update-a-style-block
335        // > The element is not on the stack of open elements of an HTML parser or XML parser, and it becomes connected or disconnected.
336        if !self.in_stack_of_open_elements.get() {
337            self.update_a_style_block();
338        }
339    }
340
341    fn pop(&self) {
342        self.super_type().unwrap().pop();
343        self.in_stack_of_open_elements.set(false);
344
345        // https://html.spec.whatwg.org/multipage/#update-a-style-block
346        // > The element is popped off the stack of open elements of an HTML parser or XML parser.
347        self.update_a_style_block();
348    }
349
350    fn unbind_from_tree(&self, context: &UnbindContext, can_gc: CanGc) {
351        if let Some(s) = self.super_type() {
352            s.unbind_from_tree(context, can_gc);
353        }
354
355        // https://html.spec.whatwg.org/multipage/#update-a-style-block
356        // > The element is not on the stack of open elements of an HTML parser or XML parser, and it becomes connected or disconnected.
357        if !self.in_stack_of_open_elements.get() {
358            self.update_a_style_block();
359        }
360    }
361
362    fn attribute_mutated(
363        &self,
364        cx: &mut js::context::JSContext,
365        attr: &Attr,
366        mutation: AttributeMutation,
367    ) {
368        if let Some(s) = self.super_type() {
369            s.attribute_mutated(cx, attr, mutation);
370        }
371
372        let node = self.upcast::<Node>();
373        if !(node.is_in_a_document_tree() || node.is_in_a_shadow_tree()) ||
374            self.in_stack_of_open_elements.get()
375        {
376            return;
377        }
378
379        if attr.name() == "type" {
380            if let AttributeMutation::Set(Some(old_value), _) = mutation {
381                if **old_value == **attr.value() {
382                    return;
383                }
384            }
385            self.remove_stylesheet();
386            self.update_a_style_block();
387        } else if attr.name() == "media" {
388            if let Some(ref stylesheet) = *self.stylesheet.borrow_mut() {
389                let shared_lock = node.owner_doc().style_shared_lock().clone();
390                let mut guard = shared_lock.write();
391                let media = stylesheet.media.write_with(&mut guard);
392                match mutation {
393                    AttributeMutation::Set(..) => *media = self.create_media_list(&attr.value()),
394                    AttributeMutation::Removed => *media = StyleMediaList::empty(),
395                };
396                self.owner_document().invalidate_stylesheets();
397            }
398        }
399    }
400}
401
402impl StylesheetOwner for HTMLStyleElement {
403    fn increment_pending_loads_count(&self) {
404        self.pending_loads.set(self.pending_loads.get() + 1)
405    }
406
407    fn load_finished(&self, succeeded: bool) -> Option<bool> {
408        assert!(self.pending_loads.get() > 0, "What finished?");
409        if !succeeded {
410            self.any_failed_load.set(true);
411        }
412
413        self.pending_loads.set(self.pending_loads.get() - 1);
414        if self.pending_loads.get() != 0 {
415            return None;
416        }
417
418        let any_failed = self.any_failed_load.get();
419        self.any_failed_load.set(false);
420        Some(any_failed)
421    }
422
423    fn parser_inserted(&self) -> bool {
424        self.parser_inserted.get()
425    }
426
427    /// <https://html.spec.whatwg.org/multipage/#potentially-render-blocking>
428    fn potentially_render_blocking(&self) -> bool {
429        // An element is potentially render-blocking if its blocking tokens set contains "render",
430        // or if it is implicitly potentially render-blocking, which will be defined at the individual elements.
431        // By default, an element is not implicitly potentially render-blocking.
432        //
433        // https://html.spec.whatwg.org/multipage/#the-style-element:implicitly-potentially-render-blocking
434        // > A style element is implicitly potentially render-blocking if the element was created by its node document's parser.
435        self.parser_inserted() ||
436            self.blocking
437                .get()
438                .is_some_and(|list| list.Contains("render".into()))
439    }
440
441    fn referrer_policy(&self) -> ReferrerPolicy {
442        ReferrerPolicy::EmptyString
443    }
444
445    fn set_origin_clean(&self, origin_clean: bool) {
446        if let Some(stylesheet) = self.get_cssom_stylesheet() {
447            stylesheet.set_origin_clean(origin_clean);
448        }
449    }
450}
451
452impl HTMLStyleElementMethods<crate::DomTypeHolder> for HTMLStyleElement {
453    /// <https://drafts.csswg.org/cssom/#dom-linkstyle-sheet>
454    fn GetSheet(&self) -> Option<DomRoot<DOMStyleSheet>> {
455        self.get_cssom_stylesheet().map(DomRoot::upcast)
456    }
457
458    /// <https://html.spec.whatwg.org/multipage/#dom-style-disabled>
459    fn Disabled(&self) -> bool {
460        self.get_cssom_stylesheet()
461            .is_some_and(|sheet| sheet.disabled())
462    }
463
464    /// <https://html.spec.whatwg.org/multipage/#dom-style-disabled>
465    fn SetDisabled(&self, value: bool) {
466        if let Some(sheet) = self.get_cssom_stylesheet() {
467            sheet.set_disabled(value);
468        }
469    }
470
471    // <https://html.spec.whatwg.org/multipage/#HTMLStyleElement-partial>
472    make_getter!(Type, "type");
473
474    // <https://html.spec.whatwg.org/multipage/#HTMLStyleElement-partial>
475    make_setter!(SetType, "type");
476
477    // <https://html.spec.whatwg.org/multipage/#attr-style-media>
478    make_getter!(Media, "media");
479
480    // <https://html.spec.whatwg.org/multipage/#attr-style-media>
481    make_setter!(SetMedia, "media");
482
483    /// <https://html.spec.whatwg.org/multipage/#attr-style-blocking>
484    fn Blocking(&self, can_gc: CanGc) -> DomRoot<DOMTokenList> {
485        self.blocking.or_init(|| {
486            DOMTokenList::new(
487                self.upcast(),
488                &local_name!("blocking"),
489                Some(vec![Atom::from("render")]),
490                can_gc,
491            )
492        })
493    }
494}