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
use std::sync::{Arc, LazyLock, Mutex};
use crate::dom::{DOMException, document::DocumentRef, document_type::DocumentTypeRef};
/// Implementation of `DOMImplementationRegistry` that is described
/// at [`1.3.7 Bootstrapping`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Bootstrap)
/// in the DOM Level 3 Core specification.
pub struct DOMImplementationRegistry {
sources: Mutex<Vec<Arc<dyn DOMImplementationSource>>>,
}
impl DOMImplementationRegistry {
pub fn add_source(&mut self, source: Arc<dyn DOMImplementationSource>) {
let mut sources = self.sources.lock().unwrap();
if let Some(pos) = sources.iter().position(|s| Arc::ptr_eq(s, &source)) {
sources.remove(pos);
}
sources.push(source);
}
/// # Specification
/// [1.3.7 Bootstrapping](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Bootstrap)
/// ```text
/// the DOMImplementationRegistry provides a getDOMImplementation method accepting
/// a features string, which is passed to every known DOMImplementationSource until
/// a suitable DOMImplementation is found and returned.
/// ```
pub fn get_dom_implementation(&self, features: &str) -> Option<Arc<dyn DOMImplementation>> {
let sources = self.sources.lock().unwrap();
// Search from the end to give preference to user-added sources.
sources
.iter()
.rev()
.find_map(|source| source.get_dom_implementation(features))
}
/// # Specification
/// [1.3.7 Bootstrapping](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Bootstrap)
/// ```text
/// The DOMImplementationRegistry also provides a getDOMImplementationList method
/// accepting a features string, which is passed to every known DOMImplementationSource,
/// and returns a list of suitable DOMImplementations.
/// ```
pub fn get_dom_implementation_list(&self, features: &str) -> Arc<dyn DOMImplementationList> {
let sources = self.sources.lock().unwrap();
let mut res = vec![];
// Search from the end to give preference to user-added sources.
for source in sources.iter().rev() {
let list = source.get_dom_implementation_list(features);
for i in 0..list.length() {
let implementation = list.item(i).unwrap();
if !res.iter().any(|p| Arc::ptr_eq(&implementation, p)) {
res.push(implementation);
}
}
}
Arc::new(res) as _
}
}
pub static DOM_IMPLEMENTATION_REGISTRY: LazyLock<DOMImplementationRegistry> =
LazyLock::new(|| DOMImplementationRegistry {
sources: Mutex::new(vec![(*DEFAULT_DOM_IMPLEMENTATION_SOURCE).clone()]),
});
/// Implementation of [`DOMImpelmentationSource`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-DOMImplementationSource) interface.
pub trait DOMImplementationSource: Send + Sync + 'static {
/// Implementation of [`getDOMImplementation`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-getDOMImpl) method.
///
/// # Specification
/// ```text
/// A method to request the first DOM implementation that supports the specified features.
///
/// Parameters
/// features of type DOMString
/// A string that specifies which features and versions are required. This is a
/// space separated list in which each feature is specified by its name optionally
/// followed by a space and a version number.
/// This method returns the first item of the list returned by
/// getDOMImplementationList.
/// As an example, the string "XML 3.0 Traversal +Events 2.0" will request
/// a DOM implementation that supports the module "XML" for its 3.0 version,
/// a module that support of the "Traversal" module for any version, and the module
/// "Events" for its 2.0 version. The module "Events" must be accessible using
/// the method Node.getFeature() and DOMImplementation.getFeature().
///
/// Return Value
/// DOMImplementation The first DOM implementation that support the desired features,
/// or null if this source has none.
///
/// No Exceptions
/// ```
fn get_dom_implementation(&self, features: &str) -> Option<Arc<dyn DOMImplementation>>;
/// Implementation of [`getDOMImplementationList`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-getDOMImpls) method.
///
/// # Specification
/// ```text
/// A method to request a list of DOM implementations that support the specified features
/// and versions, as specified in DOM Features.
///
/// Parameters
/// features of type DOMString
/// A string that specifies which features and versions are required.
/// This is a space separated list in which each feature is specified by its name
/// optionally followed by a space and a version number. This is something like:
/// "XML 3.0 Traversal +Events 2.0"
///
/// Return Value
/// DOMImplementationList A list of DOM implementations that support the desired
/// features.
///
/// No Exceptions
/// ```
fn get_dom_implementation_list(&self, features: &str) -> Arc<dyn DOMImplementationList>;
}
/// Implementation of [`DOMImpelmentationSource`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-DOMImplementationSource) interface.
pub struct DefaultDOMImplementationSource;
impl DOMImplementationSource for DefaultDOMImplementationSource {
fn get_dom_implementation(&self, features: &str) -> Option<Arc<dyn DOMImplementation>> {
let implementation = (*DEFAULT_DOM_IMPLEMENTATION).clone();
let mut features = features.split_ascii_whitespace().peekable();
while let Some(feature) = features.next() {
if feature.is_empty() {
continue;
}
while features.next_if(|f| f.is_empty()).is_some() {}
let ok = if let Some(version) = features.next_if(|version| {
version.split_once('.').is_some_and(|(major, minor)| {
major.chars().all(char::is_numeric) && minor.chars().all(char::is_numeric)
})
}) {
implementation.has_feature(feature, Some(version))
} else {
implementation.has_feature(feature, None)
};
if !ok {
return None;
}
}
Some(implementation)
}
fn get_dom_implementation_list(&self, features: &str) -> Arc<dyn DOMImplementationList> {
let mut res = vec![];
if let Some(implementation) = self.get_dom_implementation(features) {
res.push(implementation);
}
Arc::new(res) as _
}
}
pub static DEFAULT_DOM_IMPLEMENTATION_SOURCE: LazyLock<Arc<dyn DOMImplementationSource>> =
LazyLock::new(|| Arc::new(DefaultDOMImplementationSource) as _);
/// Implementation for [`DOMImplementation`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-102161490) interface.
pub trait DOMImplementation: Send + Sync + 'static {
/// Implementation of [`hasFeature`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-5CED94D7) method.
///
/// # Specification
/// ```text
/// Test if the DOM implementation implements a specific feature and version,
/// as specified in DOM Features.
///
/// Parameters
/// feature of type DOMString
/// The name of the feature to test.
/// version of type DOMString
/// This is the version number of the feature to test.
///
/// Return Value
/// boolean true if the feature is implemented in the specified version,
/// false otherwise.
/// ```
fn has_feature(&self, feature: &str, version: Option<&str>) -> bool;
/// Implementation of [`createDocumentType`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Level-2-Core-DOM-createDocType) method.
///
/// # Specification
/// ```text
/// Creates an empty DocumentType node. Entity declarations and notations are not made available.
/// Entity reference expansions and default attribute additions do not occur..
///
/// Parameters
/// qualifiedName of type DOMString
/// The qualified name of the document type to be created.
/// publicId of type DOMString
/// The external subset public identifier.
/// systemId of type DOMString
/// The external subset system identifier.
///
/// Return Value
/// DocumentType A new DocumentType node with Node.ownerDocument set to null.
///
/// Exceptions
/// DOMException
/// INVALID_CHARACTER_ERR: Raised if the specified qualified name is not an XML name
/// according to [XML 1.0].
/// NAMESPACE_ERR: Raised if the qualifiedName is malformed.
/// NOT_SUPPORTED_ERR: May be raised if the implementation does not support
/// the feature "XML" and the language exposed through
/// the Document does not support XML Namespaces
/// (such as [HTML 4.01]).
/// ```
fn create_document_type(
&self,
qualified_name: &str,
public_id: Option<&str>,
system_id: Option<&str>,
) -> Result<DocumentTypeRef, DOMException>;
/// Implementation of [`createDocument`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Level-2-Core-DOM-createDocument) method.
///
/// # Specification
/// ```text
/// Creates an empty DocumentType node. Entity declarations and notations are not made
/// available. Entity reference expansions and default attribute additions do not occur.
///
/// Parameters
/// qualifiedName of type DOMString
/// The qualified name of the document type to be created.
/// publicId of type DOMString
/// The external subset public identifier.
/// systemId of type DOMString
/// The external subset system identifier.
///
/// Return Value
/// DocumentType A new DocumentType node with Node.ownerDocument set to null.
///
/// Exceptions
/// DOMException
/// INVALID_CHARACTER_ERR: Raised if the specified qualified name is not an XML name
/// according to [XML 1.0].
/// NAMESPACE_ERR: Raised if the qualifiedName is malformed.
/// NOT_SUPPORTED_ERR: May be raised if the implementation does not support the
/// feature "XML" and the language exposed through the Document
/// does not support XML Namespaces (such as [HTML 4.01]).
/// ```
fn create_document(
&self,
namespace_uri: Option<&str>,
qualified_name: Option<&str>,
doctype: Option<DocumentTypeRef>,
) -> Result<DocumentRef, DOMException>;
/// Implementation of [`getFeature`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-DOMImplementation3-getFeature) method.
///
/// # Specification
/// ```text
/// This method returns a specialized object which implements the specialized APIs of the specified feature and version, as specified in DOM Features. The specialized object may also be obtained by using binding-specific casting methods but is not necessarily expected to, as discussed in Mixed DOM Implementations. This method also allow the implementation to provide specialized objects which do not support the DOMImplementation interface.
///
/// Parameters
/// feature of type DOMString
/// The name of the feature requested. Note that any plus sign "+" prepended to the name of the feature will be ignored since it is not significant in the context of this method.
/// version of type DOMString
/// This is the version number of the feature to test.
///
/// Return Value
/// DOMObject Returns an object which implements the specialized APIs of the
/// specified feature and version, if any, or null if there is no object
/// which implements interfaces associated with that feature. If the
/// DOMObject returned by this method implements the DOMImplementation
/// interface, it must delegate to the primary core DOMImplementation
/// and not return results inconsistent with the primary core
/// DOMImplementation such as hasFeature, getFeature, etc.
///
/// No Exceptions
/// ```
fn get_feature(
&self,
feature: &str,
version: Option<&str>,
) -> Option<Arc<dyn DOMImplementation>>;
}
/// Implementation for [`DOMImplementation`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-102161490) interface.
pub struct DefaultDOMImplementation;
impl DOMImplementation for DefaultDOMImplementation {
fn has_feature(&self, feature: &str, version: Option<&str>) -> bool {
(feature.eq_ignore_ascii_case("Core")
&& version.is_none_or(|version| matches!(version, "" | "1.0" | "2.0" | "3.0")))
|| (feature.eq_ignore_ascii_case("XML")
&& version.is_none_or(|version| matches!(version, "" | "1.0" | "2.0" | "3.0")))
|| (feature.eq_ignore_ascii_case("XMLVersion")
&& version.is_none_or(|version| matches!(version, "" | "1.0")))
}
fn create_document_type(
&self,
qualified_name: &str,
public_id: Option<&str>,
system_id: Option<&str>,
) -> Result<DocumentTypeRef, DOMException> {
DocumentTypeRef::new(qualified_name, public_id, system_id)
}
fn create_document(
&self,
namespace_uri: Option<&str>,
qualified_name: Option<&str>,
doctype: Option<DocumentTypeRef>,
) -> Result<DocumentRef, DOMException> {
DocumentRef::new(namespace_uri, qualified_name, doctype)
}
fn get_feature(
&self,
feature: &str,
version: Option<&str>,
) -> Option<Arc<dyn DOMImplementation>> {
self.has_feature(feature, version)
.then(|| Arc::new(Self) as Arc<dyn DOMImplementation>)
}
}
pub static DEFAULT_DOM_IMPLEMENTATION: LazyLock<Arc<dyn DOMImplementation>> =
LazyLock::new(|| Arc::new(DefaultDOMImplementation) as Arc<dyn DOMImplementation>);
/// Implementation of [`DOMImplementationList`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-DOMImplementationList) interface.
pub trait DOMImplementationList {
/// Implementation of [`item`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-DOMImplementationList-item) method.
///
/// # Specification
/// ```text
/// Returns the indexth item in the collection. If index is greater than or equal to
/// the number of DOMImplementations in the list, this returns null.
///
/// Parameters
/// index of type unsigned long
/// Index into the collection.
///
/// Return Value
/// DOMImplementation The DOMImplementation at the indexth position in the
/// DOMImplementationList, or null if that is not a valid index.
///
/// No Exceptions
/// ```
fn item(&self, index: usize) -> Option<Arc<dyn DOMImplementation>>;
/// Implementation of [`length`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-DOMImplementationList-length) attribute.
///
/// # Specification
/// ```text
/// length of type unsigned long, readonly
/// The number of DOMImplementations in the list. The range of valid child node
/// indices is 0 to length-1 inclusive.
/// ```
fn length(&self) -> usize;
}
impl DOMImplementationList for Vec<Arc<dyn DOMImplementation>> {
fn item(&self, index: usize) -> Option<Arc<dyn DOMImplementation>> {
self.get(index).cloned()
}
fn length(&self) -> usize {
self.len()
}
}