Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
* @typedef {CoreOptions & FormatOptions & import('./util/format-smart.js').FormatSmartOptions} CoreWithFormatOptions
*/

const defaultSubsetRegex = /["&'<>`]/g
const surrogatePairsRegex = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g
// eslint-disable-next-line no-control-regex, unicorn/no-hex-escape
const controlCharactersRegex = /[\x01-\t\v\f\x0E-\x1F\x7F\x81\x8D\x8F\x90\x9D\xA0-\uFFFF]/g
const regexEscapeRegex = /[|\\{}()[\]^$+*?.]/g

/** @type {WeakMap<string[], RegExp>} */
const subsetToRegexCache = new WeakMap()

/**
* Encode certain characters in `value`.
*
Expand All @@ -22,7 +31,9 @@
*/
export function core(value, options) {
value = value.replace(
options.subset ? charactersToExpression(options.subset) : /["&'<>`]/g,
options.subset
? charactersToExpression(options.subset)
: defaultSubsetRegex,
basic
)

Expand All @@ -33,14 +44,10 @@ export function core(value, options) {
return (
value
// Surrogate pairs.
.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, surrogate)
.replace(surrogatePairsRegex, 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
)
.replace(controlCharactersRegex, basic)
)

/**
Expand Down Expand Up @@ -78,13 +85,18 @@ export function core(value, options) {
* @returns {RegExp}
*/
function charactersToExpression(subset) {
const cached = subsetToRegexCache.get(subset)
if (cached) return cached

/** @type {Array<string>} */
const groups = []
let index = -1

while (++index < subset.length) {
groups.push(subset[index].replace(/[|\\{}()[\]^$+*?.]/g, '\\$&'))
groups.push(subset[index].replace(regexEscapeRegex, '\\$&'))
}

return new RegExp('(?:' + groups.join('|') + ')', 'g')
const regex = new RegExp('(?:' + groups.join('|') + ')', 'g')
subsetToRegexCache.set(subset, regex)
return regex
}
4 changes: 3 additions & 1 deletion lib/util/to-decimal.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const decimalRegex = /\d/

/**
* Configurable ways to encode characters as decimal references.
*
Expand All @@ -8,7 +10,7 @@
*/
export function toDecimal(code, next, omit) {
const value = '&#' + String(code)
return omit && next && !/\d/.test(String.fromCharCode(next))
return omit && next && !decimalRegex.test(String.fromCharCode(next))
? value
: value + ';'
}
4 changes: 3 additions & 1 deletion lib/util/to-hexadecimal.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const hexadecimalRegex = /[\dA-Fa-f]/

/**
* Configurable ways to encode characters as hexadecimal references.
*
Expand All @@ -8,7 +10,7 @@
*/
export function toHexadecimal(code, next, omit) {
const value = '&#x' + code.toString(16).toUpperCase()
return omit && next && !/[\dA-Fa-f]/.test(String.fromCharCode(next))
return omit && next && !hexadecimalRegex.test(String.fromCharCode(next))
? value
: value + ';'
}
4 changes: 3 additions & 1 deletion lib/util/to-named.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ for (key in characterEntitiesHtml4) {
}
}

const notAlphanumericRegex = /[^\dA-Za-z]/

/**
* Configurable ways to encode characters as named references.
*
Expand All @@ -43,7 +45,7 @@ export function toNamed(code, next, omit, attribute) {
(!attribute ||
(next &&
next !== 61 /* `=` */ &&
/[^\da-z]/i.test(String.fromCharCode(next))))
notAlphanumericRegex.test(String.fromCharCode(next))))
) {
return value
}
Expand Down