lexical-ui 0.1.2

A Rust port of Lexical's model and headless packages, at parity with the JS implementation.
Documentation
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
//! `Node`, `Element`, `Text`, `Comment`, `DocumentFragment` and `Document`:
//! the part of the WHATWG DOM that Lexical's `createDOM`, `exportDOM` and
//! HTML import and export use. One handle type, [`DomNode`], stands for every
//! kind of node, as JS code reads them all through `Node`; the methods of a
//! more specific interface answer as an empty value on other nodes.

use super::css_style_declaration::{CssStyle, CssStyleDeclaration};
use super::html_serializer::{serialize_html, serialize_html_children};
use std::cell::RefCell;
use std::fmt;
use std::rc::{Rc, Weak};

/// `Node.nodeType`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DomNodeType {
    Element = 1,
    Text = 3,
    Comment = 8,
    Document = 9,
    DocumentFragment = 11,
}

/// A DOM node: a shared handle, compared by identity, as JS compares node
/// objects. A node holds its children; its parent and document are weak
/// links.
#[derive(Clone)]
pub struct DomNode(Rc<Inner>);

struct Inner {
    node_type: DomNodeType,
    owner_document: Weak<Inner>,
    parent: RefCell<Weak<Inner>>,
    children: RefCell<Vec<DomNode>>,
    /// `CharacterData.data` of a text or comment node.
    data: RefCell<String>,
    element: Option<ElementInner>,
}

/// What an element adds: its tag, its attributes in the order they were
/// set, and its inline style declaration once something reads it.
struct ElementInner {
    local_name: String,
    attributes: RefCell<Vec<(String, String)>>,
    pub(super) style: RefCell<Option<CssStyleDeclaration>>,
}

impl PartialEq for DomNode {
    fn eq(&self, other: &Self) -> bool {
        Rc::ptr_eq(&self.0, &other.0)
    }
}

impl Eq for DomNode {}

impl fmt::Debug for DomNode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.node_type() {
            DomNodeType::Element => write!(f, "{}", self.outer_html()),
            DomNodeType::Text => write!(f, "#text {:?}", self.data()),
            DomNodeType::Comment => write!(f, "<!--{}-->", self.data()),
            DomNodeType::Document | DomNodeType::DocumentFragment => {
                write!(f, "{} {}", self.node_name(), self.inner_html())
            }
        }
    }
}

impl DomNode {
    fn new(
        node_type: DomNodeType,
        owner_document: Option<&DomNode>,
        data: String,
        element: Option<ElementInner>,
    ) -> Self {
        Self(Rc::new(Inner {
            node_type,
            owner_document: owner_document.map_or_else(Weak::new, |d| Rc::downgrade(&d.0)),
            parent: RefCell::new(Weak::new()),
            children: RefCell::new(Vec::new()),
            data: RefCell::new(data),
            element,
        }))
    }

    /// `new Document()`: an empty document, which creates nodes. A parsed
    /// HTML document has `documentElement`, `head` and `body`.
    pub fn new_document() -> Self {
        Self::new(DomNodeType::Document, None, String::new(), None)
    }

    /// An element with `local_name` (already lower case), as the parser and
    /// `createElement` make one.
    pub(crate) fn new_element(local_name: String, owner_document: Option<&DomNode>) -> Self {
        Self::new(
            DomNodeType::Element,
            owner_document,
            String::new(),
            Some(ElementInner {
                local_name,
                attributes: RefCell::new(Vec::new()),
                style: RefCell::new(None),
            }),
        )
    }

    /// `new Text(data)`.
    pub fn new_text(data: impl Into<String>, owner_document: Option<&DomNode>) -> Self {
        Self::new(DomNodeType::Text, owner_document, data.into(), None)
    }

    /// `new Comment(data)`.
    pub fn new_comment(data: impl Into<String>, owner_document: Option<&DomNode>) -> Self {
        Self::new(DomNodeType::Comment, owner_document, data.into(), None)
    }

