UNPKG

@alicd/templateparser

Version:

walle template parser

46 lines (41 loc) 1.14 kB
/** * @license * Copyright Alibaba Group and its affiliates. All Rights Reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ import output from './output'; import express from './express'; const _transformStr2Hump = (str) => { const re = /-(\w)/g; return str.replace(re, ($0, $1) => { return $1.toUpperCase(); }); }; /** * handle the style attribute format. * eg: * style="color:#F00;width:100px;" => style="{color:'#F00',width:'100px'}" */ export default (styleAttr, runtimeOn) => { if (express.test(styleAttr)) { return output(styleAttr, runtimeOn); } else { if (typeof styleAttr === 'string') { const styleArr = styleAttr.split(/;/g); const styleObj = {}; let i = 0; const j = styleArr.length; for (; i < j; i += 1) { if (styleArr[i]) { const item = styleArr[i].split(':'); const name = _transformStr2Hump(item[0].replace(/\s/g, '')); item[1] && (styleObj[name] = item[1].trim()); } } return styleObj; } return {}; } };