Skip to content

Commit a0aa363

Browse files
committed
added basic stringifier
lacks several features, useful for debugging, though
1 parent 3081eb8 commit a0aa363

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

stringify.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
var actionTypes = {
2+
"equals": "",
3+
"element": "~",
4+
"start": "^",
5+
"end": "$",
6+
"any": "*",
7+
"not": "!",
8+
"hyphen": "|"
9+
};
10+
11+
var simpleSelectors = {
12+
__proto__: null,
13+
child: " > ",
14+
parent: " < ",
15+
sibling: " ~ ",
16+
adjacent: " + ",
17+
descendant: " ",
18+
universal: "*"
19+
};
20+
21+
module.exports = stringify;
22+
23+
function stringify(token){
24+
return token.map(stringifySubselector).join(", ");
25+
}
26+
27+
function stringifySubselector(token){
28+
return token.map(stringifyToken).join("");
29+
}
30+
31+
function stringifyToken(token){
32+
if(token.type in simpleSelectors) return simpleSelectors[token.type];
33+
34+
if(token.type === "tag") return escapeName(token.name);
35+
36+
if(token.type === "attribute"){
37+
if(token.action === "exists") return "[" + escapeName(token.name) + "]";
38+
if(token.name === "id" && token.action === "equals" && !token.ignoreCase) return "#" + escapeName(token.value);
39+
if(token.name === "class" && token.action === "element" && !token.ignoreCase) return "." + escapeName(token.value);
40+
return "[" +
41+
escapeName(token.name) + actionTypes[token.action] + "='" +
42+
escapeName(token.value) + "'" + (token.ignoreCase ? "i" : "") + "]";
43+
}
44+
45+
if(token.type === "pseudo"){
46+
if(token.data === null) return ":" + escapeName(token.name);
47+
if(typeof token.data === "string") return ":" + escapeName(token.name) + "(" + token.data + ")";
48+
return ":" + escapeName(token.name) + "(" + stringify(token.data) + ")";
49+
}
50+
}
51+
52+
function escapeName(str){
53+
//TODO
54+
return str;
55+
}

0 commit comments

Comments
 (0)