Skip to content

Commit 467083a

Browse files
committed
feat: 引入 node-html-parser 替换自己处理节点查找
1 parent 4327c8d commit 467083a

15 files changed

+313
-480
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
"find-up": "^6.3.0",
8787
"lint-staged": "^13.3.0",
8888
"magic-string": "^0.30.17",
89+
"node-html-parser": "^7.0.1",
8990
"prettier": "^3.5.3",
9091
"rimraf": "^6.0.1",
9192
"simple-git-hooks": "^2.13.0",

pnpm-lock.yaml

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/node-html-parser.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { parse } from 'node-html-parser'
2+
3+
export function nodeHtmlParser(code: string, selector: string, stack?: any[]) {
4+
const results: any[] = []
5+
if (!stack || !stack.length)
6+
return results
7+
const root = parse(code)
8+
let elements: any[] = []
9+
try {
10+
elements = root.querySelectorAll(selector)
11+
}
12+
catch (error) {}
13+
14+
while (elements.length === 0 && selector.includes(':')) {
15+
// 从: 往前截取匹配因为可能有 :hover等伪类
16+
const index = selector.lastIndexOf(':')
17+
selector = selector.slice(0, index)
18+
try {
19+
elements = root.querySelectorAll(selector)
20+
}
21+
catch (error) {}
22+
}
23+
if (elements.length) {
24+
// 从 elements 的range 去匹配 stack 的 ast 节点
25+
elements.forEach((element) => {
26+
const targetNode = getMatchNode(element.range, stack)
27+
if (targetNode) {
28+
results.push(targetNode)
29+
}
30+
})
31+
}
32+
return results
33+
}
34+
35+
export function getMatchNode(
36+
range: Readonly<[number, number]>,
37+
elements: any[],
38+
): any {
39+
for (const element of elements) {
40+
const { start, end } = element.loc
41+
if (range[0] === start.offset && range[1] === end.offset) {
42+
return element
43+
}
44+
else if (element.children && element.children.length) {
45+
const match = getMatchNode(range, element.children)
46+
if (match) {
47+
return match
48+
}
49+
}
50+
}
51+
}

src/tail.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { joinWithUnderLine } from 'transform-to-unocss-core'
2+
13
export function tail(css: string) {
24
if (/not\(/.test(css))
35
return `[&:${css}]:`
@@ -47,5 +49,9 @@ export function tail(css: string) {
4749
if (['first-child', 'last-child', 'only-child'].includes(css)) {
4850
return css.split('-')[0]
4951
}
50-
return css
52+
const [first, ...rest] = css.split(':')
53+
if (rest.length)
54+
return `[&:${joinWithUnderLine(first)}]:${rest.join(':')}`
55+
56+
return `[&:${joinWithUnderLine(first)}]`
5157
}

src/transform.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
joinWithLine,
55
transformImportant,
66
trim,
7-
} from './utils'
7+
} from 'transform-to-unocss-core'
8+
import {} from './utils'
89

910
export function transform(key: string, val: string) {
1011
const [v, important] = transformImportant(val)

0 commit comments

Comments
 (0)