Skip to content

Commit

Permalink
Improve argument order of once private, now public tree walking funct…
Browse files Browse the repository at this point in the history
…ions. Improve inner nomencalture.
  • Loading branch information
pygy committed Feb 24, 2016
1 parent b4a16ce commit b563e46
Show file tree
Hide file tree
Showing 13 changed files with 434 additions and 361 deletions.
152 changes: 83 additions & 69 deletions dist/j2c.amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,23 @@ 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

switch ( type.call(o = o.valueOf()) ) {
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:
Expand All @@ -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)

}
}
Expand All @@ -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(',')
}
Expand All @@ -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) {
Expand All @@ -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')

Expand All @@ -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
)
}

Expand All @@ -207,51 +210,55 @@ 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')

}
}

/**
* 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
Expand All @@ -262,32 +269,37 @@ 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)) {
// Handle At-rules

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
Expand All @@ -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,
Expand All @@ -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
)
}
}
Expand All @@ -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')
}
Expand All @@ -351,15 +362,18 @@ 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
}
else return kv('@' + rule +' ' + params, block)
}

function j2c() {
var filters = [], atHandlers = []
var filters = []
var atHandlers = []
var instance = {
at: at,
global: global,
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand All @@ -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()
}
Expand Down

0 comments on commit b563e46

Please sign in to comment.