diff --git a/dist/j2c.amd.js b/dist/j2c.amd.js index 2475b314..20c15622 100644 --- a/dist/j2c.amd.js +++ b/dist/j2c.amd.js @@ -87,15 +87,15 @@ define(function () { 'use strict'; /** * Handles the property:value; pairs. * - * @param {array|object|string} o - the declarations. - * @param {string[]} emit - the contextual emitters to the final buffer + * @param {object} parser - holds the parser-related methods and state + * @param {object} emit - the contextual emitters to the final buffer * @param {string} prefix - the current property or a prefix in case of nested * sub-properties. + * @param {array|object|string} o - the declarations. * @param {boolean} local - are we in @local or in @global scope. - * @param {function} state - @local helper. */ - function declarations(o, emit, prefix, local, state) { + function declarations(parser, emit, prefix, o, local) { var k, v, kk if (o==null) return @@ -103,7 +103,7 @@ define(function () { 'use strict'; case ARRAY: for (k = 0; k < o.length; k++) - declarations(o[k], emit, prefix, local, state) + declarations(parser, emit, prefix, o[k], local) break case OBJECT: @@ -115,12 +115,12 @@ define(function () { 'use strict'; if (/\$/.test(k)) { for (kk in (k = k.split('$'))) if (own.call(k, kk)) { - declarations(v, emit, prefix + k[kk], local, state) + declarations(parser, emit, prefix + k[kk], v, local) } } else { - declarations(v, emit, prefix + k, local, state) + declarations(parser, emit, prefix + k, v, local) } } @@ -138,10 +138,10 @@ define(function () { 'use strict'; if (local && (k == 'animation-name' || k == 'animation' || k == 'list-style')) { // no need to tokenize here a plain `.split(',')` has all bases covered. - // We may 'state' a comment, but it's not a big deal. + // We may 'parser' a comment, but it's not a big deal. o = o.split(',').map(function (o) { - return o.replace(/:?global\(\s*([_A-Za-z][-\w]*)\s*\)|()(-?[_A-Za-z][-\w]*)/, state.l) + return o.replace(/:?global\(\s*([_A-Za-z][-\w]*)\s*\)|()(-?[_A-Za-z][-\w]*)/, parser.l) }).join(',') } @@ -154,25 +154,28 @@ define(function () { 'use strict'; /** * Hanldes at-rules * - * @param {string} k - The at-rule name, and, if takes both parameters and a - * block, the parameters. - * @param {string[]} emit - the contextual emitters to the final buffer - * @param {string[]} v - Either parameters for block-less rules or their block - * for the others. - * @param {string} prefix - the current selector or a prefix in case of nested rules + * @param {object} parser - holds the parser-related methods and state + * @param {object} emit - the contextual emitters to the final buffer + * @param {array} k - The parsed at-rule, including the parameters, + * if takes both parameters and a block. + * @param {string} prefix - the current selector or the selector prefix + * in case of nested rules + * @param {string|string[]|object|object[]} v - Either parameters for + * block-less rules or + * their block + * for the others. * @param {string} inAtRule - are we nested in an at-rule? * @param {boolean} local - are we in @local or in @global scope? - * @param {function} state - @local helper */ - function atRules(k, v, emit, prefix, inAtRule, local, state) { - k = /^(.(?:-[\w]+-)?([_A-Za-z][-\w]*))\b\s*(.*?)\s*$/.exec(k) || ['@','@','',''] + function atRules(parser, emit, k, v, prefix, local, inAtRule) { if (!k[3] && /^global$/.test(k[2])) { - sheet(v, emit, prefix, inAtRule, 0, state) + + sheet(parser, emit, prefix, v, 0, inAtRule) } else if (!k[3] && /^local$/.test(k[2])) { - sheet(v, emit, prefix, inAtRule, 1, state) + sheet(parser, emit, prefix, v, 1, inAtRule) } else if (!k[3] && /^(?:namespace|import|charset)$/.test(k[2])) { flatIter(function(v) { @@ -186,7 +189,7 @@ define(function () { 'use strict'; emit.a(k[1], '', '', ' {\n') - declarations(v, emit, '', local, state) + declarations(parser, emit, '', v, local) emit.c('}\n') @@ -198,7 +201,7 @@ define(function () { 'use strict'; k[3] = k[3].replace( // generated by script/regexps.js /:?global\(\s*([_A-Za-z][-\w]*)\s*\)|()(-?[_A-Za-z][-\w]*)/, - state.l + parser.l ) } @@ -207,19 +210,23 @@ define(function () { 'use strict'; if ('page' == k[2]) { - declarations(v, emit, '', local, state) + declarations(parser, emit, '', v, local) } else { - sheet(v, emit, prefix, 0, local, state) + sheet( + parser, emit, + 'keyframes' == k[2] ? '' : prefix, + v, local, 1 + ) } emit.c('}\n') } else { - for (var i = 0; i < state.A.length; i++) { - if (state.A[i](k, v, emit, prefix, inAtRule, local, state)) return + for (var i = 0; i < parser.A.length; i++) { + if (parser.A[i](parser, emit, k, v, prefix, local, inAtRule)) return } emit.a('@-error-unsupported-at-rule', ' ', JSON.stringify(k[0]), ';\n') @@ -227,31 +234,31 @@ define(function () { 'use strict'; } /** - * Add rulesets and other CSS statements to the sheet. + * Add rulesets and other CSS tree to the sheet. * - * @param {array|string|object} statements - a source object or sub-object. - * @param {string[]} emit - the contextual emitters to the final buffer + * @param {object} parser - holds the parser-related methods and state + * @param {object} emit - the contextual emitters to the final buffer * @param {string} prefix - the current selector or a prefix in case of nested rules - * @param {string} canCompose - are we allowed to @compose here? + * @param {array|string|object} tree - a source object or sub-object. + * @param {string} inAtRule - are we nested in an at-rule? * @param {boolean} local - are we in @local or in @global scope? - * @param {function} state - @local helper */ - function sheet(statements, emit, prefix, canCompose, local, state) { + function sheet(parser, emit, prefix, tree, local, inAtRule) { var k, v, inDeclaration, kk - switch (type.call(statements)) { + switch (type.call(tree)) { case ARRAY: - for (k = 0; k < statements.length; k++){ + for (k = 0; k < tree.length; k++){ - sheet(statements[k], emit, prefix, canCompose, local, state) + sheet(parser, emit, prefix, tree[k], local, inAtRule) } break case OBJECT: - for (k in statements) if (own.call(statements, k)) { - v = statements[k] + for (k in tree) if (own.call(tree, k)) { + v = tree[k] if (prefix && /^[-\w$]+$/.test(k)) { if (!inDeclaration) { inDeclaration = 1 @@ -262,12 +269,12 @@ define(function () { 'use strict'; if (/\$/.test(k)) { for (kk in (k = k.split('$'))) if (own.call(k, kk)) { - declarations(v, emit, k[kk], local, state) + declarations(parser, emit, k[kk], v, local) } } else { - declarations(v, emit, k, local, state) + declarations(parser, emit, k, v, local) } } else if (/^@/.test(k)) { @@ -275,19 +282,24 @@ define(function () { 'use strict'; inDeclaration = (inDeclaration && emit.c('}\n') && 0) - atRules(k, v, emit, prefix, canCompose, local, state) + atRules(parser, emit, + /^(.(?:-[\w]+-)?([_A-Za-z][-\w]*))\b\s*(.*?)\s*$/.exec(k) || ['@','@','',''], + v, prefix, local, inAtRule + ) } else { // selector or nested sub-selectors inDeclaration = (inDeclaration && emit.c('}\n') && 0) - sheet(v, emit, + sheet( + parser, emit, + // prefix... Hefty. Ugly. Sadly necessary. (/,/.test(prefix) || prefix && /,/.test(k)) ? /*0*/ (kk = splitSelector(prefix), splitSelector( local ? k.replace( - /:global\(\s*(\.-?[_A-Za-z][-\w]*)\s*\)|(\.)(-?[_A-Za-z][-\w]*)/g, state.l + /:global\(\s*(\.-?[_A-Za-z][-\w]*)\s*\)|(\.)(-?[_A-Za-z][-\w]*)/g, parser.l ) : k @@ -301,7 +313,7 @@ define(function () { 'use strict'; local ? k.replace( - /:global\(\s*(\.-?[_A-Za-z][-\w]*)\s*\)|(\.)(-?[_A-Za-z][-\w]*)/g, state.l + /:global\(\s*(\.-?[_A-Za-z][-\w]*)\s*\)|(\.)(-?[_A-Za-z][-\w]*)/g, parser.l ) : k, @@ -311,13 +323,12 @@ define(function () { 'use strict'; local ? k.replace( - /:global\(\s*(\.-?[_A-Za-z][-\w]*)\s*\)|(\.)(-?[_A-Za-z][-\w]*)/g, state.l + /:global\(\s*(\.-?[_A-Za-z][-\w]*)\s*\)|(\.)(-?[_A-Za-z][-\w]*)/g, parser.l ) : k ), - canCompose, - local, state + v, local, inAtRule ) } } @@ -330,7 +341,7 @@ define(function () { 'use strict'; emit.s(( prefix || ':-error-no-selector' ) , ' {\n') - declarations(statements, emit, '', local, state) + declarations(parser, emit, '', tree, local) emit.c('}\n') } @@ -351,7 +362,9 @@ define(function () { 'use strict'; if ( arguments.length < 3 ) { + // inner curry! var _at = at.bind.apply(at, [null].concat([].slice.call(arguments,0))) + // So that it can be used as a key in an ES6 object literal. _at.toString = function(){return '@' + rule + ' ' + params} return _at } @@ -359,7 +372,8 @@ define(function () { 'use strict'; } function j2c() { - var filters = [], atHandlers = [] + var filters = [] + var atHandlers = [] var instance = { at: at, global: global, @@ -408,7 +422,7 @@ define(function () { 'use strict'; _default(instance, plugin) }) - function makeEmitter(inline, state) { + function makeEmitter(inline, parser) { var buf = [] function push() { emptyArray.push.apply(buf, arguments) @@ -420,7 +434,7 @@ define(function () { 'use strict'; d: push, // declaration c: push // close } - for (var i = filters.length; i--;) emit = filters[i](emit, inline, state) + for (var i = filters.length; i--;) emit = filters[i](emit, inline, parser) return emit } @@ -433,41 +447,41 @@ define(function () { 'use strict'; localize.a = atHandlers /*/-statements-/*/ - instance.sheet = function(statements, emit) { - var state = { - s: sheet, + instance.sheet = function(tree) { + var parser = { + A: atHandlers, a: atRules, d: declarations, - A: atHandlers, l: localize, - n: instance.names + n: instance.names, + s: sheet } + var emit = makeEmitter(false, parser) sheet( - statements, - emit = makeEmitter(false, state), - '', '', // prefix and compose - 1, // local, by default - state + parser, + emit, + '', // prefix + tree, + 1, // local, by default + 0 // inAtRule ) return emit.x() } /*/-statements-/*/ - instance.inline = function (_declarations, emit) { - var state = { - s: sheet, - a: atRules, + instance.inline = function (tree) { + var parser = { d: declarations, - A: atHandlers, l: localize, n: instance.names } + var emit = makeEmitter(true, parser) declarations( - _declarations, - emit = makeEmitter(true, state), + parser, + emit, '', // prefix - 1, //local - state + tree, + 1 //local ) return emit.x() } diff --git a/dist/j2c.amd.min.js b/dist/j2c.amd.min.js index 3d242871..a911df90 100644 --- a/dist/j2c.amd.min.js +++ b/dist/j2c.amd.min.js @@ -1 +1 @@ -define(function(){"use strict";function n(n,e){var t,a,r=[];for(a in e)if($.call(e,a))for(t in n)$.call(n,t)&&r.push(n[t]+e[a]);return r}function e(n){for(var e,t=[],a=[],r=0;e=d.exec(n);)switch(e[0]){case"(":r++;break;case")":r--;break;case",":if(r)break;t.push(e.index)}for(e=t.length;e--;)a.unshift(n.slice(t[e]+1)),n=n.slice(0,t[e]);return a.unshift(n),a}function t(e,t){for(var a,r,l=[],i=[];r=A.exec(e);)"&"==r[0]&&l.push(r.index);for(r=l.length;r--;)i.unshift(e.slice(l[r]+1)),e=e.slice(0,l[r]);for(i.unshift(e),a=[i[0]],r=1;r