Skip to content

Commit 9b54924

Browse files
committed
Make '' plugins more powerfull, cut back on esoteric at-rules
1 parent faa8c07 commit 9b54924

13 files changed

+306
-442
lines changed

dist/j2c.amd.js

+58-57
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,18 @@ define(function () { 'use strict';
9292
* @param {string} prefix - the current property or a prefix in case of nested
9393
* sub-properties.
9494
* @param {boolean} local - are we in @local or in @global scope.
95-
* @param {function} localize - @local helper.
95+
* @param {function} state - @local helper.
9696
*/
9797

98-
function declarations(o, emit, prefix, local, localize) {
98+
function declarations(o, emit, prefix, local, state) {
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, localize)
106+
declarations(o[k], emit, prefix, local, state)
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, localize)
118+
declarations(v, emit, prefix + k[kk], local, state)
119119

120120
}
121121
} else {
122122

123-
declarations(v, emit, prefix + k, local, localize)
123+
declarations(v, emit, prefix + k, local, state)
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 'localize' a comment, but it's not a big deal.
141+
// We may 'state' 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]*)/, localize)
144+
return o.replace(/:?global\(\s*([_A-Za-z][-\w]*)\s*\)|()(-?[_A-Za-z][-\w]*)/, state.l)
145145

146146
}).join(',')
147147
}
@@ -160,23 +160,19 @@ define(function () { 'use strict';
160160
* @param {string[]} v - Either parameters for block-less rules or their block
161161
* for the others.
162162
* @param {string} prefix - the current selector or a prefix in case of nested rules
163-
* @param {string} composes - the potential target of a @composes rule, if any
163+
* @param {string} inAtRule - are we nested in an at-rule?
164164
* @param {boolean} local - are we in @local or in @global scope?
165-
* @param {function} localize - @local helper
165+
* @param {function} state - @local helper
166166
*/
167167

168-
function atRules(k, v, emit, prefix, composes, local, localize){
168+
function atRules(k, v, emit, prefix, inAtRule, local, state) {
169169
k = /^(.(?:-[\w]+-)?([_A-Za-z][-\w]*))\b\s*(.*?)\s*$/.exec(k) || ['@','@','','']
170170
if (!k[3] && /^global$/.test(k[2])) {
171-
sheet(v, emit, prefix, 1, 0, localize)
171+
sheet(v, emit, prefix, inAtRule, 0, state)
172172

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

175-
sheet(v, emit, prefix, 1, 1, localize)
176-
177-
} else if (!k[3] && /^mixin$/.test(k[2])) {
178-
179-
sheet(v, emit, prefix, composes, local, localize)
175+
sheet(v, emit, prefix, inAtRule, 1, state)
180176

181177
} else if (!k[3] && /^(?:namespace|import|charset)$/.test(k[2])) {
182178
flatIter(function(v) {
@@ -185,45 +181,45 @@ define(function () { 'use strict';
185181

186182
})(v)
187183

188-
} else if (!k[3] && /^(?:font-face|viewport|swash|ornaments|annotation|stylistic|styleset|character-variant)$/.test(k[2])) {
184+
} else if (!k[3] && /^(?:font-face|viewport)$/.test(k[2])) {
189185
flatIter(function(v) {
190186

191187
emit.a(k[1], '', '', ' {\n')
192188

193-
declarations(v, emit, '', local, localize)
189+
declarations(v, emit, '', local, state)
194190

195191
emit.c('}\n')
196192

197193
})(v)
198194

199-
} else if (k[3] && /^(?:media|supports|document|page|keyframes|counter-style|font-feature-values)$/.test(k[2])) {
195+
} else if (k[3] && /^(?:media|supports|page|keyframes)$/.test(k[2])) {
200196

201-
if (local && /^(?:keyframes|counter-style)$/.test(k[2])) {
197+
if (local && 'keyframes' == k[2]) {
202198
k[3] = k[3].replace(
203199
// generated by script/regexps.js
204200
/:?global\(\s*([_A-Za-z][-\w]*)\s*\)|()(-?[_A-Za-z][-\w]*)/,
205-
localize
201+
state.l
206202
)
207203
}
208204

209205

210206
emit.a(k[1], ' ', k[3], ' {\n')
211207

212-
if (/^(?:page|counter-style)$/.test(k[2])) {
208+
if ('page' == k[2]) {
213209

214-
declarations(v, emit, '', local, localize)
210+
declarations(v, emit, '', local, state)
215211

216212
} else {
217213

218-
sheet(v, emit, prefix, 1, local, localize)
214+
sheet(v, emit, prefix, 0, local, state)
219215

220216
}
221217

222218
emit.c('}\n')
223219

224220
} else {
225-
for (var i = 0; i < localize.a.length; i++) {
226-
if (localize.a[i](k, v, emit, prefix, composes, local, localize, sheet, declarations)) return
221+
for (var i = 0; i < state.A.length; i++) {
222+
if (state.A[i](k, v, emit, prefix, inAtRule, local, state)) return
227223
}
228224
emit.a('@-error-unsupported-at-rule', ' ', JSON.stringify(k[0]), ';\n')
229225

@@ -236,19 +232,19 @@ define(function () { 'use strict';
236232
* @param {array|string|object} statements - a source object or sub-object.
237233
* @param {string[]} emit - the contextual emitters to the final buffer
238234
* @param {string} prefix - the current selector or a prefix in case of nested rules
239-
* @param {string} composes - the potential target of a @composes rule, if any
235+
* @param {string} canCompose - are we allowed to @compose here?
240236
* @param {boolean} local - are we in @local or in @global scope?
241-
* @param {function} localize - @local helper
237+
* @param {function} state - @local helper
242238
*/
243-
function sheet(statements, emit, prefix, composes, local, localize) {
239+
function sheet(statements, emit, prefix, canCompose, local, state) {
244240
var k, v, inDeclaration, kk
245241

246242
switch (type.call(statements)) {
247243

248244
case ARRAY:
249245
for (k = 0; k < statements.length; k++){
250246

251-
sheet(statements[k], emit, prefix, composes, local, localize)
247+
sheet(statements[k], emit, prefix, canCompose, local, state)
252248

253249
}
254250
break
@@ -266,20 +262,20 @@ define(function () { 'use strict';
266262
if (/\$/.test(k)) {
267263
for (kk in (k = k.split('$'))) if (own.call(k, kk)) {
268264

269-
declarations(v, emit, k[kk], local, localize)
265+
declarations(v, emit, k[kk], local, state)
270266

271267
}
272268
} else {
273269

274-
declarations(v, emit, k, local, localize)
270+
declarations(v, emit, k, local, state)
275271

276272
}
277273
} else if (/^@/.test(k)) {
278274
// Handle At-rules
279275

280276
inDeclaration = (inDeclaration && emit.c('}\n') && 0)
281277

282-
atRules(k, v, emit, prefix, composes, local, localize)
278+
atRules(k, v, emit, prefix, canCompose, local, state)
283279

284280
} else {
285281
// selector or nested sub-selectors
@@ -291,7 +287,7 @@ define(function () { 'use strict';
291287
/*0*/ (kk = splitSelector(prefix), splitSelector( local ?
292288

293289
k.replace(
294-
/:global\(\s*(\.-?[_A-Za-z][-\w]*)\s*\)|(\.)(-?[_A-Za-z][-\w]*)/g, localize
290+
/:global\(\s*(\.-?[_A-Za-z][-\w]*)\s*\)|(\.)(-?[_A-Za-z][-\w]*)/g, state.l
295291
) :
296292

297293
k
@@ -305,7 +301,7 @@ define(function () { 'use strict';
305301
local ?
306302

307303
k.replace(
308-
/:global\(\s*(\.-?[_A-Za-z][-\w]*)\s*\)|(\.)(-?[_A-Za-z][-\w]*)/g, localize
304+
/:global\(\s*(\.-?[_A-Za-z][-\w]*)\s*\)|(\.)(-?[_A-Za-z][-\w]*)/g, state.l
309305
) :
310306

311307
k,
@@ -315,13 +311,13 @@ define(function () { 'use strict';
315311
local ?
316312

317313
k.replace(
318-
/:global\(\s*(\.-?[_A-Za-z][-\w]*)\s*\)|(\.)(-?[_A-Za-z][-\w]*)/g, localize
314+
/:global\(\s*(\.-?[_A-Za-z][-\w]*)\s*\)|(\.)(-?[_A-Za-z][-\w]*)/g, state.l
319315
) :
320316

321317
k
322318
),
323-
composes || prefix ? '' : k,
324-
local, localize
319+
canCompose,
320+
local, state
325321
)
326322
}
327323
}
@@ -334,7 +330,7 @@ define(function () { 'use strict';
334330

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

337-
declarations(statements, emit, '', local, localize)
333+
declarations(statements, emit, '', local, state)
338334

339335
emit.c('}\n')
340336
}
@@ -412,7 +408,7 @@ define(function () { 'use strict';
412408
_default(instance, plugin)
413409
})
414410

415-
function makeEmitter(inline) {
411+
function makeEmitter(inline, state) {
416412
var buf = []
417413
function push() {
418414
emptyArray.push.apply(buf, arguments)
@@ -424,21 +420,10 @@ define(function () { 'use strict';
424420
d: push, // declaration
425421
c: push // close
426422
}
427-
for (var i = filters.length; i--;) emit = filters[i](emit, inline)
423+
for (var i = filters.length; i--;) emit = filters[i](emit, inline, state)
428424
return emit
429425
}
430426

431-
instance.compose = function(target, source) {
432-
if(!/^-?[_A-Za-z][-\w]*$/.test(target))
433-
throw new Error('Bad target class ' + JSON.stringify(target))
434-
435-
localize(0,0,0,target)
436-
437-
flatIter(function(source) {
438-
instance.names[target] = instance.names[target] + ' ' + source
439-
})(source)
440-
}
441-
442427
function localize(match, global, dot, name) {
443428
if (global) return global
444429
if (!instance.names[name]) instance.names[name] = name + instance.suffix
@@ -449,24 +434,40 @@ define(function () { 'use strict';
449434

450435
/*/-statements-/*/
451436
instance.sheet = function(statements, emit) {
437+
var state = {
438+
s: sheet,
439+
a: atRules,
440+
d: declarations,
441+
A: atHandlers,
442+
l: localize,
443+
n: instance.names
444+
}
452445
sheet(
453446
statements,
454-
emit = makeEmitter(false),
447+
emit = makeEmitter(false, state),
455448
'', '', // prefix and compose
456449
1, // local, by default
457-
localize
450+
state
458451
)
459452

460453
return emit.x()
461454
}
462455
/*/-statements-/*/
463456
instance.inline = function (_declarations, emit) {
457+
var state = {
458+
s: sheet,
459+
a: atRules,
460+
d: declarations,
461+
A: atHandlers,
462+
l: localize,
463+
n: instance.names
464+
}
464465
declarations(
465466
_declarations,
466-
emit = makeEmitter(true),
467+
emit = makeEmitter(true, state),
467468
'', // prefix
468469
1, //local
469-
localize
470+
state
470471
)
471472
return emit.x()
472473
}
@@ -475,7 +476,7 @@ define(function () { 'use strict';
475476
}
476477

477478
var _j2c = j2c()
478-
'sheet|inline|names|at|global|kv|suffix|compose'.split('|').map(function(m){j2c[m] = _j2c[m]})
479+
'sheet|inline|names|at|global|kv|suffix'.split('|').map(function(m){j2c[m] = _j2c[m]})
479480

480481
return j2c;
481482

dist/j2c.amd.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)