fix(icons): normalize quoted URL SVG attributes
Signed-off-by: Mr-Neutr0n <harikp2002@gmail.com>
This commit is contained in:
@@ -29,6 +29,9 @@ jobs:
|
||||
- name: Verify accessible SVG contract
|
||||
run: python3 scripts/test-lint-a11y.py
|
||||
|
||||
- name: Verify URL SVG normalization
|
||||
run: python3 scripts/test-build-icons-quoted-attributes.py
|
||||
|
||||
- name: Run skin linter
|
||||
run: python3 scripts/lint-skin.py --all --baseline
|
||||
|
||||
|
||||
+34
-12
@@ -249,8 +249,10 @@ def normalize_url(raw: str) -> str:
|
||||
- inline style="fill:#..." is rewritten to a fill= attribute.
|
||||
- Inkscape layer translate() transforms are stripped (canvas offset artefact).
|
||||
"""
|
||||
vb_match = re.search(r'viewBox="([^"]+)"', raw, re.IGNORECASE)
|
||||
viewbox = vb_match.group(1) if vb_match else "0 0 128 128"
|
||||
vb_match = re.search(
|
||||
r'\bviewBox\s*=\s*(["\'])(.*?)\1', raw, flags=re.IGNORECASE
|
||||
)
|
||||
viewbox = vb_match.group(2).strip() if vb_match else "0 0 128 128"
|
||||
inner = re.search(r"<svg\b[^>]*>(.*?)</svg>", raw, flags=re.DOTALL | re.IGNORECASE)
|
||||
if not inner:
|
||||
raise ValueError("no <svg> in payload")
|
||||
@@ -261,27 +263,47 @@ def normalize_url(raw: str) -> str:
|
||||
# Strip Inkscape canvas-offset translate transforms from group wrappers
|
||||
body = re.sub(r'\btransform="translate\([^"]+\)"', '', body)
|
||||
|
||||
def _rewrite_fill(m: re.Match) -> str:
|
||||
def _normalized_fill(value: str) -> str:
|
||||
"""Keep white; turn everything else into currentColor."""
|
||||
hex_val = m.group(1).lower().strip()
|
||||
hex_val = value.lower().strip()
|
||||
if hex_val in ("#fff", "#ffffff", "white", "#fefefe", "#fdfdfd"):
|
||||
return 'fill="#fff"'
|
||||
return 'fill="currentColor"'
|
||||
|
||||
# Rewrite inline style="fill:#rrggbb" → standalone fill= attribute
|
||||
# Rewrite inline style="fill:#rrggbb" → standalone fill= attribute.
|
||||
body = re.sub(
|
||||
r'style="[^"]*fill:\s*(#[0-9a-fA-F]{3,8}|white)[^"]*"',
|
||||
lambda m: _rewrite_fill(re.search(r'(#[0-9a-fA-F]{3,8}|white)', m.group(0))),
|
||||
r'style\s*=\s*(["\'])(.*?)\1',
|
||||
lambda m: (
|
||||
_normalized_fill(fill.group(1))
|
||||
if (fill := re.search(
|
||||
r'\bfill\s*:\s*(#[0-9a-fA-F]{3,8}|white)\b',
|
||||
m.group(2),
|
||||
flags=re.IGNORECASE,
|
||||
))
|
||||
else m.group(0)
|
||||
),
|
||||
body,
|
||||
flags=re.IGNORECASE,
|
||||
)
|
||||
# Rewrite standalone fill="#rrggbb" attributes
|
||||
# Rewrite standalone fill="#rrggbb" attributes.
|
||||
body = re.sub(
|
||||
r'\bfill="(#[0-9a-fA-F]{3,8}|white)"',
|
||||
_rewrite_fill,
|
||||
r'\bfill\s*=\s*(["\'])(#[0-9a-fA-F]{3,8}|white)\1',
|
||||
lambda m: _normalized_fill(m.group(2)),
|
||||
body,
|
||||
flags=re.IGNORECASE,
|
||||
)
|
||||
body = re.sub(
|
||||
r'\bstroke\s*=\s*(["\'])#[0-9a-fA-F]{3,8}\1',
|
||||
'stroke="currentColor"',
|
||||
body,
|
||||
flags=re.IGNORECASE,
|
||||
)
|
||||
body = re.sub(
|
||||
r'\bfill\s*=\s*(["\'])none\1',
|
||||
'',
|
||||
body,
|
||||
flags=re.IGNORECASE,
|
||||
)
|
||||
body = re.sub(r'\bstroke="#[0-9a-fA-F]{3,8}"', 'stroke="currentColor"', body)
|
||||
body = re.sub(r'\bfill="none"', '', body)
|
||||
body = re.sub(r"\s+", " ", body).strip()
|
||||
return (
|
||||
f'<svg aria-hidden="true" width="24" height="24" viewBox="{viewbox}" '
|
||||
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Regression tests for quote-independent URL SVG normalization."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
BUILD = ROOT / "scripts" / "build-icons.py"
|
||||
|
||||
|
||||
def load_build_module():
|
||||
sys.dont_write_bytecode = True
|
||||
spec = importlib.util.spec_from_file_location("diagram_design_build_icons", BUILD)
|
||||
if spec is None or spec.loader is None:
|
||||
raise AssertionError("could not load build-icons.py")
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
def main() -> int:
|
||||
build_icons = load_build_module()
|
||||
svg = (
|
||||
"<svg viewBox='0 0 32 16'>"
|
||||
"<path style='fill:#123456;stroke:#654321' d='M0 0h32v16H0z'/>"
|
||||
"<circle fill='#abcdef' stroke='#111111' cx='8' cy='8' r='4'/>"
|
||||
"</svg>"
|
||||
)
|
||||
normalized = build_icons.normalize_url(svg)
|
||||
if 'viewBox="0 0 32 16"' not in normalized:
|
||||
raise AssertionError(f"single-quoted viewBox was not preserved: {normalized}")
|
||||
if any(color in normalized for color in ("#123456", "#654321", "#abcdef", "#111111")):
|
||||
raise AssertionError(f"source colors escaped normalization: {normalized}")
|
||||
if normalized.count('fill="currentColor"') < 2:
|
||||
raise AssertionError(f"single-quoted fills were not normalized: {normalized}")
|
||||
if 'stroke="currentColor"' not in normalized:
|
||||
raise AssertionError(f"single-quoted strokes were not normalized: {normalized}")
|
||||
print("PASS: URL SVG normalization handles single-quoted attributes")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user