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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 James Conway
//! The parsed document: every node in one `Vec`, linked by index, and every
//! string in one buffer, so building it allocates a handful of times rather
//! than once per node, and dropping it frees a handful of blocks.
use crate::tags::Tag;
/// A node's index in its document; the document itself is node 0.
pub type NodeId = u32;
/// No node, in the links.
pub(crate) const NONE: NodeId = NodeId::MAX;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Ns {
Html,
Svg,
MathMl,
/// An element of an XML document: its name is the qualified name as
/// written, and its namespace is the host's to keep.
Xml,
}
/// A namespaced attribute's namespace, from the spec's "adjust foreign
/// attributes" table; `None` for every ordinary attribute.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AttrNs {
None,
XLink,
Xml,
Xmlns,
/// An XML attribute in a namespace the host numbered; its name is the
/// qualified name as written.
Host(u32),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Quirks {
No,
Limited,
Quirks,
}
/// A span's high bit says it's in the document's copy of its source, not
/// in the buffer of strings the parser had to make.
const IN_SOURCE: u32 = 1 << 31;
/// A span with this bit (and not IN_SOURCE) is a string an edit added:
/// `start` is its index in `extras`.
const IN_EXTRA: u32 = 1 << 30;
/// The most input a document can hold. Spans keep offsets in 30 bits, and
/// the parse buffer, which only holds copies of what the input says, stays
/// well under that.
pub const MAX_INPUT: usize = 1 << 29;
#[derive(Debug, Clone, Copy)]
pub(crate) struct Span {
pub start: u32,
pub end: u32,
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum Name {
/// The tag's own lowercase name.
Tag,
/// An SVG name the parser fixed the case of, such as `clipPath`: its
/// index in the table of them.
Svg(u8),
/// Any other name, in the document's buffer.
Span(Span),
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct Attr {
pub ns: AttrNs,
pub name: Span,
pub value: Span,
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct Element {
pub ns: Ns,
pub tag: Tag,
pub name: Name,
pub attrs: Span,
/// A template's contents, a fragment node of its own.
pub contents: NodeId,
/// A MathML annotation-xml whose encoding makes it an HTML integration
/// point.
pub html_integration: bool,
}
/// Whether a shadow root's host lets scripts reach it, from
/// `shadowrootmode` or `attachShadow`'s `mode`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ShadowRootMode {
Open,
Closed,
}
/// How a shadow root's slots get their nodes: by the `slot` attribute of
/// the host's children, or only as [`Document::assign`] says.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SlotAssignment {
#[default]
Named,
Manual,
}
/// A shadow root's settings, as `attachShadow` takes them and a
/// declarative `<template shadowrootmode>` sets them.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ShadowRoot {
pub mode: ShadowRootMode,
pub delegates_focus: bool,
pub clonable: bool,
pub serializable: bool,
pub slot_assignment: SlotAssignment,
/// Made by the parser from a `<template shadowrootmode>`.
pub declarative: bool,
}
impl ShadowRoot {
/// An open root, with every flag off and named slot assignment.
pub fn open() -> ShadowRoot {
ShadowRoot {
mode: ShadowRootMode::Open,
delegates_focus: false,
clonable: false,
serializable: false,
slot_assignment: SlotAssignment::Named,
declarative: false,
}
}
/// A closed root, otherwise as [`ShadowRoot::open`].
pub fn closed() -> ShadowRoot {
ShadowRoot {
mode: ShadowRootMode::Closed,
..ShadowRoot::open()
}
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum Data {
Document,
Fragment,
/// A shadow root: like a fragment, nobody's child, with a link back to
/// its host, which [`Document::shadow_root`] gives from the host.
ShadowRoot {
host: NodeId,
init: ShadowRoot,
},
Doctype {
name: Span,
public_id: Span,
system_id: Span,
},
Element(Element),
Text(Span),
Comment(Span),
ProcessingInstruction {
target: Span,
data: Span,
},
/// An XML CDATA section.
Cdata(Span),
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct Node {
pub parent: NodeId,
pub first_child: NodeId,
pub last_child: NodeId,
pub prev: NodeId,
pub next: NodeId,
pub data: Data,
}
/// A parsed HTML document, or a fragment.
pub struct Document {
pub(crate) nodes: Vec<Node>,
/// The input, after preprocessing. Text and attribute values that
/// didn't need changing point into it rather than being copied.
pub(crate) src: String,
pub(crate) buf: String,
/// Strings with an allocation each: what edits add, and text the
/// parser had to keep growing away from the end of `buf`. Only the
/// parse grows one, so once it's done none moves, and an edit never
/// moves a string already handed out, as growing `buf` would.
pub(crate) extras: Vec<String>,
pub(crate) attrs: Vec<Attr>,
pub(crate) quirks: Quirks,
/// Whether it was parsed with scripting on, which decides how a
/// noscript element's text serializes.
pub(crate) scripting: bool,
/// Where each node and attribute was written, when the parse was
/// asked to keep that, and how to map offsets back to the input, for
/// spans or errors.
pub(crate) positions: Option<Box<Positions>>,
/// Each template's contents, mapped to the template: no link leads
/// back from the contents, and an edit's cycle check has to climb out
/// of them. Edits build it the first time they need it.
pub(crate) hosts: Option<std::collections::HashMap<NodeId, NodeId>>,
/// Each shadow host's shadow root. Empty, and so free, for a page
/// without any.
pub(crate) shadow_roots: std::collections::HashMap<NodeId, NodeId>,
/// Each slot's manually assigned nodes, for a shadow root with
/// [`SlotAssignment::Manual`].
pub(crate) manual_slots: std::collections::HashMap<NodeId, Vec<NodeId>>,
/// The parse errors, when the parse was asked to collect them.
pub(crate) errors: Vec<crate::SyntaxError>,
/// A bit per node taken out with [`Document::remove`]. A slot is never
/// reused, so a removed id stays removed. Empty until a remove.
pub(crate) dead: Vec<u64>,
}
/// A node or attribute with no recorded position.
const UNKNOWN: [u32; 2] = [u32::MAX; 2];
/// Where nodes and attributes were written, as `[start, end)` offsets
/// into the preprocessed source; the original input differs only by the
/// CRs that preprocessing dropped and a dropped byte order mark, so these
/// two are kept to map back.
#[derive(Debug, Default)]
pub(crate) struct Positions {
/// By node id: an element's start tag, or the whole of anything else.
pub nodes: Vec<[u32; 2]>,
/// By node id: an element's end tag.
pub ends: Vec<[u32; 2]>,
/// By index into [`Document::attrs`].
pub attrs: Vec<[u32; 2]>,
/// Where the preprocessed source has an LF that was a CRLF, ascending.
pub crlf: Vec<u32>,
/// Bytes before the source in the original: a byte order mark.
pub skipped: u32,
}
impl Positions {
pub(crate) fn set(list: &mut Vec<[u32; 2]>, i: usize, at: [u32; 2]) {
if list.len() <= i {
list.resize(i + 1, UNKNOWN);
}
list[i] = at;
}
fn get(list: &[[u32; 2]], i: usize) -> Option<[u32; 2]> {
list.get(i).copied().filter(|&a| a != UNKNOWN)
}
/// An offset in the preprocessed source, as one in the original.
pub(crate) fn original(&self, at: u32) -> usize {
let dropped = self.crlf.partition_point(|&lf| lf < at);
self.skipped as usize + at as usize + dropped
}
fn range(&self, at: [u32; 2]) -> std::ops::Range<usize> {
self.original(at[0])..self.original(at[1])
}
}
/// What a node is, as [`Document::kind`] reports it.
#[derive(Debug, PartialEq, Eq)]
pub enum Kind<'a> {
Document,
Fragment,
/// A shadow root: [`Document::shadow_host`] is its host.
ShadowRoot(ShadowRoot),
Doctype {
name: &'a str,
public_id: &'a str,
system_id: &'a str,
},
Element {
ns: Ns,
name: &'a str,
},
Text(&'a str),
Comment(&'a str),
ProcessingInstruction {
target: &'a str,
data: &'a str,
},
Cdata(&'a str),
}
impl Document {
pub(crate) fn new(root: Data, capacity: usize) -> Document {
// Enough for a dense page, a table cell every dozen bytes: growing
// a big page's nodes copies them, briefly holding both. Space never
// used is never touched, so it costs no memory.
let mut nodes = Vec::with_capacity(capacity / 10 + 16);
nodes.push(Node {
parent: NONE,
first_child: NONE,
last_child: NONE,
prev: NONE,
next: NONE,
data: root,
});
Document {
nodes,
src: String::new(),
buf: String::with_capacity(capacity / 8),
extras: Vec::new(),
attrs: Vec::with_capacity(capacity / 32 + 16),
quirks: Quirks::No,
scripting: false,
positions: None,
hosts: None,
shadow_roots: std::collections::HashMap::new(),
manual_slots: std::collections::HashMap::new(),
errors: Vec::new(),
dead: Vec::new(),
}
}
/// An empty document to build into, as XML is: no source, no nodes
/// but the document itself.
pub fn empty() -> Document {
Document::new(Data::Document, 0)
}
pub fn root(&self) -> NodeId {
0
}
pub fn quirks(&self) -> Quirks {
self.quirks
}
pub fn len(&self) -> usize {
self.nodes.len()
}
pub fn is_empty(&self) -> bool {
self.nodes.len() <= 1
}
pub(crate) fn str(&self, s: Span) -> &str {
if s.start & IN_SOURCE != 0 {
&self.src[(s.start & !IN_SOURCE) as usize..(s.end & !IN_SOURCE) as usize]
} else if s.start & IN_EXTRA != 0 {
&self.extras[(s.start & !IN_EXTRA) as usize]
} else {
&self.buf[s.start as usize..s.end as usize]
}
}
/// A span for a string an edit adds. Unlike the parser's strings, it
/// never moves, so a `&str` read before the edit stays good.
pub(crate) fn push_stable(&mut self, s: &str) -> Span {
let at = self.extras.len() as u32 | IN_EXTRA;
self.extras.push(s.to_owned());
Span { start: at, end: at }
}
/// Where `s` is in the source, if it's a slice of it.
pub(crate) fn source_offset(&self, s: &str) -> Option<usize> {
let base = self.src.as_ptr() as usize;
let at = s.as_ptr() as usize;
(at >= base && at + s.len() <= base + self.src.len()).then(|| at - base)
}
/// A span for `s`: into the source when it's a slice of it, which
/// costs nothing, else a copy in the buffer.
pub(crate) fn intern(&mut self, s: &str) -> Span {
match self.source_offset(s) {
Some(at) => Span {
start: at as u32 | IN_SOURCE,
end: (at + s.len()) as u32 | IN_SOURCE,
},
None => self.push_str(s),
}
}
pub(crate) fn push_str(&mut self, s: &str) -> Span {
let start = self.buf.len() as u32;
self.buf.push_str(s);
// Past this, an offset would read as one of the flags.
debug_assert!(self.buf.len() < IN_EXTRA as usize, "buffer over 2^30 bytes");
Span {
start,
end: self.buf.len() as u32,
}
}
pub(crate) fn add(&mut self, data: Data) -> NodeId {
let id = self.nodes.len() as NodeId;
self.nodes.push(Node {
parent: NONE,
first_child: NONE,
last_child: NONE,
prev: NONE,
next: NONE,
data,
});
id
}
/// Where a node was written, as a byte range of the input the parse
/// was given: an element's start tag from `<` to `>`, or the whole of
/// a text node, comment, doctype or processing instruction. A text
/// node made of several runs (text, then a character reference, then
/// text again, or text foster-parented out of a table) spans the first
/// run's start to the last one's end.
///
/// `None` for a node the parser made up rather than read (an implied
/// `<html>`, `<head>`, `<body>` or `<tbody>`, a formatting element it
/// reopened, a copy the adoption agency made), for one an edit made,
/// and for every node unless the document was parsed with
/// [`Options::spans`](crate::Options::spans) on.
pub fn span(&self, id: NodeId) -> Option<std::ops::Range<usize>> {
if self.is_removed(id) {
return None;
}
let p = self.positions.as_deref()?;
Positions::get(&p.nodes, id as usize).map(|at| p.range(at))
}
/// Where an element's end tag was written, when it had one: the end
/// tag whose handling closed it, `</p>` for a `<p>`, or a `</body>`.
pub fn end_span(&self, id: NodeId) -> Option<std::ops::Range<usize>> {
if self.is_removed(id) {
return None;
}
let p = self.positions.as_deref()?;
Positions::get(&p.ends, id as usize).map(|at| p.range(at))
}
/// Where an element's `i`th attribute, as [`Document::attrs`] lists
/// them, was written: from its name's first byte to the end of its
/// value, past a closing quote, or to the end of its name when it has
/// no value. An attribute an edit set has none; a reopened formatting
/// element's are where the original's were written.
pub fn attr_span(&self, id: NodeId, i: usize) -> Option<std::ops::Range<usize>> {
if self.is_removed(id) {
return None;
}
let p = self.positions.as_deref()?;
let e = self.element(id)?;
let at = e.attrs.start as usize + i;
if at >= e.attrs.end as usize {
return None;
}
Positions::get(&p.attrs, at).map(|at| p.range(at))
}
/// The line and column of a byte offset into the input the parse was
/// given, as an editor shows them: both start at 1, a line ends at an
/// LF, a CRLF or a lone CR, and the column counts characters (Unicode
/// scalar values), not bytes. Worked out from the source each time,
/// so it costs a pass over the text before `offset`.
pub fn line_col(&self, offset: usize) -> (usize, usize) {
// Back into the preprocessed source, where every line ends at an
// LF: past the skipped bytes and the CRs that were dropped.
let (skipped, crlf) = self
.positions
.as_deref()
.map_or((0, &[][..]), |p| (p.skipped as usize, &p.crlf[..]));
let o = offset.saturating_sub(skipped);
// The CRLFs wholly before `o`: the `i`th one's CR is at `lf + i`
// in the original, where `lf` is its LF's place in the source.
let (mut lo, mut hi) = (0, crlf.len());
while lo < hi {
let mid = (lo + hi) / 2;
if crlf[mid] as usize + mid + 1 < o {
lo = mid + 1;
} else {
hi = mid;
}
}
let at = (o - lo.min(o)).min(self.src.len());
let bytes = &self.src.as_bytes()[..at];
let line = 1 + memchr::memchr_iter(b'\n', bytes).count();
let start = memchr::memrchr(b'\n', bytes).map_or(0, |i| i + 1);
// Every byte but a UTF-8 continuation byte starts a character.
let column = 1 + bytes[start..]
.iter()
.filter(|&&b| (b as i8) >= -0x40)
.count();
(line, column)
}
/// The parse errors in the input, in source order: empty unless the
/// document was parsed with [`Options::errors`](crate::Options::errors)
/// on.
pub fn errors(&self) -> &[crate::SyntaxError] {
&self.errors
}
pub(crate) fn element(&self, id: NodeId) -> Option<&Element> {
match &self.nodes[id as usize].data {
Data::Element(e) => Some(e),
_ => None,
}
}
pub(crate) fn element_name(&self, e: &Element) -> &str {
match e.name {
Name::Tag => e.tag.name(),
Name::Svg(i) => crate::tree::SVG_TAG_NAMES[i as usize].1,
Name::Span(s) => self.str(s),
}
}
/// Inserts `child`, which has no parent, into `parent` before `before`,
/// or last when `before` is [`NONE`].
pub(crate) fn insert(&mut self, parent: NodeId, child: NodeId, before: NodeId) {
debug_assert!(
matches!(
self.nodes[parent as usize].data,
Data::Element(_) | Data::Document | Data::Fragment | Data::ShadowRoot { .. }
),
"node {parent} can't have children"
);
debug_assert!(
!matches!(
self.nodes[child as usize].data,
Data::Document | Data::Fragment | Data::ShadowRoot { .. }
),
"node {child} is a document, fragment or shadow root, never a child"
);
debug_assert!(
self.nodes[child as usize].parent == NONE
&& self.nodes[child as usize].prev == NONE
&& self.nodes[child as usize].next == NONE,
"node {child} is inserted while still linked"
);
debug_assert!(parent != child, "node {child} inserted into itself");
debug_assert!(
before == NONE || self.nodes[before as usize].parent == parent,
"node {before} isn't a child of {parent}"
);
let prev = if before == NONE {
self.nodes[parent as usize].last_child
} else {
self.nodes[before as usize].prev
};
{
let c = &mut self.nodes[child as usize];
c.parent = parent;
c.prev = prev;
c.next = before;
}
if prev == NONE {
self.nodes[parent as usize].first_child = child;
} else {
self.nodes[prev as usize].next = child;
}
if before == NONE {
self.nodes[parent as usize].last_child = child;
} else {
self.nodes[before as usize].prev = child;
}
debug_assert!(self.links_agree(child), "links around node {child}");
}
/// Takes `child` out of the tree, still usable: insert it again to move
/// it, subtree and all. To be done with a node for good, use
/// [`Document::remove`], after which its id reads as nothing.
pub fn detach(&mut self, child: NodeId) {
let Node {
parent, prev, next, ..
} = self.nodes[child as usize];
if parent == NONE {
debug_assert!(
prev == NONE && next == NONE,
"node {child} has siblings, no parent"
);
return;
}
debug_assert!(self.links_agree(child), "links around node {child}");
if prev == NONE {
self.nodes[parent as usize].first_child = next;
} else {
self.nodes[prev as usize].next = next;
}
if next == NONE {
self.nodes[parent as usize].last_child = prev;
} else {
self.nodes[next as usize].prev = prev;
}
let c = &mut self.nodes[child as usize];
c.parent = NONE;
c.prev = NONE;
c.next = NONE;
debug_assert!(
(prev == NONE || self.links_agree(prev)) && (next == NONE || self.links_agree(next)),
"links where node {child} was"
);
}
/// Whether `id`'s links and its neighbours' agree: its siblings point
/// back at it, and its parent's first and last child are it exactly
/// when it has no sibling on that side. Constant time, for the debug
/// assertions on every insert and removal; [`Document::check_tree`]
/// checks every node.
pub(crate) fn links_agree(&self, id: NodeId) -> bool {
let n = &self.nodes[id as usize];
if n.parent == NONE {
return n.prev == NONE && n.next == NONE;
}
let p = &self.nodes[n.parent as usize];
let prev_ok = if n.prev == NONE {
p.first_child == id
} else {
let s = &self.nodes[n.prev as usize];
s.next == id && s.parent == n.parent && p.first_child != id
};
let next_ok = if n.next == NONE {
p.last_child == id
} else {
let s = &self.nodes[n.next as usize];
s.prev == id && s.parent == n.parent && p.last_child != id
};
prev_ok && next_ok
}
/// Appends text to a text node, copying its old text to the end of the
/// buffer first if something else has been written after it.
/// Only the parser calls this: it may move the buffer.
pub(crate) fn append_text(&mut self, id: NodeId, text: &str) {
let Data::Text(span) = self.nodes[id as usize].data else {
unreachable!("not a text node")
};
let in_source = span.start & IN_SOURCE != 0;
let in_buf = span.start & (IN_SOURCE | IN_EXTRA) == 0;
let span =
if in_source && self.source_offset(text) == Some((span.end & !IN_SOURCE) as usize) {
// The next slice of the source: the span just grows.
debug_assert!((span.end & !IN_SOURCE) as usize + text.len() <= self.src.len());
Span {
start: span.start,
end: span.end + text.len() as u32,
}
} else if in_buf && span.end as usize == self.buf.len() {
self.buf.push_str(text);
Span {
start: span.start,
end: self.buf.len() as u32,
}
} else if span.start & IN_EXTRA != 0 {
// Already a string of its own: grows in place.
self.extras[(span.start & !IN_EXTRA) as usize].push_str(text);
span
} else {
// Something was written after it: move it to a string of its
// own, once, rather than copying it to the end of the buffer on
// every append (repeated <body x> made that quadratic).
let mut own = self.str(span).to_owned();
own.push_str(text);
let at = self.extras.len() as u32 | IN_EXTRA;
self.extras.push(own);
Span { start: at, end: at }
};
self.nodes[id as usize].data = Data::Text(span);
}
/// Checks every invariant of the tree, panicking at the first that
/// doesn't hold: every node's links agree with its parent's and its
/// siblings', only elements, documents and fragments have children,
/// each node is reached from the root at most once (template contents
/// included), each template's contents are a fragment of its own
/// that's nobody's child, the map of hosts agrees with the templates,
/// and every string and recorded position is inside its buffer.
///
/// Linear in the document's size, so it's for tests and fuzzing, not
/// for every edit; the parser and the edits assert the parts of this
/// they touch as they go. Only in builds with debug assertions.
#[cfg(debug_assertions)]
pub fn check_tree(&self) {
let len = self.nodes.len();
let span_ok = |s: Span| {
if s.start & IN_SOURCE != 0 {
let (a, b) = (s.start & !IN_SOURCE, s.end & !IN_SOURCE);
s.end & IN_SOURCE != 0
&& a <= b
&& self.src.is_char_boundary(a as usize)
&& self.src.is_char_boundary(b as usize)
} else if s.start & IN_EXTRA != 0 {
s.start == s.end && ((s.start & !IN_EXTRA) as usize) < self.extras.len()
} else {
s.start <= s.end
&& self.buf.is_char_boundary(s.start as usize)
&& self.buf.is_char_boundary(s.end as usize)
}
};
let mut contents_of = vec![NONE; len];
for (id, n) in self.nodes.iter().enumerate() {
let id = id as NodeId;
for link in [n.parent, n.first_child, n.last_child, n.prev, n.next] {
assert!(
link == NONE || (link as usize) < len,
"node {id} links past the end"
);
}
assert!(self.links_agree(id), "links around node {id}");
assert!(
!self.is_removed(id) || n.parent == NONE || self.is_removed(n.parent),
"removed node {id} is under {}, which isn't",
n.parent
);
assert_eq!(
n.first_child == NONE,
n.last_child == NONE,
"node {id} has a first child or a last, not both"
);
if n.first_child != NONE {
let first = &self.nodes[n.first_child as usize];
let last = &self.nodes[n.last_child as usize];
assert!(
first.parent == id && first.prev == NONE,
"node {id}'s first child"
);
assert!(
last.parent == id && last.next == NONE,
"node {id}'s last child"
);
assert!(
matches!(
n.data,
Data::Element(_)
| Data::Document
| Data::Fragment
| Data::ShadowRoot { .. }
),
"node {id} can't have children"
);
}
match n.data {
Data::Document | Data::Fragment => {
assert!(
n.parent == NONE,
"node {id}, a document or fragment, has a parent"
)
}
Data::ShadowRoot { host, .. } => {
assert!(n.parent == NONE, "shadow root {id} has a parent");
assert!(
self.element(host).is_some(),
"shadow root {id}'s host isn't an element"
);
assert_eq!(
self.shadow_roots.get(&host),
Some(&id),
"shadow root {id} isn't its host's"
);
}
Data::Element(e) => {
let name_ok = match e.name {
Name::Tag => true,
Name::Svg(i) => (i as usize) < crate::tree::SVG_TAG_NAMES.len(),
Name::Span(s) => span_ok(s),
};
assert!(name_ok, "node {id}'s name");
assert!(
e.attrs.start <= e.attrs.end && e.attrs.end as usize <= self.attrs.len(),
"node {id}'s attributes"
);
if e.contents != NONE {
let c = e.contents as usize;
assert!(
e.ns == Ns::Html && e.tag == Tag::Template,
"node {id} has contents but isn't a template"
);
assert!(
c < len && matches!(self.nodes[c].data, Data::Fragment),
"node {id}'s contents aren't a fragment"
);
assert!(contents_of[c] == NONE, "two templates have contents {c}");
contents_of[c] = id;
}
}
Data::Doctype {
name,
public_id,
system_id,
} => assert!(
span_ok(name) && span_ok(public_id) && span_ok(system_id),
"node {id}'s strings"
),
Data::Text(s) | Data::Comment(s) | Data::Cdata(s) => {
assert!(span_ok(s), "node {id}'s text")
}
Data::ProcessingInstruction { target, data } => {
assert!(span_ok(target) && span_ok(data), "node {id}'s strings")
}
}
}
for (i, a) in self.attrs.iter().enumerate() {
assert!(
span_ok(a.name) && span_ok(a.value),
"attribute {i}'s strings"
);
}
for (&host, &root) in &self.shadow_roots {
assert!(
matches!(self.nodes.get(root as usize).map(|n| n.data), Some(Data::ShadowRoot { host: h, .. }) if h == host),
"host {host}'s shadow root {root} doesn't name it"
);
}
for (&slot, nodes) in &self.manual_slots {
assert!(
self.element(slot).is_some(),
"manual slot {slot} isn't an element"
);
assert!(
nodes.iter().all(|&n| (n as usize) < len),
"manual slot {slot}'s nodes"
);
}
if let Some(hosts) = &self.hosts {
for (&contents, &host) in hosts {
assert_eq!(
contents_of.get(contents as usize),
Some(&host),
"the hosts map at {contents}"
);
}
let templates = contents_of.iter().filter(|&&h| h != NONE).count();
assert_eq!(hosts.len(), templates, "the hosts map misses a template");
}
// Down from the root, each node once: no cycle, no node in two
// places. Siblings are walked by their links, so a loop among
// them shows as a node seen twice.
let mut seen = vec![false; len];
let mut stack = vec![self.root()];
while let Some(id) = stack.pop() {
assert!(!seen[id as usize], "node {id} reached twice");
seen[id as usize] = true;
let n = &self.nodes[id as usize];
let mut child = n.first_child;
while child != NONE {
assert!(!seen[child as usize], "a loop among node {id}'s children");
assert_eq!(
self.nodes[child as usize].parent, id,
"node {child}'s parent"
);
stack.push(child);
child = self.nodes[child as usize].next;
if stack.len() > len {
panic!("a loop among node {id}'s children");
}
}
if let Data::Element(e) = n.data
&& e.contents != NONE
{
stack.push(e.contents);
}
if let Some(&root) = self.shadow_roots.get(&id) {
stack.push(root);
}
}
if let Some(p) = self.positions.as_deref() {
let src = self.src.len() as u32;
let in_src = |at: &[u32; 2]| *at == UNKNOWN || (at[0] <= at[1] && at[1] <= src);
assert!(
p.nodes.len() <= len && p.ends.len() <= len,
"positions past the nodes"
);
assert!(
p.attrs.len() <= self.attrs.len(),
"positions past the attributes"
);
for (i, at) in p.nodes.iter().chain(&p.ends).chain(&p.attrs).enumerate() {
assert!(
in_src(at),
"position {i}, {at:?}, outside {src} bytes of source"
);
}
assert!(p.crlf.windows(2).all(|w| w[0] < w[1]), "CRLFs out of order");
assert!(
p.crlf.last().is_none_or(|&lf| lf < src),
"a CRLF past the source"
);
let original = p.original(src);
for e in &self.errors {
assert!(
e.offset <= original,
"{e:?} past the input's {original} bytes"
);
}
}
}
pub fn parent(&self, id: NodeId) -> Option<NodeId> {
if self.is_removed(id) {
return None;
}
Some(self.nodes[id as usize].parent).filter(|&p| p != NONE)
}
pub fn first_child(&self, id: NodeId) -> Option<NodeId> {
if self.is_removed(id) {
return None;
}
Some(self.nodes[id as usize].first_child).filter(|&c| c != NONE)
}
pub fn prev_sibling(&self, id: NodeId) -> Option<NodeId> {
if self.is_removed(id) {
return None;
}
Some(self.nodes[id as usize].prev).filter(|&c| c != NONE)
}
pub fn last_child(&self, id: NodeId) -> Option<NodeId> {
if self.is_removed(id) {
return None;
}
Some(self.nodes[id as usize].last_child).filter(|&c| c != NONE)
}
pub fn next_sibling(&self, id: NodeId) -> Option<NodeId> {
if self.is_removed(id) {
return None;
}
Some(self.nodes[id as usize].next).filter(|&c| c != NONE)
}
pub fn children(&self, id: NodeId) -> impl Iterator<Item = NodeId> + '_ {
std::iter::successors(self.first_child(id), move |&c| self.next_sibling(c))
}
/// A template element's contents.
pub fn template_contents(&self, id: NodeId) -> Option<NodeId> {
if self.is_removed(id) {
return None;
}
self.element(id).map(|e| e.contents).filter(|&c| c != NONE)
}
/// Whether `id` was taken out with [`Document::remove`], or was inside
/// a node that was. A removed node reads as nothing: the readers that
/// return an `Option` give `None`, it has no children, attributes or
/// text, it serializes to `""`, it matches no selector, and inserting
/// it, or anything into it, is an error.
pub fn is_removed(&self, id: NodeId) -> bool {
self.dead
.get(id as usize / 64)
.is_some_and(|w| w >> (id % 64) & 1 == 1)
}
pub(crate) fn mark_removed(&mut self, id: NodeId) {
let word = id as usize / 64;
if self.dead.len() <= word {
self.dead.resize(word + 1, 0);
}
self.dead[word] |= 1 << (id % 64);
}
/// What `id` is. A removed node keeps its kind and an element its
/// name, but its text, a comment's, and a processing instruction's
/// data read as empty, as it has no children or attributes.
pub fn kind(&self, id: NodeId) -> Kind<'_> {
let kind = self.kind_of(id);
if !self.is_removed(id) {
return kind;
}
match kind {
Kind::Text(_) => Kind::Text(""),
Kind::Comment(_) => Kind::Comment(""),
Kind::Cdata(_) => Kind::Cdata(""),
Kind::ProcessingInstruction { target, .. } => {
Kind::ProcessingInstruction { target, data: "" }
}
other => other,
}
}
fn kind_of(&self, id: NodeId) -> Kind<'_> {
match &self.nodes[id as usize].data {
Data::Document => Kind::Document,
Data::Fragment => Kind::Fragment,
Data::ShadowRoot { init, .. } => Kind::ShadowRoot(*init),
Data::Doctype {
name,
public_id,
system_id,
} => Kind::Doctype {
name: self.str(*name),
public_id: self.str(*public_id),
system_id: self.str(*system_id),
},
Data::Element(e) => Kind::Element {
ns: e.ns,
name: self.element_name(e),
},
Data::Text(s) => Kind::Text(self.str(*s)),
Data::Comment(s) => Kind::Comment(self.str(*s)),
Data::Cdata(s) => Kind::Cdata(self.str(*s)),
Data::ProcessingInstruction { target, data } => Kind::ProcessingInstruction {
target: self.str(*target),
data: self.str(*data),
},
}
}
/// An element's attributes, in source order, as (namespace, local name,
/// value).
pub fn attrs(&self, id: NodeId) -> impl Iterator<Item = (AttrNs, &str, &str)> + '_ {
let attrs = if self.is_removed(id) {
&[][..]
} else {
self.attrs_of(id)
};
attrs.iter().map(|a| {
let name = self.str(a.name);
let name = match a.ns {
AttrNs::XLink | AttrNs::Xml | AttrNs::Xmlns => {
name.split_once(':').map_or(name, |(_, local)| local)
}
_ => name,
};
(a.ns, name, self.str(a.value))
})
}
/// An element's attributes with their names as written: `xlink:href`
/// rather than `href`, and an XML attribute's prefix kept.
pub fn attrs_written(&self, id: NodeId) -> impl Iterator<Item = (AttrNs, &str, &str)> + '_ {
let ns = self.element(id).map_or(Ns::Html, |e| e.ns);
let attrs = if self.is_removed(id) {
&[][..]
} else {
self.attrs_of(id)
};
attrs
.iter()
.map(move |a| (a.ns, self.written_name(ns, a), self.str(a.value)))
}
pub(crate) fn attrs_of(&self, id: NodeId) -> &[Attr] {
let range = self
.element(id)
.map_or(0..0, |e| e.attrs.start as usize..e.attrs.end as usize);
&self.attrs[range]
}
/// An attribute's name as the spec writes it on an element in `ns`:
/// SVG's `viewBox` and MathML's `definitionURL` get their case back.
pub(crate) fn written_name(&self, ns: Ns, a: &Attr) -> &str {
let name = self.str(a.name);
match (ns, a.ns) {
(Ns::Svg, AttrNs::None) => crate::tree::svg_attr_name(name).unwrap_or(name),
(Ns::MathMl, AttrNs::None) if name == "definitionurl" => "definitionURL",
_ => name,
}
}
/// An element's name lowercased, without allocating: SVG's
/// `foreignObject` gives `foreignobject`, whether it was parsed or made
/// with [`Document::create_element`]. An XML element's name as written,
/// and so is a foreign element made under a name in another case
/// (`LINEARGRADIENT`), which the DOM treats as a different element.
pub fn lower_name(&self, id: NodeId) -> Option<&str> {
if self.is_removed(id) {
return None;
}
let e = self.element(id)?;
Some(match e.name {
Name::Tag => e.tag.name(),
Name::Svg(i) => crate::tree::SVG_TAG_NAMES[i as usize].0,
Name::Span(s) => self.str(s),
})
}
/// The parser's number for an element's tag, from its lowercase name:
/// what [`crate::tag_number`] gives for that name. HTML, SVG and MathML
/// elements alike get one, so an SVG `<title>` shares HTML `title`'s;
/// an XML element never does.
pub fn known_tag(&self, id: NodeId) -> Option<u8> {
if self.is_removed(id) {
return None;
}
self.element(id)
.filter(|e| e.tag != Tag::Other && e.ns != Ns::Xml)
.map(|e| e.tag as u8)
}
}
#[cfg(test)]
mod tests {
use crate::{AttrNs, parse};
/// Names as tokenized, lowercase and without a prefix, from attrs();
/// as the spec writes them, case fixed and prefix kept, from
/// attrs_written() and the serializer.
#[test]
fn foreign_attributes_have_a_lowercase_name_and_a_written_one() {
let doc = parse(
"<svg viewBox=0 xmlns:xlink=x><use xlink:href=#i></use></svg>\
<math definitionURL=d></math>",
false,
);
let svg = (0..doc.len() as u32)
.find(|&n| doc.lower_name(n) == Some("svg"))
.unwrap();
let use_ = doc.first_child(svg).unwrap();
let math = doc.next_sibling(svg).unwrap();
let names = |n, written: bool| -> Vec<String> {
if written {
doc.attrs_written(n)
.map(|(_, a, _)| a.to_string())
.collect()
} else {
doc.attrs(n).map(|(_, a, _)| a.to_string()).collect()
}
};
assert_eq!(names(svg, false), ["viewbox", "xlink"]);
assert_eq!(names(svg, true), ["viewBox", "xmlns:xlink"]);
assert_eq!(names(use_, false), ["href"]);
assert_eq!(doc.attrs(use_).next().unwrap().0, AttrNs::XLink);
assert_eq!(names(use_, true), ["xlink:href"]);
assert_eq!(names(math, false), ["definitionurl"]);
assert_eq!(names(math, true), ["definitionURL"]);
assert!(doc.outer_html(svg).starts_with(r#"<svg viewBox="0""#));
assert!(doc.outer_html(math).contains("definitionURL"));
}
#[test]
fn an_edit_finds_a_foreign_attribute_by_its_written_name() {
let mut doc = parse("<svg viewBox=0></svg>", false);
let svg = (0..doc.len() as u32)
.find(|&n| doc.lower_name(n) == Some("svg"))
.unwrap();
doc.set_attr(svg, "viewBox", "1");
assert_eq!(doc.outer_html(svg), r#"<svg viewBox="1"></svg>"#);
assert!(doc.remove_attr(svg, "viewBox"));
assert_eq!(doc.outer_html(svg), "<svg></svg>");
}
}