1use std::cell::{Cell, Ref, RefCell};
6
7use dom_struct::dom_struct;
8use html5ever::{LocalName, Prefix, local_name, ns};
9use js::gc::RootedVec;
10use js::rust::HandleObject;
11use script_bindings::codegen::InheritTypes::{CharacterDataTypeId, NodeTypeId};
12
13use crate::ScriptThread;
14use crate::dom::attr::Attr;
15use crate::dom::bindings::codegen::Bindings::HTMLSlotElementBinding::{
16 AssignedNodesOptions, HTMLSlotElementMethods,
17};
18use crate::dom::bindings::codegen::Bindings::NodeBinding::{GetRootNodeOptions, NodeMethods};
19use crate::dom::bindings::codegen::Bindings::ShadowRootBinding::ShadowRoot_Binding::ShadowRootMethods;
20use crate::dom::bindings::codegen::Bindings::ShadowRootBinding::{
21 ShadowRootMode, SlotAssignmentMode,
22};
23use crate::dom::bindings::codegen::UnionTypes::ElementOrText;
24use crate::dom::bindings::inheritance::Castable;
25use crate::dom::bindings::root::{Dom, DomRoot};
26use crate::dom::bindings::str::DOMString;
27use crate::dom::document::Document;
28use crate::dom::element::{AttributeMutation, Element};
29use crate::dom::globalscope::GlobalScope;
30use crate::dom::html::htmlelement::HTMLElement;
31use crate::dom::node::{
32 BindContext, IsShadowTree, Node, NodeDamage, NodeTraits, ShadowIncluding, UnbindContext,
33};
34use crate::dom::virtualmethods::VirtualMethods;
35use crate::script_runtime::CanGc;
36
37#[dom_struct]
39pub(crate) struct HTMLSlotElement {
40 htmlelement: HTMLElement,
41
42 assigned_nodes: RefCell<Vec<Slottable>>,
44
45 manually_assigned_nodes: RefCell<Vec<Slottable>>,
47
48 is_in_agents_signal_slots: Cell<bool>,
52}
53
54impl HTMLSlotElementMethods<crate::DomTypeHolder> for HTMLSlotElement {
55 make_getter!(Name, "name");
57
58 make_atomic_setter!(SetName, "name");
60
61 fn AssignedNodes(&self, options: &AssignedNodesOptions) -> Vec<DomRoot<Node>> {
63 if !options.flatten {
65 return self
66 .assigned_nodes
67 .borrow()
68 .iter()
69 .map(|slottable| slottable.node())
70 .map(DomRoot::from_ref)
71 .collect();
72 }
73
74 rooted_vec!(let mut flattened_slottables);
76 self.find_flattened_slottables(&mut flattened_slottables);
77
78 flattened_slottables
79 .iter()
80 .map(|slottable| DomRoot::from_ref(slottable.node()))
81 .collect()
82 }
83
84 fn AssignedElements(&self, options: &AssignedNodesOptions) -> Vec<DomRoot<Element>> {
86 self.AssignedNodes(options)
87 .into_iter()
88 .flat_map(|node| node.downcast::<Element>().map(DomRoot::from_ref))
89 .collect()
90 }
91
92 fn Assign(&self, nodes: Vec<ElementOrText>) {
94 let cx = GlobalScope::get_cx();
95
96 for slottable in self.manually_assigned_nodes.borrow().iter() {
98 slottable.set_manual_slot_assignment(None);
99 }
100
101 rooted_vec!(let mut nodes_set);
103
104 for element_or_text in nodes.into_iter() {
106 rooted!(in(*cx) let node = match element_or_text {
107 ElementOrText::Element(element) => Slottable(Dom::from_ref(element.upcast())),
108 ElementOrText::Text(text) => Slottable(Dom::from_ref(text.upcast())),
109 });
110
111 if let Some(slot) = node.manual_slot_assignment() {
114 let mut manually_assigned_nodes = slot.manually_assigned_nodes.borrow_mut();
115 if let Some(position) = manually_assigned_nodes
116 .iter()
117 .position(|value| *value == *node)
118 {
119 manually_assigned_nodes.remove(position);
120 }
121 }
122
123 node.set_manual_slot_assignment(Some(self));
125
126 if !nodes_set.contains(&*node) {
128 nodes_set.push(node.clone());
129 }
130 }
131
132 *self.manually_assigned_nodes.borrow_mut() = nodes_set.iter().cloned().collect();
134
135 self.upcast::<Node>()
137 .GetRootNode(&GetRootNodeOptions::empty())
138 .assign_slottables_for_a_tree();
139 }
140}
141
142#[derive(Clone, JSTraceable, MallocSizeOf, PartialEq)]
151#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
152#[repr(transparent)]
153pub(crate) struct Slottable(pub Dom<Node>);
154#[derive(Default, JSTraceable, MallocSizeOf)]
160#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
161pub struct SlottableData {
162 pub(crate) assigned_slot: Option<Dom<HTMLSlotElement>>,
164
165 pub(crate) manual_slot_assignment: Option<Dom<HTMLSlotElement>>,
167}
168
169impl HTMLSlotElement {
170 fn new_inherited(
171 local_name: LocalName,
172 prefix: Option<Prefix>,
173 document: &Document,
174 ) -> HTMLSlotElement {
175 HTMLSlotElement {
176 htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
177 assigned_nodes: Default::default(),
178 manually_assigned_nodes: Default::default(),
179 is_in_agents_signal_slots: Default::default(),
180 }
181 }
182
183 pub(crate) fn new(
184 local_name: LocalName,
185 prefix: Option<Prefix>,
186 document: &Document,
187 proto: Option<HandleObject>,
188 can_gc: CanGc,
189 ) -> DomRoot<HTMLSlotElement> {
190 Node::reflect_node_with_proto(
191 Box::new(HTMLSlotElement::new_inherited(local_name, prefix, document)),
192 document,
193 proto,
194 can_gc,
195 )
196 }
197
198 pub(crate) fn has_assigned_nodes(&self) -> bool {
199 !self.assigned_nodes.borrow().is_empty()
200 }
201
202 fn find_flattened_slottables(&self, result: &mut RootedVec<Slottable>) {
204 debug_assert!(result.is_empty());
206
207 if self.upcast::<Node>().containing_shadow_root().is_none() {
209 return;
210 };
211
212 rooted_vec!(let mut slottables);
214 self.find_slottables(&mut slottables);
215
216 if slottables.is_empty() {
219 for child in self.upcast::<Node>().children() {
220 let is_slottable = matches!(
221 child.type_id(),
222 NodeTypeId::Element(_) |
223 NodeTypeId::CharacterData(CharacterDataTypeId::Text(_))
224 );
225 if is_slottable {
226 slottables.push(Slottable(child.as_traced()));
227 }
228 }
229 }
230
231 for slottable in slottables.iter() {
233 match slottable.0.downcast::<HTMLSlotElement>() {
235 Some(slot_element)
236 if slot_element
237 .upcast::<Node>()
238 .containing_shadow_root()
239 .is_some() =>
240 {
241 rooted_vec!(let mut temporary_result);
243 slot_element.find_flattened_slottables(&mut temporary_result);
244
245 result.extend_from_slice(&temporary_result);
247 },
248 _ => {
250 result.push(slottable.clone());
251 },
252 };
253 }
254
255 }
257
258 fn find_slottables(&self, result: &mut RootedVec<Slottable>) {
263 let cx = GlobalScope::get_cx();
264
265 debug_assert!(result.is_empty());
267
268 let Some(root) = self.upcast::<Node>().containing_shadow_root() else {
271 return;
272 };
273
274 let host = root.Host();
276
277 if root.SlotAssignment() == SlotAssignmentMode::Manual {
279 for slottable in self.manually_assigned_nodes.borrow().iter() {
285 if slottable
286 .node()
287 .GetParentNode()
288 .is_some_and(|node| &*node == host.upcast::<Node>())
289 {
290 result.push(slottable.clone());
291 }
292 }
293 }
294 else {
296 for child in host.upcast::<Node>().children() {
297 let is_slottable = matches!(
298 child.type_id(),
299 NodeTypeId::Element(_) |
300 NodeTypeId::CharacterData(CharacterDataTypeId::Text(_))
301 );
302 if is_slottable {
303 rooted!(in(*cx) let slottable = Slottable(child.as_traced()));
304 let found_slot = slottable.find_a_slot(false);
306
307 if found_slot.is_some_and(|found_slot| &*found_slot == self) {
309 result.push(slottable.clone());
310 }
311 }
312 }
313 }
314
315 }
317
318 pub(crate) fn assign_slottables(&self) {
320 rooted_vec!(let mut slottables);
322 self.find_slottables(&mut slottables);
323
324 if self.assigned_nodes.borrow().iter().eq(slottables.iter()) {
329 return;
330 }
331 self.signal_a_slot_change();
332
333 for slottable in self.assigned_nodes().iter() {
337 slottable.set_assigned_slot(None);
338 }
339
340 *self.assigned_nodes.borrow_mut() = slottables.iter().cloned().collect();
342
343 for slottable in slottables.iter() {
345 slottable.set_assigned_slot(Some(self));
346 }
347 }
348
349 pub(crate) fn signal_a_slot_change(&self) {
351 self.upcast::<Node>().dirty(NodeDamage::ContentOrHeritage);
352
353 if self.is_in_agents_signal_slots.get() {
354 return;
355 }
356 self.is_in_agents_signal_slots.set(true);
357
358 let mutation_observers = ScriptThread::mutation_observers();
359 mutation_observers.add_signal_slot(self);
361
362 mutation_observers.queue_mutation_observer_microtask(ScriptThread::microtask_queue());
364 }
365
366 pub(crate) fn remove_from_signal_slots(&self) {
367 debug_assert!(self.is_in_agents_signal_slots.get());
368 self.is_in_agents_signal_slots.set(false);
369 }
370
371 pub(crate) fn assigned_nodes(&self) -> Ref<'_, [Slottable]> {
374 Ref::map(self.assigned_nodes.borrow(), Vec::as_slice)
375 }
376}
377
378impl Slottable {
379 pub(crate) fn find_a_slot(&self, open_flag: bool) -> Option<DomRoot<HTMLSlotElement>> {
381 let parent = self.node().GetParentNode()?;
383
384 let shadow_root = parent
387 .downcast::<Element>()
388 .and_then(Element::shadow_root)?;
389
390 if open_flag && shadow_root.Mode() != ShadowRootMode::Open {
392 return None;
393 }
394
395 if shadow_root.SlotAssignment() == SlotAssignmentMode::Manual {
398 for node in shadow_root
399 .upcast::<Node>()
400 .traverse_preorder(ShadowIncluding::No)
401 {
402 if let Some(slot) = node.downcast::<HTMLSlotElement>() {
403 if slot.manually_assigned_nodes.borrow().contains(self) {
404 return Some(DomRoot::from_ref(slot));
405 }
406 }
407 }
408 return None;
409 }
410
411 shadow_root.slot_for_name(&self.name())
414 }
415
416 pub(crate) fn assign_a_slot(&self) {
418 let slot = self.find_a_slot(false);
420
421 if let Some(slot) = slot {
423 slot.assign_slottables();
424 }
425 }
426
427 fn node(&self) -> &Node {
428 &self.0
429 }
430
431 pub(crate) fn assigned_slot(&self) -> Option<DomRoot<HTMLSlotElement>> {
432 self.node().assigned_slot()
433 }
434
435 pub(crate) fn set_assigned_slot(&self, assigned_slot: Option<&HTMLSlotElement>) {
436 self.node().set_assigned_slot(assigned_slot);
437 }
438
439 pub(crate) fn set_manual_slot_assignment(
440 &self,
441 manually_assigned_slot: Option<&HTMLSlotElement>,
442 ) {
443 self.node()
444 .set_manual_slot_assignment(manually_assigned_slot);
445 }
446
447 pub(crate) fn manual_slot_assignment(&self) -> Option<DomRoot<HTMLSlotElement>> {
448 self.node().manual_slot_assignment()
449 }
450
451 fn name(&self) -> DOMString {
452 let Some(element) = self.0.downcast::<Element>() else {
454 return DOMString::new();
455 };
456
457 element.get_string_attribute(&local_name!("slot"))
458 }
459}
460
461impl VirtualMethods for HTMLSlotElement {
462 fn super_type(&self) -> Option<&dyn VirtualMethods> {
463 Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
464 }
465
466 fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation, can_gc: CanGc) {
468 self.super_type()
469 .unwrap()
470 .attribute_mutated(attr, mutation, can_gc);
471
472 if attr.local_name() == &local_name!("name") && attr.namespace() == &ns!() {
473 if let Some(shadow_root) = self.containing_shadow_root() {
474 let old_value = match mutation {
477 AttributeMutation::Set(old, _) => old
478 .map(|value| value.to_string().into())
479 .unwrap_or_default(),
480 AttributeMutation::Removed => attr.value().to_string().into(),
481 };
482
483 shadow_root.unregister_slot(old_value, self);
484 shadow_root.register_slot(self);
485 }
486
487 self.upcast::<Node>()
489 .GetRootNode(&GetRootNodeOptions::empty())
490 .assign_slottables_for_a_tree()
491 }
492 }
493
494 fn bind_to_tree(&self, context: &BindContext, can_gc: CanGc) {
495 if let Some(s) = self.super_type() {
496 s.bind_to_tree(context, can_gc);
497 }
498
499 let was_already_in_shadow_tree = context.is_shadow_tree == IsShadowTree::Yes;
500 if !was_already_in_shadow_tree {
501 if let Some(shadow_root) = self.containing_shadow_root() {
502 shadow_root.register_slot(self);
503 }
504 }
505 }
506
507 fn unbind_from_tree(&self, context: &UnbindContext, can_gc: CanGc) {
508 if let Some(s) = self.super_type() {
509 s.unbind_from_tree(context, can_gc);
510 }
511
512 if !self.upcast::<Node>().is_in_a_shadow_tree() {
513 if let Some(old_shadow_root) = self.containing_shadow_root() {
514 old_shadow_root.unregister_slot(self.Name(), self);
516 }
517 }
518 }
519}
520
521impl js::gc::Rootable for Slottable {}
522
523impl js::gc::Initialize for Slottable {
524 #[expect(unsafe_code)]
525 #[cfg_attr(crown, expect(crown::unrooted_must_root))]
526 unsafe fn initial() -> Option<Self> {
527 None
528 }
529}