cssesc icon indicating copy to clipboard operation
cssesc copied to clipboard

cssesc always escapes identifier characters above U+00A0

Open Jessidhia opened this issue 8 years ago • 4 comments

As is not demonstrated by the online demo at https://mothereff.in/css-escapes.

If a @charset "utf-8"; directive is present in the input, or an appropriate option is set in the options object, it would be nice to be able to preserve characters that don't need to be escaped.

The CSS 2.1 spec allows identifiers to contain characters above U+00A0, and at least CSS 2.0 allowed characters "161 and higher".

Jessidhia avatar Jul 06 '16 10:07 Jessidhia

This suggestion goes against the goal of this library:

A JavaScript library for escaping CSS strings and identifiers while generating the shortest possible ASCII-only output.

To only escape what’s absolutely necessary for modern browsers, you could use CSS.escape().

That said, cssesc could have an option to opt-in to similar behavior. If someone submits a patch, I’ll consider it.

mathiasbynens avatar Jul 06 '16 11:07 mathiasbynens

Indeed, including high characters should be an off-by-default option if provided at all. As for css.escape, there is unfortunately no way to use it without it writing to global :(

Jessidhia avatar Jul 06 '16 12:07 Jessidhia

I would like to have this option and will make a PR for it. The polyfil does not optimize string escapes, and I feel this library is a better place to have both capabilities so that a single dependency can accommodate libraries that need to handle a range of browser support preferences from users.

Do you have any preference for the option name (and corresponding CLI switch)? I suggest one of the following (in order of my most preferred to least preferred):

  • preserveUnicode (defaults to false)
  • onlySpecialChars (defaults to false)
  • unicodeOutput (defaults to false)
  • asciiOutput (defaults to true)

chriseppstein avatar Mar 26 '18 18:03 chriseppstein

I'd prefer the name minimal, to match jsesc: https://github.com/mathiasbynens/jsesc#minimal

mathiasbynens avatar Mar 27 '18 05:03 mathiasbynens