Skip to content

Commit

Permalink
feat: keep unitOnlyRE
Browse files Browse the repository at this point in the history
  • Loading branch information
MellowCo committed Apr 5, 2024
1 parent 4691f27 commit ff38db3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/utils/handlers/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { escapeSelector } from '@unocss/core'
import { globalKeywords } from '../mappings'
import { bracketTypeRe, numberRE, numberWithUnitRE, unitOnlyRE } from './regex'
import { bracketTypeRe, numberRE, numberWithUnitRE, unitOnlyMap, unitOnlyRE } from './regex'

// Not all, but covers most high frequency attributes
const cssProps = [
Expand Down Expand Up @@ -79,7 +79,7 @@ export function remToRpx(str: string) {
// 小程序 rpx
export function rpx(str: string) {
if (str.match(unitOnlyRE))
return `1${str}`
return `${unitOnlyMap[str]}${str}`
const match = str.match(numberWithUnitRE)
if (!match)
return
Expand All @@ -94,7 +94,7 @@ export function rpx(str: string) {

export function px(str: string) {
if (str.match(unitOnlyRE))
return `1${str}`
return `${unitOnlyMap[str]}${str}`
const match = str.match(numberWithUnitRE)
if (!match)
return
Expand Down
13 changes: 12 additions & 1 deletion src/utils/handlers/regex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
export const numberWithUnitRE = /^(-?\d*(?:\.\d+)?)(px|pt|pc|%|r?(?:em|ex|lh|cap|ch|ic)|(?:[sld]?v|cq)(?:[whib]|min|max)|in|cm|mm|rpx)?$/i
export const numberRE = /^(-?[0-9.]+)$/i
export const unitOnlyRE = /^(px)$/i
export const unitOnlyRE = /^(px|[sld]?v[wh])$/i
export const unitOnlyMap: Record<string, number> = {
px: 1,
vw: 100,
vh: 100,
svw: 100,
svh: 100,
dvw: 100,
dvh: 100,
lvh: 100,
lvw: 100,
}
export const bracketTypeRe = /^\[(color|length|size|position|quoted|string):/i
export const splitComma = /,(?![^()]*\))/g

0 comments on commit ff38db3

Please sign in to comment.