Skip to content

Commit e453b35

Browse files
claude: Improve ensureCssRegexMatches to parse HTML for CSS links
Instead of globbing all CSS files in the support directory, parse the HTML document to find `<link rel="stylesheet">` tags and only check those specific CSS files. This is more targeted and accurate. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent bf48448 commit e453b35

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

tests/verify.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import { existsSync, walkSync } from "../src/deno_ral/fs.ts";
8-
import { DOMParser, NodeList } from "../src/core/deno-dom.ts";
8+
import { DOMParser, Element, NodeList } from "../src/core/deno-dom.ts";
99
import { assert } from "testing/asserts";
1010
import { basename, dirname, join, relative, resolve } from "../src/deno_ral/path.ts";
1111
import { parseXmlDocument } from "slimdom";
@@ -584,7 +584,7 @@ export const ensureFileRegexMatches = (
584584
return(verifyFileRegexMatches(regexChecker)(file, matchesUntyped, noMatchesUntyped));
585585
};
586586

587-
// Use this function to Regex match text in CSS files in the supporting files directory
587+
// Use this function to Regex match text in CSS files linked from the HTML document
588588
export const ensureCssRegexMatches = (
589589
file: string,
590590
matchesUntyped: (string | RegExp)[],
@@ -602,14 +602,24 @@ export const ensureCssRegexMatches = (
602602
return {
603603
name: `Inspecting CSS files for Regex matches`,
604604
verify: async (_output: ExecuteOutput[]) => {
605-
// Find support directory from file path
606-
const [dir, stem] = dirAndStem(file);
607-
const supportDir = join(dir, stem + "_files");
605+
// Parse the HTML file to find linked CSS files
606+
const htmlContent = await Deno.readTextFile(file);
607+
const doc = new DOMParser().parseFromString(htmlContent, "text/html")!;
608+
const [dir] = dirAndStem(file);
608609

609-
// Find all CSS files recursively and combine their content
610+
// Find all stylesheet links and read their content
610611
let combinedContent = "";
611-
for (const entry of walkSync(supportDir, { exts: [".css"] })) {
612-
combinedContent += await Deno.readTextFile(entry.path) + "\n";
612+
const links = doc.querySelectorAll('link[rel="stylesheet"]');
613+
for (const link of links) {
614+
const href = (link as Element).getAttribute("href");
615+
if (href && !href.startsWith("http://") && !href.startsWith("https://")) {
616+
const cssPath = join(dir, href);
617+
try {
618+
combinedContent += await Deno.readTextFile(cssPath) + "\n";
619+
} catch {
620+
// Skip files that don't exist (e.g., external URLs we couldn't parse)
621+
}
622+
}
613623
}
614624

615625
matches.forEach((regex) => {

0 commit comments

Comments
 (0)