    /// `new DocumentFragment()`.
    pub fn new_document_fragment(owner_document: Option<&DomNode>) -> Self {
        Self::new(
            DomNodeType::DocumentFragment,
            owner_document,
            String::new(),
            None,
        )
    }

    /// An identity for sets of nodes.
    pub(crate) fn identity(&self) -> usize {
        Rc::as_ptr(&self.0) as usize
    }

    pub fn node_type(&self) -> DomNodeType {
        self.0.node_type
    }

    /// `nodeName`: the upper-case tag name of an element, `#text`,
    /// `#comment`, `#document` or `#document-fragment`.
    pub fn node_name(&self) -> String {
        match self.node_type() {
            DomNodeType::Element => self.tag_name(),
            DomNodeType::Text => "#text".to_owned(),
            DomNodeType::Comment => "#comment".to_owned(),
            DomNodeType::Document => "#document".to_owned(),
            DomNodeType::DocumentFragment => "#document-fragment".to_owned(),
        }
    }

    pub fn is_element(&self) -> bool {
        self.node_type() == DomNodeType::Element
    }

    pub fn is_text(&self) -> bool {
        self.node_type() == DomNodeType::Text
    }

    pub fn is_document(&self) -> bool {
        self.node_type() == DomNodeType::Document
    }

    pub fn is_document_fragment(&self) -> bool {
        self.node_type() == DomNodeType::DocumentFragment
    }

    /// `ownerDocument`: the document that created the node.
    pub fn owner_document(&self) -> Option<DomNode> {
        self.0.owner_document.upgrade().map(DomNode)
    }

    /// The document new nodes made for this one belong to: its owner, or
    /// itself when it is a document.
    fn document_for_new_nodes(&self) -> Option<DomNode> {
        if self.is_document() {
            Some(self.clone())
        } else {
            self.owner_document()
        }
    }

    pub fn parent_node(&self) -> Option<DomNode> {
        self.0.parent.borrow().upgrade().map(DomNode)
    }

    pub fn parent_element(&self) -> Option<DomNode> {
        self.parent_node().filter(DomNode::is_element)
    }

    /// `childNodes`, as a snapshot.
    pub fn child_nodes(&self) -> Vec<DomNode> {
        self.0.children.borrow().clone()
    }

    pub fn first_child(&self) -> Option<DomNode> {
        self.0.children.borrow().first().cloned()
    }

    pub fn last_child(&self) -> Option<DomNode> {
        self.0.children.borrow().last().cloned()
    }

    pub fn has_child_nodes(&self) -> bool {
        !self.0.children.borrow().is_empty()
    }

    fn index_of(&self, child: &DomNode) -> Option<usize> {
        self.0.children.borrow().iter().position(|c| c == child)
    }

    fn sibling(&self, offset: isize) -> Option<DomNode> {
        let parent = self.parent_node()?;
        let index = parent.index_of(self)?;
        let at = index.checked_add_signed(offset)?;
        parent.0.children.borrow().get(at).cloned()
    }

    pub fn next_sibling(&self) -> Option<DomNode> {
        self.sibling(1)
    }

    pub fn previous_sibling(&self) -> Option<DomNode> {
        self.sibling(-1)
    }

    /// `previousElementSibling`: the nearest element before this node.
    pub fn previous_element_sibling(&self) -> Option<DomNode> {
        let parent = self.parent_node()?;
        let index = parent.index_of(self)?;
        parent.0.children.borrow()[..index]
            .iter()
            .rev()
            .find(|n| n.is_element())
            .cloned()
    }

    /// `nextElementSibling`: the nearest element after this node.
    pub fn next_element_sibling(&self) -> Option<DomNode> {
        let parent = self.parent_node()?;
        let index = parent.index_of(self)?;
        parent.0.children.borrow()[index + 1..]
            .iter()
            .find(|n| n.is_element())
            .cloned()
    }

    /// `children`: the element children.
    pub fn children(&self) -> Vec<DomNode> {
        self.0
            .children
            .borrow()
            .iter()
            .filter(|n| n.is_element())
            .cloned()
            .collect()
    }

