Skip to content

Commit b563e46

Browse files
committed
Improve argument order of once private, now public tree walking functions. Improve inner nomencalture.
1 parent b4a16ce commit b563e46

13 files changed

Lines changed: 434 additions & 361 deletions

dist/j2c.amd.js

Lines changed: 83 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,23 @@ define(function () { 'use strict';
8787
/**
8888
* Handles the property:value; pairs.
8989
*
90-
* @param {array|object|string} o - the declarations.
91-
* @param {string[]} emit - the contextual emitters to the final buffer
90+
* @param {object} parser - holds the parser-related methods and state
91+
* @param {object} emit - the contextual emitters to the final buffer
9292
* @param {string} prefix - the current property or a prefix in case of nested
9393
* sub-properties.
94+
* @param {array|object|string} o - the declarations.
9495
* @param {boolean} local - are we in @local or in @global scope.
95-
* @param {function} state - @local helper.
9696
*/
9797

98-
function declarations(o, emit, prefix, local, state) {
98+
function declarations(parser, emit, prefix, o, local) {
9999
var k, v, kk
100100
if (o==null) return
101101

102102
switch ( type.call(o = o.valueOf()) ) {
103103
case ARRAY:
104104
for (k = 0; k < o.length; k++)
105105

106-
declarations(o[k], emit, prefix, local, state)
106+
declarations(parser, emit, prefix, o[k], local)
107107

108108
break
109109
case OBJECT:
@@ -115,12 +115,12 @@ define(function () { 'use strict';
115115
if (/\$/.test(k)) {
116116
for (kk in (k = k.split('$'))) if (own.call(k, kk)) {
117117

118-
declarations(v, emit, prefix + k[kk], local, state)
118+
declarations(parser, emit, prefix + k[kk], v, local)
119119

120120
}
121121
} else {
122122

123-
declarations(v, emit, prefix + k, local, state)
123+
declarations(parser, emit, prefix + k, v, local)
124124

125125
}
126126
}
@@ -138,10 +138,10 @@ define(function () { 'use strict';
138138

139139
if (local && (k == 'animation-name' || k == 'animation' || k == 'list-style')) {
140140
// no need to tokenize here a plain `.split(',')` has all bases covered.
141-
// We may 'state' a comment, but it's not a big deal.
141+
// We may 'parser' a comment, but it's not a big deal.
142142
o = o.split(',').map(function (o) {
143143

144-
return o.replace(/:?global\(\s*([_A-Za-z][-\w]*)\s*\)|()(-?[_A-Za-z][-\w]*)/, state.l)
144+
return o.replace(/:?global\(\s*([_A-Za-z][-\w]*)\s*\)|()(-?[_A-Za-z][-\w]*)/, parser.l)
145145

146146
}).join(',')
147147
}
@@ -154,25 +154,28 @@ define(function () { 'use strict';
154154
/**
155155
* Hanldes at-rules
156156
*
157-
* @param {string} k - The at-rule name, and, if takes both parameters and a
158-
* block, the parameters.
159-
* @param {string[]} emit - the contextual emitters to the final buffer
160-
* @param {string[]} v - Either parameters for block-less rules or their block
161-
* for the others.
162-
* @param {string} prefix - the current selector or a prefix in case of nested rules
157+
* @param {object} parser - holds the parser-related methods and state
158+
* @param {object} emit - the contextual emitters to the final buffer
159+
* @param {array} k - The parsed at-rule, including the parameters,
160+
* if takes both parameters and a block.
161+
* @param {string} prefix - the current selector or the selector prefix
162+
* in case of nested rules
163+
* @param {string|string[]|object|object[]} v - Either parameters for
164+
* block-less rules or
165+
* their block
166+
* for the others.
163167
* @param {string} inAtRule - are we nested in an at-rule?
164168
* @param {boolean} local - are we in @local or in @global scope?
165-
* @param {function} state - @local helper
166169
*/
167170

168-
function atRules(k, v, emit, prefix, inAtRule, local, state) {
169-
k = /^(.(?:-[\w]+-)?([_A-Za-z][-\w]*))\b\s*(.*?)\s*$/.exec(k) || ['@','@','','']
171+
function atRules(parser, emit, k, v, prefix, local, inAtRule) {
170172
if (!k[3] && /^global$/.test(k[2])) {
171-
sheet(v, emit, prefix, inAtRule, 0, state)
173+
174+
sheet(parser, emit, prefix, v, 0, inAtRule)
172175

173176
} else if (!k[3] && /^local$/.test(k[2])) {
174177

175-
sheet(v, emit, prefix, inAtRule, 1, state)
178+
sheet(parser, emit, prefix, v, 1, inAtRule)
176179

177180
} else if (!k[3] && /^(?:namespace|import|charset)$/.test(k[2])) {
178181
flatIter(function(v) {
@@ -186,7 +189,7 @@ define(function () { 'use strict';
186189

187190
emit.a(k[1], '', '', ' {\n')
188191

189-
declarations(v, emit, '', local, state)
192+
declarations(parser, emit, '', v, local)
190193

191194
emit.c('}\n')
192195

@@ -198,7 +201,7 @@ define(function () { 'use strict';
198201
k[3] = k[3].replace(
199202
// generated by script/regexps.js
200203
/:?global\(\s*([_A-Za-z][-\w]*)\s*\)|()(-?[_A-Za-z][-\w]*)/,
201-
state.l
204+
parser.l
202205
)
203206
}
204207

@@ -207,51 +210,55 @@ define(function () { 'use strict';
207210

208211
if ('page' == k[2]) {
209212

210-
declarations(v, emit, '', local, state)
213+
declarations(parser, emit, '', v, local)
211214

212215
} else {
213216

214-
sheet(v, emit, prefix, 0, local, state)
217+
sheet(
218+
parser, emit,
219+
'keyframes' == k[2] ? '' : prefix,
220+
v, local, 1
221+
)
215222

216223
}
217224

218225
emit.c('}\n')
219226

220227
} else {
221-
for (var i = 0; i < state.A.length; i++) {
222-
if (state.A[i](k, v, emit, prefix, inAtRule, local, state)) return
228+
for (var i = 0; i < parser.A.length; i++) {
229+
if (parser.A[i](parser, emit, k, v, prefix, local, inAtRule)) return
223230
}
224231
emit.a('@-error-unsupported-at-rule', ' ', JSON.stringify(k[0]), ';\n')
225232

226233
}
227234
}
228235

229236
/**
230-
* Add rulesets and other CSS statements to the sheet.
237+
* Add rulesets and other CSS tree to the sheet.
231238
*
232-
* @param {array|string|object} statements - a source object or sub-object.
233-
* @param {string[]} emit - the contextual emitters to the final buffer
239+
* @param {object} parser - holds the parser-related methods and state
240+
* @param {object} emit - the contextual emitters to the final buffer
234241
* @param {string} prefix - the current selector or a prefix in case of nested rules
235-
* @param {string} canCompose - are we allowed to @compose here?
242+
* @param {array|string|object} tree - a source object or sub-object.
243+
* @param {string} inAtRule - are we nested in an at-rule?
236244
* @param {boolean} local - are we in @local or in @global scope?
237-
* @param {function} state - @local helper
238245
*/
239-
function sheet(statements, emit, prefix, canCompose, local, state) {
246+
function sheet(parser, emit, prefix, tree, local, inAtRule) {
240247
var k, v, inDeclaration, kk
241248

242-
switch (type.call(statements)) {
249+
switch (type.call(tree)) {
243250

244251
case ARRAY:
245-
for (k = 0; k < statements.length; k++){
252+
for (k = 0; k < tree.length; k++){
246253

247-
sheet(statements[k], emit, prefix, canCompose, local, state)
254+
sheet(parser, emit, prefix, tree[k], local, inAtRule)
248255

249256
}
250257
break
251258

252259
case OBJECT:
253-
for (k in statements) if (own.call(statements, k)) {
254-
v = statements[k]
260+
for (k in tree) if (own.call(tree, k)) {
261+
v = tree[k]
255262
if (prefix && /^[-\w$]+$/.test(k)) {
256263
if (!inDeclaration) {
257264
inDeclaration = 1
@@ -262,32 +269,37 @@ define(function () { 'use strict';
262269
if (/\$/.test(k)) {
263270
for (kk in (k = k.split('$'))) if (own.call(k, kk)) {
264271

265-
declarations(v, emit, k[kk], local, state)
272+
declarations(parser, emit, k[kk], v, local)
266273

267274
}
268275
} else {
269276

270-
declarations(v, emit, k, local, state)
277+
declarations(parser, emit, k, v, local)
271278

272279
}
273280
} else if (/^@/.test(k)) {
274281
// Handle At-rules
275282

276283
inDeclaration = (inDeclaration && emit.c('}\n') && 0)
277284

278-
atRules(k, v, emit, prefix, canCompose, local, state)
285+
atRules(parser, emit,
286+
/^(.(?:-[\w]+-)?([_A-Za-z][-\w]*))\b\s*(.*?)\s*$/.exec(k) || ['@','@','',''],
287+
v, prefix, local, inAtRule
288+
)
279289

280290
} else {
281291
// selector or nested sub-selectors
282292

283293
inDeclaration = (inDeclaration && emit.c('}\n') && 0)
284294

285-
sheet(v, emit,
295+
sheet(
296+
parser, emit,
297+
// prefix... Hefty. Ugly. Sadly necessary.
286298
(/,/.test(prefix) || prefix && /,/.test(k)) ?
287299
/*0*/ (kk = splitSelector(prefix), splitSelector( local ?
288300

289301
k.replace(
290-
/:global\(\s*(\.-?[_A-Za-z][-\w]*)\s*\)|(\.)(-?[_A-Za-z][-\w]*)/g, state.l
302+
/:global\(\s*(\.-?[_A-Za-z][-\w]*)\s*\)|(\.)(-?[_A-Za-z][-\w]*)/g, parser.l
291303
) :
292304

293305
k
@@ -301,7 +313,7 @@ define(function () { 'use strict';
301313
local ?
302314

303315
k.replace(
304-
/:global\(\s*(\.-?[_A-Za-z][-\w]*)\s*\)|(\.)(-?[_A-Za-z][-\w]*)/g, state.l
316+
/:global\(\s*(\.-?[_A-Za-z][-\w]*)\s*\)|(\.)(-?[_A-Za-z][-\w]*)/g, parser.l
305317
) :
306318

307319
k,
@@ -311,13 +323,12 @@ define(function () { 'use strict';
311323
local ?
312324

313325
k.replace(
314-
/:global\(\s*(\.-?[_A-Za-z][-\w]*)\s*\)|(\.)(-?[_A-Za-z][-\w]*)/g, state.l
326+
/:global\(\s*(\.-?[_A-Za-z][-\w]*)\s*\)|(\.)(-?[_A-Za-z][-\w]*)/g, parser.l
315327
) :
316328

317329
k
318330
),
319-
canCompose,
320-
local, state
331+
v, local, inAtRule
321332
)
322333
}
323334
}
@@ -330,7 +341,7 @@ define(function () { 'use strict';
330341

331342
emit.s(( prefix || ':-error-no-selector' ) , ' {\n')
332343

333-
declarations(statements, emit, '', local, state)
344+
declarations(parser, emit, '', tree, local)
334345

335346
emit.c('}\n')
336347
}
@@ -351,15 +362,18 @@ define(function () { 'use strict';
351362
if (
352363
arguments.length < 3
353364
) {
365+
// inner curry!
354366
var _at = at.bind.apply(at, [null].concat([].slice.call(arguments,0)))
367+
// So that it can be used as a key in an ES6 object literal.
355368
_at.toString = function(){return '@' + rule + ' ' + params}
356369
return _at
357370
}
358371
else return kv('@' + rule +' ' + params, block)
359372
}
360373

361374
function j2c() {
362-
var filters = [], atHandlers = []
375+
var filters = []
376+
var atHandlers = []
363377
var instance = {
364378
at: at,
365379
global: global,
@@ -408,7 +422,7 @@ define(function () { 'use strict';
408422
_default(instance, plugin)
409423
})
410424

411-
function makeEmitter(inline, state) {
425+
function makeEmitter(inline, parser) {
412426
var buf = []
413427
function push() {
414428
emptyArray.push.apply(buf, arguments)
@@ -420,7 +434,7 @@ define(function () { 'use strict';
420434
d: push, // declaration
421435
c: push // close
422436
}
423-
for (var i = filters.length; i--;) emit = filters[i](emit, inline, state)
437+
for (var i = filters.length; i--;) emit = filters[i](emit, inline, parser)
424438
return emit
425439
}
426440

@@ -433,41 +447,41 @@ define(function () { 'use strict';
433447
localize.a = atHandlers
434448

435449
/*/-statements-/*/
436-
instance.sheet = function(statements, emit) {
437-
var state = {
438-
s: sheet,
450+
instance.sheet = function(tree) {
451+
var parser = {
452+
A: atHandlers,
439453
a: atRules,
440454
d: declarations,
441-
A: atHandlers,
442455
l: localize,
443-
n: instance.names
456+
n: instance.names,
457+
s: sheet
444458
}
459+
var emit = makeEmitter(false, parser)
445460
sheet(
446-
statements,
447-
emit = makeEmitter(false, state),
448-
'', '', // prefix and compose
449-
1, // local, by default
450-
state
461+
parser,
462+
emit,
463+
'', // prefix
464+
tree,
465+
1, // local, by default
466+
0 // inAtRule
451467
)
452468

453469
return emit.x()
454470
}
455471
/*/-statements-/*/
456-
instance.inline = function (_declarations, emit) {
457-
var state = {
458-
s: sheet,
459-
a: atRules,
472+
instance.inline = function (tree) {
473+
var parser = {
460474
d: declarations,
461-
A: atHandlers,
462475
l: localize,
463476
n: instance.names
464477
}
478+
var emit = makeEmitter(true, parser)
465479
declarations(
466-
_declarations,
467-
emit = makeEmitter(true, state),
480+
parser,
481+
emit,
468482
'', // prefix
469-
1, //local
470-
state
483+
tree,
484+
1 //local
471485
)
472486
return emit.x()
473487
}

0 commit comments

Comments
 (0)