The design language that makes your AI harness better at design.
JavaScript
Fix: broken-image accepts srcset as an image source (#1112) (#1119)
Abdul Wahab583595f175d3
diff --git a/crates/detect/src/regex_matchers.rs b/crates/detect/src/regex_matchers.rsindex 4ef46e472..88f64e6d8 100644--- a/crates/detect/src/regex_matchers.rs+++ b/crates/detect/src/regex_matchers.rs@@ -1764,7 +1764,7 @@ pub(crate) fn image_source_is_missing(tag: &str, ext: &str) -> bool {if b[p] == b'{' {let Some(close) = matching_close(b, p) else { return false; };let expression = tag[p + 1..close].trim();- if shorthand && (expression == "src" || expression.starts_with("...")) {+ if shorthand && (matches!(expression, "src" | "srcset") || expression.starts_with("...")) {return false;}// JSX spreads also leave the source unknown.@@ -1816,15 +1816,16 @@ pub(crate) fn image_source_is_missing(tag: &str, ext: &str) -> bool {true}-/// `src`, a binding of it (`:src`, `v-bind:src`, Svelte `bind:src`, Vue-/// modifiers), or an alias such as lazy loading's `data-src`, all of which-/// the original `\bsrc\s*=` matcher accepted.+/// `src` or `srcset` (an `<img>` needs either), a binding of one (`:src`,+/// `v-bind:src`, Svelte `bind:src`, Vue modifiers), or an alias such as lazy+/// loading's `data-src`, which the original `\bsrc\s*=` matcher accepted.fn names_image_source(name: &str) -> bool {+ let is_source = |n: &str| n.eq_ignore_ascii_case("src") || n.eq_ignore_ascii_case("srcset");let base = name.split('.').next().unwrap_or(name);- base.eq_ignore_ascii_case("src")+ is_source(base)|| base.rsplit_once(|c| c == ':' || c == '-')- .is_some_and(|(_, last)| last.eq_ignore_ascii_case("src"))+ .is_some_and(|(_, last)| is_source(last))}/// How far a tag may run past its `<img`, so an unclosed one costs little.diff --git a/crates/detect/tests/broken_image_source.rs b/crates/detect/tests/broken_image_source.rsindex fcdeba3a9..ce8fe2510 100644--- a/crates/detect/tests/broken_image_source.rs+++ b/crates/detect/tests/broken_image_source.rs@@ -19,7 +19,7 @@ fn source_images_distinguish_markup_from_bindings_and_code_literals() {// An unclosed attribute quote does not hide the rest of the file.("oops.html", include_str!("fixtures/broken-image/unclosed-quote.html"), &[3]),("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]),+ ("index.html", include_str!("fixtures/broken-image/missing.html"), &[1, 2, 3, 4, 5, 7, 9, 11, 18, 24]),("markers.ts", include_str!("fixtures/broken-image/literals.js"), &[]),("markers.js", include_str!("fixtures/broken-image/literals.js"), &[]),("markers.mjs", include_str!("fixtures/broken-image/literals.js"), &[]),diff --git a/crates/detect/tests/fixtures/broken-image/bindings.vue b/crates/detect/tests/fixtures/broken-image/bindings.vueindex 35c566a60..faf5d4715 100644--- a/crates/detect/tests/fixtures/broken-image/bindings.vue+++ b/crates/detect/tests/fixtures/broken-image/bindings.vue@@ -9,4 +9,5 @@<img:src="src"alt="">+ <img :srcset="set" alt=""></template>diff --git a/crates/detect/tests/fixtures/broken-image/elements.jsx b/crates/detect/tests/fixtures/broken-image/elements.jsxindex 67a23cc98..8f60b09fe 100644--- a/crates/detect/tests/fixtures/broken-image/elements.jsx+++ b/crates/detect/tests/fixtures/broken-image/elements.jsx@@ -30,3 +30,4 @@ export const MultiMissing = () => (alt=""/>);+export const Srcset = () => <img srcSet="/a.png 1x" alt="" />;diff --git a/crates/detect/tests/fixtures/broken-image/missing.html b/crates/detect/tests/fixtures/broken-image/missing.htmlindex 518f7ce7c..e50e5b131 100644--- a/crates/detect/tests/fixtures/broken-image/missing.html+++ b/crates/detect/tests/fixtures/broken-image/missing.html@@ -18,3 +18,8 @@<p title="C:\"><img src="" alt=""></p><div data-tooltip="<imgalt='Preview'>">Hover</div>+<img srcset="photo.png 1x, [email protected] 2x" alt="Example">+<img srcset="photo.png 400w, [email protected] 800w" sizes="100vw" alt="Example">+<img src="" srcset="/a.png 1x" alt="">+<img srcset="" alt="">+<img data-srcset="/lazy.png 1x" class="lazyload" alt="">diff --git a/crates/detect/tests/fixtures/broken-image/shorthand.svelte b/crates/detect/tests/fixtures/broken-image/shorthand.svelteindex ef9c2ec94..e7aa70277 100644--- a/crates/detect/tests/fixtures/broken-image/shorthand.svelte+++ b/crates/detect/tests/fixtures/broken-image/shorthand.svelte@@ -12,3 +12,4 @@<imgsrc=""alt="Portrait" />+<img {srcset} alt="">diff --git a/crates/html/src/adapters.rs b/crates/html/src/adapters.rsindex b015372f4..90cad57fa 100644--- a/crates/html/src/adapters.rs+++ b/crates/html/src/adapters.rs@@ -1473,6 +1473,13 @@ pub fn check_element_glow(/// JS: detect-html.mjs#checkElementBrokenImage(el)pub fn check_element_broken_image(el: &StaticElement<'_>) -> Vec<RuleHit> {+ // `srcset` is a source too (HTML Standard: an img needs `src` or `srcset`).+ if el.get_attribute("srcset").is_some_and(|set| {+ let set = js::trim(set);+ !set.is_empty() && set != "#"+ }) {+ return Vec::new();+ }let Some(src) = el.get_attribute("src") else {return vec![RuleHit::new("broken-image",diff --git a/crates/html/tests/broken_image.rs b/crates/html/tests/broken_image.rsnew file mode 100644index 000000000..e85d0961b--- /dev/null+++ b/crates/html/tests/broken_image.rs@@ -0,0 +1,21 @@+use impeccable_html::{detect_html_source, DetectHtmlOptions};+use std::path::Path;++fn count(body: &str) -> usize {+ detect_html_source(body, Path::new("/tmp/broken-image.html"), &DetectHtmlOptions::default())+ .iter().filter(|f| f.antipattern == "broken-image").count()+}++#[test]+fn srcset_is_an_image_source() {+ assert_eq!(count("<img srcset=\"photo.png 1x, [email protected] 2x\" alt=\"Example\">"), 0);+ assert_eq!(count("<img srcset=\"photo.png 400w, [email protected] 800w\" sizes=\"100vw\" alt=\"Example\">"), 0);+ assert_eq!(count("<img src=\"\" srcset=\"/a.png 1x\" alt=\"\">"), 0);+}++#[test]+fn an_image_without_a_source_is_still_reported() {+ assert_eq!(count("<img srcset=\"\" alt=\"\">"), 1);+ assert_eq!(count("<img alt=\"\">"), 1);+ assert_eq!(count("<img src=\"#\" alt=\"\">"), 1);+}