-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
140 lines (99 loc) · 2.82 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'use strict';
var parse = require('slick').parse;
/* Slick.parse returns: https://github.com/kamicane/slick#format */
module.exports = selector2tag;
function selector2tag(raw) {
return new ParsedSelector(raw);
}
function ParsedSelector(raw) {
var selectorObj = parse(raw);
var oneSelector = (selectorObj.length === 1) && (selectorObj[0].length === 1);
if (!oneSelector) { // todo: Rework to handle multi-tag selectors?
throw new Error('selector2tag only handles one-tag selectors.');
}
this.selectorObj = selectorObj;
}
Object.defineProperties(ParsedSelector.prototype, {
'tagName': { get: getTagName },
'id': { get: getId },
'classNames': { get: getClassNames },
'miscAttrs': { get: getMiscAttrs },
'openingTag': { get: getOpeningTag },
'closingTag': { get: getClosingTag },
'toString': {
value: function parsedSelectorToString() {
return this.selectorObj.raw;
}
},
});
function getTagName() {
var tag = this.selectorObj[0][0].tag;
if (tag === '*') tag = 'div';
return tag;
}
function getId() {
var id = this.selectorObj[0][0].id;
if (id === undefined) return '';
return id;
}
function getClassNames() {
var classList = this.selectorObj[0][0].classList;
if (classList === undefined) return '';
return classList.join(' ');
}
function getMiscAttrs() {
var selAttrs = this.selectorObj[0][0].attributes;
if (selAttrs === undefined) return '';
var attrs = {};
/* Find the attribute rules specified */
selAttrs.forEach(function(v) {
var attr = attrs[v.escapedName] || (attrs[v.escapedName] = {});
switch (v.operator) {
case '=':
attr.value = v.escapedValue;
break;
case '^=': /* Starts with value */
case '|=': /* Start with value or value- */
attr.starts = v.escapedValue;
break;
case '~=': /* Includes value (space-seperated) */
case '*=': /* Includes value (with or without spaces) */
attr.has = attr.has || '';
attr.has += ' '+v.escapedValue+' ';
break;
case '$=': /* Ends with value */
attr.ends = v.escapedValue;
break;
default: /* [attr] */
attrs[v.escapedName] = Object.keys(attr) === 0
? attr
: null;
}
});
var result = '';
/* Build attributes which match those rules */
for (var attrName in attrs) {
var attr = attrs[attrName];
if (attr === null) {
result += ' ' + attrName;
continue;
}
attr.starts = attr.starts || '';
attr.has = attr.has || '';
attr.ends = attr.ends || '';
attr.value = attr.value || (attr.starts + attr.has + attr.ends);
result += ' ' + attrName + '="' + attr.value + '"';
}
return result;
}
function getOpeningTag() {
var result = '<' + this.tagName;
if (this.id) result += ' id="' + this.id + '"';
if (this.classNames) result += ' class="' + this.classNames + '"';
result += this.miscAttrs;
result += '>';
return result;
}
function getClosingTag() {
return '</' + this.tagName + '>';
}