servo-script 0.7.0

A component of the servo web-engine.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use std::cell::Ref;
use std::ops::Deref;

use devtools_traits::AttrInfo;
use html5ever::{LocalName, Namespace, Prefix, ns};
use js::context::JSContext;
use script_bindings::cell::DomRefCell;
use script_bindings::root::{Dom, DomRoot};
use script_bindings::str::DOMString;
use style::attr::{AttrIdentifier, AttrValue};
use style::values::GenericAtomIdent;

use crate::dom::attr::Attr;
use crate::dom::bindings::root::{LayoutDom, ToLayout};
use crate::dom::element::Element;
use crate::dom::node::node::NodeTraits;

/// The empty namespace, so that [`AttrName::namespace`] can hand out a reference
/// for unqualified attributes without storing one per attribute.
static EMPTY_NAMESPACE: Namespace = ns!();

/// The name of a content attribute.
#[derive(Clone, MallocSizeOf)]
pub(crate) enum AttrName {
    /// Common case: `name == local_name`, empty namespace, no prefix.
    Unqualified(LocalName),
    /// Namespaced and/or prefixed.
    Qualified(Box<AttrIdentifier>),
}

impl AttrName {
    /// Build a name from the four parts of an [`AttrIdentifier`], picking the
    /// compact representation when they allow it.
    pub(crate) fn new(
        local_name: LocalName,
        name: LocalName,
        namespace: Namespace,
        prefix: Option<Prefix>,
    ) -> Self {
        if prefix.is_none() && name == local_name && namespace == ns!() {
            AttrName::Unqualified(local_name)
        } else {
            AttrName::Qualified(Box::new(AttrIdentifier {
                local_name: GenericAtomIdent(local_name),
                name: GenericAtomIdent(name),
                namespace: GenericAtomIdent(namespace),
                prefix: prefix.map(GenericAtomIdent),
            }))
        }
    }

    #[inline]
    pub(crate) fn local_name(&self) -> &LocalName {
        match self {
            AttrName::Unqualified(local_name) => local_name,
            AttrName::Qualified(identifier) => &identifier.local_name.0,
        }
    }

    #[inline]
    pub(crate) fn name(&self) -> &LocalName {
        match self {
            AttrName::Unqualified(local_name) => local_name,
            AttrName::Qualified(identifier) => &identifier.name.0,
        }
    }

    #[inline]
    pub(crate) fn namespace(&self) -> &Namespace {
        match self {
            AttrName::Unqualified(_) => &EMPTY_NAMESPACE,
            AttrName::Qualified(identifier) => &identifier.namespace.0,
        }
    }

    #[inline]
    pub(crate) fn prefix(&self) -> Option<&Prefix> {
        match self {
            AttrName::Unqualified(_) => None,
            AttrName::Qualified(identifier) => Some(&identifier.prefix.as_ref()?.0),
        }
    }

    /// Decompose into the parts `Attr::new` takes.
    pub(crate) fn into_parts(self) -> (LocalName, LocalName, Namespace, Option<Prefix>) {
        match self {
            AttrName::Unqualified(local_name) => (local_name.clone(), local_name, ns!(), None),
            AttrName::Qualified(identifier) => {
                let AttrIdentifier {
                    local_name,
                    name,
                    namespace,
                    prefix,
                } = *identifier;
                (local_name.0, name.0, namespace.0, prefix.map(|p| p.0))
            },
        }
    }

    /// Materialize a stylo [`AttrIdentifier`].
    pub(crate) fn as_identifier(&self) -> AttrIdentifier {
        match self {
            AttrName::Unqualified(local_name) => AttrIdentifier {
                local_name: GenericAtomIdent(local_name.clone()),
                name: GenericAtomIdent(local_name.clone()),
                namespace: GenericAtomIdent(ns!()),
                prefix: None,
            },
            AttrName::Qualified(identifier) => (**identifier).clone(),
        }
    }
}

/// Lightweight attribute storage that avoids allocating a full DOM `Attr` node.
#[derive(MallocSizeOf)]
pub(crate) struct ContentAttributeData {
    pub identifier: AttrName,
    pub value: AttrValue,
}

impl ContentAttributeData {
    #[inline]
    pub(crate) fn local_name(&self) -> &LocalName {
        self.identifier.local_name()
    }

    #[inline]
    pub(crate) fn name(&self) -> &LocalName {
        self.identifier.name()
    }

    #[inline]
    pub(crate) fn namespace(&self) -> &Namespace {
        self.identifier.namespace()
    }

    #[inline]
    pub(crate) fn prefix(&self) -> Option<&Prefix> {
        self.identifier.prefix()
    }

    #[inline]
    pub(crate) fn value(&self) -> &AttrValue {
        &self.value
    }
}

/// A reference to an attribute value, abstracting over direct and RefCell-borrowed access.
pub(crate) enum AttrValueRef<'a> {
    /// Direct reference to a value (from [`ContentAttributeData`]).
    Direct(&'a AttrValue),
    /// Borrowed from a [`DomRefCell`] (from [`Attr`]).
    Borrowed(Ref<'a, AttrValue>),
}

impl Deref for AttrValueRef<'_> {
    type Target = AttrValue;

    fn deref(&self) -> &AttrValue {
        match self {
            AttrValueRef::Direct(value) => value,
            AttrValueRef::Borrowed(value) => value,
        }
    }
}