    /// `textContent`: a text or comment node's data, or the text of the
    /// descendants.
    pub fn text_content(&self) -> String {
        match self.node_type() {
            DomNodeType::Text | DomNodeType::Comment => self.data(),
            _ => {
                let mut out = String::new();
                self.collect_text(&mut out);
                out
            }
        }
    }

    fn collect_text(&self, out: &mut String) {
        for child in self.0.children.borrow().iter() {
            match child.node_type() {
                DomNodeType::Text => out.push_str(&child.0.data.borrow()),
                DomNodeType::Comment => {}
                _ => child.collect_text(out),
            }
        }
    }

    /// Setting `textContent`: a text or comment node's data, or the
    /// children replaced by one text node (none for empty text).
    pub fn set_text_content(&self, text: &str) {
        match self.node_type() {
            DomNodeType::Text | DomNodeType::Comment => self.set_data(text),
            _ => {
                let nodes = if text.is_empty() {
                    Vec::new()
                } else {
                    vec![DomNode::new_text(
                        text,
                        self.document_for_new_nodes().as_ref(),
                    )]
                };
                self.replace_children(&nodes);
            }
        }
    }

    /// `CharacterData.data` (and `nodeValue`) of a text or comment node.
    pub fn data(&self) -> String {
        self.0.data.borrow().clone()
    }

    pub fn set_data(&self, data: &str) {
        *self.0.data.borrow_mut() = data.to_owned();
    }

    /// Appends to a text node's data, as the parser does.
    pub(crate) fn push_data(&self, data: &str) {
        self.0.data.borrow_mut().push_str(data);
    }

    /// The nodes `node` stands for when inserted: a fragment's children.
    fn expand(node: &DomNode) -> Vec<DomNode> {
        if node.is_document_fragment() {
            node.child_nodes()
        } else {
            vec![node.clone()]
        }
    }

    fn detach(&self, child: &DomNode) {
        if let Some(index) = self.index_of(child) {
            self.0.children.borrow_mut().remove(index);
            *child.0.parent.borrow_mut() = Weak::new();
        }
    }

    /// `insertBefore(node, child)`: moves `node` (a fragment's children) in
    /// front of `child`, or to the end when `child` is `None`.
    pub fn insert_before(&self, node: &DomNode, child: Option<&DomNode>) -> DomNode {
        let nodes = Self::expand(node);
        for n in &nodes {
            if let Some(parent) = n.parent_node() {
                parent.detach(n);
            }
        }
        let index = child
            .and_then(|c| self.index_of(c))
            .unwrap_or_else(|| self.0.children.borrow().len());
        for n in &nodes {
            *n.0.parent.borrow_mut() = Rc::downgrade(&self.0);
        }
        self.0.children.borrow_mut().splice(index..index, nodes);
        node.clone()
    }

    /// `appendChild(node)`.
    pub fn append_child(&self, node: &DomNode) -> DomNode {
        self.insert_before(node, None)
    }

    /// `append(...nodes)`.
    pub fn append(&self, nodes: &[DomNode]) {
        for node in nodes {
            self.append_child(node);
        }
    }

    /// `append(text)`.
    pub fn append_text(&self, text: &str) {
        self.append_child(&DomNode::new_text(
            text,
            self.document_for_new_nodes().as_ref(),
        ));
    }

    /// `prepend(...nodes)`.
    pub fn prepend(&self, nodes: &[DomNode]) {
        let first = self.first_child();
        for node in nodes {
            self.insert_before(node, first.as_ref());
        }
    }

    /// `removeChild(child)`.
    pub fn remove_child(&self, child: &DomNode) -> DomNode {
        self.detach(child);
        child.clone()
    }

    /// `replaceChild(node, child)`.
    pub fn replace_child(&self, node: &DomNode, child: &DomNode) -> DomNode {
        let next = child.next_sibling();
        self.detach(child);
        self.insert_before(node, next.as_ref());
        child.clone()
    }

    /// `remove()`.
    pub fn remove(&self) {
        if let Some(parent) = self.parent_node() {
            parent.detach(self);
        }
    }

