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,181 @@
|
|
|
1
|
+
//! Nokogiri's `html_standard_serialize` (`ext/nokogiri/xml_node.c`). It writes
|
|
2
|
+
//! the HTML standard serialization of a node, the node included.
|
|
3
|
+
|
|
4
|
+
use super::{is_html, VOID_ELEMENTS};
|
|
5
|
+
use crate::dom::{c_string, Attribute, Document, NodeId, NodeKind, QualName};
|
|
6
|
+
use html5ever::ns;
|
|
7
|
+
|
|
8
|
+
const RAW_TEXT_ELEMENTS: &[&str] = &[
|
|
9
|
+
"style",
|
|
10
|
+
"script",
|
|
11
|
+
"xmp",
|
|
12
|
+
"iframe",
|
|
13
|
+
"noembed",
|
|
14
|
+
"noframes",
|
|
15
|
+
"plaintext",
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
/// The HTML of the node. With `preserve_newline`, a newline that starts
|
|
19
|
+
/// a `pre`, `textarea` or `listing` is written twice, so that parsing
|
|
20
|
+
/// the output keeps it. `scripting_enabled` writes `noscript` text
|
|
21
|
+
/// as is, as for a document parsed with `parse_noscript_content_as_text`. Walks
|
|
22
|
+
/// the tree without recursion, so depth uses no stack.
|
|
23
|
+
pub fn serialize(
|
|
24
|
+
document: &Document,
|
|
25
|
+
id: NodeId,
|
|
26
|
+
preserve_newline: bool,
|
|
27
|
+
scripting_enabled: bool,
|
|
28
|
+
) -> Vec<u8> {
|
|
29
|
+
enum Step {
|
|
30
|
+
Open(NodeId),
|
|
31
|
+
Close(NodeId),
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let mut out = Vec::new();
|
|
35
|
+
let mut steps = vec![Step::Open(id)];
|
|
36
|
+
while let Some(step) = steps.pop() {
|
|
37
|
+
let id = match step {
|
|
38
|
+
Step::Open(id) => id,
|
|
39
|
+
Step::Close(id) => {
|
|
40
|
+
if let NodeKind::Element(element) = document.node_kind(id) {
|
|
41
|
+
out.extend_from_slice(b"</");
|
|
42
|
+
write_tag_name(&mut out, &element.name);
|
|
43
|
+
out.push(b'>');
|
|
44
|
+
}
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
let open_children = match document.node_kind(id) {
|
|
49
|
+
NodeKind::Element(element) => {
|
|
50
|
+
out.push(b'<');
|
|
51
|
+
write_tag_name(&mut out, &element.name);
|
|
52
|
+
for &attribute in &element.attributes {
|
|
53
|
+
if let NodeKind::Attribute(attribute) = document.node_kind(attribute) {
|
|
54
|
+
out.push(b' ');
|
|
55
|
+
write_attribute(&mut out, attribute);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
out.push(b'>');
|
|
59
|
+
if is_html(&element.name) && VOID_ELEMENTS.contains(&&*element.name.local) {
|
|
60
|
+
false
|
|
61
|
+
} else {
|
|
62
|
+
if preserve_newline && prepend_newline(document, id) {
|
|
63
|
+
out.push(b'\n');
|
|
64
|
+
}
|
|
65
|
+
steps.push(Step::Close(id));
|
|
66
|
+
true
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
NodeKind::Attribute(attribute) => {
|
|
70
|
+
write_attribute(&mut out, attribute);
|
|
71
|
+
false
|
|
72
|
+
}
|
|
73
|
+
NodeKind::Text(text) => {
|
|
74
|
+
let raw = document.parent(id).is_some_and(|parent| {
|
|
75
|
+
matches!(document.node_kind(parent), NodeKind::Element(e)
|
|
76
|
+
if is_html(&e.name) && (RAW_TEXT_ELEMENTS.contains(&&*e.name.local)
|
|
77
|
+
|| scripting_enabled && &*e.name.local == "noscript"))
|
|
78
|
+
});
|
|
79
|
+
if raw {
|
|
80
|
+
out.extend_from_slice(c_string(text));
|
|
81
|
+
} else {
|
|
82
|
+
write_escaped(&mut out, text, false);
|
|
83
|
+
}
|
|
84
|
+
false
|
|
85
|
+
}
|
|
86
|
+
NodeKind::CData(text) => {
|
|
87
|
+
write_all(&mut out, &[b"<![CDATA[", c_string(text), b"]]>"]);
|
|
88
|
+
false
|
|
89
|
+
}
|
|
90
|
+
NodeKind::Comment(text) => {
|
|
91
|
+
write_all(&mut out, &[b"<!--", c_string(text), b"-->"]);
|
|
92
|
+
false
|
|
93
|
+
}
|
|
94
|
+
// libxml2 keeps the data of a processing instruction
|
|
95
|
+
// as its content.
|
|
96
|
+
NodeKind::ProcessingInstruction(pi) => {
|
|
97
|
+
write_all(&mut out, &[b"<?", c_string(&pi.data), b">"]);
|
|
98
|
+
false
|
|
99
|
+
}
|
|
100
|
+
NodeKind::Doctype(doctype) => {
|
|
101
|
+
write_all(&mut out, &[b"<!DOCTYPE ", c_string(&doctype.name), b">"]);
|
|
102
|
+
false
|
|
103
|
+
}
|
|
104
|
+
NodeKind::Document | NodeKind::DocumentFragment => true,
|
|
105
|
+
};
|
|
106
|
+
if open_children {
|
|
107
|
+
let first = steps.len();
|
|
108
|
+
steps.extend(document.children(id).map(Step::Open));
|
|
109
|
+
steps[first..].reverse();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
out
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/// Whether a `pre`, `textarea` or `listing` starts with a newline,
|
|
116
|
+
/// as `prepend_newline?` of Nokogiri reports. It checks the name only.
|
|
117
|
+
pub fn prepend_newline(document: &Document, id: NodeId) -> bool {
|
|
118
|
+
let NodeKind::Element(element) = document.node_kind(id) else {
|
|
119
|
+
return false;
|
|
120
|
+
};
|
|
121
|
+
matches!(&*element.name.local, "pre" | "textarea" | "listing")
|
|
122
|
+
&& document.first_child(id).is_some_and(|child| {
|
|
123
|
+
matches!(document.node_kind(child), NodeKind::Text(text) if text.first() == Some(&b'\n'))
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
fn write_tag_name(out: &mut Vec<u8>, name: &QualName) {
|
|
128
|
+
let foreign = ![ns!(), ns!(html), ns!(svg), ns!(mathml)].contains(&name.ns);
|
|
129
|
+
if let (true, Some(prefix)) = (foreign, &name.prefix) {
|
|
130
|
+
write_all(out, &[prefix.as_bytes(), b":"]);
|
|
131
|
+
}
|
|
132
|
+
out.extend_from_slice(name.local.as_bytes());
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
fn write_attribute(out: &mut Vec<u8>, attribute: &Attribute) {
|
|
136
|
+
let name = &attribute.name;
|
|
137
|
+
let prefix = match &name.ns {
|
|
138
|
+
ns if *ns == ns!(xml) => Some("xml"),
|
|
139
|
+
ns if *ns == ns!(xmlns) && &*name.local != "xmlns" => Some("xmlns"),
|
|
140
|
+
ns if *ns == ns!(xlink) => Some("xlink"),
|
|
141
|
+
ns if *ns == ns!() || *ns == ns!(xmlns) => None,
|
|
142
|
+
_ => name.prefix.as_deref(),
|
|
143
|
+
};
|
|
144
|
+
if let Some(prefix) = prefix {
|
|
145
|
+
write_all(out, &[prefix.as_bytes(), b":"]);
|
|
146
|
+
}
|
|
147
|
+
out.extend_from_slice(name.local.as_bytes());
|
|
148
|
+
out.extend_from_slice(b"=\"");
|
|
149
|
+
write_escaped(out, &attribute.value, true);
|
|
150
|
+
out.push(b'"');
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
fn write_escaped(out: &mut Vec<u8>, text: &[u8], attribute: bool) {
|
|
154
|
+
crate::work::add(text.len());
|
|
155
|
+
let text = c_string(text);
|
|
156
|
+
let mut start = 0;
|
|
157
|
+
let mut index = 0;
|
|
158
|
+
while index < text.len() {
|
|
159
|
+
let (replacement, length): (&[u8], usize) = match text[index] {
|
|
160
|
+
b'&' => (b"&", 1),
|
|
161
|
+
0xC2 if text.get(index + 1) == Some(&0xA0) => (b" ", 2),
|
|
162
|
+
b'"' if attribute => (b""", 1),
|
|
163
|
+
b'<' => (b"<", 1),
|
|
164
|
+
b'>' => (b">", 1),
|
|
165
|
+
_ => {
|
|
166
|
+
index += 1;
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
write_all(out, &[&text[start..index], replacement]);
|
|
171
|
+
index += length;
|
|
172
|
+
start = index;
|
|
173
|
+
}
|
|
174
|
+
out.extend_from_slice(&text[start..]);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
fn write_all(out: &mut Vec<u8>, parts: &[&[u8]]) {
|
|
178
|
+
for part in parts {
|
|
179
|
+
out.extend_from_slice(part);
|
|
180
|
+
}
|
|
181
|
+
}
|