-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
132 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
packages/wepy-cli/core/plugins/template/parseClassAndStyle.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const exprParser = require('../../util/exprParser'); | ||
|
||
|
||
exports = module.exports = function () { | ||
|
||
this.register('template-parse-ast-attr-:class', function parseBindClass ({item, name, expr}) { | ||
let exprObj = exprParser.str2obj(expr); | ||
item.bindClass = Object.keys(exprObj).map(name => { | ||
let exp = exprObj[name].replace(/\'/ig, '\\\'').replace(/\"/ig, '\\"'); | ||
name = name.replace(/\'/ig, '\\\'').replace(/\"/ig, '\\"'); | ||
return `${exp} ? '${name}' : ''`; | ||
}); | ||
// return {} means remove :class | ||
return {}; | ||
}); | ||
|
||
|
||
this.register('template-parse-ast-attr-:style', function parseBindClass ({item, name, expr}) { | ||
let exprObj = exprParser.str2obj(expr); | ||
item.bindStyle = Object.keys(exprObj).map(name => { | ||
let exp = exprObj[name].replace(/\'/ig, '\\\'').replace(/\"/ig, '\\"'); | ||
name = name.replace(/\'/ig, '\\\'').replace(/\"/ig, '\\"'); | ||
name = exprParser.hyphenate(name); | ||
return `'${name}:' + ${exp} + ';'`; | ||
}); | ||
// return {} means remove :class | ||
return {}; | ||
}); | ||
|
||
|
||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
const camelizeRE = /-(\w)/g; | ||
const hyphenateRE = /([^-])([A-Z])/g; | ||
|
||
|
||
const cached = (fn) => { | ||
let cache = {}; | ||
return ((str) => (cache[str] || (cache[str] = fn(str)))); | ||
}; | ||
|
||
exports = module.exports = { | ||
hyphenate: cached((str) => { | ||
return str | ||
.replace(hyphenateRE, '$1-$2') | ||
.replace(hyphenateRE, '$1-$2') | ||
.toLowerCase(); | ||
}), | ||
camelize: cached((str) => { | ||
return str.replace(camelizeRE, function (_, c) { return c ? c.toUpperCase() : ''; }); | ||
}), | ||
/** | ||
* str2obj parse a object string to a real object | ||
* @param {String} str "{someKey: someCondition}" | ||
* @return {Object} {someKey: someCondition} | ||
*/ | ||
str2obj: cached((exp) => { | ||
exp = exp.replace(/^\s/ig, '').replace(/\s$/ig, ''); | ||
if (exp[0] === '{' && exp[exp.length - 1] === '}') { | ||
exp = exp.substring(1, exp.length - 1); | ||
let i = 0, len = exp.length; | ||
let flagStack = [], flag = 'start'; | ||
let classNames = [], result = {}, str = ''; | ||
for (i = 0; i < len; i++) { | ||
if ((exp[i] === '\'' || exp[i] === '"')) { | ||
if (flagStack.length && flagStack[0] === exp[i]) { | ||
flagStack.pop(); | ||
if (flag === 'class') { | ||
flag = ':'; | ||
continue; | ||
} else if (flag === 'expression') { | ||
str += exp[i]; | ||
continue; | ||
} | ||
} else { | ||
if (flagStack.length === 0) { | ||
flagStack.push(exp[i]); | ||
if (flag === 'start') { | ||
flag = 'class'; | ||
continue; | ||
} else if (flag === 'expression') { | ||
str += exp[i]; | ||
continue; | ||
} | ||
} | ||
} | ||
} | ||
// {abc: num < 1} or {'abc': num <1} | ||
if (exp[i] === ':' && (flag === ':' || flag === 'class') && flagStack.length === 0) { | ||
flag = 'expression'; | ||
classNames.push(str); | ||
str = ''; | ||
continue; | ||
} | ||
if (exp[i] === ',' && flag === 'expression' && flagStack.length === 0) { | ||
result[classNames[classNames.length - 1]] = str.replace(/^\s/ig, '').replace(/\s$/ig, '');; | ||
str = ''; | ||
flag = 'start'; | ||
continue; | ||
} | ||
// get rid of the begining space | ||
if (!str.length && exp[i] === ' ') | ||
continue; | ||
|
||
// not started with '', like {abc: num < 1} | ||
if (flag === 'start') { | ||
flag = 'class'; | ||
} | ||
|
||
if (flag === 'class' || flag === 'expression') { | ||
str += exp[i]; | ||
} | ||
} | ||
if (str.length) { | ||
result[classNames[classNames.length - 1]] = str.replace(/^\s/ig, '').replace(/\s$/ig, ''); | ||
} | ||
return result; | ||
} else { | ||
throw ':class expression is not correct, it has to be {\'className\': mycondition}'; | ||
} | ||
}) | ||
} | ||
|
||
|