Repository navigation
Benchmark the Doors checks on one content page #1287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3164196
ff41859
ea7d095
79b4328
5c2d1c1
f837119
c7e4d03
286ca34
9022cea
bd31d6b
ff313c0
7877c2f
23e45ea
583bbcd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| #!/usr/bin/env python3 | ||
| """Compare built pages and CSS/JS, and select the browser check mode.""" | ||
|
|
||
| import argparse | ||
| import hashlib | ||
| import json | ||
| import re | ||
| from pathlib import Path | ||
|
|
||
| FINGERPRINT = re.compile(r"\.(?:[0-9a-f]{128}|[0-9a-f]{96}|[0-9a-f]{64}|[0-9a-f]{32})(?=\.(?:css|js)\b)") | ||
| INTEGRITY = re.compile(r"""\s+integrity=(?:"[^"]*"|'[^']*'|[^\s>]+)""") | ||
|
|
||
|
|
||
| def normalize(html): | ||
| def clean_tag(match): | ||
| return INTEGRITY.sub("", match[0]) | ||
|
|
||
| return re.sub(r"<(?:link|script)\b[^>]*>", clean_tag, FINGERPRINT.sub("", html)) | ||
|
|
||
|
|
||
| def page_path(path): | ||
| path = "/" + path.lstrip("/") | ||
| return path[:-len("index.html")] if path.endswith("/index.html") else path | ||
|
|
||
|
|
||
| def inventory(root): | ||
| pages, bundles = {}, {} | ||
| for path in sorted(root.rglob("*")): | ||
| suffix = path.suffix | ||
| if suffix not in {".html", ".css", ".js"} or not path.is_file(): | ||
| continue | ||
| relative = path.relative_to(root).as_posix() | ||
| if suffix == ".html": | ||
| pages[page_path(relative)] = hashlib.sha256(normalize(path.read_text(encoding="utf-8")).encode()).digest() | ||
| else: | ||
| bundles.setdefault(FINGERPRINT.sub("", relative), set()).add(hashlib.sha256(path.read_bytes()).digest()) | ||
| return pages, bundles | ||
|
|
||
|
|
||
| def compare(base, head): | ||
| before, old_bundles = inventory(base) | ||
| after, new_bundles = inventory(head) | ||
| return { | ||
| "changed": sorted(path for path in before.keys() & after.keys() if before[path] != after[path]), | ||
| "added": sorted(after.keys() - before.keys()), | ||
| "removed": sorted(before.keys() - after.keys()), | ||
| "bundles_changed": old_bundles != new_bundles, | ||
| } | ||
|
|
||
|
|
||
| def load_selection(path): | ||
| if path is None: | ||
| print("Check mode: full (all pages)") | ||
| return "full", set() | ||
| diff = json.loads(Path(path).read_text(encoding="utf-8")) | ||
| live = set(diff["changed"]) | set(diff["added"]) | ||
| total = len(live) + len(diff["removed"]) | ||
| if diff.get("force_full") or total > 60: | ||
| mode = "full" | ||
| elif diff["bundles_changed"]: | ||
| mode = "core" | ||
| elif live: | ||
| mode = "changed" | ||
| else: | ||
| mode = "none" | ||
| print(f"Check mode: {mode}") | ||
| for field in ("changed", "added", "removed"): | ||
| print(f"{field.capitalize()} pages: {', '.join(diff[field]) or '(none)'}") | ||
| if mode == "none": | ||
| print("no changed pages") | ||
| return mode, live | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument("base", type=Path, nargs="?") | ||
| parser.add_argument("head", type=Path, nargs="?") | ||
| parser.add_argument("--out", type=Path, required=True) | ||
| parser.add_argument("--full", action="store_true") | ||
| args = parser.parse_args() | ||
| if args.full: | ||
| diff = {"changed": [], "added": [], "removed": [], "bundles_changed": False, "force_full": True} | ||
| else: | ||
| if not all(path and (path / "index.html").is_file() for path in (args.base, args.head)): | ||
| parser.error("base and head must each contain a built index.html") | ||
| diff = compare(args.base, args.head) | ||
| args.out.write_text(json.dumps(diff, indent=2) + "\n", encoding="utf-8") | ||
| load_selection(args.out) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -21,6 +21,8 @@ | |||||||
| sys.path.insert(0, HERE) | ||||||||
| from check_design import serve # noqa: E402 | ||||||||
|
|
||||||||
| from changed_pages import load_selection # noqa: E402 | ||||||||
|
|
||||||||
| LEVEL_A = {"wcag2a", "wcag21a", "wcag22a"} | ||||||||
| LEVEL_AA = {"wcag2aa", "wcag21aa", "wcag22aa"} | ||||||||
| EXTRA = { | ||||||||
|
|
@@ -57,14 +59,26 @@ def main(): | |||||||
| parser = argparse.ArgumentParser(description=__doc__) | ||||||||
| parser.add_argument("public", nargs="?") | ||||||||
| parser.add_argument("--url") | ||||||||
| parser.add_argument("--pages", help="JSON from changed_pages.py") | ||||||||
| parser.add_argument("--base", default="/") | ||||||||
| parser.add_argument("--axe", required=True, help="path to axe.min.js") | ||||||||
| parser.add_argument("--out", help="write every finding to this JSON file") | ||||||||
| args = parser.parse_args() | ||||||||
| mode, pages = load_selection(args.pages) | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: The scoped selection reintroduces Prompt for AI agents
Suggested change
|
||||||||
| if mode == "none": | ||||||||
| if args.out: | ||||||||
| with open(args.out, "w", encoding="utf-8") as handle: | ||||||||
| json.dump([], handle) | ||||||||
| return | ||||||||
| base = args.url.rstrip("/") + "/" if args.url else serve(os.path.abspath(args.public), args.base) | ||||||||
| with open(os.path.join(HERE, "sample_pages.json"), encoding="utf-8") as handle: | ||||||||
| sample = {**json.load(handle), **EXTRA} | ||||||||
| sample.pop("404", None) | ||||||||
| if mode == "changed": | ||||||||
| names = {path: name for name, path in sample.items()} | ||||||||
| sample = {names.get(path, path): path for path in sorted(pages)} | ||||||||
| elif mode == "core": | ||||||||
| sample.update({path: path for path in sorted(pages - set(sample.values()))}) | ||||||||
| with open(args.axe, encoding="utf-8") as handle: | ||||||||
| axe = handle.read() | ||||||||
| findings, failures, practice = [], [], collections.Counter() | ||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2:
normalizedrops SRI from every<link>/<script>, including unfingerprinted vendor resources. Ansri-only change indata/assets.tomltherefore selects no checks, even though it can change whether that dependency loads.Prompt for AI agents