Skip to content

Commit 2b2f56a

Browse files
author
mic
committedMar 5, 2010
move from function to object
1 parent d52e03b commit 2b2f56a

File tree

3 files changed

+54
-48
lines changed

3 files changed

+54
-48
lines changed
 

‎demo.html

+11-14
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,32 @@
66
<title>jstyle</title>
77
<script src="jstyle.js"></script>
88
<script>
9-
jstyle().addStyle({
10-
/*
11-
* Variables
12-
*/
13-
// assign a string value
14-
$main:'#0C0',
15-
$space:'2em',
16-
9+
jstyle.setVars({
10+
/*
11+
* Variables
12+
*/
13+
main:'#0C0',
14+
space:'2em',
1715
// assign a value by a function
18-
$mainLight:function(css){
16+
mainLight:function(){
1917
// css is a reference to this json itself
20-
return css['$main'].replace(/0/g, '8');
21-
},
22-
18+
return this.vars.main.replace(/0/g, '8');
19+
}
20+
}).addStyle({
2321
/*
2422
* Style definition, css like but in JSON:
2523
*
2624
* with commas "," instead of semi-colons ";"
2725
* and some reserved js words can't be used as a property, i.e: "float" as below
2826
* to overcome this, place the property name between quotes/double quotes
2927
*/
30-
3128
'body':{
3229
background:'#121',
3330
color:'$main'
3431
},
3532
'.hello':{
3633
border:'solid 1px $mainLight',
37-
background:'#040',
34+
background:'#040', //normal assignment (no variable)
3835
'float':'left',
3936
padding:'$space',
4037
margin:'$space'

‎jstyle.js

+27-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* JSTYLE - MIT License - (c) 2010 BeeBole.com - rev 1.0 */
2-
window.jstyle = function(arg){
2+
window.jstyle = (function(arg){
33
var vars = {},
44
agent = navigator.userAgent.toLowerCase(),
55
browsers = {
@@ -8,31 +8,25 @@ window.jstyle = function(arg){
88
'Opera':agent.indexOf('opera') > -1,
99
'webkit':agent.indexOf('webkit') > -1
1010
},
11-
clean = function(s){
12-
var t = s.replace(/^\s\s*/, ''),
13-
w = /\s/,
14-
l = t.length;
15-
while(w.test(t.charAt(l--)));
16-
return t.slice(0, l + 1).replace(/\s*\n\s*/g, '').replace(/\t/g,'').replace(/\/\*[^\*]+\*\//m, '');
17-
},
1811
json2css = function(json){
19-
var parseJSON = function(sub){
20-
var a = [], prop, subProp, varCall = /(\$\S+)/, varVal;
12+
var jstyle = this,
13+
parseJSON = function(sub){
14+
var a = [],
15+
prop, subProp,
16+
varCall = /(\$(\S+))/, varVal, vars = jstyle.vars;
2117
for(prop in sub){
2218
subProp = sub[prop];
23-
subProp = typeof subProp === 'function' ? subProp(json) : subProp;
24-
if(prop[0] === '$'){
25-
vars[prop] = subProp;
19+
if(typeof subProp === 'object'){
20+
a.push(prop + '{' + parseJSON(subProp) + '}');
2621
}else{
27-
if(typeof subProp === 'object'){
28-
a.push(prop + '{' + parseJSON(subProp) + '}');
29-
}else{
30-
if((varCall).test(subProp)){
31-
varVal = vars[subProp.match(varCall)[0]];
22+
if((varCall).test(subProp)){
23+
varVal = vars[subProp.match(varCall)[2]];
24+
varVal = typeof varVal === 'function' ? varVal.call(jstyle, json) : varVal;
25+
if(subProp){
3226
subProp = subProp.replace(varCall, varVal);
3327
}
34-
a.push(prop + ':' + subProp + ';');
3528
}
29+
a.push(prop + ':' + subProp + ';');
3630
}
3731
}
3832
return a.join('');
@@ -58,7 +52,7 @@ window.jstyle = function(arg){
5852
browsers:browsers,
5953
json:{},
6054
styleNode:{},
61-
jstyle:arguments.callee,
55+
vars:{},
6256
addMethod:function(name, fn){
6357
this[name] = fn;
6458
return this;
@@ -81,7 +75,7 @@ window.jstyle = function(arg){
8175
styleNode = document.createElement('STYLE');
8276
styleNode.setAttribute('type', 'text/css');
8377
add2head(styleNode);
84-
this.css = json2css(this.json);
78+
this.css = json2css.call(this, this.json);
8579
styleNode.styleSheet ?
8680
styleNode.styleSheet.cssText = this.css:
8781
styleNode.appendChild( document.createTextNode(this.css) );
@@ -111,6 +105,17 @@ window.jstyle = function(arg){
111105
this.json[rule] && delete this.json[rule];
112106
}
113107
return this;
108+
},
109+
setVars:function(varHash){
110+
for(v in varHash){
111+
if(this.vars[v]){
112+
alert('The name ' + v + ' is already in use(' + this.vars[v] + '), please choose another one');
113+
break;
114+
}else{
115+
this.vars[v] = varHash[v];
116+
}
117+
}
118+
return this;
114119
}
115120
};
116-
};
121+
})();

‎jstylePlugins.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
jstyle()
2-
3-
.addMethod('css2json', function(css){
1+
jstyle.css2json = function(css){
42
var css2 = css || this.css,
53
json = {},
6-
s, e, k, v, i, ii, props;
4+
s, e, k, v, i, ii, props,
5+
clean = function(s){
6+
var t = s.replace(/^\s\s*/, ''),
7+
w = /\s/,
8+
l = t.length;
9+
while(w.test(t.charAt(l--)));
10+
return t.slice(0, l + 1).replace(/\s*\n\s*/g, '').replace(/\t/g,'').replace(/\/\*[^\*]+\*\//m, '');
11+
};
712
while(css2){
813
s = css2.indexOf('{');
914
e = css2.indexOf('}', s);
@@ -28,15 +33,14 @@ jstyle()
2833
};
2934
this.json = json;
3035
return this;
31-
})
36+
};
3237

33-
.addMethod('dom2css', function(id){
34-
id = id || domId;
35-
var styleNodes = id ? [document.getElementById(id)]:document.getElementsByTagName('STYLE'),
36-
i = 0, ii = styleNodes.length;
37-
for(;i < ii; i++){
38+
jstyle.dom2css = function(){
39+
var styleNodes = document.getElementsByTagName('STYLE'),
40+
i = 0, ii = styleNodes.length, css;
41+
do{
3842
css += styleNodes[i].styleSheet ? styleNodes[i].styleSheet.cssText : styleNodes[i].innerHTML;
39-
}
43+
}while(++i < ii)
4044
this.css = css;
4145
return this;
42-
});
46+
};

0 commit comments

Comments
 (0)
Please sign in to comment.