    /// `replaceWith(...nodes)`.
    pub fn replace_with(&self, nodes: &[DomNode]) {
        let Some(parent) = self.parent_node() else {
            return;
        };
        let next = self.next_sibling();
        parent.detach(self);
        for node in nodes {
            parent.insert_before(node, next.as_ref());
        }
    }

    /// `replaceChildren(...nodes)`.
    pub fn replace_children(&self, nodes: &[DomNode]) {
        let old = std::mem::take(&mut *self.0.children.borrow_mut());
        for child in old {
            *child.0.parent.borrow_mut() = Weak::new();
        }
        self.append(nodes);
    }

    /// `contains(other)`: whether `other` is this node or a descendant.
    pub fn contains(&self, other: Option<&DomNode>) -> bool {
        let mut node = other.cloned();
        while let Some(current) = node {
            if &current == self {
                return true;
            }
            node = current.parent_node();
        }
        false
    }

    /// `cloneNode(deep)`.
    pub fn clone_node(&self, deep: bool) -> DomNode {
        let document = self.owner_document();
        let copy = match self.node_type() {
            DomNodeType::Element => {
                let copy = DomNode::new_element(self.local_name().to_owned(), document.as_ref());
                *copy.element_inner().attributes.borrow_mut() = self.attributes();
                copy
            }
            DomNodeType::Text => DomNode::new_text(self.data(), document.as_ref()),
            DomNodeType::Comment => DomNode::new_comment(self.data(), document.as_ref()),
            DomNodeType::Document => DomNode::new_document(),
            DomNodeType::DocumentFragment => DomNode::new_document_fragment(document.as_ref()),
        };
        if deep {
            for child in self.child_nodes() {
                copy.append_child(&child.clone_node(true));
            }
        }
        copy
    }

    // Document

    /// `document.createElement(tagName)`.
    pub fn create_element(&self, tag_name: &str) -> DomNode {
        DomNode::new_element(tag_name.to_ascii_lowercase(), Some(self))
    }

    /// `document.createTextNode(data)`.
    pub fn create_text_node(&self, data: &str) -> DomNode {
        DomNode::new_text(data, Some(self))
    }

    /// `document.createComment(data)`.
    pub fn create_comment(&self, data: &str) -> DomNode {
        DomNode::new_comment(data, Some(self))
    }

    /// `document.createDocumentFragment()`.
    pub fn create_document_fragment(&self) -> DomNode {
        DomNode::new_document_fragment(Some(self))
    }

    /// `document.documentElement`.
    pub fn document_element(&self) -> Option<DomNode> {
        self.children().into_iter().next()
    }

    /// `document.head`.
    pub fn head(&self) -> Option<DomNode> {
        self.document_element()?
            .children()
            .into_iter()
            .find(|e| e.local_name() == "head")
    }

    /// `document.body`.
    pub fn body(&self) -> Option<DomNode> {
        self.document_element()?
            .children()
            .into_iter()
            .find(|e| e.local_name() == "body")
    }

    // Element

    fn element_inner(&self) -> &ElementInner {
        self.0
            .element
            .as_ref()
            .expect("an element method on a node that is not an element")
    }

    /// The element's style slot, for the declaration.
    pub(super) fn style_slot(&self) -> Option<&RefCell<Option<CssStyleDeclaration>>> {
        self.0.element.as_ref().map(|e| &e.style)
    }

    /// `localName`: the lower-case tag name, or empty for a node that is not
    /// an element.
    pub fn local_name(&self) -> &str {
        self.0
            .element
            .as_ref()
            .map_or("", |e| e.local_name.as_str())
    }

    /// `tagName`: the upper-case tag name.
    pub fn tag_name(&self) -> String {
        self.local_name().to_ascii_uppercase()
    }

    /// `attributes`, as `(name, value)` pairs in order.
    pub fn attributes(&self) -> Vec<(String, String)> {
        self.0
            .element
            .as_ref()
            .map(|e| e.attributes.borrow().clone())
            .unwrap_or_default()
    }

