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