Skip to content

Commit

Permalink
Replace cssesc with CSS.escape
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed May 5, 2023
1 parent 81377fc commit df88b72
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 97 deletions.
100 changes: 3 additions & 97 deletions finder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ function id(input: Element): Knot | null {
const elementId = input.getAttribute('id')
if (elementId && config.idName(elementId)) {
return {
name: '#' + cssesc(elementId, {isIdentifier: true}),
name: '#' + CSS.escape(elementId),
penalty: 0,
}
}
Expand All @@ -201,12 +201,7 @@ function attr(input: Element): Knot[] {
)
return attrs.map(
(attr): Knot => ({
name:
'[' +
cssesc(attr.name, {isIdentifier: true}) +
'="' +
cssesc(attr.value) +
'"]',
name: `[${CSS.escape(attr.name)}="${CSS.escape(attr.value)}"]`,
penalty: 0.5,
})
)
Expand All @@ -216,7 +211,7 @@ function classNames(input: Element): Knot[] {
const names = Array.from(input.classList).filter(config.className)
return names.map(
(name): Knot => ({
name: '.' + cssesc(name, {isIdentifier: true}),
name: '.' + CSS.escape(name),
penalty: 1,
})
)
Expand Down Expand Up @@ -336,92 +331,3 @@ function* optimize(
function same(path: Path, input: Element) {
return rootDocument.querySelector(selector(path)) === input
}

const regexAnySingleEscape = /[ -,\.\/:-@\[-\^`\{-~]/
const regexSingleEscape = /[ -,\.\/:-@\[\]\^`\{-~]/
const regexExcessiveSpaces =
/(^|\\+)?(\\[A-F0-9]{1,6})\x20(?![a-fA-F0-9\x20])/g

const defaultOptions = {
escapeEverything: false,
isIdentifier: false,
quotes: 'single',
wrap: false,
}

function cssesc(string: string, opt: Partial<typeof defaultOptions> = {}) {
const options = {...defaultOptions, ...opt}
if (options.quotes != 'single' && options.quotes != 'double') {
options.quotes = 'single'
}
const quote = options.quotes == 'double' ? '"' : '\''
const isIdentifier = options.isIdentifier
const firstChar = string.charAt(0)
let output = ''
let counter = 0
const length = string.length
while (counter < length) {
const character = string.charAt(counter++)
let codePoint = character.charCodeAt(0)
let value: string | undefined = void 0
// If it’s not a printable ASCII character…
if (codePoint < 0x20 || codePoint > 0x7e) {
if (codePoint >= 0xd800 && codePoint <= 0xdbff && counter < length) {
// It’s a high surrogate, and there is a next character.
const extra = string.charCodeAt(counter++)
if ((extra & 0xfc00) == 0xdc00) {
// next character is low surrogate
codePoint = ((codePoint & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000
} else {
// It’s an unmatched surrogate; only append this code unit, in case
// the next code unit is the high surrogate of a surrogate pair.
counter--
}
}
value = '\\' + codePoint.toString(16).toUpperCase() + ' '
} else {
if (options.escapeEverything) {
if (regexAnySingleEscape.test(character)) {
value = '\\' + character
} else {
value = '\\' + codePoint.toString(16).toUpperCase() + ' '
}
} else if (/[\t\n\f\r\x0B]/.test(character)) {
value = '\\' + codePoint.toString(16).toUpperCase() + ' '
} else if (
character == '\\' ||
(!isIdentifier &&
((character == '"' && quote == character) ||
(character == '\'' && quote == character))) ||
(isIdentifier && regexSingleEscape.test(character))
) {
value = '\\' + character
} else {
value = character
}
}
output += value
}
if (isIdentifier) {
if (/^-[-\d]/.test(output)) {
output = '\\-' + output.slice(1)
} else if (/\d/.test(firstChar)) {
output = '\\3' + firstChar + ' ' + output.slice(1)
}
}
// Remove spaces after `\HEX` escapes that are not followed by a hex digit,
// since they’re redundant. Note that this is only possible if the escape
// sequence isn’t preceded by an odd number of backslashes.
output = output.replace(regexExcessiveSpaces, function ($0, $1, $2) {
if ($1 && $1.length % 2) {
// It’s not safe to remove the space, so don’t.
return $0
}
// Strip the space.
return ($1 || '') + $2
})
if (!isIdentifier && options.wrap) {
return quote + output + quote
}
return output
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"release": "release-it --access public"
},
"devDependencies": {
"css.escape": "^1.5.1",
"jsdom": "^21.1.0",
"release-it": "^15.7.0",
"typescript": "4.9.5",
Expand Down
2 changes: 2 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { fileURLToPath } from 'node:url'
import { dirname } from 'node:path'
import { finder } from '../finder.js'

import 'css.escape'

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

Expand Down

0 comments on commit df88b72

Please sign in to comment.