fix: escape newlines in WireManager.add_text
The KiCad s-expression parser rejects raw newline and carriage-return characters inside quoted string literals — a multi-line text annotation written through `add_text` produced a `.kicad_sch` file that eeschema silently tolerated but `kicad-cli sch ...` refused with "Failed to load schematic." The escape pass only handled backslashes and double quotes. Add `\\n` → `\\\\n` and `\\r` → `\\\\r` to the same escape chain. Order matters: backslashes are escaped first so we don't double-escape our own escapes. A new regression test (`test_escapes_newlines_in_multiline_text`) checks both that the resulting quoted string literal contains no raw newline characters and that the file round-trips cleanly through the sexpdata parser. End-to-end smoke: a 4-line annotation written through the patched add_text now passes `kicad-cli sch erc` (exit 0) where the previous behaviour failed parse. Note: the same escape gap exists in `_make_hierarchical_label_text` and `_make_sheet_pin_text` for unescaped quotes/newlines in the user- supplied text. Not fixed here to keep this PR scoped to the documented add_text bug; happy to fold it in if a reviewer prefers.
This commit is contained in:
@@ -1045,7 +1045,15 @@ class WireManager:
|
||||
) -> bool:
|
||||
"""Add a free-form text annotation (SCH_TEXT) to a KiCad schematic."""
|
||||
try:
|
||||
text_escaped = text.replace("\\", "\\\\").replace('"', '\\"')
|
||||
# KiCad's parser rejects raw newlines inside quoted string literals,
|
||||
# so escape them along with backslashes and quotes. Order matters:
|
||||
# backslashes first, otherwise we double-escape our own escapes.
|
||||
text_escaped = (
|
||||
text.replace("\\", "\\\\")
|
||||
.replace('"', '\\"')
|
||||
.replace("\n", "\\n")
|
||||
.replace("\r", "\\r")
|
||||
)
|
||||
uid = str(uuid.uuid4())
|
||||
font_attrs = f"\n\t\t\t\t(size {font_size} {font_size})"
|
||||
if bold:
|
||||
|
||||
Reference in New Issue
Block a user