forked from jmrocela/munch-jquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.js
139 lines (121 loc) · 4.44 KB
/
jquery.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
/**
* munch-jquery
* http://jmrocela.github.com/munch-jquery
*
* munch-jquery is a parser for munch.js
*
* Copyright (c) 2013 John Rocela
* Licensed under the MIT license.
* https://github.com/gruntjs/grunt/blob/master/LICENSE-MIT
*/
module.exports.parser = function(js) {
var that = this;
var pass1 = js.match(/(\$|jQuery)\([\'"](.*?)[\'"]/gi);
if (pass1) {
pass1.forEach(function(selector) {
that.parseCssSelector(selector);
});
}
var pass2 = /addClass\([\'"](.*?)[\'"]/gi;
var match;
while (match = pass2.exec(js)) this.addClass(match[1]);
var pass3 = /removeClass\([\'"](.*?)[\'"]/gi;
var match;
while (match = pass3.exec(js)) this.addClass(match[1]);
var pass8 = /hasClass\([\'"](.*?)[\'"]/gi;
var match;
while (match = pass3.exec(js)) this.addClass(match[1]);
var pass9 = /toggleClass\([\'"](.*?)[\'"]/gi;
var match;
while (match = pass9.exec(js)) this.addClass(match[1]);
var pass6 = /attr\([\'"](id|class)[\'"], [\'"](.*?)[\'"]/gi;
var match;
while (match = pass6.exec(js)) {
if (match[1] == 'class') this.addClass(match[2].split(' '), 2);
if (match[1] == 'id') this.addId(match[2]);
}
}
module.exports.writer = function(js) {
var that = this;
var pass1 = js.match(/(\$|jQuery)\([\'"](.*?)[\'"]/gi);
if (pass1) {
pass1.forEach(function(selector) {
var match = null,
tid = selector.match(/#[\w\-]+/gi),
tcl = selector.match(/\.[\w\-]+/gi);
if (tid) {
for (var i in tid) {
var match = tid[i];
var id = match.replace('#', '');
if (that.ignoreIds.indexOf(id) > -1) break;
var original = selector;
selector = selector.replace(id, that.map["id"][id]);
js = js.replace(original, selector);
}
}
if (tcl) {
for (var i in tcl) {
var match = tcl[i];
var cl = match.replace('.', '');
if (that.ignoreClasses.indexOf(cl) > -1) break;
var original = selector;
selector = selector.replace(cl, that.map["class"][cl]);
js = js.replace(original, selector);
}
}
});
}
var pass2 = /addClass\([\'"](.*?)[\'"]/gi;
var match;
while (match = pass2.exec(js)) {
if (that.ignoreClasses.indexOf(match[1]) > -1) continue;
var selector = match[0].replace(match[1], that.map["class"][match[1]]);
js = js.replace(match[0], selector);
}
var pass3 = /removeClass\([\'"](.*?)[\'"]/gi;
var match;
while (match = pass3.exec(js)) {
if (that.ignoreClasses.indexOf(match[1]) > -1) continue;
var selector = match[0].replace(match[1], that.map["class"][match[1]]);
js = js.replace(match[0], selector);
}
var pass8 = /hasClass\([\'"](.*?)[\'"]/gi;
var match;
while (match = pass8.exec(js)) {
if (that.ignoreClasses.indexOf(match[1]) > -1) continue;
var selector = match[0].replace(match[1], that.map["class"][match[1]]);
js = js.replace(match[0], selector);
}
var pass9 = /toggleClass\([\'"](.*?)[\'"]/gi;
var match;
while (match = pass9.exec(js)) {
if (that.ignoreClasses.indexOf(match[1]) > -1) continue;
var selector = match[0].replace(match[1], that.map["class"][match[1]]);
js = js.replace(match[0], selector);
}
var pass6 = /attr\([\'"](id|class)[\'"], [\'"](.*?)[\'"]/gi;
var match;
while (match = pass6.exec(js)) {
if (match[1] == 'class') {
var split = match[2].split(' ');
for (var i in split) {
if (that.ignoreClasses.indexOf(split[i]) > -1) break;
var original = match[0];
match[0] = match[0].replace(split[i], that.map["class"][split[i]]);
js = js.replace(original, match[0]);
}
}
if (match[1] == 'id') {
if (that.ignoreIds.indexOf(match[2]) > -1) break;
var original = match[0];
match[0] = match[0].replace(match[2], that.map["id"][match[2]]);
js = js.replace(original, match[0]);
}
}
return js;
}
var pass = function (passes, that) {
passes.forEach(function(pass) {
that.parseCssSelector(pass);
});
}