|
| 1 | +var gonzales = require('gonzales-pe'), |
| 2 | + traverser = require('./ast-traverser'); |
| 3 | + |
| 4 | +// Parse Style variables to object |
| 5 | +function parseVariableDeclarations(string) { |
| 6 | + // Do not parse empty files. Otherwise gonzales.parse will fail |
| 7 | + if (!string) { |
| 8 | + return []; |
| 9 | + } |
| 10 | + |
| 11 | + var out = [], |
| 12 | + ast = gonzales.parse(string, { |
| 13 | + syntax: 'less' |
| 14 | + }), |
| 15 | + visitor = { |
| 16 | + // Visitor for SASS, SCSS and plain CSS syntaxes |
| 17 | + test: function(name) { |
| 18 | + return name === 'atrules'; |
| 19 | + }, |
| 20 | + process: function(nodes) { |
| 21 | + var varName = nodes.content[0].content[0].content, |
| 22 | + varVal = ''; |
| 23 | + |
| 24 | + // Skip at-keywords that do not decrade variable (Ex. @imports) |
| 25 | + if (nodes.content[1].type === 'operator' && nodes.content[1].content === ':') { |
| 26 | + /* Grabs all the listed values |
| 27 | + * Fix then https://github.com/tonyganch/gonzales-pe/issues/17 is fixed */ |
| 28 | + |
| 29 | + nodes.content.forEach(function(element) { |
| 30 | + if (element.type === 'atrules' || element.type === 'atkeyword') { |
| 31 | + return; |
| 32 | + } |
| 33 | + if (element.type === 'operator' && element.content === ':') { |
| 34 | + return; |
| 35 | + } |
| 36 | + varVal += element.toCSS('less'); // Syntax is always less as this visitor is only for LESS |
| 37 | + }); |
| 38 | + |
| 39 | + out.push({ |
| 40 | + name: varName, |
| 41 | + value: varVal.trim(), |
| 42 | + line: nodes.content[1].start.line |
| 43 | + }); |
| 44 | + } |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + traverser.traverse(ast, visitor); |
| 49 | + return out; |
| 50 | +} |
| 51 | + |
| 52 | +// Parse Style variables to object |
| 53 | +function findVariables(string) { |
| 54 | + // Do not parse empty files. Otherwise gonzales.parse will fail |
| 55 | + if (!string) { |
| 56 | + return []; |
| 57 | + } |
| 58 | + |
| 59 | + var out = [], |
| 60 | + ast = gonzales.parse(string, { |
| 61 | + syntax: 'less' |
| 62 | + }), |
| 63 | + visitor = { |
| 64 | + // Visitor for SASS, SCSS and plain CSS syntaxes |
| 65 | + test: function(name, nodes) { |
| 66 | + return (name === 'declaration' && nodes.content[0].content[0].type === 'variable') || (name === 'variable' && nodes.content[0].type === 'ident'); |
| 67 | + }, |
| 68 | + process: function(nodes) { |
| 69 | + if (nodes.type !== 'declaration') { |
| 70 | + out.push(nodes.content[0].content); |
| 71 | + } |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + traverser.traverse(ast, visitor); |
| 76 | + return out; |
| 77 | +} |
| 78 | + |
| 79 | +function setVariables(string, variables) { |
| 80 | + |
| 81 | + var ast = gonzales.parse(string, { |
| 82 | + syntax: 'less' |
| 83 | + }); |
| 84 | + |
| 85 | + variables.forEach(function(variable) { |
| 86 | + ast.map(function(node) { |
| 87 | + if (node.type === 'atrules') { |
| 88 | + var hasOperator = false, |
| 89 | + isThisVariable = false, |
| 90 | + metOperator = false, |
| 91 | + metFirstSpace = false, |
| 92 | + newContent = []; |
| 93 | + node.map(function(nnode) { |
| 94 | + if (nnode.type === 'ident' && nnode.content === variable.name) { |
| 95 | + isThisVariable = true; |
| 96 | + } |
| 97 | + if (nnode.type === 'operator' && nnode.content === ':') { |
| 98 | + metOperator = true; |
| 99 | + hasOperator = true; |
| 100 | + } |
| 101 | + }); |
| 102 | + // Take only at-rules with operators |
| 103 | + if (!hasOperator) { |
| 104 | + return; |
| 105 | + } |
| 106 | + // For given variable only |
| 107 | + if (!isThisVariable) { |
| 108 | + return; |
| 109 | + } |
| 110 | + node.content.forEach(function(node) { |
| 111 | + if (metOperator && metFirstSpace) { |
| 112 | + return; |
| 113 | + } |
| 114 | + if (node.type === 'operator') { |
| 115 | + metOperator = true; |
| 116 | + } |
| 117 | + newContent.push(node); |
| 118 | + if (node.type === 'space' && metOperator) { |
| 119 | + metFirstSpace = true; |
| 120 | + } |
| 121 | + }); |
| 122 | + node.content = newContent; |
| 123 | + node.content.push({ |
| 124 | + type: 'string', |
| 125 | + content: variable.value |
| 126 | + }); |
| 127 | + } |
| 128 | + }); |
| 129 | + }); |
| 130 | + return ast.toCSS('less'); |
| 131 | +} |
| 132 | + |
| 133 | +module.exports = { |
| 134 | + parseVariableDeclarations: parseVariableDeclarations, |
| 135 | + findVariables: findVariables, |
| 136 | + setVariables: setVariables |
| 137 | +}; |
0 commit comments