| 1 |
use std::borrow::Cow; |
| 2 |
use std::char; |
| 3 |
use std::cmp::min; |
| 4 |
use std::str; |
| 5 |
|
| 6 |
use crate::ctype::isdigit; |
| 7 |
|
| 8 |
pub const ENTITY_MIN_LENGTH: usize = 2; |
| 9 |
pub const ENTITY_MAX_LENGTH: usize = 32; |
| 10 |
|
| 11 |
pub fn unescape(text: &str) -> Option<(Cow<'static, str>, usize)> { |
| 12 |
let bytes = text.as_bytes(); |
| 13 |
if text.len() >= 3 && bytes[0] == b'#' { |
| 14 |
let mut codepoint: u32 = 0; |
| 15 |
let mut i = 0; |
| 16 |
|
| 17 |
let num_digits = if isdigit(bytes[1]) { |
| 18 |
i = 1; |
| 19 |
while i < text.len() && isdigit(bytes[i]) { |
| 20 |
codepoint = (codepoint * 10) + (bytes[i] as u32 - '0' as u32); |
| 21 |
codepoint = min(codepoint, 0x11_0000); |
| 22 |
i += 1; |
| 23 |
} |
| 24 |
i - 1 |
| 25 |
} else if bytes[1] == b'x' || bytes[1] == b'X' { |
| 26 |
i = 2; |
| 27 |
while i < bytes.len() && bytes[i].is_ascii_hexdigit() { |
| 28 |
codepoint = (codepoint * 16) + ((bytes[i] as u32 | 32) % 39 - 9); |
| 29 |
codepoint = min(codepoint, 0x11_0000); |
| 30 |
i += 1; |
| 31 |
} |
| 32 |
i - 2 |
| 33 |
} else { |
| 34 |
0 |
| 35 |
}; |
| 36 |
|
| 37 |
let is_hex = bytes[1] == b'x' || bytes[1] == b'X'; |
| 38 |
|
| 39 |
if i < bytes.len() |
| 40 |
&& bytes[i] == b';' |
| 41 |
&& ((is_hex && (1..=6).contains(&num_digits)) |
| 42 |
|| (!is_hex && (1..=7).contains(&num_digits))) |
| 43 |
{ |
| 44 |
if codepoint == 0 || (0xD800..=0xE000).contains(&codepoint) || codepoint >= 0x110000 { |
| 45 |
codepoint = 0xFFFD; |
| 46 |
} |
| 47 |
return Some(( |
| 48 |
char::from_u32(codepoint) |
| 49 |
.unwrap_or('\u{FFFD}') |
| 50 |
.to_string() |
| 51 |
.into(), |
| 52 |
i + 1, |
| 53 |
)); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
let size = min(text.len(), ENTITY_MAX_LENGTH); |
| 58 |
for i in ENTITY_MIN_LENGTH..size { |
| 59 |
if bytes[i] == b' ' { |
| 60 |
return None; |
| 61 |
} |
| 62 |
|
| 63 |
if bytes[i] == b';' { |
| 64 |
return lookup(&text[..i]).map(|e| (e.into(), i + 1)); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
None |
| 69 |
} |
| 70 |
|
| 71 |
include!(concat!(env!("OUT_DIR"), "/entitydata.rs")); |
| 72 |
|
| 73 |
fn lookup(text: &str) -> Option<&'static str> { |
| 74 |
entitydata::ENTITY_MAP.get(text).copied() |
| 75 |
} |
| 76 |
|
| 77 |
pub fn unescape_html(src: &str) -> Cow<'_, str> { |
| 78 |
let bytes = src.as_bytes(); |
| 79 |
let size = src.len(); |
| 80 |
let mut i = 0; |
| 81 |
let mut v = String::with_capacity(size); |
| 82 |
|
| 83 |
while i < size { |
| 84 |
let org = i; |
| 85 |
while i < size && bytes[i] != b'&' { |
| 86 |
i += 1; |
| 87 |
} |
| 88 |
|
| 89 |
if i > org { |
| 90 |
if org == 0 && i >= size { |
| 91 |
return src.into(); |
| 92 |
} |
| 93 |
|
| 94 |
v.push_str(&src[org..i]); |
| 95 |
} |
| 96 |
|
| 97 |
if i >= size { |
| 98 |
return v.into(); |
| 99 |
} |
| 100 |
|
| 101 |
i += 1; |
| 102 |
match unescape(&src[i..]) { |
| 103 |
Some((chs, size)) => { |
| 104 |
v.push_str(&chs); |
| 105 |
i += size; |
| 106 |
} |
| 107 |
None => v.push('&'), |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
v.into() |
| 112 |
} |