forked from kajic/jquery-injectCSS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.injectCSS.js
178 lines (157 loc) · 6.01 KB
/
jquery.injectCSS.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* jquery.injectCSS.js - jquery css injection plugin
* Copyright (C) 2013, Robert Kajic (robert@kajic.com)
* http://kajic.com
*
* https://github.com/kajic/jquery-injectCSS
* Allows for injection of CSS defined as javascript JSS objects.
*
* Based on JSS (http://jss-lang.org/).
*
* Licensed under the MIT License.
*
* Date: 2013-01-08
* Version: 0.1
*/
(function (jQuery) {
'use strict';
function toCSS(jss, options) {
function jsonToCSS(scope, css) {
if (scope && !result[scope]) {
result[scope] = {};
}
for (var property in css) {
var value = css[property];
if (value instanceof Array) {
var values = value;
for (var i = 0; i < values.length; i++) {
addProperty(scope, property, values[i]);
}
}
else {
switch (typeof(value)) {
case "number":
case "string":
addProperty(scope, property, value);
break;
case "object":
var endChar = property.charAt(property.length - 1);
if (scope && (endChar === "_" || endChar === "-")) {
var variants = value;
for (var key in variants) {
// key may be a comma separted list
var list = key.split(/\s*,\s*/);
for (var j = 0; j < list.length; j++) {
var valueVariant = variants[key];
if (valueVariant instanceof Array) {
var valuesVariant = valueVariant;
for (var k = 0; k < valuesVariant.length; k++) {
addProperty(scope, property + list[j], valuesVariant[k]);
}
}
else {
addProperty(scope, property + list[j], variants[key]);
}
}
}
}
else {
jsonToCSS(makeSelectorName(scope, property), value);
}
break;
}
}
}
}
function makePropertyName(n) {
return n.replace(/_/g, "-");
}
function makeSelectorName(scope, name) {
var snames = [];
var names = name.split(/\s*,\s*/);
var scopes = scope.split(/\s*,\s*/);
for (var s = 0; s < scopes.length; s++) {
var currentScope = scopes[s];
for (var i = 0; i < names.length; i++) {
var currentName = names[i];
if (currentName.charAt(0) === "&") {
snames.push(currentScope + currentName.substr(1));
} else {
snames.push(currentScope ? currentScope + " " + currentName : currentName);
}
}
}
return snames.join(", ");
}
function addProperty(scope, property, value) {
if (typeof(value) === "number" && !options.useRawValues) {
value = value + "px";
}
var properties = property.split(/\s*,\s*/);
for (var i = 0; i < properties.length; i++) {
var currentProperty = makePropertyName(properties[i]);
if (result[scope][currentProperty]) {
result[scope][currentProperty].push(value);
} else {
result[scope][currentProperty] = [value];
}
}
}
// --------------
var result = {};
if (typeof(jss) === "string") {
// evaluate the JSS object:
try {
eval("var jss = {" + jss + "}");
}
catch (e) {
return "/*\nUnable to parse JSS: " + e + "\n*/";
}
}
jsonToCSS("", jss);
// output result:
var ret = "";
for (var a in result) {
var css = result[a];
ret += a + " {\n";
for (var i in css) {
var values = css[i]; // this is an array !
for (var j = 0; j < values.length; j++) {
ret += "\t" + i + ": " + values[j] + ";\n";
}
}
ret += "}\n";
}
return ret;
}
var defaults = {
truncateFirst: false,
container: null,
containerName: "injectCSSContainer",
useRawValues: false
};
jQuery.injectCSS = function (jss, options) {
options = jQuery.extend({}, defaults, options);
options.media = options.media || 'all';
var container = (options.container && jQuery(options.container)) || jQuery("#" + options.containerName);
if (!container.length) {
container = jQuery("<style></style>").appendTo('head').attr({
media: options.media,
id: options.containerName,
type: 'text/css'
});
}
var css = "";
if (!options.truncateFirst) {
css += container.text();
}
css += toCSS(jss, options);
var containerDomElem = container[0];
if (containerDomElem.styleSheet !== undefined && containerDomElem.styleSheet.cssText !== undefined) { // IE
containerDomElem.styleSheet.cssText = css;
} else {
container.text(css); //Others
}
return container;
};
}(jQuery));