-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
53 lines (45 loc) · 1.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict'
module.exports = parse
var dot = '.'.charCodeAt(0)
var hash = '#'.charCodeAt(0)
/* Parse a simple CSS selector into a HAST node. */
function parse(selector, defaultTagName) {
var value = selector || ''
var name = defaultTagName || 'div'
var props = {}
var index = -1
var length = value.length
var className
var type
var code
var subvalue
var lastIndex
while (++index <= length) {
code = value.charCodeAt(index)
if (!code || code === dot || code === hash) {
subvalue = value.slice(lastIndex, index)
if (subvalue) {
if (type === dot) {
if (className) {
className.push(subvalue)
} else {
className = [subvalue]
props.className = className
}
} else if (type === hash) {
props.id = subvalue
} else {
name = subvalue
}
}
lastIndex = index + 1
type = code
}
}
return {
type: 'element',
tagName: name,
properties: props,
children: []
}
}