diff --git a/Cargo.toml b/Cargo.toml index 7cf828f..bf18b86 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cssifier" -version = "0.1.2" +version = "0.1.3" authors = ["kirbylife <kirbylife@protonmail.com>"] license = " GPL-3.0-or-later" readme = "README.md" diff --git a/src/lib.rs b/src/lib.rs index 19daaa6..2acf6bf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,19 +12,22 @@ use regex::Regex; /// /// Basic usage: /// ``` -/// cssifier("//a/p") -/// Some("a p") +/// let result = cssifier::cssifier("//a/p"); +/// assert_eq!(result, Some("a p".to_string())); /// -/// cssifier("//a/p[@id='hello']") -/// Some("a b#hello") +/// let result = cssifier::cssifier("//a/p[@id='hello']"); +/// assert_eq!(result, Some("a p#hello".to_string())); /// -/// cssfier("//a/p/[contains(text(), 'hello')]") -/// Some(a b:contains(hello)) +/// let result = cssifier::cssifier("//a/p[contains(text(), 'hello')]"); +/// assert_eq!(result, Some("a p:contains(hello)".to_string())); /// -/// cssifier("*random selector//*") // Invalid selectors throw a empty string (WIP) -/// Some("") +/// let result = cssifier::cssifier("*random selector//*"); // Invalid selectors throw a empty string (WIP) +/// assert_eq!(result, Some("".to_string())); /// ``` -pub fn cssifier(xpath: &'static str) -> Option<String> { +pub fn cssifier<S: AsRef<str>>(xpath: S) -> Option<String> { + // Trait to &str + let xpath = xpath.as_ref(); + // Ultra magic regex to parse XPath selectors let reg = Regex::new(r#"(?P<node>(^id\(["']?(?P<idvalue>\s*[\w/:][-/\w\s,:;.]*)["']?\)|(?P<nav>//?)(?P<tag>([a-zA-Z][a-zA-Z0-9]{0,10}|\*))(\[((?P<matched>(?P<mattr>@?[.a-zA-Z_:][-\w:.]*(\(\))?)=["'](?P<mvalue>\s*[\w/:][-/\w\s,:;.]*))["']|(?P<contained>contains\((?P<cattr>@?[.a-zA-Z_:][-\w:.]*(\(\))?),\s*["'](?P<cvalue>\s*[\w/:][-/\w\s,:;.]*)["']\)))\])?(\[(?P<nth>\d)\])?))"#).unwrap(); let mut css = String::new();