Comparing version 2.0.0 to 3.0.0
177
index.js
var assert = require('assert'); | ||
var React = require('react'); | ||
var React = require('react'), | ||
parseful = require('parseful'); | ||
function NOOP () { /* NOOP */ } | ||
var p = parseful.define('cssSelector', function (c, $, _) { | ||
var a = c.action, | ||
chr = function (e) { | ||
return c.satisfy(function (c) { | ||
return c === e; | ||
}); | ||
}, | ||
letter = c.satisfy(function (c) { | ||
return /^[a-zA-Z]$/.test(c); | ||
}), | ||
digit = c.satisfy(function (c) { | ||
return /^[0-9]$/.test(c); | ||
}), | ||
optional = function (p) { | ||
return c.choice([p, parseful.Unit]); | ||
}; | ||
return { | ||
'cssIdent': a($(a(optional(chr('-')), function (r) { return r === parseful.Unit ? '' : r; }), | ||
a(c.many1(c.choice([chr('_'), letter])), function (list) { return list.join(''); }), | ||
a(c.many(c.choice([chr('_'), letter, digit, chr('-')])), function (list) { return list.join(''); })), | ||
function (list) { return list.join(''); }), | ||
'class': $(chr('.'), _('cssIdent')), | ||
'id': $(chr('#'), _('cssIdent')), | ||
'cssSelector': a(c.choice([ | ||
a($(c.many(_('class')), | ||
a(_('id'), function (id) { return [id]; }), | ||
c.many(_('class'))), | ||
function (list) { | ||
return list.reduce(function (prev, curr) { return prev.concat(curr); }, []); | ||
}), | ||
c.many1(_('class')) | ||
]), function (list) { | ||
return list.reduce(function (r, selector) { | ||
switch (selector[0]) { | ||
case '.': | ||
if (!r.hasOwnProperty('classes')) { | ||
r.classes = {}; | ||
} | ||
r.classes[selector[1]] = true; | ||
break; | ||
case '#': | ||
r.id = selector[1]; | ||
break; | ||
} | ||
return r; | ||
}, {}); | ||
}) | ||
}; | ||
}); | ||
function parseCssSelector (input) { | ||
var s = new parseful.State(0, input.split('')), | ||
r = p(s); | ||
if (s.peek === parseful.Nothing) { | ||
return r; | ||
} else { | ||
return null; | ||
} | ||
} | ||
function Context (params) { | ||
@@ -13,16 +75,67 @@ this.params = params; | ||
Context.prototype.createElement = function createElement (tag, attr_, children_) { | ||
var elem = React.createElement(tag); | ||
this.current.push(elem); | ||
Context.prototype.createElement = function createElementWrapper () { | ||
var alreadyCreated = false; | ||
var opts = function (attr, children) { | ||
if (typeof attr !== 'object') { | ||
children = attr; | ||
attr = {}; | ||
} | ||
if (typeof children !== 'function' && typeof children !== 'string') { | ||
function createElement (tag, selectorStr, attr, children) { | ||
var selector = null; | ||
switch (arguments.length) { | ||
case 4: | ||
selector = parseCssSelector(selectorStr); | ||
break; | ||
case 3: | ||
selector = parseCssSelector(tag); | ||
if (selector === null) { | ||
if (typeof selectorStr === 'string') { | ||
children = NOOP; | ||
selector = parseCssSelector(selectorStr); | ||
} else { | ||
children = attr; | ||
attr = selectorStr; | ||
} | ||
} else { | ||
children = attr; | ||
attr = selector; | ||
tag = 'div'; | ||
} | ||
break; | ||
case 2: | ||
if (typeof tag === 'string') { | ||
if (typeof selectorStr === 'object') { | ||
children = NOOP; | ||
attr = selectorStr; | ||
} else { | ||
if (typeof selectorStr === 'function') { | ||
children = selectorStr; | ||
attr = {}; | ||
} else { | ||
selector = parseCssSelector(selectorStr); | ||
if (selector === null) { | ||
attr = {}; | ||
} else { | ||
attr = {}; | ||
children = NOOP; | ||
} | ||
} | ||
} | ||
} else { | ||
children = selectorStr; | ||
attr = null; | ||
selector = null; | ||
} | ||
break; | ||
case 1: | ||
attr = {}; | ||
children = NOOP; | ||
break; | ||
} | ||
this.current.pop(); | ||
assert(typeof tag === 'string'); | ||
assert(typeof selector === 'object'); | ||
assert(typeof attr === 'object'); | ||
assert(typeof children === 'function' || typeof children === 'string'); | ||
if (alreadyCreated) { | ||
this.current.pop(); | ||
} | ||
alreadyCreated = true; | ||
@@ -34,2 +147,34 @@ if (this.stack.length === 0 && this.current.length > 0) { | ||
var className = {}; | ||
if (attr.hasOwnProperty('className')) { | ||
if (typeof attr.className === 'string') { | ||
attr.className.split(/\s+/).forEach(function (name) { | ||
className[name] = true; | ||
}); | ||
} else { | ||
Object.keys(attr.className).forEach(function (name) { | ||
if (attr.claasName[name]) { | ||
className[name] = true; | ||
} | ||
}); | ||
} | ||
} | ||
if (selector !== null) { | ||
if (selector.hasOwnProperty('id') && !attr.hasOwnProperty('id')) { | ||
attr.id = selector.id; | ||
} | ||
if (selector.hasOwnProperty('classes')) { | ||
Object.keys(selector.classes).forEach(function (name) { | ||
if (selector.classes[name] && !className.hasOwnProperty(name)) { | ||
className[name] = true; | ||
} | ||
}); | ||
} | ||
} | ||
if (Object.keys(className).length > 0) { | ||
attr.className = Object.keys(className).join(' '); | ||
} | ||
this.stack.push(this.current); | ||
@@ -53,9 +198,7 @@ this.current = []; | ||
this.current.push(elem); | ||
}.bind(this); | ||
return tag; | ||
} | ||
if (arguments.length >= 2) { | ||
return opts(attr_, children_); | ||
} else { | ||
return opts; | ||
} | ||
var tag = createElement.apply(this, arguments); | ||
return createElement.bind(this, tag); | ||
}; | ||
@@ -62,0 +205,0 @@ |
{ | ||
"name": "coffeex", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "Coffee DSL for React Virtual DOM instead of JSX", | ||
@@ -16,3 +16,6 @@ "main": "index.js", | ||
"coffee-script": "^1.9.0" | ||
}, | ||
"dependencies": { | ||
"parseful": "0.0.1" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8792
205
2
+ Addedparseful@0.0.1
+ Addedparseful@0.0.1(transitive)