Skip to content

Commit 7a1b548

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 70bb0e9 commit 7a1b548

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";
@@ -643,7 +643,7 @@ export const ensureFileRegexMatches = (
643643
return(verifyFileRegexMatches(regexChecker)(file, matchesUntyped, noMatchesUntyped));
644644
};
645645

646-
// Use this function to Regex match text in CSS files in the supporting files directory
646+
// Use this function to Regex match text in CSS files linked from the HTML document
647647
export const ensureCssRegexMatches = (
648648
file: string,
649649
matchesUntyped: (string | RegExp)[],
@@ -661,14 +661,24 @@ export const ensureCssRegexMatches = (
661661
return {
662662
name: `Inspecting CSS files for Regex matches`,
663663
verify: async (_output: ExecuteOutput[]) => {
664-
// Find support directory from file path
665-
const [dir, stem] = dirAndStem(file);
666-
const supportDir = join(dir, stem + "_files");
664+
// Parse the HTML file to find linked CSS files
665+
const htmlContent = await Deno.readTextFile(file);
666+
const doc = new DOMParser().parseFromString(htmlContent, "text/html")!;
667+
const [dir] = dirAndStem(file);
667668

668-
// Find all CSS files recursively and combine their content
669+
// Find all stylesheet links and read their content
669670
let combinedContent = "";
670-
for (const entry of walkSync(supportDir, { exts: [".css"] })) {
671-
combinedContent += await Deno.readTextFile(entry.path) + "\n";
671+
const links = doc.querySelectorAll('link[rel="stylesheet"]');
672+
for (const link of links) {
673+
const href = (link as Element).getAttribute("href");
674+
if (href && !href.startsWith("http://") && !href.startsWith("https://")) {
675+
const cssPath = join(dir, href);
676+
try {
677+
combinedContent += await Deno.readTextFile(cssPath) + "\n";
678+
} catch {
679+
// Skip files that don't exist (e.g., external URLs we couldn't parse)
680+
}
681+
}
672682
}
673683

674684
matches.forEach((regex) => {

0 commit comments

Comments
 (0)