use crate::markdown_it::js;
fn is_alpha(c: u16) -> bool {
matches!(c, 0x41..=0x5A | 0x61..=0x7A)
}
fn is_tag_name_char(c: u16) -> bool {
matches!(c, 0x41..=0x5A | 0x61..=0x7A | 0x30..=0x39 | 0x2D)
}
fn is_attr_name_start(c: u16) -> bool {
matches!(c, 0x41..=0x5A | 0x61..=0x7A | 0x5F | 0x3A)
}
fn is_attr_name_char(c: u16) -> bool {
matches!(c, 0x41..=0x5A | 0x61..=0x7A | 0x30..=0x39 | 0x3A | 0x2E | 0x5F | 0x2D)
}
fn is_unquoted_char(c: u16) -> bool {
c > 0x20 && !matches!(c, 0x22 | 0x27 | 0x3D | 0x3C | 0x3E | 0x60)
}
fn is_ws(c: u16) -> bool {
js::is_js_space(u32::from(c))
}
fn open_tag(s: &[u16]) -> Option<usize> {
const TAG_NAME: u16 = 1 << 0; const ATTR_START: u16 = 1 << 1; const ATTR_SPACE: u16 = 1 << 2; const ATTR_NAME: u16 = 1 << 3; const EQ_SPACE: u16 = 1 << 4; const VALUE_SPACE: u16 = 1 << 5; const UNQUOTED: u16 = 1 << 6; const SINGLE: u16 = 1 << 7; const DOUBLE: u16 = 1 << 8; const END_SPACE: u16 = 1 << 9; const SLASH: u16 = 1 << 10; const LOOP: u16 = ATTR_START | END_SPACE;
if s.first() != Some(&0x3C) || !s.get(1).is_some_and(|&c| is_alpha(c)) {
return None;
}
let mut states = TAG_NAME | LOOP;
let mut pos = 2;
while states != 0 {
let c = *s.get(pos)?;
pos += 1;
let mut next = 0;
if states & TAG_NAME != 0 && is_tag_name_char(c) {
next |= TAG_NAME | LOOP;
}
if states & (ATTR_START | ATTR_SPACE) != 0 && is_ws(c) {
next |= ATTR_SPACE;
}
if states & ATTR_SPACE != 0 && is_attr_name_start(c) {
next |= ATTR_NAME | EQ_SPACE | LOOP;
}
if states & ATTR_NAME != 0 && is_attr_name_char(c) {
next |= ATTR_NAME | EQ_SPACE | LOOP;
}
if states & EQ_SPACE != 0 {
if is_ws(c) {
next |= EQ_SPACE;
}
if c == 0x3D {
next |= VALUE_SPACE;
}
}
if states & VALUE_SPACE != 0 {
if is_ws(c) {
next |= VALUE_SPACE;
}
if is_unquoted_char(c) {
next |= UNQUOTED | LOOP;
}
if c == 0x27 {
next |= SINGLE;
}
if c == 0x22 {
next |= DOUBLE;
}
}
if states & UNQUOTED != 0 && is_unquoted_char(c) {
next |= UNQUOTED | LOOP;
}
if states & SINGLE != 0 {
next |= if c == 0x27 { LOOP } else { SINGLE };
}
if states & DOUBLE != 0 {
next |= if c == 0x22 { LOOP } else { DOUBLE };
}
if states & END_SPACE != 0 {
if is_ws(c) {
next |= END_SPACE;
}
if c == 0x2F {
next |= SLASH;
}
if c == 0x3E {
return Some(pos);
}
}
if states & SLASH != 0 && c == 0x3E {
return Some(pos);
}
states = next;
}
None
}
fn close_tag(s: &[u16]) -> Option<usize> {
if s.first() != Some(&0x3C)
|| s.get(1) != Some(&0x2F)
|| !s.get(2).is_some_and(|&c| is_alpha(c))
{
return None;
}
let mut pos = 3;
while s.get(pos).is_some_and(|&c| is_tag_name_char(c)) {
pos += 1;
}
while s.get(pos).is_some_and(|&c| is_ws(c)) {
pos += 1;
}
if s.get(pos) == Some(&0x3E) {
Some(pos + 1)
} else {
None
}
}
fn comment(s: &[u16]) -> Option<usize> {
const OPEN: [u16; 4] = [0x3C, 0x21, 0x2D, 0x2D]; if !s.starts_with(&OPEN) {
return None;
}
if s.get(4) == Some(&0x2D) && s.get(5) == Some(&0x3E) {
return Some(6);
}
if s.get(4) == Some(&0x3E) {
return Some(5);
}
let mut pos = 4;
loop {
match (s.get(pos), s.get(pos + 1), s.get(pos + 2)) {
(Some(&c), _, _) if c != 0x2D => pos += 1,
(Some(_), Some(&c1), _) if c1 != 0x2D => pos += 2,
(Some(_), Some(_), Some(&c2)) if c2 != 0x3E => pos += 3,
_ => break,
}
}
if js::starts_with(s, &[0x2D, 0x2D, 0x3E], pos) {
Some(pos + 3)
} else {
None
}
}
fn processing(s: &[u16]) -> Option<usize> {
if !s.starts_with(&[0x3C, 0x3F]) {
return None;
}
js::index_of(s, &[0x3F, 0x3E], 2).map(|i| i + 2)
}
fn declaration(s: &[u16]) -> Option<usize> {
if !s.starts_with(&[0x3C, 0x21]) || !s.get(2).is_some_and(|&c| is_alpha(c)) {
return None;
}
js::index_of(s, &[0x3E], 3).map(|i| i + 1)
}
fn cdata(s: &[u16]) -> Option<usize> {
const OPEN: [u16; 9] = [0x3C, 0x21, 0x5B, 0x43, 0x44, 0x41, 0x54, 0x41, 0x5B]; if !s.starts_with(&OPEN) {
return None;
}
js::index_of(s, &[0x5D, 0x5D, 0x3E], 9).map(|i| i + 3)
}
pub fn html_tag_re(s: &[u16]) -> Option<usize> {
open_tag(s)
.or_else(|| close_tag(s))
.or_else(|| comment(s))
.or_else(|| processing(s))
.or_else(|| declaration(s))
.or_else(|| cdata(s))
}
pub fn html_open_close_tag_re(s: &[u16]) -> Option<usize> {
open_tag(s).or_else(|| close_tag(s))
}
pub fn html_open_close_tag_re_to_end(s: &[u16]) -> bool {
html_open_close_tag_re(s).is_some_and(|end| s[end..].iter().all(|&c| is_ws(c)))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn html_regexes_match_node() {
let cases: &[(&str, Option<usize>, Option<usize>, bool)] = &[
("<a>", Some(3), Some(3), true),
("<a", None, None, false),
("<a/>", Some(4), Some(4), true),
("<a />", Some(5), Some(5), true),
("<a//>", None, None, false),
("<A-1 b>", Some(7), Some(7), true),
("<1a>", None, None, false),
("<a b>", Some(5), Some(5), true),
("<a b=c>", Some(7), Some(7), true),
("<a b = c >", Some(10), Some(10), true),
("<a b='>'>", Some(9), Some(9), true),
("<a b=\"x'y\">", Some(11), Some(11), true),
("<a b=c/>", Some(8), Some(8), true),
("<a b=c\u{a0}d>", Some(9), Some(9), true),
("<a b=c\u{a0}d=e>", Some(11), Some(11), true),
("<a b=c\u{a0}d='>'>", Some(13), Some(13), true),
("<a b=\u{a0}>", Some(7), Some(7), true),
("<a b=>", None, None, false),
("<a b=c=d>", None, None, false),
("<a\u{3000}b>", Some(5), Some(5), true),
("<a\nb\n=\n\"c\"\n>", Some(12), Some(12), true),
("<a b=c`>", None, None, false),
("<a :x._-y=1>", Some(12), Some(12), true),
("<a b><c>", Some(5), Some(5), false),
("<a b='c' d>x", Some(11), Some(11), false),
("</a>", Some(4), Some(4), true),
("</a >", Some(5), Some(5), true),
("</a b>", None, None, false),
("</>", None, None, false),
("</1>", None, None, false),
("<!---->", Some(7), None, false),
("<!-->", Some(5), None, false),
("<!--->", Some(6), None, false),
("<!-- -->", Some(8), None, false),
("<!-- --->", None, None, false),
("<!-- a ---->", None, None, false),
("<!-- a -- b -->", Some(15), None, false),
("<!---x-->", Some(9), None, false),
("<!-->-->", Some(5), None, false),
("<?>", None, None, false),
("<??>", Some(4), None, false),
("<?x?>y?>", Some(5), None, false),
("<!A>", Some(4), None, false),
("<!a b>", Some(6), None, false),
("<!>", None, None, false),
("<![CDATA[]]>", Some(12), None, false),
("<![CDATA[x]]]>", Some(14), None, false),
("<![CDATA[", None, None, false),
("a<b>", None, None, false),
("<a b='' c=\"\">", Some(13), Some(13), true),
("<a b='c'd>", None, None, false),
("<a\u{2028}b=c>", Some(7), Some(7), true),
];
for &(s, tag, open_close, to_end) in cases {
let s16 = js::to_utf16(s);
assert_eq!(
(
html_tag_re(&s16),
html_open_close_tag_re(&s16),
html_open_close_tag_re_to_end(&s16)
),
(tag, open_close, to_end),
"{s:?}"
);
}
}
}