Comparing version 1.0.0 to 1.1.0
11
index.js
var escape = module.exports = function escape(string) { | ||
return string.replace(/([&"<>'])/g, function(str, item) { | ||
var escape = module.exports = function escape(string, ignore) { | ||
var pattern; | ||
if (string === null || string === undefined) return; | ||
ignore = (ignore || '').replace(/[^&"<>\']/g, ''); | ||
pattern = '([&"<>\'])'.replace(new RegExp('[' + ignore + ']', 'g'), ''); | ||
return string.replace(new RegExp(pattern, 'g'), function(str, item) { | ||
return escape.map[item]; | ||
@@ -6,0 +13,0 @@ }) |
{ | ||
"name": "xml-escape", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Escape XML ", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,11 +6,34 @@ xml-escape | ||
npm install xml-escape | ||
```javascript | ||
// Warning escape is a reserved word, so maybe best to use xmlescape for var name | ||
var xmlescape = require('xml-escape'); | ||
xmlescape('"hello" \'world\' & false < true > -1') | ||
xmlescape('"hello" \'world\' & false < true > -1'); | ||
// output | ||
// '"hello" 'world' & true < false > -1' | ||
// '"hello" 'world' & false < true > -1' | ||
// don't escape some characters | ||
xmlescape('"hello" \'world\' & false < true > -1', '>"&') | ||
// output | ||
// '"hello" 'world' & false < true > -1' | ||
``` | ||
There is also now an ignore function thanks to @jayflo | ||
```javascript | ||
esc = require('./'); | ||
ignore = '"<&' | ||
// note you should never ignore an & | ||
output = esc('I am "<¬>" escaped', ignore) | ||
console.log(output) | ||
//I am "<¬>" escaped | ||
``` | ||
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/miketheprogrammer/xml-escape/trend.png)](https://bitdeli.com/free "Bitdeli Badge") |
24
test.js
@@ -7,2 +7,24 @@ var test = require('tape'); | ||
t.equals(escape('" \' < > &'), '" ' < > &'); | ||
}) | ||
}) | ||
test("Module should respect ignore string", function (t) { | ||
t.plan(3); | ||
t.equals(escape('" \' < > &', '"'), '" ' < > &'); | ||
t.equals(escape('" \' < > &', '>&'), '" ' < > &'); | ||
t.equals(escape('" \' < > &', '"\'<>&'), '" \' < > &'); | ||
}) | ||
test("Module should not escape random characters", function (t) { | ||
t.plan(1); | ||
t.equals(escape('<[whats up]>', '<]what'), '<[whats up]>'); | ||
}) | ||
test("Module should not crash on null or undefined input", function (t) { | ||
t.plan(3); | ||
t.equals((escape("")), ""); | ||
t.doesNotThrow(function(){escape(null);}, TypeError); | ||
t.doesNotThrow(function(){escape(undefined);}, TypeError); | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4132
37
39