Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

xml-escape

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xml-escape - npm Package Compare versions

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 @@ })

2

package.json
{
"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
// '&quot;hello&quot; &apos;world&apos; &amp; true &lt; false &gt; -1'
// '&quot;hello&quot; &apos;world&apos; &amp; false &lt; true &gt; -1'
// don't escape some characters
xmlescape('"hello" \'world\' & false < true > -1', '>"&')
// output
// '"hello" &apos;world&apos; & false &lt; 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 "<&not>" escaped', ignore)
console.log(output)
//I am "<&not&gt;" escaped
```
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/miketheprogrammer/xml-escape/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

@@ -7,2 +7,24 @@ var test = require('tape');

t.equals(escape('" \' < > &'), '&quot; &apos; &lt; &gt; &amp;');
})
})
test("Module should respect ignore string", function (t) {
t.plan(3);
t.equals(escape('" \' < > &', '"'), '" &apos; &lt; &gt; &amp;');
t.equals(escape('" \' < > &', '>&'), '&quot; &apos; &lt; > &');
t.equals(escape('" \' < > &', '"\'<>&'), '" \' < > &');
})
test("Module should not escape random characters", function (t) {
t.plan(1);
t.equals(escape('<[whats up]>', '<]what'), '<[whats up]&gt;');
})
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);
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc