The design language that makes your AI harness better at design.
JavaScript
Broken images: an <img> inside a quoted attribute across lines isn't markup
Paul Bakausc19dda2719a6
diff --git a/crates/detect/src/detect_text.rs b/crates/detect/src/detect_text.rsindex 52f850f57..b6c5b82b0 100644--- a/crates/detect/src/detect_text.rs+++ b/crates/detect/src/detect_text.rs@@ -1906,29 +1906,60 @@ pub fn run_regex_matchers(let run = || {let mut matches = Vec::new();let image = matcher.id == "broken-image";- if image && !lines.iter().any(|l| l.as_bytes().windows(4).any(|w| w.eq_ignore_ascii_case(b"<img"))) {+ if image+ && (phase != "source"+ || !lines.iter().any(|l| {+ l.as_bytes().windows(4).any(|w| w.eq_ignore_ascii_case(b"<img"))+ }))+ {return matches;}+ let mut image_cursor = 0;for (i, line) in lines.iter().enumerate() {let (line, ctxs): (&str, Vec<MatchCtx>) = if image {- let (text, starts) = image_source.get_or_init(|| {+ let (text, starts, markup) = image_source.get_or_init(|| {let joined = lines.join("\n");- let mut starts = Vec::with_capacity(lines.len());- let mut at = 0;- for l in lines {- starts.push(at);- at += l.len() + 1;- }// Masking keeps every byte offset and newline.let text = if JS_SOURCE_EXTS.contains(&ext.as_str()) {mask_js_literals(&joined, matches!(ext.as_str(), ".js" | ".jsx" | ".tsx"))} else {joined};- (text, starts)+ let mut starts = Vec::with_capacity(lines.len());+ let mut at = 0;+ for (i, l) in text.split('\n').enumerate() {+ starts.push(at);+ at += l.len() + usize::from(i + 1 < lines.len());+ }+ let jsx = matches!(ext.as_str(), ".js" | ".jsx" | ".tsx");+ let template = matches!(ext.as_str(), ".svelte" | ".vue" | ".astro");+ let css = CSS_LIKE_EXTS.contains(&ext.as_str());+ let markup = crate::regex_matchers::scan_markup_context(+ &text,+ crate::regex_matchers::MarkupScanOptions {+ jsx,+ expressions: jsx || template,+ raw_text: !JS_SOURCE_EXTS.contains(&ext.as_str()) && !css,+ markup: !css,+ },+ );+ (text, starts, markup)});- let rest = &text[starts[i]..];- (&rest[..line.len()], crate::regex_matchers::find_broken_images(rest))+ let line_start = starts[i];+ let line_end = text[line_start..]+ .find('\n')+ .map(|end| line_start + end)+ .unwrap_or(text.len());+ (+ &text[line_start..line_end],+ crate::regex_matchers::find_broken_images_in_line(+ text,+ markup,+ &mut image_cursor,+ line_start,+ line_end,+ ),+ )} else {(line, (matcher.find_all)(line))};diff --git a/crates/detect/src/regex_matchers.rs b/crates/detect/src/regex_matchers.rsindex bf7c66b43..65b34a6d0 100644--- a/crates/detect/src/regex_matchers.rs+++ b/crates/detect/src/regex_matchers.rs@@ -1751,8 +1751,6 @@ re!(CUBIC_BEZIER_RE,format!("cubic-bezier\\({WS}*([0-9.-]+){WS}*,{WS}*([0-9.-]+){WS}*,{WS}*([0-9.-]+){WS}*,{WS}*([0-9.-]+){WS}*\\)"));-re!(IMG_OPEN_RE, format!("<{}{B}", ci("img")));-/// Read attributes, not substrings of an attribute value or expression./// Dynamic bindings/spreads cannot establish a missing source statically.pub(crate) fn image_source_is_missing(tag: &str, ext: &str) -> bool {@@ -1832,117 +1830,510 @@ fn names_image_source(name: &str) -> bool {/// How far a tag may run past its `<img`, so an unclosed one costs little.const MAX_IMG_TAG_BYTES: usize = 8 * 1024;-enum TagScan {- /// The tag's closing `>`.- End(usize),- /// The probe sits in a quoted attribute value or an expression's string.- Hidden,- /// The probe sits in an expression as code (nested JSX).- Visible,- NotATag,-}--/// Walk the markup tag opened at `start`. Attribute quotes close at the next-/// matching quote with no escapes (HTML, Svelte, Vue and JSX strings alike),-/// `{...}` expressions follow JS rules, and a top-level `<` means the text-/// was never a tag.-fn scan_markup_tag(b: &[u8], start: usize, probe: Option<usize>) -> TagScan {- let inside = |open: usize, close: usize| probe.filter(|x| *x > open && *x < close);+#[derive(Clone, Copy, Debug, PartialEq, Eq)]+enum MarkupContext {+ Text,+ Tag,+ AttributeValue,+ Expression,+ Comment,+ RawText,+}++#[derive(Clone, Copy)]+pub(crate) struct MarkupScanOptions {+ pub jsx: bool,+ pub expressions: bool,+ pub raw_text: bool,+ pub markup: bool,+}++/// One scan of a source file, retaining the lexical context at every byte and+/// the bounded closing offset of each real `<img` start tag.+pub(crate) struct MarkupContextIndex {+ context: Vec<MarkupContext>,+ images: Vec<(usize, usize)>,+}++enum MarkupFrame {+ Text,+ Tag {+ start: usize,+ name_start: usize,+ name_end: usize,+ closing: bool,+ raw_eligible: bool,+ image_slot: Option<usize>,+ quote: Option<u8>,+ escaped: bool,+ last_non_space: Option<u8>,+ },+ Expression {+ depth: usize,+ quote: Option<u8>,+ escaped: bool,+ line_comment: bool,+ block_comment: bool,+ regex_can_start: bool,+ word: Vec<u8>,+ },+ RawText(RawTag),+}++#[derive(Clone, Copy)]+enum RawTag {+ Script,+ Style,+}++fn tag_open(b: &[u8], at: usize) -> Option<(usize, usize, bool)> {+ if b.get(at) != Some(&b'<') {+ return None;+ }+ let mut p = at + 1;+ let closing = b.get(p) == Some(&b'/');+ if closing {+ p += 1;+ }+ let name_start = p;+ if !b.get(p).is_some_and(u8::is_ascii_alphabetic) {+ return None;+ }+ p += 1;+ while b+ .get(p)+ .is_some_and(|c| c.is_ascii_alphanumeric() || matches!(c, b'-' | b':' | b'_' | b'.'))+ {+ p += 1;+ }+ Some((name_start, p, closing))+}++fn starts_with_ascii_case_insensitive(b: &[u8], at: usize, needle: &[u8]) -> bool {+ b.get(at..at.saturating_add(needle.len()))+ .is_some_and(|part| part.eq_ignore_ascii_case(needle))+}++fn image_tag_start(b: &[u8], at: usize) -> bool {+ starts_with_ascii_case_insensitive(b, at, b"<img")+ && b.get(at + 4)+ .is_none_or(|c| !c.is_ascii_alphanumeric() && *c != b'_')+}++fn skip_js_regex(b: &[u8], start: usize) -> Option<usize> {let mut p = start + 1;- while p < b.len() {+ let mut escaped = false;+ let mut in_class = false;+ while p < b.len() && !matches!(b[p], b'\n' | b'\r') {match b[p] {- b'>' => return TagScan::End(p),- b'<' => return TagScan::NotATag,- quote @ (b'"' | b'\'') => {- let Some(close) = b[p + 1..].iter().position(|c| *c == quote) else {- return TagScan::NotATag;- };- let close = p + 1 + close;- if inside(p, close).is_some() {- return TagScan::Hidden;- }- p = close + 1;- }- b'{' => {- let Some(close) = matching_close(b, p) else { return TagScan::NotATag; };- if let Some(x) = inside(p, close) {- return if in_js_string(&b[p..x]) { TagScan::Hidden } else { TagScan::Visible };+ b'\\' if !escaped => escaped = true,+ b'[' if !escaped => in_class = true,+ b']' if !escaped => in_class = false,+ b'/' if !escaped && !in_class => {+ p += 1;+ while b.get(p).is_some_and(u8::is_ascii_alphabetic) {+ p += 1;}- p = close + 1;+ return Some(p);}- _ => p += 1,+ _ => escaped = false,}+ p += 1;}- TagScan::NotATag+ None}-fn in_js_string(b: &[u8]) -> bool {- let mut quote = None;- let mut p = 0;- while p < b.len() {- match (quote, b[p]) {- (Some(_), b'\\') => p += 1,- (Some(q), c) if c == q => quote = None,- (None, c @ (b'"' | b'\'' | b'`')) => quote = Some(c),- _ => {}- }- p += 1;+fn regex_prefix_keyword(word: &[u8]) -> bool {+ matches!(+ word,+ b"return"+ | b"throw"+ | b"case"+ | b"delete"+ | b"void"+ | b"typeof"+ | b"new"+ | b"in"+ | b"of"+ | b"instanceof"+ | b"yield"+ | b"await"+ | b"else"+ )+}++fn mark_range(context: &mut [MarkupContext], start: usize, end: usize, kind: MarkupContext) {+ let len = context.len();+ context[start.min(len)..end.min(len)].fill(kind);+}++/// Tokenize markup and template expressions in one forward pass. JS/TS input+/// arrives with its code literals masked by `mask_js_literals`; template+/// expressions are scanned here so their strings and comments stay opaque.+pub(crate) fn scan_markup_context(text: &str, options: MarkupScanOptions) -> MarkupContextIndex {+ let b = text.as_bytes();+ let mut context = vec![MarkupContext::Text; b.len()];+ if !options.markup {+ return MarkupContextIndex {+ context,+ images: Vec::new(),+ };}- quote.is_some()-}--/// A candidate inside the quoted attribute of an earlier tag on its line is-/// text, even when the quote closes on a later line; one in a JSX attribute-/// expression is a real nested element.-fn hidden_in_parent_tag(text: &[u8], at: usize) -> bool {- let window = &text[..text.len().min(at + MAX_IMG_TAG_BYTES)];- let mut i = 0;- while i < at {- if text[i] == b'<' && text.get(i + 1).is_some_and(|c| c.is_ascii_alphabetic()) {- match scan_markup_tag(window, i, Some(at)) {- TagScan::Hidden => return true,- TagScan::Visible => return false,- TagScan::End(end) => {- i = end + 1;++ let mut images = Vec::new();+ let mut stack = vec![MarkupFrame::Text];+ let mut p = 0usize;+ while p < b.len() {+ match stack.last_mut().expect("markup scan has a root frame") {+ MarkupFrame::Text => {+ if b.get(p..p + 4) == Some(b"<!--") {+ let end = b[p + 4..]+ .windows(3)+ .position(|w| w == b"-->")+ .map(|i| p + 4 + i + 3)+ .unwrap_or(b.len());+ mark_range(&mut context, p, end, MarkupContext::Comment);+ p = end;continue;}- TagScan::NotATag => {}+ if b[p] == b'<' {+ if let Some((name_start, name_end, closing)) = tag_open(b, p) {+ mark_range(&mut context, p, name_end, MarkupContext::Tag);+ let image_slot = (!closing && image_tag_start(b, p)).then(|| {+ images.push((p, 0));+ images.len() - 1+ });+ stack.push(MarkupFrame::Tag {+ start: p,+ name_start,+ name_end,+ closing,+ raw_eligible: options.raw_text,+ image_slot,+ quote: None,+ escaped: false,+ last_non_space: b.get(name_end.wrapping_sub(1)).copied(),+ });+ p = name_end;+ continue;+ }+ }+ if options.expressions && b[p] == b'{' {+ context[p] = MarkupContext::Expression;+ stack.push(MarkupFrame::Expression {+ depth: 1,+ quote: None,+ escaped: false,+ line_comment: false,+ block_comment: false,+ regex_can_start: true,+ word: Vec::new(),+ });+ }+ p += 1;+ }+ MarkupFrame::Tag {+ start,+ name_start,+ name_end,+ closing,+ raw_eligible,+ image_slot,+ quote,+ escaped,+ last_non_space,+ } => {+ if let Some(q) = *quote {+ context[p] = MarkupContext::AttributeValue;+ if options.jsx && b[p] == b'\\' && !*escaped {+ *escaped = true;+ } else {+ if b[p] == q && !*escaped {+ *quote = None;+ }+ *escaped = false;+ }+ p += 1;+ continue;+ }+ match b[p] {+ b'"' | b'\'' => {+ context[p] = MarkupContext::AttributeValue;+ *quote = Some(b[p]);+ *escaped = false;+ p += 1;+ }+ b'{' if options.expressions => {+ context[p] = MarkupContext::Expression;+ stack.push(MarkupFrame::Expression {+ depth: 1,+ quote: None,+ escaped: false,+ line_comment: false,+ block_comment: false,+ regex_can_start: true,+ word: Vec::new(),+ });+ p += 1;+ }+ b'>' => {+ context[p] = MarkupContext::Tag;+ let tag_start = *start;+ let tag_end = p + 1;+ let is_closing = *closing;+ let is_raw_script =+ b[*name_start..*name_end].eq_ignore_ascii_case(b"script");+ let is_raw_style = b[*name_start..*name_end].eq_ignore_ascii_case(b"style");+ let self_closing = *last_non_space == Some(b'/');+ if let Some(slot) = image_slot {+ if tag_end - tag_start <= MAX_IMG_TAG_BYTES+ && context[tag_start] == MarkupContext::Tag+ {+ images[*slot].1 = tag_end;+ }+ }+ let enter_raw = *raw_eligible && !is_closing && !self_closing;+ stack.pop();+ if enter_raw && (is_raw_script || is_raw_style) {+ stack.push(MarkupFrame::RawText(if is_raw_script {+ RawTag::Script+ } else {+ RawTag::Style+ }));+ }+ p += 1;+ }+ b'<' => {+ // A second unquoted `<` means this opener was malformed.+ // Reprocess it in the parent mode so a real following tag+ // is still found without rescanning the preceding bytes.+ stack.pop();+ }+ c => {+ context[p] = MarkupContext::Tag;+ if !c.is_ascii_whitespace() {+ *last_non_space = Some(c);+ }+ p += 1;+ }+ }+ }+ MarkupFrame::Expression {+ depth,+ quote,+ escaped,+ line_comment,+ block_comment,+ regex_can_start,+ word,+ } => {+ context[p] = MarkupContext::Expression;+ let c = b[p];+ if *line_comment {+ if c == b'\n' {+ *line_comment = false;+ }+ context[p] = MarkupContext::Comment;+ p += 1;+ continue;+ }+ if *block_comment {+ context[p] = MarkupContext::Comment;+ if c == b'*' && b.get(p + 1) == Some(&b'/') {+ context[p + 1] = MarkupContext::Comment;+ *block_comment = false;+ p += 2;+ } else {+ p += 1;+ }+ continue;+ }+ if let Some(q) = *quote {+ if c == b'\\' && !*escaped {+ *escaped = true;+ } else {+ if c == q && !*escaped {+ *quote = None;+ }+ *escaped = false;+ }+ p += 1;+ continue;+ }+ if c == b'/' && !word.is_empty() && !matches!(b.get(p + 1), Some(b'/') | Some(b'*'))+ {+ *regex_can_start = regex_prefix_keyword(word);+ word.clear();+ }+ if c == b'/' && b.get(p + 1) == Some(&b'/') {+ *line_comment = true;+ context[p] = MarkupContext::Comment;+ context[p + 1] = MarkupContext::Comment;+ p += 2;+ continue;+ }+ if c == b'/' && b.get(p + 1) == Some(&b'*') {+ *block_comment = true;+ context[p] = MarkupContext::Comment;+ context[p + 1] = MarkupContext::Comment;+ p += 2;+ continue;+ }+ if c == b'/' && word.is_empty() && *regex_can_start {+ if let Some(end) = skip_js_regex(b, p) {+ mark_range(&mut context, p, end, MarkupContext::Expression);+ p = end;+ *regex_can_start = false;+ continue;+ }+ }+ if c.is_ascii_whitespace() {+ if !word.is_empty() {+ *regex_can_start = regex_prefix_keyword(word);+ word.clear();+ }+ p += 1;+ continue;+ }+ if c.is_ascii_alphanumeric() || c == b'_' || c == b'$' {+ word.push(c.to_ascii_lowercase());+ if word.len() > 16 {+ word.clear();+ word.push(0);+ }+ p += 1;+ continue;+ }+ if !word.is_empty() {+ *regex_can_start = regex_prefix_keyword(word);+ word.clear();+ }+ if options.jsx && c == b'<' {+ if let Some((name_start, name_end, closing)) = tag_open(b, p) {+ mark_range(&mut context, p, name_end, MarkupContext::Tag);+ stack.push(MarkupFrame::Tag {+ start: p,+ name_start,+ name_end,+ closing,+ raw_eligible: false,+ image_slot: (!closing && image_tag_start(b, p)).then(|| {+ images.push((p, 0));+ images.len() - 1+ }),+ quote: None,+ escaped: false,+ last_non_space: b.get(name_end.wrapping_sub(1)).copied(),+ });+ p = name_end;+ continue;+ }+ }+ match c {+ b'\'' | b'"' | b'`' => {+ *quote = Some(c);+ *escaped = false;+ *regex_can_start = false;+ }+ b'{' => {+ *depth += 1;+ *regex_can_start = true;+ }+ b'}' => {+ *depth -= 1;+ *regex_can_start = false;+ if *depth == 0 {+ stack.pop();+ }+ }+ b')' | b']' | b'.' => *regex_can_start = false,+ _ => *regex_can_start = true,+ }+ p += 1;+ }+ MarkupFrame::RawText(tag) => {+ let needle = match tag {+ RawTag::Script => b"</script".as_slice(),+ RawTag::Style => b"</style".as_slice(),+ };+ let after = p + needle.len();+ if starts_with_ascii_case_insensitive(b, p, needle)+ && b.get(after)+ .is_none_or(|c| c.is_ascii_whitespace() || *c == b'>')+ {+ stack.pop();+ } else {+ context[p] = MarkupContext::RawText;+ p += 1;+ }}}- i += 1;}- false++ images.retain(|(start, end)| *end > *start);+ MarkupContextIndex { context, images }}-/// `text` runs from the start of a line to the end of the file: candidates-/// start on that line, and a tag may continue on the lines after it.-pub(crate) fn find_broken_images(text: &str) -> Vec<MatchCtx> {- let line = &text[..text.find('\n').unwrap_or(text.len())];- let b = text.as_bytes();+/// Scan the image candidates starting on one line, using the file-wide+/// context index. Tag ends were collected during that single scan and are+/// accepted only within the existing 8 KiB image-tag window.+pub(crate) fn find_broken_images_in_line(+ text: &str,+ index: &MarkupContextIndex,+ cursor: &mut usize,+ line_start: usize,+ line_end: usize,+) -> Vec<MatchCtx> {let mut out = Vec::new();- let mut pos = 0;- while let Some(m) = IMG_OPEN_RE.find_at(line, pos) {- pos = m.end();- if hidden_in_parent_tag(b, m.start()) { continue; }- let window = &b[..b.len().min(m.start() + MAX_IMG_TAG_BYTES)];- let TagScan::End(end) = scan_markup_tag(window, m.start(), None) else { continue; };- pos = (end + 1).min(line.len());- let tag = &text[m.start()..end + 1];+ while *cursor < index.images.len() && index.images[*cursor].0 < line_start {+ *cursor += 1;+ }+ while *cursor < index.images.len() {+ let (start, end) = index.images[*cursor];+ if start >= line_end {+ break;+ }+ *cursor += 1;+ if index.context.get(start) != Some(&MarkupContext::Tag) {+ continue;+ }+ let tag = &text[start..end];if image_source_is_missing(tag, "") {let tag = if tag.contains('\n') {- tag.lines().map(str::trim).filter(|l| !l.is_empty()).collect::<Vec<_>>().join(" ")+ tag.lines()+ .map(str::trim)+ .filter(|l| !l.is_empty())+ .collect::<Vec<_>>()+ .join(" ")} else {tag.to_string()};out.push(MatchCtx {groups: vec![Some(tag)],- index: m.start(),+ index: start - line_start,});}}out}+/// Matcher fallback for focused unit coverage. Production uses the shared+/// file-wide index built by `run_regex_matchers`.+pub(crate) fn find_broken_images(text: &str) -> Vec<MatchCtx> {+ let line_end = text.find('\n').unwrap_or(text.len());+ let index = scan_markup_context(+ text,+ MarkupScanOptions {+ jsx: true,+ expressions: true,+ raw_text: true,+ markup: true,+ },+ );+ let mut cursor = 0;+ find_broken_images_in_line(text, &index, &mut cursor, 0, line_end)+}+pub static REGEX_MATCHERS: Lazy<Vec<Matcher>> = Lazy::new(|| {vec![Matcher {@@ -2986,6 +3377,46 @@ mod tests {);}+ #[test]+ fn broken_image_context_scan_ignores_markup_literals_and_raw_text() {+ let svelte = r#"<!-- <img alt> -->+<script>const marker = '<img alt>';</script>+<style>body::before { content: '<img alt>'; }</style>+<div title="<img alt>"></div>+<div title={/* <img alt> */ "'<img alt>'"}></div>+<img alt="actual">"#;+ let index = scan_markup_context(+ svelte,+ MarkupScanOptions {+ jsx: false,+ expressions: true,+ raw_text: true,+ markup: true,+ },+ );+ assert_eq!(index.images.len(), 1);+ assert_eq!(+ &svelte[index.images[0].0..index.images[0].1],+ "<img alt=\"actual\">"+ );++ let jsx = r#"const View = () => <Box icon={<img alt="" />} title={'<img>'}>{'<img>'}{/* <img> */}{/<img>/}</Box>;"#;+ let index = scan_markup_context(+ jsx,+ MarkupScanOptions {+ jsx: true,+ expressions: true,+ raw_text: false,+ markup: true,+ },+ );+ assert_eq!(index.images.len(), 1);+ assert_eq!(+ &jsx[index.images[0].0..index.images[0].1],+ "<img alt=\"\" />"+ );+ }+#[test]fn border_accent_skips_tailwind_spinner() {let g = |line: &str| run("border-accent-on-rounded", line);diff --git a/crates/detect/tests/broken_image_source.rs b/crates/detect/tests/broken_image_source.rsindex cf6d6d08d..308d81248 100644--- a/crates/detect/tests/broken_image_source.rs+++ b/crates/detect/tests/broken_image_source.rs@@ -7,6 +7,8 @@ use impeccable_detect::detect_text::{detect_text, TextOptions};fn source_images_distinguish_markup_from_bindings_and_code_literals() {let cases: &[(&str, &str, &[usize])] = &[("View.svelte", include_str!("fixtures/broken-image/shorthand.svelte"), &[5, 6, 7, 11, 12]),+ ("TooltipMultiline.svelte", include_str!("fixtures/broken-image/tooltip-multiline.svelte"), &[]),+ ("TooltipSameLine.svelte", include_str!("fixtures/broken-image/tooltip-same-line.svelte"), &[]),("View.astro", include_str!("fixtures/broken-image/shorthand.astro"), &[4]),("View.vue", include_str!("fixtures/broken-image/bindings.vue"), &[7, 8]),("index.html", include_str!("fixtures/broken-image/missing.html"), &[1, 2, 3, 4, 5, 7, 9, 11, 18]),diff --git a/crates/detect/tests/fixtures/broken-image/tooltip-multiline.svelte b/crates/detect/tests/fixtures/broken-image/tooltip-multiline.sveltenew file mode 100644index 000000000..79184f285--- /dev/null+++ b/crates/detect/tests/fixtures/broken-image/tooltip-multiline.svelte@@ -0,0 +1,4 @@+<div data-tooltip="+ <img+ alt='Preview'>+">Hover</div>diff --git a/crates/detect/tests/fixtures/broken-image/tooltip-same-line.svelte b/crates/detect/tests/fixtures/broken-image/tooltip-same-line.sveltenew file mode 100644index 000000000..48f8f01ed--- /dev/null+++ b/crates/detect/tests/fixtures/broken-image/tooltip-same-line.svelte@@ -0,0 +1 @@+<div data-tooltip="<img alt='Preview'>">Hover</div>