fix: preserve negative zero sign when stringifying values - #22
Open
rajanpanth wants to merge 1 commit into
Open
Conversation
Every raw-value-to-string conversion in the builder (String()/toString()/ implicit ToString via concatenation) silently drops the sign of -0, corrupting it to 0 for both text and attribute values, in both preserveOrder modes, and in stopNode raw-content paths. XML has no separate int/float syntax, so this loss is silent and easy to miss on a parse -> build round trip. Add a single shared valToStr() helper that special-cases Object.is(val, -0) and use it at every such conversion site instead of the ad hoc String(val)/''+val/val.toString() calls.
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
Every raw-value-to-string conversion in the builder silently drops the sign of
-0, corrupting it to0:This affects text values, attribute values,
oneListGrouparray items, and stopNode raw content — in bothpreserveOrdermodes. XML has no separate int/float syntax, so the sign carries the only signal that a value was a negative float rather than0, and it's silently lost on a build round trip (e.g.parser.parse(builder.build(x))no longer matchesxfor any-0value).Root cause:
String(val),val.toString(), and'' + valall render JS-0as"0"— none of them special-case it — and the builder does this raw-to-string conversion at many separate call sites acrossfxb.js,orderedJs2Xml.js, andutil.js.Changes
src/util.js: add a small sharedvalToStr()helper that special-casesObject.is(val, -0), and use it insidesafeComment/safeCdata/escapeAttributeinstead of their bareString(val).src/fxb.js/src/orderedJs2Xml.js: replace every ad hoc'' + val/val.toString()/ implicit-ToString raw-value stringification withvalToStr(), covering: attribute values, text values,oneListGrouparray items, and stopNode raw content, in bothpreserveOrder: falseandpreserveOrder: true.spec/j2x_spec.js,spec/j2x_ordered_spec.js, andspec/stopNodes_spec.js(7 new cases total).Testing
npm run lintreports the same pre-existing errors with and without this change (unrelated files/lines; confirmed viagit stash) — this PR introduces no new lint issues.