impl AsRef<str> for AttrValueRef<'_> {
    fn as_ref(&self) -> &str {
        self.deref()
    }
}

/// A reference to attribute data, either from a lightweight [`ContentAttributeData`]
/// or from a full [`Attr`] DOM node. Provides the same accessor interface regardless
/// of storage form.
#[derive(Clone, Copy)]
pub(crate) enum AttrRef<'a> {
    /// Lightweight data (no DOM node allocated).
    Raw(&'a ContentAttributeData),
    /// Full Attr DOM node.
    Dom(&'a Attr),
}

impl<'a> AttrRef<'a> {
    #[inline]
    pub(crate) fn local_name(&self) -> &'a LocalName {
        match self {
            AttrRef::Raw(data) => data.local_name(),
            AttrRef::Dom(attr) => attr.local_name(),
        }
    }

    #[inline]
    pub(crate) fn name(&self) -> &'a LocalName {
        match self {
            AttrRef::Raw(data) => data.name(),
            AttrRef::Dom(attr) => attr.name(),
        }
    }

    #[inline]
    pub(crate) fn namespace(&self) -> &'a Namespace {
        match self {
            AttrRef::Raw(data) => data.namespace(),
            AttrRef::Dom(attr) => attr.namespace(),
        }
    }

    #[inline]
    pub(crate) fn prefix(&self) -> Option<&'a Prefix> {
        match self {
            AttrRef::Raw(data) => data.prefix(),
            AttrRef::Dom(attr) => attr.prefix(),
        }
    }

    #[inline]
    pub(crate) fn value(&self) -> AttrValueRef<'a> {
        match self {
            AttrRef::Raw(data) => AttrValueRef::Direct(data.value()),
            AttrRef::Dom(attr) => AttrValueRef::Borrowed(attr.value()),
        }
    }

    /// Returns the underlying `&Attr` if this is a `Dom` reference.
    /// Returns `None` for `Raw` data (no DOM node exists).
    #[inline]
    pub(crate) fn as_attr(&self) -> Option<&'a Attr> {
        match self {
            AttrRef::Dom(attr) => Some(attr),
            AttrRef::Raw(_) => None,
        }
    }

    /// Returns the attribute value as a `DOMString`, equivalent to `Attr::Value()`.
    pub(crate) fn to_dom_string(self) -> DOMString {
        DOMString::from(&**self.value())
    }

    /// Returns a summary for devtools.
    pub(crate) fn summarize(&self) -> AttrInfo {
        AttrInfo {
            namespace: (**self.namespace()).to_owned(),
            name: (**self.name()).to_owned(),
            value: (**self.value()).to_owned(),
        }
    }

    /// Materializes the stylo `AttrIdentifier` for this attribute.
    pub(crate) fn as_identifier(&self) -> AttrIdentifier {
        match self {
            AttrRef::Raw(data) => data.identifier.as_identifier(),
            AttrRef::Dom(attr) => attr.identifier().clone(),
        }
    }
}

/// A single attribute entry, either lightweight raw data or a full DOM Attr node.
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
#[derive(MallocSizeOf)]
pub(crate) enum AttributeEntry {
    /// Lightweight data — no Attr DOM node allocated.
    Raw(ContentAttributeData),
    /// Full Attr DOM node.
    Dom(Dom<Attr>),
}

impl AttributeEntry {
    /// Get an `AttrRef` for this entry.
    #[inline]
    pub(crate) fn as_ref(&self) -> AttrRef<'_> {
        match self {
            AttributeEntry::Raw(data) => AttrRef::Raw(data),
            AttributeEntry::Dom(attr) => AttrRef::Dom(attr),
        }
    }

    /// Get the value of this attribute for layout.
    #[expect(unsafe_code)]
    #[inline]
    pub(crate) fn value_for_layout(&self) -> &AttrValue {
        match self {
            AttributeEntry::Raw(data) => &data.value,
            AttributeEntry::Dom(attr) => unsafe {
                let value: LayoutDom<'_, _> = attr.to_layout();
                value
            }
            .value(),
        }
    }

    /// Get the local name of this attribute for layout.
    #[expect(unsafe_code)]
    #[inline]
    pub(crate) fn local_name_for_layout(&self) -> &LocalName {
        match self {
            AttributeEntry::Raw(data) => data.local_name(),
            AttributeEntry::Dom(attr) => unsafe {
                let value: LayoutDom<'_, _> = attr.to_layout();
                value
            }
            .local_name(),
        }
    }

    /// Get the namespace of this attribute for layout.
    #[expect(unsafe_code)]
    #[inline]
    pub(crate) fn namespace_for_layout(&self) -> &Namespace {
        match self {
            AttributeEntry::Raw(data) => data.namespace(),
            AttributeEntry::Dom(attr) => unsafe {
                let value: LayoutDom<'_, _> = attr.to_layout();
                value
            }
            .namespace(),
        }
    }
}

