ladybird/Meta/check-html-doctype.py
Psychpsyo 3ab14f5595
Some checks failed
CI / macOS, arm64, Sanitizer_CI, Clang (push) Waiting to run
CI / Linux, x86_64, Fuzzers_CI, Clang (push) Waiting to run
CI / Linux, x86_64, Sanitizer_CI, GNU (push) Waiting to run
CI / Linux, x86_64, Sanitizer_CI, Clang (push) Waiting to run
Package the js repl as a binary artifact / macOS, arm64 (push) Waiting to run
Package the js repl as a binary artifact / Linux, x86_64 (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
Build Dev Container Image / build (push) Has been cancelled
Meta: Enforce doctypes on layout tests
2025-06-21 22:58:01 +02:00

56 lines
1.8 KiB
Python
Executable file

#!/usr/bin/env python3
import os
import re
import subprocess
import sys
RE_RELEVANT_FILE = re.compile("^Tests/LibWeb/(Layout|Ref|Screenshot|Text)/(.(?!wpt-import/))*\\.html$")
# Exclude files with encodings that would cause python to error out.
# FIXME: Ideally, these should be supported.
EXCLUDED_FILES = [
"Tests/LibWeb/Layout/input/html-encoding-detection-crash.html",
"Tests/LibWeb/Layout/input/utf-16-be-xhtml-file-should-decode-correctly.html",
]
RE_DOCTYPE = re.compile("^<!doctype .*>", re.IGNORECASE)
def should_check_file(filename):
if filename in EXCLUDED_FILES:
return False
return RE_RELEVANT_FILE.match(filename) is not None
def find_files_here_or_argv():
if len(sys.argv) > 1:
raw_list = sys.argv[1:]
else:
process = subprocess.run(["git", "ls-files"], check=True, capture_output=True)
raw_list = process.stdout.decode().strip("\n").split("\n")
return filter(should_check_file, raw_list)
def run():
files_with_missing_doctypes = []
for filename in find_files_here_or_argv():
with open(filename, "r") as file:
if not RE_DOCTYPE.search(file.readline()):
files_with_missing_doctypes.append(filename)
if files_with_missing_doctypes:
print(
"The following HTML files should include a doctype declaration at the start of the file but don't:\n"
+ "You should add <!DOCTYPE html> to the very beginning of these files, except if they absolutely need "
+ "to run in quirks mode. In that case, you can clearly indicate so with a bogus doctype that says "
+ '"quirks" instead of "html".\n',
" ".join(files_with_missing_doctypes),
)
sys.exit(1)
if __name__ == "__main__":
os.chdir(os.path.dirname(__file__) + "/..")
run()