forked from Idered/cssParentSelector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jQuery.cssParentSelector.js
148 lines (99 loc) · 5.24 KB
/
jQuery.cssParentSelector.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
141
142
143
144
145
146
147
148
/**
* jQuery cssParentSelector 1.0.9
* https://github.com/Idered/cssParentSelector
*
* Copyright 2011-2012, Kasper Mikiewicz
* Released under the MIT and GPL Licenses.
* Date 2012-02-08
*/
(function($) {
$.fn.cssParentSelector = function() {
var k = 0, i, j,
CLASS = 'CPS',
stateMap = {
checked: 'click',
focus: 'focus blur',
active: 'mousedown mouseup',
selected: 'change',
changed: 'change'
},
attachStateMap = {
mousedown: 'mouseout'
},
detachStateMap = {
mouseup: 'mouseout'
},
pseudoMap = {
'after': 'appendTo',
'before': 'prependTo'
},
pseudo = {},
parsed, matches, selectors, selector,
parent, target, child, state, declarations,
pseudoParent, pseudoTarget,
REGEX = /[\w\s\"\'\.\[\]\(\)\=\*\-\:#]*(?=!)[\w\s\.\,\[\]\(\)\=\*\-\:#>!]+[\w\s\"\'\.\,\[\]\=\:\-#>]*\{{1}[\.\*\/\?\:\^\+\\\=\|\w\s\'-;#%]+\}{1}/gi,
parse = function(css) {
// Remove comments.
css = css.replace(/(\/\*([\s\S]*?)\*\/)/gm, '');
if ( matches = css.match(REGEX) ) {
parsed = '';
for (i = -1; matches[++i], style = matches[i];) {
// E! P > F, E F { declarations } => E! P > F, E F
selectors = style.split('{')[0].split(',');
// E! P > F { declarations } => declarations
declarations = '{' + style.split(/\{|\}/)[1].replace(/^\s+|\s+$[\t\n\r]*/g, '') + '}';
// There's nothing so we can skip this one.
if ( declarations === '{}' ) continue;
declarations = declarations.replace(/;/g, ' !important;');
for (j = -1; selectors[++j], selector = $.trim(selectors[j]);) {
j && (parsed += ',');
if (/!/.test(selector) ) {
// E! P > F => E
parent = $.trim(selector.split('!')[0].split(':')[0]);
// E! P > F => P
target = $.trim(selector.split('!')[1].split('>')[0].split(':')[0]) || []._;
// E:after! P > after
pseudoParent = $.trim(selector.split('>')[0].split('!')[0].split(':')[1]) || []._;
// E! P:after > after
pseudoTarget = target ? ($.trim(selector.split('>')[0].split('!')[1].split(':')[1]) || []._) : []._;
// E! P > F => F
child = $($.trim(selector.split('>')[1]).split(':')[0]);
// E! P > F:state => state
state = (selector.split('>')[1].split(/:+/)[1] || '').split(' ')[0] || []._;
child.each(function(i) {
var subject = $(this).parent(parent);
pseudoParent && (subject = pseudoMap[pseudoParent] ?
$('<div></div>')[pseudoMap[pseudoParent]](subject) :
subject.filter(':' + pseudoParent));
target && (subject = subject.find(target));
target && pseudoTarget && (subject = pseudoMap[pseudoTarget] ?
$('<div></div>')[pseudoMap[pseudoTarget]](subject) :
subject.filter(':' + pseudoTarget));
var id = CLASS + k++,
toggleFn = function(e) {
e && attachStateMap[e.type] &&
$(subject).one(attachStateMap[e.type], function() {$(subject).toggleClass(id) });
e && detachStateMap[e.type] &&
$(subject).off(detachStateMap[e.type]);
$(subject).toggleClass(id)
};
i && (parsed += ',');
parsed += '.' + id;
! state ? toggleFn() : $(this).on( stateMap[state] || state , toggleFn );
});
} else {
parsed += selector;
}
}
parsed += declarations;
};
$('<style type="text/css">' + parsed + '</style>').appendTo('head');
};
};
$('link[rel=stylesheet], style').each(function() {
$(this).is('link') ?
$.get(this.href).success(function(css) { parse(css); }) : parse($(this).text());
});
};
$().cssParentSelector();
})(jQuery);