Skip to content

fix(export): preserve attributes before an empty style attribute (#682)#709

Closed
tongshu2023 wants to merge 2 commits into
THU-MAIC:mainfrom
tongshu2023:fix/empty-style-attr-682
Closed

fix(export): preserve attributes before an empty style attribute (#682)#709
tongshu2023 wants to merge 2 commits into
THU-MAIC:mainfrom
tongshu2023:fix/empty-style-attr-682

Conversation

@tongshu2023

Copy link
Copy Markdown
Contributor

Bug

formatAttributes in lib/export/html-parser/stringify.ts drops every attribute accumulated before an empty style attribute, because the empty-style branch returns '' — which becomes the new reduce accumulator — instead of the accumulated string. Closes #682.

if (key === 'style' && !value) return '';   // resets the accumulator

For an element like { id: 'box', style: '' }, serialization produced <div> instead of <div id='box'> — the id (and any other earlier attribute) silently disappears from exported HTML.

Fix

Return the accumulator unchanged so the empty style is skipped without discarding prior attributes:

if (key === 'style' && !value) return attrs;

Tests

Adds tests/export/stringify.test.ts (none existed for this module):

  • formatAttributes: value attribute quoting, null → boolean attribute, single→double quote switch when the value contains ', empty style omitted, regression: attributes before an empty style are kept, non-empty style preserved
  • toHTML: element whose first attribute precedes an empty style (regression), void-tag serialization, text/comment passthrough

Verification

$ npx vitest run tests/export/stringify.test.ts
 Test Files  1 passed (1)
      Tests  9 passed (9)

prettier --check, eslint, and tsc --noEmit all clean.

@tongshu2023

Copy link
Copy Markdown
Contributor Author

Apologies — I missed that #683 (opened earlier by the team) already covers this. Closing in favor of that PR. The tests added here are free to cherry-pick if useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

formatAttributes drops earlier attributes when style is empty

1 participant