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,118 @@
|
|
|
1
|
+
//! The XPath engine. The CSS of Nokogiri compiles to XPath and libxml2 runs
|
|
2
|
+
//! it, so CSS semantics are XPath semantics. The CSS front end (`crate::css`)
|
|
3
|
+
//! builds the [`Expr`]s that the visitor of Nokogiri would write as strings.
|
|
4
|
+
//! [`evaluate`] runs them as libxml2 would. [`parse`] builds the same trees
|
|
5
|
+
//! from XPath strings (plan.md decision 2).
|
|
6
|
+
|
|
7
|
+
mod ast;
|
|
8
|
+
pub(crate) mod chars;
|
|
9
|
+
mod eval;
|
|
10
|
+
mod parse;
|
|
11
|
+
#[cfg(test)]
|
|
12
|
+
mod tests;
|
|
13
|
+
|
|
14
|
+
use ast::Merge;
|
|
15
|
+
pub use ast::{
|
|
16
|
+
Arithmetic, Axis, Comparison, Expr, Filter, Function, NodeTest, Operator, Path, QName, Step,
|
|
17
|
+
};
|
|
18
|
+
pub use eval::{evaluate, Answer, Call, Outcome, Value};
|
|
19
|
+
pub use parse::{parse, ParseError, MAX_NESTING};
|
|
20
|
+
|
|
21
|
+
use std::fmt;
|
|
22
|
+
|
|
23
|
+
pub const NOKOGIRI_URI: &str = "http://www.nokogiri.org/default_ns/ruby/extensions_functions";
|
|
24
|
+
pub const NOKOGIRI_BUILTIN_URI: &str = "https://www.nokogiri.org/default_ns/ruby/builtins";
|
|
25
|
+
pub const XQUERY_FUNCTIONS_URI: &str = "http://www.w3.org/2002/08/xquery-functions";
|
|
26
|
+
|
|
27
|
+
#[derive(Debug)]
|
|
28
|
+
pub struct Query {
|
|
29
|
+
pub(crate) expr: Expr,
|
|
30
|
+
functions: Vec<FunctionName>,
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
34
|
+
pub struct FunctionName {
|
|
35
|
+
pub prefix: Option<String>,
|
|
36
|
+
pub local: String,
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
impl Query {
|
|
40
|
+
/// A query compiled from CSS. `functions` names each custom call
|
|
41
|
+
/// by its `site`, as the visitor of Nokogiri writes `nokogiri:name(…)`.
|
|
42
|
+
pub fn new(mut expr: Expr, functions: Vec<String>) -> Query {
|
|
43
|
+
expr.merge_descendant_steps(Merge::UnlessPositional);
|
|
44
|
+
let functions = functions
|
|
45
|
+
.into_iter()
|
|
46
|
+
.map(|local| FunctionName {
|
|
47
|
+
prefix: Some("nokogiri".into()),
|
|
48
|
+
local,
|
|
49
|
+
})
|
|
50
|
+
.collect();
|
|
51
|
+
Query { expr, functions }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
fn xpath(mut expr: Expr, functions: Vec<FunctionName>) -> Query {
|
|
55
|
+
expr.merge_descendant_steps(Merge::WithoutPredicates);
|
|
56
|
+
Query { expr, functions }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
pub fn function(&self, site: usize) -> &FunctionName {
|
|
60
|
+
&self.functions[site]
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/// The XPath errors of libxml2 that Nokogiri raises as `XPath::SyntaxError`,
|
|
65
|
+
/// numbered as libxml2's `xmlXPathError`.
|
|
66
|
+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
67
|
+
pub enum Error {
|
|
68
|
+
UnfinishedLiteral = 2,
|
|
69
|
+
StartLiteral = 3,
|
|
70
|
+
VariableReference = 4,
|
|
71
|
+
UndefinedVariable = 5,
|
|
72
|
+
InvalidPredicate = 6,
|
|
73
|
+
InvalidExpression = 7,
|
|
74
|
+
Unclosed = 8,
|
|
75
|
+
UnregisteredFunction = 9,
|
|
76
|
+
InvalidType = 11,
|
|
77
|
+
InvalidArity = 12,
|
|
78
|
+
UndefinedNamespacePrefix = 19,
|
|
79
|
+
Encoding = 20,
|
|
80
|
+
InvalidChar = 21,
|
|
81
|
+
StackUsage = 23,
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
impl Error {
|
|
85
|
+
/// The `xmlError` code: `XML_XPATH_EXPRESSION_OK` (1200) plus the number
|
|
86
|
+
/// of the XPath error.
|
|
87
|
+
pub fn code(self) -> i32 {
|
|
88
|
+
1200 + self as i32
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
pub fn message(self) -> &'static str {
|
|
92
|
+
match self {
|
|
93
|
+
Error::UnfinishedLiteral => "Unfinished literal",
|
|
94
|
+
Error::StartLiteral => "Start of literal",
|
|
95
|
+
Error::VariableReference => "Expected $ for variable reference",
|
|
96
|
+
Error::UndefinedVariable => "Undefined variable",
|
|
97
|
+
Error::InvalidPredicate => "Invalid predicate",
|
|
98
|
+
Error::InvalidExpression => "Invalid expression",
|
|
99
|
+
// Sic: this is the message of libxml2 for a missing `)`.
|
|
100
|
+
Error::Unclosed => "Missing closing curly brace",
|
|
101
|
+
Error::UnregisteredFunction => "Unregistered function",
|
|
102
|
+
Error::InvalidType => "Invalid type",
|
|
103
|
+
Error::InvalidArity => "Invalid number of arguments",
|
|
104
|
+
Error::UndefinedNamespacePrefix => "Undefined namespace prefix",
|
|
105
|
+
Error::Encoding => "Encoding error",
|
|
106
|
+
Error::InvalidChar => "Char out of XML range",
|
|
107
|
+
Error::StackUsage => "Stack usage error",
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
impl fmt::Display for Error {
|
|
113
|
+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
114
|
+
f.write_str(self.message())
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
impl std::error::Error for Error {}
|