use super::state_inline::StateInline;
use crate::markdown_it::common::utils::is_space;
use crate::markdown_it::js;
const ESCAPED: [u8; 256] = {
let mut escaped = [0u8; 256];
let chars = b"\\!\"#$%&'()*+,./:;<=>?@[]^_`{|}~-";
let mut i = 0;
while i < chars.len() {
escaped[chars[i] as usize] = 1;
i += 1;
}
escaped
};
pub fn escape(state: &mut StateInline, silent: bool) -> bool {
let mut pos = state.pos;
let max = state.pos_max;
if js::char_code_at(&state.src, pos) != Some(0x5C ) {
return false;
}
pos += 1;
if pos >= max {
return false;
}
let mut ch1 = js::char_code_at(&state.src, pos);
if ch1 == Some(0x0A) {
if !silent {
state.push("hardbreak", "br", 0);
}
pos += 1;
while pos < max {
ch1 = js::char_code_at(&state.src, pos);
if !ch1.is_some_and(is_space) {
break;
}
pos += 1;
}
state.pos = pos;
return true;
}
if ch1 == Some(0x20) {
if !silent {
let token = state.push("text_special", "", 0);
token.content = "\\".into();
token.markup = "\\".into();
token.info = "escape".into();
}
state.pos = pos;
return true;
}
let mut escaped_str: Vec<u16> = match state.src.get(pos) {
Some(&c) => vec![c],
None => js::to_utf16("undefined"),
};
if let Some(c1) = ch1
&& c1 >= 0xD800
&& c1 <= 0xDBFF
&& pos + 1 < max
{
let ch2 = js::char_code_at(&state.src, pos + 1);
if let Some(c2) = ch2
&& c2 >= 0xDC00
&& c2 <= 0xDFFF
{
escaped_str.push(c2);
pos += 1;
}
}
let mut orig_str: Vec<u16> = vec![0x5C ];
orig_str.extend_from_slice(&escaped_str);
if !silent {
let token = state.push("text_special", "", 0);
if let Some(c1) = ch1
&& c1 < 256
&& ESCAPED[c1 as usize] != 0
{
token.content = js::to_string(&escaped_str);
} else {
token.content = js::to_string(&orig_str);
}
token.markup = js::to_string(&orig_str);
token.info = "escape".into();
}
state.pos = pos + 1;
true
}
#[cfg(test)]
mod tests {
use super::*;
use crate::markdown_it::{Env, MarkdownIt};
#[test]
fn escaped_table_matches_escape_ts() {
let want = "0000000000000000000000000000000001111111111111110000000000111111\
1000000000000000000000000001111110000000000000000000000000011110\
0000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000";
let got: String = ESCAPED
.iter()
.map(|&e| if e != 0 { '1' } else { '0' })
.collect();
assert_eq!(got, want);
}
fn only(rules: &[&str], src: &str) -> Vec<String> {
let mut md = MarkdownIt::default();
md.inline.ruler.enable_only(rules, false).unwrap();
md.core.ruler.disable(&["text_join"], false).unwrap();
let tokens = md.parse_inline(src, &mut Env::default());
let children = tokens[0].children.as_ref().unwrap();
children
.iter()
.map(|t| format!("{}|{}|{}|{}", t.kind, t.content, t.markup, t.info))
.collect()
}
#[test]
fn escapes_hardbreaks_and_literal_backslashes() {
let cases: [(&str, &[&str]); 8] = [
("\\", &["text|\\||"]),
("a\\", &["text|a\\||"]),
("\\\n b", &["hardbreak|||", "text|b||"]),
(
"\\ \nb",
&["text_special|\\|\\|escape", "softbreak|||", "text|b||"],
),
(
"a\\ \nb",
&[
"text|a||",
"text_special|\\|\\|escape",
"hardbreak|||",
"text|b||",
],
),
(
"\\\u{1F600}x",
&["text_special|\\\u{1F600}|\\\u{1F600}|escape", "text|x||"],
),
("\\\u{E9}", &["text_special|\\\u{E9}|\\\u{E9}|escape"]),
("\\\\", &["text_special|\\|\\\\|escape"]),
];
for (src, want) in cases {
assert_eq!(only(&["text", "newline", "escape"], src), want, "{src:?}");
}
}
#[test]
fn lone_surrogates_are_escaped_one_unit_at_a_time() {
let md = MarkdownIt::default();
for (src, pos) in [(vec![0x5C, 0xD83D, 0x78], 2), (vec![0x5C, 0xDE00], 2)] {
let mut env = Env::default();
let mut st = StateInline::new(src, &md, &mut env, Vec::new());
assert!(escape(&mut st, false));
assert_eq!(st.pos, pos);
let t = &st.tokens[0];
assert_eq!(
(t.kind.as_str(), t.content.as_str(), t.markup.as_str()),
("text_special", "\\\u{FFFD}", "\\\u{FFFD}")
);
}
}
}