Fix debug repr line boundaries check; reduce debug repr output by stopping at read position; provide expected and actual tag names for closing tag mismatch error

This commit is contained in:
Wilson Lin 2020-07-30 00:28:57 +10:00
parent 8badd30b40
commit 08a2f39108
3 changed files with 18 additions and 9 deletions

View File

@ -7,4 +7,4 @@ edition = "2018"
[dependencies]
minify-html = { path = "..", features = ["js-esbuild"] }
structopt = "0.3.5"
structopt = "0.3"

View File

@ -1,7 +1,7 @@
// Implement debug to allow .unwrap().
#[derive(Debug)]
pub enum ErrorType {
ClosingTagMismatch,
ClosingTagMismatch { expected: String, got: String },
NotFound(&'static str),
UnexpectedEnd,
}
@ -9,8 +9,8 @@ pub enum ErrorType {
impl ErrorType {
pub fn message(self) -> String {
match self {
ErrorType::ClosingTagMismatch => {
format!("Closing tag name does not match opening tag.")
ErrorType::ClosingTagMismatch { expected, got } => {
format!("Closing tag name does not match opening tag (expected \"{}\", got \"{}\").", expected, got)
}
ErrorType::NotFound(exp) => {
format!("Expected {}.", exp)
@ -38,15 +38,18 @@ pub struct FriendlyError {
pub type ProcessingResult<T> = Result<T, ErrorType>;
#[inline(always)]
fn maybe_mark_indicator(line: &mut Vec<u8>, marker: u8, maybe_pos: isize, lower: usize, upper: usize) -> () {
fn maybe_mark_indicator(line: &mut Vec<u8>, marker: u8, maybe_pos: isize, lower: usize, upper: usize) -> bool {
let pos = maybe_pos as usize;
if maybe_pos > -1 && pos < upper {
if maybe_pos > -1 && pos >= lower && pos < upper {
let pos_in_line = pos - lower;
while line.len() <= pos_in_line {
line.push(b' ');
};
line.insert(pos_in_line, if line[pos_in_line] != b' ' { b'B' } else { marker });
};
true
} else {
false
}
}
// Pass -1 for read_pos or write_pos to prevent them from being represented.
@ -62,12 +65,15 @@ pub fn debug_repr(code: &[u8], read_pos: isize, write_pos: isize) -> String {
// Rust does lazy allocation by default, so this is not wasteful.
let mut indicator_line = Vec::new();
maybe_mark_indicator(&mut indicator_line, b'R', read_pos, cur_pos, new_pos);
maybe_mark_indicator(&mut indicator_line, b'W', write_pos, cur_pos, new_pos);
let marked_read = maybe_mark_indicator(&mut indicator_line, b'R', read_pos, cur_pos, new_pos);
if !indicator_line.is_empty() {
lines.push((-1, unsafe { String::from_utf8_unchecked(indicator_line) }));
};
cur_pos = new_pos;
if marked_read {
break;
};
};
let line_no_col_width = lines.len().to_string().len();

View File

@ -221,7 +221,10 @@ pub fn process_tag(proc: &mut Processor, cfg: &Cfg, ns: Namespace, mut prev_sibl
let closing_tag = proc.m(WhileInLookup(TAG_NAME_CHAR), Discard).require("closing tag name")?;
// We need to check closing tag matches as otherwise when we later write closing tag, it might be longer than source closing tag and cause source to be overwritten.
if !proc[closing_tag].eq(&proc[tag_name]) {
return Err(ErrorType::ClosingTagMismatch);
return Err(ErrorType::ClosingTagMismatch {
expected: unsafe { String::from_utf8_unchecked(proc[tag_name].to_vec()) },
got: unsafe { String::from_utf8_unchecked(proc[closing_tag].to_vec()) },
});
};
proc.m(WhileInLookup(WHITESPACE), Discard);
proc.m(IsChar(b'>'), Discard).require("closing tag end")?;