Skip to content

Commit 1c905f2

Browse files
authored
minor: better type safety and best practice to escape function (#16579)
1 parent 8b13076 commit 1c905f2

File tree

2 files changed

+16
-89
lines changed

2 files changed

+16
-89
lines changed

integrations/utils.ts

Lines changed: 1 addition & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { platform, tmpdir } from 'node:os'
66
import path from 'node:path'
77
import { stripVTControlCharacters } from 'node:util'
88
import { test as defaultTest, type ExpectStatic } from 'vitest'
9+
import { escape } from '../packages/tailwindcss/src/utils/escape'
910

1011
const REPO_ROOT = path.join(__dirname, '..')
1112
const PUBLIC_PACKAGES = (await fs.readdir(path.join(REPO_ROOT, 'dist'))).map((name) =>
@@ -521,80 +522,6 @@ export function candidate(strings: TemplateStringsArray, ...values: any[]) {
521522
return `.${escape(output.join('').trim())}`
522523
}
523524

524-
// https://drafts.csswg.org/cssom/#serialize-an-identifier
525-
export function escape(value: string) {
526-
if (arguments.length == 0) {
527-
throw new TypeError('`CSS.escape` requires an argument.')
528-
}
529-
var string = String(value)
530-
var length = string.length
531-
var index = -1
532-
var codeUnit
533-
var result = ''
534-
var firstCodeUnit = string.charCodeAt(0)
535-
536-
if (
537-
// If the character is the first character and is a `-` (U+002D), and
538-
// there is no second character, […]
539-
length == 1 &&
540-
firstCodeUnit == 0x002d
541-
) {
542-
return '\\' + string
543-
}
544-
545-
while (++index < length) {
546-
codeUnit = string.charCodeAt(index)
547-
// Note: there’s no need to special-case astral symbols, surrogate
548-
// pairs, or lone surrogates.
549-
550-
// If the character is NULL (U+0000), then the REPLACEMENT CHARACTER
551-
// (U+FFFD).
552-
if (codeUnit == 0x0000) {
553-
result += '\uFFFD'
554-
continue
555-
}
556-
557-
if (
558-
// If the character is in the range [\1-\1F] (U+0001 to U+001F) or is
559-
// U+007F, […]
560-
(codeUnit >= 0x0001 && codeUnit <= 0x001f) ||
561-
codeUnit == 0x007f ||
562-
// If the character is the first character and is in the range [0-9]
563-
// (U+0030 to U+0039), […]
564-
(index == 0 && codeUnit >= 0x0030 && codeUnit <= 0x0039) ||
565-
// If the character is the second character and is in the range [0-9]
566-
// (U+0030 to U+0039) and the first character is a `-` (U+002D), […]
567-
(index == 1 && codeUnit >= 0x0030 && codeUnit <= 0x0039 && firstCodeUnit == 0x002d)
568-
) {
569-
// https://drafts.csswg.org/cssom/#escape-a-character-as-code-point
570-
result += '\\' + codeUnit.toString(16) + ' '
571-
continue
572-
}
573-
574-
// If the character is not handled by one of the above rules and is
575-
// greater than or equal to U+0080, is `-` (U+002D) or `_` (U+005F), or
576-
// is in one of the ranges [0-9] (U+0030 to U+0039), [A-Z] (U+0041 to
577-
// U+005A), or [a-z] (U+0061 to U+007A), […]
578-
if (
579-
codeUnit >= 0x0080 ||
580-
codeUnit == 0x002d ||
581-
codeUnit == 0x005f ||
582-
(codeUnit >= 0x0030 && codeUnit <= 0x0039) ||
583-
(codeUnit >= 0x0041 && codeUnit <= 0x005a) ||
584-
(codeUnit >= 0x0061 && codeUnit <= 0x007a)
585-
) {
586-
// the character itself
587-
result += string.charAt(index)
588-
continue
589-
}
590-
591-
// Otherwise, the escaped character.
592-
// https://drafts.csswg.org/cssom/#escape-a-character
593-
result += '\\' + string.charAt(index)
594-
}
595-
return result
596-
}
597-
598525
export async function retryAssertion<T>(
599526
fn: () => Promise<T>,
600527
{ timeout = ASSERTION_TIMEOUT, delay = 5 }: { timeout?: number; delay?: number } = {},

packages/tailwindcss/src/utils/escape.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
// https://drafts.csswg.org/cssom/#serialize-an-identifier
22
export function escape(value: string) {
3-
if (arguments.length == 0) {
3+
if (arguments.length === 0) {
44
throw new TypeError('`CSS.escape` requires an argument.')
55
}
6-
var string = String(value)
7-
var length = string.length
8-
var index = -1
9-
var codeUnit
10-
var result = ''
11-
var firstCodeUnit = string.charCodeAt(0)
6+
let string = String(value)
7+
let length = string.length
8+
let index = -1
9+
let codeUnit: number
10+
let result = ''
11+
let firstCodeUnit = string.charCodeAt(0)
1212

1313
if (
1414
// If the character is the first character and is a `-` (U+002D), and
1515
// there is no second character, […]
16-
length == 1 &&
17-
firstCodeUnit == 0x002d
16+
length === 1 &&
17+
firstCodeUnit === 0x002d
1818
) {
1919
return '\\' + string
2020
}
@@ -26,7 +26,7 @@ export function escape(value: string) {
2626

2727
// If the character is NULL (U+0000), then the REPLACEMENT CHARACTER
2828
// (U+FFFD).
29-
if (codeUnit == 0x0000) {
29+
if (codeUnit === 0x0000) {
3030
result += '\uFFFD'
3131
continue
3232
}
@@ -35,13 +35,13 @@ export function escape(value: string) {
3535
// If the character is in the range [\1-\1F] (U+0001 to U+001F) or is
3636
// U+007F, […]
3737
(codeUnit >= 0x0001 && codeUnit <= 0x001f) ||
38-
codeUnit == 0x007f ||
38+
codeUnit === 0x007f ||
3939
// If the character is the first character and is in the range [0-9]
4040
// (U+0030 to U+0039), […]
41-
(index == 0 && codeUnit >= 0x0030 && codeUnit <= 0x0039) ||
41+
(index === 0 && codeUnit >= 0x0030 && codeUnit <= 0x0039) ||
4242
// If the character is the second character and is in the range [0-9]
4343
// (U+0030 to U+0039) and the first character is a `-` (U+002D), […]
44-
(index == 1 && codeUnit >= 0x0030 && codeUnit <= 0x0039 && firstCodeUnit == 0x002d)
44+
(index === 1 && codeUnit >= 0x0030 && codeUnit <= 0x0039 && firstCodeUnit === 0x002d)
4545
) {
4646
// https://drafts.csswg.org/cssom/#escape-a-character-as-code-point
4747
result += '\\' + codeUnit.toString(16) + ' '
@@ -54,8 +54,8 @@ export function escape(value: string) {
5454
// U+005A), or [a-z] (U+0061 to U+007A), […]
5555
if (
5656
codeUnit >= 0x0080 ||
57-
codeUnit == 0x002d ||
58-
codeUnit == 0x005f ||
57+
codeUnit === 0x002d ||
58+
codeUnit === 0x005f ||
5959
(codeUnit >= 0x0030 && codeUnit <= 0x0039) ||
6060
(codeUnit >= 0x0041 && codeUnit <= 0x005a) ||
6161
(codeUnit >= 0x0061 && codeUnit <= 0x007a)

0 commit comments

Comments
 (0)