fix: ios 不支持零宽断言,换种实现 (#2272)

* fix: ios 不支持零宽断言,换种实现

* 优化一下
This commit is contained in:
liaoxuezhi 2021-07-13 21:34:31 +08:00 committed by GitHub
parent 3b31bc3264
commit ce77ea2980
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -959,10 +959,39 @@ export function dataMapping(
return ret;
}
function matchSynatax(str: string) {
let from = 0;
while (true) {
const idx = str.indexOf('$', from);
if (~idx) {
const nextToken = str[idx + 1];
// 如果没有下一个字符,或者下一个字符是引号或者空格
// 这个一般不是取值用法
if (!nextToken || ~['"', "'", ' '].indexOf(nextToken)) {
from = idx + 1;
continue;
}
// 如果上个字符是转义也不是取值用法
const prevToken = str[idx - 1];
if (prevToken && prevToken === '\\') {
from = idx + 1;
continue;
}
return true;
} else {
break;
}
}
return false;
}
export function register(): Enginer & {name: string} {
return {
name: 'builtin',
test: (str: string) => typeof str === 'string' && /(?<!\\)\$\S/.test(str),
test: (str: string) => typeof str === 'string' && matchSynatax(str),
removeEscapeToken: (str: string) =>
typeof str === 'string' ? str.replace(/\\\$/g, '$') : str,
compile: (str: string, data: object, defaultFilter = '| html') =>