Skip to content

Preserve attribute source order in output#113

Merged
dereuromark merged 1 commit intomasterfrom
fix/attribute-source-order-v2
Mar 23, 2026
Merged

Preserve attribute source order in output#113
dereuromark merged 1 commit intomasterfrom
fix/attribute-source-order-v2

Conversation

@dereuromark
Copy link
Copy Markdown
Contributor

Summary

  • Fixed attribute ordering to match source order (per djot spec and JS reference)
  • {#id .class} now correctly outputs id="id" class="class" instead of class="class" id="id"

Root Cause

Two issues were causing attributes to be output in wrong order:

  1. AttributeParser::parse() processed attributes in fixed order (class → id → key=value → boolean) instead of source order
  2. HtmlRenderer::renderAttributes() explicitly moved id to the front

Changes

  • Added parseOrdered() method using single-pass regex (same approach as applyToNode())
  • Updated parseAndMerge() and BlockParser to use parseOrdered()
  • Removed "id first" logic from HtmlRenderer

Test

All existing tests pass, including the official test suite which already expected source order.

Per djot spec and JS reference implementation, attributes should be
rendered in the order they appear in the source. For example,
`{#id .class}` should output `id="id" class="class"`.

Changes:
- Added `parseOrdered()` method to AttributeParser that uses single-pass
  regex matching to preserve source order (same approach as applyToNode)
- Updated `parseAndMerge()` to use `parseOrdered()` instead of `parse()`
- Updated BlockParser list item attributes to use `parseOrdered()`
- Removed "id first" logic from HtmlRenderer that was overriding order

The old `parse()` method is kept for backwards compatibility but is no
longer used internally.
@dereuromark dereuromark merged commit bc80fc8 into master Mar 23, 2026
4 checks passed
@dereuromark dereuromark deleted the fix/attribute-source-order-v2 branch March 23, 2026 07:42
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 23, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.75%. Comparing base (dc18eed) to head (5d10bf6).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #113      +/-   ##
============================================
+ Coverage     93.68%   93.75%   +0.07%     
- Complexity     2333     2340       +7     
============================================
  Files            79       79              
  Lines          6171     6198      +27     
============================================
+ Hits           5781     5811      +30     
+ Misses          390      387       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

1 participant