use super::{entities, ucmicro};
use crate::markdown_it::js;
pub fn array_replace_at<T: Clone>(src: &[T], pos: usize, new_elements: Vec<T>) -> Vec<T> {
let mut out: Vec<T> = src[..pos.min(src.len())].to_vec();
out.extend(new_elements);
if let Some(rest) = src.get(pos.saturating_add(1)..) {
out.extend_from_slice(rest);
}
out
}
pub fn is_valid_entity_code(c: u32) -> bool {
if (0xD800..=0xDFFF).contains(&c) {
return false;
}
if (0xFDD0..=0xFDEF).contains(&c) {
return false;
}
if (c & 0xFFFF) == 0xFFFF || (c & 0xFFFF) == 0xFFFE {
return false;
}
if c <= 0x08 {
return false;
}
if c == 0x0B {
return false;
}
if (0x0E..=0x1F).contains(&c) {
return false;
}
if (0x7F..=0x9F).contains(&c) {
return false;
}
if c > 0x10FFFF {
return false;
}
true
}
pub fn from_code_point(c: u32) -> Vec<u16> {
if c > 0xffff {
let c = c - 0x10000;
let surrogate1 = 0xd800 + (c >> 10);
let surrogate2 = 0xdc00 + (c & 0x3ff);
return js::from_char_code(&[surrogate1, surrogate2]);
}
js::from_char_code(&[c])
}
fn is_escapable(c: u16) -> bool {
matches!(c, 0x21..=0x2F | 0x3A..=0x40 | 0x5B..=0x60 | 0x7B..=0x7E)
}
fn match_escape(s: &[u16], i: usize) -> Option<u16> {
if s[i] == u16::from(b'\\')
&& let Some(&c) = s.get(i + 1)
&& is_escapable(c)
{
return Some(c);
}
None
}
fn match_entity(s: &[u16], i: usize) -> Option<usize> {
if s[i] != u16::from(b'&') {
return None;
}
let first = *s.get(i + 1)?;
if !(first == u16::from(b'#') || (first < 0x80 && (first as u8).is_ascii_alphabetic())) {
return None;
}
let run = s[i + 2..]
.iter()
.take_while(|&&c| c < 0x80 && (c as u8).is_ascii_alphanumeric())
.count();
if (1..=31).contains(&run) && s.get(i + 2 + run) == Some(&u16::from(b';')) {
return Some(run + 3);
}
None
}
fn is_digital_entity(name: &[u16]) -> bool {
let is_hex = |c: &u16| *c < 0x80 && (*c as u8).is_ascii_hexdigit();
let is_dec = |c: &u16| *c < 0x80 && (*c as u8).is_ascii_digit();
if name.first() != Some(&u16::from(b'#')) {
return false;
}
let rest = &name[1..];
if rest
.first()
.is_some_and(|&c| c == u16::from(b'x') || c == u16::from(b'X'))
&& (1..=8).contains(&(rest.len() - 1))
&& rest[1..].iter().all(is_hex)
{
return true;
}
(1..=8).contains(&rest.len()) && rest.iter().all(is_dec)
}
fn replace_entity_pattern(m: &[u16], name: &[u16]) -> Vec<u16> {
if name[0] == 0x23 && is_digital_entity(name) {
let code = if js::to_lower_case(&name[1..2]) == [u16::from(b'x')] {
js::parse_int(&name[2..], 16)
} else {
js::parse_int(&name[1..], 10)
};
let code = code as u32;
if is_valid_entity_code(code) {
return from_code_point(code);
}
return m.to_vec();
}
let decoded = decode_html_strict(m);
if decoded != m {
return decoded;
}
m.to_vec()
}
pub fn unescape_md(s: &[u16]) -> Vec<u16> {
if js::index_of(s, &[u16::from(b'\\')], 0).is_none() {
return s.to_vec();
}
let mut out = Vec::with_capacity(s.len());
let mut i = 0;
while i < s.len() {
if let Some(c) = match_escape(s, i) {
out.push(c);
i += 2;
} else {
out.push(s[i]);
i += 1;
}
}
out
}
pub fn unescape_all(s: &[u16]) -> Vec<u16> {
if js::index_of(s, &[u16::from(b'\\')], 0).is_none()
&& js::index_of(s, &[u16::from(b'&')], 0).is_none()
{
return s.to_vec();
}
let mut out = Vec::with_capacity(s.len());
let mut i = 0;
while i < s.len() {
if let Some(escaped) = match_escape(s, i) {
out.push(escaped);
i += 2;
} else if let Some(len) = match_entity(s, i) {
let m = &s[i..i + len];
out.extend(replace_entity_pattern(m, &m[1..len - 1]));
i += len;
} else {
out.push(s[i]);
i += 1;
}
}
out
}
pub fn escape_html(s: &str) -> String {
let mut out = String::with_capacity(s.len());
for c in s.chars() {
match c {
'&' => out.push_str("&"),
'<' => out.push_str("<"),
'>' => out.push_str(">"),
'"' => out.push_str("""),
_ => out.push(c),
}
}
out
}
pub fn escape_re(s: &str) -> String {
let mut out = String::with_capacity(s.len());
for c in s.chars() {
if ".?*+^$[]\\(){}|-".contains(c) {
out.push('\\');
}
out.push(c);
}
out
}
pub fn is_space(code: impl Into<u32>) -> bool {
matches!(code.into(), 0x09 | 0x20)
}
pub fn is_white_space(code: impl Into<u32>) -> bool {
let code = code.into();
if (0x2000..=0x200A).contains(&code) {
return true;
}
matches!(
code,
0x09 | 0x0A | 0x0B | 0x0C | 0x0D | 0x20 | 0xA0 | 0x1680 | 0x202F | 0x205F | 0x3000
)
}
pub fn is_punct_char(ch: &[u16]) -> bool {
(0..ch.len()).any(|i| {
let c = u32::from(ch[i]);
if (0xD800..=0xDBFF).contains(&c)
&& let Some(&low) = ch.get(i + 1)
&& (0xDC00..=0xDFFF).contains(&low)
{
let cp = 0x10000 + ((c - 0xD800) << 10) + (u32::from(low) - 0xDC00);
return ucmicro::is_p(cp) || ucmicro::is_s(cp);
}
ucmicro::is_p(c) || ucmicro::is_s(c)
})
}
pub fn is_punct_char_code(code: u32) -> bool {
is_punct_char(&from_code_point(code))
}
pub fn is_md_ascii_punct(ch: impl Into<u32>) -> bool {
matches!(ch.into(), 0x21..=0x2F | 0x3A..=0x40 | 0x5B..=0x60 | 0x7B..=0x7E)
}
pub fn normalize_reference(s: &[u16]) -> String {
let mut collapsed: Vec<u16> = Vec::with_capacity(s.len());
let mut in_space = false;
for &c in js::trim(s) {
if js::is_js_space(u32::from(c)) {
if !in_space {
collapsed.push(0x20);
}
in_space = true;
} else {
collapsed.push(c);
in_space = false;
}
}
js::to_string(&js::to_upper_case(&js::to_lower_case(&collapsed)))
}
fn is_ascii_trimmable(c: u16) -> bool {
c == 0x20 || c == 0x09 || c == 0x0a || c == 0x0d
}
pub fn ascii_trim(s: &[u16]) -> &[u16] {
let mut start = 0;
while start < s.len() {
if !is_ascii_trimmable(s[start]) {
break;
}
start += 1;
}
let mut end = s.len();
while end > start {
if !is_ascii_trimmable(s[end - 1]) {
break;
}
end -= 1;
}
&s[start..end]
}
fn replace_code_point(code_point: u64) -> u32 {
if (0xD800..=0xDFFF).contains(&code_point) || code_point > 0x10FFFF {
return 0xFFFD;
}
match code_point {
0 => 0xFFFD,
128 => 8364,
130 => 8218,
131 => 402,
132 => 8222,
133 => 8230,
134 => 8224,
135 => 8225,
136 => 710,
137 => 8240,
138 => 352,
139 => 8249,
140 => 338,
142 => 381,
145 => 8216,
146 => 8217,
147 => 8220,
148 => 8221,
149 => 8226,
150 => 8211,
151 => 8212,
152 => 732,
153 => 8482,
154 => 353,
155 => 8250,
156 => 339,
158 => 382,
159 => 376,
other => other as u32,
}
}
fn decode_entity_strict(input: &[u16], amp: usize, out: &mut Vec<u16>) -> usize {
let mut offset = amp + 1;
if input.get(offset) == Some(&u16::from(b'#')) {
let mut consumed: usize = 2;
offset += 1;
let Some(&c) = input.get(offset) else {
return 0; };
let (radix, expected) = if (c | 0x20) == u16::from(b'x') {
consumed += 1;
offset += 1;
(16u64, 3usize)
} else {
(10u64, 2usize)
};
let mut result: u64 = 0;
while let Some(&c) = input.get(offset) {
let digit = match c {
0x30..=0x39 => u64::from(c - 0x30),
0x41..=0x46 | 0x61..=0x66 if radix == 16 => u64::from((c | 0x20) - 0x61 + 10),
_ => break,
};
result = result.saturating_mul(radix).saturating_add(digit);
consumed += 1;
offset += 1;
}
let Some(&last) = input.get(offset) else {
return 0; };
if consumed <= expected {
return 0; }
if last != u16::from(b';') {
return 0; }
consumed += 1;
out.extend(from_code_point(replace_code_point(result)));
return consumed;
}
let name_len = input[offset..]
.iter()
.take_while(|&&c| c < 0x80 && (c as u8).is_ascii_alphanumeric())
.count();
if name_len == 0 || input.get(offset + name_len) != Some(&u16::from(b';')) {
return 0;
}
let name = js::to_string(&input[offset..offset + name_len]);
match entities::lookup(&name) {
Some(value) => {
out.extend(value.encode_utf16());
name_len + 2
}
None => 0,
}
}
pub fn decode_html_strict(input: &[u16]) -> Vec<u16> {
let mut ret = Vec::with_capacity(input.len());
let mut last_index = 0;
let mut offset = 0;
while let Some(amp) = js::index_of(input, &[u16::from(b'&')], offset) {
ret.extend_from_slice(&input[last_index..amp]);
let length = decode_entity_strict(input, amp, &mut ret);
last_index = amp + length;
offset = if length == 0 {
last_index + 1
} else {
last_index
};
}
ret.extend_from_slice(&input[last_index..]);
ret
}
#[cfg(test)]
mod tests {
use super::*;
fn u(s: &str) -> Vec<u16> {
js::to_utf16(s)
}
fn s(v: &[u16]) -> String {
js::to_string(v)
}
#[test]
fn array_replace_at_like_js() {
assert_eq!(array_replace_at(&[1, 2, 3], 1, vec![7, 8]), [1, 7, 8, 3]);
assert_eq!(array_replace_at(&[1, 2, 3], 5, vec![7, 8]), [1, 2, 3, 7, 8]);
assert_eq!(array_replace_at(&[1, 2, 3], 2, vec![7, 8]), [1, 2, 7, 8]);
}
#[test]
fn entity_codes_and_from_code_point() {
for (c, ok) in [
(0x41, true),
(0xD800, false),
(0xFDD0, false),
(0x1FFFF, false),
(0x10FFFE, false),
(0x08, false),
(0x09, true),
(0x0B, false),
(0x0C, true),
(0x1F, false),
(0x7F, false),
(0x9F, false),
(0xA0, true),
(0x10FFFD, true),
(0x110000, false),
] {
assert_eq!(is_valid_entity_code(c), ok, "{c:#x}");
}
assert_eq!(from_code_point(0x1F600), u("\u{1F600}"));
assert_eq!(from_code_point(0x41), u("A"));
assert_eq!(from_code_point(0x110000), vec![0xDC00, 0xDC00]);
}
#[test]
fn unescape_md_and_all_match_node() {
assert_eq!(s(&unescape_md(&u(r"\*a\b\\"))), r"*a\b\");
assert_eq!(s(&unescape_md(&u("plain"))), "plain");
let cases = [
(r"a\*b", "a*b"),
("&<", "&<"),
("≧̸", "\u{2267}\u{338}"),
("AB", "AB"),
("A", "A"),
("�", "�"),
("�", "�"),
("�", "\u{FFFD}"),
("�", "�"),
("a;", "a;"),
("&", "&"),
("&x;", "&x;"),
("&", "&"),
("&Amp;", "&Amp;"),
("&#x;", "&#x;"),
("&#xg;", "&#xg;"),
("&#abc;", "&#abc;"),
(r"\&", "&"),
("€", "€"),
("	", "\t"),
("😀", "\u{1F600}"),
(
"&aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
"&aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
),
];
for (input, want) in cases {
assert_eq!(s(&unescape_all(&u(input))), want, "{input}");
}
}
#[test]
fn decode_html_strict_matches_entities() {
let cases = [
("&", "&"),
("&", "&"),
("a<b>c", "a<b>c"),
("&&", "&&"),
("€", "\u{20AC}"),
("�", "\u{FFFD}"),
("�", "\u{FFFD}"),
("�", "\u{FFFD}"),
("A", "A"),
("&#x;", "&#x;"),
("⪢̸", "\u{2AA2}\u{338}"),
("&nosuch;", "&nosuch;"),
];
for (input, want) in cases {
assert_eq!(s(&decode_html_strict(&u(input))), want, "{input}");
}
}
#[test]
fn character_classes() {
assert!(is_space(0x20u16) && is_space(0x09u32) && !is_space(0x0Au16) && !is_space(0xA0u32));
for c in [
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x20, 0xA0, 0x1680, 0x2000, 0x200A, 0x202F, 0x205F,
0x3000,
] {
assert!(is_white_space(c as u32), "{c:#x}");
}
for c in [0x2028u32, 0x2029, 0xFEFF, 0x85, 0x180E, 0x200B] {
assert!(!is_white_space(c), "{c:#x}");
}
let md: Vec<u32> = (0..0x80).filter(|&c| is_md_ascii_punct(c)).collect();
let want: Vec<u32> = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
.chars()
.map(u32::from)
.collect();
assert_eq!(md, want);
for (c, want) in [
(0x21, true),
(0x24, true), (0x41, false),
(0xA7, true), (0x2019, true),
(0x20AC, true), (0x1F600, true),
(0x10100, true),
(0xD800, false),
(0x3000, false),
(0x110000, false),
] {
assert_eq!(is_punct_char_code(c), want, "{c:#x}");
}
assert!(is_punct_char(&u("a!")));
assert!(!is_punct_char(&[0xD83D]));
assert!(!is_punct_char(&u("abc")));
}
#[test]
fn normalize_reference_matches_node() {
let cases = [
(" Foo \t\n bar ", "FOO BAR"),
("ẞ", "SS"),
(
"\u{398}\u{3F4}\u{3B8}\u{3D1}",
"\u{398}\u{398}\u{398}\u{398}",
),
("a\u{A0}\u{3000}b", "A B"),
("\u{130}", "I\u{307}"),
("\u{2126}", "\u{3A9}"),
];
for (input, want) in cases {
assert_eq!(normalize_reference(&u(input)), want, "{input:?}");
}
}
#[test]
fn ascii_trim_keeps_unicode_spaces() {
assert_eq!(
ascii_trim(&u(" \t\n\r a b \u{A0}\r\n")),
&u("a b \u{A0}")[..]
);
assert_eq!(ascii_trim(&u(" ")), &u("")[..]);
}
#[test]
fn escapes() {
assert_eq!(
escape_html("<a href=\"x\">&'</a>"),
"<a href="x">&'</a>"
);
assert_eq!(
escape_re("a.b*c(d)|[e]-{f}^$?+\\"),
"a\\.b\\*c\\(d\\)\\|\\[e\\]\\-\\{f\\}\\^\\$\\?\\+\\\\"
);
}
}