Permalink
Browse files

fixed char

  • Loading branch information...
1 parent 2efe828 commit 3e2b444933f4d16efd9722805721a2ef1795f679 @jeffsu committed Apr 5, 2011
Showing with 395 additions and 1 deletion.
  1. +196 −0 dist/browser/js2.js
  2. +196 −0 flavors/browser.js
  3. +2 −0 flavors/browser.js.erb
  4. +1 −1 src/Common/JSML.js2
View
@@ -181,6 +181,8 @@ function mainFunction (arg) {
return JS2;
})(undefined, JS2);
+ JS2.TEMPLATES = { jsml: JS2.JSML };
+
JS2.Array = function (arr) {
if (arr instanceof Array) {
this.append(arr);
@@ -270,6 +272,200 @@ JS2.Array.prototype.any = function() {
};
+ JS2.Class.extend('JSML', function(KLASS, OO){
+ OO.addStaticMember("process",function (txt) {
+ return new KLASS(txt);
+ });
+
+ OO.addMember("initialize",function (txt) {
+ var lines = txt.split(/\n/);
+ this.root = new JS2.JSMLElement();
+ this.stack = [ this.root ];
+
+ for(var _i1=0,_c1=lines,_l1=_c1.length,l;(l=_c1[_i1])||(_i1<_l1);_i1++){
+ if (l.match(/^\s*$/)) continue;
+ this.processLine(l);
+ }
+
+ var toEval = 'function process() { var out = [];\n' + this.flatten().join('') + '\n return out.join("");\n}';
+ eval(toEval);
+
+ this.result = function(bound) {
+ bound = bound || {};
+ return process.call(bound);
+ };
+ });
+
+ OO.addMember("flatten",function () {
+ return this.root.flatten();
+ });
+
+ OO.addMember("processLine",function (line) {
+ var ele = new JS2.JSMLElement(line);
+ var scope = this.getScope();
+
+ if (ele.scope == scope) {
+ this.stack.pop();
+ this.getLast().push(ele);
+ this.stack.push(ele);
+ } else if (ele.scope > scope) {
+ this.getLast().push(ele);
+ this.stack.push(ele);
+ } else if (ele.scope < scope) {
+ var diff = scope - ele.scope + 1;
+ while(diff-- > 0) {
+ this.stack.pop();
+ }
+ this.getLast().push(ele);
+ this.stack.push(ele);
+ }
+ });
+
+
+ OO.addMember("getScope",function () {
+ return this.stack.length - 1;
+ });
+
+ OO.addMember("getLast",function () {
+ return this.stack[this.stack.length-1];
+ });
+
+});
+
+JS2.Class.extend('JSMLElement', function(KLASS, OO){
+ OO.addMember("SCOPE_REGEX",/^(\s*)(.*)$/);
+ OO.addMember("SPLIT_REGEX",/^((?:\.|\#|\%)[^=-\s\{]*)?(\{.*\})?(=|-)?(?:\s*)(.*)$/);
+ OO.addMember("TOKEN_REGEX",/(\%|\#|\.)([\w-]+)/g);
+ OO.addMember("JS_REGEX",/^(-|=)(.*)$/g);
+ OO.addMember("SCOPE_OFFSET",1);
+
+ OO.addMember("initialize",function (line) {
+ this.children = [];
+
+ if (line == null) {
+ this.scope = this.SCOPE_OFFSET;
+ return;
+ }
+
+ var spaceMatch = line.match(this.SCOPE_REGEX);
+ this.scope = spaceMatch[1].length / 2 + this.SCOPE_OFFSET;
+
+ this.classes = [];
+ this.nodeID = null;
+
+ this.parse(spaceMatch[2]);
+ });
+
+ OO.addMember("push",function (child) {
+ this.children.push(child);
+ });
+
+ OO.addMember("parse",function (line) {
+ this.attributes = {};
+ this.line = line;
+ var self = this;
+
+ var splitted = line.match(this.SPLIT_REGEX);
+ var tokens = splitted[1];
+ var attrs = splitted[2];
+ var jsType = splitted[3];
+ var content = splitted[4];
+
+ if (tokens) {
+ tokens.replace(this.TOKEN_REGEX, function(match, type, name){
+ switch(type) {
+ case '%': self.nodeType = name; break;
+ case '.': self.classes.push(name); break;
+ case '#': self.nodeID = name; break;
+ }
+ return '';
+ });
+ }
+
+ if (jsType == '=') {
+ this.jsEQ = content;
+ } else if (jsType == '-') {
+ this.jsExec = content;
+ } else {
+ this.content = content;
+ }
+
+ if (attrs) {
+ eval('this.attributes = ' + attrs + ';');
+ }
+
+ if (!this.nodeType && (this.classes.length || this.nodeID)) {
+ this.nodeType = 'div';
+ }
+ });
+
+ OO.addMember("flatten",function () {
+ var out = [];
+
+ for(var _i1=0,_c1=this.children,_l1=_c1.length,c;(c=_c1[_i1])||(_i1<_l1);_i1++){
+ var arr = c.flatten();
+ for(var _i2=0,_c2=arr,_l2=_c2.length,item;(item=_c2[_i2])||(_i2<_l2);_i2++){
+ out.push(item);
+ }
+ }
+
+ if (this.nodeType) {
+ this.handleJsEQ(out);
+ this.handleContent(out);
+ out.unshift('out.push(' + JSON.stringify("<"+(this.nodeType)+""+(this.getAttributes())+">") + ');\n');
+ out.push('out.push(' + JSON.stringify("</"+(this.nodeType)+">") + ');\n');
+ } else {
+ this.handleJsExec(out);
+ this.handleJsEQ(out);
+ this.handleContent(out);
+ }
+
+ return out;
+ });
+
+ OO.addMember("handleJsEQ",function (out) {
+ if (this.jsEQ) {
+ this.jsEQ = this.jsEQ.replace(/;\s*$/, '');
+ out.unshift('out.push(' + this.jsEQ + ');\n');
+ }
+ });
+
+ OO.addMember("handleContent",function (out) {
+ if (this.content != null && this.content.length > 0) {
+ out.unshift('out.push(' + JSON.stringify(this.content) + ');\n');
+ }
+ });
+
+
+ OO.addMember("handleJsExec",function (out) {
+ if (this.jsExec) {
+ out.unshift(this.jsExec);
+ if (this.jsExec.match(/\{\s*$/)) {
+ out.push("}\n");
+ }
+ }
+ });
+
+ OO.addMember("getAttributes",function () {
+ if (!this.attributes) return '';
+
+ var out = [];
+ var attrs = this.attributes;
+
+ if (attrs['class']) this.classes.push(attrs['class']);
+ if (this.classes.length) attrs['class'] = this.classes.join(' ');
+
+ for (var k in attrs) {
+ if (attrs.hasOwnProperty(k)) {
+ out.push(k + '=' + JSON.stringify(attrs[k]));
+ }
+ }
+
+ return (out.length ? ' ' : '') + out.join(' ');
+ });
+});
+
+
js2.ROOT = root;
Oops, something went wrong.

0 comments on commit 3e2b444

Please sign in to comment.