js2 0.3.13 → 0.3.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/CHANGELOG +8 -1
  2. data/lib/js2/browser.js +193 -1
  3. data/lib/js2/js2.js +26 -27
  4. metadata +6 -6
data/CHANGELOG CHANGED
@@ -1,3 +1,10 @@
1
+ 0.3.15
2
+ * JSML: added "eval'd" attribute handling
3
+ * JSML: fixed browser js2 file
4
+
5
+ 0.3.14
6
+ * JSML: added plain content children
7
+
1
8
  0.3.13
2
9
  * fixed problem that doesn't allow toString methods
3
10
  * fixed string sanitizer problem with double quotes
@@ -22,7 +29,7 @@
22
29
  * fixed regex/divide issue
23
30
 
24
31
  0.3.8
25
- * fied "render"
32
+ * fixed "render"
26
33
  * fixed comment block bug
27
34
  * using target-dir instead of out-dir
28
35
 
data/lib/js2/browser.js CHANGED
@@ -19,7 +19,7 @@ function mainFunction (arg) {
19
19
 
20
20
  var JS2 = root.JS2 = mainFunction;
21
21
  var js2 = root.js2 = JS2;
22
- js2.VERSION = "0.3.13";
22
+ js2.VERSION = "0.3.15";
23
23
 
24
24
  JS2.ROOT = JS2;
25
25
 
@@ -270,6 +270,198 @@ JS2.Array.prototype.any = function() {
270
270
  };
271
271
 
272
272
 
273
+ JS2.Class.extend('JSML', function(KLASS, OO){
274
+ OO.addStaticMember("process",function (txt) {
275
+ return new KLASS(txt);
276
+ });
277
+
278
+ OO.addMember("initialize",function (txt) {
279
+ var lines = txt.split(/\n/);
280
+ this.root = new JS2.JSMLElement();
281
+ this.stack = [ this.root ];
282
+
283
+ for(var _i1=0,_c1=lines,_l1=_c1.length,l;(l=_c1[_i1])||(_i1<_l1);_i1++){
284
+ if (l.match(/^\s*$/)) continue;
285
+ this.processLine(l);
286
+ }
287
+
288
+ var toEval = 'function process() { var out = [];\n' + this.flatten().join('') + '\n return out.join("");\n}';
289
+ eval(toEval);
290
+
291
+ this.result = function(bound) {
292
+ bound = bound || {};
293
+ return process.call(bound);
294
+ };
295
+ });
296
+
297
+ OO.addMember("flatten",function () {
298
+ return this.root.flatten();
299
+ });
300
+
301
+ OO.addMember("processLine",function (line) {
302
+ var ele = new JS2.JSMLElement(line);
303
+ var scope = this.getScope();
304
+
305
+ if (ele.scope == scope) {
306
+ this.stack.pop();
307
+ this.getLast().push(ele);
308
+ this.stack.push(ele);
309
+ } else if (ele.scope > scope) {
310
+ this.getLast().push(ele);
311
+ this.stack.push(ele);
312
+ } else if (ele.scope < scope) {
313
+ var diff = scope - ele.scope + 1;
314
+ while(diff-- > 0) {
315
+ this.stack.pop();
316
+ }
317
+ this.getLast().push(ele);
318
+ this.stack.push(ele);
319
+ }
320
+ });
321
+
322
+
323
+ OO.addMember("getScope",function () {
324
+ return this.stack.length - 1;
325
+ });
326
+
327
+ OO.addMember("getLast",function () {
328
+ return this.stack[this.stack.length-1];
329
+ });
330
+
331
+ });
332
+
333
+ JS2.Class.extend('JSMLElement', function(KLASS, OO){
334
+ OO.addMember("SCOPE_REGEX",/^(\s*)(.*)$/);
335
+ OO.addMember("SPLIT_REGEX",/^((?:\.|\#|\%)[^=\-\s\{]*)?(\{.*\})?(=|-)?(?:\s*)(.*)$/);
336
+ OO.addMember("TOKEN_REGEX",/(\%|\#|\.)([\w-]+)/g);
337
+ OO.addMember("JS_REGEX",/^(-|=)(.*)$/g);
338
+ OO.addMember("SCOPE_OFFSET",1);
339
+
340
+ OO.addMember("initialize",function (line) {
341
+ this.children = [];
342
+
343
+ if (line == null) {
344
+ this.scope = this.SCOPE_OFFSET;
345
+ return;
346
+ }
347
+
348
+ var spaceMatch = line.match(this.SCOPE_REGEX);
349
+ this.scope = spaceMatch[1].length / 2 + this.SCOPE_OFFSET;
350
+
351
+ this.classes = [];
352
+ this.nodeID = null;
353
+
354
+ this.parse(spaceMatch[2]);
355
+ });
356
+
357
+ OO.addMember("push",function (child) {
358
+ this.children.push(child);
359
+ });
360
+
361
+ OO.addMember("parse",function (line) {
362
+ this.attributes;
363
+ this.line = line;
364
+ var self = this;
365
+
366
+ var splitted = line.match(this.SPLIT_REGEX);
367
+ var tokens = splitted[1];
368
+ var attrs = splitted[2];
369
+ var jsType = splitted[3];
370
+ var content = splitted[4];
371
+
372
+ if (tokens) {
373
+ tokens.replace(this.TOKEN_REGEX, function(match, type, name){
374
+ switch(type) {
375
+ case '%': self.nodeType = name; break;
376
+ case '.': self.classes.push(name); break;
377
+ case '#': self.nodeID = name; break;
378
+ }
379
+ return '';
380
+ });
381
+ }
382
+
383
+ if (jsType == '=') {
384
+ this.jsEQ = content;
385
+ } else if (jsType == '-') {
386
+ this.jsExec = content;
387
+ } else {
388
+ this.content = content;
389
+ }
390
+
391
+ if (attrs) {
392
+ this.attributes = attrs;
393
+ }
394
+
395
+ if (!this.nodeType && (this.classes.length || this.nodeID)) {
396
+ this.nodeType = 'div';
397
+ }
398
+ });
399
+
400
+ OO.addMember("flatten",function () {
401
+ var out = [];
402
+
403
+ for(var _i1=0,_c1=this.children,_l1=_c1.length,c;(c=_c1[_i1])||(_i1<_l1);_i1++){
404
+ var arr = c.flatten();
405
+ for(var _i2=0,_c2=arr,_l2=_c2.length,item;(item=_c2[_i2])||(_i2<_l2);_i2++){
406
+ out.push(item);
407
+ }
408
+ }
409
+
410
+ if (this.nodeType) {
411
+ this.handleJsEQ(out);
412
+ this.handleContent(out);
413
+ out.unshift('out.push("<' + this.nodeType + '"+JS2.JSMLElement.parseAttributes(' + (this.attributes || "{}") + ', ' + JSON.stringify(this.classes || []) + ', ' + JSON.stringify(this.id || null) + ')+">");\n');
414
+ out.push('out.push(' + JSON.stringify("</"+(this.nodeType)+">") + ');\n');
415
+ } else {
416
+ this.handleJsExec(out);
417
+ this.handleJsEQ(out);
418
+ this.handleContent(out);
419
+ }
420
+
421
+ return out;
422
+ });
423
+
424
+ OO.addMember("handleJsEQ",function (out) {
425
+ if (this.jsEQ) {
426
+ this.jsEQ = this.jsEQ.replace(/;\s*$/, '');
427
+ out.unshift('out.push(' + this.jsEQ + ');\n');
428
+ }
429
+ });
430
+
431
+ OO.addMember("handleContent",function (out) {
432
+ if (this.content != null && this.content.length > 0) {
433
+ out.unshift('out.push(' + JSON.stringify(this.content) + ');\n');
434
+ }
435
+ });
436
+
437
+
438
+ OO.addMember("handleJsExec",function (out) {
439
+ if (this.jsExec) {
440
+ out.unshift(this.jsExec);
441
+ if (this.jsExec.match(/\{\s*$/)) {
442
+ out.push("}\n");
443
+ }
444
+ }
445
+ });
446
+
447
+ OO.addStaticMember("parseAttributes",function (hash, classes, id) {
448
+ var out = [];
449
+ classes = classes || [];
450
+ if (hash['class']) classes.push(hash['class']);
451
+ if (classes.length) hash['class'] = classes.join(" ");
452
+
453
+ for (var k in hash) {
454
+ if (hash.hasOwnProperty(k)) {
455
+ out.push(k + '=' + JSON.stringify(hash[k]));
456
+ }
457
+ }
458
+ return (out.length ? ' ' : '') + out.join(' ');
459
+ });
460
+ });
461
+
462
+
463
+ JS2.TEMPLATES = { jsml: JS2.JSML };
464
+
273
465
 
274
466
  js2.ROOT = root;
275
467
 
data/lib/js2/js2.js CHANGED
@@ -14,7 +14,7 @@ function mainFunction (arg) {
14
14
 
15
15
  var JS2 = root.JS2 = mainFunction;
16
16
  var js2 = root.js2 = JS2;
17
- js2.VERSION = "0.3.13";
17
+ js2.VERSION = "0.3.15";
18
18
 
19
19
  JS2.ROOT = JS2;
20
20
 
@@ -1503,8 +1503,9 @@ JS2.Class.extend('JSML', function(KLASS, OO){
1503
1503
  var toEval = 'function process() { var out = [];\n' + this.flatten().join('') + '\n return out.join("");\n}';
1504
1504
  eval(toEval);
1505
1505
 
1506
- this.result = function(hash) {
1507
- return process.call(hash);
1506
+ this.result = function(bound) {
1507
+ bound = bound || {};
1508
+ return process.call(bound);
1508
1509
  };
1509
1510
  });
1510
1511
 
@@ -1546,7 +1547,7 @@ JS2.Class.extend('JSML', function(KLASS, OO){
1546
1547
 
1547
1548
  JS2.Class.extend('JSMLElement', function(KLASS, OO){
1548
1549
  OO.addMember("SCOPE_REGEX",/^(\s*)(.*)$/);
1549
- OO.addMember("SPLIT_REGEX",/^([^=-\s\{]*)(\{.*\})?(=|-)?(?:\s*)(.*)$/);
1550
+ OO.addMember("SPLIT_REGEX",/^((?:\.|\#|\%)[^=\-\s\{]*)?(\{.*\})?(=|-)?(?:\s*)(.*)$/);
1550
1551
  OO.addMember("TOKEN_REGEX",/(\%|\#|\.)([\w-]+)/g);
1551
1552
  OO.addMember("JS_REGEX",/^(-|=)(.*)$/g);
1552
1553
  OO.addMember("SCOPE_OFFSET",1);
@@ -1573,7 +1574,7 @@ JS2.Class.extend('JSMLElement', function(KLASS, OO){
1573
1574
  });
1574
1575
 
1575
1576
  OO.addMember("parse",function (line) {
1576
- this.attributes = {};
1577
+ this.attributes;
1577
1578
  this.line = line;
1578
1579
  var self = this;
1579
1580
 
@@ -1583,14 +1584,16 @@ JS2.Class.extend('JSMLElement', function(KLASS, OO){
1583
1584
  var jsType = splitted[3];
1584
1585
  var content = splitted[4];
1585
1586
 
1586
- tokens.replace(this.TOKEN_REGEX, function(match, type, name){
1587
- switch(type) {
1588
- case '%': self.nodeType = name; break;
1589
- case '.': self.classes.push(name); break;
1590
- case '#': self.nodeID = name; break;
1591
- }
1592
- return '';
1593
- });
1587
+ if (tokens) {
1588
+ tokens.replace(this.TOKEN_REGEX, function(match, type, name){
1589
+ switch(type) {
1590
+ case '%': self.nodeType = name; break;
1591
+ case '.': self.classes.push(name); break;
1592
+ case '#': self.nodeID = name; break;
1593
+ }
1594
+ return '';
1595
+ });
1596
+ }
1594
1597
 
1595
1598
  if (jsType == '=') {
1596
1599
  this.jsEQ = content;
@@ -1601,7 +1604,7 @@ JS2.Class.extend('JSMLElement', function(KLASS, OO){
1601
1604
  }
1602
1605
 
1603
1606
  if (attrs) {
1604
- eval('this.attributes = ' + attrs + ';');
1607
+ this.attributes = attrs;
1605
1608
  }
1606
1609
 
1607
1610
  if (!this.nodeType && (this.classes.length || this.nodeID)) {
@@ -1622,7 +1625,7 @@ JS2.Class.extend('JSMLElement', function(KLASS, OO){
1622
1625
  if (this.nodeType) {
1623
1626
  this.handleJsEQ(out);
1624
1627
  this.handleContent(out);
1625
- out.unshift('out.push(' + JSON.stringify("<"+(this.nodeType)+""+(this.getAttributes())+">") + ');\n');
1628
+ out.unshift('out.push("<' + this.nodeType + '"+JS2.JSMLElement.parseAttributes(' + (this.attributes || "{}") + ', ' + JSON.stringify(this.classes || []) + ', ' + JSON.stringify(this.id || null) + ')+">");\n');
1626
1629
  out.push('out.push(' + JSON.stringify("</"+(this.nodeType)+">") + ');\n');
1627
1630
  } else {
1628
1631
  this.handleJsExec(out);
@@ -1656,21 +1659,17 @@ JS2.Class.extend('JSMLElement', function(KLASS, OO){
1656
1659
  }
1657
1660
  });
1658
1661
 
1659
- OO.addMember("getAttributes",function () {
1660
- if (!this.attributes) return '';
1661
-
1662
+ OO.addStaticMember("parseAttributes",function (hash, classes, id) {
1662
1663
  var out = [];
1663
- var attrs = this.attributes;
1664
+ classes = classes || [];
1665
+ if (hash['class']) classes.push(hash['class']);
1666
+ if (classes.length) hash['class'] = classes.join(" ");
1664
1667
 
1665
- if (attrs['class']) this.classes.push(attrs['class']);
1666
- if (this.classes.length) attrs['class'] = this.classes.join(' ');
1667
-
1668
- for (var k in attrs) {
1669
- if (attrs.hasOwnProperty(k)) {
1670
- out.push(k + '=' + JSON.stringify(attrs[k]));
1668
+ for (var k in hash) {
1669
+ if (hash.hasOwnProperty(k)) {
1670
+ out.push(k + '=' + JSON.stringify(hash[k]));
1671
1671
  }
1672
- }
1673
-
1672
+ }
1674
1673
  return (out.length ? ' ' : '') + out.join(' ');
1675
1674
  });
1676
1675
  });
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: js2
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.13
5
+ version: 0.3.15
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jeff Su
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-04 00:00:00 -07:00
13
+ date: 2011-04-06 00:00:00 +08:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -36,11 +36,11 @@ extra_rdoc_files: []
36
36
  files:
37
37
  - bin/js2
38
38
  - bin/js2-ruby
39
- - lib/js2/command.rb
39
+ - lib/js2.rb
40
40
  - lib/js2/context.rb
41
- - lib/js2/fs.rb
42
41
  - lib/js2/rack.rb
43
- - lib/js2.rb
42
+ - lib/js2/command.rb
43
+ - lib/js2/fs.rb
44
44
  - lib/js2/browser.js
45
45
  - lib/js2/js2.js
46
46
  - CHANGELOG
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  requirements: []
69
69
 
70
70
  rubyforge_project:
71
- rubygems_version: 1.6.2
71
+ rubygems_version: 1.5.2
72
72
  signing_key:
73
73
  specification_version: 3
74
74
  summary: Javascript Syntactic Sugar