wooorm / stringify-entities

Compare 3551001 ... +17 ... 81a1252

No flags found

Use flags to group coverage reports by test type, project and/or folders.
Then setup custom commit statuses and notifications for each flag.

e.g., #unittest #integration

#production #enterprise

#frontend #backend

Learn more about Codecov Flags here.

Showing 10 of 24 files from the diff.
Newly tracked file
lib/core.js created.
Newly tracked file
lib/index.js created.
Other files ignored by Codecov
package.json has changed.
types/tslint.json was deleted.
build.js has changed.
types/tests.ts was deleted.
dangerous.json was deleted.
test.js has changed.
readme.md has changed.
types/index.d.ts was deleted.
.gitignore has changed.
.prettierignore has changed.
.travis.yml was deleted.

@@ -0,0 +1,14 @@
Loading
1 +
/**
2 +
 * Transform `code` into a hexadecimal character reference.
3 +
 *
4 +
 * @param {number} code
5 +
 * @param {number} next
6 +
 * @param {boolean} omit
7 +
 * @returns {string}
8 +
 */
9 +
export function toHexadecimal(code, next, omit) {
10 +
  var value = '&#x' + code.toString(16).toUpperCase()
11 +
  return omit && next && !/[\dA-Fa-f]/.test(String.fromCharCode(next))
12 +
    ? value
13 +
    : value + ';'
14 +
}

@@ -0,0 +1,10 @@
Loading
1 +
export var dangerous = [
2 +
  'cent',
3 +
  'copy',
4 +
  'divide',
5 +
  'gt',
6 +
  'lt',
7 +
  'not',
8 +
  'para',
9 +
  'times'
10 +
]

@@ -0,0 +1,14 @@
Loading
1 +
/**
2 +
 * Transform `code` into a decimal character reference.
3 +
 *
4 +
 * @param {number} code
5 +
 * @param {number} next
6 +
 * @param {boolean} omit
7 +
 * @returns {string}
8 +
 */
9 +
export function toDecimal(code, next, omit) {
10 +
  var value = '&#' + String(code)
11 +
  return omit && next && !/\d/.test(String.fromCharCode(next))
12 +
    ? value
13 +
    : value + ';'
14 +
}

@@ -0,0 +1,86 @@
Loading
1 +
/**
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 `` ` ``
5 +
 *
6 +
 * @typedef {Object} FormatOptions
7 +
 * @property {function (number, number, CoreWithFormatOptions): string} format
8 +
 *
9 +
 * @typedef {CoreOptions & FormatOptions & import('./util/format-smart.js').FormatSmartOptions} CoreWithFormatOptions
10 +
 */
11 +
12 +
/**
13 +
 * Encode special characters in `value`.
14 +
 *
15 +
 * @param {string} value
16 +
 * @param {CoreWithFormatOptions} [options]
17 +
 * @returns {string}
18 +
 */
19 +
export function core(value, options) {
20 +
  value = value.replace(
21 +
    options.subset ? charactersToExpression(options.subset) : /["&'<>`]/g,
22 +
    basic
23 +
  )
24 +
25 +
  if (options.subset || options.escapeOnly) {
26 +
    return value
27 +
  }
28 +
29 +
  return (
30 +
    value
31 +
      // Surrogate pairs.
32 +
      .replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, surrogate)
33 +
      // BMP control characters (C0 except for LF, CR, SP; DEL; and some more
34 +
      // non-ASCII ones).
35 +
      .replace(
36 +
        // eslint-disable-next-line no-control-regex, unicorn/no-hex-escape
37 +
        /[\x01-\t\v\f\x0E-\x1F\x7F\x81\x8D\x8F\x90\x9D\xA0-\uFFFF]/g,
38 +
        basic
39 +
      )
40 +
  )
41 +
42 +
  /**
43 +
   * @param {string} pair
44 +
   * @param {number} index
45 +
   * @param {string} all
46 +
   */
47 +
  function surrogate(pair, index, all) {
48 +
    return options.format(
49 +
      (pair.charCodeAt(0) - 0xd800) * 0x400 +
50 +
        pair.charCodeAt(1) -
51 +
        0xdc00 +
52 +
        0x10000,
53 +
      all.charCodeAt(index + 2),
54 +
      options
55 +
    )
56 +
  }
57 +
58 +
  /**
59 +
   * @param {string} character
60 +
   * @param {number} index
61 +
   * @param {string} all
62 +
   */
63 +
  function basic(character, index, all) {
64 +
    return options.format(
65 +
      character.charCodeAt(0),
66 +
      all.charCodeAt(index + 1),
67 +
      options
68 +
    )
69 +
  }
70 +
}
71 +
72 +
/**
73 +
 * @param {string[]} subset
74 +
 * @returns {RegExp}
75 +
 */
76 +
function charactersToExpression(subset) {
77 +
  /** @type {string[]} */
78 +
  var groups = []
79 +
  var index = -1
80 +
81 +
  while (++index < subset.length) {
82 +
    groups.push(subset[index].replace(/[|\\{}()[\]^$+*?.]/g, '\\$&'))
83 +
  }
84 +
85 +
  return new RegExp('(?:' + groups.join('|') + ')', 'g')
86 +
}

@@ -0,0 +1,43 @@
Loading
1 +
import {characterEntitiesLegacy} from 'character-entities-legacy'
2 +
import {characters} from '../constant/characters.js'
3 +
import {dangerous} from '../constant/dangerous.js'
4 +
5 +
var own = {}.hasOwnProperty
6 +
7 +
/**
8 +
 * Transform `code` into a named character reference.
9 +
 *
10 +
 * @param {number} code
11 +
 * @param {number} next
12 +
 * @param {boolean} omit
13 +
 * @param {boolean} attribute
14 +
 * @returns {string}
15 +
 */
16 +
export function toNamed(code, next, omit, attribute) {
17 +
  var character = String.fromCharCode(code)
18 +
  /** @type {string} */
19 +
  var name
20 +
  /** @type {string} */
21 +
  var value
22 +
23 +
  if (own.call(characters, character)) {
24 +
    name = characters[character]
25 +
    value = '&' + name
26 +
27 +
    if (
28 +
      omit &&
29 +
      own.call(characterEntitiesLegacy, name) &&
30 +
      !dangerous.includes(name) &&
31 +
      (!attribute ||
32 +
        (next &&
33 +
          next !== 61 /* `=` */ &&
34 +
          /[^\da-z]/i.test(String.fromCharCode(next))))
35 +
    ) {
36 +
      return value
37 +
    }
38 +
39 +
    return value + ';'
40 +
  }
41 +
42 +
  return ''
43 +
}

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Learn more Showing 9 files with coverage changes found.

New file lib/util/to-hexadecimal.js
New
Loading file...
New file lib/util/to-named.js
New
Loading file...
New file lib/util/to-decimal.js
New
Loading file...
New file lib/core.js
New
Loading file...
New file lib/constant/characters.js
New
Loading file...
New file lib/constant/dangerous.js
New
Loading file...
New file lib/index.js
New
Loading file...
New file lib/util/format-smart.js
New
Loading file...
New file lib/util/format-basic.js
New
Loading file...
Files Coverage
lib +100.00% 100.00%
index.js 100.00%
Project Totals (10 files) 100.00%
Loading