escape-html-template-tag
Advanced tools
Comparing version 1.0.1 to 1.1.0
20
index.js
@@ -14,2 +14,16 @@ const ENTITIES = { | ||
function join (array, separator) { | ||
if (separator === undefined || separator === null) { | ||
separator = ',' | ||
} | ||
if (array.length <= 0) { | ||
return new HtmlSafeString([ '' ], []) | ||
} | ||
return new HtmlSafeString([ '', ...Array(array.length - 1).fill(separator), '' ], array) | ||
} | ||
function safe (value) { | ||
return new HtmlSafeString([String(value)], []) | ||
} | ||
class HtmlSafeString { | ||
@@ -26,3 +40,3 @@ constructor (parts, subs) { | ||
if (Array.isArray(unsafe)) { | ||
return new HtmlSafeString(Array(unsafe.length + 1).fill(''), unsafe) | ||
return join(unsafe, '') | ||
} | ||
@@ -48,6 +62,4 @@ return String(unsafe).replace(ENT_REGEX, char => ENTITIES[char]) | ||
escapeHtml.safe = function (value) { | ||
return new HtmlSafeString([String(value)], []) | ||
} | ||
Object.assign(escapeHtml, { safe, join }) | ||
module.exports = escapeHtml |
{ | ||
"name": "escape-html-template-tag", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Tag literal strings with this function to html escape interpolated values", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -59,5 +59,5 @@ # escape-html-template-tag | ||
const navigation = escapeHtml` | ||
<div> | ||
${escapeHtml.safe(trustedString)} | ||
</div> | ||
<div> | ||
${escapeHtml.safe(trustedString)} | ||
</div> | ||
` | ||
@@ -69,2 +69,18 @@ // <div> | ||
### Join fragments together with `escapeHtml.join` | ||
```js | ||
const navigation = escapeHtml` | ||
<div> | ||
${escapeHtml.join( | ||
['home', 'about', 'blog'].map(page => escapeHtml`<a href="/${page}">${page}</div>a>`), | ||
' | ' | ||
)} | ||
</div> | ||
` | ||
// <div> | ||
// <a href="/home">home</div>a> | <a href="/about">about</div>a> | <a href="/blog">blog</div>a> | ||
// </div> | ||
``` | ||
### Compose templates easily with functions | ||
@@ -93,2 +109,2 @@ | ||
// </ul> | ||
``` | ||
``` |
51
test.js
const assert = require('assert') | ||
const escapeHtml = require('.') | ||
const H = require('.') | ||
assert.strictEqual(String(escapeHtml`<a>`), '<a>') | ||
assert.strictEqual(String(escapeHtml`${'<a>'}`), '<a>') | ||
assert.strictEqual(String(escapeHtml`${1}`), '1') | ||
assert.strictEqual(String(H`<a>`), '<a>') | ||
assert.strictEqual(String(H`${'<a>'}`), '<a>') | ||
assert.strictEqual(String(H`${1}`), '1') | ||
// nesting | ||
assert.strictEqual(String(escapeHtml`${escapeHtml`<a>`}`), '<a>') | ||
assert.strictEqual(String(escapeHtml`${escapeHtml`${'<a>'}`}`), '<a>') | ||
assert.strictEqual(String(H`${H`<a>`}`), '<a>') | ||
assert.strictEqual(String(H`${H`${'<a>'}`}`), '<a>') | ||
// escape characters | ||
assert.strictEqual(String(escapeHtml`${'&'}`), '&') | ||
assert.strictEqual(String(escapeHtml`${'<'}`), '<') | ||
assert.strictEqual(String(escapeHtml`${'>'}`), '>') | ||
assert.strictEqual(String(escapeHtml`${'"'}`), '"') | ||
assert.strictEqual(String(escapeHtml`${"'"}`), ''') | ||
assert.strictEqual(String(escapeHtml`${'/'}`), '/') | ||
assert.strictEqual(String(escapeHtml`${'`'}`), '`') | ||
assert.strictEqual(String(escapeHtml`${'='}`), '=') | ||
assert.strictEqual(String(H`${'&'}`), '&') | ||
assert.strictEqual(String(H`${'<'}`), '<') | ||
assert.strictEqual(String(H`${'>'}`), '>') | ||
assert.strictEqual(String(H`${'"'}`), '"') | ||
assert.strictEqual(String(H`${"'"}`), ''') | ||
assert.strictEqual(String(H`${'/'}`), '/') | ||
assert.strictEqual(String(H`${'`'}`), '`') | ||
assert.strictEqual(String(H`${'='}`), '=') | ||
// flattening arrays | ||
assert.strictEqual(String(escapeHtml`${['a', 1, '<']}`), 'a1<') | ||
assert.strictEqual(String(escapeHtml`${['a', escapeHtml`<`]}`), 'a<') | ||
assert.strictEqual(String(escapeHtml`${[]}`), '') | ||
assert.strictEqual(String(H`${['a', 1, '<']}`), 'a1<') | ||
assert.strictEqual(String(H`${['a', H`<`]}`), 'a<') | ||
assert.strictEqual(String(H`${[]}`), '') | ||
// escapeHtml.safe | ||
assert.strictEqual(String(escapeHtml`${escapeHtml.safe('<a>')}`), '<a>') | ||
assert.strictEqual(String(escapeHtml`${escapeHtml.safe(1)}`), '1') | ||
assert.strictEqual(String(escapeHtml`${escapeHtml.safe([1, 2, 3])}`), '1,2,3') | ||
assert.strictEqual(String(escapeHtml`${escapeHtml.safe(escapeHtml`<a>`)}`), '<a>') | ||
// H.safe | ||
assert.strictEqual(String(H`${H.safe('<a>')}`), '<a>') | ||
assert.strictEqual(String(H`${H.safe(1)}`), '1') | ||
assert.strictEqual(String(H`${H.safe([1, 2, 3])}`), '1,2,3') | ||
assert.strictEqual(String(H`${H.safe(H`<a>`)}`), '<a>') | ||
// .join | ||
assert.strictEqual(String(H.join([H`a`, H`b`, H`c`], ' ')), 'a b c') | ||
assert.strictEqual(String(H.join([H`a`, H`b`, H`c`])), 'a,b,c') | ||
assert.strictEqual(String(H.join([H`a`, H`b`, H`c`], null)), 'a,b,c') | ||
assert.strictEqual(String(H.join([H`<`, H`>`], H`${'&'}`)), '<&>') | ||
assert.strictEqual(String(H.join([H`${'<'}`, H`${'>'}`], H`&`)), '<&>') |
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
6015
84
108