forked from fb55/css-what
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringify.js
55 lines (46 loc) · 1.5 KB
/
stringify.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
54
55
var actionTypes = {
"equals": "",
"element": "~",
"start": "^",
"end": "$",
"any": "*",
"not": "!",
"hyphen": "|"
};
var simpleSelectors = {
__proto__: null,
child: " > ",
parent: " < ",
sibling: " ~ ",
adjacent: " + ",
descendant: " ",
universal: "*"
};
module.exports = stringify;
function stringify(token){
return token.map(stringifySubselector).join(", ");
}
function stringifySubselector(token){
return token.map(stringifyToken).join("");
}
function stringifyToken(token){
if(token.type in simpleSelectors) return simpleSelectors[token.type];
if(token.type === "tag") return escapeName(token.name);
if(token.type === "attribute"){
if(token.action === "exists") return "[" + escapeName(token.name) + "]";
if(token.name === "id" && token.action === "equals" && !token.ignoreCase) return "#" + escapeName(token.value);
if(token.name === "class" && token.action === "element" && !token.ignoreCase) return "." + escapeName(token.value);
return "[" +
escapeName(token.name) + actionTypes[token.action] + "='" +
escapeName(token.value) + "'" + (token.ignoreCase ? "i" : "") + "]";
}
if(token.type === "pseudo"){
if(token.data === null) return ":" + escapeName(token.name);
if(typeof token.data === "string") return ":" + escapeName(token.name) + "(" + token.data + ")";
return ":" + escapeName(token.name) + "(" + stringify(token.data) + ")";
}
}
function escapeName(str){
//TODO
return str;
}