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

escape-html-template-tag

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

escape-html-template-tag - npm Package Compare versions

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

2

package.json
{
"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>
```
```
const assert = require('assert')
const escapeHtml = require('.')
const H = require('.')
assert.strictEqual(String(escapeHtml`<a>`), '<a>')
assert.strictEqual(String(escapeHtml`${'<a>'}`), '&lt;a&gt;')
assert.strictEqual(String(escapeHtml`${1}`), '1')
assert.strictEqual(String(H`<a>`), '<a>')
assert.strictEqual(String(H`${'<a>'}`), '&lt;a&gt;')
assert.strictEqual(String(H`${1}`), '1')
// nesting
assert.strictEqual(String(escapeHtml`${escapeHtml`<a>`}`), '<a>')
assert.strictEqual(String(escapeHtml`${escapeHtml`${'<a>'}`}`), '&lt;a&gt;')
assert.strictEqual(String(H`${H`<a>`}`), '<a>')
assert.strictEqual(String(H`${H`${'<a>'}`}`), '&lt;a&gt;')
// escape characters
assert.strictEqual(String(escapeHtml`${'&'}`), '&amp;')
assert.strictEqual(String(escapeHtml`${'<'}`), '&lt;')
assert.strictEqual(String(escapeHtml`${'>'}`), '&gt;')
assert.strictEqual(String(escapeHtml`${'"'}`), '&quot;')
assert.strictEqual(String(escapeHtml`${"'"}`), '&#39;')
assert.strictEqual(String(escapeHtml`${'/'}`), '&#x2F;')
assert.strictEqual(String(escapeHtml`${'`'}`), '&#x60;')
assert.strictEqual(String(escapeHtml`${'='}`), '&#x3D;')
assert.strictEqual(String(H`${'&'}`), '&amp;')
assert.strictEqual(String(H`${'<'}`), '&lt;')
assert.strictEqual(String(H`${'>'}`), '&gt;')
assert.strictEqual(String(H`${'"'}`), '&quot;')
assert.strictEqual(String(H`${"'"}`), '&#39;')
assert.strictEqual(String(H`${'/'}`), '&#x2F;')
assert.strictEqual(String(H`${'`'}`), '&#x60;')
assert.strictEqual(String(H`${'='}`), '&#x3D;')
// flattening arrays
assert.strictEqual(String(escapeHtml`${['a', 1, '<']}`), 'a1&lt;')
assert.strictEqual(String(escapeHtml`${['a', escapeHtml`<`]}`), 'a<')
assert.strictEqual(String(escapeHtml`${[]}`), '')
assert.strictEqual(String(H`${['a', 1, '<']}`), 'a1&lt;')
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`${'&'}`)), '<&amp;>')
assert.strictEqual(String(H.join([H`${'<'}`, H`${'>'}`], H`&`)), '&lt;&&gt;')
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