提交 0c071609 编写于 作者: Q qiang

fix(mp): string express with escape quote

上级 a206e689
......@@ -54,6 +54,24 @@ describe('mp:compiler-mp-weixin', () => {
)
})
it('generate string express with escape quote', () => {
assertCodegen(
`<view :data-text="text+'\\''"></view>`,
'<view data-text="{{$root.a0}}"></view>',
`with(this){var a0=text+"'";$mp.data=Object.assign({},{$root:{a0:a0}})}`
)
assertCodegen(
`<view>{{text+'\\''}}</view>`,
'<view>{{$root.t0}}</view>',
`with(this){var t0=text+"'";$mp.data=Object.assign({},{$root:{t0:t0}})}`
)
assertCodegen(
`<view>{{text+"\\""}}</view>`,
'<view>{{$root.t0}}</view>',
`with(this){var t0=text+"\\"";$mp.data=Object.assign({},{$root:{t0:t0}})}`
)
})
it('generate named scoped slot', () => {
assertCodegen(
'<foo><template slot="foo" slot-scope="{bar}">{{ bar.foo }}</template></foo>',
......
......@@ -117,6 +117,7 @@ module.exports = {
PREFIX_CLASS: 'c',
PREFIX_STYLE: 's',
PREFIX_EVENT: 'e',
PREFIX_TEXT: 't',
IDENTIFIER_FOR: '__$$for$$__',
IDENTIFIER_ATTR: '__$$attr$$__',
IDENTIFIER_METHOD: '__$$method$$__',
......@@ -124,5 +125,6 @@ module.exports = {
IDENTIFIER_CLASS: '__$$class$$__',
IDENTIFIER_STYLE: '__$$style$$__',
IDENTIFIER_EVENT: '__$$event$$__',
IDENTIFIER_GLOBAL: '__$$global$$__'
IDENTIFIER_GLOBAL: '__$$global$$__',
IDENTIFIER_TEXT: '__$$text$$__'
}
......@@ -3,18 +3,24 @@ const {
} = require('../../../constants')
const {
isSimpleObjectExpression
isSimpleObjectExpression,
hasEscapeQuote
} = require('../../../util')
const getMemberExpr = require('../member-expr')
function checkObjectExpression (path) {
return path.isObjectExpression() && !isSimpleObjectExpression(path.node)
}
module.exports = function processAttrs (paths, path, state, isComponent, tagName) {
const attrsPath = paths.attrs
if (attrsPath) {
attrsPath.get('value.properties').forEach(propertyPath => {
const valuePath = propertyPath.get('value')
// 对于简单的ObjectExpression不再单独处理,改为在转换temlplte时用()包裹(微信、QQ)
if (valuePath.isObjectExpression() && !isSimpleObjectExpression(valuePath.node)) {
// 属性中包含转义引号时部分小程序平台报错或显示异常
if (checkObjectExpression(valuePath) || hasEscapeQuote(valuePath)) {
valuePath.replaceWith(getMemberExpr(path, IDENTIFIER_ATTR, valuePath.node, state))
}
})
......
......@@ -12,6 +12,7 @@ const {
IDENTIFIER_STYLE,
IDENTIFIER_EVENT,
IDENTIFIER_GLOBAL,
IDENTIFIER_TEXT,
PREFIX_ATTR,
PREFIX_GLOBAL,
PREFIX_METHOD,
......@@ -19,7 +20,8 @@ const {
PREFIX_FOR,
PREFIX_CLASS,
PREFIX_STYLE,
PREFIX_EVENT
PREFIX_EVENT,
PREFIX_TEXT
} = require('../../constants')
const {
......@@ -63,6 +65,10 @@ function reIdentifier (identifierArray) {
[IDENTIFIER_ATTR]: {
prefix: PREFIX_ATTR,
id: 0
},
[IDENTIFIER_TEXT]: {
prefix: PREFIX_TEXT,
id: 0
}
}
// TODO order
......
......@@ -11,7 +11,8 @@ const {
METHOD_RESOLVE_SCOPED_SLOTS,
IDENTIFIER_FILTER,
IDENTIFIER_METHOD,
IDENTIFIER_GLOBAL
IDENTIFIER_GLOBAL,
IDENTIFIER_TEXT
} = require('../../constants')
const {
......@@ -22,7 +23,8 @@ const {
hasOwn,
hyphenate,
traverseFilter,
getComponentName
getComponentName,
hasEscapeQuote
} = require('../../util')
const traverseData = require('./data')
......@@ -186,7 +188,13 @@ module.exports = {
break
case METHOD_TO_STRING:
{
const stringNodes = path.node.arguments[0]
const stringPath = path.get('arguments.0')
if (hasEscapeQuote(stringPath)) {
// 属性中包含转义引号时部分小程序平台报错或显示异常
// TODO 简单情况翻转外层引号
stringPath.replaceWith(getMemberExpr(path, IDENTIFIER_TEXT, stringPath.node, this))
}
const stringNodes = stringPath.node
stringNodes.$toString = true
path.replaceWith(stringNodes)
}
......
......@@ -232,6 +232,34 @@ function isSimpleObjectExpression (node) {
}) => !t.isIdentifier(key) || !(t.isIdentifier(value) || t.isStringLiteral(value) || t.isBooleanLiteral(value) ||
t.isNumericLiteral(value) || t.isNullLiteral(value)))
}
/**
* 是否包含转义引号
* @param {*} path
* @returns {boolean}
*/
function hasEscapeQuote (path) {
let has = false
function hasEscapeQuote (node) {
const quote = node.extra ? node.extra.raw[0] : '"'
if (node.value.includes(quote)) {
return true
}
}
if (path.isStringLiteral()) {
return hasEscapeQuote(path.node)
} else {
path.traverse({
noScope: true,
StringLiteral (path) {
if (hasEscapeQuote(path.node)) {
has = true
path.stop()
}
}
})
}
return has
}
module.exports = {
hasOwn,
......@@ -263,5 +291,6 @@ module.exports = {
}),
processMemberExpression,
getForIndexIdentifier,
isSimpleObjectExpression
isSimpleObjectExpression,
hasEscapeQuote
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册