Merge remote-tracking branch 'origin/main' into codex/pr28-final

This commit is contained in:
Cathryn Lavery
2026-08-12 10:07:10 -07:00
16 changed files with 663 additions and 21 deletions
+50 -16
View File
@@ -246,11 +246,13 @@ def normalize_url(raw: str) -> str:
- White / near-white fills (#fff, #ffffff, white) are kept as-is — they
act as contrast cutouts in 2-colour logos (e.g. white letter on coloured
circle). Every other colour becomes currentColor.
- inline style="fill:#..." is rewritten to a fill= attribute.
- inline fill/stroke colours are normalized without discarding other styles.
- 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,55 @@ 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"'
return "#fff"
return "currentColor"
# Rewrite inline style="fill:#rrggbb" → standalone fill= attribute
def _normalize_inline_style(match: re.Match) -> str:
quote, declarations = match.group(1), match.group(2)
declarations = re.sub(
r'(\bfill\s*:\s*)(#[0-9a-fA-F]{3,8}|white)\b',
lambda fill: f"{fill.group(1)}{_normalized_fill(fill.group(2))}",
declarations,
flags=re.IGNORECASE,
)
declarations = re.sub(
r'(\bstroke\s*:\s*)#[0-9a-fA-F]{3,8}\b',
lambda stroke: f"{stroke.group(1)}currentColor",
declarations,
flags=re.IGNORECASE,
)
return f"style={quote}{declarations}{quote}"
# Normalize supported inline colours while preserving unrelated declarations.
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',
_normalize_inline_style,
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: f'fill="{_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}" '
@@ -292,6 +322,10 @@ def normalize_url(raw: str) -> str:
def normalize_devicon(raw: str) -> str:
"""Devicon SVGs have colored fills and a 128x128 or 32x32 viewBox.
Strip fills/classes and rewrite to 24x24 currentColor."""
viewbox_match = re.search(
r'\bviewBox\s*=\s*(["\'])(.*?)\1', raw, flags=re.IGNORECASE
)
viewbox = viewbox_match.group(2).strip() if viewbox_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 Devicon payload")
@@ -303,7 +337,7 @@ def normalize_devicon(raw: str) -> str:
body = re.sub(r'\bclass="[^"]*"', 'fill="currentColor"', body)
body = re.sub(r"\s+", " ", body).strip()
return (
'<svg aria-hidden="true" width="24" height="24" viewBox="0 0 128 128" '
f'<svg aria-hidden="true" width="24" height="24" viewBox="{viewbox}" '
f'fill="currentColor">{body}</svg>'
)
+4 -2
View File
@@ -17,8 +17,10 @@ PAIRS = [
('…', '…'), # horizontal ellipsis
('•', '•'), # bullet
('→', '→'), # right arrow
('↑', '←'), # left arrow
('↑', '↑'), # up arrow (same prefix collision - rare)
# CP1252 leaves byte 0x90 undefined, so spell the left-arrow corruption
# with a Unicode escape instead of embedding an invisible control character.
('\u00e2\u2020\u0090', '←'), # left arrow
('↑', '↑'), # up arrow
('↓', '↓'), # down arrow
('≤', '≤'), # less-than-or-equal
('≥', '≥'), # greater-than-or-equal
+40
View File
@@ -0,0 +1,40 @@
#!/usr/bin/env python3
"""Regression tests for Devicon viewBox preservation."""
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 32"><path fill="#123456" d="M0 0h32v32H0z"/></svg>'
normalized = build_icons.normalize_devicon(svg)
if 'viewBox="0 0 32 32"' not in normalized:
raise AssertionError(f"source viewBox was not preserved: {normalized}")
if 'fill="#123456"' in normalized:
raise AssertionError("source fill escaped currentColor normalization")
if 'fill="currentColor"' not in normalized:
raise AssertionError("normalized icon does not inherit currentColor")
print("PASS: Devicon normalization preserves non-128 source viewBoxes")
return 0
if __name__ == "__main__":
raise SystemExit(main())
+58
View File
@@ -0,0 +1,58 @@
#!/usr/bin/env python3
"""Regression tests for quote-independent URL SVG normalization."""
from __future__ import annotations
import importlib.util
import re
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;opacity:.5' 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}")
path = re.search(r"<path\b([^>]*)>", normalized)
if path is None:
raise AssertionError(f"normalized path is missing: {normalized}")
path_attributes = path.group(1)
for declaration in ("fill:currentColor", "stroke:currentColor", "opacity:.5"):
if declaration not in path_attributes:
raise AssertionError(
f"path lost or failed to normalize {declaration}: {normalized}"
)
if (
'fill="currentColor"' not in normalized
or 'stroke="currentColor"' not in normalized
):
raise AssertionError(f"standalone quoted colours were not normalized: {normalized}")
print("PASS: URL SVG normalization handles single-quoted attributes")
return 0
if __name__ == "__main__":
raise SystemExit(main())
+40
View File
@@ -0,0 +1,40 @@
#!/usr/bin/env python3
"""Regression tests for the mojibake repair utility."""
from __future__ import annotations
import subprocess
import sys
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
SCRIPT = ROOT / "scripts" / "fix-mojibake.py"
def main() -> int:
# These are the strings produced when UTF-8 arrow bytes are decoded as
# Windows-1252. Byte 0x90 is undefined in CP1252, hence the explicit escape.
source = "right →, left \u00e2\u2020\u0090, up ↑, down ↓\n"
expected = "right →, left ←, up ↑, down ↓\n"
with tempfile.TemporaryDirectory(prefix="diagram-design-mojibake-") as directory:
path = Path(directory) / "fixture.md"
path.write_text(source, encoding="utf-8")
result = subprocess.run(
[sys.executable, str(SCRIPT), str(path)],
capture_output=True,
text=True,
check=False,
)
if result.returncode != 0:
raise AssertionError(f"repair failed: {result.stdout}{result.stderr}")
actual = path.read_text(encoding="utf-8")
if actual != expected:
raise AssertionError(f"expected {expected!r}, got {actual!r}")
print("PASS: mojibake arrow sequences repair to the intended directions")
return 0
if __name__ == "__main__":
raise SystemExit(main())