fix: avoid crash when exporting an empty document#462
Open
SAY-5 wants to merge 1 commit intomkoskim:masterfrom
Open
fix: avoid crash when exporting an empty document#462SAY-5 wants to merge 1 commit intomkoskim:masterfrom
SAY-5 wants to merge 1 commit intomkoskim:masterfrom
Conversation
`formatTXT.file` and `formatMD.file` interpolated `title.toUpperCase()` without first checking that `title` was defined. A freshly-created or otherwise empty story has no `<title>` in its head, so opening Export view (or running the TXT/MD exporter) crashed with `TypeError: Cannot read properties of undefined (reading 'toUpperCase')`. The trailing `?? ""` in those template literals only applied to the result of `.toUpperCase()`, not to `title` itself, so the crash hit before the fallback could be reached. Now the title line is rendered the same way subtitle already is — emitted only when present, and skipped entirely when missing. A regression test (`test/test_export_empty.js`) loads a minimal empty mawe document and runs every formatter (RTF, HTML, TEX1, TEX2, TXT, MD) through `flattedFormat` to make sure none of them throws. Closes mkoskim#460
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #460.
`formatTXT.file` and `formatMD.file` both interpolate
`title.toUpperCase()` directly into their template literals:
The trailing `?? ""` only fires after the method call, so when
`title` is undefined (a brand-new or otherwise empty mawe document
that has no `<title>` in its head), the call throws
`TypeError: Cannot read properties of undefined (reading 'toUpperCase')`
before the fallback can be reached.
That throw bubbles out of the Preview `flattedFormat` call, which is
why opening the Export view on an empty document crashes — it's the
TXT/MD path the issue was filed against, but every render of the
preview stack hits the same code in those two formats.
Fix
Render the title line the same way `subtitle` is already handled:
emit it only when the value is present, and skip it entirely when
missing. No layout change for documents that do have a title.
Test plan
`<story format="mawe" version="6">`,
flattens it, then runs RTF, HTML, TEX1, TEX2, TXT, MD through
`flattedFormat` with `assert.doesNotThrow`.
`formatTXT.js` change makes the new test fail with the exact
`title.toUpperCase()` TypeError described above.
`test_roundtrip`, and the new `test_export_empty` all pass).