lobzique 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/Cargo.lock +520 -0
- data/Cargo.toml +11 -0
- data/LICENSE-NOKOGIRI.md +15 -0
- data/LICENSE.txt +21 -0
- data/README.md +144 -0
- data/crates/lobzique-core/Cargo.toml +14 -0
- data/crates/lobzique-core/src/css/mod.rs +127 -0
- data/crates/lobzique-core/src/css/parser.rs +805 -0
- data/crates/lobzique-core/src/css/tables.rs +282 -0
- data/crates/lobzique-core/src/css/tests.rs +476 -0
- data/crates/lobzique-core/src/css/tokenizer.rs +215 -0
- data/crates/lobzique-core/src/css/translate.rs +806 -0
- data/crates/lobzique-core/src/css/write.rs +370 -0
- data/crates/lobzique-core/src/dom/attribute.rs +144 -0
- data/crates/lobzique-core/src/dom/check.rs +160 -0
- data/crates/lobzique-core/src/dom/copy.rs +229 -0
- data/crates/lobzique-core/src/dom/mod.rs +298 -0
- data/crates/lobzique-core/src/dom/namespace.rs +134 -0
- data/crates/lobzique-core/src/dom/node.rs +129 -0
- data/crates/lobzique-core/src/dom/path.rs +116 -0
- data/crates/lobzique-core/src/dom/reparent.rs +308 -0
- data/crates/lobzique-core/src/dom/tests.rs +1057 -0
- data/crates/lobzique-core/src/dom/text.rs +159 -0
- data/crates/lobzique-core/src/dom/tree.rs +446 -0
- data/crates/lobzique-core/src/html5/mod.rs +28 -0
- data/crates/lobzique-core/src/html5/parse.rs +838 -0
- data/crates/lobzique-core/src/html5/serialize.rs +181 -0
- data/crates/lobzique-core/src/html5/tests.rs +548 -0
- data/crates/lobzique-core/src/lib.rs +12 -0
- data/crates/lobzique-core/src/work.rs +26 -0
- data/crates/lobzique-core/src/xml/mod.rs +8 -0
- data/crates/lobzique-core/src/xml/serialize.rs +681 -0
- data/crates/lobzique-core/src/xml/tests.rs +174 -0
- data/crates/lobzique-core/src/xpath/ast.rs +415 -0
- data/crates/lobzique-core/src/xpath/chars.rs +139 -0
- data/crates/lobzique-core/src/xpath/eval.rs +1305 -0
- data/crates/lobzique-core/src/xpath/mod.rs +118 -0
- data/crates/lobzique-core/src/xpath/parse.rs +729 -0
- data/crates/lobzique-core/src/xpath/tests.rs +921 -0
- data/ext/lobzique/Cargo.toml +22 -0
- data/ext/lobzique/build.rs +5 -0
- data/ext/lobzique/clippy.toml +15 -0
- data/ext/lobzique/extconf.rb +6 -0
- data/ext/lobzique/src/css/mod.rs +9 -0
- data/ext/lobzique/src/css/parser.rs +44 -0
- data/ext/lobzique/src/css/selector.rs +101 -0
- data/ext/lobzique/src/html4/document.rs +31 -0
- data/ext/lobzique/src/html4/mod.rs +7 -0
- data/ext/lobzique/src/html5/mod.rs +16 -0
- data/ext/lobzique/src/html5ever.rs +278 -0
- data/ext/lobzique/src/lib.rs +45 -0
- data/ext/lobzique/src/support.rs +156 -0
- data/ext/lobzique/src/xml/attr.rs +27 -0
- data/ext/lobzique/src/xml/cdata.rs +33 -0
- data/ext/lobzique/src/xml/comment.rs +34 -0
- data/ext/lobzique/src/xml/document.rs +199 -0
- data/ext/lobzique/src/xml/document_fragment.rs +27 -0
- data/ext/lobzique/src/xml/dtd.rs +37 -0
- data/ext/lobzique/src/xml/mod.rs +44 -0
- data/ext/lobzique/src/xml/namespace.rs +56 -0
- data/ext/lobzique/src/xml/node.rs +944 -0
- data/ext/lobzique/src/xml/node_set.rs +20 -0
- data/ext/lobzique/src/xml/syntax_error.rs +33 -0
- data/ext/lobzique/src/xml/text.rs +44 -0
- data/ext/lobzique/src/xml/xpath_context.rs +305 -0
- data/lib/lobzique/action_text.rb +29 -0
- data/lib/lobzique/class_resolver.rb +33 -0
- data/lib/lobzique/css/selector_cache.rb +36 -0
- data/lib/lobzique/css/syntax_error.rb +12 -0
- data/lib/lobzique/css/xpath_visitor.rb +60 -0
- data/lib/lobzique/css.rb +57 -0
- data/lib/lobzique/extension.rb +31 -0
- data/lib/lobzique/html.rb +14 -0
- data/lib/lobzique/html4/document.rb +235 -0
- data/lib/lobzique/html4/document_fragment.rb +75 -0
- data/lib/lobzique/html4/encoding_reader.rb +374 -0
- data/lib/lobzique/html4.rb +35 -0
- data/lib/lobzique/html5/document.rb +106 -0
- data/lib/lobzique/html5/document_fragment.rb +113 -0
- data/lib/lobzique/html5/node.rb +66 -0
- data/lib/lobzique/html5.rb +92 -0
- data/lib/lobzique/html5ever.rb +14 -0
- data/lib/lobzique/rails_dom_testing.rb +95 -0
- data/lib/lobzique/sanitizer/concerns.rb +110 -0
- data/lib/lobzique/sanitizer/document.rb +14 -0
- data/lib/lobzique/sanitizer/document_fragment.rb +12 -0
- data/lib/lobzique/sanitizer/escape_raw_text_in_select.rb +31 -0
- data/lib/lobzique/sanitizer/scrubbers.rb +46 -0
- data/lib/lobzique/sanitizer.rb +96 -0
- data/lib/lobzique/syntax_error.rb +8 -0
- data/lib/lobzique/version.rb +6 -0
- data/lib/lobzique/xml/attr.rb +23 -0
- data/lib/lobzique/xml/cdata.rb +13 -0
- data/lib/lobzique/xml/character_data.rb +11 -0
- data/lib/lobzique/xml/document.rb +149 -0
- data/lib/lobzique/xml/document_fragment.rb +109 -0
- data/lib/lobzique/xml/dtd.rb +28 -0
- data/lib/lobzique/xml/namespace.rb +19 -0
- data/lib/lobzique/xml/node/save_options.rb +52 -0
- data/lib/lobzique/xml/node.rb +629 -0
- data/lib/lobzique/xml/node_set.rb +329 -0
- data/lib/lobzique/xml/parse_options.rb +89 -0
- data/lib/lobzique/xml/pp/character_data.rb +22 -0
- data/lib/lobzique/xml/pp/node.rb +73 -0
- data/lib/lobzique/xml/pp.rb +6 -0
- data/lib/lobzique/xml/searchable.rb +123 -0
- data/lib/lobzique/xml/syntax_error.rb +77 -0
- data/lib/lobzique/xml/text.rb +14 -0
- data/lib/lobzique/xml/xpath/syntax_error.rb +20 -0
- data/lib/lobzique/xml/xpath.rb +16 -0
- data/lib/lobzique/xml/xpath_context.rb +27 -0
- data/lib/lobzique/xml.rb +22 -0
- data/lib/lobzique.rb +15 -0
- metadata +177 -0
|
@@ -0,0 +1,944 @@
|
|
|
1
|
+
//! `XML::Node` and the wrapper that all node objects share, documents included.
|
|
2
|
+
|
|
3
|
+
use super::ELEMENT;
|
|
4
|
+
use super::{
|
|
5
|
+
attr::ATTR, cdata::CDATA, comment::COMMENT, document, document_fragment::DOCUMENT_FRAGMENT,
|
|
6
|
+
dtd::DTD, namespace, node_set, text::TEXT,
|
|
7
|
+
};
|
|
8
|
+
use crate::html5ever;
|
|
9
|
+
use crate::support::{c_string, call_initialize, obj_class, warn_deprecated, yield_to_block};
|
|
10
|
+
use crate::XML;
|
|
11
|
+
use lobzique_core::dom::{
|
|
12
|
+
self, CopyDepth, Doctype, NamespaceRef, NodeId, NodeType, Position, ReparentError,
|
|
13
|
+
};
|
|
14
|
+
use lobzique_core::{html5, xml};
|
|
15
|
+
use magnus::{
|
|
16
|
+
block::Proc,
|
|
17
|
+
encoding::{Encoding, EncodingCapable, RbEncoding},
|
|
18
|
+
gc, method,
|
|
19
|
+
prelude::*,
|
|
20
|
+
rb_sys::AsRawValue,
|
|
21
|
+
typed_data::{Obj, Writebarrier},
|
|
22
|
+
value::{Lazy, Opaque},
|
|
23
|
+
DataTypeFunctions, Error, KwArgs, RArray, RClass, RString, Ruby, TypedData, Value,
|
|
24
|
+
};
|
|
25
|
+
use std::cell::{Cell, OnceCell, RefCell};
|
|
26
|
+
use std::mem::size_of;
|
|
27
|
+
|
|
28
|
+
pub static NODE: Lazy<RClass> = Lazy::new(|ruby| {
|
|
29
|
+
ruby.get_inner(&XML)
|
|
30
|
+
.define_class("Node", ruby.class_object())
|
|
31
|
+
.unwrap()
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
/// The data behind every node object. A node is its document plus its id
|
|
35
|
+
/// in the arena of that document. Both change when the node moves to another
|
|
36
|
+
/// document. A document is the node that owns the arena, so `Document < Node`
|
|
37
|
+
/// works without a second data type.
|
|
38
|
+
///
|
|
39
|
+
/// Its objects are write-barrier protected, so that a minor GC does not mark
|
|
40
|
+
/// every node object of every live document. Only `complete_document`
|
|
41
|
+
/// and `repoint` store the two Ruby references, and they tell the GC.
|
|
42
|
+
#[derive(Default, TypedData)]
|
|
43
|
+
#[magnus(
|
|
44
|
+
class = "Lobzique::XML::Node",
|
|
45
|
+
mark,
|
|
46
|
+
compact,
|
|
47
|
+
size,
|
|
48
|
+
free_immediately,
|
|
49
|
+
wb_protected
|
|
50
|
+
)]
|
|
51
|
+
pub struct Node {
|
|
52
|
+
/// `None` only on an object made with `allocate` and never initialized.
|
|
53
|
+
/// Typed, so that reaching the arena from a node needs no check.
|
|
54
|
+
document: Cell<Option<Opaque<Obj<Node>>>>,
|
|
55
|
+
id: Cell<Option<NodeId>>,
|
|
56
|
+
/// Boxed, so that the objects of other nodes do not carry an empty one.
|
|
57
|
+
owned: OnceCell<Box<Owned>>,
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
struct Owned {
|
|
61
|
+
arena: RefCell<dom::Document>,
|
|
62
|
+
/// Node objects by node id: the `@node_cache` of Nokogiri. `None`
|
|
63
|
+
/// until the own object of the document exists.
|
|
64
|
+
cache: Cell<Option<Opaque<RArray>>>,
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
impl Owned {
|
|
68
|
+
fn new(arena: dom::Document) -> Box<Owned> {
|
|
69
|
+
Box::new(Owned {
|
|
70
|
+
arena: RefCell::new(arena),
|
|
71
|
+
cache: Cell::new(None),
|
|
72
|
+
})
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
impl DataTypeFunctions for Node {
|
|
77
|
+
fn mark(&self, marker: &gc::Marker) {
|
|
78
|
+
if let Some(document) = self.document.get() {
|
|
79
|
+
marker.mark_movable(document);
|
|
80
|
+
}
|
|
81
|
+
if let Some(cache) = self.owned.get().and_then(|owned| owned.cache.get()) {
|
|
82
|
+
marker.mark(cache);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
fn compact(&self, compactor: &gc::Compactor) {
|
|
87
|
+
if let Some(document) = self.document.get() {
|
|
88
|
+
self.document.set(Some(compactor.location(document)));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
fn size(&self) -> usize {
|
|
93
|
+
let Some(owned) = self.owned.get() else {
|
|
94
|
+
return size_of::<Self>();
|
|
95
|
+
};
|
|
96
|
+
let heap = owned
|
|
97
|
+
.arena
|
|
98
|
+
.try_borrow()
|
|
99
|
+
.map_or(0, |arena| arena.heap_size());
|
|
100
|
+
size_of::<Self>() + size_of::<Owned>() + heap
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
impl Node {
|
|
105
|
+
fn new(document: Obj<Node>, id: NodeId) -> Node {
|
|
106
|
+
Node {
|
|
107
|
+
document: Cell::new(Some(document.into())),
|
|
108
|
+
id: Cell::new(Some(id)),
|
|
109
|
+
owned: OnceCell::new(),
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/// The data of the document node itself, which owns the arena.
|
|
114
|
+
/// It is not usable before [`Node::complete_document`].
|
|
115
|
+
pub fn new_document(arena: dom::Document) -> Node {
|
|
116
|
+
Node {
|
|
117
|
+
document: Cell::new(None),
|
|
118
|
+
id: Cell::new(Some(NodeId::DOCUMENT)),
|
|
119
|
+
owned: OnceCell::from(Owned::new(arena)),
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/// Gives an allocated, empty object the arena that it is the document
|
|
124
|
+
/// of. `false` if it has one already.
|
|
125
|
+
pub fn adopt_arena(&self, arena: dom::Document) -> bool {
|
|
126
|
+
if self.owned.set(Owned::new(arena)).is_err() {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
self.id.set(Some(NodeId::DOCUMENT));
|
|
130
|
+
true
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/// Called once the Ruby object of a document exists. It is its own document
|
|
134
|
+
/// and starts with an empty node cache.
|
|
135
|
+
pub fn complete_document(ruby: &Ruby, document: Obj<Node>) {
|
|
136
|
+
document.document.set(Some(document.into()));
|
|
137
|
+
if let Some(owned) = document.owned.get() {
|
|
138
|
+
let cache = ruby.ary_new();
|
|
139
|
+
owned.cache.set(Some(cache.into()));
|
|
140
|
+
document.writebarrier(cache);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/// Points the object at another node, possibly in another document,
|
|
145
|
+
/// as Nokogiri resets `DATA_PTR` after it copies or merges a node.
|
|
146
|
+
fn repoint(node: Obj<Node>, document: Obj<Node>, id: NodeId) {
|
|
147
|
+
node.document.set(Some(document.into()));
|
|
148
|
+
node.id.set(Some(id));
|
|
149
|
+
node.writebarrier(document);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
fn owned(&self, ruby: &Ruby) -> Result<&Owned, Error> {
|
|
153
|
+
self.owned.get().map(Box::as_ref).ok_or_else(|| {
|
|
154
|
+
Error::new(
|
|
155
|
+
ruby.exception_runtime_error(),
|
|
156
|
+
"Uninitialized document struct (null data pointer)",
|
|
157
|
+
)
|
|
158
|
+
})
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
pub fn arena(&self, ruby: &Ruby) -> Result<&RefCell<dom::Document>, Error> {
|
|
162
|
+
Ok(&self.owned(ruby)?.arena)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
fn cache(&self, ruby: &Ruby) -> Result<RArray, Error> {
|
|
166
|
+
let cache = self.owned(ruby)?.cache.get();
|
|
167
|
+
Ok(ruby.get_inner(cache.expect("a document object has a node cache")))
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
pub fn document(&self, ruby: &Ruby) -> Result<Obj<Node>, Error> {
|
|
171
|
+
self.document
|
|
172
|
+
.get()
|
|
173
|
+
.map(|document| ruby.get_inner(document))
|
|
174
|
+
.ok_or_else(|| uninitialized(ruby))
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
pub fn id(&self, ruby: &Ruby) -> Result<NodeId, Error> {
|
|
178
|
+
self.id.get().ok_or_else(|| uninitialized(ruby))
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/// Runs `f` on the arena of the node, under the rules
|
|
182
|
+
/// of [`document::with_arena`] (plan.md decision 5).
|
|
183
|
+
pub fn with_arena<R: Send>(
|
|
184
|
+
&self,
|
|
185
|
+
ruby: &Ruby,
|
|
186
|
+
f: impl FnOnce(&dom::Document, NodeId) -> R + Send,
|
|
187
|
+
) -> Result<R, Error> {
|
|
188
|
+
let id = self.id(ruby)?;
|
|
189
|
+
document::with_arena(ruby, self.document(ruby)?, |arena| f(arena, id))
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
fn node_type(ruby: &Ruby, rb_self: &Self) -> Result<u8, Error> {
|
|
193
|
+
rb_self.with_arena(ruby, |arena, id| arena.node_type(id) as u8)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
fn node_name(ruby: &Ruby, rb_self: &Self) -> Result<Option<RString>, Error> {
|
|
197
|
+
let name = rb_self.with_arena(ruby, |arena, id| arena.node_name(id).map(<[u8]>::to_vec))?;
|
|
198
|
+
Ok(name.map(|name| utf8_string(ruby, name)))
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
fn data_ptr_p(&self) -> bool {
|
|
202
|
+
self.document.get().is_some()
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
fn document_method(ruby: &Ruby, rb_self: &Self) -> Result<Obj<Node>, Error> {
|
|
206
|
+
rb_self.document(ruby)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
fn wrap_found(
|
|
210
|
+
&self,
|
|
211
|
+
ruby: &Ruby,
|
|
212
|
+
f: impl FnOnce(&dom::Document, NodeId) -> Option<NodeId> + Send,
|
|
213
|
+
) -> Result<Option<Value>, Error> {
|
|
214
|
+
self.with_arena(ruby, f)?
|
|
215
|
+
.map(|id| wrap(ruby, self.document(ruby)?, id))
|
|
216
|
+
.transpose()
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
fn internal_subset(ruby: &Ruby, rb_self: &Self) -> Result<Option<Value>, Error> {
|
|
220
|
+
rb_self.wrap_found(ruby, |arena, _| arena.doctype())
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
fn parent(ruby: &Ruby, rb_self: &Self) -> Result<Option<Value>, Error> {
|
|
224
|
+
rb_self.wrap_found(ruby, |arena, id| arena.parent(id))
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
fn child(ruby: &Ruby, rb_self: &Self) -> Result<Option<Value>, Error> {
|
|
228
|
+
rb_self.wrap_found(ruby, |arena, id| arena.first_child(id))
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
fn next_sibling(ruby: &Ruby, rb_self: &Self) -> Result<Option<Value>, Error> {
|
|
232
|
+
rb_self.wrap_found(ruby, |arena, id| arena.next_in_parent(id))
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
fn previous_sibling(ruby: &Ruby, rb_self: &Self) -> Result<Option<Value>, Error> {
|
|
236
|
+
rb_self.wrap_found(ruby, |arena, id| arena.previous_in_parent(id))
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
fn next_element(ruby: &Ruby, rb_self: &Self) -> Result<Option<Value>, Error> {
|
|
240
|
+
rb_self.wrap_found(ruby, |arena, id| arena.next_element_sibling(id))
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
fn previous_element(ruby: &Ruby, rb_self: &Self) -> Result<Option<Value>, Error> {
|
|
244
|
+
rb_self.wrap_found(ruby, |arena, id| arena.previous_element_sibling(id))
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
fn first_element_child(ruby: &Ruby, rb_self: &Self) -> Result<Option<Value>, Error> {
|
|
248
|
+
rb_self.wrap_found(ruby, |arena, id| arena.element_children(id).next())
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
fn last_element_child(ruby: &Ruby, rb_self: &Self) -> Result<Option<Value>, Error> {
|
|
252
|
+
rb_self.wrap_found(ruby, |arena, id| arena.last_element_child(id))
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
fn children(ruby: &Ruby, rb_self: &Self) -> Result<Value, Error> {
|
|
256
|
+
let ids = rb_self.with_arena(ruby, |arena, id| arena.children(id).collect())?;
|
|
257
|
+
node_set::wrap(ruby, rb_self.document(ruby)?, ids)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
fn element_children(ruby: &Ruby, rb_self: &Self) -> Result<Value, Error> {
|
|
261
|
+
let ids = rb_self.with_arena(ruby, |arena, id| arena.element_children(id).collect())?;
|
|
262
|
+
node_set::wrap(ruby, rb_self.document(ruby)?, ids)
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
fn attribute(ruby: &Ruby, rb_self: &Self, name: Value) -> Result<Option<Value>, Error> {
|
|
266
|
+
let name = c_string(name)?;
|
|
267
|
+
rb_self.wrap_found(ruby, move |arena, id| arena.attribute_named(id, &name))
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
fn attribute_with_ns(
|
|
271
|
+
ruby: &Ruby,
|
|
272
|
+
rb_self: &Self,
|
|
273
|
+
name: Value,
|
|
274
|
+
namespace: Value,
|
|
275
|
+
) -> Result<Option<Value>, Error> {
|
|
276
|
+
let (name, href) = (c_string(name)?, optional_c_string(namespace)?);
|
|
277
|
+
rb_self.wrap_found(ruby, move |arena, id| {
|
|
278
|
+
arena.attribute_in_namespace(id, &name, href.as_deref())
|
|
279
|
+
})
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
fn key_p(ruby: &Ruby, rb_self: &Self, name: Value) -> Result<bool, Error> {
|
|
283
|
+
let name = c_string(name)?;
|
|
284
|
+
rb_self.with_arena(ruby, move |arena, id| {
|
|
285
|
+
arena.attribute_named(id, &name).is_some()
|
|
286
|
+
})
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
fn namespaced_key_p(
|
|
290
|
+
ruby: &Ruby,
|
|
291
|
+
rb_self: &Self,
|
|
292
|
+
name: Value,
|
|
293
|
+
namespace: Value,
|
|
294
|
+
) -> Result<bool, Error> {
|
|
295
|
+
let (name, href) = (c_string(name)?, optional_c_string(namespace)?);
|
|
296
|
+
rb_self.with_arena(ruby, move |arena, id| {
|
|
297
|
+
arena
|
|
298
|
+
.attribute_in_namespace(id, &name, href.as_deref())
|
|
299
|
+
.is_some()
|
|
300
|
+
})
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
fn get(ruby: &Ruby, rb_self: &Self, name: Value) -> Result<Option<RString>, Error> {
|
|
304
|
+
let Some(name) = optional_c_string(name)? else {
|
|
305
|
+
return Ok(None);
|
|
306
|
+
};
|
|
307
|
+
let value = rb_self.with_arena(ruby, move |arena, id| {
|
|
308
|
+
arena.attribute_value(id, &name).map(<[u8]>::to_vec)
|
|
309
|
+
})?;
|
|
310
|
+
Ok(value.map(|value| utf8_string(ruby, value)))
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
fn lang(ruby: &Ruby, rb_self: &Self) -> Result<Option<RString>, Error> {
|
|
314
|
+
let lang = rb_self.with_arena(ruby, |arena, id| arena.lang(id).map(<[u8]>::to_vec))?;
|
|
315
|
+
Ok(lang.map(|lang| utf8_string(ruby, lang)))
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
fn blank_p(ruby: &Ruby, rb_self: &Self) -> Result<bool, Error> {
|
|
319
|
+
rb_self.with_arena(ruby, |arena, id| arena.is_blank(id))
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
fn path(ruby: &Ruby, rb_self: &Self) -> Result<RString, Error> {
|
|
323
|
+
let path = rb_self.with_arena(ruby, |arena, id| arena.path(id))?;
|
|
324
|
+
Ok(utf8_string(ruby, path.as_deref().unwrap_or(b"?")))
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/// Unique among live nodes, as the address of a libxml2 node
|
|
328
|
+
/// is: the address of the data of the document, which does not move,
|
|
329
|
+
/// and the id of the node.
|
|
330
|
+
fn pointer_id(ruby: &Ruby, rb_self: &Self) -> Result<u128, Error> {
|
|
331
|
+
let document = rb_self.document(ruby)?;
|
|
332
|
+
let address = &*document as *const Node as usize;
|
|
333
|
+
Ok((address as u128) << 32 | rb_self.id(ruby)?.index() as u128)
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/// `xmlXPathCmpNodes(other, self)`: 1 if this node comes after `other`, -1
|
|
337
|
+
/// if before, 0 if they are the same, -2 if they are in different trees.
|
|
338
|
+
fn compare(ruby: &Ruby, rb_self: &Self, other: Value) -> Result<i8, Error> {
|
|
339
|
+
let other = <&Node>::try_convert(other)?;
|
|
340
|
+
let document = rb_self.document(ruby)?;
|
|
341
|
+
if document.as_raw() != other.document(ruby)?.as_raw() {
|
|
342
|
+
return Ok(-2);
|
|
343
|
+
}
|
|
344
|
+
let other = other.id(ruby)?;
|
|
345
|
+
let order = rb_self.with_arena(ruby, move |arena, id| arena.document_order(id, other))?;
|
|
346
|
+
Ok(order.map_or(-2, |order| order as i8))
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
fn namespace_definitions(ruby: &Ruby, rb_self: &Self) -> Result<RArray, Error> {
|
|
350
|
+
let definitions = rb_self.with_arena(ruby, |arena, id| {
|
|
351
|
+
owned_namespaces(arena.namespace_definitions(id).iter().map(Into::into))
|
|
352
|
+
})?;
|
|
353
|
+
wrap_namespaces(ruby, rb_self.document(ruby)?.as_value(), definitions)
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
fn namespace_scopes(ruby: &Ruby, rb_self: &Self) -> Result<RArray, Error> {
|
|
357
|
+
let scopes = rb_self.with_arena(ruby, |arena, id| {
|
|
358
|
+
owned_namespaces(arena.namespace_scopes(id).into_iter())
|
|
359
|
+
})?;
|
|
360
|
+
wrap_namespaces(ruby, rb_self.document(ruby)?.as_value(), scopes)
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
fn encode_special_chars(ruby: &Ruby, _rb_self: &Self, string: Value) -> Result<RString, Error> {
|
|
364
|
+
Ok(utf8_string(
|
|
365
|
+
ruby,
|
|
366
|
+
dom::encode_special_chars(&c_string(string)?),
|
|
367
|
+
))
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
fn attribute_nodes(ruby: &Ruby, rb_self: &Self) -> Result<RArray, Error> {
|
|
371
|
+
let ids = rb_self.with_arena(ruby, |arena, id| arena.attributes(id).to_vec())?;
|
|
372
|
+
wrap_all(ruby, rb_self.document(ruby)?, ids)
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
fn content(ruby: &Ruby, rb_self: &Self) -> Result<Option<RString>, Error> {
|
|
376
|
+
let content = rb_self.with_arena(ruby, |arena, id| arena.content(id))?;
|
|
377
|
+
Ok(content.map(|content| utf8_string(ruby, content)))
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
fn namespace(ruby: &Ruby, rb_self: &Self) -> Result<Option<Value>, Error> {
|
|
381
|
+
let namespace = rb_self.with_arena(ruby, |arena, id| {
|
|
382
|
+
arena
|
|
383
|
+
.namespace(id)
|
|
384
|
+
.map(|ns| (ns.prefix.map(str::to_owned), ns.href.to_owned()))
|
|
385
|
+
})?;
|
|
386
|
+
let document = rb_self.document(ruby)?.as_value();
|
|
387
|
+
namespace
|
|
388
|
+
.map(|(prefix, href)| namespace::wrap(ruby, document, prefix, href))
|
|
389
|
+
.transpose()
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
fn html_standard_serialize(
|
|
393
|
+
ruby: &Ruby,
|
|
394
|
+
rb_self: &Self,
|
|
395
|
+
preserve_newline: Value,
|
|
396
|
+
scripting_enabled: Value,
|
|
397
|
+
) -> Result<RString, Error> {
|
|
398
|
+
let preserve_newline = preserve_newline.to_bool();
|
|
399
|
+
let scripting_enabled = scripting_enabled.to_bool();
|
|
400
|
+
let html = rb_self.with_arena(ruby, |arena, id| {
|
|
401
|
+
html5::serialize(arena, id, preserve_newline, scripting_enabled)
|
|
402
|
+
})?;
|
|
403
|
+
Ok(utf8_string(ruby, html))
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/// The XML and XHTML output of libxml2 for the node, transcoded and written
|
|
407
|
+
/// to `io`.
|
|
408
|
+
fn native_write_to(
|
|
409
|
+
ruby: &Ruby,
|
|
410
|
+
rb_self: &Self,
|
|
411
|
+
io: Value,
|
|
412
|
+
encoding: Value,
|
|
413
|
+
indent_string: Value,
|
|
414
|
+
options: i32,
|
|
415
|
+
) -> Result<Value, Error> {
|
|
416
|
+
let encoding = optional_c_string(encoding)?;
|
|
417
|
+
let indent = c_string(indent_string)?;
|
|
418
|
+
let output = rb_self
|
|
419
|
+
.with_arena(ruby, |arena, id| {
|
|
420
|
+
xml::write(arena, id, encoding.as_deref(), &indent, options as u32)
|
|
421
|
+
})?
|
|
422
|
+
.map_err(|xml::HtmlUnsupported| {
|
|
423
|
+
Error::new(
|
|
424
|
+
ruby.exception_not_imp_error(),
|
|
425
|
+
"libxml2's HTML serializer isn't supported; HTML documents use the HTML5 one",
|
|
426
|
+
)
|
|
427
|
+
})?;
|
|
428
|
+
if let Some(string) = transcode(ruby, output)? {
|
|
429
|
+
write_to_io(ruby, io, string)?;
|
|
430
|
+
}
|
|
431
|
+
Ok(io)
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
fn prepend_newline_p(ruby: &Ruby, rb_self: &Self) -> Result<bool, Error> {
|
|
435
|
+
rb_self.with_arena(ruby, html5::prepend_newline)
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/// The options are the parser options of libxml2. The HTML5 parser does
|
|
439
|
+
/// not use them, but they are still converted as Nokogiri converts them.
|
|
440
|
+
fn in_context(
|
|
441
|
+
ruby: &Ruby,
|
|
442
|
+
rb_self: &Self,
|
|
443
|
+
input: Value,
|
|
444
|
+
_options: i32,
|
|
445
|
+
) -> Result<Value, Error> {
|
|
446
|
+
html5ever::in_context(ruby, rb_self, input)
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
fn add_child_node(ruby: &Ruby, rb_self: &Self, node: Value) -> Result<Value, Error> {
|
|
450
|
+
reparent(ruby, rb_self, node, Position::LastChild)
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
fn add_previous_sibling_node(ruby: &Ruby, rb_self: &Self, node: Value) -> Result<Value, Error> {
|
|
454
|
+
reparent(ruby, rb_self, node, Position::Before)
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
fn add_next_sibling_node(ruby: &Ruby, rb_self: &Self, node: Value) -> Result<Value, Error> {
|
|
458
|
+
reparent(ruby, rb_self, node, Position::After)
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
fn replace_node(ruby: &Ruby, rb_self: &Self, node: Value) -> Result<Value, Error> {
|
|
462
|
+
reparent(ruby, rb_self, node, Position::Replace)
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
fn unlink(ruby: &Ruby, rb_self: Obj<Self>) -> Result<Obj<Self>, Error> {
|
|
466
|
+
rb_self.with_arena_mut(ruby, |arena, id| arena.detach(id))?;
|
|
467
|
+
Ok(rb_self)
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/// `Node#[]=`, native half: `nil`, and no change, unless an element.
|
|
471
|
+
fn set(ruby: &Ruby, rb_self: &Self, name: Value, value: Value) -> Result<Value, Error> {
|
|
472
|
+
let is_element =
|
|
473
|
+
rb_self.with_arena(ruby, |arena, id| arena.node_type(id) == NodeType::Element)?;
|
|
474
|
+
if !is_element {
|
|
475
|
+
return Ok(ruby.qnil().as_value());
|
|
476
|
+
}
|
|
477
|
+
let name = element_name(ruby, c_string(name)?)?;
|
|
478
|
+
let bytes = c_string(value)?;
|
|
479
|
+
rb_self.with_arena_mut(ruby, move |arena, id| {
|
|
480
|
+
arena.set_attribute(id, &name, bytes);
|
|
481
|
+
})?;
|
|
482
|
+
Ok(value)
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
fn set_native_content(ruby: &Ruby, rb_self: &Self, content: Value) -> Result<Value, Error> {
|
|
486
|
+
let bytes = c_string(content)?;
|
|
487
|
+
rb_self.with_arena_mut(ruby, move |arena, id| arena.set_content(id, bytes))?;
|
|
488
|
+
Ok(content)
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
fn set_name(ruby: &Ruby, rb_self: &Self, new_name: Value) -> Result<Value, Error> {
|
|
492
|
+
let name = element_name(ruby, c_string(new_name)?)?;
|
|
493
|
+
rb_self.with_arena_mut(ruby, move |arena, id| arena.set_name(id, &name))?;
|
|
494
|
+
Ok(new_name)
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
fn set_lang(ruby: &Ruby, rb_self: &Self, lang: Value) -> Result<(), Error> {
|
|
498
|
+
let lang = c_string(lang)?;
|
|
499
|
+
rb_self.with_arena_mut(ruby, move |arena, id| arena.set_lang(id, lang))
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
fn create_internal_subset(
|
|
503
|
+
ruby: &Ruby,
|
|
504
|
+
rb_self: &Self,
|
|
505
|
+
name: Value,
|
|
506
|
+
external_id: Value,
|
|
507
|
+
system_id: Value,
|
|
508
|
+
) -> Result<Value, Error> {
|
|
509
|
+
let doctype = Doctype {
|
|
510
|
+
name: optional_c_string(name)?.unwrap_or_default(),
|
|
511
|
+
public_id: optional_c_string(external_id)?,
|
|
512
|
+
system_id: optional_c_string(system_id)?,
|
|
513
|
+
};
|
|
514
|
+
let document = rb_self.document(ruby)?;
|
|
515
|
+
let dtd = document::with_arena_mut(ruby, document, |arena| {
|
|
516
|
+
arena.create_internal_subset(doctype)
|
|
517
|
+
})?
|
|
518
|
+
.ok_or_else(|| {
|
|
519
|
+
Error::new(
|
|
520
|
+
ruby.exception_runtime_error(),
|
|
521
|
+
"Document already has an internal subset",
|
|
522
|
+
)
|
|
523
|
+
})?;
|
|
524
|
+
wrap(ruby, document, dtd)
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/// `initialize_copy_with_args(other, level, new_parent_doc)`, after
|
|
528
|
+
/// `Object#dup` or `#clone` has allocated the copy: `xmlDocCopyNode`. Then
|
|
529
|
+
/// the object is cached and decorated. `nil` for nodes that libxml2 does
|
|
530
|
+
/// not copy, such as doctypes.
|
|
531
|
+
fn initialize_copy_with_args(
|
|
532
|
+
ruby: &Ruby,
|
|
533
|
+
rb_self: Obj<Self>,
|
|
534
|
+
other: Value,
|
|
535
|
+
level: i32,
|
|
536
|
+
new_parent_doc: Value,
|
|
537
|
+
) -> Result<Option<Obj<Self>>, Error> {
|
|
538
|
+
let node_class = ruby.get_inner(&NODE);
|
|
539
|
+
if !other.is_kind_of(node_class) {
|
|
540
|
+
let message = format!("argument must be a kind of {}", node_class.inspect());
|
|
541
|
+
return Err(Error::new(ruby.exception_type_error(), message));
|
|
542
|
+
}
|
|
543
|
+
// `Object#dup` hands over a fresh object. Repointing a live one would
|
|
544
|
+
// leave it in the cache slot of its old node.
|
|
545
|
+
if rb_self.data_ptr_p() {
|
|
546
|
+
return Err(Error::new(
|
|
547
|
+
ruby.exception_runtime_error(),
|
|
548
|
+
"node is already initialized",
|
|
549
|
+
));
|
|
550
|
+
}
|
|
551
|
+
let other = <&Node>::try_convert(other)?;
|
|
552
|
+
let (source, id) = (other.document(ruby)?, other.id(ruby)?);
|
|
553
|
+
let new_parent_doc = document::unwrap(ruby, new_parent_doc)?;
|
|
554
|
+
let depth = CopyDepth::from_level(level);
|
|
555
|
+
let copy = if source.as_raw() == new_parent_doc.as_raw() {
|
|
556
|
+
document::with_arena_mut(ruby, source, move |arena| arena.copy(id, depth))?
|
|
557
|
+
} else {
|
|
558
|
+
document::with_arenas_mut(ruby, new_parent_doc, source, move |arena, source| {
|
|
559
|
+
arena.import(source, id, depth)
|
|
560
|
+
})?
|
|
561
|
+
};
|
|
562
|
+
let Some(copy) = copy else {
|
|
563
|
+
return Ok(None);
|
|
564
|
+
};
|
|
565
|
+
Node::repoint(rb_self, new_parent_doc, copy);
|
|
566
|
+
let cache = new_parent_doc.cache(ruby)?;
|
|
567
|
+
cache.store(cache_index(copy), rb_self)?;
|
|
568
|
+
new_parent_doc.funcall::<_, _, Value>("decorate", (rb_self,))?;
|
|
569
|
+
Ok(Some(rb_self))
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
pub fn with_arena_mut<R: Send>(
|
|
573
|
+
&self,
|
|
574
|
+
ruby: &Ruby,
|
|
575
|
+
f: impl FnOnce(&mut dom::Document, NodeId) -> R + Send,
|
|
576
|
+
) -> Result<R, Error> {
|
|
577
|
+
let id = self.id(ruby)?;
|
|
578
|
+
document::with_arena_mut(ruby, self.document(ruby)?, |arena| f(arena, id))
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/// Nokogiri's `reparent_node_with`. The core can copy a node because it is text
|
|
583
|
+
/// or from another document, or merge it into neighboring text. That node keeps
|
|
584
|
+
/// its object, which is pointed at what went into the tree. What went
|
|
585
|
+
/// into the tree gets an object of its own, which is decorated and returned.
|
|
586
|
+
/// As in Nokogiri, the original of a text node loses its cached object.
|
|
587
|
+
fn reparent(ruby: &Ruby, pivot: &Node, node: Value, position: Position) -> Result<Value, Error> {
|
|
588
|
+
let node_class = ruby.get_inner(&NODE);
|
|
589
|
+
if !node.is_kind_of(node_class) || node.is_kind_of(ruby.get_inner(&document::DOCUMENT)) {
|
|
590
|
+
let message = format!("node must be a {}", node_class.inspect());
|
|
591
|
+
return Err(Error::new(ruby.exception_arg_error(), message));
|
|
592
|
+
}
|
|
593
|
+
let reparentee = Obj::<Node>::try_convert(node)?;
|
|
594
|
+
let (document, pivot_id) = (pivot.document(ruby)?, pivot.id(ruby)?);
|
|
595
|
+
let (source, id) = (reparentee.document(ruby)?, reparentee.id(ruby)?);
|
|
596
|
+
let namespace_inheritance = document
|
|
597
|
+
.ivar_get::<_, Value>("@namespace_inheritance")?
|
|
598
|
+
.to_bool();
|
|
599
|
+
let is_text = reparentee.with_arena(ruby, |arena, id| arena.node_type(id) == NodeType::Text)?;
|
|
600
|
+
|
|
601
|
+
let reparented = if source.as_raw() == document.as_raw() {
|
|
602
|
+
document::with_arena_mut(ruby, document, |arena| {
|
|
603
|
+
arena.reparent(pivot_id, position, id, namespace_inheritance)
|
|
604
|
+
})?
|
|
605
|
+
} else {
|
|
606
|
+
document::with_arenas_mut(ruby, document, source, |arena, source| {
|
|
607
|
+
arena.reparent_from(pivot_id, position, source, id, namespace_inheritance)
|
|
608
|
+
})?
|
|
609
|
+
};
|
|
610
|
+
let reparented = match reparented {
|
|
611
|
+
Ok(reparented) => reparented,
|
|
612
|
+
Err(ReparentError::NotAllowed) => {
|
|
613
|
+
let message = format!("cannot reparent {} there", obj_class(node).inspect());
|
|
614
|
+
return Err(Error::new(ruby.exception_arg_error(), message));
|
|
615
|
+
}
|
|
616
|
+
Err(ReparentError::Refused) => {
|
|
617
|
+
return Err(Error::new(
|
|
618
|
+
ruby.exception_runtime_error(),
|
|
619
|
+
"Could not reparent node",
|
|
620
|
+
));
|
|
621
|
+
}
|
|
622
|
+
Err(ReparentError::Cycle) => {
|
|
623
|
+
let name = reparentee.with_arena(ruby, |arena, id| {
|
|
624
|
+
String::from_utf8_lossy(arena.node_name(id).unwrap_or_default()).into_owned()
|
|
625
|
+
})?;
|
|
626
|
+
let message = format!("cycle detected: node '{name}' is an ancestor of itself");
|
|
627
|
+
return Err(Error::new(ruby.exception_runtime_error(), message));
|
|
628
|
+
}
|
|
629
|
+
};
|
|
630
|
+
|
|
631
|
+
if is_text {
|
|
632
|
+
source.cache(ruby)?.store(cache_index(id), ruby.qnil())?;
|
|
633
|
+
}
|
|
634
|
+
Node::repoint(reparentee, document, reparented);
|
|
635
|
+
let reparented = wrap(ruby, document, reparented)?;
|
|
636
|
+
reparented.funcall::<_, _, Value>("decorate!", ())?;
|
|
637
|
+
Ok(reparented)
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/// The output in the encoding that libxml2 would transcode it to, with `&#NNN;`
|
|
641
|
+
/// for characters that the encoding lacks, as the encoder of libxml2 writes
|
|
642
|
+
/// them. `None` for an encoding that Ruby does not know. libxml2 writes nothing
|
|
643
|
+
/// for it.
|
|
644
|
+
fn transcode(ruby: &Ruby, output: xml::Output) -> Result<Option<RString>, Error> {
|
|
645
|
+
let string = utf8_string(ruby, output.bytes);
|
|
646
|
+
let Some(label) = output.encoding else {
|
|
647
|
+
return Ok(Some(string));
|
|
648
|
+
};
|
|
649
|
+
let Some(encoding) = std::str::from_utf8(&label)
|
|
650
|
+
.ok()
|
|
651
|
+
.and_then(|label| ruby.find_encindex(label).ok())
|
|
652
|
+
.map(RbEncoding::from)
|
|
653
|
+
else {
|
|
654
|
+
return Ok(None);
|
|
655
|
+
};
|
|
656
|
+
if encoding.name() == ruby.utf8_encoding().name() {
|
|
657
|
+
return Ok(Some(string));
|
|
658
|
+
}
|
|
659
|
+
let options = ruby.hash_new();
|
|
660
|
+
options.aset(
|
|
661
|
+
ruby.to_symbol("fallback"),
|
|
662
|
+
ruby.proc_new(character_reference),
|
|
663
|
+
)?;
|
|
664
|
+
options.aset(ruby.to_symbol("invalid"), ruby.to_symbol("replace"))?;
|
|
665
|
+
string
|
|
666
|
+
.funcall("encode", (Encoding::from(encoding), KwArgs(options)))
|
|
667
|
+
.map(Some)
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
fn character_reference(
|
|
671
|
+
_ruby: &Ruby,
|
|
672
|
+
args: &[Value],
|
|
673
|
+
_block: Option<Proc>,
|
|
674
|
+
) -> Result<String, Error> {
|
|
675
|
+
let character: u32 = args[0].funcall("ord", ())?;
|
|
676
|
+
Ok(format!("&#{character};"))
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
/// `noko_io_write`: the bytes tagged with the external encoding of the IO,
|
|
680
|
+
/// or as binary. If the write raises a StandardError, the output ends there.
|
|
681
|
+
fn write_to_io(ruby: &Ruby, io: Value, string: RString) -> Result<(), Error> {
|
|
682
|
+
let external_encoding: Value = if io.respond_to("external_encoding", false)? {
|
|
683
|
+
io.funcall("external_encoding", ())?
|
|
684
|
+
} else {
|
|
685
|
+
ruby.qnil().as_value()
|
|
686
|
+
};
|
|
687
|
+
let encoding = if external_encoding.is_nil() {
|
|
688
|
+
ruby.ascii8bit_encoding()
|
|
689
|
+
} else {
|
|
690
|
+
RbEncoding::try_convert(external_encoding)?
|
|
691
|
+
};
|
|
692
|
+
string.enc_associate(encoding)?;
|
|
693
|
+
match io.funcall::<_, _, Value>("write", (string,)) {
|
|
694
|
+
Err(error) if error.is_kind_of(ruby.exception_standard_error()) => Ok(()),
|
|
695
|
+
written => written.map(drop),
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
fn optional_c_string(value: Value) -> Result<Option<Vec<u8>>, Error> {
|
|
700
|
+
if value.is_nil() {
|
|
701
|
+
Ok(None)
|
|
702
|
+
} else {
|
|
703
|
+
c_string(value).map(Some)
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
type OwnedNamespace = (Option<String>, String);
|
|
708
|
+
|
|
709
|
+
fn owned_namespaces<'a>(namespaces: impl Iterator<Item = NamespaceRef<'a>>) -> Vec<OwnedNamespace> {
|
|
710
|
+
namespaces
|
|
711
|
+
.map(|namespace| {
|
|
712
|
+
(
|
|
713
|
+
namespace.prefix.map(str::to_owned),
|
|
714
|
+
namespace.href.to_owned(),
|
|
715
|
+
)
|
|
716
|
+
})
|
|
717
|
+
.collect()
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
fn wrap_namespaces(
|
|
721
|
+
ruby: &Ruby,
|
|
722
|
+
document: Value,
|
|
723
|
+
namespaces: Vec<OwnedNamespace>,
|
|
724
|
+
) -> Result<RArray, Error> {
|
|
725
|
+
let array = ruby.ary_new_capa(namespaces.len());
|
|
726
|
+
for (prefix, href) in namespaces {
|
|
727
|
+
array.push(namespace::wrap(ruby, document, prefix, href)?)?;
|
|
728
|
+
}
|
|
729
|
+
Ok(array)
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
pub fn wrap_all(ruby: &Ruby, document: Obj<Node>, ids: Vec<NodeId>) -> Result<RArray, Error> {
|
|
733
|
+
let nodes = ruby.ary_new_capa(ids.len());
|
|
734
|
+
for id in ids {
|
|
735
|
+
nodes.push(wrap(ruby, document, id)?)?;
|
|
736
|
+
}
|
|
737
|
+
Ok(nodes)
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
/// Bytes from the arena as a Ruby string tagged UTF-8, valid or not, as
|
|
741
|
+
/// Nokogiri returns what libxml2 holds.
|
|
742
|
+
pub fn utf8_string(ruby: &Ruby, bytes: impl AsRef<[u8]>) -> RString {
|
|
743
|
+
ruby.enc_str_new(bytes, ruby.utf8_encoding())
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
fn uninitialized(ruby: &Ruby) -> Error {
|
|
747
|
+
let class = ruby.get_inner(&NODE);
|
|
748
|
+
let message = format!(
|
|
749
|
+
"Uninitialized {} struct (null data pointer)",
|
|
750
|
+
class.inspect()
|
|
751
|
+
);
|
|
752
|
+
Error::new(ruby.exception_runtime_error(), message)
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
/// The one Ruby object for a node, created on first use:
|
|
756
|
+
/// the `noko_xml_node_wrap` of Nokogiri. The class follows the node type unless
|
|
757
|
+
/// `class` is given. A new object goes into the node cache of the document
|
|
758
|
+
/// and through `Document#decorate`. `initialize` is not called.
|
|
759
|
+
pub fn wrap_as(
|
|
760
|
+
ruby: &Ruby,
|
|
761
|
+
document: Obj<Node>,
|
|
762
|
+
id: NodeId,
|
|
763
|
+
class: Option<RClass>,
|
|
764
|
+
) -> Result<Value, Error> {
|
|
765
|
+
if id == NodeId::DOCUMENT {
|
|
766
|
+
return Ok(document.as_value());
|
|
767
|
+
}
|
|
768
|
+
let cache = document.cache(ruby)?;
|
|
769
|
+
let cached: Value = cache.entry(cache_index(id))?;
|
|
770
|
+
if !cached.is_nil() {
|
|
771
|
+
return Ok(cached);
|
|
772
|
+
}
|
|
773
|
+
let class = match class {
|
|
774
|
+
Some(class) => class,
|
|
775
|
+
None => {
|
|
776
|
+
let node_type = document::with_arena(ruby, document, |arena| arena.node_type(id))?;
|
|
777
|
+
class_for(ruby, node_type)
|
|
778
|
+
}
|
|
779
|
+
};
|
|
780
|
+
let node = ruby.obj_wrap_as(Node::new(document, id), class).as_value();
|
|
781
|
+
cache.store(cache_index(id), node)?;
|
|
782
|
+
document.funcall::<_, _, Value>("decorate", (node,))?;
|
|
783
|
+
Ok(node)
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
pub fn wrap(ruby: &Ruby, document: Obj<Node>, id: NodeId) -> Result<Value, Error> {
|
|
787
|
+
wrap_as(ruby, document, id, None)
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
fn cache_index(id: NodeId) -> isize {
|
|
791
|
+
isize::try_from(id.index()).expect("node ids fit in an isize")
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
/// The table of Nokogiri from node type to class, for the types of an HTML
|
|
795
|
+
/// tree.
|
|
796
|
+
fn class_for(ruby: &Ruby, node_type: NodeType) -> RClass {
|
|
797
|
+
match node_type {
|
|
798
|
+
NodeType::Element => ruby.get_inner(&ELEMENT),
|
|
799
|
+
NodeType::Text => ruby.get_inner(&TEXT),
|
|
800
|
+
NodeType::CDataSection => ruby.get_inner(&CDATA),
|
|
801
|
+
NodeType::Comment => ruby.get_inner(&COMMENT),
|
|
802
|
+
NodeType::DocumentFragment => ruby.get_inner(&DOCUMENT_FRAGMENT),
|
|
803
|
+
NodeType::Dtd => ruby.get_inner(&DTD),
|
|
804
|
+
NodeType::Attribute => ruby.get_inner(&ATTR),
|
|
805
|
+
NodeType::ProcessingInstruction | NodeType::Document | NodeType::HtmlDocument => {
|
|
806
|
+
ruby.get_inner(&NODE)
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
/// The document that a new node goes into. Nokogiri deprecates passing another
|
|
812
|
+
/// node instead. It resolves to the document of that node.
|
|
813
|
+
pub fn document_for_new_node(
|
|
814
|
+
ruby: &Ruby,
|
|
815
|
+
document: Value,
|
|
816
|
+
deprecation: &str,
|
|
817
|
+
) -> Result<Obj<Node>, Error> {
|
|
818
|
+
if document.is_kind_of(ruby.get_inner(&document::DOCUMENT)) {
|
|
819
|
+
return document::unwrap(ruby, document);
|
|
820
|
+
}
|
|
821
|
+
warn_deprecated(deprecation)?;
|
|
822
|
+
<&Node>::try_convert(document)?.document(ruby)
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
/// Shared tail of the native constructors of Nokogiri: wrap as the receiving
|
|
826
|
+
/// class, run `initialize` with the original arguments, then yield.
|
|
827
|
+
pub fn finish_new(
|
|
828
|
+
ruby: &Ruby,
|
|
829
|
+
document: Obj<Node>,
|
|
830
|
+
id: NodeId,
|
|
831
|
+
class: RClass,
|
|
832
|
+
args: &[Value],
|
|
833
|
+
) -> Result<Value, Error> {
|
|
834
|
+
let node = wrap_as(ruby, document, id, Some(class))?;
|
|
835
|
+
call_initialize(node, args)?;
|
|
836
|
+
yield_to_block(ruby, node)?;
|
|
837
|
+
Ok(node)
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
/// `Node.new(name, document, *rest)`.
|
|
841
|
+
fn new(ruby: &Ruby, class: RClass, args: &[Value]) -> Result<Value, Error> {
|
|
842
|
+
ruby.check_arity(args.len(), 2..)?;
|
|
843
|
+
let node_class = ruby.get_inner(&NODE);
|
|
844
|
+
if !args[1].is_kind_of(node_class) {
|
|
845
|
+
let message = format!("document must be a {}", node_class.inspect());
|
|
846
|
+
return Err(Error::new(ruby.exception_arg_error(), message));
|
|
847
|
+
}
|
|
848
|
+
let document = document_for_new_node(
|
|
849
|
+
ruby,
|
|
850
|
+
args[1],
|
|
851
|
+
"Passing a Node as the second parameter to Node.new is deprecated. Please pass a Document instead, \
|
|
852
|
+
or prefer an alternative constructor like Node#add_child.",
|
|
853
|
+
)?;
|
|
854
|
+
let name = element_name(ruby, c_string(args[0])?)?;
|
|
855
|
+
let id = document::with_arena_mut(ruby, document, |arena| arena.create_element_named(&name))?;
|
|
856
|
+
// `Node.new` gives an Element, as the node type of libxml2 decides.
|
|
857
|
+
let class = if class.equal(node_class)? {
|
|
858
|
+
ruby.get_inner(&ELEMENT)
|
|
859
|
+
} else {
|
|
860
|
+
class
|
|
861
|
+
};
|
|
862
|
+
finish_new(ruby, document, id, class, args)
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
/// Names are atoms that the parser and the selector engine share, so they must
|
|
866
|
+
/// be UTF-8, unlike in Nokogiri (ARCHITECTURE.md, departures).
|
|
867
|
+
fn element_name(ruby: &Ruby, name: Vec<u8>) -> Result<String, Error> {
|
|
868
|
+
String::from_utf8(name)
|
|
869
|
+
.map_err(|_| Error::new(ruby.exception_arg_error(), "invalid byte sequence in UTF-8"))
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
pub fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
873
|
+
let class = ruby.get_inner(&NODE);
|
|
874
|
+
class.define_alloc_func::<Node>();
|
|
875
|
+
class.define_singleton_method("new", method!(new, -1))?;
|
|
876
|
+
class.define_method("data_ptr?", method!(Node::data_ptr_p, 0))?;
|
|
877
|
+
class.define_method("document", method!(Node::document_method, 0))?;
|
|
878
|
+
class.define_method("node_name", method!(Node::node_name, 0))?;
|
|
879
|
+
class.define_method("node_type", method!(Node::node_type, 0))?;
|
|
880
|
+
class.define_method("internal_subset", method!(Node::internal_subset, 0))?;
|
|
881
|
+
class.define_method("parent", method!(Node::parent, 0))?;
|
|
882
|
+
class.define_method("child", method!(Node::child, 0))?;
|
|
883
|
+
class.define_method("children", method!(Node::children, 0))?;
|
|
884
|
+
class.define_method("element_children", method!(Node::element_children, 0))?;
|
|
885
|
+
class.define_method("first_element_child", method!(Node::first_element_child, 0))?;
|
|
886
|
+
class.define_method("last_element_child", method!(Node::last_element_child, 0))?;
|
|
887
|
+
class.define_method("next_sibling", method!(Node::next_sibling, 0))?;
|
|
888
|
+
class.define_method("previous_sibling", method!(Node::previous_sibling, 0))?;
|
|
889
|
+
class.define_method("next_element", method!(Node::next_element, 0))?;
|
|
890
|
+
class.define_method("previous_element", method!(Node::previous_element, 0))?;
|
|
891
|
+
class.define_method("attribute", method!(Node::attribute, 1))?;
|
|
892
|
+
class.define_method("attribute_with_ns", method!(Node::attribute_with_ns, 2))?;
|
|
893
|
+
class.define_method("key?", method!(Node::key_p, 1))?;
|
|
894
|
+
class.define_method("namespaced_key?", method!(Node::namespaced_key_p, 2))?;
|
|
895
|
+
class.define_method("blank?", method!(Node::blank_p, 0))?;
|
|
896
|
+
class.define_method("lang", method!(Node::lang, 0))?;
|
|
897
|
+
class.define_method("path", method!(Node::path, 0))?;
|
|
898
|
+
class.define_method("pointer_id", method!(Node::pointer_id, 0))?;
|
|
899
|
+
class.define_method(
|
|
900
|
+
"encode_special_chars",
|
|
901
|
+
method!(Node::encode_special_chars, 1),
|
|
902
|
+
)?;
|
|
903
|
+
class.define_method("attribute_nodes", method!(Node::attribute_nodes, 0))?;
|
|
904
|
+
class.define_method("content", method!(Node::content, 0))?;
|
|
905
|
+
class.define_method("namespace", method!(Node::namespace, 0))?;
|
|
906
|
+
class.define_method(
|
|
907
|
+
"namespace_definitions",
|
|
908
|
+
method!(Node::namespace_definitions, 0),
|
|
909
|
+
)?;
|
|
910
|
+
class.define_method("namespace_scopes", method!(Node::namespace_scopes, 0))?;
|
|
911
|
+
class.define_method("unlink", method!(Node::unlink, 0))?;
|
|
912
|
+
class.define_method("native_content=", method!(Node::set_native_content, 1))?;
|
|
913
|
+
class.define_method("node_name=", method!(Node::set_name, 1))?;
|
|
914
|
+
class.define_method("lang=", method!(Node::set_lang, 1))?;
|
|
915
|
+
class.define_method(
|
|
916
|
+
"create_internal_subset",
|
|
917
|
+
method!(Node::create_internal_subset, 3),
|
|
918
|
+
)?;
|
|
919
|
+
class.define_protected_method(
|
|
920
|
+
"initialize_copy_with_args",
|
|
921
|
+
method!(Node::initialize_copy_with_args, 3),
|
|
922
|
+
)?;
|
|
923
|
+
class.define_private_method("add_child_node", method!(Node::add_child_node, 1))?;
|
|
924
|
+
class.define_private_method(
|
|
925
|
+
"add_previous_sibling_node",
|
|
926
|
+
method!(Node::add_previous_sibling_node, 1),
|
|
927
|
+
)?;
|
|
928
|
+
class.define_private_method(
|
|
929
|
+
"add_next_sibling_node",
|
|
930
|
+
method!(Node::add_next_sibling_node, 1),
|
|
931
|
+
)?;
|
|
932
|
+
class.define_private_method("replace_node", method!(Node::replace_node, 1))?;
|
|
933
|
+
class.define_private_method("set", method!(Node::set, 2))?;
|
|
934
|
+
class.define_private_method("compare", method!(Node::compare, 1))?;
|
|
935
|
+
class.define_private_method("get", method!(Node::get, 1))?;
|
|
936
|
+
class.define_private_method("in_context", method!(Node::in_context, 2))?;
|
|
937
|
+
class.define_private_method("native_write_to", method!(Node::native_write_to, 4))?;
|
|
938
|
+
class.define_private_method("prepend_newline?", method!(Node::prepend_newline_p, 0))?;
|
|
939
|
+
class.define_private_method(
|
|
940
|
+
"html_standard_serialize",
|
|
941
|
+
method!(Node::html_standard_serialize, 2),
|
|
942
|
+
)?;
|
|
943
|
+
Ok(())
|
|
944
|
+
}
|