j2c
Version:
90 lines (77 loc) • 3.06 kB
JavaScript
import {type, ARRAY, emptyArray} from './helpers'
import {sheet} from './sheet'
var findClass = /()(?::global\(\s*(\.[-\w]+)\s*\)|(\.)([-\w]+))/g
/**
* Hanldes at-rules
*
* @param {string} k - The at-rule name, and, if takes both parameters and a
* block, the parameters.
* @param {string[]} buf - the buffer in which the final style sheet is built
* @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 {string} rawPrefix - as above, but without localization transformations
* @param {string} vendors - a list of vendor prefixes
* @Param {boolean} local - are we in @local or in @global scope?
* @param {object} ns - helper functions to populate or create the @local namespace
* and to @extend classes
* @param {function} ns.e - @extend helper
* @param {function} ns.l - @local helper
*/
export function at(k, v, buf, prefix, rawPrefix, vendors, local, ns){
var kk
if (/^@(?:namespace|import|charset)$/.test(k)) {
if(type.call(v) == ARRAY){
for (kk = 0; kk < v.length; kk++) {
buf.push(k, ' ', v[kk], ';\n')
}
} else {
buf.push(k, ' ', v, ';\n')
}
} else if (/^@keyframes /.test(k)) {
k = local ? k.replace(
// generated by script/regexps.js
/( )(?::global\(\s*([-\w]+)\s*\)|()([-\w]+))/,
ns.l
) : k
// add a @-webkit-keyframes block too.
buf.push('@-webkit-', k.slice(1), ' {\n')
sheet(v, buf, '', '', ['webkit'])
buf.push('}\n')
buf.push(k, ' {\n')
sheet(v, buf, '', '', vendors, local, ns)
buf.push('}\n')
} else if (/^@extends?$/.test(k)) {
/*eslint-disable no-cond-assign*/
// pick the last class to be extended
while (kk = findClass.exec(rawPrefix)) k = kk[4]
/*eslint-enable no-cond-assign*/
if (k == null || !local) {
// we're in a @global{} block
buf.push('@-error-cannot-extend-in-global-context ', JSON.stringify(rawPrefix), ';\n')
return
} else if (/^@extends?$/.test(k)) {
// no class in the selector
buf.push('@-error-no-class-to-extend-in ', JSON.stringify(rawPrefix), ';\n')
return
}
ns.e(
type.call(v) == ARRAY ? v.map(function (parent) {
return parent.replace(/()(?::global\(\s*(\.[-\w]+)\s*\)|()\.([-\w]+))/, ns.l)
}).join(' ') : v.replace(/()(?::global\(\s*(\.[-\w]+)\s*\)|()\.([-\w]+))/, ns.l),
k
)
} else if (/^@(?:font-face$|viewport$|page )/.test(k)) {
sheet(v, buf, k, k, emptyArray)
} else if (/^@global$/.test(k)) {
sheet(v, buf, prefix, rawPrefix, vendors, 0, ns)
} else if (/^@local$/.test(k)) {
sheet(v, buf, prefix, rawPrefix, vendors, 1, ns)
} else if (/^@(?:media |supports |document )./.test(k)) {
buf.push(k, ' {\n')
sheet(v, buf, prefix, rawPrefix, vendors, local, ns)
buf.push('}\n')
} else {
buf.push('@-error-unsupported-at-rule ', JSON.stringify(k), ';\n')
}
}