Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Code based unicode symbols #14

Closed
piuccio opened this issue May 15, 2014 · 9 comments
Closed

Code based unicode symbols #14

piuccio opened this issue May 15, 2014 · 9 comments

Comments

@piuccio
Copy link

piuccio commented May 15, 2014

Do you think it's possible to extend multiline to support unicode characters?

In my tests I need to prepare multi line text including control characters, like this

console.log(multiline(function(){/*
\0
\u0000
*/}));

But the \ is escaped and displayed as a \, I'd like to insert the \0 control character in the string.

@sindresorhus
Copy link
Owner

That comes out as:

\0
\u0000

for me.

Can you submit a failing test or a fix?

@piuccio
Copy link
Author

piuccio commented May 15, 2014

Exactly, it comes as a string of two characters \ and 0.

What I'd like is to have one character, the control character 0.

You can try with \n or \t.

Maybe I'll have time to submit a test tomorrow

@sindresorhus
Copy link
Owner

Hmm, I guess i could decode the string with decodeURIComponent().

@mathiasbynens thoughts?

@mathiasbynens
Copy link

That’s not what decodeURIComponent does.

The easiest way to turn '\\0' into '\0' (and similar for any other possible type of escape sequence) is to use eval, sadly. Be careful not to introduce any security vulnerabilities along the way :( I do something like it here: https://github.com/mathiasbynens/mothereff.in/blob/1a74e1614c7b4293ac28572a007bd6aada3e267e/js-escapes/eff.js#L61-L70

You could try to do the unescaping statically with a bunch of .replace() calls but it’s tricky.

@mathiasbynens
Copy link

Maybe you could use https://github.com/espadrine/localeval.

@sindresorhus
Copy link
Owner

@mathiasbynens localeval looks extremely slow as it uses childprocess/worker.

Can you make a module based on https://github.com/mathiasbynens/mothereff.in/blob/1a74e1614c7b4293ac28572a007bd6aada3e267e/js-escapes/eff.js#L61-L70 that I can recommend?

@mathiasbynens
Copy link

IMHO this should be WONTFIXed. The main reason for using multiline is to not have to use escape sequences (for line terminators). If you want to use escape sequences in your strings, then don’t use multiline.

Fixing it would be confusing — people expecting some level of compatibility with template strings (as implied in the README) would then get incorrect results. See the first example on https://github.com/lukehoban/es6features#template-strings which the README links to:

// Basic literal string creation
`In JavaScript '\n' is a line-feed.`

That template string is equivalent to 'In JavaScript \'\\n\' is a line-feed.'. Fixing this issue would make multiline behave differently for no good reason AFAICT.

In OP’s case, just use '\0\n\u0000' or just '\0\n\0'. There’s a tool that makes this sort of thing easy for you: http://mothereff.in/js-escapes#1%5C0%0A%5Cu0000

@sindresorhus
Copy link
Owner

@mathiasbynens good point. i completely agree. thanks for chiming in :)

@piuccio
Copy link
Author

piuccio commented May 19, 2014

Thanks for the useful references

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants