-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
40 lines (33 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
var assert = require('assert')
/* eslint-disable no-useless-escape */
var regex = /([\.#]?[^\s#.]+)/
/* eslint-enable no-useless-escape */
module.exports = parseClass
// Our minimal parser doesn’t understand escaping CSS special
// characters like `#`. Don’t use them. More reading:
// https://mathiasbynens.be/notes/css-escapes
function parseClass (str) {
assert.equal(typeof str, 'string', 'parse-class: str should be type string')
assert.ok(str.length, 'parse-class: str should be length > 0')
var arr = str.split(regex)
var res = {}
if (!/^\.|#/.test(arr[1])) {
res.name = arr[1]
}
if (arr.length < 4) return res
for (var i = 3, len = arr.length; i < len; i += 2) {
var raw = arr[i]
// strip the leading char, keep the value
var value = raw.substring(1, raw.length)
if (raw[0] === '.') {
res.class = (res.class)
? (res.class + ' ' + value)
: value
} else if (raw[0] === '#') {
res.id = (res.id)
? (res.id + ' ' + value)
: value
}
}
return res
}