All checks were successful
Publish To Prod / deploy_and_publish (push) Successful in 35s
59 lines
1.3 KiB
JavaScript
59 lines
1.3 KiB
JavaScript
'use strict'
|
|
|
|
module.exports = encode
|
|
|
|
// Encode special characters in `value`.
|
|
function encode(value, options) {
|
|
value = value.replace(
|
|
options.subset ? charactersToExpression(options.subset) : /["&'<>`]/g,
|
|
basic
|
|
)
|
|
|
|
if (options.subset || options.escapeOnly) {
|
|
return value
|
|
}
|
|
|
|
return (
|
|
value
|
|
// Surrogate pairs.
|
|
.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, surrogate)
|
|
// BMP control characters (C0 except for LF, CR, SP; DEL; and some more
|
|
// non-ASCII ones).
|
|
.replace(
|
|
// eslint-disable-next-line no-control-regex, unicorn/no-hex-escape
|
|
/[\x01-\t\v\f\x0E-\x1F\x7F\x81\x8D\x8F\x90\x9D\xA0-\uFFFF]/g,
|
|
basic
|
|
)
|
|
)
|
|
|
|
function surrogate(pair, index, all) {
|
|
return options.format(
|
|
(pair.charCodeAt(0) - 0xd800) * 0x400 +
|
|
pair.charCodeAt(1) -
|
|
0xdc00 +
|
|
0x10000,
|
|
all.charCodeAt(index + 2),
|
|
options
|
|
)
|
|
}
|
|
|
|
function basic(character, index, all) {
|
|
return options.format(
|
|
character.charCodeAt(0),
|
|
all.charCodeAt(index + 1),
|
|
options
|
|
)
|
|
}
|
|
}
|
|
|
|
function charactersToExpression(subset) {
|
|
var groups = []
|
|
var index = -1
|
|
|
|
while (++index < subset.length) {
|
|
groups.push(subset[index].replace(/[|\\{}()[\]^$+*?.]/g, '\\$&'))
|
|
}
|
|
|
|
return new RegExp('(?:' + groups.join('|') + ')', 'g')
|
|
}
|