[Pkg-javascript-commits] [node-jsesc] 01/03: Imported Upstream version 1.2.0

Julien Puydt julien.puydt at laposte.net
Sat May 21 15:33:39 UTC 2016


This is an automated email from the git hooks/post-receive script.

jpuydt-guest pushed a commit to branch master
in repository node-jsesc.

commit 717bbd3ba45a5eeaee6f02cc7165b919252443f2
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Sat May 21 16:59:57 2016 +0200

    Imported Upstream version 1.2.0
---
 .travis.yml    |   5 +--
 README.md      |  17 ++++++--
 bin/jsesc      |   5 +++
 bower.json     |   2 +-
 component.json |   2 +-
 jsesc.js       |  27 ++++++++++++-
 man/jsesc.1    |   6 +--
 package.json   |   2 +-
 src/jsesc.js   |  23 +++++++++++
 tests/tests.js | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 10 files changed, 197 insertions(+), 15 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index 7b45eb0..3423994 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,9 +1,6 @@
 language: node_js
 node_js:
-  - "0.10"
-  - "0.12"
-  - "4"
-  - "5"
+  - "6"
 before_script:
   - "npm install -g grunt-cli"
   # Narwhal uses a hardcoded path to openjdk v6, so use that version
diff --git a/README.md b/README.md
index eb8be1a..d9c8f59 100644
--- a/README.md
+++ b/README.md
@@ -80,7 +80,7 @@ jsesc('foo 𝌆 bar');
 // → 'foo \\uD834\\uDF06 bar'
 ```
 
-Instead of a string, the `value` can also be an array, or an object. In such cases, `jsesc` will return a stringified version of the value where any characters that are not printable ASCII symbols are escaped in the same way.
+Instead of a string, the `value` can also be an array, an object, a map, or a set. In such cases, `jsesc` will return a stringified version of the value where any characters that are not printable ASCII symbols are escaped in the same way.
 
 ```js
 // Escaping an array
@@ -100,7 +100,7 @@ The optional `options` argument accepts an object with the following options:
 
 #### `quotes`
 
-The default value for the `quotes` option is `'single'`. This means that any occurences of `'` in the input string will be escaped as `\'`, so that the output can be used in a string literal wrapped in single quotes.
+The default value for the `quotes` option is `'single'`. This means that any occurrences of `'` in the input string will be escaped as `\'`, so that the output can be used in a string literal wrapped in single quotes.
 
 ```js
 jsesc('Lorem ipsum "dolor" sit \'amet\' etc.');
@@ -206,6 +206,17 @@ jsesc([ 'Ich ♥ Bücher': 'foo 𝌆 bar' ], {
 // → '[\'\x49\x63\x68\x20\u2665\x20\x42\xFC\x63\x68\x65\x72\',\'\x66\x6F\x6F\x20\uD834\uDF06\x20\x62\x61\x72\']'
 ```
 
