Skip to content

Commit 70896d7

Browse files
committed
feat: commit util and new htmlHandler
1 parent b1955e3 commit 70896d7

File tree

9 files changed

+28
-28
lines changed

9 files changed

+28
-28
lines changed

packages/core/src/html/index.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
import { parse, serialize } from 'parse5'
22
import { traverse } from '@parse5/tools'
33
import { IHtmlHandlerOptions } from '../types'
4-
4+
import { splitCode } from '../split'
5+
import { makeRegex } from '../regex'
56
export function htmlHandler(rawSource: string, options: IHtmlHandlerOptions) {
67
const { runtimeSet, classGenerator } = options
78
const fragment = parse(rawSource)
89
traverse(fragment, {
910
element(node, parent) {
1011
const attr = node.attrs.find((x) => x.name === 'class')
1112
if (attr) {
12-
const arr = attr.value.split(/\s/).filter((x) => x)
13-
attr.value = arr
14-
.map((x) => {
15-
if (runtimeSet.has(x)) {
16-
return classGenerator.generateClassName(x).name
17-
}
18-
return x
19-
})
20-
.join(' ')
13+
const arr = splitCode(attr.value, {
14+
splitQuote: false
15+
})
16+
for (let i = 0; i < arr.length; i++) {
17+
const v = arr[i]
18+
if (runtimeSet.has(v)) {
19+
attr.value = attr.value.replace(makeRegex(v), classGenerator.generateClassName(v).name)
20+
}
21+
}
2122
}
2223
}
2324
})

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './css'
22
export * from './html'
33
export * from './js'
44
export { default as ClassGenerator } from './classGenerator'
5+
export * from './types'

packages/core/src/js/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@ import type { StringLiteral, TemplateElement, CallExpression } from '@babel/type
22
import * as t from '@babel/types'
33
import { transformSync, type BabelFileResult, type NodePath } from '@babel/core'
44
import type { IJsHandlerOptions } from '../types'
5-
import { escapeStringRegexp } from '../utils'
6-
import { splitCode } from './split'
7-
8-
export function makeRegex(str: string) {
9-
return new RegExp('(?<=^|[\\s"])' + escapeStringRegexp(str), 'g')
10-
}
5+
import { makeRegex } from '../regex'
6+
import { splitCode } from '../split'
117

128
export function handleValue(str: string, node: StringLiteral | TemplateElement, options: IJsHandlerOptions) {
139
const { runtimeSet: set, classGenerator: clsGen, splitQuote = true } = options

packages/core/src/regex.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function escapeStringRegexp(str: string) {
2+
if (typeof str !== 'string') {
3+
throw new TypeError('Expected a string')
4+
}
5+
return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d')
6+
}
7+
8+
export function makeRegex(str: string) {
9+
return new RegExp('(?<=^|[\\s"])' + escapeStringRegexp(str), 'g')
10+
}
File renamed without changes.

packages/core/src/utils.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,3 @@ export function regExpTest(arr: (string | RegExp)[] = [], str: string) {
121121
}
122122
throw new TypeError("paramater 'arr' should be a Array of Regexp | String !")
123123
}
124-
125-
export function escapeStringRegexp(str: string) {
126-
if (typeof str !== 'string') {
127-
throw new TypeError('Expected a string')
128-
}
129-
return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d')
130-
}

packages/core/test/exports.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { ClassGenerator, cssHandler, handleValue, htmlHandler, jsHandler, makeRegex } from '../'
1+
import { ClassGenerator, cssHandler, handleValue, htmlHandler, jsHandler } from '../'
22

33
describe('exports', () => {
44
it('exports should be defined', () => {
5-
;[ClassGenerator, cssHandler, handleValue, htmlHandler, jsHandler, makeRegex].forEach((x) => {
5+
;[ClassGenerator, cssHandler, handleValue, htmlHandler, jsHandler].forEach((x) => {
66
expect(x).toBeDefined()
77
})
88
})

packages/core/test/reg.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// import { escapeStringRegexp } from '../src/utils'
2-
import { makeRegex } from '../src/js'
1+
import { makeRegex } from '../src/regex'
32

43
describe('regex', () => {
54
it('z-10 regex', () => {

packages/core/test/split.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { splitCode } from '../src/js/split'
1+
import { splitCode } from '../src/split'
22

33
describe('split code', () => {
44
it('split vue static str', () => {

0 commit comments

Comments
 (0)