| 1 | //! Regexps to match html elements |
| 2 | //! |
| 3 | #![allow(non_upper_case_globals)] |
| 4 | use const_format::formatcp; |
| 5 | use once_cell::sync::Lazy; |
| 6 | use regex::Regex; |
| 7 | |
| 8 | const attr_name : &str = r#"[a-zA-Z_:][a-zA-Z0-9:._-]*"#; |
| 9 | |
| 10 | const unquoted : &str = r#"[^"'=<>`\x00-\x20]+"#; |
| 11 | const single_quoted : &str = r#"'[^']*'"#; |
| 12 | const double_quoted : &str = r#""[^"]*""#; |
| 13 | |
| 14 | const attr_value : &str = formatcp!("(?:{unquoted}|{single_quoted}|{double_quoted})"); |
| 15 | |
| 16 | const attribute : &str = formatcp!("(?:\\s+{attr_name}(?:\\s*=\\s*{attr_value})?)"); |
| 17 | |
| 18 | const open_tag : &str = formatcp!("<[A-Za-z][A-Za-z0-9\\-]*{attribute}*\\s*/?>"); |
| 19 | |
| 20 | const close_tag : &str = r#"</[A-Za-z][A-Za-z0-9\-]*\s*>"#; |
| 21 | const comment : &str = r#"<!---->|<!--(?:-?[^>-])(?:-?[^-])*-->"#; |
| 22 | const processing : &str = r#"<[?][\s\S]*?[?]>"#; |
| 23 | const declaration : &str = r#"<![A-Z]+\s+[^>]*>"#; |
| 24 | const cdata : &str = r#"<!\[CDATA\[[\s\S]*?\]\]>"#; |
| 25 | |
| 26 | #[allow(clippy::double_parens)] |
| 27 | pub static HTML_TAG_RE : Lazy<Regex> = Lazy::new(|| { |
| 28 | Regex::new( |
| 29 | formatcp!("^(?:{open_tag}|{close_tag}|{comment}|{processing}|{declaration}|{cdata})") |
| 30 | ).unwrap() |
| 31 | }); |
| 32 | |
| 33 | #[allow(clippy::double_parens)] |
| 34 | pub static HTML_OPEN_CLOSE_TAG_RE : Lazy<Regex> = Lazy::new(|| { |
| 35 | Regex::new( |
| 36 | formatcp!("^(?:{open_tag}|{close_tag})") |
| 37 | ).unwrap() |
| 38 | }); |
| 39 | |
| 40 | pub static HTML_LINK_OPEN : Lazy<Regex> = Lazy::new(|| { |
| 41 | Regex::new(r#"^<a[>\s]"#).unwrap() |
| 42 | }); |
| 43 | |
| 44 | pub static HTML_LINK_CLOSE : Lazy<Regex> = Lazy::new(|| { |
| 45 | Regex::new(r#"^</a\s*>"#).unwrap() |
| 46 | }); |