+#### `escapeEtago`
+
+The `escapeEtago` option takes a boolean value (`true` or `false`), and defaults to `false` (disabled). When enabled, occurrences of [`</script` and `</style`](https://mathiasbynens.be/notes/etago) in the output are escaped as `<\/script` and `<\/style`. This setting is useful when jsesc’s output ends up as part of a `<script>` or `<style>` element in an HTML document.
+
+```js
+jsesc('foo</script>bar', {
+  'escapeEtago': true
+});
+// → 'foo<\\/script>bar'
+```
+
 #### `compact`
 
 The `compact` option takes a boolean value (`true` or `false`), and defaults to `true` (enabled). When enabled, the output for arrays and objects will be as compact as possible; it won’t be formatted nicely.
@@ -364,7 +375,7 @@ See `jsesc --help` for the full list of options.
 
 ## Support
 
-This library has been tested in at least Chrome 27, Firefox 3, Safari 4, Opera 10, IE 6, Node.js v0.10.0, io.js v1.0.0, Narwhal 0.3.2, RingoJS 0.8-0.11, PhantomJS 1.9.0, and Rhino 1.7RC4.
+This library has been tested in at least Chrome 27, Firefox 3, Safari 4, Opera 10, IE 6, Node.js v6.0.0, Narwhal 0.3.2, RingoJS 0.8-0.11, PhantomJS 1.9.0, and Rhino 1.7RC4.
 
 **Note:** Using the `json` option on objects or arrays that contain non-string values relies on `JSON.parse()`. For legacy environments like IE ≤ 7, use [a `JSON` polyfill](https://bestiejs.github.io/json3/).
 
diff --git a/bin/jsesc b/bin/jsesc
index d7bffd6..e9a541d 100755
--- a/bin/jsesc
+++ b/bin/jsesc
@@ -26,6 +26,7 @@
 				'\tjsesc [-d | --double-quotes] [string]',
 				'\tjsesc [-w | --wrap] [string]',
 				'\tjsesc [-e | --escape-everything] [string]',
+				'\tjsesc [-t | --escape-etago] [string]',
 				'\tjsesc [-6 | --es6] [string]',
 				'\tjsesc [-l | --lowercase-hex] [string]',
 				'\tjsesc [-j | --json] [string]',
@@ -66,6 +67,10 @@
 				options.escapeEverything = true;
 				return;
 			}
+			if (/^(?:-t|--escape-etago)$/.test(string)) {
+				options.escapeEtago = true;
+				return;
+			}
 			if (/^(?:-6|--es6)$/.test(string)) {
 				options.es6 = true;
 				return;
diff --git a/bower.json b/bower.json
index 16c88f6..f2ad633 100644
--- a/bower.json
+++ b/bower.json
@@ -1,6 +1,6 @@
 {
 	"name": "jsesc",
-	"version": "1.0.0",
+	"version": "1.2.0",
 	"main": "jsesc.js",
 	"ignore": [
 		"bin",
diff --git a/component.json b/component.json
index 9367afa..09591b8 100644
--- a/component.json
+++ b/component.json
@@ -1,6 +1,6 @@
 {
 	"name": "jsesc",
-	"version": "1.0.0",
+	"version": "1.2.0",
 	"description": "A JavaScript library for escaping JavaScript strings while generating the shortest possible ASCII-only output.",
 	"repo": "mathiasbynens/jsesc",
 	"license": "MIT",
diff --git a/jsesc.js b/jsesc.js
index da289b0..8827ce3 100644
--- a/jsesc.js
+++ b/jsesc.js
@@ -1,4 +1,4 @@
-/*! https://mths.be/jsesc v1.0.0 by @mathias */
+/*! https://mths.be/jsesc v1.2.0 by @mathias */
 ;(function(root) {
 
 	// Detect free variables `exports`
@@ -65,6 +65,12 @@
 		return typeof value == 'function' ||
 			toString.call(value) == '[object Function]';
 	};
+	var isMap = function(value) {
+		return toString.call(value) == '[object Map]';
+	};
+	var isSet = function(value) {
+		return toString.call(value) == '[object Set]';
+	};
 
 	/*--------------------------------------------------------------------------*/
 
@@ -90,6 +96,7 @@
 		// Handle options
 		var defaults = {
 			'escapeEverything': false,
+			'escapeEtago': false,
 			'quotes': 'single',
 			'wrap': false,
 			'es6': false,
@@ -121,6 +128,18 @@
 		}
 
 		if (!isString(argument)) {
+			if (isMap(argument)) {
+				if (argument.size == 0) {
+					return 'new Map()';
+				}
+				return 'new Map(' + jsesc(Array.from(argument), options) + ')';
+			}
+			if (isSet(argument)) {
+				if (argument.size == 0) {
+					return 'new Set()';
+				}
+				return 'new Set(' + jsesc(Array.from(argument), options) + ')';
+			}
 			if (isArray(argument)) {
 				result = [];
 				options.wrap = true;
@@ -243,10 +262,14 @@
 		if (options.wrap) {
 			result = quote + result + quote;
 		}
+		if (options.escapeEtago) {
+			// https://mathiasbynens.be/notes/etago
+			return result.replace(/<\/(script|style)/gi, '<\\/$1');
+		}
 		return result;
 	};
 
-	jsesc.version = '1.0.0';
+	jsesc.version = '1.2.0';
 
 	/*--------------------------------------------------------------------------*/
 
diff --git a/man/jsesc.1 b/man/jsesc.1
index 70da148..2655ee7 100644
--- a/man/jsesc.1
+++ b/man/jsesc.1
@@ -1,4 +1,4 @@
-.Dd October 25, 2013
+.Dd May 13, 2016
 .Dt jsesc 1
 .Sh NAME
 .Nm jsesc
@@ -32,9 +32,9 @@ escapes strings for use in JavaScript string literals while generating the short
 .Sh OPTIONS
 .Bl -ohang -offset
 .It Sy "-s, --single-quotes"
-Escape any occurences of ' in the input string as \\', so that the output can be used in a JavaScript string literal wrapped in single quotes.
+Escape any occurrences of ' in the input string as \\', so that the output can be used in a JavaScript string literal wrapped in single quotes.
 .It Sy "-d, --double-quotes"
-Escape any occurences of " in the input string as \\", so that the output can be used in a JavaScript string literal wrapped in double quotes.
+Escape any occurrences of " in the input string as \\", so that the output can be used in a JavaScript string literal wrapped in double quotes.
 .It Sy "-w, --wrap"
 Make sure the output is a valid JavaScript string literal wrapped in quotes. The type of quotes can be specified using the
 .Ar -s | --single-quotes
diff --git a/package.json b/package.json
index 9469fb1..193b82a 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
 	"name": "jsesc",
-	"version": "1.0.0",
+	"version": "1.2.0",
 	"description": "A JavaScript library for escaping JavaScript strings while generating the shortest possible valid output.",
 	"homepage": "https://mths.be/jsesc",
 	"main": "jsesc.js",
diff --git a/src/jsesc.js b/src/jsesc.js
index f8bed02..55b0518 100644
--- a/src/jsesc.js
+++ b/src/jsesc.js
@@ -65,6 +65,12 @@
 		return typeof value == 'function' ||
 			toString.call(value) == '[object Function]';
 	};
+	var isMap = function(value) {
+		return toString.call(value) == '[object Map]';
+	};
+	var isSet = function(value) {
+		return toString.call(value) == '[object Set]';
+	};
 
 	/*--------------------------------------------------------------------------*/
 
@@ -90,6 +96,7 @@
 		// Handle options
 		var defaults = {
 			'escapeEverything': false,
+			'escapeEtago': false,
 			'quotes': 'single',
 			'wrap': false,
 			'es6': false,
@@ -121,6 +128,18 @@
 		}
 
 		if (!isString(argument)) {
+			if (isMap(argument)) {
+				if (argument.size == 0) {
+					return 'new Map()';
+				}
+				return 'new Map(' + jsesc(Array.from(argument), options) + ')';
+			}
+			if (isSet(argument)) {
+				if (argument.size == 0) {
+					return 'new Set()';
+				}
+				return 'new Set(' + jsesc(Array.from(argument), options) + ')';
+			}
 			if (isArray(argument)) {
 				result = [];
 				options.wrap = true;
@@ -243,6 +262,10 @@
 		if (options.wrap) {
 			result = quote + result + quote;
 		}
+		if (options.escapeEtago) {
+			// https://mathiasbynens.be/notes/etago
+			return result.replace(/<\/(script|style)/gi, '<\\/$1');
+		}
 		return result;
 	};
 
diff --git a/tests/tests.js b/tests/tests.js
index 092bd7d..5f53a14 100644
--- a/tests/tests.js
+++ b/tests/tests.js
@@ -103,6 +103,20 @@
 			'escapeEverything'
 		);
 		equal(
+			jsesc('foo</script>bar</style>baz</script>qux', {
+				'escapeEtago': true
+			}),
+			'foo<\\/script>bar<\\/style>baz<\\/script>qux',
+			'escapeEtago'
+		);
+		equal(
+			jsesc('foo</sCrIpT>bar</STYLE>baz</SCRIPT>qux', {
+				'escapeEtago': true
+			}),
+			'foo<\\/sCrIpT>bar<\\/STYLE>baz<\\/SCRIPT>qux',
+			'escapeEtago'
+		);
+		equal(
 			jsesc('a\uD834\uDF06b', {
 				'es6': true
 			}),
@@ -202,6 +216,79 @@
 			'[\n\t\'foo\\0bar\\uFFFDbaz\',\n\t\'\\xA9\'\n]',
 			'Stringifying a flat array with `compact: false`'
 		);
+		// Maps
+		if (typeof Map != 'undefined') {
+			equal(
+				jsesc(
+					new Map([])
+				),
+				'new Map()',
+				'Stringifying an empty Map'
+			);
+			equal(
+				jsesc(
+					new Map([
+						['a', 1],
+						['b', 2]
+					]),
+					{
+						'compact': true
+					}
+				),
+				'new Map([[\'a\',1],[\'b\',2]])',
+				'Stringifying a Map with `compact: true`'
+			);
+			equal(
+				jsesc(
+					new Map([
+						['a', 1],
+						['b', 2]
+					]),
+					{
+						'compact': false
+					}
+				),
+				'new Map([\n\t[\n\t\t\'a\',\n\t\t1\n\t],\n\t[\n\t\t\'b\',\n\t\t2\n\t]\n])',
+				'Stringifying a Map with `compact: false`'
+			);
+		}
+		if (typeof Set != 'undefined') {
+			equal(
+				jsesc(
+					new Set([])
+				),
+				'new Set()',
+				'Stringifying an empty Set'
+			);
+			equal(
+				jsesc(
+					new Set([
+						['a'],
+						'b',
+						{}
+					]),
+					{
+						'compact': true
+					}
+				),
+				'new Set([[\'a\'],\'b\',{}])',
+				'Stringifying a Set with `compact: true`'
+			);
+			equal(
+				jsesc(
+					new Set([
+						['a'],
+						'b',
+						{}
+					]),
+					{
+						'compact': false
+					}
+				),
+				'new Set([\n\t[\n\t\t\'a\'\n\t],\n\t\'b\',\n\t{}\n])',
+				'Stringifying a Set with `compact: false`'
+			);
+		}
 		// JSON
 		equal(
 			jsesc('foo\x00bar\xFF\uFFFDbaz', {
@@ -708,6 +795,42 @@
 					}
 				},
 				{
+					'description': '-t option',
+					'command': './bin/jsesc -t "foo</script>bar"',
+					'expected': {
+						'stdout': 'foo<\\/script>bar\n',
+						'stderr': '',
+						'exitStatus': 0
+					}
+				},
+				{
+					'description': '-t option, piping content',
+					'command': 'echo "foo</script>bar" | ./bin/jsesc -t',
+					'expected': {
+						'stdout': 'foo<\\/script>bar\n',
+						'stderr': '',
+						'exitStatus': 0
+					}
+				},
+				{
+					'description': '--escape-etago option',
+					'command': './bin/jsesc --escape-etago "foo</script>bar"',
+					'expected': {
+						'stdout': 'foo<\\/script>bar\n',
+						'stderr': '',
+						'exitStatus': 0
+					}
+				},
+				{
+					'description': '--escape-etago option, piping content',
+					'command': 'echo "foo</script>bar" | ./bin/jsesc --escape-etago',
+					'expected': {
+						'stdout': 'foo<\\/script>bar\n',
+						'stderr': '',
+						'exitStatus': 0
+					}
+				},
+				{
 					'description': '-l option',
 					'command': './bin/jsesc -l a\uD834\uDF06b\uD83D\uDCA9c',
 					'expected': {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-jsesc.git



More information about the Pkg-javascript-commits mailing list