v0.43.1
This commit is contained in:
1 parent
5b6d4a7903
commit
4d1087e357
160 files changed
+2407
-1933
No files matched your search
@@ -28,7 +28,6 @@ UNSUPPORTED_PSEUDO_ELEMENTS = re.compile(r'::(?:-webkit-[\w-]+|-moz-[\w-]+|backd
|
||||
WORD_PATTERN = re.compile(r'(?<![\w-])[a-zA-Z_][\w-]*(?![\w-])')
|
||||
VENDOR_SELECTOR = re.compile(r'::?-(webkit|moz|ms|o)-[\w-]+')
|
||||
|
||||
|
||||
def panel_sources() -> list[Path]:
|
||||
if __package__:
|
||||
from .build.panel_css import source_files
|
||||
@@ -36,7 +35,6 @@ def panel_sources() -> list[Path]:
|
||||
from build.panel_css import source_files
|
||||
return source_files(ROOT)
|
||||
|
||||
|
||||
def embedded_javascript_sources() -> list[Path]:
|
||||
paths = set()
|
||||
for parent in (ROOT / 'src').rglob('*.rs'):
|
||||
@@ -46,7 +44,6 @@ def embedded_javascript_sources() -> list[Path]:
|
||||
paths.add(path)
|
||||
return sorted(paths)
|
||||
|
||||
|
||||
def structural_inventory(paths: list[Path]) -> dict:
|
||||
"""Report repetitions in their actual conditional scope; never reorder CSS."""
|
||||
selectors = defaultdict(list)
|
||||
@@ -99,7 +96,6 @@ def structural_inventory(paths: list[Path]) -> dict:
|
||||
families = [tuple(sorted({family for selector in entry['selectors']
|
||||
for family in VENDOR_SELECTOR.findall(selector)}))
|
||||
for entry in entries]
|
||||
# Vendor pseudo-element families legitimately repeat one rule body.
|
||||
vendor_only = (any(families) and all(len(family) <= 1 for family in families)
|
||||
and len(families) == len(set(families)))
|
||||
repeated_bodies.append({'scope': list(scope), 'rules': entries,
|
||||
@@ -116,10 +112,7 @@ def structural_inventory(paths: list[Path]) -> dict:
|
||||
'parse_errors': parse_errors,
|
||||
}
|
||||
|
||||
|
||||
def source_words(scope: str) -> set[str]:
|
||||
# Rust's include_str! lists are the asset entry points; an abandoned JS file
|
||||
# beside a bundle must not make its obsolete selectors appear live.
|
||||
paths = embedded_javascript_sources() + list(WEB.rglob('*.html'))
|
||||
if scope == 'panel':
|
||||
paths += list(WEB.rglob('*.json')) + list((ROOT / 'src').rglob('*.rs'))
|
||||
@@ -130,7 +123,6 @@ def source_words(scope: str) -> set[str]:
|
||||
words.update(WORD_PATTERN.findall(path.read_text(encoding='utf-8', errors='replace')))
|
||||
return words
|
||||
|
||||
|
||||
def selector_parts(tokens: list) -> list[str]:
|
||||
parts: list[list] = [[]]
|
||||
for tok in tokens:
|
||||
@@ -140,7 +132,6 @@ def selector_parts(tokens: list) -> list[str]:
|
||||
parts[-1].append(tok)
|
||||
return [tinycss2.serialize(part).strip() for part in parts if tinycss2.serialize(part).strip()]
|
||||
|
||||
|
||||
def rules(stylesheet: str):
|
||||
def walk(items):
|
||||
for item in items:
|
||||
@@ -150,7 +141,6 @@ def rules(stylesheet: str):
|
||||
yield from walk(tinycss2.parse_rule_list(item.content, skip_whitespace=True, skip_comments=True))
|
||||
return list(walk(tinycss2.parse_stylesheet(stylesheet, skip_whitespace=True, skip_comments=True)))
|
||||
|
||||
|
||||
def token_refs(node) -> list[tuple[str, str]]:
|
||||
if isinstance(node, css_parser.ClassSelector):
|
||||
return [('class', node.class_name)]
|
||||
@@ -168,10 +158,7 @@ def token_refs(node) -> list[tuple[str, str]]:
|
||||
out.extend(token_refs(child))
|
||||
return out
|
||||
|
||||
|
||||
def possibly_matches(node, words: set[str]) -> bool:
|
||||
# Only positive class/id requirements identify review candidates. Runtime
|
||||
# interpolation still requires manual inspection before removing a rule.
|
||||
if isinstance(node, css_parser.ClassSelector):
|
||||
return node.class_name in words
|
||||
if isinstance(node, css_parser.IDSelector):
|
||||
@@ -192,10 +179,8 @@ def possibly_matches(node, words: set[str]) -> bool:
|
||||
return possibly_matches(node.parsed_tree, words)
|
||||
if isinstance(node, css_parser.RelativeSelector):
|
||||
return possibly_matches(node.selector, words)
|
||||
# Pseudo-classes, type selectors, vendor syntax and other unknowns: KEEP.
|
||||
return True
|
||||
|
||||
|
||||
def audit_file(path: Path, words: set[str], scope: str) -> dict:
|
||||
raw = path.read_text(encoding='utf-8')
|
||||
entries = []
|
||||
@@ -210,13 +195,10 @@ def audit_file(path: Path, words: set[str], scope: str) -> dict:
|
||||
references = set().union(*(set(token_refs(item)) for item in parsed))
|
||||
all_classes.update(word for typ, word in references if typ == 'class')
|
||||
dead = bool(parsed) and not any(possibly_matches(item, words) for item in parsed)
|
||||
# docs.css targets classes emitted by the third-party Swagger UI bundle.
|
||||
external = scope == 'docs' and '.swagger-ui' in selector
|
||||
if external:
|
||||
dead = False
|
||||
missing = sorted({f'{typ}:{word}' for typ, word in references if word not in words})
|
||||
# Retain the historical JSON key for older audit consumers;
|
||||
# "dead" is a candidate flag, never proof of runtime absence.
|
||||
parts.append({'selector': selector, 'dead': dead, 'review_required': dead, 'missing_tokens': missing,
|
||||
'external': external})
|
||||
except css_parser.SelectorError as exc:
|
||||
@@ -234,7 +216,6 @@ def audit_file(path: Path, words: set[str], scope: str) -> dict:
|
||||
'unparsed': unknown, 'entries': entries,
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
cli = argparse.ArgumentParser(description=__doc__)
|
||||
cli.add_argument('--json', type=Path, help='Write complete selector audit JSON to this file')
|
||||
@@ -270,6 +251,5 @@ def main():
|
||||
if args.strict and failures:
|
||||
raise SystemExit('Panel CSS structure check failed: ' + ', '.join(failures))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in new issue
Block a user