1use 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::cell::DomRefCell;
14use script_bindings::root::Dom;
15use servo_arc::Arc;
16use style::media_queries::MediaList as StyleMediaList;
17use style::stylesheets::{Stylesheet, StylesheetInDocument, UrlExtraData};
18use stylo_atoms::Atom;
19
20use crate::dom::bindings::codegen::Bindings::DOMTokenListBinding::DOMTokenList_Binding::DOMTokenListMethods;
21use crate::dom::bindings::codegen::Bindings::HTMLStyleElementBinding::HTMLStyleElementMethods;
22use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
23use crate::dom::bindings::inheritance::Castable;
24use crate::dom::bindings::root::{DomRoot, MutNullableDom};
25use crate::dom::bindings::str::DOMString;
26use crate::dom::csp::{CspReporting, InlineCheckType};
27use crate::dom::css::cssstylesheet::CSSStyleSheet;
28use crate::dom::css::stylesheet::StyleSheet as DOMStyleSheet;
29use crate::dom::css::stylesheetcontentscache::{
30 StylesheetContentsCache, StylesheetContentsCacheKey,
31};
32use crate::dom::document::Document;
33use crate::dom::documentorshadowroot::StylesheetSource;
34use crate::dom::domtokenlist::DOMTokenList;
35use crate::dom::element::attributes::storage::AttrRef;
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 parser_inserted: Cell<bool>,
55 in_stack_of_open_elements: Cell<bool>,
56 pending_loads: Cell<u32>,
57 any_failed_load: Cell<bool>,
58 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 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 pub(crate) fn update_a_style_block(&self, cx: &mut JSContext) {
107 self.remove_stylesheet();
113
114 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 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 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 let data = node
152 .GetTextContent()
153 .expect("Element.textContent must be a string");
154 let shared_lock = node.owner_doc().style_shared_author_lock().clone();
155 let mq = Arc::new(shared_lock.wrap(self.create_media_list(&self.Media().str())));
156
157 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 if self.pending_loads.get() == 0 {
191 self.owner_global()
196 .task_manager()
197 .networking_task_source()
198 .queue_simple_event(self.upcast(), atom!("load"));
199 }
200
201 self.clean_stylesheet_ownership();
213 self.set_stylesheet(cx, sheet, cache_key);
214 }
215
216 pub(crate) fn set_stylesheet(
221 &self,
222 cx: &mut JSContext,
223 s: Arc<Stylesheet>,
224 cache_key: Option<StylesheetContentsCacheKey>,
225 ) {
226 *self.stylesheet.borrow_mut() = Some(s.clone());
227 *self.stylesheetcontents_cache_key.borrow_mut() = cache_key;
228 self.stylesheet_list_owner()
229 .add_owned_stylesheet(cx, self.upcast(), s);
230 }
231
232 pub(crate) fn will_modify_stylesheet(&self, cx: &mut JSContext) {
233 if let Some(stylesheet_with_owned_contents) = self.create_owned_contents_stylesheet() {
234 self.remove_stylesheet();
235 if let Some(cssom_stylesheet) = self.cssom_stylesheet.get() {
236 let guard = stylesheet_with_owned_contents.shared_lock.read();
237 cssom_stylesheet.update_style_stylesheet(&stylesheet_with_owned_contents, &guard);
238 }
239 self.set_stylesheet(cx, stylesheet_with_owned_contents, None);
240 }
241 }
242
243 pub(crate) fn get_stylesheet(&self) -> Option<Arc<Stylesheet>> {
244 self.stylesheet.borrow().clone()
245 }
246
247 pub(crate) fn get_cssom_stylesheet(&self) -> Option<DomRoot<CSSStyleSheet>> {
248 self.get_stylesheet().map(|sheet| {
249 self.cssom_stylesheet.or_init(|| {
250 CSSStyleSheet::new(
251 &self.owner_window(),
252 Some(self.upcast::<Element>()),
253 "text/css".into(),
254 None, None, sheet,
257 None, CanGc::deprecated_note(),
259 )
260 })
261 })
262 }
263
264 fn create_owned_contents_stylesheet(&self) -> Option<Arc<Stylesheet>> {
265 let cache_key = self.stylesheetcontents_cache_key.borrow_mut().take()?;
266 if cache_key.is_uniquely_owned() {
267 StylesheetContentsCache::remove(cache_key);
268 return None;
269 }
270
271 let stylesheet_with_shared_contents = self.stylesheet.borrow().clone()?;
272 let lock = stylesheet_with_shared_contents.shared_lock.clone();
273 let guard = stylesheet_with_shared_contents.shared_lock.read();
274 let stylesheet_with_owned_contents = Arc::new(Stylesheet {
275 contents: lock.wrap(
276 stylesheet_with_shared_contents
277 .contents(&guard)
278 .deep_clone(&lock, None, &guard),
279 ),
280 shared_lock: lock,
281 media: stylesheet_with_shared_contents.media.clone(),
282 disabled: AtomicBool::new(
283 stylesheet_with_shared_contents
284 .disabled
285 .load(Ordering::SeqCst),
286 ),
287 });
288
289 Some(stylesheet_with_owned_contents)
290 }
291
292 fn clean_stylesheet_ownership(&self) {
293 if let Some(cssom_stylesheet) = self.cssom_stylesheet.get() {
294 if let Some(stylesheet) = self.create_owned_contents_stylesheet() {
299 let guard = stylesheet.shared_lock.read();
300 cssom_stylesheet.update_style_stylesheet(&stylesheet, &guard);
301 }
302 cssom_stylesheet.set_owner_node(None);
303 }
304 self.cssom_stylesheet.set(None);
305 }
306
307 fn remove_stylesheet(&self) {
308 self.clean_stylesheet_ownership();
309 if let Some(s) = self.stylesheet.borrow_mut().take() {
310 self.stylesheet_list_owner()
311 .remove_stylesheet(StylesheetSource::Element(Dom::from_ref(self.upcast())), &s);
312 let _ = self.stylesheetcontents_cache_key.borrow_mut().take();
313 }
314 }
315}
316
317impl VirtualMethods for HTMLStyleElement {
318 fn super_type(&self) -> Option<&dyn VirtualMethods> {
319 Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
320 }
321
322 fn children_changed(&self, cx: &mut JSContext, mutation: &ChildrenMutation) {
323 self.super_type().unwrap().children_changed(cx, mutation);
324
325 if !self.in_stack_of_open_elements.get() {
328 self.update_a_style_block(cx);
329 }
330 }
331
332 fn bind_to_tree(&self, cx: &mut JSContext, context: &BindContext) {
333 self.super_type().unwrap().bind_to_tree(cx, context);
334
335 if !self.in_stack_of_open_elements.get() {
338 self.update_a_style_block(cx);
339 }
340 }
341
342 fn pop(&self, cx: &mut js::context::JSContext) {
343 self.super_type().unwrap().pop(cx);
344 self.in_stack_of_open_elements.set(false);
345
346 self.update_a_style_block(cx);
349 }
350
351 fn unbind_from_tree(&self, cx: &mut js::context::JSContext, context: &UnbindContext) {
352 if let Some(s) = self.super_type() {
353 s.unbind_from_tree(cx, context);
354 }
355
356 if !self.in_stack_of_open_elements.get() {
359 self.update_a_style_block(cx);
360 }
361 }
362
363 fn attribute_mutated(
364 &self,
365 cx: &mut js::context::JSContext,
366 attr: AttrRef<'_>,
367 mutation: AttributeMutation,
368 ) {
369 if let Some(s) = self.super_type() {
370 s.attribute_mutated(cx, attr, mutation);
371 }
372
373 let node = self.upcast::<Node>();
374 if !(node.is_in_a_document_tree() || node.is_in_a_shadow_tree()) ||
375 self.in_stack_of_open_elements.get()
376 {
377 return;
378 }
379
380 if attr.name() == "type" {
381 if let AttributeMutation::Set(Some(old_value), _) = mutation &&
382 **old_value == **attr.value()
383 {
384 return;
385 }
386 self.remove_stylesheet();
387 self.update_a_style_block(cx);
388 } else if attr.name() == "media" &&
389 let Some(ref stylesheet) = *self.stylesheet.borrow_mut()
390 {
391 let shared_lock = node.owner_doc().style_shared_author_lock().clone();
392 let mut guard = shared_lock.write();
393 let media = stylesheet.media.write_with(&mut guard);
394 match mutation {
395 AttributeMutation::Set(..) => *media = self.create_media_list(&attr.value()),
396 AttributeMutation::Removed => *media = StyleMediaList::empty(),
397 };
398 self.owner_document().invalidate_stylesheets();
399 }
400 }
401}
402
403impl StylesheetOwner for HTMLStyleElement {
404 fn increment_pending_loads_count(&self) {
405 self.pending_loads.set(self.pending_loads.get() + 1)
406 }
407
408 fn load_finished(&self, succeeded: bool) -> Option<bool> {
409 assert!(self.pending_loads.get() > 0, "What finished?");
410 if !succeeded {
411 self.any_failed_load.set(true);
412 }
413
414 self.pending_loads.set(self.pending_loads.get() - 1);
415 if self.pending_loads.get() != 0 {
416 return None;
417 }
418
419 let any_failed = self.any_failed_load.get();
420 self.any_failed_load.set(false);
421 Some(any_failed)
422 }
423
424 fn parser_inserted(&self) -> bool {
425 self.parser_inserted.get()
426 }
427
428 fn potentially_render_blocking(&self) -> bool {
430 self.parser_inserted() ||
437 self.blocking
438 .get()
439 .is_some_and(|list| list.Contains("render".into()))
440 }
441
442 fn referrer_policy(&self, _cx: &mut js::context::JSContext) -> ReferrerPolicy {
443 ReferrerPolicy::EmptyString
444 }
445
446 fn set_origin_clean(&self, origin_clean: bool) {
447 if let Some(stylesheet) = self.get_cssom_stylesheet() {
448 stylesheet.set_origin_clean(origin_clean);
449 }
450 }
451}
452
453impl HTMLStyleElementMethods<crate::DomTypeHolder> for HTMLStyleElement {
454 fn GetSheet(&self) -> Option<DomRoot<DOMStyleSheet>> {
456 self.get_cssom_stylesheet().map(DomRoot::upcast)
457 }
458
459 fn Disabled(&self) -> bool {
461 self.get_cssom_stylesheet()
462 .is_some_and(|sheet| sheet.disabled())
463 }
464
465 fn SetDisabled(&self, _cx: &mut js::context::JSContext, value: bool) {
467 if let Some(sheet) = self.get_cssom_stylesheet() {
468 sheet.set_disabled(value);
469 }
470 }
471
472 make_getter!(Type, "type");
474
475 make_setter!(SetType, "type");
477
478 make_getter!(Media, "media");
480
481 make_setter!(SetMedia, "media");
483
484 fn Blocking(&self, cx: &mut js::context::JSContext) -> DomRoot<DOMTokenList> {
486 self.blocking.or_init(|| {
487 DOMTokenList::new(
488 cx,
489 self.upcast(),
490 &local_name!("blocking"),
491 Some(vec![Atom::from("render")]),
492 )
493 })
494 }
495}