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:
parent
8badd30b40
commit
08a2f39108
|
@ -7,4 +7,4 @@ edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
minify-html = { path = "..", features = ["js-esbuild"] }
|
minify-html = { path = "..", features = ["js-esbuild"] }
|
||||||
structopt = "0.3.5"
|
structopt = "0.3"
|
||||||
|
|
20
src/err.rs
20
src/err.rs
|
@ -1,7 +1,7 @@
|
||||||
// Implement debug to allow .unwrap().
|
// Implement debug to allow .unwrap().
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum ErrorType {
|
pub enum ErrorType {
|
||||||
ClosingTagMismatch,
|
ClosingTagMismatch { expected: String, got: String },
|
||||||
NotFound(&'static str),
|
NotFound(&'static str),
|
||||||
UnexpectedEnd,
|
UnexpectedEnd,
|
||||||
}
|
}
|
||||||
|
@ -9,8 +9,8 @@ pub enum ErrorType {
|
||||||
impl ErrorType {
|
impl ErrorType {
|
||||||
pub fn message(self) -> String {
|
pub fn message(self) -> String {
|
||||||
match self {
|
match self {
|
||||||
ErrorType::ClosingTagMismatch => {
|
ErrorType::ClosingTagMismatch { expected, got } => {
|
||||||
format!("Closing tag name does not match opening tag.")
|
format!("Closing tag name does not match opening tag (expected \"{}\", got \"{}\").", expected, got)
|
||||||
}
|
}
|
||||||
ErrorType::NotFound(exp) => {
|
ErrorType::NotFound(exp) => {
|
||||||
format!("Expected {}.", exp)
|
format!("Expected {}.", exp)
|
||||||
|
@ -38,15 +38,18 @@ pub struct FriendlyError {
|
||||||
pub type ProcessingResult<T> = Result<T, ErrorType>;
|
pub type ProcessingResult<T> = Result<T, ErrorType>;
|
||||||
|
|
||||||
#[inline(always)]
|
#[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;
|
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;
|
let pos_in_line = pos - lower;
|
||||||
while line.len() <= pos_in_line {
|
while line.len() <= pos_in_line {
|
||||||
line.push(b' ');
|
line.push(b' ');
|
||||||
};
|
};
|
||||||
line.insert(pos_in_line, if line[pos_in_line] != b' ' { b'B' } else { marker });
|
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.
|
// 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.
|
// Rust does lazy allocation by default, so this is not wasteful.
|
||||||
let mut indicator_line = Vec::new();
|
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);
|
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() {
|
if !indicator_line.is_empty() {
|
||||||
lines.push((-1, unsafe { String::from_utf8_unchecked(indicator_line) }));
|
lines.push((-1, unsafe { String::from_utf8_unchecked(indicator_line) }));
|
||||||
};
|
};
|
||||||
cur_pos = new_pos;
|
cur_pos = new_pos;
|
||||||
|
if marked_read {
|
||||||
|
break;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
let line_no_col_width = lines.len().to_string().len();
|
let line_no_col_width = lines.len().to_string().len();
|
||||||
|
|
|
@ -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")?;
|
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.
|
// 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]) {
|
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(WhileInLookup(WHITESPACE), Discard);
|
||||||
proc.m(IsChar(b'>'), Discard).require("closing tag end")?;
|
proc.m(IsChar(b'>'), Discard).require("closing tag end")?;
|
||||||
|
|
Loading…
Reference in New Issue