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
//! `compare_document_position` — DOM spec bitflag describing how two nodes
//! relate (precedes / follows / contains / is-contained-by / disconnected).
//!
//! Spec: <https://dom.spec.whatwg.org/#dom-node-comparedocumentposition>
use crate::bitflags_like;
use crate::dom::Dom;
use crate::node_id::NodeId;
bitflags_like! {
/// Bitflags matching MDN's `Node.compareDocumentPosition` return value.
/// Multiple bits can be set — e.g. `CONTAINED_BY | FOLLOWING` when the
/// other node is a descendant (descendants are considered "following"
/// in document order).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct DocumentPosition(u16) {
DISCONNECTED = 0b0000_0001;
PRECEDING = 0b0000_0010;
FOLLOWING = 0b0000_0100;
CONTAINS = 0b0000_1000;
CONTAINED_BY = 0b0001_0000;
IMPLEMENTATION_SPECIFIC = 0b0010_0000;
}
}
impl<Ext> Dom<Ext> {
/// `a.compareDocumentPosition(b)`: a bitmask describing **`b` relative
/// to `a`** (DOM §4.4). Same orientation as the web: the bits say where
/// the *argument* sits.
///
/// - `a == b` → empty bits (0).
/// - `b` is a descendant of `a` → `CONTAINED_BY | FOLLOWING` (20).
/// - `b` is an ancestor of `a` → `CONTAINS | PRECEDING` (10).
/// - `b` comes later in tree order → `FOLLOWING`.
/// - `b` comes earlier in tree order → `PRECEDING`.
/// - different trees → `DISCONNECTED | IMPLEMENTATION_SPECIFIC | PRECEDING`.
///
/// `FOLLOWING` therefore always means "`b` is later in tree order",
/// whether `b` is a sibling's subtree or `a`'s own descendant.
pub fn compare_document_position(&self, a: NodeId, b: NodeId) -> DocumentPosition {
if a == b {
return DocumentPosition::empty();
}
// DOM §4.4 step 6: different trees.
const IN_DIFFERENT_TREES: DocumentPosition = DocumentPosition::from_bits_truncate(
DocumentPosition::DISCONNECTED.bits()
| DocumentPosition::IMPLEMENTATION_SPECIFIC.bits()
| DocumentPosition::PRECEDING.bits(),
);
let (Some(da), Some(db)) = (self.depth_of(a), self.depth_of(b)) else {
return IN_DIFFERENT_TREES;
};
// Bring both cursors to the same depth. If they meet, one node is
// an ancestor of the other. No paths are materialized: this runs
// per text node per frame while a selection is painted
// (`CORE-DOCPOS-ALLOC-1`).
let (mut x, mut y) = (a, b);
for _ in db..da {
x = self.parent_of(x);
}
for _ in da..db {
y = self.parent_of(y);
}
if x == y {
return if da < db {
// b is a descendant of a: contained by a and, in tree
// order, after it.
DocumentPosition::CONTAINED_BY | DocumentPosition::FOLLOWING
} else {
// b is an ancestor of a: contains a and precedes it.
DocumentPosition::CONTAINS | DocumentPosition::PRECEDING
};
}
// Walk up in lockstep until the parents coincide; `x` / `y` are
// then the two branches under the lowest common ancestor.
let lca = loop {
match (
self.get_node(x).and_then(|n| n.parent),
self.get_node(y).and_then(|n| n.parent),
) {
(Some(px), Some(py)) if px == py => break px,
(Some(px), Some(py)) => {
x = px;
y = py;
}
// Different roots.
_ => return IN_DIFFERENT_TREES,
}
};
// Which branch comes first among `lca`'s children? The flags
// describe b relative to a: a's branch first → b FOLLOWING.
let mut cur = self.get_node(lca).and_then(|n| n.first_child);
while let Some(c) = cur {
if c == x {
return DocumentPosition::FOLLOWING;
}
if c == y {
return DocumentPosition::PRECEDING;
}
cur = self.get_node(c).and_then(|n| n.next_sibling);
}
debug_assert!(
false,
"compare_document_position: branches not under their LCA"
);
DocumentPosition::empty()
}
/// Depth of `id` below its root (root = 0); `None` if `id` is not in
/// the arena.
fn depth_of(&self, id: NodeId) -> Option<usize> {
let mut node = self.get_node(id)?;
let mut depth = 0;
while let Some(p) = node.parent {
node = self.get_node(p)?;
depth += 1;
}
Some(depth)
}
/// Parent of a node known to be live and non-root (callers have
/// measured its depth).
fn parent_of(&self, id: NodeId) -> NodeId {
self.get_node(id)
.and_then(|n| n.parent)
.expect("parent_of: node has a parent (depth checked by caller)")
}
/// The root of the tree `id` is in; `None` if `id` is not live.
pub(crate) fn root_of(&self, id: NodeId) -> Option<NodeId> {
let mut cur = id;
let mut node = self.get_node(cur)?;
while let Some(p) = node.parent {
cur = p;
node = self.get_node(cur)?;
}
Some(cur)
}
/// Is `a` equal to `b` structurally (same tag, attrs, classes, text,
/// and recursively equal children)? Compares the tree shape — IDs +
/// parents are not considered. Iterative over a stack of node pairs,
/// so any depth is safe (`P7G-CORE-SMALL-1`).
pub fn is_equal_node(&self, a: NodeId, b: NodeId) -> bool {
let mut pending = vec![(a, b)];
while let Some((a, b)) = pending.pop() {
let (Some(na), Some(nb)) = (self.get_node(a), self.get_node(b)) else {
return false;
};
if !Self::same_node_data(&na.data, &nb.data) {
return false;
}
// Children pairwise, in order; a length mismatch is unequal.
let (mut ca, mut cb) = (na.first_child, nb.first_child);
loop {
match (ca, cb) {
(None, None) => break,
(Some(x), Some(y)) => {
pending.push((x, y));
ca = self.get_node(x).and_then(|n| n.next_sibling);
cb = self.get_node(y).and_then(|n| n.next_sibling);
}
_ => return false,
}
}
}
true
}
/// `is_equal_node`'s per-node test: same kind, and the same tag /
/// attributes / classes or the same data.
fn same_node_data(a: &crate::node::NodeData<Ext>, b: &crate::node::NodeData<Ext>) -> bool {
use crate::node::NodeData;
match (a, b) {
(
NodeData::Element {
tag: ta,
attrs: aa,
classes: ca,
..
},
NodeData::Element {
tag: tb,
attrs: ab,
classes: cb,
..
},
) => ta == tb && aa == ab && ca == cb,
(NodeData::Text { data: da }, NodeData::Text { data: db })
| (NodeData::Comment { data: da }, NodeData::Comment { data: db }) => da == db,
(NodeData::Fragment, NodeData::Fragment) => true,
_ => false,
}
}
/// Order two boundary points per DOM §5.2 ("position of a boundary
/// point relative to another"). `Less` when `a` comes before `b`,
/// `Equal` when they are the same point, `None` when the nodes are in
/// different trees.
///
/// An element position `(el, k)` sits between `el`'s children `k-1`
/// and `k`, so it orders against a point inside child `j` by comparing
/// `j` with `k` — not by which node contains the other.
pub fn compare_boundary_points(
&self,
a: crate::Position,
b: crate::Position,
) -> Option<std::cmp::Ordering> {
use std::cmp::Ordering;
if a.node == b.node {
return Some(a.offset.cmp(&b.offset));
}
let pos = self.compare_document_position(a.node, b.node);
if pos.contains(DocumentPosition::DISCONNECTED) {
return None;
}
// `a.node` follows `b.node` (including `a.node` inside `b.node`):
// answer from the other side and flip.
if pos.contains(DocumentPosition::PRECEDING) {
return self.compare_boundary_points(b, a).map(Ordering::reverse);
}
// `a.node` is an ancestor of `b.node`: find `a.node`'s child on
// the way down to `b.node` and compare its index with `a.offset`.
if pos.contains(DocumentPosition::CONTAINED_BY) {
// Climb from `b.node` to the child of `a.node` on its path.
let mut child = b.node;
loop {
let parent = self.get_node(child)?.parent?;
if parent == a.node {
break;
}
child = parent;
}
let index = self.child_index_of(child)?;
return Some(if index < a.offset {
Ordering::Greater
} else {
Ordering::Less
});
}
// Plain tree order: `b.node` follows `a.node`.
Some(Ordering::Less)
}
/// Index of `id` among its parent's children, `None` for a root or a
/// freed node.
fn child_index_of(&self, id: NodeId) -> Option<usize> {
let parent = self.get_node(id)?.parent?;
let mut cur = self.get_node(parent)?.first_child;
let mut index = 0;
while let Some(c) = cur {
if c == id {
return Some(index);
}
index += 1;
cur = self.get_node(c)?.next_sibling;
}
None
}
/// Path from root → this node as `Vec<NodeId>` (inclusive on both ends).
/// Empty if the node isn't in the arena.
pub fn ancestor_path(&self, id: NodeId) -> Vec<NodeId> {
let mut path = Vec::new();
let mut cur = Some(id);
while let Some(c) = cur {
if self.get_node(c).is_none() {
return Vec::new();
}
path.push(c);
cur = self.get_node(c).and_then(|n| n.parent);
}
path.reverse();
path
}
/// Lowest common ancestor of `a` and `b` — the deepest node
/// that contains both. Returns `None` if `a` and `b` live in
/// different arenas or if either node is invalid.
///
/// Used by the runtime for click synthesis: when `mousedown`
/// fires on one target and `mouseup` on another, the `click`
/// event dispatches on their common ancestor (HTML semantics).
///
/// When `a == b`, returns `Some(a)`. When one is an ancestor
/// of the other, returns the ancestor.
pub fn common_ancestor(&self, a: NodeId, b: NodeId) -> Option<NodeId> {
let (da, db) = (self.depth_of(a)?, self.depth_of(b)?);
let (mut x, mut y) = (a, b);
for _ in db..da {
x = self.parent_of(x);
}
for _ in da..db {
y = self.parent_of(y);
}
while x != y {
// Same depth: both have parents or neither does. Neither →
// two roots → disconnected.
x = self.get_node(x)?.parent?;
y = self.get_node(y)?.parent?;
}
Some(x)
}
}
// ─────────────────────────────────────────────────────────────────────
// Small bitflags-without-crate helper
// ─────────────────────────────────────────────────────────────────────
/// Minimal bitflag macro so we don't pull in the `bitflags` crate for
/// a single use. Generates impls for `|`, `&`, `contains`, `empty`,
/// `bits`, `all`, `from_bits_truncate`, etc.
#[macro_export]
#[doc(hidden)]
macro_rules! bitflags_like {
(
$(#[$outer:meta])*
$vis:vis struct $name:ident ( $repr:ty ) {
$( $flag:ident = $value:expr; )+
}
) => {
$(#[$outer])*
$vis struct $name($repr);
impl $name {
$( pub const $flag: Self = Self($value); )+
#[inline] pub const fn empty() -> Self { Self(0) }
#[inline] pub const fn all() -> Self { Self( $( $value )|+ ) }
#[inline] pub const fn bits(self) -> $repr { self.0 }
#[inline] pub const fn from_bits_truncate(bits: $repr) -> Self {
Self(bits & Self::all().0)
}
#[inline] pub const fn contains(self, other: Self) -> bool {
(self.0 & other.0) == other.0
}
#[inline] pub const fn is_empty(self) -> bool { self.0 == 0 }
/// Clear the bits of `other` from `self`. Equivalent to
/// `self & !other` but doesn't need a `Not` impl.
#[inline] pub const fn without(self, other: Self) -> Self {
Self(self.0 & !other.0)
}
}
impl std::ops::BitOr for $name {
type Output = Self;
#[inline] fn bitor(self, rhs: Self) -> Self { Self(self.0 | rhs.0) }
}
impl std::ops::BitOrAssign for $name {
#[inline] fn bitor_assign(&mut self, rhs: Self) { self.0 |= rhs.0; }
}
impl std::ops::BitAnd for $name {
type Output = Self;
#[inline] fn bitand(self, rhs: Self) -> Self { Self(self.0 & rhs.0) }
}
impl std::ops::BitAndAssign for $name {
#[inline] fn bitand_assign(&mut self, rhs: Self) { self.0 &= rhs.0; }
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Dom;
/// The original path-materializing algorithm, kept as the oracle for
/// the allocation-free depth walk.
fn reference_compare(dom: &Dom, a: NodeId, b: NodeId) -> DocumentPosition {
if a == b {
return DocumentPosition::empty();
}
let a_path = dom.ancestor_path(a);
let b_path = dom.ancestor_path(b);
if a_path.first() != b_path.first() || a_path.is_empty() {
return DocumentPosition::DISCONNECTED
| DocumentPosition::IMPLEMENTATION_SPECIFIC
| DocumentPosition::PRECEDING;
}
let mut common = 0;
while common < a_path.len() && common < b_path.len() && a_path[common] == b_path[common] {
common += 1;
}
if common == a_path.len() {
return DocumentPosition::CONTAINED_BY | DocumentPosition::FOLLOWING;
}
if common == b_path.len() {
return DocumentPosition::CONTAINS | DocumentPosition::PRECEDING;
}
let lca = a_path[common - 1];
let mut cur = dom.get_node(lca).and_then(|n| n.first_child);
while let Some(c) = cur {
if c == a_path[common] {
return DocumentPosition::FOLLOWING;
}
if c == b_path[common] {
return DocumentPosition::PRECEDING;
}
cur = dom.get_node(c).and_then(|n| n.next_sibling);
}
unreachable!()
}
/// `CORE-DOCPOS-ALLOC-1`: the depth walk agrees with the path oracle
/// on every ordered pair of a bushy tree plus a detached subtree.
#[test]
fn depth_walk_matches_the_path_oracle_on_every_pair() {
let mut dom: Dom = Dom::new();
let root = dom.root();
let mut all = vec![root];
// Three levels, three children each: 1 + 3 + 9 + 27 nodes.
let mut frontier = vec![root];
for _ in 0..3 {
let mut next = Vec::new();
for &p in &frontier {
for _ in 0..3 {
let c = dom.create_element("n");
dom.append_child(p, c).unwrap();
all.push(c);
next.push(c);
}
}
frontier = next;
}
// A detached subtree with its own root.
let loose = dom.create_element("loose");
let loose_child = dom.create_element("lc");
dom.append_child(loose, loose_child).unwrap();
all.push(loose);
all.push(loose_child);
// A freed node.
let gone = dom.create_element("gone");
dom.drop_subtree(gone).unwrap();
all.push(gone);
for &a in &all {
for &b in &all {
assert_eq!(
dom.compare_document_position(a, b),
reference_compare(&dom, a, b),
"pair ({a:?}, {b:?})"
);
let expected_lca = {
let pa = dom.ancestor_path(a);
let pb = dom.ancestor_path(b);
pa.iter()
.zip(pb.iter())
.take_while(|(x, y)| x == y)
.last()
.map(|(x, _)| *x)
};
assert_eq!(
dom.common_ancestor(a, b),
expected_lca,
"lca ({a:?}, {b:?})"
);
}
}
}
fn build() -> (Dom, NodeId, NodeId, NodeId, NodeId) {
// root
// ├─ a
// │ └─ grandchild
// └─ b
let mut dom: Dom = Dom::new();
let root = dom.root();
let a = dom.create_element("a");
let b = dom.create_element("b");
let grandchild = dom.create_element("g");
dom.append_child(root, a).unwrap();
dom.append_child(root, b).unwrap();
dom.append_child(a, grandchild).unwrap();
(dom, a, b, grandchild, root)
}
#[test]
fn self_is_empty() {
let (dom, a, _, _, _) = build();
assert_eq!(
dom.compare_document_position(a, a),
DocumentPosition::empty()
);
}
/// DOM §4.4 `compareDocumentPosition`: the bits describe `other`
/// relative to `this`. `parent.compareDocumentPosition(child)` is
/// `CONTAINED_BY | FOLLOWING` (20) in every browser.
#[test]
fn descendant_argument_is_contained_by_and_following() {
let (dom, a, _, g, _) = build();
let r = dom.compare_document_position(a, g);
assert_eq!(
r,
DocumentPosition::CONTAINED_BY | DocumentPosition::FOLLOWING
);
}
/// `child.compareDocumentPosition(parent)` is `CONTAINS | PRECEDING` (10).
#[test]
fn ancestor_argument_contains_and_precedes() {
let (dom, a, _, g, _) = build();
let r = dom.compare_document_position(g, a);
assert_eq!(r, DocumentPosition::CONTAINS | DocumentPosition::PRECEDING);
}
/// The containment branch and the sibling branch must agree on what
/// FOLLOWING means: "the argument comes later in tree order".
#[test]
fn following_bit_is_consistent_across_containment_and_siblings() {
let (dom, a, b, g, _) = build();
// g is inside a, and a precedes b — so g precedes b too.
assert!(
dom.compare_document_position(a, g)
.contains(DocumentPosition::FOLLOWING)
);
assert!(
dom.compare_document_position(a, b)
.contains(DocumentPosition::FOLLOWING)
);
assert!(
dom.compare_document_position(g, b)
.contains(DocumentPosition::FOLLOWING)
);
}
// ── Boundary points (DOM §5.2) ─────────────────────────────────────
#[test]
fn boundary_points_same_node_order_by_offset() {
use crate::Position;
use std::cmp::Ordering;
let (dom, a, _, _, _) = build();
assert_eq!(
dom.compare_boundary_points(Position::new(a, 0), Position::new(a, 1)),
Some(Ordering::Less)
);
assert_eq!(
dom.compare_boundary_points(Position::new(a, 1), Position::new(a, 1)),
Some(Ordering::Equal)
);
}
/// (ancestor, offset) vs a point inside child `j`: the ancestor point
/// is before iff `j >= offset`.
#[test]
fn boundary_points_ancestor_offset_splits_around_child_index() {
use crate::Position;
use std::cmp::Ordering;
let (dom, a, _, g, root) = build();
// root children: [a, b]; g is inside a (child index 0 of root).
let in_g = Position::new(g, 0);
assert_eq!(
dom.compare_boundary_points(Position::new(root, 0), in_g),
Some(Ordering::Less)
);
assert_eq!(
dom.compare_boundary_points(Position::new(root, 1), in_g),
Some(Ordering::Greater)
);
// Symmetric.
assert_eq!(
dom.compare_boundary_points(in_g, Position::new(root, 1)),
Some(Ordering::Less)
);
assert_eq!(
dom.compare_boundary_points(in_g, Position::new(a, 0)),
Some(Ordering::Greater)
);
}
/// An offset past the last child is "after every child" (a Range end
/// of `(parent, childCount)`), not an error.
#[test]
fn boundary_point_offset_beyond_child_count_orders_after_all_children() {
use crate::Position;
use std::cmp::Ordering;
let (dom, a, b, g, root) = build();
let end = Position::new(root, 99);
for inside in [
Position::new(a, 0),
Position::new(g, 0),
Position::new(b, 0),
] {
assert_eq!(
dom.compare_boundary_points(end, inside),
Some(Ordering::Greater)
);
assert_eq!(
dom.compare_boundary_points(inside, end),
Some(Ordering::Less)
);
}
}
#[test]
fn boundary_points_disconnected_is_none() {
use crate::Position;
let (mut dom, a, _, _, _) = build();
let loose = dom.create_element("x");
assert_eq!(
dom.compare_boundary_points(Position::new(a, 0), Position::new(loose, 0)),
None
);
}
#[test]
fn siblings_ordered_by_position() {
let (dom, a, b, _, _) = build();
assert!(
dom.compare_document_position(a, b)
.contains(DocumentPosition::FOLLOWING)
);
assert!(
dom.compare_document_position(b, a)
.contains(DocumentPosition::PRECEDING)
);
}
#[test]
fn disconnected_nodes_flagged() {
let mut dom: Dom = Dom::new();
let a = dom.create_element("a"); // orphan
let b = dom.create_element("b"); // orphan
let r = dom.compare_document_position(a, b);
assert!(r.contains(DocumentPosition::DISCONNECTED));
}
// ── common_ancestor ──────────────────────────────────────────────
#[test]
fn common_ancestor_self_is_self() {
let (dom, a, _, _, _) = build();
assert_eq!(dom.common_ancestor(a, a), Some(a));
}
#[test]
fn common_ancestor_siblings_is_parent() {
let (dom, a, b, _, root) = build();
assert_eq!(dom.common_ancestor(a, b), Some(root));
}
#[test]
fn common_ancestor_nested_is_ancestor() {
// g is descendant of a → common ancestor is a itself.
let (dom, a, _, g, _) = build();
assert_eq!(dom.common_ancestor(a, g), Some(a));
assert_eq!(dom.common_ancestor(g, a), Some(a));
}
#[test]
fn common_ancestor_cousins_is_lca() {
// root → a → g, root → b. g and b share root.
let (dom, _, b, g, root) = build();
assert_eq!(dom.common_ancestor(g, b), Some(root));
}
#[test]
fn common_ancestor_disconnected_returns_none() {
let mut dom: Dom = Dom::new();
let a = dom.create_element("a"); // orphan
let b = dom.create_element("b"); // orphan
assert_eq!(dom.common_ancestor(a, b), None);
}
// ── is_equal_node ────────────────────────────────────────────────
#[test]
fn equal_node_same_tag_and_attrs() {
let mut dom: Dom = Dom::new();
let a = dom.create_element("div");
let b = dom.create_element("div");
dom.set_attribute(a, "role", "banner").unwrap();
dom.set_attribute(b, "role", "banner").unwrap();
assert!(dom.is_equal_node(a, b));
}
#[test]
fn unequal_different_tag() {
let mut dom: Dom = Dom::new();
let a = dom.create_element("div");
let b = dom.create_element("span");
assert!(!dom.is_equal_node(a, b));
}
#[test]
fn unequal_different_attr() {
let mut dom: Dom = Dom::new();
let a = dom.create_element("div");
let b = dom.create_element("div");
dom.set_attribute(a, "role", "banner").unwrap();
dom.set_attribute(b, "role", "navigation").unwrap();
assert!(!dom.is_equal_node(a, b));
}
#[test]
fn equal_text_nodes_same_data() {
let mut dom: Dom = Dom::new();
let a = dom.create_text_node("hi");
let b = dom.create_text_node("hi");
let c = dom.create_text_node("bye");
assert!(dom.is_equal_node(a, b));
assert!(!dom.is_equal_node(a, c));
}
#[test]
fn equal_with_children() {
let mut dom: Dom = Dom::new();
let a = dom.create_element("div");
let a1 = dom.create_text_node("hello");
dom.append_child(a, a1).unwrap();
let b = dom.create_element("div");
let b1 = dom.create_text_node("hello");
dom.append_child(b, b1).unwrap();
assert!(dom.is_equal_node(a, b));
}
#[test]
fn unequal_different_child_count() {
let mut dom: Dom = Dom::new();
let a = dom.create_element("div");
let a1 = dom.create_text_node("x");
dom.append_child(a, a1).unwrap();
let b = dom.create_element("div");
// no children
assert!(!dom.is_equal_node(a, b));
}
#[test]
fn unequal_different_node_types() {
let mut dom: Dom = Dom::new();
let a = dom.create_element("div");
let b = dom.create_text_node("div");
assert!(!dom.is_equal_node(a, b));
}
}