From 08a2f39108d879c289d45ffd983451e7fc689407 Mon Sep 17 00:00:00 2001 From: Wilson Lin Date: Thu, 30 Jul 2020 00:28:57 +1000 Subject: [PATCH] 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 --- cli/Cargo.toml | 2 +- src/err.rs | 20 +++++++++++++------- src/unit/tag.rs | 5 ++++- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 963d58d..56d2c52 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -7,4 +7,4 @@ edition = "2018" [dependencies] minify-html = { path = "..", features = ["js-esbuild"] } -structopt = "0.3.5" +structopt = "0.3" diff --git a/src/err.rs b/src/err.rs index ef07e8f..0976a81 100644 --- a/src/err.rs +++ b/src/err.rs @@ -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 = Result; #[inline(always)] -fn maybe_mark_indicator(line: &mut Vec, marker: u8, maybe_pos: isize, lower: usize, upper: usize) -> () { +fn maybe_mark_indicator(line: &mut Vec, 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(); diff --git a/src/unit/tag.rs b/src/unit/tag.rs index 730b63c..c3e83eb 100644 --- a/src/unit/tag.rs +++ b/src/unit/tag.rs @@ -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")?;