cssesc
Advanced tools
Comparing version 1.0.1 to 2.0.0
/*! https://mths.be/cssesc v1.0.1 by @mathias */ | ||
'use strict'; | ||
const object = {}; | ||
const hasOwnProperty = object.hasOwnProperty; | ||
const merge = (options, defaults) => { | ||
var object = {}; | ||
var hasOwnProperty = object.hasOwnProperty; | ||
var merge = function merge(options, defaults) { | ||
if (!options) { | ||
return defaults; | ||
} | ||
const result = {}; | ||
for (let key in defaults) { | ||
var result = {}; | ||
for (var key in defaults) { | ||
// `if (defaults.hasOwnProperty(key) { … }` is not needed here, since | ||
// only recognized option names are used. | ||
result[key] = hasOwnProperty.call(options, key) | ||
? options[key] | ||
: defaults[key]; | ||
result[key] = hasOwnProperty.call(options, key) ? options[key] : defaults[key]; | ||
} | ||
@@ -21,9 +19,9 @@ return result; | ||
const regexAnySingleEscape = /[ -,\.\/;-@\[-\^`\{-~]/; | ||
const regexSingleEscape = /[ -,\.\/;-@\[\]\^`\{-~]/; | ||
const regexAlwaysEscape = /['"\\]/; | ||
const regexExcessiveSpaces = /(^|\\+)?(\\[A-F0-9]{1,6})\x20(?![a-fA-F0-9\x20])/g; | ||
var regexAnySingleEscape = /[ -,\.\/;-@\[-\^`\{-~]/; | ||
var regexSingleEscape = /[ -,\.\/;-@\[\]\^`\{-~]/; | ||
var regexAlwaysEscape = /['"\\]/; | ||
var regexExcessiveSpaces = /(^|\\+)?(\\[A-F0-9]{1,6})\x20(?![a-fA-F0-9\x20])/g; | ||
// https://mathiasbynens.be/notes/css-escapes#css | ||
const cssesc = (string, options) => { | ||
var cssesc = function cssesc(string, options) { | ||
options = merge(options, cssesc.options); | ||
@@ -33,13 +31,13 @@ if (options.quotes != 'single' && options.quotes != 'double') { | ||
} | ||
const quote = options.quotes == 'double' ? '"' : '\''; | ||
const isIdentifier = options.isIdentifier; | ||
var quote = options.quotes == 'double' ? '"' : '\''; | ||
var isIdentifier = options.isIdentifier; | ||
const firstChar = string.charAt(0); | ||
let output = ''; | ||
let counter = 0; | ||
const length = string.length; | ||
var firstChar = string.charAt(0); | ||
var output = ''; | ||
var counter = 0; | ||
var length = string.length; | ||
while (counter < length) { | ||
const character = string.charAt(counter++); | ||
let codePoint = character.charCodeAt(); | ||
let value; | ||
var character = string.charAt(counter++); | ||
var codePoint = character.charCodeAt(); | ||
var value = void 0; | ||
// If it’s not a printable ASCII character… | ||
@@ -49,4 +47,5 @@ if (codePoint < 0x20 || codePoint > 0x7E) { | ||
// It’s a high surrogate, and there is a next character. | ||
const extra = string.charCodeAt(counter++); | ||
if ((extra & 0xFC00) == 0xDC00) { // next character is low surrogate | ||
var extra = string.charCodeAt(counter++); | ||
if ((extra & 0xFC00) == 0xDC00) { | ||
// next character is low surrogate | ||
codePoint = ((codePoint & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000; | ||
@@ -67,3 +66,3 @@ } else { | ||
} | ||
// Note: `:` could be escaped as `\:`, but that fails in IE < 8. | ||
// Note: `:` could be escaped as `\:`, but that fails in IE < 8. | ||
} else if (/[\t\n\f\r\x0B:]/.test(character)) { | ||
@@ -75,13 +74,3 @@ if (!isIdentifier && character == ':') { | ||
} | ||
} else if ( | ||
character == '\\' || | ||
( | ||
!isIdentifier && | ||
( | ||
(character == '"' && quote == character) || | ||
(character == '\'' && quote == character) | ||
) | ||
) || | ||
(isIdentifier && regexSingleEscape.test(character)) | ||
) { | ||
} else if (character == '\\' || !isIdentifier && (character == '"' && quote == character || character == '\'' && quote == character) || isIdentifier && regexSingleEscape.test(character)) { | ||
value = '\\' + character; | ||
@@ -110,3 +99,3 @@ } else { | ||
// sequence isn’t preceded by an odd number of backslashes. | ||
output = output.replace(regexExcessiveSpaces, function($0, $1, $2) { | ||
output = output.replace(regexExcessiveSpaces, function ($0, $1, $2) { | ||
if ($1 && $1.length % 2) { | ||
@@ -113,0 +102,0 @@ // It’s not safe to remove the space, so don’t. |
{ | ||
"name": "cssesc", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "A JavaScript library for escaping CSS strings and identifiers while generating the shortest possible ASCII-only output.", | ||
@@ -36,3 +36,3 @@ "homepage": "https://mths.be/cssesc", | ||
"scripts": { | ||
"build": "grunt template", | ||
"build": "grunt template && babel cssesc.js -o cssesc.js", | ||
"test": "mocha tests", | ||
@@ -42,2 +42,4 @@ "cover": "istanbul cover --report html node_modules/.bin/_mocha tests -- -u exports -R spec" | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-preset-env": "^1.6.1", | ||
"codecov": "^1.0.1", | ||
@@ -44,0 +46,0 @@ "grunt": "^1.0.1", |
@@ -191,3 +191,3 @@ # cssesc [![Build status](https://travis-ci.org/mathiasbynens/cssesc.svg?branch=master)](https://travis-ci.org/mathiasbynens/cssesc) [![Code coverage status](https://img.shields.io/codecov/c/github/mathiasbynens/cssesc.svg)](https://codecov.io/gh/mathiasbynens/cssesc) | ||
This library supports Node.js v4+ only. For a version that supports browser and environments out-of-the-box, [see v0.1.0](https://github.com/mathiasbynens/cssesc/releases/tag/v0.1.0). | ||
This library supports the Node.js and browser versions mentioned in [`.babelrc`](https://github.com/mathiasbynens/cssesc/blob/master/.babelrc). For a version that supports a wider variety of legacy browsers and environments out-of-the-box, [see v0.1.0](https://github.com/mathiasbynens/cssesc/releases/tag/v0.1.0). | ||
@@ -194,0 +194,0 @@ ## Author |
17831
9
109