0
Fork 0
mirror of https://github.com/penpot/penpot-export.git synced 2025-01-04 13:50:05 -05:00

test(core): add tests for CSS ident tokens

This commit is contained in:
Roberto Redradix 2023-09-04 13:41:29 +02:00
parent e35186eae9
commit bf6f6a723a
4 changed files with 51 additions and 1 deletions

View file

@ -7,6 +7,7 @@
"scripts": {
"dev": "npm run dev -w @penpot-export/cli",
"build": "npm run build -w @penpot-export/core -w @penpot-export/cli",
"test": "npm run test --workspaces --if-present",
"demo": "npm run generate-css -w demo"
}
}

View file

@ -9,6 +9,8 @@
],
"scripts": {
"build": "tsc",
"pretest": "npm run build",
"test": "node --test",
"format": "prettier -w ."
},
"dependencies": {

View file

@ -0,0 +1,47 @@
import assert from 'node:assert'
import { describe, it } from 'node:test'
import { textToCssIdentToken } from './helpers'
describe('CSS helpers', () => {
describe('Transforming text to CSS ident tokens', () => {
describe('when input text includes ASCII printable characters', () => {
it('escapes all ASCII printable characters except ident code points (-, 0-9, A-Z, _, a-z)', () => {
const allAsciiPrintableCharacters =
'!"#$%&' +
"'" +
'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[' +
'\\' +
']^_`abcdefghijklmnopqrtuvwxyz{|}~'
const expect =
'\\!\\"\\#\\$\\%\\&' +
"\\'" +
'\\(\\)\\*\\+\\,-\\.\\/0123456789\\:\\;\\<\\=\\>\\?\\@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\[' +
'\\\\' +
'\\]\\^_\\`abcdefghijklmnopqrtuvwxyz\\{\\|\\}\\~'
assert.ok(/^[\x20-\x7E]*$/.test(allAsciiPrintableCharacters))
assert.equal(textToCssIdentToken(allAsciiPrintableCharacters), expect)
})
})
describe('when input text does NOT start with a ident-start code point', () => {
it('escapes starting number', () => {
const expect = '\\0123456789'
assert.equal(textToCssIdentToken('0123456789'), expect)
})
})
describe('when input text starts with a hyphen', () => {
describe('when the following character is NOT an ident-start code point', () => {
it('escapes first number following a hyphen', () => {
const expect = '-\\0123456789'
assert.equal(textToCssIdentToken('-0123456789'), expect)
})
})
})
})
})

View file

@ -23,7 +23,7 @@ function escapeCssCharacter(char: string) {
* letter: An uppercase letter or a lowercase letter.
* non-ASCII code point: A code point with a value equal to or greater than U+0080 <control>.
*/
function textToCssIdentToken(str: string) {
export function textToCssIdentToken(str: string) {
const normalizedString = str.trim().replace(/\s/g, '_')
const escapedString = normalizedString