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
//! `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();
}
// Walk ancestors of each, record paths root → node.
let a_path = self.ancestor_path(a);
let b_path = self.ancestor_path(b);
// Disconnected: one is not reachable from a shared ancestor.
// For this arena we consider "disconnected" = different roots.
match (a_path.first(), b_path.first()) {
(Some(&ra), Some(&rb)) if ra != rb => {
return DocumentPosition::DISCONNECTED
| DocumentPosition::IMPLEMENTATION_SPECIFIC
| DocumentPosition::PRECEDING;
}
(None, _) | (_, None) => {
return DocumentPosition::DISCONNECTED
| DocumentPosition::IMPLEMENTATION_SPECIFIC
| DocumentPosition::PRECEDING;
}
_ => {}
}
// Find the common prefix length (lowest common ancestor).
let mut common = 0;
while common < a_path.len() && common < b_path.len() && a_path[common] == b_path[common] {
common += 1;
}
// If one path is a prefix of the other, it's an ancestor relationship.
if common == a_path.len() && common < b_path.len() {
// b is a descendant of a: b is contained by a and, in tree
// order, comes after it.
return DocumentPosition::CONTAINED_BY | DocumentPosition::FOLLOWING;
}
if common == b_path.len() && common < a_path.len() {
// b is an ancestor of a: b contains a and precedes it.
return DocumentPosition::CONTAINS | DocumentPosition::PRECEDING;
}
// Otherwise we diverged at `common`. Compare child positions under
// the common ancestor at index `common - 1`. If `common == 0`
// something is wrong (handled by the disconnected check above).
debug_assert!(common > 0, "compare_document_position: no LCA found");
let lca = a_path[common - 1];
let a_branch = a_path[common];
let b_branch = b_path[common];
// Which branch comes first in the child order of `lca`?
// The returned flags describe b's position relative to a:
// - if we encounter a_branch first → b comes after a → FOLLOWING
// - if we encounter b_branch first → b comes before a → PRECEDING
let mut cur = self.get_node(lca).and_then(|n| n.first_child);
while let Some(c) = cur {
if c == a_branch {
return DocumentPosition::FOLLOWING;
}
if c == b_branch {
return DocumentPosition::PRECEDING;
}
cur = self.get_node(c).and_then(|n| n.next_sibling);
}
// Shouldn't reach here.
DocumentPosition::empty()
}
/// Is `a` equal to `b` structurally (same tag, attrs, classes, text,
/// and recursively equal children)? Compares the tree shape — IDs +
/// parents are not considered.
pub fn is_equal_node(&self, a: NodeId, b: NodeId) -> bool {
use crate::node::NodeData;
let Some(na) = self.get_node(a) else {
return false;
};
let Some(nb) = self.get_node(b) else {
return false;
};
match (&na.data, &nb.data) {
(
NodeData::Element {
tag: ta,
attrs: aa,
classes: ca,
..
},
NodeData::Element {
tag: tb,
attrs: ab,
classes: cb,
..
},
) => {
if ta != tb || aa != ab || ca != cb {
return false;
}
}
(NodeData::Text { data: da }, NodeData::Text { data: db }) => {
return da == db;
}
(NodeData::Comment { data: da }, NodeData::Comment { data: db }) => {
return da == db;
}
(NodeData::Fragment, NodeData::Fragment) => {}
_ => return false,
}
// Compare children in order.
let mut ca = na.first_child;
let mut cb = nb.first_child;
loop {
match (ca, cb) {
(None, None) => return true,
(Some(ca_id), Some(cb_id)) => {
if !self.is_equal_node(ca_id, cb_id) {
return false;
}
ca = self.get_node(ca_id).and_then(|n| n.next_sibling);
cb = self.get_node(cb_id).and_then(|n| n.next_sibling);
}
_ => return 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) {
let b_path = self.ancestor_path(b.node);
let depth = b_path.iter().position(|&n| n == a.node)?;
let child = *b_path.get(depth + 1)?;
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> {
if a == b {
return self.get_node(a).map(|_| a);
}
let a_path = self.ancestor_path(a);
let b_path = self.ancestor_path(b);
// Different roots → no common ancestor.
match (a_path.first(), b_path.first()) {
(Some(ra), Some(rb)) if ra != rb => return None,
(None, _) | (_, None) => return None,
_ => {}
}
// Walk both paths in lock-step from the root, keeping the
// last matching node.
let mut last = None;
for (x, y) in a_path.iter().zip(b_path.iter()) {
if x == y {
last = Some(*x);
} else {
break;
}
}
last
}
}
// ─────────────────────────────────────────────────────────────────────
// 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;
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));
}
}