Skip to content
This repository has been archived by the owner on May 8, 2021. It is now read-only.

Commit

Permalink
Closes #47: Adds Variables to RJSS
Browse files Browse the repository at this point in the history
Such as:

    $color = '#fff';
    Window { backgroundColor: $color }

Variables must start with $, must be valid JavaScript names, and the line must end with ";".
  • Loading branch information
Dawson Toth committed Nov 4, 2013
1 parent 87f997a commit 1743cf0
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 7 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -233,6 +233,11 @@ Not Supported until Version 1.5
text: 'Establecido por el nombre de clase'
}
}
/* variables */
$background = 'white';
Window {
backgroundColor: $background
}


Here are some key features of RJSS to note--you'll want to use them:
Expand All @@ -251,6 +256,7 @@ Here are some key features of RJSS to note--you'll want to use them:
- Chain attributes for more complicated rules -- [Ti.Platform.osname="iphone"||Ti.Platform.osname="ipad"] Window { backgroundColor: '#bef'}.
- Specify multiple class names on your objects -- className: 'big_text red_text'.
- Add comments to your RJSS using /\* and \*/. Note that // comments are not supported.
- Variables must start with a $, and end with a ;. They must be valid JavaScript variable names.


RJSS COMPILATION
Expand Down
22 changes: 21 additions & 1 deletion plugins/redux/plugin.py
Expand Up @@ -28,6 +28,7 @@ def compileRJSS(path):
compileRJSS.result = ''
compileRJSS.braceDepth = 0
compileRJSS.inComment = False
compileRJSS.inVariable = False
compileRJSS.inSelector = False
compileRJSS.inAttributeBrace = False
compileRJSS.canStartSelector = True
Expand All @@ -39,6 +40,20 @@ def compileRJSS(path):
compileRJSS.inComment = False
continue

def dollar():
if (compileRJSS.braceDepth > 0):
compileRJSS.result += '$'
else:
compileRJSS.canStartSelector = False
compileRJSS.inVariable = True
compileRJSS.result += 'var $'

def semiColon():
if (compileRJSS.inVariable):
compileRJSS.canStartSelector = True
compileRJSS.inVariable = False
compileRJSS.result += ';'

def space():
compileRJSS.result += ' '

Expand All @@ -54,7 +69,10 @@ def leftBracket():
compileRJSS.result += 'if ('

def equals():
compileRJSS.result += '==' if (rjss[i - 1] != '!' and rjss[i - 1] != '<' and rjss[i - 1] != '>') else '='
if (compileRJSS.inVariable):
compileRJSS.result += ';'
else:
compileRJSS.result += '==' if (rjss[i - 1] != '!' and rjss[i - 1] != '<' and rjss[i - 1] != '>') else '='

def rightBracket():
if (compileRJSS.braceDepth > 0):
Expand Down Expand Up @@ -94,6 +112,8 @@ def default():
compileRJSS.result += rjss[i]

switch = {
'$': dollar,
';': semiColon,
' ': space,
'/': slash,
'[': leftBracket,
Expand Down
29 changes: 25 additions & 4 deletions redux.js
Expand Up @@ -109,8 +109,8 @@ function inject(context) {
parseRJSS: function(rjss) {
rjss = rjss.replace(/[\r\t\n]/g, ' ');
var result = '', braceDepth = 0;
var inComment = false, inSelector = false, inAttributeBrace = false;
var canStartSelector = true, canBeAttributeBrace = false;
var inComment = false, inSelector = false, inAttributeBrace = false, inVariable = false;
var canStartSelector = true, canBeAttributeBrace;

for (var i = 0, l = rjss.length; i < l; i++) {
if (inComment) {
Expand All @@ -120,11 +120,27 @@ function inject(context) {
continue;
}
switch (rjss[i]) {
case '$':
if (braceDepth == 0 && canStartSelector) {
canStartSelector = false;
inVariable = true;
result += 'var $';
} else {
result += '$';
}
break;
case ';':
if (inVariable) {
canStartSelector = true;
inVariable = false;
}
result += ';';
break;
case ' ':
result += ' ';
break;
case '/':
inComment = rjss[i+1] == '*';
inComment = rjss[i + 1] == '*';
result += inComment ? '' : '/';
break;
case '[':
Expand All @@ -136,7 +152,12 @@ function inject(context) {
}
break;
case '=':
result += (rjss[i - 1] != '!' && rjss[i - 1] != '<' && rjss[i - 1] != '>') ? '==' : '=';
if (!inVariable) {
result += (rjss[i - 1] != '!' && rjss[i - 1] != '<' && rjss[i - 1] != '>') ? '==' : '=';
}
else {
result += '=';
}
break;
case ']':
if (braceDepth > 0) {
Expand Down
13 changes: 11 additions & 2 deletions test/general.js
Expand Up @@ -4,7 +4,7 @@
var should = require('should'),
mockti = require('mockti');

describe('rjss', function () {
describe('redux', function () {
var redux;

before(function () {
Expand Down Expand Up @@ -88,7 +88,7 @@ describe('rjss', function () {
window.children[1].should.equal(label2);
});

it('should support styling', function () {
it('rjss should support styling', function () {
var params = {
backgroundColor: '#fff'
};
Expand All @@ -98,6 +98,15 @@ describe('rjss', function () {
window.should.have.property('backgroundColor', params.backgroundColor);
});

it('rjss should support variables', function () {
var params = { backgroundColor: '#fff' },
parsed = redux.fn.parseRJSS('$mainColor = "#fff";\nWindow { backgroundColor: $mainColor }');
eval(parsed);
Ti.UI.addEventListener('function::createWindow', curryAssertFirstParameter(params));
var window = new this.Window();
window.should.have.property('backgroundColor', params.backgroundColor);
});

/*
Utility.
*/
Expand Down

0 comments on commit 1743cf0

Please sign in to comment.