    /// `getAttribute(name)`.
    pub fn get_attribute(&self, name: &str) -> Option<String> {
        let element = self.0.element.as_ref()?;
        let name = name.to_ascii_lowercase();
        element
            .attributes
            .borrow()
            .iter()
            .find(|(n, _)| *n == name)
            .map(|(_, v)| v.clone())
    }

    pub fn has_attribute(&self, name: &str) -> bool {
        self.get_attribute(name).is_some()
    }

    pub fn get_attribute_names(&self) -> Vec<String> {
        self.attributes().into_iter().map(|(n, _)| n).collect()
    }

    /// `setAttribute(name, value)`. Setting `style` replaces the inline
    /// declaration's properties with those of the value.
    pub fn set_attribute(&self, name: &str, value: &str) {
        let name = name.to_ascii_lowercase();
        self.set_attribute_value(&name, value);
        if name == "style"
            && let Some(declaration) = self.element_inner().style.borrow_mut().as_mut()
        {
            declaration.parse(value);
        }
    }

    /// `removeAttribute(name)`.
    pub fn remove_attribute(&self, name: &str) {
        let name = name.to_ascii_lowercase();
        let element = self.element_inner();
        element.attributes.borrow_mut().retain(|(n, _)| *n != name);
        if name == "style"
            && let Some(declaration) = element.style.borrow_mut().as_mut()
        {
            declaration.parse("");
        }
    }

    /// `toggleAttribute(name, force)`.
    pub fn toggle_attribute(&self, name: &str, force: Option<bool>) -> bool {
        let present = self.has_attribute(name);
        let next = force.unwrap_or(!present);
        if next && !present {
            self.set_attribute(name, "");
        }
        if !next && present {
            self.remove_attribute(name);
        }
        next
    }

    /// Writes an attribute without touching the style declaration (which is
    /// how the declaration itself updates the `style` attribute).
    pub(super) fn set_attribute_value(&self, name: &str, value: &str) {
        let mut attributes = self.element_inner().attributes.borrow_mut();
        match attributes.iter_mut().find(|(n, _)| n == name) {
            Some(entry) => value.clone_into(&mut entry.1),
            None => attributes.push((name.to_owned(), value.to_owned())),
        }
    }

    /// `id`.
    pub fn id(&self) -> String {
        self.get_attribute("id").unwrap_or_default()
    }

    pub fn set_id(&self, id: &str) {
        self.set_attribute("id", id);
    }

    /// `dir`.
    pub fn dir(&self) -> String {
        self.get_attribute("dir").unwrap_or_default()
    }

    pub fn set_dir(&self, dir: &str) {
        self.set_attribute("dir", dir);
    }

    /// `className`.
    pub fn class_name(&self) -> String {
        self.get_attribute("class").unwrap_or_default()
    }

    pub fn set_class_name(&self, class_name: &str) {
        self.set_attribute("class", class_name);
    }

    /// `classList`.
    pub fn class_list(&self) -> DomTokenList {
        DomTokenList {
            element: self.clone(),
            attribute: "class",
        }
    }

    /// `style`: the inline style declaration, kept in step with the `style`
    /// attribute.
    pub fn style(&self) -> CssStyle {
        CssStyle::new(self.clone())
    }

    /// `innerHTML`: the serialized children.
    pub fn inner_html(&self) -> String {
        serialize_html_children(self)
    }

    /// `outerHTML`: the element serialized.
    pub fn outer_html(&self) -> String {
        let mut out = String::new();
        serialize_html(self, &mut out);
        out
    }

    /// The descendant elements in document order.
    pub fn descendant_elements(&self) -> Vec<DomNode> {
        fn walk(node: &DomNode, out: &mut Vec<DomNode>) {
            for child in node.0.children.borrow().iter() {
                if child.is_element() {
                    out.push(child.clone());
                    walk(child, out);
                }
            }
        }
        let mut out = Vec::new();
        walk(self, &mut out);
        out
    }

