Skip to content

Commit

Permalink
feat: add escape ignoreHead option
Browse files Browse the repository at this point in the history
  • Loading branch information
sonofmagic committed Jul 11, 2024
1 parent c170389 commit a4f3a94
Show file tree
Hide file tree
Showing 11 changed files with 11,400 additions and 6,454 deletions.
5 changes: 0 additions & 5 deletions .eslintrc.js

This file was deleted.

9 changes: 0 additions & 9 deletions .prettierrc

This file was deleted.

2 changes: 1 addition & 1 deletion apps/uni-app-vue3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"@rollup/plugin-replace": "^5.0.5",
"@vue/runtime-core": "^3.4.19",
"@vue/tsconfig": "^0.5.1",
"typescript": "^5.3.3",
"typescript": "^5.5.3",
"vite": "5.1.1",
"vue-tsc": "^1.8.27",
"weapp-ide-cli": "^1.0.1"
Expand Down
8 changes: 8 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { icebreaker } from '@icebreakers/eslint-config'

export default icebreaker(
{},
{
ignores: ['**/fixtures/**']
}
)
39 changes: 19 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
{
"name": "weapp-core",
"type": "module",
"version": "0.0.0",
"private": true,
"workspaces": [
"apps/*",
"packages/*"
],
"packageManager": "pnpm@9.3.0",
"repository": {
"type": "git",
"url": "git+https://github.com/sonofmagic/weapp-core.git"
},
"engines": {
"node": ">=16.0.0"
},
"scripts": {
"build": "turbo run build",
"dev": "turbo run dev --parallel",
"test": "vitest run --coverage.enabled",
"test:dev": "vitest --coverage.enabled",
"lint": "turbo run lint",
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
"release": "turbo run release",
"publish-packages": "turbo run build lint && npm run test && changeset version && changeset publish",
"sync": "cnpm sync @weapp-core/escape @weapp-core/regex @weapp-core/http @weapp-core/runtime-injector weapp-websocket weapp-xmlhttprequest"
},
"devDependencies": {
"@changesets/cli": "^2.27.1",
"@vitest/coverage-v8": "^1.2.2",
"@changesets/cli": "^2.27.7",
"@icebreakers/eslint-config": "^0.3.12",
"@vitest/coverage-v8": "^2.0.2",
"@weapp-core/escape": "workspace:^",
"@weapp-core/regex": "workspace:^",
"eslint": "^8.56.0",
"eslint-config-icebreaker": "^1.2.3",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"defu": "^6.1.4",
"eslint": "^9.6.0",
"miniprogram-api-typings": "^3.12.2",
"prettier": "^3.2.5",
"prettier": "^3.3.2",
"ts-node": "^10.9.2",
"turbo": "^1.12.3",
"typescript": "^5.3.3",
"turbo": "^2.0.6",
"typescript": "^5.5.3",
"unbuild": "^2.0.0",
"vitest": "^1.2.2"
"vitest": "^2.0.2"
},
"engines": {
"node": ">=16.0.0"
},
"packageManager": "pnpm@8.6.9",
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
},
"repository": {
"type": "git",
"url": "git+https://github.com/sonofmagic/weapp-core.git"
}
}
}
36 changes: 18 additions & 18 deletions packages/escape/package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
{
"name": "@weapp-core/escape",
"version": "2.0.0",
"type": "commonjs",
"version": "2.1.0",
"description": "escape class name in weapp",
"scripts": {
"build": "unbuild",
"test": "vitest run --coverage.enabled",
"test:dev": "vitest --coverage.enabled",
"release": "pnpm publish"
"author": "SonOfMagic <qq1324318532@gmail.com>",
"license": "MIT",
"homepage": "https://github.com/sonofmagic/weapp-core/tree/main/packages/escape#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/sonofmagic/weapp-core.git"
},
"bugs": {
"url": "https://github.com/sonofmagic/weapp-core/issues"
},
"keywords": [
"weapp",
"escape",
"css"
],
"sideEffects": false,
"type": "commonjs",
"exports": {
".": {
"types": "./dist/index.d.ts",
Expand All @@ -28,19 +32,15 @@
"files": [
"dist"
],
"scripts": {
"build": "unbuild",
"test": "vitest run --coverage.enabled",
"test:dev": "vitest --coverage.enabled",
"release": "pnpm publish"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
},
"author": "SonOfMagic <qq1324318532@gmail.com>",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/sonofmagic/weapp-core.git"
},
"bugs": {
"url": "https://github.com/sonofmagic/weapp-core/issues"
},
"homepage": "https://github.com/sonofmagic/weapp-core/tree/main/packages/escape#readme",
"dependencies": {}
}
}
80 changes: 40 additions & 40 deletions packages/escape/src/dic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const SYMBOL_TABLE = {
BRACERIGHT: '}',
SEMICOLON: ';',
COLON: ':',
QUOTE: "'",
QUOTE: '\'',
DOUBLEQUOTE: '"',
BACKSLASH: '\\',
BAR: '|',
Expand All @@ -33,7 +33,7 @@ export const SYMBOL_TABLE = {
QUESTION: '?',
SPACE: ' ',
DOT: '.',
HASH: '#'
HASH: '#',
} as const

export type SYMBOL_TABLE_TYPE = typeof SYMBOL_TABLE
Expand All @@ -55,7 +55,7 @@ export const MappingChars2String: MappingStringDictionary = {
':': '_c_',
'%': '_p_',
',': '_co_',
"'": '_q_',
'\'': '_q_',
'"': '_dq_',
'*': '_a_',
'&': '_am_',
Expand All @@ -74,7 +74,7 @@ export const MappingChars2String: MappingStringDictionary = {
'^': '_ca_',
'`': '_bq_',
'|': '_b_',
$: '_do_'
'$': '_do_',
// _: '_u_'
} as const

Expand All @@ -86,45 +86,45 @@ export const MappingChars2StringEntries = Object.entries(MappingChars2String)
// 同样一条 decl 你可以写 px 也可以写 PX
// 0-31 48-57(0-9数字字符) 65-90(大写英文) 97-122(小写英文) 127(DEL)不考虑
// #region 32-47 count:16
// 32 20 space // 其实可以忽略空格
// 33 21 ! ✔
// 34 22 " ✔
// 35 23 # ✔
// 36 24 $ ✔
// 37 25 % ✔
// 38 26 & ✔
// 39 27 ' ✔
// 40 28 ( ✔
// 41 29 ) ✔
// 42 2A * ✔
// 43 2B + ✔
// 44 2C , ✔
// 45 2D - //小程序中允许,但是不能单独放在前后第一位-
// 46 2E . ✔
// 47 2F / ✔
// 32 20 space // 其实可以忽略空格
// 33 21 ! ✔
// 34 22 " ✔
// 35 23 # ✔
// 36 24 $ ✔
// 37 25 % ✔
// 38 26 & ✔
// 39 27 ' ✔
// 40 28 ( ✔
// 41 29 ) ✔
// 42 2A * ✔
// 43 2B + ✔
// 44 2C , ✔
// 45 2D - //小程序中允许,但是不能单独放在前后第一位-
// 46 2E . ✔
// 47 2F / ✔
// #endregion
// #region 58-64 count:7 ✔
// 58 3A : ✔
// 59 3B ; ✔
// 60 3C < ✔
// 61 3D = ✔
// 62 3E > ✔
// 63 3F ? ✔
// 64 40 @ ✔
// 58 3A : ✔
// 59 3B ; ✔
// 60 3C < ✔
// 61 3D = ✔
// 62 3E > ✔
// 63 3F ? ✔
// 64 40 @ ✔
// #endregion
// #region 91-96 count:6 ✔
// 91 5B [ ✔
// 92 5C \ ✔
// 93 5D ] ✔
// 94 5E ^ ✔
// 95 5F _ // 小程序中合法
// 96 60 ` ✔
// 91 5B [ ✔
// 92 5C \ ✔
// 93 5D ] ✔
// 94 5E ^ ✔
// 95 5F _ // 小程序中合法
// 96 60 ` ✔
// #endregion
// #region 123-126 count:4 ✔
// 123 7B { ✔
// 124 7C | ✔
// 125 7D } ✔
// 126 7E ~ ✔
// 123 7B { ✔
// 124 7C | ✔
// 125 7D } ✔
// 126 7E ~ ✔
// #endregion
// 15 + 7 + 6 + 4 = 32
export const SimpleMappingChars2String: MappingStringDictionary = {
Expand All @@ -150,14 +150,14 @@ export const SimpleMappingChars2String: MappingStringDictionary = {
'&': 'n',
'?': 'o',
'%': 'p',
"'": 'q',
$: 'r',
'\'': 'q',
'$': 'r',
'/': 's',
'~': 't',
'|': 'u',
'`': 'v',
'\\': 'w',
'"': 'x'
'"': 'x',
}

export const SimpleMappingChars2StringEntries = Object.entries(SimpleMappingChars2String)
37 changes: 23 additions & 14 deletions packages/escape/src/escape.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import defu from 'defu'
import { SimpleMappingChars2String } from './dic'

const MAX_ASCII_CHAR_CODE = 127

export function isAsciiNumber(code: number) {
return code >= 48 && code <= 57
}

export interface Options {
map?: Record<string, string>
ignoreHead?: boolean
}

/**
* @description 转义
* @param selectors
Expand All @@ -13,13 +20,11 @@ export function isAsciiNumber(code: number) {
*/
export function escape(
selectors: string,
options: {
map?: Record<string, string>
} = {
map: SimpleMappingChars2String
}
options?: Options,
) {
const { map = <Record<string, string>>SimpleMappingChars2String } = options
const { map, ignoreHead } = defu<Required<Options>, Options[]>(options, {
map: SimpleMappingChars2String,
})

// unicode replace
const sb = [...selectors]
Expand All @@ -33,30 +38,34 @@ export function escape(
if (isCodeExisted) {
if (code > MAX_ASCII_CHAR_CODE) {
// 'u' means 'unicode'
sb[i] = 'u' + Number(code).toString(16)
} else if (hit) {
sb[i] = `u${Number(code).toString(16)}`
}
else if (hit) {
sb[i] = hit
} else if (i === 0) {
}
else if (!ignoreHead && i === 0) {
// 首位转义逻辑
// https://www.w3.org/TR/css-syntax-3/#ident-token-diagram
if (isAsciiNumber(code)) {
// 首位假如是数字,则需要向前补位_,不然会出现
// 2xl:text-base -> .\32xlctext-base
// 导致选择器报错
// code >= 48 && code <= 57 < 10 -> 0~9
sb[i] = '_' + char
} else if (char === '-') {
sb[i] = `_${char}`
}
else if (char === '-') {
const nextChar = sb[i + 1]
if (nextChar) {
const nextCharCode = nextChar.codePointAt(0)
if (nextCharCode && isAsciiNumber(nextCharCode)) {
// 负数情况
// 首位为 - ,第二位为数字的情况
sb[i] = '_' + char
sb[i] = `_${char}`
}
} else if (nextChar === undefined) {
}
else if (nextChar === undefined) {
// 只有 -,则 - 需要转义
sb[i] = '_' + char
sb[i] = `_${char}`
}
}
}
Expand Down
Loading

0 comments on commit a4f3a94

Please sign in to comment.