@@ -32,9 +32,9 @@ pub const Interfaces = .{
32
32
33
33
pub const AbstractRange = struct {
34
34
collapsed : bool ,
35
- end_container : * parser.Node ,
35
+ end_node : * parser.Node ,
36
36
end_offset : u32 ,
37
- start_container : * parser.Node ,
37
+ start_node : * parser.Node ,
38
38
start_offset : u32 ,
39
39
40
40
pub fn updateCollapsed (self : * AbstractRange ) void {
@@ -47,15 +47,15 @@ pub const AbstractRange = struct {
47
47
}
48
48
49
49
pub fn get_endContainer (self : * const AbstractRange ) ! NodeUnion {
50
- return Node .toInterface (self .end_container );
50
+ return Node .toInterface (self .end_node );
51
51
}
52
52
53
53
pub fn get_endOffset (self : * const AbstractRange ) u32 {
54
54
return self .end_offset ;
55
55
}
56
56
57
57
pub fn get_startContainer (self : * const AbstractRange ) ! NodeUnion {
58
- return Node .toInterface (self .start_container );
58
+ return Node .toInterface (self .start_node );
59
59
}
60
60
61
61
pub fn get_startOffset (self : * const AbstractRange ) u32 {
@@ -69,15 +69,20 @@ pub const Range = struct {
69
69
70
70
proto : AbstractRange ,
71
71
72
+ pub const _START_TO_START = 0 ;
73
+ pub const _START_TO_END = 1 ;
74
+ pub const _END_TO_END = 2 ;
75
+ pub const _END_TO_START = 3 ;
76
+
72
77
// The Range() constructor returns a newly created Range object whose start
73
78
// and end is the global Document object.
74
79
// https://developer.mozilla.org/en-US/docs/Web/API/Range/Range
75
80
pub fn constructor (page : * Page ) Range {
76
81
const proto : AbstractRange = .{
77
82
.collapsed = true ,
78
- .end_container = parser .documentHTMLToNode (page .window .document ),
83
+ .end_node = parser .documentHTMLToNode (page .window .document ),
79
84
.end_offset = 0 ,
80
- .start_container = parser .documentHTMLToNode (page .window .document ),
85
+ .start_node = parser .documentHTMLToNode (page .window .document ),
81
86
.start_offset = 0 ,
82
87
};
83
88
@@ -87,11 +92,11 @@ pub const Range = struct {
87
92
pub fn _setStart (self : * Range , node : * parser.Node , offset_ : i32 ) ! void {
88
93
try ensureValidOffset (node , offset_ );
89
94
const offset : u32 = @intCast (offset_ );
90
- const position = compare (node , offset , self .proto .start_container , self .proto .start_offset ) catch | err | switch (err ) {
95
+ const position = compare (node , offset , self .proto .start_node , self .proto .start_offset ) catch | err | switch (err ) {
91
96
error .WrongDocument = > blk : {
92
97
// allow a node with a different root than the current, or
93
98
// a disconnected one. Treat it as if it's "after", so that
94
- // we also update the end_offset and end_container .
99
+ // we also update the end_offset and end_node .
95
100
break :blk 1 ;
96
101
},
97
102
else = > return err ,
@@ -101,34 +106,34 @@ pub const Range = struct {
101
106
// if we're setting the node after the current start, the end must
102
107
// be set too.
103
108
self .proto .end_offset = offset ;
104
- self .proto .end_container = node ;
109
+ self .proto .end_node = node ;
105
110
}
106
- self .proto .start_container = node ;
111
+ self .proto .start_node = node ;
107
112
self .proto .start_offset = offset ;
108
113
self .proto .updateCollapsed ();
109
114
}
110
115
111
116
pub fn _setStartBefore (self : * Range , node : * parser.Node ) ! void {
112
117
const parent , const index = try getParentAndIndex (node );
113
- self .proto .start_container = parent ;
118
+ self .proto .start_node = parent ;
114
119
self .proto .start_offset = index ;
115
120
}
116
121
117
122
pub fn _setStartAfter (self : * Range , node : * parser.Node ) ! void {
118
123
const parent , const index = try getParentAndIndex (node );
119
- self .proto .start_container = parent ;
124
+ self .proto .start_node = parent ;
120
125
self .proto .start_offset = index + 1 ;
121
126
}
122
127
123
128
pub fn _setEnd (self : * Range , node : * parser.Node , offset_ : i32 ) ! void {
124
129
try ensureValidOffset (node , offset_ );
125
130
const offset : u32 = @intCast (offset_ );
126
131
127
- const position = compare (node , offset , self .proto .start_container , self .proto .start_offset ) catch | err | switch (err ) {
132
+ const position = compare (node , offset , self .proto .start_node , self .proto .start_offset ) catch | err | switch (err ) {
128
133
error .WrongDocument = > blk : {
129
134
// allow a node with a different root than the current, or
130
135
// a disconnected one. Treat it as if it's "before", so that
131
- // we also update the end_offset and end_container .
136
+ // we also update the end_offset and end_node .
132
137
break :blk -1 ;
133
138
},
134
139
else = > return err ,
@@ -138,23 +143,23 @@ pub const Range = struct {
138
143
// if we're setting the node before the current start, the start
139
144
// must be set too.
140
145
self .proto .start_offset = offset ;
141
- self .proto .start_container = node ;
146
+ self .proto .start_node = node ;
142
147
}
143
148
144
- self .proto .end_container = node ;
149
+ self .proto .end_node = node ;
145
150
self .proto .end_offset = offset ;
146
151
self .proto .updateCollapsed ();
147
152
}
148
153
149
154
pub fn _setEndBefore (self : * Range , node : * parser.Node ) ! void {
150
155
const parent , const index = try getParentAndIndex (node );
151
- self .proto .end_container = parent ;
156
+ self .proto .end_node = parent ;
152
157
self .proto .end_offset = index ;
153
158
}
154
159
155
160
pub fn _setEndAfter (self : * Range , node : * parser.Node ) ! void {
156
161
const parent , const index = try getParentAndIndex (node );
157
- self .proto .end_container = parent ;
162
+ self .proto .end_node = parent ;
158
163
self .proto .end_offset = index + 1 ;
159
164
}
160
165
@@ -166,9 +171,9 @@ pub const Range = struct {
166
171
}
167
172
168
173
pub fn _selectNodeContents (self : * Range , node : * parser.Node ) ! void {
169
- self .proto .start_container = node ;
174
+ self .proto .start_node = node ;
170
175
self .proto .start_offset = 0 ;
171
- self .proto .end_container = node ;
176
+ self .proto .end_node = node ;
172
177
173
178
// Set end_offset
174
179
switch (try parser .nodeType (node )) {
@@ -196,16 +201,16 @@ pub const Range = struct {
196
201
return .{
197
202
.proto = .{
198
203
.collapsed = self .proto .collapsed ,
199
- .end_container = self .proto .end_container ,
204
+ .end_node = self .proto .end_node ,
200
205
.end_offset = self .proto .end_offset ,
201
- .start_container = self .proto .start_container ,
206
+ .start_node = self .proto .start_node ,
202
207
.start_offset = self .proto .start_offset ,
203
208
},
204
209
};
205
210
}
206
211
207
212
pub fn _comparePoint (self : * const Range , node : * parser.Node , offset_ : i32 ) ! i32 {
208
- const start = self .proto .start_container ;
213
+ const start = self .proto .start_node ;
209
214
if (try parser .nodeGetRootNode (start ) != try parser .nodeGetRootNode (node )) {
210
215
// WPT really wants this error to be first. Later, when we check
211
216
// if the relative position is 'disconnected', it'll also catch this
@@ -225,7 +230,7 @@ pub const Range = struct {
225
230
return -1 ;
226
231
}
227
232
228
- if (try compare (node , offset , self .proto .end_container , self .proto .end_offset ) == 1 ) {
233
+ if (try compare (node , offset , self .proto .end_node , self .proto .end_offset ) == 1 ) {
229
234
return 1 ;
230
235
}
231
236
@@ -240,7 +245,7 @@ pub const Range = struct {
240
245
}
241
246
242
247
pub fn _intersectsNode (self : * const Range , node : * parser.Node ) ! bool {
243
- const start_root = try parser .nodeGetRootNode (self .proto .start_container );
248
+ const start_root = try parser .nodeGetRootNode (self .proto .start_node );
244
249
const node_root = try parser .nodeGetRootNode (node );
245
250
if (start_root != node_root ) {
246
251
return false ;
@@ -251,19 +256,29 @@ pub const Range = struct {
251
256
else = > return err ,
252
257
};
253
258
254
- if (try compare (parent , index + 1 , self .proto .start_container , self .proto .start_offset ) != 1 ) {
259
+ if (try compare (parent , index + 1 , self .proto .start_node , self .proto .start_offset ) != 1 ) {
255
260
// node isn't after start, can't intersect
256
261
return false ;
257
262
}
258
263
259
- if (try compare (parent , index , self .proto .end_container , self .proto .end_offset ) != -1 ) {
264
+ if (try compare (parent , index , self .proto .end_node , self .proto .end_offset ) != -1 ) {
260
265
// node isn't before end, can't intersect
261
266
return false ;
262
267
}
263
268
264
269
return true ;
265
270
}
266
271
272
+ pub fn _compareBoundaryPoints (self : * const Range , how : i32 , other : * const Range ) ! i32 {
273
+ return switch (how ) {
274
+ _START_TO_START = > compare (self .proto .start_node , self .proto .start_offset , other .proto .start_node , other .proto .start_offset ),
275
+ _START_TO_END = > compare (self .proto .start_node , self .proto .start_offset , other .proto .end_node , other .proto .end_offset ),
276
+ _END_TO_END = > compare (self .proto .end_node , self .proto .end_offset , other .proto .end_node , other .proto .end_offset ),
277
+ _END_TO_START = > compare (self .proto .end_node , self .proto .end_offset , other .proto .start_node , other .proto .start_offset ),
278
+ else = > error .NotSupported , // this is the correct DOM Exception to return
279
+ };
280
+ }
281
+
267
282
// The Range.detach() method does nothing. It used to disable the Range
268
283
// object and enable the browser to release associated resources. The
269
284
// method has been kept for compatibility.
0 commit comments