use crate::query::QueryOp;
pub fn translate(src: &str) -> Result<Vec<QueryOp>, String> {
let s = src.trim();
if s.is_empty() {
return Err("xpath: empty expression".into());
}
if let Some(rest) = s.strip_prefix('@') {
return Ok(vec![QueryOp::Attr(validate_name(rest, s)?)]);
}
if !s.starts_with('/') {
return Err(format!(
"xpath: expression must start with `/`, `//`, or `@`: `{src}`"
));
}
if let Some(branches) = split_union(s) {
let mut sels = Vec::new();
for b in &branches {
let b = b.trim();
if !b.starts_with('/') {
return Err(format!("xpath: a union branch must start with `/`: `{b}`"));
}
if b.ends_with("/text()") || b.contains("/@") {
return Err(format!(
"xpath: a union branch cannot carry an accessor: `{b}`"
));
}
sels.push(path_to_css(b, s)?);
}
return Ok(vec![QueryOp::Find(sels.join(", "))]);
}
let (path, trailer) = if let Some(p) = s.strip_suffix("/text()") {
(p, Some(Trailer::Text))
} else if let Some(idx) = s.rfind("/@") {
(
&s[..idx],
Some(Trailer::Attr(validate_name(&s[idx + 2..], s)?)),
)
} else {
(s, None)
};
let mut ops = vec![QueryOp::Find(path_to_css(path, s)?)];
match trailer {
Some(Trailer::Attr(a)) => ops.push(QueryOp::Attr(a)),
Some(Trailer::Text) => ops.push(QueryOp::Text),
None => {}
}
Ok(ops)
}
enum Trailer {
Attr(String),
Text,
}
fn path_to_css(path: &str, whole: &str) -> Result<String, String> {
let mut css: Vec<String> = Vec::new();
let mut rest = path;
let mut first = true;
while !rest.is_empty() {
let descendant = if let Some(r) = rest.strip_prefix("//") {
rest = r;
true
} else if let Some(r) = rest.strip_prefix('/') {
rest = r;
false
} else {
return Err(format!("xpath: malformed path near `{rest}` in `{whole}`"));
};
let end = rest.find('/').unwrap_or(rest.len());
let (step, tail) = rest.split_at(end);
rest = tail;
let sel = step_to_css(step, whole)?;
if first {
css.push(sel);
first = false;
} else if descendant {
css.push(format!(" {sel}"));
} else {
css.push(format!(" > {sel}"));
}
}
if css.is_empty() {
return Err(format!("xpath: empty location path in `{whole}`"));
}
Ok(css.concat())
}
fn step_to_css(step: &str, whole: &str) -> Result<String, String> {
if step.is_empty() {
return Err(format!("xpath: empty step in `{whole}` (a stray `/`?)"));
}
let (tag, pred) = match step.find('[') {
Some(b) => {
if !step.ends_with(']') {
return Err(format!("xpath: malformed predicate in step `{step}`"));
}
(&step[..b], Some(&step[b + 1..step.len() - 1]))
}
None => (step, None),
};
let tag_css = if tag == "*" {
"*".to_string()
} else if is_ident(tag) {
tag.to_string()
} else if tag.contains("::") || tag == ".." || tag == "." {
return Err(format!(
"xpath: axis `{tag}` is not supported (subset: `//`, `/`, `@`)"
));
} else if tag.contains('(') {
return Err(format!("xpath: function `{tag}` is not supported"));
} else {
return Err(format!("xpath: unsupported step `{step}` in `{whole}`"));
};
match pred {
None => Ok(tag_css),
Some(p) => Ok(format!("{tag_css}{}", predicate_to_css(p, step)?)),
}
}
fn predicate_to_css(pred: &str, step: &str) -> Result<String, String> {
if let Some(inner) = pred
.strip_prefix("contains(")
.and_then(|s| s.strip_suffix(')'))
{
let (a, v) = inner
.split_once(',')
.ok_or_else(|| format!("xpath: `contains()` needs `(@attr, 'value')` in `[{pred}]`"))?;
let attr = a
.trim()
.strip_prefix('@')
.filter(|a| is_ident(a))
.ok_or_else(|| {
format!("xpath: `contains()` first arg must be `@attr` in `[{pred}]`")
})?;
let val = unquote(v.trim())
.ok_or_else(|| format!("xpath: `contains()` value must be quoted in `[{pred}]`"))?;
return Ok(format!("[{attr}*=\"{}\"]", css_escape(&val)));
}
let attr_part = pred.strip_prefix('@').ok_or_else(|| {
format!(
"xpath: unsupported predicate `[{pred}]` in step `{step}` \
(use `[@attr]`, `[@attr='v']`, or `[contains(@attr,'v')]`)"
)
})?;
match attr_part.split_once('=') {
None => {
if !is_ident(attr_part) {
return Err(format!("xpath: unsupported predicate `[{pred}]`"));
}
Ok(format!("[{attr_part}]"))
}
Some((name, val)) => {
let name = name.trim();
if !is_ident(name) {
return Err(format!("xpath: unsupported attribute in `[{pred}]`"));
}
let val = unquote(val.trim()).ok_or_else(|| {
format!("xpath: value in `[{pred}]` must be quoted (`[@{name}='v']`)")
})?;
Ok(format!("[{name}=\"{}\"]", css_escape(&val)))
}
}
}
fn split_union(s: &str) -> Option<Vec<&str>> {
let (mut depth, mut quote) = (0i32, 0u8);
let mut parts = Vec::new();
let mut start = 0;
for (i, &c) in s.as_bytes().iter().enumerate() {
if quote != 0 {
if c == quote {
quote = 0;
}
} else {
match c {
b'\'' | b'"' => quote = c,
b'[' => depth += 1,
b']' => depth -= 1,
b'|' if depth == 0 => {
parts.push(&s[start..i]);
start = i + 1;
}
_ => {}
}
}
}
if parts.is_empty() {
None
} else {
parts.push(&s[start..]);
Some(parts)
}
}
fn unquote(s: &str) -> Option<String> {
let b = s.as_bytes();
if b.len() >= 2 && (b[0] == b'\'' || b[0] == b'"') && b[b.len() - 1] == b[0] {
Some(s[1..s.len() - 1].to_string())
} else {
None
}
}
fn css_escape(s: &str) -> String {
s.replace('\\', "\\\\").replace('"', "\\\"")
}
fn is_ident(s: &str) -> bool {
!s.is_empty()
&& s.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
}
fn validate_name(name: &str, whole: &str) -> Result<String, String> {
if is_ident(name) {
Ok(name.to_string())
} else {
Err(format!(
"xpath: unsupported attribute name in `{whole}` (use `@name`)"
))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::query::{eval, QueryOp, QueryResult};
fn ops(src: &str) -> Vec<QueryOp> {
translate(src).unwrap()
}
fn run(src: &str, html: &str) -> Vec<String> {
match eval(&translate(src).unwrap(), &[html.to_string()], 0.0) {
QueryResult::Lines(v) => v,
other => panic!("expected Lines, got {other:?}"),
}
}
#[test]
fn descendant_and_child_paths() {
assert!(matches!(ops("//a").as_slice(), [QueryOp::Find(s)] if s == "a"));
assert!(matches!(ops("//div//span").as_slice(), [QueryOp::Find(s)] if s == "div span"));
assert!(matches!(ops("//div/span").as_slice(), [QueryOp::Find(s)] if s == "div > span"));
assert!(
matches!(ops("/html/body/p").as_slice(), [QueryOp::Find(s)] if s == "html > body > p")
);
}
#[test]
fn attr_predicate_and_accessors() {
assert!(matches!(ops("//a[@href]").as_slice(), [QueryOp::Find(s)] if s == "a[href]"));
assert!(
matches!(ops("//a/@href").as_slice(), [QueryOp::Find(s), QueryOp::Attr(a)] if s == "a" && a == "href")
);
assert!(
matches!(ops("//a/text()").as_slice(), [QueryOp::Find(s), QueryOp::Text] if s == "a")
);
assert!(matches!(ops("@href").as_slice(), [QueryOp::Attr(a)] if a == "href"));
assert!(matches!(ops("//*").as_slice(), [QueryOp::Find(s)] if s == "*"));
}
#[test]
fn end_to_end_over_html() {
let doc = "<div><a href=\"x\">1</a><a href=\"y\">2</a></div>";
assert_eq!(run("//a/@href", doc), vec!["x", "y"]);
assert_eq!(run("//a/text()", doc), vec!["1", "2"]);
let two = [translate("//a").unwrap(), translate("@href").unwrap()].concat();
match eval(&two, &[doc.to_string()], 0.0) {
QueryResult::Lines(v) => assert_eq!(v, vec!["x", "y"]),
other => panic!("{other:?}"),
}
}
#[test]
fn union_value_predicate_and_contains() {
assert!(matches!(ops("//a|//b").as_slice(), [QueryOp::Find(s)] if s == "a, b"));
assert!(
matches!(ops("//div//span | //p").as_slice(), [QueryOp::Find(s)] if s == "div span, p")
);
assert!(
matches!(ops("//a[@class='btn']").as_slice(), [QueryOp::Find(s)] if s == "a[class=\"btn\"]")
);
assert!(
matches!(ops("//input[@type='text']").as_slice(), [QueryOp::Find(s)] if s == "input[type=\"text\"]")
);
assert!(
matches!(ops("//a[contains(@href,'x')]").as_slice(), [QueryOp::Find(s)] if s == "a[href*=\"x\"]")
);
assert!(matches!(ops("//a[@href]").as_slice(), [QueryOp::Find(s)] if s == "a[href]"));
assert!(
matches!(ops("//a[@class='x|y']").as_slice(), [QueryOp::Find(s)] if s == "a[class=\"x|y\"]")
);
assert_eq!(
run(
"//a[@class='x']/text()",
"<a class='x'>1</a><a class='y'>2</a>"
),
vec!["1"]
);
}
#[test]
fn unsupported_constructs_error_not_mishandle() {
for bad in [
"//a[1]", "//a[last()]", "//a[text()='x']", "//a[@class=btn]", "//following-sibling::b", "//..", ".", "//a/@href|//b", "count(//a)", "//ns:a", "@*", "foo", ] {
assert!(translate(bad).is_err(), "expected `{bad}` to error");
}
}
}