wooorm / stringify-entities

@@ -1,16 +1,20 @@
Loading
1 1
/**
2 2
 * @typedef {Object} CoreOptions
3 -
 * @property {string[]} [subset=[]] Whether to only escape the given subset of characters (`string[]`)
4 -
 * @property {boolean} [escapeOnly=false] Whether to only escape possibly dangerous characters (`boolean`, default: `false`). Those characters are `"`, `&`, `'`, `<`, `>`, and `` ` ``
3 +
 * @property {string[]} [subset=[]]
4 +
 *   Whether to only escape the given subset of characters.
5 +
 * @property {boolean} [escapeOnly=false]
6 +
 *   Whether to only escape possibly dangerous characters.
7 +
 *   Those characters are `"`, `&`, `'`, `<`, `>`, and `` ` ``.
5 8
 *
6 9
 * @typedef {Object} FormatOptions
7 -
 * @property {function (number, number, CoreWithFormatOptions): string} format
10 +
 * @property {(code: number, next: number, options: CoreWithFormatOptions) => string} format
11 +
 *   Format strategy.
8 12
 *
9 13
 * @typedef {CoreOptions & FormatOptions & import('./util/format-smart.js').FormatSmartOptions} CoreWithFormatOptions
10 14
 */
11 15
12 16
/**
13 -
 * Encode special characters in `value`.
17 +
 * Encode certain characters in `value`.
14 18
 *
15 19
 * @param {string} value
16 20
 * @param {CoreWithFormatOptions} options

@@ -1,11 +1,27 @@
Loading
1 1
import {characterEntitiesLegacy} from 'character-entities-legacy'
2 -
import {characters} from '../constant/characters.js'
2 +
import {characterEntitiesHtml4} from 'character-entities-html4'
3 3
import {dangerous} from '../constant/dangerous.js'
4 4
5 5
const own = {}.hasOwnProperty
6 6
7 7
/**
8 -
 * Transform `code` into a named character reference.
8 +
 * `characterEntitiesHtml4` but inverted.
9 +
 *
10 +
 * @type {Object.<string, string>}
11 +
 */
12 +
const characters = {}
13 +
14 +
/** @type {string} */
15 +
let key
16 +
17 +
for (key in characterEntitiesHtml4) {
18 +
  if (own.call(characterEntitiesHtml4, key)) {
19 +
    characters[characterEntitiesHtml4[key]] = key
20 +
  }
21 +
}
22 +
23 +
/**
24 +
 * Configurable ways to encode characters as named references.
9 25
 *
10 26
 * @param {number} code
11 27
 * @param {number} next

@@ -1,5 +1,5 @@
Loading
1 1
/**
2 -
 * Transform `code` into a decimal character reference.
2 +
 * Configurable ways to encode characters as decimal references.
3 3
 *
4 4
 * @param {number} code
5 5
 * @param {number} next

@@ -1,5 +1,5 @@
Loading
1 1
/**
2 -
 * Transform `code` into a hexadecimal character reference.
2 +
 * Configurable ways to encode characters as hexadecimal references.
3 3
 *
4 4
 * @param {number} code
5 5
 * @param {number} next

@@ -1,3 +1,9 @@
Loading
1 +
/**
2 +
 * List of legacy (that don’t need a trailing `;`) named references which could,
3 +
 * depending on what follows them, turn into a different meaning
4 +
 *
5 +
 * @type {Array.<string>}
6 +
 */
