Editorial diagram design for Claude Code, Codex, GitHub Copilot, Cursor, Factory Droid, Qoder, and Pi. 45 diagram types. Self-contained HTML + SVG. No shadows. No Mermaid slop.
fix(export): preserve comment-like text in CSS strings (#426)
Rudy Mizrahi Celekli02b0ff9085d7
diff --git a/README.md b/README.mdindex 73827f8..f3f2eab 100644--- a/README.md+++ b/README.md@@ -457,7 +457,7 @@ What never carries over: source or renderer coordinates, source palette, source## Export to PNG / SVG-Diagrams ship as self-contained HTML, but you can export the diagram itself for Figma, slides, or social cards. SVG export also carries CDATA-wrapped diagram CSS, including selectors bound to the original SVG root. Use the slash command for your agent:+Diagrams ship as self-contained HTML, but you can export the diagram itself for Figma, slides, or social cards. SVG export also carries CDATA-wrapped diagram CSS, including selectors bound to the original SVG root, and preserves quoted selector values containing comment delimiters. Use the slash command for your agent:**Pi:**diff --git a/scripts/test-export-svg-standalone.py b/scripts/test-export-svg-standalone.pyindex 8ce611f..1817b6d 100644--- a/scripts/test-export-svg-standalone.py+++ b/scripts/test-export-svg-standalone.py@@ -66,6 +66,30 @@ class ExportSvgStandaloneTests(unittest.TestCase):def setUpClass(cls) -> None:cls.mod = load_helper()+ def test_css_comment_delimiters_inside_literals_survive_export(self) -> None:+ selectors = [+ 'rect[data-label="net/*retained*/revenue"]',+ "rect[data-label='net/*retained*/revenue']",+ r'rect[data-label="net\"/*retained*/revenue"]',+ r'.net\/\*retained\*\/revenue',+ ]+ for selector in selectors:+ with self.subTest(selector=selector):+ source = '<style>' + selector + ' {fill:red}</style>'+ source += '<svg viewBox="0 0 20 20"><rect width="20" height="20"/></svg>'+ exported = self.mod.export_svg_document(source, Path("literal.html"), system_fonts=True)+ self.assertIn('#literal-root ' + selector + ' { fill:red }', embedded_css(exported))++ def test_real_css_comments_still_leave_tokens_and_rules_intact(self) -> None:+ source = '<style>/* heading */ :root {--paint:red} rect/* between */.paint {fill:var(--paint)}</style>'+ source += '<svg viewBox="0 0 20 20"><rect class="paint" width="20" height="20"/></svg>'+ exported = self.mod.export_svg_document(source, Path("literal.html"), system_fonts=True)+ css = embedded_css(exported)+ self.assertIn('#literal-root { --paint:red }', css)+ self.assertIn('#literal-root rect.paint { fill:var(--paint) }', css)+ self.assertNotIn('heading', css)+ self.assertNotIn('between', css)+def test_selector_lists_preserve_quoted_and_escaped_literals(self) -> None:selectors = ['rect[data-label="Revenue, net"]',diff --git a/skills/diagram-design/references/export.md b/skills/diagram-design/references/export.mdindex 8357ce9..c64c6d4 100644--- a/skills/diagram-design/references/export.md+++ b/skills/diagram-design/references/export.md@@ -50,7 +50,7 @@ Source read and output write failures return exit 2 with an error diagnostic. Th- Make comments valid XML. Inside each `<!-- ... -->` outside CDATA, turn every `--` into `- -` and add a space after a body that ends in `-`. Refuse a comment that a browser closes before the next `-->` (`<!-->`, `<!--->`, or one containing `--!>`): the markup after it is live, and rewriting the comment would hide it.- Set `id="<slug>-root"` on the opening `<svg>` tag, where `<slug>` is the source basename without extension (e.g. `example-loop.html` → `example-loop`). This ID scopes carried CSS so several inlined figures do not leak rules into each other. When replacing an existing SVG root ID, retarget whole CSS ID tokens for that root in the carried CSS, including escaped identifier spellings; compound selectors such as `.diagram#original` remain bound to the root without adding another ancestor. Descendant IDs and quoted selector values remain unchanged; an unescaped dot belongs to a class selector, not to the ID token.4. **Carry page CSS into the SVG.** Class-styled diagrams (the loop family, process, medallion, data-flow, and others) declare fills and type in the page `<style>` block — `.station`, `.hub`, `.node-name`, and so on. Extracting the bare `<svg>` without those rules yields black boxes. Copy the page's diagram rules into a `<style>` inside `<defs>`, then:- - Unwrap CDATA sections in SVG stylesheet text before parsing rules, then strip CSS comments, so neither XML delimiters nor a comment becomes part of a selector. Keep CDATA content as raw text rather than XML-decoding it; the carried copy is XML-escaped below. The original embedded stylesheet stays in the SVG. CDATA-looking text in an HTML stylesheet remains literal, including inside SVG `foreignObject`, `desc`, or `title` elements. A nested `<svg>` inside HTML switches back to SVG stylesheet handling.+ - Unwrap CDATA sections in SVG stylesheet text before parsing rules, then strip real CSS comments, so neither XML delimiters nor a comment becomes part of a selector. Preserve comment-looking text inside quoted strings and escapes: `[data-label="net/*retained*/revenue"]` must still match the original attribute value. Keep CDATA content as raw text rather than XML-decoding it; the carried copy is XML-escaped below. The original embedded stylesheet stays in the SVG. CDATA-looking text in an HTML stylesheet remains literal, including inside SVG `foreignObject`, `desc`, or `title` elements. A nested `<svg>` inside HTML switches back to SVG stylesheet handling.- Re-scope `:root { … }` custom properties onto `#<slug>-root` so the figure keeps its own tokens.- Start selectors that begin at the `<svg>` element at the root instead: `svg .zone` becomes `#<slug>-root .zone` and `svg text` becomes `#<slug>-root text`. The exported root element is the `<svg>` itself, so `#<slug>-root svg .zone` would match nothing.- Split selector lists only at top-level commas. Preserve quoted and escaped attribute values, function arguments, and meaningful whitespace while scoping.diff --git a/skills/diagram-design/scripts/export_svg.py b/skills/diagram-design/scripts/export_svg.pyindex a9f5506..6da88fa 100755--- a/skills/diagram-design/scripts/export_svg.py+++ b/skills/diagram-design/scripts/export_svg.py@@ -59,7 +59,18 @@ SVG_TYPE_PREFIX_RE = re.compile(r"^svg(?![\w-])", re.IGNORECASE)# `stroke="currentColor"` reads `color`, and text without its own font rule# reads `font-family`.BODY_INHERITED_RE = re.compile(r"(?:^|;)\s*(color|font-family)\s*:\s*([^;]+)", re.IGNORECASE)-CSS_COMMENT_RE = re.compile(r"/\*.*?\*/", re.DOTALL)+# Strings and escapes consume their comment-looking text before comments do.+CSS_COMMENT_OR_LITERAL_RE = re.compile(+ r'''"(?:\\[\s\S]|[^"\\])*"|'(?:\\[\s\S]|[^'\\])*'|\\[\s\S]|/\*.*?\*/''',+ re.DOTALL,+)+++def strip_css_comments(css: str) -> str:+ """Remove CSS comments while preserving quoted and escaped literals."""+ return CSS_COMMENT_OR_LITERAL_RE.sub(+ lambda token: "" if token.group(0).startswith("/*") else token.group(0), css+ )# HTML-only attribute syntax that strict XML rejects: a valueless attribute# (`<g data-motion-item>`) or an unquoted value (`data-step=1`).@@ -636,7 +647,7 @@ def diagram_css_from_html(html: str, root_id: str, original_root_id: str = "") -block = CDATA_SECTION_RE.sub(lambda match: match.group(1), block)# A comment before a rule would otherwise become part of its selector# (`/* Tokens */ :root` is not recognised as `:root`).- block = CSS_COMMENT_RE.sub("", block)+ block = strip_css_comments(block)for match in RULE_RE.finditer(block):selector = match.group(1).strip()if original_root_id:@@ -889,7 +900,7 @@ def namespace_defs_ids(svg: str, prefix: str) -> str:def _decoded_css_property_text(style: str) -> str:"""Read opacity spellings conservatively without changing carried CSS."""- style = CSS_COMMENT_RE.sub("", html_entities.unescape(style))+ style = strip_css_comments(html_entities.unescape(style))def escape(match: re.Match[str]) -> str:hexadecimal, literal = match.groups()@@ -1003,7 +1014,7 @@ def normalize_rgba_presentation_attrs(svg: str) -> str:re.search(rf"(?:^|[;{{])\s*{prop}\s*:", _decoded_css_property_text(rule.group(2)), re.I)and _css_paint_may_target_descendants(rule.group(1), [element] + children)for style in STYLE_BLOCK_RE.findall(svg)- for rule in RULE_RE.finditer(CSS_COMMENT_RE.sub("", style))+ for rule in RULE_RE.finditer(strip_css_comments(style)))if inline_paint or descendant_paint or stylesheet_paint or opacity_name in css_opacity or opacity_counts[opacity_name] > int(opacity_name in values):continue@@ -1035,7 +1046,7 @@ def has_diagram_stylesheet(svg: str) -> bool:"""True when a <style> block contains CSS rules beyond the fonts @import."""for block in STYLE_BLOCK_RE.findall(svg):stripped = re.sub(r"@import\b[^;]*;", "", block, flags=re.IGNORECASE)- stripped = re.sub(r"/\*.*?\*/", "", stripped, flags=re.DOTALL)+ stripped = strip_css_comments(stripped)if RULE_RE.search(stripped):return Truereturn False