/// Storage for an element's attributes. Contains an internal `DomRefCell` so that
/// `ensure_dom` can split its borrow around `Attr::new()` allocation, preventing
/// GC-during-allocation panics from double-borrowing.
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
#[derive(Default, MallocSizeOf)]
pub(crate) struct AttributeStorage(DomRefCell<Vec<AttributeEntry>>);

/// A borrowed view of attribute storage that provides convenient access
/// to attributes as `AttrRef` items.
pub(crate) struct AttributesBorrow<'a>(Ref<'a, Vec<AttributeEntry>>);

impl<'a> AttributesBorrow<'a> {
    /// Iterate over attributes as `AttrRef`.
    #[inline]
    pub(crate) fn iter(&self) -> impl Iterator<Item = AttrRef<'_>> + '_ {
        self.0.iter().map(AttributeEntry::as_ref)
    }

    #[inline]
    pub(crate) fn len(&self) -> usize {
        self.0.len()
    }

    #[inline]
    pub(crate) fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    #[inline]
    pub(crate) fn first(&self) -> Option<AttrRef<'_>> {
        self.0.first().map(AttributeEntry::as_ref)
    }

    #[inline]
    pub(crate) fn get(&self, index: usize) -> Option<AttrRef<'_>> {
        self.0.get(index).map(AttributeEntry::as_ref)
    }
}

impl AttributeStorage {
    /// Borrow the attributes for read access with convenient `AttrRef` iteration.
    #[inline]
    pub(crate) fn borrow(&self) -> AttributesBorrow<'_> {
        AttributesBorrow(self.0.borrow())
    }

    /// Borrow the underlying entries for layout access (unsafe).
    #[expect(unsafe_code)]
    #[inline]
    pub(crate) unsafe fn borrow_for_layout(&self) -> &Vec<AttributeEntry> {
        unsafe { self.0.borrow_for_layout() }
    }

    /// Reserve room for at least `additional` more attributes.
    #[inline]
    pub(crate) fn reserve_exact(&self, additional: usize) {
        self.0.borrow_mut().reserve_exact(additional);
    }

    /// Push raw attribute data.
    pub(crate) fn push_raw(&self, data: ContentAttributeData) {
        self.0.borrow_mut().push(AttributeEntry::Raw(data));
    }

    /// Push a Dom Attr node.
    pub(crate) fn push_dom(&self, attr: &Attr) {
        self.0
            .borrow_mut()
            .push(AttributeEntry::Dom(Dom::from_ref(attr)));
    }

    /// Remove an attribute by index, returning the entry.
    pub(crate) fn remove(&self, index: usize) -> AttributeEntry {
        self.0.borrow_mut().remove(index)
    }

    /// Set an attribute entry by index.
    #[cfg_attr(crown, expect(crown::unrooted_must_root))]
    pub(crate) fn set(&self, index: usize, entry: AttributeEntry) {
        self.0.borrow_mut()[index] = entry;
    }

    /// Ensure entry at index is a Dom Attr node, materializing if needed.
    /// Returns a `DomRoot<Attr>`.
    ///
    /// This method carefully splits the mutable borrow around the `Attr::new()`
    /// allocation so that GC tracing can safely borrow the storage.
    #[cfg_attr(crown, expect(crown::unrooted_must_root))]
    pub(crate) fn ensure_dom(
        &self,
        cx: &mut JSContext,
        index: usize,
        element: &Element,
    ) -> DomRoot<Attr> {
        // Fast path: already materialized.
        if let AttributeEntry::Dom(attr) = &self.0.borrow()[index] {
            return DomRoot::from_ref(&**attr);
        }

        // Extract the raw data, dropping the mutable borrow before allocating.
        let data = {
            let mut entries = self.0.borrow_mut();
            let placeholder = AttributeEntry::Raw(ContentAttributeData {
                identifier: AttrName::Unqualified(html5ever::local_name!("")),
                value: AttrValue::String(String::new()),
            });
            let old = std::mem::replace(&mut entries[index], placeholder);
            match old {
                AttributeEntry::Raw(data) => data,
                _ => unreachable!(),
            }
        };

        let doc = element.owner_document();
        let (local_name, name, namespace, prefix) = data.identifier.into_parts();
        let attr = Attr::new(
            cx,
            &doc,
            local_name,
            data.value,
            name,
            namespace,
            prefix,
            Some(element),
        );

        self.0.borrow_mut()[index] = AttributeEntry::Dom(Dom::from_ref(&*attr));
        attr
    }
}

// Safety: Only Dom entries contain GC-traced Dom<Attr> pointers.
// Raw entries have no pointers to trace.
#[expect(unsafe_code)]
unsafe impl crate::dom::bindings::trace::JSTraceable for AttributeStorage {
    unsafe fn trace(&self, trc: *mut js::jsapi::JSTracer) {
        for entry in self.0.borrow().iter() {
            if let AttributeEntry::Dom(attr) = entry {
                unsafe { js::rust::Trace::trace(attr, trc) };
            }
        }
    }
}