use super::state_inline::StateInline;
use crate::markdown_it::common::html_re::html_tag_re;
use crate::markdown_it::js;
fn is_link_open(s: &[u16]) -> bool {
s.len() >= 3
&& s[0] == 0x3C
&& (s[1] | 0x20) == 0x61
&& (s[2] == 0x3E || js::is_js_space(u32::from(s[2])))
}
fn is_link_close(s: &[u16]) -> bool {
if s.len() < 3 || s[0] != 0x3C || s[1] != 0x2F || (s[2] | 0x20) != 0x61 {
return false;
}
let mut i = 3;
while i < s.len() && js::is_js_space(u32::from(s[i])) {
i += 1;
}
s.get(i) == Some(&0x3E )
}
fn is_letter(ch: u16) -> bool {
let lc = ch | 0x20; (lc >= 0x61) && (lc <= 0x7a)
}
pub fn html_inline(state: &mut StateInline, silent: bool) -> bool {
if !state.md.options.html {
return false;
}
let max = state.pos_max;
let pos = state.pos;
if js::char_code_at(&state.src, pos) != Some(0x3C ) || pos + 2 >= max {
return false;
}
let ch = js::char_code_at(&state.src, pos + 1);
if ch != Some(0x21 )
&& ch != Some(0x3F )
&& ch != Some(0x2F )
&& !ch.is_some_and(is_letter)
{
return false;
}
let Some(match_len) = html_tag_re(js::slice_to(&state.src, pos, state.src.len())) else {
return false;
};
if !silent {
let matched = state.src[pos..pos + match_len].to_vec();
let token = state.push("html_inline", "", 0);
token.content = js::to_string(&matched);
if is_link_open(&matched) {
state.link_level += 1;
}
if is_link_close(&matched) {
state.link_level -= 1;
}
}
state.pos += match_len;
true
}
#[cfg(test)]
#[allow(clippy::type_complexity)]
mod tests {
use super::*;
use crate::markdown_it::{Env, MarkdownIt};
#[test]
fn link_open_and_close_tests_match_the_regexes() {
let cases: [(&str, (bool, bool)); 11] = [
("<a>", (true, false)),
("<A>", (true, false)),
("<a b>", (true, false)),
("<a\u{A0}b>", (true, false)),
("<a\u{85}b>", (false, false)),
("<ab>", (false, false)),
("</a>", (false, true)),
("</A >", (false, true)),
("</a\u{3000}>", (false, true)),
("</ab>", (false, false)),
("<a/>", (false, false)),
];
for (s, want) in cases {
let u = js::to_utf16(s);
assert_eq!((is_link_open(&u), is_link_close(&u)), want, "{s:?}");
}
}
#[test]
fn html_inline_counts_link_levels() {
let md = MarkdownIt::with_options(
"default",
serde_json::json!({ "html": true }).as_object().unwrap(),
)
.unwrap();
let cases: [(&str, (bool, usize, i32, &[&str])); 7] = [
(
"<a href=\"x\">",
(true, 12, 1, &["html_inline|<a href=\"x\">|0"]),
),
("</a>", (true, 4, -1, &["html_inline|</a>|0"])),
("<A\tb>", (true, 5, 1, &["html_inline|<A\tb>|0"])),
("</a >", (true, 5, -1, &["html_inline|</a >|0"])),
("<span>", (true, 6, 0, &["html_inline|<span>|0"])),
("<a", (false, 0, 0, &[])),
("x", (false, 0, 0, &[])),
];
for (src, (ok, pos, link_level, tokens)) in cases {
let mut env = Env::default();
let mut st = StateInline::new(js::to_utf16(src), &md, &mut env, Vec::new());
let got_ok = html_inline(&mut st, false);
let got_tokens: Vec<String> = st
.tokens
.iter()
.map(|t| format!("{}|{}|{}", t.kind, t.content, t.level))
.collect();
assert_eq!(
(got_ok, st.pos, st.link_level, got_tokens),
(
ok,
pos,
link_level,
tokens.iter().map(|t| t.to_string()).collect()
),
"{src:?}"
);
}
}
}