Gathering detailed insights and metrics for cssesc
Gathering detailed insights and metrics for cssesc
Gathering detailed insights and metrics for cssesc
Gathering detailed insights and metrics for cssesc
A JavaScript library for escaping CSS strings and identifiers while generating the shortest possible ASCII-only output.
npm install cssesc
99.7
Supply Chain
100
Quality
75.2
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
167 Stars
28 Commits
16 Forks
4 Watching
2 Branches
5 Contributors
Updated on 25 Oct 2024
Minified
Minified + Gzipped
HTML (76.26%)
JavaScript (23.74%)
Cumulative downloads
Total Downloads
Last day
-5.4%
4,985,846
Compared to previous day
Last week
1.6%
28,631,280
Compared to previous week
Last month
9%
120,447,523
Compared to previous month
Last year
5.9%
1,251,251,176
Compared to previous year
A JavaScript library for escaping CSS strings and identifiers while generating the shortest possible ASCII-only output.
This is a JavaScript library for escaping text for use in CSS strings or identifiers while generating the shortest possible valid ASCII-only output. Here’s an online demo.
A polyfill for the CSSOM CSS.escape()
method is available in a separate repository. (In comparison, cssesc is much more powerful.)
Feel free to fork if you see possible improvements!
Via npm:
1npm install cssesc
In a browser:
1<script src="cssesc.js"></script>
In Node.js:
1const cssesc = require('cssesc');
In Ruby using the ruby-cssesc
wrapper gem:
1gem install ruby-cssesc
1require 'ruby-cssesc' 2CSSEsc.escape('I ♥ Ruby', is_identifier: true)
In Sass using sassy-escape
:
1gem install sassy-escape
1body { 2 content: escape('I ♥ Sass', $is-identifier: true); 3}
cssesc(value, options)
This function takes a value and returns an escaped version of the value where any characters that are not printable ASCII symbols are escaped using the shortest possible (but valid) escape sequences for use in CSS strings or identifiers.
1cssesc('Ich ♥ Bücher'); 2// → 'Ich \\2665 B\\FC cher' 3 4cssesc('foo 𝌆 bar'); 5// → 'foo \\1D306 bar'
By default, cssesc
returns a string that can be used as part of a CSS string. If the target is a CSS identifier rather than a CSS string, use the isIdentifier: true
setting (see below).
The optional options
argument accepts an object with the following options:
isIdentifier
The default value for the isIdentifier
option is false
. This means that the input text will be escaped for use in a CSS string literal. If you want to use the result as a CSS identifier instead (in a selector, for example), set this option to true
.
1cssesc('123a2b'); 2// → '123a2b' 3 4cssesc('123a2b', { 5 'isIdentifier': true 6}); 7// → '\\31 23a2b'
quotes
The default value for the quotes
option is 'single'
. This means that any occurences of '
in the input text will be escaped as \'
, so that the output can be used in a CSS string literal wrapped in single quotes.
1cssesc('Lorem ipsum "dolor" sit \'amet\' etc.'); 2// → 'Lorem ipsum "dolor" sit \\\'amet\\\' etc.' 3// → "Lorem ipsum \"dolor\" sit \\'amet\\' etc." 4 5cssesc('Lorem ipsum "dolor" sit \'amet\' etc.', { 6 'quotes': 'single' 7}); 8// → 'Lorem ipsum "dolor" sit \\\'amet\\\' etc.' 9// → "Lorem ipsum \"dolor\" sit \\'amet\\' etc."
If you want to use the output as part of a CSS string literal wrapped in double quotes, set the quotes
option to 'double'
.
1cssesc('Lorem ipsum "dolor" sit \'amet\' etc.', { 2 'quotes': 'double' 3}); 4// → 'Lorem ipsum \\"dolor\\" sit \'amet\' etc.' 5// → "Lorem ipsum \\\"dolor\\\" sit 'amet' etc."
wrap
The wrap
option takes a boolean value (true
or false
), and defaults to false
(disabled). When enabled, the output will be a valid CSS string literal wrapped in quotes. The type of quotes can be specified through the quotes
setting.
1cssesc('Lorem ipsum "dolor" sit \'amet\' etc.', { 2 'quotes': 'single', 3 'wrap': true 4}); 5// → '\'Lorem ipsum "dolor" sit \\\'amet\\\' etc.\'' 6// → "\'Lorem ipsum \"dolor\" sit \\\'amet\\\' etc.\'" 7 8cssesc('Lorem ipsum "dolor" sit \'amet\' etc.', { 9 'quotes': 'double', 10 'wrap': true 11}); 12// → '"Lorem ipsum \\"dolor\\" sit \'amet\' etc."' 13// → "\"Lorem ipsum \\\"dolor\\\" sit \'amet\' etc.\""
escapeEverything
The escapeEverything
option takes a boolean value (true
or false
), and defaults to false
(disabled). When enabled, all the symbols in the output will be escaped, even printable ASCII symbols.
1cssesc('lolwat"foo\'bar', { 2 'escapeEverything': true 3}); 4// → '\\6C\\6F\\6C\\77\\61\\74\\"\\66\\6F\\6F\\\'\\62\\61\\72' 5// → "\\6C\\6F\\6C\\77\\61\\74\\\"\\66\\6F\\6F\\'\\62\\61\\72"
The global default settings can be overridden by modifying the css.options
object. This saves you from passing in an options
object for every call to encode
if you want to use the non-default setting.
1// Read the global default setting for `escapeEverything`: 2cssesc.options.escapeEverything; 3// → `false` by default 4 5// Override the global default setting for `escapeEverything`: 6cssesc.options.escapeEverything = true; 7 8// Using the global default setting for `escapeEverything`, which is now `true`: 9cssesc('foo © bar ≠ baz 𝌆 qux'); 10// → '\\66\\6F\\6F\\ \\A9\\ \\62\\61\\72\\ \\2260\\ \\62\\61\\7A\\ \\1D306\\ \\71\\75\\78'
cssesc.version
A string representing the semantic version number.
cssesc
binaryTo use the cssesc
binary in your shell, simply install cssesc globally using npm:
1npm install -g cssesc
After that you will be able to escape text for use in CSS strings or identifiers from the command line:
1$ cssesc 'föo ♥ bår 𝌆 baz' 2f\F6o \2665 b\E5r \1D306 baz
If the output needs to be a CSS identifier rather than part of a string literal, use the -i
/--identifier
option:
1$ cssesc --identifier 'föo ♥ bår 𝌆 baz' 2f\F6o\ \2665\ b\E5r\ \1D306\ baz
See cssesc --help
for the full list of options.
This library supports the Node.js and browser versions mentioned in .babelrc
. For a version that supports a wider variety of legacy browsers and environments out-of-the-box, see v0.1.0.
Mathias Bynens |
This library is available under the MIT license.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
Found 1/28 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More