    /// `getElementsByTagName(name)`.
    pub fn get_elements_by_tag_name(&self, name: &str) -> Vec<DomNode> {
        let name = name.to_ascii_lowercase();
        self.descendant_elements()
            .into_iter()
            .filter(|e| name == "*" || e.local_name() == name)
            .collect()
    }
}

/// `DOMTokenList` over an attribute: unique tokens, separated by single
/// spaces once changed.
#[derive(Clone, Debug)]
pub struct DomTokenList {
    element: DomNode,
    attribute: &'static str,
}

impl DomTokenList {
    /// The tokens, without duplicates.
    pub fn tokens(&self) -> Vec<String> {
        let mut out: Vec<String> = Vec::new();
        let value = self
            .element
            .get_attribute(self.attribute)
            .unwrap_or_default();
        for token in value
            .split([' ', '\t', '\n', '\r', '\u{0C}'])
            .filter(|t| !t.is_empty())
        {
            if !out.iter().any(|t| t == token) {
                out.push(token.to_owned());
            }
        }
        out
    }

    pub fn length(&self) -> usize {
        self.tokens().len()
    }

    pub fn value(&self) -> String {
        self.element
            .get_attribute(self.attribute)
            .unwrap_or_default()
    }

    pub fn contains(&self, token: &str) -> bool {
        self.tokens().iter().any(|t| t == token)
    }

    fn update(&self, tokens: &[String]) {
        // The spec skips the update when there is no attribute and no tokens.
        if !self.element.has_attribute(self.attribute) && tokens.is_empty() {
            return;
        }
        self.element
            .set_attribute(self.attribute, &tokens.join(" "));
    }

    /// `add(...tokens)`.
    pub fn add<S: AsRef<str>>(&self, tokens: &[S]) {
        let mut list = self.tokens();
        for token in tokens {
            let token = token.as_ref();
            if !list.iter().any(|t| t == token) {
                list.push(token.to_owned());
            }
        }
        self.update(&list);
    }

    /// `remove(...tokens)`.
    pub fn remove<S: AsRef<str>>(&self, tokens: &[S]) {
        let list: Vec<String> = self
            .tokens()
            .into_iter()
            .filter(|t| !tokens.iter().any(|r| r.as_ref() == t))
            .collect();
        self.update(&list);
    }

    /// `toggle(token, force)`.
    pub fn toggle(&self, token: &str, force: Option<bool>) -> bool {
        let present = self.contains(token);
        let next = force.unwrap_or(!present);
        if next && !present {
            self.add(&[token]);
        }
        if !next && present {
            self.remove(&[token]);
        }
        next
    }
}

#[cfg(test)]
mod tests {
    use crate::dom::get_document;

    /// The style attribute and the declaration stay in step as jsdom keeps
    /// them: an attribute reads as written until the declaration changes,
    /// then as its `cssText`; removing every property leaves `style=""`.
    #[test]
    fn style_attribute_follows_the_declaration() {
        let document = get_document();
        let span = document.create_element("span");
        span.set_attribute("style", "color:RED");
        assert_eq!(span.get_attribute("style").as_deref(), Some("color:RED"));
        assert_eq!(span.style().get_property_value("color"), "red");
        span.style().set_property("font-size", "0", "");
        assert_eq!(
            span.get_attribute("style").as_deref(),
            Some("color: red; font-size: 0px;")
        );
        span.style().remove_property("color");
        span.style().remove_property("font-size");
        assert_eq!(span.outer_html(), r#"<span style=""></span>"#);
        span.set_attribute("style", "margin: 0");
        assert_eq!(span.style().css_text(), "margin: 0px;");
    }

    /// Appending a fragment moves its children and leaves it empty.
    #[test]
    fn appending_a_fragment_moves_its_children() {
        let document = get_document();
        let fragment = document.create_document_fragment();
        fragment.append(&[document.create_element("b")]);
        fragment.append_text("x");
        let p = document.create_element("p");
        p.append(std::slice::from_ref(&fragment));
        assert_eq!(p.outer_html(), "<p><b></b>x</p>");
        assert!(!fragment.has_child_nodes());
    }
}