Comparing version 3.1.4 to 4.0.0
214
index.js
@@ -9,3 +9,9 @@ var assert = require('assert'); | ||
var parseCssSelectorMemo = {}; | ||
function parseCssSelector (input) { | ||
if (parseCssSelectorMemo.hasOwnProperty(input)) { | ||
return parseCssSelectorMemo[input]; | ||
} | ||
function parseIdent (input) { | ||
@@ -47,9 +53,99 @@ var match = input.match(CSS_IDENT); | ||
parseCssSelectorMemo[input] = result; | ||
return result; | ||
} | ||
function Context (params) { | ||
function Nothing () { return Nothing; } | ||
var m = { | ||
tag: { | ||
name: 'tag', | ||
cond: function tag (val) { | ||
if (typeof val === 'string' && parseCssSelector(val) === null) { | ||
// tag | ||
return val; | ||
} else if (typeof val === 'function') { | ||
// component | ||
return val; | ||
} | ||
return Nothing; | ||
} | ||
}, | ||
selector: { | ||
name: 'selector', | ||
cond: function (val) { | ||
if (typeof val !== 'string') { return Nothing; } | ||
var parsed = parseCssSelector(val); | ||
if (parsed === null) { return Nothing; } | ||
return parsed; | ||
} | ||
}, | ||
attr: { | ||
name: 'attr', | ||
cond: function attr (val) { | ||
return (typeof val === 'object') ? val : Nothing; | ||
} | ||
}, | ||
children: { | ||
name: 'children', | ||
cond: function children (val) { | ||
return (typeof val === 'function' || typeof val === 'string') ? val : Nothing; | ||
} | ||
} | ||
}; | ||
function createMatcher (patterns) { | ||
var patternsMap = patterns.reduce(function (memo, pattern) { | ||
if (!memo.hasOwnProperty(pattern.length)) { | ||
memo[pattern.length] = []; | ||
} | ||
memo[pattern.length].push(pattern); | ||
return memo; | ||
}, {}); | ||
return function match (args) { | ||
var pattens = patternsMap[args.length], | ||
i = 0, k = 0, ret = {}, pattern, arg; | ||
outer: | ||
for (i = 0; i < patterns.length; ++i) { | ||
ret = { selector: { classes: {} }, attr: {}, children: NOOP }; | ||
pattern = patterns[i]; | ||
for (k = 0; k < pattern.length; ++k) { | ||
arg = pattern[k]; | ||
if ((ret[arg.name] = arg.cond(args[k])) === Nothing) { | ||
continue outer; | ||
} | ||
} | ||
return ret; | ||
} | ||
}; | ||
} | ||
var match = createMatcher([ | ||
[m.tag, m.selector, m.attr, m.children], | ||
[m.tag, m.selector, m.attr ], | ||
[m.tag, m.selector, m.children], | ||
[m.tag, m.attr, m.children], | ||
[ m.selector, m.attr, m.children], | ||
[m.tag, m.selector ], | ||
[m.tag, m.attr ], | ||
[m.tag, m.children], | ||
[ m.attr, m.children], | ||
[m.tag ] | ||
]); | ||
function Context (params, that) { | ||
this.params = params; | ||
this.stack = []; | ||
this.current = []; | ||
this.that = that; | ||
} | ||
@@ -60,66 +156,5 @@ | ||
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') { | ||
selector = parseCssSelector(selectorStr); | ||
if (typeof attr === 'object') { | ||
children = NOOP; | ||
} else { | ||
children = attr; | ||
attr = {}; | ||
} | ||
} 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 = {}; | ||
children = selectorStr; | ||
} else { | ||
attr = {}; | ||
children = NOOP; | ||
} | ||
} | ||
} | ||
} else { | ||
children = selectorStr; | ||
attr = null; | ||
selector = null; | ||
} | ||
break; | ||
case 1: | ||
attr = {}; | ||
children = NOOP; | ||
break; | ||
} | ||
function createElement () { | ||
var args = match(arguments); | ||
assert(typeof tag === 'string'); | ||
assert(typeof selector === 'object'); | ||
assert(typeof attr === 'object'); | ||
assert(typeof children === 'function' || typeof children === 'string'); | ||
if (alreadyCreated) { | ||
@@ -131,3 +166,2 @@ this.current.pop(); | ||
if (this.stack.length === 0 && this.current.length > 0) { | ||
console.log(this.current, tag, attr, children); | ||
throw new SyntaxError('root node must be only one'); | ||
@@ -137,10 +171,10 @@ } | ||
var className = {}; | ||
if (attr.hasOwnProperty('className')) { | ||
if (typeof attr.className === 'string') { | ||
attr.className.split(/\s+/).forEach(function (name) { | ||
if (args.attr.hasOwnProperty('className')) { | ||
if (typeof args.attr.className === 'string') { | ||
args.attr.className.split(/\s+/).forEach(function (name) { | ||
className[name] = true; | ||
}); | ||
} else { | ||
Object.keys(attr.className).forEach(function (name) { | ||
if (attr.claasName[name]) { | ||
Object.keys(args.attr.className).forEach(function (name) { | ||
if (args.attr.claasName[name]) { | ||
className[name] = true; | ||
@@ -152,17 +186,18 @@ } | ||
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 both of attr's `id` and selector's `id` are given, | ||
// use attr's `id` rather than selector's `id` | ||
if (args.selector.hasOwnProperty('id') && !args.attr.hasOwnProperty('id')) { | ||
args.attr.id = args.selector.id; | ||
} | ||
Object.keys(args.selector.classes).forEach(function (name) { | ||
// if `class` is specified in both of attr and selector, | ||
// use attr's `class` rather than selectors `class` | ||
if (!className.hasOwnProperty(name)) { | ||
className[name] = true; | ||
} | ||
}); | ||
if (Object.keys(className).length > 0) { | ||
attr.className = Object.keys(className).join(' '); | ||
args.attr.className = Object.keys(className).join(' '); | ||
} | ||
@@ -172,10 +207,10 @@ | ||
this.current = []; | ||
if (typeof children === 'string') { | ||
this.current.push(children); | ||
if (typeof args.children === 'string') { | ||
this.current.push(args.children); | ||
} else { | ||
children.call(this.params); | ||
args.children.call(this.params); | ||
} | ||
var elem; | ||
if (this.current.length === 0) { | ||
elem = React.createElement(tag, attr); | ||
elem = React.createElement(args.tag, args.attr); | ||
} else { | ||
@@ -185,7 +220,7 @@ if (this.current.length === 1) { | ||
} | ||
elem = React.createElement(tag, attr, this.current); | ||
elem = React.createElement(args.tag, args.attr, this.current); | ||
} | ||
this.current = this.stack.pop(); | ||
this.current.push(elem); | ||
return tag; | ||
return args.tag; | ||
} | ||
@@ -207,3 +242,3 @@ | ||
return function render (params) { | ||
var ctx = new Context(params), | ||
var ctx = new Context(params, this), | ||
tags = Object.keys(React.DOM), | ||
@@ -216,2 +251,3 @@ $ = ctx.createElement.bind(ctx); | ||
}, {})); | ||
fn($, ctx.createTextNode.bind(ctx), params); | ||
@@ -218,0 +254,0 @@ return ctx.getRootNode(); |
{ | ||
"name": "coffeex", | ||
"version": "3.1.4", | ||
"version": "4.0.0", | ||
"description": "Coffee DSL for React Virtual DOM instead of JSX", | ||
@@ -15,4 +15,5 @@ "main": "index.js", | ||
"devDependencies": { | ||
"coffee-script": "^1.9.0" | ||
"coffee-script": "^1.9.0", | ||
"node-inspector": "^0.9.2" | ||
} | ||
} |
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
12319
6
229
2