Fix tests on comments and now accept &str and String

master
kirbylife 2021-03-21 01:29:10 -06:00
parent 46fdefc893
commit 22ebd0780e
2 changed files with 13 additions and 10 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "cssifier" name = "cssifier"
version = "0.1.2" version = "0.1.3"
authors = ["kirbylife <kirbylife@protonmail.com>"] authors = ["kirbylife <kirbylife@protonmail.com>"]
license = " GPL-3.0-or-later" license = " GPL-3.0-or-later"
readme = "README.md" readme = "README.md"

View File

@ -12,19 +12,22 @@ use regex::Regex;
/// ///
/// Basic usage: /// Basic usage:
/// ``` /// ```
/// cssifier("//a/p") /// let result = cssifier::cssifier("//a/p");
/// Some("a p") /// assert_eq!(result, Some("a p".to_string()));
/// ///
/// cssifier("//a/p[@id='hello']") /// let result = cssifier::cssifier("//a/p[@id='hello']");
/// Some("a b#hello") /// assert_eq!(result, Some("a p#hello".to_string()));
/// ///
/// cssfier("//a/p/[contains(text(), 'hello')]") /// let result = cssifier::cssifier("//a/p[contains(text(), 'hello')]");
/// Some(a b:contains(hello)) /// assert_eq!(result, Some("a p:contains(hello)".to_string()));
/// ///
/// cssifier("*random selector//*") // Invalid selectors throw a empty string (WIP) /// let result = cssifier::cssifier("*random selector//*"); // Invalid selectors throw a empty string (WIP)
/// Some("") /// 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 // 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 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(); let mut css = String::new();