vivaxy / WXML

See https://github.com/oft/wxml.

Home Page:https://github.com/vivaxy/WXML

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

标签的属性中包含json字符串数据时解析有误

bigmeow opened this issue · comments

const wxml = require("@vivaxy/wxml");
function dealwith(inputCode) {
    const parsed = wxml.parse(inputCode);
    wxml.traverse(parsed, function visitor(node) {
        console.log(node);
    });
    return wxml.serialize(parsed);
}

const test1 = dealwith(`
<view data-route="{{ {'name': 'user-info'} }}"></view>
<view class="icon-box" data-route='{\"name\": \"user-info\"}' ></view>
`);
console.log("test1:", test1);

实际输出结果:

正确<view data-route="{{ {'name': 'user-info'} }}"></view>
不正确<view class="icon-box" data-route="{"name": "user-info"}"></view>

期望输出结果:

<view data-route="{{ {'name': 'user-info'} }}"></view>
<view class="icon-box" data-route='{\"name\": \"user-info\"}' ></view>

从结果看,转义符被干掉了,单引号会被转成双引号,希望这块不被处理

return `${name}="${node.attributes[name]}"`;

将上行代码改进一下即可解决:

return `${name}=${JSON.stringify(node.attributes[name])}`;

在自己项目里是没有发现问题,最好再跑一下测试用例
@vivaxy

2.0.3 已修复