Skip to content

Your app looks AI-generated. Vincent Van Gogh noticed. An agent skill that finds and fixes AI-looking UI and UX in mobile apps, web apps, and websites.

fix(detect): flag lowercase onclick in clickable-div (#141) (#150)

Pure Tech182178489ab0
diff --git a/skills/unslop-anything/scripts/detect.js b/skills/unslop-anything/scripts/detect.js
index 1781fa2..6d57287 100644
--- a/skills/unslop-anything/scripts/detect.js
+++ b/skills/unslop-anything/scripts/detect.js
@@ -300,14 +300,37 @@ function lineAt(starts, index) {
// Index of the ">" that closes the tag, skipping any "{...}" attribute expression so an arrow
// function (=>) or a comparison inside it does not end the tag early. -1 when nothing closes it.
function tagEnd(text, from) {
+ const end = walkTag(text, from);
+ return end === -1 ? text.indexOf(">", from) : end;
+}
+
+// Walks an opening tag from `from` and returns the index of its closing ">", or -1. A > inside a
+// quoted attribute value (title="a > b") does not end the tag, and a brace inside a string or a
+// regex literal in an expression (onLoad={() => f("}")}) does not close it. `own(i)` is called
+// for each character outside {...} expressions: the element's own attributes.
+function walkTag(text, from, own) {
let depth = 0;
+ let quote = null;
for (let i = from; i < text.length; i++) {
const c = text[i];
- if (c === "{") depth++;
+ if (own && depth === 0) own(i);
+ if (quote) {
+ if (c === "\\" && depth > 0) i++;
+ else if (c === quote) quote = null;
+ } else if (c === '"' || c === "'" || (depth > 0 && c === "`")) quote = c;
+ else if (c === "/" && depth > 0 && regexInExpression(text, i)) i = skipRegex(text, i) - 1;
+ else if (c === "{") depth++;
else if (c === "}" && depth > 0) depth--;
else if (c === ">" && depth === 0) return i;
}
- return text.indexOf(">", from);
+ return -1;
+}
+
+// isRegexStart, plus the slash right after an arrow (() => /x/.test(v)).
+function regexInExpression(text, i) {
+ let j = i - 1;
+ while (j >= 0 && (text[j] === " " || text[j] === "\t")) j--;
+ return (text[j] === ">" && text[j - 1] === "=") || isRegexStart(text, i);
}
function* matches(re, text) {
@@ -1046,9 +1069,15 @@ const rules = [
fix: "Use a <button> for actions and an <a href> for navigation. They come with focus, Enter and Space, and the right role.",
find({ code }) {
const hits = [];
- for (const m of matches(/<(?:div|span|li|td|img|a)\b[^<>]*?\s(?:onClick|@click|v-on:click|on:click)\s*=/, code)) {
+ // Each opening tag is read to its real end first, so an arrow function in an earlier
+ // attribute (`onKeyDown={(e) => ...}`) does not hide the click handler after it.
+ for (const m of matches(/<(?:div|span|li|td|img|a)\b/, code)) {
const end = tagEnd(code, m.index + m[0].length);
const tag = code.slice(m.index, end === -1 ? undefined : end);
+ // Only this element's own attributes: a nested element inside {...} is checked on its own.
+ let top = m[0];
+ walkTag(code, m.index + m[0].length, (i) => { top += code[i]; });
+ if (!/\s(?:on[cC]lick|@click|v-on:click|on:click)(?:\.[\w-]+|\|\w+)*\s*=/.test(top)) continue;
if (/^<a\b/.test(tag) && /(?:^|[\s{:[])href\]?\s*=/.test(tag)) continue;
// A non-semantic clickable element (or href-less anchor) is only
// keyboard- and screen-reader-accessible when it has both an
diff --git a/test/detect.test.js b/test/detect.test.js
index 36b939d..04d75e2 100644
--- a/test/detect.test.js
+++ b/test/detect.test.js
@@ -884,6 +884,42 @@ test("clickable-div requires both an interactive role and tabIndex on div/span/l
assert.deepStrictEqual(rulesFor('<div role="button" tabindex="0" on:click={open}>Open</div>\n', "A.svelte"), []);
});
+test("tagEnd skips strings inside an attribute expression", () => {
+ const imgs = (code) => scanSource(code, "A.tsx").filter((f) => f.rule === "img-no-alt");
+ assert.deepStrictEqual(imgs('<img onLoad={() => loaded("}")} alt="Photo" src={src} />\n').length, 0);
+ // nor does a quote inside a regex literal in an expression
+ assert.deepStrictEqual(imgs('<img onLoad={() => /"/.test(value)} alt="Photo" src={src} />\n').length, 0);
+});
+
+test("clickable-div catches lowercase onclick in Svelte 5 and plain HTML (issue #141)", () => {
+ const lines = (src, file) => scanSource(src, file).filter((f) => f.rule === "clickable-div").map((f) => f.line);
+ const svelte = '<script>\n let open = $state(false);\n</script>\n\n<div class="card" onclick={() => (open = true)}>Details</div>\n';
+ assert.deepStrictEqual(lines(svelte, "Card.svelte"), [5]);
+ assert.deepStrictEqual(lines('<div class="card" onclick="openCard()">Details</div>\n', "page.html"), [1]);
+ assert.deepStrictEqual(lines('<span onclick="toggle()">More</span>\n', "page.html"), [1]);
+ // The exemptions still apply: an anchor with href, or an interactive role and tabindex.
+ assert.deepStrictEqual(lines('<a href="/details" onclick="track()">Details</a>\n', "page.html"), []);
+ assert.deepStrictEqual(lines('<a href={url} onclick={track}>Details</a>\n', "Card.svelte"), []);
+ assert.deepStrictEqual(lines('<div role="button" tabindex="0" onclick={open}>Open</div>\n', "Card.svelte"), []);
+ // Event modifiers: Vue @click.stop, Svelte 4 on:click|preventDefault.
+ assert.deepStrictEqual(lines('<div @click.stop="open">x</div>\n', "A.vue"), [1]);
+ assert.deepStrictEqual(lines('<div v-on:click.prevent.self="open">x</div>\n', "A.vue"), [1]);
+ assert.deepStrictEqual(lines("<div on:click|preventDefault={open}>x</div>\n", "A.svelte"), [1]);
+ assert.deepStrictEqual(lines('<div role="button" tabindex="0" @click.stop="open">x</div>\n', "A.vue"), []);
+ // An arrow function or a quoted > in an earlier attribute does not hide the handler.
+ assert.deepStrictEqual(lines("<div onKeyDown={(e) => handleKey(e)} onClick={open}>Open</div>\n", "A.tsx"), [1]);
+ assert.deepStrictEqual(lines("<div on:keydown={(e) => handleKey(e)} onclick={open}>Open</div>\n", "A.svelte"), [1]);
+ assert.deepStrictEqual(lines('<div title="a > b" onclick="go()">x</div>\n', "page.html"), [1]);
+ assert.deepStrictEqual(lines('<a title="a > b" href="/x" onclick="go()">x</a>\n', "page.html"), []);
+ // A handler of a nested element inside an attribute belongs to that element.
+ assert.deepStrictEqual(lines('<div render={<span role="button" tabIndex={0} onClick={x}>Open</span>} />\n', "A.tsx"), []);
+ assert.deepStrictEqual(lines('<div render={\n <span onClick={x}>Open</span>\n} />\n', "A.tsx"), [2]);
+ // A brace inside a quoted value is text, not an expression.
+ assert.deepStrictEqual(lines('<div title="{" onClick={x}>Open</div>\n', "A.tsx"), [1]);
+ // A real button with onclick is fine.
+ assert.deepStrictEqual(lines('<button onclick="openCard()">Details</button>\n', "page.html"), []);
+});
+
test("focus-removed flags a bare focus:outline-none, not one with a ring replacement", () => {
const rulesFor = (src) => scanSource(src, "a.tsx").map((f) => f.rule);
assert.deepStrictEqual(rulesFor('<input className="focus:outline-none" />\n'), ["focus-removed"]);