use regex::Regex;
use std::sync::LazyLock;
use std::vec::IntoIter;
static SELECTOR_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r#"(?x)
\s*([*>~+])\s* | # all elements, child, sibling
(:not)\(((?:[^()'"]|'[^']*'|"[^"]*")+)\) | # negation
(:[A-Za-z][A-Za-z-]+)(?:\((\d+)\))? | # root, pseudo-classes
(\.[A-Za-z-_][A-Za-z0-9-_]*) | # class
(\#[A-Za-z][A-Za-z0-9-_.]*) | # id
([A-Za-z][A-Za-z0-9]*) | # tag
(\[)([A-Za-z][\w-]*)(]) | # attribute without value
(\[)([A-Za-z][\w-]*)([\^$*~|]?=)'([^']+)'(]) | # attribute single quotes
(\[)([A-Za-z][\w-]*)([\^$*~|]?=)"([^"]+)"(]) | # attribute double quotes
(\s)\s* | # descendant combinator
"#,
)
.expect("static selector regex compiles")
});
pub fn xpathify(css: &str) -> String {
let mut items: Vec<String> = vec![];
let mut tokens = tokenize(css).into_iter();
loop {
if convert(&mut tokens, &mut items) {
break;
}
}
let xpath = items.join("");
if xpath != "/" && (xpath.ends_with('/') || xpath.ends_with("::")) {
return String::new();
}
xpath
}
fn tokenize(css: &str) -> Vec<Vec<&str>> {
let mut out = Vec::new();
let mut cursor = 0usize;
for m in SELECTOR_REGEX.captures_iter(css) {
let whole = m.get(0).expect("capture group 0 always present");
if !css[cursor..whole.start()].trim().is_empty() {
return vec![];
}
cursor = whole.end();
out.push(
m.iter()
.enumerate()
.filter(|(index, s)| *index > 0 && s.is_some())
.map(|(_, s)| s.expect("filtered group is Some").as_str())
.collect::<Vec<&str>>(),
);
}
if !css[cursor..].trim().is_empty() {
return vec![];
}
out
}
fn convert(tokens: &mut IntoIter<Vec<&str>>, items: &mut Vec<String>) -> bool {
if let Some(token) = tokens.next() {
match *token.as_slice() {
[":root"] if items.is_empty() => {
items.push("/".to_string());
}
[":root"] => {
items.clear();
return true;
}
[":first-of-type"] => {
push_star_if_empty_or_no_tag(items);
items.push("[1]".to_string());
}
[":last-of-type"] => {
push_star_if_empty_or_no_tag(items);
items.push("[last()]".to_string());
}
[":first-child"] => {
push_star_if_empty_or_no_tag(items);
items.push("[not(preceding-sibling::*)]".to_string());
}
[":last-child"] => {
push_star_if_empty_or_no_tag(items);
items.push("[not(following-sibling::*)]".to_string());
}
[":nth-child", position] => {
push_star_if_empty_or_no_tag(items);
items.push(format!("[(count(preceding-sibling::*)+1) = {position}]"));
}
[":nth-of-type", position] => {
push_star_if_empty_or_no_tag(items);
items.push(format!("[{position}]"));
}
[s] if s.starts_with("#") => {
push_star_if_empty_or_no_tag(items);
items.push(format!("[{}]", id_predicate(s.split_at(1).1)));
}
[s] if s.starts_with(".") => {
push_star_if_empty_or_no_tag(items);
items.push(format!("[{}]", class_predicate(s.split_at(1).1)));
}
[":not", argument] => {
let Some(predicate) = negation_predicate(argument) else {
items.clear();
return true;
};
push_star_if_empty_or_no_tag(items);
items.push(format!("[not({predicate})]"));
}
[" "] if items.is_empty() => {}
[" "] if tokens.as_slice().is_empty() => return true,
[" "] => {
items.push("//".to_string());
}
[">"] if items.is_empty() => {
items.clear();
return true;
}
[">"] => {
if items[items.len() - 1] != "/" {
items.push("/".to_string());
}
}
["~" | "+"] if !items.is_empty() && items[items.len() - 1] != "/" => {
if matches!(tokens.as_slice().first().map(Vec::as_slice), Some([">" | "~" | "+" | " "])) {
items.clear();
return true;
}
items.push("/following-sibling::".to_string());
if convert(tokens, items) {
items.clear();
return true;
}
if token[0] == "+" {
items.push("[1]".to_string());
}
}
["~" | "+"] => {
items.clear();
return true;
}
["[", attribute, "]"] => {
push_star_if_empty_or_no_tag(items);
items.push("[".to_string());
items.push(format!("@{attribute}"));
items.push("]".to_string());
}
["[", attribute, operator, value, "]"] => {
push_star_if_empty_or_no_tag(items);
items.push("[".to_string());
let literal = xpath_literal(&normalize_value(value), '\'');
let quoted = xpath_literal(&normalize_value(value), '"');
match operator {
"^=" => {
items.push(format!("starts-with(@{attribute}, {literal})"));
}
"$=" => {
items.push(format!(
"substring(@{attribute}, string-length(@{attribute}) - string-length({literal}) + 1) = {literal}"
));
}
"*=" => {
items.push(format!("contains(@{attribute}, {literal})"));
}
"~=" => {
let padded = xpath_literal(&format!(" {} ", normalize_value(value)), '\'');
items.push(format!("contains(concat(' ',normalize-space(@{attribute}),' '),{padded})"));
}
"|=" => {
items
.push(format!("@{attribute}={quoted} or starts-with(@{attribute},concat({quoted},\"-\"))"));
}
_ => {
items.push(format!("@{attribute}{operator}{quoted}"));
}
}
items.push("]".to_string());
}
[s] if !s.starts_with(":") => {
if items.is_empty() {
items.push("//".to_string());
}
items.push(s.to_string());
}
_ => {
items.clear();
return true;
}
};
false
} else {
true
}
}
fn class_predicate(class: &str) -> String {
format!("contains(concat(' ',normalize-space(@class),' '),' {class} ')")
}
fn id_predicate(id: &str) -> String {
format!("@id=\"{id}\"")
}
fn normalize_value(value: &str) -> String {
value.replace('\\', "")
}
fn xpath_literal(value: &str, preferred: char) -> String {
let fallback = if preferred == '"' { '\'' } else { '"' };
if !value.contains(preferred) {
return format!("{preferred}{value}{preferred}");
}
if !value.contains(fallback) {
return format!("{fallback}{value}{fallback}");
}
let parts = value.split('"').map(|part| format!("\"{part}\"")).collect::<Vec<_>>();
format!("concat({})", parts.join(", '\"', "))
}
fn unquote(value: &str) -> &str {
let bytes = value.as_bytes();
match bytes.first() {
Some(&open @ (b'"' | b'\'')) if bytes.len() >= 2 && bytes[bytes.len() - 1] == open => {
&value[1..value.len() - 1]
}
_ => value,
}
}
fn outside_quotes(argument: &str) -> String {
let mut out = String::new();
let mut open: Option<char> = None;
for character in argument.chars() {
match open {
Some(quote) if character == quote => open = None,
Some(_) => {}
None if character == '\'' || character == '"' => open = Some(character),
None => out.push(character),
}
}
out
}
fn is_name(candidate: &str) -> bool {
candidate.starts_with(|c: char| c.is_ascii_alphabetic())
&& candidate.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
}
fn negation_predicate(argument: &str) -> Option<String> {
let argument = argument.trim();
if outside_quotes(argument).contains([' ', '>', '+', '~', ',']) {
return None;
}
if let Some(class) = argument.strip_prefix('.') {
return is_name(class).then(|| class_predicate(class));
}
if let Some(id) = argument.strip_prefix('#') {
return is_name(id).then(|| id_predicate(id));
}
if let Some(body) = argument.strip_prefix('[').and_then(|rest| rest.strip_suffix(']')) {
return match body.split_once('=') {
None => is_name(body).then(|| format!("@{body}")),
Some((name, value)) => {
is_name(name).then(|| format!("@{name}={}", xpath_literal(&normalize_value(unquote(value)), '"')))
}
};
}
is_name(argument).then(|| format!("self::{argument}"))
}
fn push_star_if_empty_or_no_tag(items: &mut Vec<String>) {
if items.is_empty() {
items.push("//*".to_string());
return;
}
let last = items[items.len() - 1].as_str();
if last == "/" || last == "//" || last.ends_with("::") {
items.push("*".to_string());
}
}
#[cfg(test)]
mod tests {
use super::{tokenize, xpath_literal};
use crate::xpathify;
#[rstest::rstest]
#[case::root(":root", vec![vec![":root"]])]
#[case::tag_div("div", vec![vec!["div"]])]
#[case::tag_h1("h1", vec![vec!["h1"]])]
#[case::descendant("div p", vec![vec!["div"], vec![" "], vec!["p"]])]
#[case::descendant_collapses_spaces("div p", vec![vec!["div"], vec![" "], vec!["p"]])]
#[case::child_tight("div>p", vec![vec!["div"], vec![">"], vec!["p"]])]
#[case::child_right_space("div> p", vec![vec!["div"], vec![">"], vec!["p"]])]
#[case::child_both_spaces("div > p", vec![vec!["div"], vec![">"], vec!["p"]])]
#[case::child_left_space("div >p", vec![vec!["div"], vec![">"], vec!["p"]])]
#[case::subsequent_sibling("div ~ p", vec![vec!["div"], vec!["~"], vec!["p"]])]
#[case::adjacent_sibling("div + p", vec![vec!["div"], vec!["+"], vec!["p"]])]
#[case::id_alone("#hello", vec![vec!["#hello"]])]
#[case::id_on_tag("a#hello", vec![vec!["a"], vec!["#hello"]])]
#[case::id_descendant("a #hello", vec![vec!["a"], vec![" "], vec!["#hello"]])]
#[case::class_alone(".hello", vec![vec![".hello"]])]
#[case::class_on_tag("a.hello", vec![vec!["a"], vec![".hello"]])]
#[case::class_descendant("a .hello", vec![vec!["a"], vec![" "], vec![".hello"]])]
#[case::attribute_no_value("a[rel]", vec![vec!["a"], vec!["[", "rel", "]"]])]
#[case::data_attribute_no_value("[data-id]", vec![vec!["[", "data-id", "]"]])]
#[case::data_attribute_with_value(
"[data-testid='price']",
vec![vec!["[", "data-testid", "=", "price", "]"]],
)]
#[case::not_class(".card:not(.ad)", vec![vec![".card"], vec![":not", ".ad"]])]
#[case::attribute_escaped_colon(
"div[style=\"-webkit-line-clamp\\:2\"]",
vec![vec!["div"], vec!["[", "style", "=", "-webkit-line-clamp\\:2", "]"]],
)]
#[case::attribute_double_quoted_value(
"input[type=\"submit\"]",
vec![vec!["input"], vec!["[", "type", "=", "submit", "]"]],
)]
#[case::attribute_starts_with_single_quoted(
"a[href^='/']",
vec![vec!["a"], vec!["[", "href", "^=", "/", "]"]],
)]
#[case::nth_child(
"h3:nth-child(1)",
vec![vec!["h3"], vec![":nth-child", "1"]],
)]
fn tokenizes(#[case] css: &str, #[case] expected: Vec<Vec<&str>>) {
assert_eq!(expected, tokenize(css));
}
#[rstest::rstest]
#[case::comma_between_selectors("div,span")]
#[case::unknown_combinator("div ^ span")]
#[case::trailing_garbage("div@@@")]
#[case::malformed_attribute("a[rel")]
#[case::nested_not(":not(:not(.a))")]
fn rejects_partial_tokenize(#[case] css: &str) {
assert!(tokenize(css).is_empty(), "expected empty tokens for '{css}'");
assert_eq!(xpathify(css), "", "expected empty xpath for '{css}'");
}
#[rstest::rstest]
#[case::root_alone(":root", "/")]
#[case::root_then_body(":root > body", "/body")]
#[case::tag_div("div", "//div")]
#[case::tag_h1("h1", "//h1")]
#[case::descendant("div p", "//div//p")]
#[case::descendant_collapses_spaces("div p", "//div//p")]
#[case::trailing_whitespace_is_trimmed("div ", "//div")]
#[case::surrounding_whitespace_is_trimmed(" div ", "//div")]
#[case::child_tight("div>p", "//div/p")]
#[case::child_right_space("div> p", "//div/p")]
#[case::child_both_spaces("div > p", "//div/p")]
#[case::child_left_space("div >p", "//div/p")]
#[case::child_wildcard("div > *", "//div/*")]
#[case::consecutive_child_collapses("div > > p", "//div/p")]
#[case::root_then_dangling_child(":root >", "/")]
#[case::root_descendant(":root body", "///body")]
#[case::root_consecutive_child(":root > > body", "/body")]
#[case::id_alone("#hello", "//*[@id=\"hello\"]")]
#[case::id_on_tag("a#hello", "//a[@id=\"hello\"]")]
#[case::id_descendant("a #hello", "//a//*[@id=\"hello\"]")]
#[case::class_alone(".hello", "//*[contains(concat(' ',normalize-space(@class),' '),' hello ')]")]
#[case::class_on_tag("a.hello", "//a[contains(concat(' ',normalize-space(@class),' '),' hello ')]")]
#[case::class_descendant("a .hello", "//a//*[contains(concat(' ',normalize-space(@class),' '),' hello ')]")]
#[case::attribute_no_value("a[rel]", "//a[@rel]")]
#[case::attribute_escaped_colon("div[style=\"-webkit-line-clamp\\:2\"]", "//div[@style=\"-webkit-line-clamp:2\"]")]
#[case::id_with_attribute("a#abc[for=\"xyz\"]", "//a[@id=\"abc\"][@for=\"xyz\"]")]
#[case::attribute_starts_with("a[href^='/']", "//a[starts-with(@href, '/')]")]
#[case::attribute_ends_with(
"a[href$='.pdf']",
"//a[substring(@href, string-length(@href) - string-length('.pdf') + 1) = '.pdf']"
)]
#[case::attribute_contains("a[href*='://']", "//a[contains(@href, '://')]")]
#[case::attribute_word("a[rel~='help']", "//a[contains(concat(' ',normalize-space(@rel),' '),' help ')]")]
#[case::attribute_lang_dash("[lang|=\"en\"]", "//*[@lang=\"en\" or starts-with(@lang,concat(\"en\",\"-\"))]")]
#[case::subsequent_sibling("h1 ~ ul", "//h1/following-sibling::ul")]
#[case::adjacent_sibling("h1 + ul", "//h1/following-sibling::ul[1]")]
#[case::subsequent_sibling_with_id("h1 ~ #id", "//h1/following-sibling::*[@id=\"id\"]")]
#[case::nth_child("h3:nth-child(1)", "//h3[(count(preceding-sibling::*)+1) = 1]")]
#[case::first_of_type("ul > li:first-of-type", "//ul/li[1]")]
#[case::nth_of_type("ul > li:nth-of-type(2)", "//ul/li[2]")]
#[case::last_of_type("ul > li:last-of-type", "//ul/li[last()]")]
#[case::id_with_first_of_type("li#id:first-of-type", "//li[@id=\"id\"][1]")]
#[case::first_child("a:first-child", "//a[not(preceding-sibling::*)]")]
#[case::last_child("a:last-child", "//a[not(following-sibling::*)]")]
#[case::data_attribute_no_value("[data-id]", "//*[@data-id]")]
#[case::data_attribute_with_value("[data-testid=\"price\"]", "//*[@data-testid=\"price\"]")]
#[case::data_attribute_on_tag("span[data-qa='total']", "//span[@data-qa=\"total\"]")]
#[case::aria_attribute("[aria-label=\"Close\"]", "//*[@aria-label=\"Close\"]")]
#[case::not_class_alone(":not(.ad)", "//*[not(contains(concat(' ',normalize-space(@class),' '),' ad '))]")]
#[case::not_class_on_class(
".card:not(.ad)",
"//*[contains(concat(' ',normalize-space(@class),' '),' card ')][not(contains(concat(' ',normalize-space(@class),' '),' ad '))]"
)]
#[case::not_id("div:not(#main)", "//div[not(@id=\"main\")]")]
#[case::not_attribute_no_value("a:not([rel])", "//a[not(@rel)]")]
#[case::not_attribute_with_value("input:not([type='hidden'])", "//input[not(@type=\"hidden\")]")]
#[case::not_tag(".row:not(div)", "//*[contains(concat(' ',normalize-space(@class),' '),' row ')][not(self::div)]")]
#[case::not_attribute_value_with_space("a:not([title=\"Sponsored ad\"])", "//a[not(@title=\"Sponsored ad\")]")]
#[case::not_attribute_value_with_parens(
"a:not([aria-label=\"Close (dialog)\"])",
"//a[not(@aria-label=\"Close (dialog)\")]"
)]
#[case::not_attribute_value_with_double_quote(
"a:not([title='He said \"yes\"'])",
"//a[not(@title='He said \"yes\"')]"
)]
#[case::attribute_value_with_double_quote("[title='He said \"yes\"']", "//*[@title='He said \"yes\"']")]
#[case::not_attribute_strips_css_escape(
":not([style=\"-webkit-line-clamp\\:2\"])",
"//*[not(@style=\"-webkit-line-clamp:2\")]"
)]
fn converts_css_to_xpath(#[case] css: &str, #[case] expected: &str) {
assert_eq!(expected.to_string(), xpathify(css));
}
#[rstest::rstest]
#[case::plain_value_takes_the_preferred_quote("plain", '"', "\"plain\"")]
#[case::value_holding_the_preferred_quote_takes_the_other("he said \"yes\"", '"', "'he said \"yes\"'")]
#[case::value_holding_both_quotes_is_assembled("it's \"x\"", '"', "concat(\"it's \", '\"', \"x\", '\"', \"\")")]
fn encodes_a_value_as_an_xpath_literal(#[case] value: &str, #[case] preferred: char, #[case] expected: &str) {
assert_eq!(xpath_literal(value, preferred), expected);
}
#[rstest::rstest]
#[case::trailing_child("div >")]
#[case::trailing_adjacent_sibling("h1 +")]
#[case::trailing_subsequent_sibling("h1 ~")]
#[case::child_after_adjacent_sibling("h1 + > ul")]
#[case::child_after_subsequent_sibling("h1 ~ > ul")]
fn rejects_dangling_combinator(#[case] css: &str) {
assert_eq!(xpathify(css), "", "expected empty xpath for '{css}'");
}
#[rstest::rstest]
#[case::combinator_inside_not(":not(.a > .b)")]
#[case::descendant_inside_not(":not(a b)")]
#[case::selector_list_inside_not(":not(.a, .b)")]
#[case::attribute_operator_inside_not(":not([href^='/'])")]
fn rejects_unsupported_not_argument(#[case] css: &str) {
assert_eq!(xpathify(css), "", "expected empty xpath for '{css}'");
}
}