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,681 @@
|
|
|
1
|
+
//! libxml2's `xmlSaveTree` (xmlsave.c 2.13) for the `native_write_to`
|
|
2
|
+
//! of Nokogiri: a node, the node included, as XML or XHTML.
|
|
3
|
+
//!
|
|
4
|
+
//! libxml2 escapes while it writes, then transcodes the whole stream. It writes
|
|
5
|
+
//! `&#NNN;` for characters that the target encoding lacks. [`write`] does
|
|
6
|
+
//! the first half and names the encoding for the second.
|
|
7
|
+
|
|
8
|
+
use crate::dom::{c_string, Doctype, Document, DocumentKind, NodeId, NodeKind};
|
|
9
|
+
|
|
10
|
+
/// Nokogiri's `SaveOptions`, libxml2's `xmlSaveOption`.
|
|
11
|
+
pub mod save_options {
|
|
12
|
+
pub const FORMAT: u32 = 1;
|
|
13
|
+
pub const NO_DECLARATION: u32 = 2;
|
|
14
|
+
pub const NO_EMPTY_TAGS: u32 = 4;
|
|
15
|
+
pub const NO_XHTML: u32 = 8;
|
|
16
|
+
pub const AS_XHTML: u32 = 16;
|
|
17
|
+
pub const AS_XML: u32 = 32;
|
|
18
|
+
pub const AS_HTML: u32 = 64;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
use save_options::*;
|
|
22
|
+
|
|
23
|
+
/// What [`write`] wrote, still UTF-8, and the encoding that libxml2 would
|
|
24
|
+
/// transcode it to, if any.
|
|
25
|
+
#[derive(Debug, PartialEq, Eq)]
|
|
26
|
+
pub struct Output {
|
|
27
|
+
pub bytes: Vec<u8>,
|
|
28
|
+
pub encoding: Option<Vec<u8>>,
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/// The options ask for the HTML serializer of libxml2, which is not ported
|
|
32
|
+
/// (plan.md decision 3).
|
|
33
|
+
#[derive(Debug, PartialEq, Eq)]
|
|
34
|
+
pub struct HtmlUnsupported;
|
|
35
|
+
|
|
36
|
+
/// libxml2's `MAX_INDENT`: the indentation of the deepest levels is cut
|
|
37
|
+
/// to as many copies of the indent string as fit in this number of bytes.
|
|
38
|
+
const MAX_INDENT: usize = 60;
|
|
39
|
+
|
|
40
|
+
const XHTML_NAMESPACE: &str = "http://www.w3.org/1999/xhtml";
|
|
41
|
+
|
|
42
|
+
/// `xmlSaveToIO` with `encoding`, `indent` as `xmlTreeIndentString` and
|
|
43
|
+
/// `options`, then `xmlSaveTree` on `id`. Walks the tree without recursion.
|
|
44
|
+
pub fn write(
|
|
45
|
+
document: &Document,
|
|
46
|
+
id: NodeId,
|
|
47
|
+
encoding: Option<&[u8]>,
|
|
48
|
+
indent: &[u8],
|
|
49
|
+
options: u32,
|
|
50
|
+
) -> Result<Output, HtmlUnsupported> {
|
|
51
|
+
let is_document = matches!(document.node_kind(id), NodeKind::Document);
|
|
52
|
+
let html = if options & AS_XHTML != 0 {
|
|
53
|
+
// Only xmlDocContentDumpOutput checks this.
|
|
54
|
+
is_document && options & AS_HTML != 0
|
|
55
|
+
} else {
|
|
56
|
+
options & AS_HTML != 0 || (document.kind() == DocumentKind::Html && options & AS_XML == 0)
|
|
57
|
+
};
|
|
58
|
+
if html {
|
|
59
|
+
return Err(HtmlUnsupported);
|
|
60
|
+
}
|
|
61
|
+
let mut saver = Saver {
|
|
62
|
+
document,
|
|
63
|
+
out: Vec::new(),
|
|
64
|
+
encoding,
|
|
65
|
+
switched_encoding: None,
|
|
66
|
+
options,
|
|
67
|
+
format: options & FORMAT != 0,
|
|
68
|
+
level: 0,
|
|
69
|
+
indent,
|
|
70
|
+
indent_count: if indent.is_empty() {
|
|
71
|
+
0
|
|
72
|
+
} else {
|
|
73
|
+
MAX_INDENT / indent.len()
|
|
74
|
+
},
|
|
75
|
+
escape: if encoding.is_none() {
|
|
76
|
+
Escape::Entities
|
|
77
|
+
} else {
|
|
78
|
+
Escape::Content
|
|
79
|
+
},
|
|
80
|
+
has_encoding: document.encoding().is_some(),
|
|
81
|
+
};
|
|
82
|
+
saver.node(id, options & AS_XHTML != 0);
|
|
83
|
+
let encoding = encoding.map(<[u8]>::to_vec).or(saver.switched_encoding);
|
|
84
|
+
Ok(Output {
|
|
85
|
+
bytes: saver.out,
|
|
86
|
+
encoding,
|
|
87
|
+
})
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
91
|
+
enum Escape {
|
|
92
|
+
Entities,
|
|
93
|
+
Content,
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
struct Saver<'a> {
|
|
97
|
+
document: &'a Document,
|
|
98
|
+
out: Vec<u8>,
|
|
99
|
+
encoding: Option<&'a [u8]>,
|
|
100
|
+
switched_encoding: Option<Vec<u8>>,
|
|
101
|
+
options: u32,
|
|
102
|
+
format: bool,
|
|
103
|
+
level: usize,
|
|
104
|
+
indent: &'a [u8],
|
|
105
|
+
indent_count: usize,
|
|
106
|
+
escape: Escape,
|
|
107
|
+
/// `doc->encoding != NULL`. It stops attribute values from being written
|
|
108
|
+
/// as character references.
|
|
109
|
+
has_encoding: bool,
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
impl Saver<'_> {
|
|
113
|
+
/// `xmlNodeDumpOutputInternal`, or `xhtmlNodeDumpOutput` with `xhtml`. They
|
|
114
|
+
/// differ only where `xhtml` is read.
|
|
115
|
+
fn node(&mut self, root: NodeId, xhtml: bool) {
|
|
116
|
+
let document = self.document;
|
|
117
|
+
if xhtml {
|
|
118
|
+
// Never restored: xhtmlNodeDumpOutput returns before it would be.
|
|
119
|
+
self.options |= AS_XHTML;
|
|
120
|
+
}
|
|
121
|
+
let format = self.format;
|
|
122
|
+
let mut unformatted = None;
|
|
123
|
+
let mut parent = document.parent(root);
|
|
124
|
+
let mut current = root;
|
|
125
|
+
loop {
|
|
126
|
+
let mut descend = false;
|
|
127
|
+
match document.node_kind(current) {
|
|
128
|
+
NodeKind::Document => self.document_content(),
|
|
129
|
+
NodeKind::Doctype(doctype) => self.doctype(doctype),
|
|
130
|
+
NodeKind::DocumentFragment => descend = document.first_child(current).is_some(),
|
|
131
|
+
NodeKind::Element(element) => {
|
|
132
|
+
if current != root && self.format {
|
|
133
|
+
self.indent(self.level);
|
|
134
|
+
}
|
|
135
|
+
let prefix = self.document.namespace(current).and_then(|ns| ns.prefix);
|
|
136
|
+
self.out.push(b'<');
|
|
137
|
+
self.qualified_name(prefix, &element.name.local);
|
|
138
|
+
for definition in &element.namespace_definitions {
|
|
139
|
+
self.namespace_definition(definition.prefix.as_deref(), &definition.href);
|
|
140
|
+
}
|
|
141
|
+
let mut add_meta = false;
|
|
142
|
+
if xhtml {
|
|
143
|
+
if &*element.name.local == "html"
|
|
144
|
+
&& document.namespace(current).is_none()
|
|
145
|
+
&& element.namespace_definitions.is_empty()
|
|
146
|
+
{
|
|
147
|
+
self.out.extend_from_slice(b" xmlns=\"");
|
|
148
|
+
self.out.extend_from_slice(XHTML_NAMESPACE.as_bytes());
|
|
149
|
+
self.out.push(b'"');
|
|
150
|
+
}
|
|
151
|
+
self.xhtml_attributes(current, &element.attributes);
|
|
152
|
+
add_meta = self.lacks_content_type(current, parent);
|
|
153
|
+
} else {
|
|
154
|
+
for &attribute in &element.attributes {
|
|
155
|
+
self.attribute(attribute);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
let has_children = document.first_child(current).is_some();
|
|
159
|
+
if !has_children && xhtml {
|
|
160
|
+
if prefix.is_none() && !add_meta && self.is_empty_xhtml(current) {
|
|
161
|
+
self.out.extend_from_slice(b" />");
|
|
162
|
+
} else {
|
|
163
|
+
self.out.push(b'>');
|
|
164
|
+
if add_meta {
|
|
165
|
+
self.content_type_meta(true);
|
|
166
|
+
}
|
|
167
|
+
self.end_tag(prefix, &element.name.local);
|
|
168
|
+
}
|
|
169
|
+
} else if !has_children {
|
|
170
|
+
if self.options & NO_EMPTY_TAGS == 0 {
|
|
171
|
+
self.out.extend_from_slice(b"/>");
|
|
172
|
+
} else {
|
|
173
|
+
self.out.push(b'>');
|
|
174
|
+
self.end_tag(prefix, &element.name.local);
|
|
175
|
+
}
|
|
176
|
+
} else {
|
|
177
|
+
if xhtml {
|
|
178
|
+
self.out.push(b'>');
|
|
179
|
+
if add_meta {
|
|
180
|
+
self.content_type_meta(false);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
if self.format
|
|
184
|
+
&& document.children(current).any(|child| {
|
|
185
|
+
matches!(document.node_kind(child), NodeKind::Text(_))
|
|
186
|
+
|| (!xhtml
|
|
187
|
+
&& matches!(document.node_kind(child), NodeKind::CData(_)))
|
|
188
|
+
})
|
|
189
|
+
{
|
|
190
|
+
self.format = false;
|
|
191
|
+
unformatted = Some(current);
|
|
192
|
+
}
|
|
193
|
+
if !xhtml {
|
|
194
|
+
self.out.push(b'>');
|
|
195
|
+
}
|
|
196
|
+
if self.format {
|
|
197
|
+
self.out.push(b'\n');
|
|
198
|
+
}
|
|
199
|
+
self.level += 1;
|
|
200
|
+
descend = true;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
NodeKind::Text(text) => self.text(c_string(text)),
|
|
204
|
+
NodeKind::ProcessingInstruction(pi) => {
|
|
205
|
+
if !xhtml && current != root && self.format {
|
|
206
|
+
self.indent(self.level);
|
|
207
|
+
}
|
|
208
|
+
self.out.extend_from_slice(b"<?");
|
|
209
|
+
self.out.extend_from_slice(c_string(&pi.target));
|
|
210
|
+
self.out.push(b' ');
|
|
211
|
+
self.out.extend_from_slice(c_string(&pi.data));
|
|
212
|
+
self.out.extend_from_slice(b"?>");
|
|
213
|
+
}
|
|
214
|
+
NodeKind::Comment(text) => {
|
|
215
|
+
if !xhtml && current != root && self.format {
|
|
216
|
+
self.indent(self.level);
|
|
217
|
+
}
|
|
218
|
+
self.out.extend_from_slice(b"<!--");
|
|
219
|
+
self.out.extend_from_slice(c_string(text));
|
|
220
|
+
self.out.extend_from_slice(b"-->");
|
|
221
|
+
}
|
|
222
|
+
NodeKind::CData(text) => self.cdata(c_string(text)),
|
|
223
|
+
NodeKind::Attribute(_) => self.attribute(current),
|
|
224
|
+
}
|
|
225
|
+
if descend {
|
|
226
|
+
parent = Some(current);
|
|
227
|
+
current = document
|
|
228
|
+
.first_child(current)
|
|
229
|
+
.expect("descending into children");
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
loop {
|
|
233
|
+
if current == root {
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
if self.format {
|
|
237
|
+
self.out.push(b'\n');
|
|
238
|
+
}
|
|
239
|
+
if let Some(next) = document.next_sibling(current) {
|
|
240
|
+
current = next;
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
current = parent.expect("a node below the root has a parent");
|
|
244
|
+
parent = document.parent(current);
|
|
245
|
+
if let NodeKind::Element(element) = document.node_kind(current) {
|
|
246
|
+
self.level = self.level.saturating_sub(1);
|
|
247
|
+
if self.format {
|
|
248
|
+
self.indent(self.level);
|
|
249
|
+
}
|
|
250
|
+
let prefix = self.document.namespace(current).and_then(|ns| ns.prefix);
|
|
251
|
+
self.end_tag(prefix, &element.name.local);
|
|
252
|
+
if unformatted == Some(current) {
|
|
253
|
+
self.format = format;
|
|
254
|
+
unformatted = None;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
fn document_content(&mut self) {
|
|
262
|
+
let document = self.document;
|
|
263
|
+
let had_encoding = self.has_encoding;
|
|
264
|
+
let encoding = match self.encoding {
|
|
265
|
+
Some(encoding) => {
|
|
266
|
+
self.has_encoding = true;
|
|
267
|
+
Some(encoding)
|
|
268
|
+
}
|
|
269
|
+
None => document.encoding(),
|
|
270
|
+
};
|
|
271
|
+
if let (Some(encoding), None) = (encoding, self.encoding) {
|
|
272
|
+
if self.options & NO_DECLARATION == 0 {
|
|
273
|
+
// xmlParseCharEncoding: only UTF-8 and no encoding stay
|
|
274
|
+
// untranscoded.
|
|
275
|
+
let utf8 = encoding.is_empty()
|
|
276
|
+
|| encoding.eq_ignore_ascii_case(b"UTF-8")
|
|
277
|
+
|| encoding.eq_ignore_ascii_case(b"UTF8");
|
|
278
|
+
if !utf8 {
|
|
279
|
+
self.switched_encoding = Some(encoding.to_vec());
|
|
280
|
+
}
|
|
281
|
+
self.escape = Escape::Content;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
if self.options & NO_DECLARATION == 0 {
|
|
285
|
+
self.out.extend_from_slice(b"<?xml version=");
|
|
286
|
+
self.quoted(document.version().unwrap_or(b"1.0"));
|
|
287
|
+
if let Some(encoding) = encoding {
|
|
288
|
+
self.out.extend_from_slice(b" encoding=");
|
|
289
|
+
self.quoted(encoding);
|
|
290
|
+
}
|
|
291
|
+
// htmlNewDocNoDtD makes HTML documents standalone. xmlNewDoc leaves
|
|
292
|
+
// it unset.
|
|
293
|
+
if document.kind() == DocumentKind::Html {
|
|
294
|
+
self.out.extend_from_slice(b" standalone=\"yes\"");
|
|
295
|
+
}
|
|
296
|
+
self.out.extend_from_slice(b"?>\n");
|
|
297
|
+
}
|
|
298
|
+
let mut xhtml = self.options & AS_XHTML != 0;
|
|
299
|
+
if self.options & NO_XHTML == 0 {
|
|
300
|
+
if let Some(doctype) = document.doctype() {
|
|
301
|
+
if let NodeKind::Doctype(doctype) = document.node_kind(doctype) {
|
|
302
|
+
xhtml = is_xhtml(doctype);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
for child in document.children(NodeId::DOCUMENT) {
|
|
307
|
+
self.level = 0;
|
|
308
|
+
self.node(child, xhtml);
|
|
309
|
+
self.out.push(b'\n');
|
|
310
|
+
}
|
|
311
|
+
self.has_encoding = had_encoding;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
fn doctype(&mut self, doctype: &Doctype) {
|
|
315
|
+
self.out.extend_from_slice(b"<!DOCTYPE ");
|
|
316
|
+
self.out.extend_from_slice(c_string(&doctype.name));
|
|
317
|
+
if let Some(public_id) = &doctype.public_id {
|
|
318
|
+
self.out.extend_from_slice(b" PUBLIC ");
|
|
319
|
+
self.quoted(public_id);
|
|
320
|
+
self.out.push(b' ');
|
|
321
|
+
self.quoted(doctype.system_id.as_deref().unwrap_or_default());
|
|
322
|
+
} else if let Some(system_id) = &doctype.system_id {
|
|
323
|
+
self.out.extend_from_slice(b" SYSTEM ");
|
|
324
|
+
self.quoted(system_id);
|
|
325
|
+
}
|
|
326
|
+
self.out.push(b'>');
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
fn attribute(&mut self, id: NodeId) {
|
|
330
|
+
let NodeKind::Attribute(attribute) = self.document.node_kind(id) else {
|
|
331
|
+
return;
|
|
332
|
+
};
|
|
333
|
+
let namespace = self.document.namespace(id);
|
|
334
|
+
self.out.push(b' ');
|
|
335
|
+
self.qualified_name(namespace.and_then(|ns| ns.prefix), &attribute.name.local);
|
|
336
|
+
self.out.extend_from_slice(b"=\"");
|
|
337
|
+
let value = c_string(&attribute.value);
|
|
338
|
+
if self.options & AS_XHTML != 0
|
|
339
|
+
&& namespace.is_none()
|
|
340
|
+
&& value.is_empty()
|
|
341
|
+
&& is_boolean_attribute(&attribute.name.local)
|
|
342
|
+
{
|
|
343
|
+
self.out.extend_from_slice(attribute.name.local.as_bytes());
|
|
344
|
+
} else {
|
|
345
|
+
self.attribute_value(value);
|
|
346
|
+
}
|
|
347
|
+
self.out.push(b'"');
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/// `xhtmlAttrListDumpOutput`: the attributes, then an `id` for a `name`,
|
|
351
|
+
/// and the missing one of `lang` and `xml:lang` (XHTML 1.0 appendix C.8
|
|
352
|
+
/// and C.7).
|
|
353
|
+
fn xhtml_attributes(&mut self, element: NodeId, attributes: &[NodeId]) {
|
|
354
|
+
let document = self.document;
|
|
355
|
+
let (mut id, mut name, mut lang, mut xml_lang) = (None, None, None, None);
|
|
356
|
+
for &attribute in attributes {
|
|
357
|
+
if let NodeKind::Attribute(attr) = document.node_kind(attribute) {
|
|
358
|
+
let namespace = document.namespace(attribute);
|
|
359
|
+
let value = c_string(&attr.value);
|
|
360
|
+
match (&*attr.name.local, namespace) {
|
|
361
|
+
("id", None) => id = Some(value),
|
|
362
|
+
("name", None) => name = Some(value),
|
|
363
|
+
("lang", None) => lang = Some(value),
|
|
364
|
+
("lang", Some(ns)) if ns.prefix == Some("xml") => xml_lang = Some(value),
|
|
365
|
+
_ => {}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
self.attribute(attribute);
|
|
369
|
+
}
|
|
370
|
+
if let (Some(name), None) = (name, id) {
|
|
371
|
+
let named = matches!(
|
|
372
|
+
document.node_kind(element),
|
|
373
|
+
NodeKind::Element(e) if matches!(
|
|
374
|
+
&*e.name.local,
|
|
375
|
+
"a" | "p" | "div" | "img" | "map" | "applet" | "form" | "frame" | "iframe"
|
|
376
|
+
)
|
|
377
|
+
);
|
|
378
|
+
if named {
|
|
379
|
+
self.out.extend_from_slice(b" id=\"");
|
|
380
|
+
self.attribute_value(name);
|
|
381
|
+
self.out.push(b'"');
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
match (lang, xml_lang) {
|
|
385
|
+
(Some(lang), None) => {
|
|
386
|
+
self.out.extend_from_slice(b" xml:lang=\"");
|
|
387
|
+
self.attribute_value(lang);
|
|
388
|
+
self.out.push(b'"');
|
|
389
|
+
}
|
|
390
|
+
(None, Some(xml_lang)) => {
|
|
391
|
+
self.out.extend_from_slice(b" lang=\"");
|
|
392
|
+
self.attribute_value(xml_lang);
|
|
393
|
+
self.out.push(b'"');
|
|
394
|
+
}
|
|
395
|
+
_ => {}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/// Whether a `head` that is a child of the `html` of the document has no
|
|
400
|
+
/// Content-Type `meta`. XHTML output then adds one.
|
|
401
|
+
fn lacks_content_type(&self, head: NodeId, parent: Option<NodeId>) -> bool {
|
|
402
|
+
let document = self.document;
|
|
403
|
+
let named = |id: NodeId, name: &str| matches!(document.node_kind(id), NodeKind::Element(e) if &*e.name.local == name);
|
|
404
|
+
let Some(parent) = parent else {
|
|
405
|
+
return false;
|
|
406
|
+
};
|
|
407
|
+
document.parent(parent) == Some(NodeId::DOCUMENT)
|
|
408
|
+
&& named(head, "head")
|
|
409
|
+
&& named(parent, "html")
|
|
410
|
+
&& !document.children(head).any(|child| {
|
|
411
|
+
named(child, "meta")
|
|
412
|
+
&& document
|
|
413
|
+
.attribute_named(child, b"http-equiv")
|
|
414
|
+
.and_then(|attribute| match document.node_kind(attribute) {
|
|
415
|
+
NodeKind::Attribute(attribute) => Some(c_string(&attribute.value)),
|
|
416
|
+
_ => None,
|
|
417
|
+
})
|
|
418
|
+
.is_some_and(|value| value.eq_ignore_ascii_case(b"Content-Type"))
|
|
419
|
+
})
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/// The `meta` that XHTML output adds to a `head`. The `head` has children
|
|
423
|
+
/// unless `empty`.
|
|
424
|
+
fn content_type_meta(&mut self, empty: bool) {
|
|
425
|
+
if self.format {
|
|
426
|
+
self.out.push(b'\n');
|
|
427
|
+
self.indent(self.level + 1);
|
|
428
|
+
}
|
|
429
|
+
self.out
|
|
430
|
+
.extend_from_slice(b"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=");
|
|
431
|
+
self.out
|
|
432
|
+
.extend_from_slice(self.encoding.unwrap_or(b"UTF-8"));
|
|
433
|
+
self.out.extend_from_slice(b"\" />");
|
|
434
|
+
if empty && self.format {
|
|
435
|
+
self.out.push(b'\n');
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/// `xhtmlIsEmpty`: an element that XHTML output closes with ` />`.
|
|
440
|
+
fn is_empty_xhtml(&self, id: NodeId) -> bool {
|
|
441
|
+
if self
|
|
442
|
+
.document
|
|
443
|
+
.namespace(id)
|
|
444
|
+
.is_some_and(|ns| ns.href != XHTML_NAMESPACE)
|
|
445
|
+
{
|
|
446
|
+
return false;
|
|
447
|
+
}
|
|
448
|
+
matches!(
|
|
449
|
+
self.document.node_kind(id),
|
|
450
|
+
NodeKind::Element(e) if matches!(
|
|
451
|
+
&*e.name.local,
|
|
452
|
+
"area" | "br" | "base" | "basefont" | "col" | "frame" | "hr" | "img" | "input"
|
|
453
|
+
| "isindex" | "link" | "meta" | "param"
|
|
454
|
+
)
|
|
455
|
+
)
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
fn namespace_definition(&mut self, prefix: Option<&str>, href: &str) {
|
|
459
|
+
match prefix {
|
|
460
|
+
Some("xml") => return,
|
|
461
|
+
Some(prefix) => {
|
|
462
|
+
self.out.extend_from_slice(b" xmlns:");
|
|
463
|
+
self.out.extend_from_slice(prefix.as_bytes());
|
|
464
|
+
}
|
|
465
|
+
None => self.out.extend_from_slice(b" xmlns"),
|
|
466
|
+
}
|
|
467
|
+
self.out.extend_from_slice(b"=\"");
|
|
468
|
+
self.attribute_value(href.as_bytes());
|
|
469
|
+
self.out.push(b'"');
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
fn qualified_name(&mut self, prefix: Option<&str>, local: &str) {
|
|
473
|
+
if let Some(prefix) = prefix {
|
|
474
|
+
self.out.extend_from_slice(prefix.as_bytes());
|
|
475
|
+
self.out.push(b':');
|
|
476
|
+
}
|
|
477
|
+
self.out.extend_from_slice(local.as_bytes());
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
fn end_tag(&mut self, prefix: Option<&str>, local: &str) {
|
|
481
|
+
self.out.extend_from_slice(b"</");
|
|
482
|
+
self.qualified_name(prefix, local);
|
|
483
|
+
self.out.push(b'>');
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
fn indent(&mut self, level: usize) {
|
|
487
|
+
for _ in 0..level.min(self.indent_count) {
|
|
488
|
+
self.out.extend_from_slice(self.indent);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
fn text(&mut self, text: &[u8]) {
|
|
493
|
+
crate::work::add(text.len());
|
|
494
|
+
let mut index = 0;
|
|
495
|
+
while index < text.len() {
|
|
496
|
+
let byte = text[index];
|
|
497
|
+
index += 1;
|
|
498
|
+
let replacement: &[u8] = match byte {
|
|
499
|
+
b'<' => b"<",
|
|
500
|
+
b'>' => b">",
|
|
501
|
+
b'&' => b"&",
|
|
502
|
+
b'\r' if self.escape == Escape::Entities => b"
",
|
|
503
|
+
b'\r' => b" ",
|
|
504
|
+
_ if self.escape == Escape::Content => {
|
|
505
|
+
self.out.push(byte);
|
|
506
|
+
continue;
|
|
507
|
+
}
|
|
508
|
+
0x20.. if byte < 0x80 => {
|
|
509
|
+
self.out.push(byte);
|
|
510
|
+
continue;
|
|
511
|
+
}
|
|
512
|
+
b'\n' | b'\t' => {
|
|
513
|
+
self.out.push(byte);
|
|
514
|
+
continue;
|
|
515
|
+
}
|
|
516
|
+
..0x80 => {
|
|
517
|
+
hex_character_reference(&mut self.out, 0xFFFD);
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
_ => {
|
|
521
|
+
let (character, length) = decode_utf8(&text[index - 1..]);
|
|
522
|
+
index += length - 1;
|
|
523
|
+
hex_character_reference(&mut self.out, character);
|
|
524
|
+
continue;
|
|
525
|
+
}
|
|
526
|
+
};
|
|
527
|
+
self.out.extend_from_slice(replacement);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/// `xmlBufAttrSerializeTxtContent`. With no encoding in effect, anything
|
|
532
|
+
/// other than ASCII is written as character references, except a last byte.
|
|
533
|
+
fn attribute_value(&mut self, value: &[u8]) {
|
|
534
|
+
crate::work::add(value.len());
|
|
535
|
+
let mut index = 0;
|
|
536
|
+
while index < value.len() {
|
|
537
|
+
let byte = value[index];
|
|
538
|
+
let replacement: &[u8] = match byte {
|
|
539
|
+
b'\n' => b" ",
|
|
540
|
+
b'\r' => b" ",
|
|
541
|
+
b'\t' => b"	",
|
|
542
|
+
b'"' => b""",
|
|
543
|
+
b'<' => b"<",
|
|
544
|
+
b'>' => b">",
|
|
545
|
+
b'&' => b"&",
|
|
546
|
+
0x80.. if index + 1 < value.len() && !self.has_encoding => {
|
|
547
|
+
let (character, length) = decode_utf8(&value[index..]);
|
|
548
|
+
index += length;
|
|
549
|
+
hex_character_reference(&mut self.out, character);
|
|
550
|
+
continue;
|
|
551
|
+
}
|
|
552
|
+
_ => {
|
|
553
|
+
self.out.push(byte);
|
|
554
|
+
index += 1;
|
|
555
|
+
continue;
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
self.out.extend_from_slice(replacement);
|
|
559
|
+
index += 1;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/// A CDATA section, split after each `]]` that is followed by `>`.
|
|
564
|
+
fn cdata(&mut self, text: &[u8]) {
|
|
565
|
+
crate::work::add(text.len());
|
|
566
|
+
if text.is_empty() {
|
|
567
|
+
self.out.extend_from_slice(b"<![CDATA[]]>");
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
let mut start = 0;
|
|
571
|
+
let mut end = 0;
|
|
572
|
+
while end < text.len() {
|
|
573
|
+
if text[end..].starts_with(b"]]>") {
|
|
574
|
+
end += 2;
|
|
575
|
+
self.out.extend_from_slice(b"<![CDATA[");
|
|
576
|
+
self.out.extend_from_slice(&text[start..end]);
|
|
577
|
+
self.out.extend_from_slice(b"]]>");
|
|
578
|
+
start = end;
|
|
579
|
+
}
|
|
580
|
+
end += 1;
|
|
581
|
+
}
|
|
582
|
+
if start != end {
|
|
583
|
+
self.out.extend_from_slice(b"<![CDATA[");
|
|
584
|
+
self.out.extend_from_slice(&text[start..]);
|
|
585
|
+
self.out.extend_from_slice(b"]]>");
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
fn quoted(&mut self, string: &[u8]) {
|
|
590
|
+
let string = c_string(string);
|
|
591
|
+
if !string.contains(&b'"') {
|
|
592
|
+
self.out.push(b'"');
|
|
593
|
+
self.out.extend_from_slice(string);
|
|
594
|
+
self.out.push(b'"');
|
|
595
|
+
} else if !string.contains(&b'\'') {
|
|
596
|
+
self.out.push(b'\'');
|
|
597
|
+
self.out.extend_from_slice(string);
|
|
598
|
+
self.out.push(b'\'');
|
|
599
|
+
} else {
|
|
600
|
+
self.out.push(b'"');
|
|
601
|
+
for &byte in string {
|
|
602
|
+
if byte == b'"' {
|
|
603
|
+
self.out.extend_from_slice(b""");
|
|
604
|
+
} else {
|
|
605
|
+
self.out.push(byte);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
self.out.push(b'"');
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/// `xmlIsXHTML`: whether a doctype names one of the DTDs of XHTML 1.0.
|
|
614
|
+
fn is_xhtml(doctype: &Doctype) -> bool {
|
|
615
|
+
const PUBLIC_IDS: [&[u8]; 3] = [
|
|
616
|
+
b"-//W3C//DTD XHTML 1.0 Strict//EN",
|
|
617
|
+
b"-//W3C//DTD XHTML 1.0 Frameset//EN",
|
|
618
|
+
b"-//W3C//DTD XHTML 1.0 Transitional//EN",
|
|
619
|
+
];
|
|
620
|
+
const SYSTEM_IDS: [&[u8]; 3] = [
|
|
621
|
+
b"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd",
|
|
622
|
+
b"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd",
|
|
623
|
+
b"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd",
|
|
624
|
+
];
|
|
625
|
+
let listed = |id: &Option<Vec<u8>>, ids: &[&[u8]]| {
|
|
626
|
+
id.as_deref().is_some_and(|id| ids.contains(&c_string(id)))
|
|
627
|
+
};
|
|
628
|
+
listed(&doctype.public_id, &PUBLIC_IDS) || listed(&doctype.system_id, &SYSTEM_IDS)
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
/// `htmlIsBooleanAttr`, which ignores case.
|
|
632
|
+
fn is_boolean_attribute(name: &str) -> bool {
|
|
633
|
+
[
|
|
634
|
+
"checked", "compact", "declare", "defer", "disabled", "ismap", "multiple", "nohref",
|
|
635
|
+
"noresize", "noshade", "nowrap", "readonly", "selected",
|
|
636
|
+
]
|
|
637
|
+
.iter()
|
|
638
|
+
.any(|boolean| boolean.eq_ignore_ascii_case(name))
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/// `xmlGetUTF8Char` on the bytes from a non-ASCII one: the character
|
|
642
|
+
/// and how many bytes it took. A malformed sequence gives U+FFFD and one byte.
|
|
643
|
+
/// Characters that XML does not allow also give U+FFFD.
|
|
644
|
+
fn decode_utf8(bytes: &[u8]) -> (u32, usize) {
|
|
645
|
+
let length = match bytes[0] {
|
|
646
|
+
0xC2..=0xDF => 2,
|
|
647
|
+
0xE0..=0xEF => 3,
|
|
648
|
+
0xF0..=0xFF => 4,
|
|
649
|
+
_ => return (0xFFFD, 1),
|
|
650
|
+
};
|
|
651
|
+
let Some(sequence) = bytes.get(..length) else {
|
|
652
|
+
return (0xFFFD, 1);
|
|
653
|
+
};
|
|
654
|
+
if sequence[1..].iter().any(|&byte| byte & 0xC0 != 0x80) {
|
|
655
|
+
return (0xFFFD, 1);
|
|
656
|
+
}
|
|
657
|
+
let lead = u32::from(sequence[0]) & (0x7F >> length);
|
|
658
|
+
let character = sequence[1..].iter().fold(lead, |character, &byte| {
|
|
659
|
+
character << 6 | u32::from(byte & 0x3F)
|
|
660
|
+
});
|
|
661
|
+
let valid = match length {
|
|
662
|
+
3 => character >= 0x800 && !(0xD800..0xE000).contains(&character),
|
|
663
|
+
4 => (0x10000..0x110000).contains(&character),
|
|
664
|
+
_ => true,
|
|
665
|
+
};
|
|
666
|
+
if !valid {
|
|
667
|
+
return (0xFFFD, 1);
|
|
668
|
+
}
|
|
669
|
+
// IS_CHAR, past ASCII
|
|
670
|
+
let character = if (0xFFFE..0x10000).contains(&character) {
|
|
671
|
+
0xFFFD
|
|
672
|
+
} else {
|
|
673
|
+
character
|
|
674
|
+
};
|
|
675
|
+
(character, length)
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
/// `xmlSerializeHexCharRef`: uppercase, without leading zeros.
|
|
679
|
+
fn hex_character_reference(out: &mut Vec<u8>, character: u32) {
|
|
680
|
+
out.extend_from_slice(format!("&#x{character:X};").as_bytes());
|
|
681
|
+
}
|