Skip to content

preserve escape characters #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions transforms/angle-brackets/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ function transformAttrs(tagName, attrs) {
} else {
_value = b.text(a.value.original || _EMPTY_STRING_);
}

if (_value.chars && _value.chars.length > 0) {
_value.chars = JSON.stringify(_value.chars).slice(1, -1);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this. What are we stringifying something here?

Copy link
Collaborator Author

@tylerturdenpants tylerturdenpants Jan 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason i do this is because _value has the \ stripped within the string:
image

hence:"Choose a "thing"..."
JSON.stringify is great at escaping strings. So I use it to properly escape strings and remove the first and last double-quote resulting in Choose a \"thing\"...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't that a bug in the printer or template-recast?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn’t consider that. I guess I could check how other parsers handle such things. I’ll look into this a little more.

return b.attr(_key, _value);
});
}
Expand Down
14 changes: 14 additions & 0 deletions transforms/angle-brackets/transform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ test('boolean-values', () => {
`);
});

test('string-preserve-quotes', () => {
let input = '{{foo-bar placeholder="Choose a \\"thing\\"..."}}';
// eslint-disable-next-line prettier/prettier
expect(runTest('string-preserve-quotes.hbs', input)).toMatch('<FooBar @placeholder="Choose a \\"thing\\"..." />');
});

test('string-mixed-line-endings', () => {
let input = '{{foo-bar placeholder="Choose a... \\t\\r\\n"}}';

expect(runTest('string-mixed-line-endings.hbs', input)).toMatch(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Turbo87 I had to change to toMatch from toMatchInlineSnapshot because toMatchInlineSnapshot runs prettier not allowing me to express the string correctly for the test. I

'<FooBar @placeholder="Choose a... \\\\t\\\\r\\\\n" />'
);
});

test('curly', () => {
let input = `
<div>{{foo}}</div>
Expand Down