From b563e4620e8ecb451c6516a9540957863f4848a7 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Gerardy Date: Wed, 24 Feb 2016 02:52:37 +0100 Subject: [PATCH] Improve argument order of once private, now public tree walking functions. Improve inner nomencalture. --- dist/j2c.amd.js | 152 +++++++++++++++++++++----------------- dist/j2c.amd.min.js | 2 +- dist/j2c.commonjs.js | 152 +++++++++++++++++++++----------------- dist/j2c.commonjs.min.js | 2 +- dist/j2c.es6.js | 152 +++++++++++++++++++++----------------- dist/j2c.global.js | 152 +++++++++++++++++++++----------------- dist/j2c.global.min.js | 2 +- dist/j2c.global.min.js.gz | Bin 1901 -> 1926 bytes src/at-rules.js | 41 +++++----- src/declarations.js | 18 ++--- src/main.js | 47 ++++++------ src/sheet.js | 46 ++++++------ test/test.js | 29 ++++---- 13 files changed, 434 insertions(+), 361 deletions(-) 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;rpr2Lw;^X0G$EZVvWEs+n&hZOZF^z8SKDbH$^+PqYR9=+PgnXx)g zt+(psGes1W^!NztEfY-hC|PBtCSsPWnWT9}E`wAQ1(8fpif=5wQM3~)W~uM%psLND zsK_RB8Ht3hw6wKWGkqE?NuuZx6`(E2ELdU!ADUmUB;N+|S*EaNMa@%g(>;;VbwZD+ zFw!GlsOR(5MoaNyl#U(>%wM-)l2mXS!oF|WU@7PPK!+$mX2tCgbOA;&#>fv@ z02{$JJ&hLhXu(@}a~0U~@8y%!ze0M+1PQVZq78J-Tjm;8vUCM7R9zGoT=Wd;Pzf%A zM^V)BYepz-DK$HUh|&N;b1Qm;1`rO~BLnH96N=3qL(8r2+d05Vp1i78H!&^ z?zS)E%Vl7tvBb2+li~)$O<<}bx5T^jXkST&rBF$o^|^`C1iZ?-NDbmv)?(VqOw@%9 z4e(mast~}0<->Bv03IDB(f#7z2}m_%7kSfoLgfd6fm|qYklqD1zCQ}P??rCx!q}I> zEod4S#11sjA~=YO49!Ij#E_%eaJ?(ajVNLgn=2AM01C1APZBNSr{s!)ET#>iWU+qz zYRObk&=Va=2viTY5FoSmLiq6@^WG{}-jqCfmp09EWP@Yng^fNznJvG!NcvpZ=p&Hb zxD7dMxQ69zn5TP+GX43Mn|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