.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stringify-entities / index.js
1 'use strict'
2
3 var entities = require('character-entities-html4')
4 var legacy = require('character-entities-legacy')
5 var hexadecimal = require('is-hexadecimal')
6 var alphanumerical = require('is-alphanumerical')
7 var dangerous = require('./dangerous.json')
8
9 /* Expose. */
10 module.exports = encode
11 encode.escape = escape
12
13 var own = {}.hasOwnProperty
14
15 /* List of enforced escapes. */
16 var escapes = ['"', "'", '<', '>', '&', '`']
17
18 /* Map of characters to names. */
19 var characters = construct()
20
21 /* Default escapes. */
22 var defaultEscapes = toExpression(escapes)
23
24 /* Surrogate pairs. */
25 var surrogatePair = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g
26
27 /* Non-ASCII characters. */
28 // eslint-disable-next-line no-control-regex, unicorn/no-hex-escape
29 var bmp = /[\x01-\t\x0B\f\x0E-\x1F\x7F\x81\x8D\x8F\x90\x9D\xA0-\uFFFF]/g
30
31 /* Encode special characters in `value`. */
32 function encode(value, options) {
33   var settings = options || {}
34   var subset = settings.subset
35   var set = subset ? toExpression(subset) : defaultEscapes
36   var escapeOnly = settings.escapeOnly
37   var omit = settings.omitOptionalSemicolons
38
39   value = value.replace(set, function(char, pos, val) {
40     return one(char, val.charAt(pos + 1), settings)
41   })
42
43   if (subset || escapeOnly) {
44     return value
45   }
46
47   return value
48     .replace(surrogatePair, replaceSurrogatePair)
49     .replace(bmp, replaceBmp)
50
51   function replaceSurrogatePair(pair, pos, val) {
52     return toHexReference(
53       (pair.charCodeAt(0) - 0xd800) * 0x400 +
54         pair.charCodeAt(1) -
55         0xdc00 +
56         0x10000,
57       val.charAt(pos + 2),
58       omit
59     )
60   }
61
62   function replaceBmp(char, pos, val) {
63     return one(char, val.charAt(pos + 1), settings)
64   }
65 }
66
67 /* Shortcut to escape special characters in HTML. */
68 function escape(value) {
69   return encode(value, {
70     escapeOnly: true,
71     useNamedReferences: true
72   })
73 }
74
75 /* Encode `char` according to `options`. */
76 function one(char, next, options) {
77   var shortest = options.useShortestReferences
78   var omit = options.omitOptionalSemicolons
79   var named
80   var numeric
81
82   if ((shortest || options.useNamedReferences) && own.call(characters, char)) {
83     named = toNamed(characters[char], next, omit, options.attribute)
84   }
85
86   if (shortest || !named) {
87     numeric = toHexReference(char.charCodeAt(0), next, omit)
88   }
89
90   if (named && (!shortest || named.length < numeric.length)) {
91     return named
92   }
93
94   return numeric
95 }
96
97 /* Transform `code` into an entity. */
98 function toNamed(name, next, omit, attribute) {
99   var value = '&' + name
100
101   if (
102     omit &&
103     own.call(legacy, name) &&
104     dangerous.indexOf(name) === -1 &&
105     (!attribute || (next && next !== '=' && !alphanumerical(next)))
106   ) {
107     return value
108   }
109
110   return value + ';'
111 }
112
113 /* Transform `code` into a hexadecimal character reference. */
114 function toHexReference(code, next, omit) {
115   var value = '&#x' + code.toString(16).toUpperCase()
116   return omit && next && !hexadecimal(next) ? value : value + ';'
117 }
118
119 /* Create an expression for `characters`. */
120 function toExpression(characters) {
121   return new RegExp('[' + characters.join('') + ']', 'g')
122 }
123
124 /* Construct the map. */
125 function construct() {
126   var chars = {}
127   var name
128
129   for (name in entities) {
130     chars[entities[name]] = name
131   }
132
133   return chars
134 }