Skip to content

Commit faa8c07

Browse files
committed
Add $at plugins
1 parent 796b5ef commit faa8c07

12 files changed

+231
-110
lines changed

dist/j2c.amd.js

+29-21
Original file line numberDiff line numberDiff line change
@@ -165,51 +165,51 @@ define(function () { 'use strict';
165165
* @param {function} localize - @local helper
166166
*/
167167

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

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

175175
sheet(v, emit, prefix, 1, 1, localize)
176176

177-
} else if (/^.(?:-[\w]+-)?(?:namespace|import|charset)$/.test(k)) {
177+
} else if (!k[3] && /^mixin$/.test(k[2])) {
178+
179+
sheet(v, emit, prefix, composes, local, localize)
180+
181+
} else if (!k[3] && /^(?:namespace|import|charset)$/.test(k[2])) {
178182
flatIter(function(v) {
179183

180-
emit.a(k, ' ', v, ';\n')
184+
emit.a(k[0], ' ', v, ';\n')
181185

182186
})(v)
183187

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

187-
emit.a(k, '', '', ' {\n')
191+
emit.a(k[1], '', '', ' {\n')
188192

189193
declarations(v, emit, '', local, localize)
190194

191195
emit.c('}\n')
192196

193197
})(v)
194198

195-
} else if (/^.(?:-[\w]+-)?(?:media|supports|document|page|keyframes|counter-style|font-feature-values)\b\s*(\S.*)/.test(k)) {
196-
197-
params = k.match(/^.(?:-[\w]+-)?(?:media|supports|document|page|keyframes|counter-style|font-feature-values)\b\s*(\S.*)/)[1]
198-
199-
k = k.match(/^.(?:-[\w]+-)?(?:media|supports|document|page|keyframes|counter-style|font-feature-values)/)[0]
199+
} else if (k[3] && /^(?:media|supports|document|page|keyframes|counter-style|font-feature-values)$/.test(k[2])) {
200200

201-
if (local && /^.(?:-[\w]+-)?(?:keyframes|counter-style)/.test(k)) {
202-
params = params.replace(
201+
if (local && /^(?:keyframes|counter-style)$/.test(k[2])) {
202+
k[3] = k[3].replace(
203203
// generated by script/regexps.js
204204
/:?global\(\s*([_A-Za-z][-\w]*)\s*\)|()(-?[_A-Za-z][-\w]*)/,
205205
localize
206206
)
207207
}
208208

209209

210-
emit.a(k, ' ', params, ' {\n')
210+
emit.a(k[1], ' ', k[3], ' {\n')
211211

212-
if (/.(?:-[\w]+-)?(?:page|counter-style)/.test(k)) {
212+
if (/^(?:page|counter-style)$/.test(k[2])) {
213213

214214
declarations(v, emit, '', local, localize)
215215

@@ -222,8 +222,10 @@ define(function () { 'use strict';
222222
emit.c('}\n')
223223

224224
} else {
225-
226-
emit.a('@-error-unsupported-at-rule', ' ', JSON.stringify(k), ';\n')
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
227+
}
228+
emit.a('@-error-unsupported-at-rule', ' ', JSON.stringify(k[0]), ';\n')
227229

228230
}
229231
}
@@ -277,7 +279,7 @@ define(function () { 'use strict';
277279

278280
inDeclaration = (inDeclaration && emit.c('}\n') && 0)
279281

280-
at$1(k, v, emit, prefix, composes, local, localize)
282+
atRules(k, v, emit, prefix, composes, local, localize)
281283

282284
} else {
283285
// selector or nested sub-selectors
@@ -361,7 +363,7 @@ define(function () { 'use strict';
361363
}
362364

363365
function j2c() {
364-
var filters = []
366+
var filters = [], atHandlers = []
365367
var instance = {
366368
at: at,
367369
global: global,
@@ -403,6 +405,10 @@ define(function () { 'use strict';
403405
filters.push(filter)
404406
})(plugin.$filter||[])
405407

408+
flatIter(function(handler) {
409+
atHandlers.push(handler)
410+
})(plugin.$at||[])
411+
406412
_default(instance, plugin)
407413
})
408414

@@ -439,6 +445,8 @@ define(function () { 'use strict';
439445
return dot + instance.names[name].match(/^\S+/)
440446
}
441447

448+
localize.a = atHandlers
449+
442450
/*/-statements-/*/
443451
instance.sheet = function(statements, emit) {
444452
sheet(

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.

dist/j2c.commonjs.js

+29-21
Original file line numberDiff line numberDiff line change
@@ -165,51 +165,51 @@ function declarations(o, emit, prefix, local, localize) {
165165
* @param {function} localize - @local helper
166166
*/
167167

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

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

175175
sheet(v, emit, prefix, 1, 1, localize)
176176

177-
} else if (/^.(?:-[\w]+-)?(?:namespace|import|charset)$/.test(k)) {
177+
} else if (!k[3] && /^mixin$/.test(k[2])) {
178+
179+
sheet(v, emit, prefix, composes, local, localize)
180+
181+
} else if (!k[3] && /^(?:namespace|import|charset)$/.test(k[2])) {
178182
flatIter(function(v) {
179183

180-
emit.a(k, ' ', v, ';\n')
184+
emit.a(k[0], ' ', v, ';\n')
181185

182186
})(v)
183187

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

187-
emit.a(k, '', '', ' {\n')
191+
emit.a(k[1], '', '', ' {\n')
188192

189193
declarations(v, emit, '', local, localize)
190194

191195
emit.c('}\n')
192196

193197
})(v)
194198

195-
} else if (/^.(?:-[\w]+-)?(?:media|supports|document|page|keyframes|counter-style|font-feature-values)\b\s*(\S.*)/.test(k)) {
196-
197-
params = k.match(/^.(?:-[\w]+-)?(?:media|supports|document|page|keyframes|counter-style|font-feature-values)\b\s*(\S.*)/)[1]
198-
199-
k = k.match(/^.(?:-[\w]+-)?(?:media|supports|document|page|keyframes|counter-style|font-feature-values)/)[0]
199+
} else if (k[3] && /^(?:media|supports|document|page|keyframes|counter-style|font-feature-values)$/.test(k[2])) {
200200

201-
if (local && /^.(?:-[\w]+-)?(?:keyframes|counter-style)/.test(k)) {
202-
params = params.replace(
201+
if (local && /^(?:keyframes|counter-style)$/.test(k[2])) {
202+
k[3] = k[3].replace(
203203
// generated by script/regexps.js
204204
/:?global\(\s*([_A-Za-z][-\w]*)\s*\)|()(-?[_A-Za-z][-\w]*)/,
205205
localize
206206
)
207207
}
208208

209209

210-
emit.a(k, ' ', params, ' {\n')
210+
emit.a(k[1], ' ', k[3], ' {\n')
211211

212-
if (/.(?:-[\w]+-)?(?:page|counter-style)/.test(k)) {
212+
if (/^(?:page|counter-style)$/.test(k[2])) {
213213

214214
declarations(v, emit, '', local, localize)
215215

@@ -222,8 +222,10 @@ function at$1(k, v, emit, prefix, composes, local, localize){
222222
emit.c('}\n')
223223

224224
} else {
225-
226-
emit.a('@-error-unsupported-at-rule', ' ', JSON.stringify(k), ';\n')
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
227+
}
228+
emit.a('@-error-unsupported-at-rule', ' ', JSON.stringify(k[0]), ';\n')
227229

228230
}
229231
}
@@ -277,7 +279,7 @@ function sheet(statements, emit, prefix, composes, local, localize) {
277279

278280
inDeclaration = (inDeclaration && emit.c('}\n') && 0)
279281

280-
at$1(k, v, emit, prefix, composes, local, localize)
282+
atRules(k, v, emit, prefix, composes, local, localize)
281283

282284
} else {
283285
// selector or nested sub-selectors
@@ -361,7 +363,7 @@ function at (rule, params, block) {
361363
}
362364

363365
function j2c() {
364-
var filters = []
366+
var filters = [], atHandlers = []
365367
var instance = {
366368
at: at,
367369
global: global,
@@ -403,6 +405,10 @@ function j2c() {
403405
filters.push(filter)
404406
})(plugin.$filter||[])
405407

408+
flatIter(function(handler) {
409+
atHandlers.push(handler)
410+
})(plugin.$at||[])
411+
406412
_default(instance, plugin)
407413
})
408414

@@ -439,6 +445,8 @@ function j2c() {
439445
return dot + instance.names[name].match(/^\S+/)
440446
}
441447

448+
localize.a = atHandlers
449+
442450
/*/-statements-/*/
443451
instance.sheet = function(statements, emit) {
444452
sheet(

0 commit comments

Comments
 (0)