stringify-entities
Encode HTML character references and character entities
Last updated 5 years ago by wooorm .
MIT · Repository · Bugs · Original npm
$ cnpm install stringify-entities 
SYNC missed versions from official npm registry.

stringify-entities

Build Status Coverage Status Downloads Size

Encode HTML character references and character entities.

  • [x] Very fast
  • [x] Just the encoding part
  • [x] Reliable: '`' characters are escaped to ensure no scripts run in Internet Explorer 6 to 8. Additionally, only named entities recognized by HTML4 are encoded, meaning the infamous ' (which people think is a virus) won’t show up

Algorithm

By default, all dangerous, non-ASCII, and non-printable ASCII characters are encoded. A subset of characters can be given to encode just those characters. Alternatively, pass escapeOnly to escape just the dangerous characters (", ', <, >, &, `). By default, numeric entities are used. Pass useNamedReferences to use named entities when possible, or useShortestReferences to use them if that results in less bytes.

Install

npm:

npm install stringify-entities

Use

var stringify = require('stringify-entities')

stringify('alpha © bravo ≠ charlie ???? delta')
// => 'alpha &#xA9; bravo &#x2260; charlie &#x1D306; delta'

stringify('alpha © bravo ≠ charlie ???? delta', {useNamedReferences: true})
// => 'alpha &copy; bravo &ne; charlie &#x1D306; delta'

API

stringifyEntities(value[, options])

Encode special characters in value.

options
options.escapeOnly

Whether to only escape possibly dangerous characters (boolean, default: false). Those characters are ", ', <, > &, and `.

options.subset

Whether to only escape the given subset of characters (Array.<string>).

options.useNamedReferences

Whether to use named entities where possible (boolean?, default: false).

options.useShortestReferences

Whether to use named entities, where possible, if that results in less bytes (boolean?, default: false). Note: useNamedReferences can be omitted when using useShortestReferences.

options.omitOptionalSemicolons

Whether to omit semicolons when possible (boolean?, default: false). Note: This creates parse errors, don’t use this except when building a minifier.

Omitting semicolons is possible for certain legacy named references, and numeric entities, in some cases.

options.attribute

Only needed when operating dangerously with omitOptionalSemicolons: true. Create entities which don’t fail in attributes (boolean?, default: false).

Related

License

MIT © Titus Wormer

Current Tags

  • 4.0.3                                ...           latest (3 years ago)

8 Versions

  • 4.0.3                                ...           3 years ago
  • 2.0.0                                ...           7 years ago
  • 4.0.2                                ...           4 years ago
  • 4.0.1                                ...           4 years ago
  • 3.1.0                                ...           5 years ago
  • 3.0.1                                ...           5 years ago
  • 1.3.2                                ...           7 years ago
  • 3.0.0                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dev Dependencies (9)
Dependents (0)
None

Copyright 2013 - present © cnpmjs.org