diff --git a/deps/weex-styler/lib/util.js b/deps/weex-styler/lib/util.js index f164d1b689aa80fb03c92c8672579ea56718c704..507fcf9fde060ad57a0072e371d37ff0292bbaf6 100644 --- a/deps/weex-styler/lib/util.js +++ b/deps/weex-styler/lib/util.js @@ -9,7 +9,7 @@ const { DEVICE_LEVEL } = require('../../lite/lite-enum') * @return {string} */ exports.hyphenedToCamelCase = function hyphenedToCamelCase(value) { - return value.replace(/-([a-z])/g, function(s, m) { + return value.replace(/(?= 0) { let result = { @@ -587,7 +596,7 @@ var SHORTHAND_VALIDATOR = function SHORTHAND_VALIDATOR(v, validateFunction, isAr v = (v || '').toString().trim() let value = [] let reason = [] - let results = v.split(/\s+/).map(validateFunction) + let results = v.split(/(? { @@ -3043,6 +3049,177 @@ function genValidatorMap() { genValidatorMap() +function getValueUnit(dem) { + var str = dem.toString() + var getValue = str.match(/[-]{0,1}[1-9][0-9]*/) + var getUnit = str.match(/px|cm|%|em|vp|fp/) + var result = {value: getValue, unit: getUnit} + return result +} + +function isOperator(value) { + var operatorString = "+-*/()" + return operatorString.indexOf(value) > -1 +} + +function getPrioraty(value) { + switch(value) { + case '+': + case '-': + return 1 + case '*': + case '/': + return 2 + default: + return 0 + } +} + +function prioraty(o1, o2) { + return getPrioraty(o1) <= getPrioraty(o2) +} + +function dal2Rpn(exp) { + var inputStack = [] + var outputStack = [] + var outputQueue = [] + var str = exp.replace(/calc/g, "").replace(/(? 0) { + var cur = inputStack.shift() + if(isOperator(cur)) { + if(cur == '(') { + outputStack.push(cur) + } else if(cur == ')') { + var po = outputStack.pop() + while(po != '(' && outputStack.length > 0) { + outputQueue.push(po) + po = outputStack.pop() + } + if(po != '(') { + throw "error: unmatched ()" + } + } else { + while(prioraty(cur, outputStack[outputStack.length - 1]) && outputStack.length > 0) { + outputQueue.push(outputStack.pop()) + } + outputStack.push(cur) + } + } else { + outputQueue.push(cur) + } + } + return outputQueue +} + +function getResult(left, right, operator) { + if (left.match(/var/)) { + left = cssVarFun(left) + } + if (right.match(/var/)) { + right = cssVarFun(right) + } + var result, value, unit + var leftValue = getValueUnit(left) + var rightValue = getValueUnit(right) + if (left.match(/\(/) | right.match(/\(/)) { + result = left + ' ' + operator + ' ' + right + } else { + if (operator == '*') { + value = leftValue.value * rightValue.value + if (leftValue.unit == null) { + unit = rightValue.unit + } else { unit = leftValue.unit} + result = value + unit + } else if (operator == '/') { + if (rightValue != 0) { + value = leftValue.value / rightValue.value + unit = leftValue.unit + result = value + unit + } + } else if (operator == '+') { + if (JSON.stringify(leftValue.unit) == JSON.stringify(rightValue.unit)) { + value = parseInt(leftValue.value) + parseInt(rightValue.value) + unit = leftValue.unit + result = value + unit + } else result = '(' + left + ' ' + operator + ' ' + right + ')' + } else if (operator == '-') { + if (JSON.stringify(leftValue.unit) == JSON.stringify(rightValue.unit)) { + value = parseInt(leftValue.value) - parseInt(rightValue.value) + unit = leftValue.unit + result = value + unit + } else result = '(' + left + ' ' + operator + ' ' + right + ')' + } + } + return result +} + +function evalRpn(rpnQueue) { + var outputStack = [] + while(rpnQueue.length > 0) { + var cur = rpnQueue.shift() + if(!isOperator(cur)) { + outputStack.push(cur) + } else { + if(outputStack.length < 2) { + throw "unvalid stack length" + } + var sec = outputStack.pop() + var fir = outputStack.pop() + var res = getResult(fir, sec, cur) + outputStack.push(res) + } + } + if(outputStack.length != 1) { + throw "unvalid expression" + } else { + if (outputStack[0].match(/[+-]/)) { + return 'calc' + outputStack[0] + } else { + return outputStack[0] + } + } +} + +var cssPropData = [] + +function saveCssProp(name, value) { + if (name.match(/\-\-/)) { + while (value.match(/var/)) { + var value = cssVarFun(value) + } + cssPropData.push({name: name,value: value}) + } +} + +function cssVarFun(value) { + if (value.match(/calc/)) { + return value + } else { + if (value.match(/var/)) { + if (value.match(/\,/)) { + var cssVarFir = value.substring(0,value.indexOf(",")).replace("var(","").trim() + var cssVarSec = value.substring(value.indexOf(",")+1,value.length).replace(")","").trim() + } else { + var cssVarFir = value.replace("var(","").replace(")","").trim() + var cssVarSec = "" + } + let varValue = cssVarSec + for(var i=0, len=cssPropData.length; i