-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.cssp.js
More file actions
74 lines (65 loc) · 1.72 KB
/
Copy pathjquery.cssp.js
File metadata and controls
74 lines (65 loc) · 1.72 KB
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
/*!
* jQuery cssP Plugin - https://github.com/ReallyGood/jQuery-cssP
* v0.0.2, MIT licensed
* by Really Good: http://reallygoodteam.com
*/
(function($){
$.cssP = (function(){
var cache = {},
methods = {};
$.extend(methods, {
set: function(el, changes){
var s = el.selector;
if(!s) return;
styleTag = cache[s] || $('<style></style>').appendTo('body');
cache[s] = styleTag;
var stylesheet = '';
$.each(changes, function(key, ruleset){
var newRuleset,
declaration,
skip;
if(typeof ruleset === 'function') {
// let's do our best to get a string back
try {
newRuleset = ruleset();
if(typeof newRuleset != 'string') skip = true;
} catch(e){
throw Error('The function failed:', e);
skip = true;
}
} else if(typeof ruleset == 'string') {
// yay, an actual string!
newRuleset = ruleset;
}
if(!skip) {
declaration = s + ':' + key + '{' + newRuleset + '}';
stylesheet += declaration + '\r';
}
});
methods.updateSS(styleTag, stylesheet);
},
updateSS: function(tag, sheet){
$(tag).text(sheet);
},
get: function(el, pseudo, getFull){
if(!cache[el.selector]) return null;
var sheet = cache[el.selector].text();
if(!getFull) {
sheet = sheet.split(':' + pseudo)[1];
if(!sheet) return null;
sheet = sheet.split('}')[0].slice(1);
}
return sheet;
}
});
$.fn.cssP = function(data, getFull){
var $this = $(this);
if(typeof data === 'object') {
methods.set($this, data);
return $this;
} else if(data == 'before' || data == 'after'){
return methods.get($this, data, getFull);
}
};
})();
})(jQuery);