1 7
export const dangerous = [
2 8
  'cent',
3 9
  'copy',

@@ -1,16 +1,21 @@
Loading
1 +
/**
2 +
 * @typedef {import('./core.js').CoreOptions & import('./util/format-smart.js').FormatSmartOptions} Options
3 +
 * @typedef {import('./core.js').CoreOptions} LightOptions
4 +
 */
5 +
1 6
import {core} from './core.js'
2 7
import {formatSmart} from './util/format-smart.js'
3 8
import {formatBasic} from './util/format-basic.js'
4 9
5 -
/**
6 -
 * @typedef {import('./core.js').CoreOptions & import('./util/format-smart.js').FormatSmartOptions} StringifyEntitiesOptions
7 -
 * @typedef {import('./core.js').CoreOptions} StringifyEntitiesLightOptions
8 -
 */
9 -
10 10
/**
11 11
 * Encode special characters in `value`.
12 +
 *
12 13
 * @param {string} value
13 -
 * @param {StringifyEntitiesOptions} [options]
14 +
 *   Value to encode.
15 +
 * @param {Options} [options]
16 +
 *   Configuration.
17 +
 * @returns {string}
18 +
 *   Encoded value.
14 19
 */
15 20
export function stringifyEntities(value, options) {
16 21
  return core(value, Object.assign({format: formatSmart}, options))
@@ -18,8 +23,13 @@
Loading
18 23
19 24
/**
20 25
 * Encode special characters in `value` as hexadecimals.
26 +
 *
21 27
 * @param {string} value
22 -
 * @param {StringifyEntitiesLightOptions} [options]
28 +
 *   Value to encode.
29 +
 * @param {LightOptions} [options]
30 +
 *   Configuration.
31 +
 * @returns {string}
32 +
 *   Encoded value.
23 33
 */
24 34
export function stringifyEntitiesLight(value, options) {
25 35
  return core(value, Object.assign({format: formatBasic}, options))

@@ -1,9 +1,18 @@
Loading
1 1
/**
2 2
 * @typedef {Object} FormatSmartOptions
3 -
 * @property {boolean} [useNamedReferences=false] Prefer named character references (`&amp;`) where possible (`boolean?`, default: `false`)
4 -
 * @property {boolean} [useShortestReferences=false] Prefer the shortest possible reference, if that results in less bytes (`boolean?`, default: `false`). **Note**: `useNamedReferences` can be omitted when using `useShortestReferences`
5 -
 * @property {boolean} [omitOptionalSemicolons=false] Whether to omit semicolons when possible (`boolean?`, default: `false`). **Note**: This creates what HTML calls “parse errors” but is otherwise still valid HTML — don’t use this except when building a minifier. Omitting semicolons is possible for legacy named references in certain cases, and numeric references in some cases
6 -
 * @property {boolean} [attribute=false] Only needed when operating dangerously with `omitOptionalSemicolons: true`. Create character references which don’t fail in attributes (`boolean?`, default: `false`).
3 +
 * @property {boolean} [useNamedReferences=false]
4 +
 *   Prefer named character references (`&amp;`) where possible.
5 +
 * @property {boolean} [useShortestReferences=false]
6 +
 *   Prefer the shortest possible reference, if that results in less bytes.
7 +
 *   **Note**: `useNamedReferences` can be omitted when using `useShortestReferences`.
8 +
 * @property {boolean} [omitOptionalSemicolons=false]
9 +
 *   Whether to omit semicolons when possible.
10 +
 *   **Note**: This creates what HTML calls “parse errors” but is otherwise still valid HTML — don’t use this except when building a minifier.
11 +
 *   Omitting semicolons is possible for certain named and numeric references in some cases.
12 +
 * @property {boolean} [attribute=false]
13 +
 *   Create character references which don’t fail in attributes.
14 +
 *   **Note**: `attribute` only applies when operating dangerously with
15 +
 *   `omitOptionalSemicolons: true`.
7 16
 */
8 17
9 18
import {toHexadecimal} from './to-hexadecimal.js'
@@ -11,7 +20,7 @@
Loading
11 20
import {toNamed} from './to-named.js'
12 21
13 22
/**
14 -
 * Encode `character` according to `options`.
23 +
 * Configurable ways to encode a character yielding pretty or small results.
15 24
 *
16 25
 * @param {number} code
17 26
 * @param {number} next

@@ -1,4 +1,6 @@
Loading
1 1
/**
2 +
 * The smallest way to encode a character.
3 +
 *
2 4
 * @param {number} code
3 5
 * @returns {string}
4 6
 */

@@ -1,6 +1,6 @@
Loading
1 1
/**
2 -
 * @typedef {import('./lib/index.js').StringifyEntitiesLightOptions} StringifyEntitiesLightOptions
3 -
 * @typedef {import('./lib/index.js').StringifyEntitiesOptions} StringifyEntitiesOptions
2 +
 * @typedef {import('./lib/index.js').LightOptions} LightOptions
3 +
 * @typedef {import('./lib/index.js').Options} Options
4 4
 */
5 5
6 6
export * from './lib/index.js'
Files Coverage
lib 100.00%
index.js 100.00%
Project Totals (9 files) 100.00%
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading