fix: preserve consecutive backslashes in attribute values#309
Merged
Conversation
quoteAttribute serialized attribute values with JSON.stringify (which doubles every backslash) and then tried to undo the doubling with `.replace(/([^\\])\\/g, '$1')`. That regex needs a non-backslash before each backslash and matches non-overlapping, so it cannot collapse runs of consecutive backslashes: a value such as "C:\\Users\\me" gained an extra backslash on every serialization and no longer round-tripped through setAttribute -> toString -> parse. (The single-backslash case fixed in issue taoqf#306 worked; consecutive backslashes were the unhandled residual.) Attribute values are literal text, so only the double quote needs escaping (as "). Quote the value directly, which preserves any number of backslashes and still escapes embedded quotes (issue taoqf#62 behaviour).
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.
Problem
quoteAttributeserializes an attribute value withJSON.stringify(which doubles every backslash) and then tries to undo the doubling with.replace(/([^\\])\\/g, '$1'). That regex requires a non-backslash before each backslash and matches non-overlapping, so it cannot collapse a run of consecutive backslashes. The value gains an extra backslash on every serialization and no longer round-trips:The single-backslash case fixed in #306 works; consecutive backslashes (e.g. a JSON-escaped Windows path or a regex stored in an attribute) were the unhandled residual.
Fix
Attribute values are literal text, so only the double quote needs escaping (as
"). Quote the value directly instead of round-tripping it throughJSON.stringify. This preserves any number of backslashes and still escapes embedded quotes (the #62 behaviour).Test
Added a round-trip assertion for an attribute value with consecutive backslashes. It fails before the change and passes after; the full suite (263 passing) and lint are green.