Skip to content

A comprehensive logo-design skill for Claude, Gemini CLI, Codex and other AI agents: principles, process, SVG craft, testing tools and a 1,400+ logo reference library.

Escape free-form titles in exported SVG variants (#6)

N884448a22c85
diff --git a/skills/logo-design/scripts/export_variants.py b/skills/logo-design/scripts/export_variants.py
index 79a46cf..897c909 100644
--- a/skills/logo-design/scripts/export_variants.py
+++ b/skills/logo-design/scripts/export_variants.py
@@ -29,6 +29,7 @@ Notes:
- Optical centring: --optical-offset -0.02 nudges square/app-icon/favicon marks up by 2 % of the canvas.
"""
import argparse
+import html
import json
import os
import re
@@ -68,7 +69,7 @@ def set_title(raw, title):
if not title:
return raw
raw = re.sub(r"<title>.*?</title>", "", raw, flags=re.S)
- return re.sub(r"(<svg\b[^>]*>)", lambda m: m.group(1) + f"<title>{title}</title>", raw, count=1)
+ return re.sub(r"(<svg\b[^>]*>)", lambda m: m.group(1) + f"<title>{html.escape(title)}</title>", raw, count=1)
def inner_markup(raw):
@@ -99,7 +100,7 @@ def square_wrap(raw, root, padding=0.1, bg=None, radius=0.0, fg=None, symbol_sca
if m:
carried += f' {attr}="{m.group(1)}"'
tile = f'<rect width="256" height="256" rx="{canvas * radius:g}" fill="{bg}"/>' if bg else ""
- t = f"<title>{title}</title>" if title else ""
+ t = f"<title>{html.escape(title)}</title>" if title else ""
return (f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" width="256" height="256">{t}'
f'{tile}<g transform="translate({tx:g} {ty:g}) scale({s:g})"{carried}>{body}</g></svg>')
diff --git a/tests/test_export_titles.py b/tests/test_export_titles.py
new file mode 100644
index 0000000..9c0eddb
--- /dev/null
+++ b/tests/test_export_titles.py
@@ -0,0 +1,31 @@
+"""SVG title regression tests; no renderer or assets required."""
+from pathlib import Path
+import sys
+import unittest
+import xml.etree.ElementTree as ET
+
+sys.dont_write_bytecode = True
+sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'skills/logo-design/scripts'))
+import export_variants as ev
+
+SVG = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256"><rect x="32" y="32" width="192" height="192"/></svg>'
+NS = '{http://www.w3.org/2000/svg}'
+
+
+class ExportTitles(unittest.TestCase):
+ def test_titles_round_trip_as_text_in_each_writer(self):
+ for title in ('Harbor', 'A & B', '<Studio> "Brand"', '東京 café', '</title><path/>'):
+ for writer in (lambda t: ev.set_title(SVG, t),
+ lambda t: ev.square_wrap(SVG, ET.fromstring(SVG), title=t)):
+ with self.subTest(title=title, writer=writer):
+ root = ET.fromstring(writer(title))
+ self.assertEqual(root.find(NS + 'title').text, title)
+ self.assertEqual(len(root.findall('.//' + NS + 'path')), 0)
+
+ def test_no_title_preserves_existing_behavior(self):
+ self.assertEqual(ev.set_title(SVG, None), SVG)
+ self.assertIsNone(ET.fromstring(ev.square_wrap(SVG, ET.fromstring(SVG))).find(NS + 'title'))
+
+
+if __name__ == '__main__':
+ unittest.main()