Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export default function transpileDecl(result, promises, decl, opts, cache) { // eslint-disable-line max-params
// path to the current working file and directory by declaration
const declWF = path.resolve(decl.source && decl.source.input && decl.source.input.file ? decl.source.input.file : result.root.source && result.root.source.input && result.root.source.input.file ? result.root.source.input.file : path.join(process.cwd(), 'index.css'));
const declWD = path.dirname(declWF);
// list of files to watch
const files = {};
// walk each node of the declaration
const declAST = parser(decl.value).parse();
declAST.walk(node => {
// if the node is a url containing an svg fragment
if (isExternalURLFunction(node)) {
// of url()
const urlNode = node.nodes[1];
// split by fragment identifier symbol (#)
const urlParts = urlNode.value.split('#');
// src
const src = urlParts[0];
// fragment identifier
const id = urlParts.slice(1).join('#');
styleAST.walkDecls(decl => {
const declAST = parser(decl.value).parse();
// update the declaration with all transpiled var()s
declAST.walk(node => {
// conditionally update the var()
if (isVarFuntion(node)) {
transpileVar(node, params);
}
});
decl.value = declAST.toString();
});
css.walkDecls(decl => {
const valueAst = parser(decl.value).parse();
valueAst.walk(child => {
if (child.type !== 'operator' || (child.value !== '+' && child.value !== '-')) {
return;
}
const {parent} = child;
// 当前 child 的索引
const index = parent.index(child);
// child 的后一个元素
const nextElem = parent.nodes[index + 1];
// child 的前一个元素
const prevElem = parent.nodes[index - 1] || {};
css.walkDecls(decl => {
const valueAst = parser(decl.value).parse();
valueAst.walk(child => {
if (child.type !== 'operator') {
return;
}
const {parent} = child;
// 当前 child 的索引
const index = parent.index(child);
// child 的后一个元素
const nextElem = parent.nodes[index + 1];
// child 的前一个元素
const prevElem = parent.nodes[index - 1];
root.walkDecls(decl => {
if (hasAlphaHex(decl)) {
// replace instances of hexa with rgba()
const ast = valueParser(decl.value).parse();
walk(ast, node => {
if (isAlphaHex(node)) {
node.replaceWith(hexa2rgba(node));
}
});
// conditionally update the declaration
const modifiedValue = String(ast);
if (decl.value !== modifiedValue) {
if (preserve) {
decl.cloneBefore({ value: modifiedValue });
} else {
decl.value = modifiedValue;
}
root.walkDecls(decl => {
if (hasGrayFunction(decl)) {
const { value: originalValue } = decl;
// parse the declaration value
const ast = parser(originalValue).parse();
// walk every node in the value that contains a gray() function
ast.walk(node => {
const [lightness, alpha] = getFunctionGrayArgs(node);
if (lightness !== undefined) {
// rename the gray() function to rgb()
node.value = 'rgb';
// convert the lab gray lightness into rgb
const [r, g, b] = lab2rgb(lightness, 0, 0).map(
channel => Math.max(Math.min(Math.round(channel * 2.55), 255), 0)
);
// preserve the slash nodes within rgb()
const openingSlash = node.first;
root.walkDecls(decl => {
const originalValue = decl.value;
if (colorModFunctionMatch.test(originalValue)) {
const ast = parser(originalValue, { loose: true }).parse();
transformAST(ast, {
unresolved: unresolvedOpt,
stringifier: stringifierOpt,
transformVars: transformVarsOpt,
decl,
result,
customProperties
});
const modifiedValue = ast.toString();
if (originalValue !== modifiedValue) {
decl.value = modifiedValue;
}
}
function importCustomPropertiesFromObject(object) {
const customProperties = Object.assign(
{},
Object(object).customProperties || Object(object)['custom-properties']
);
for (const prop in customProperties) {
customProperties[prop] = valueParser(customProperties[prop]).parse();
}
return customProperties;
}