sbfkcel / towxml

微信小程序HTML、Markdown渲染库
https://github.com/sbfkcel/towxml/wiki
2.5k stars 315 forks source link

HTML解析错误 #157

Open CcphAmy opened 4 years ago

CcphAmy commented 4 years ago

异常抛出

thirdScriptError Unexpected token u in JSON at position 0;at App towxml function SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse () at http://127.0.0.1:12311/appservice/towxml/parse/index.js:79:31 at Array.forEach () at eachFn (http://127.0.0.1:12311/appservice/towxml/parse/index.js:64:13) at http://127.0.0.1:12311/appservice/towxml/parse/index.js:97:21 at Array.forEach () at eachFn (http://127.0.0.1:12311/appservice/towxml/parse/index.js:64:13) at initObj (http://127.0.0.1:12311/appservice/towxml/parse/index.js:103:7) at module.exports (http://127.0.0.1:12311/appservice/towxml/parse/index.js:117:12) at ve.module.exports (http://127.0.0.1:12311/appservice/towxml/index.js:16:22)

      arr.forEach(item => {
                if(item.type === 'comment'){
                    return;
                };
                let o = {},
                    e = {};
                o.type = e.type = item.type;
                o._e = e;
                if(item.type === 'text'){
                    o.text = e.text = item.data;
                }else{
                    o.tag = getWxmlTag(item.name);      // 转换之后的标签
                    // o.tag = o.tag === 'text' ? 'view' : o.tag;
                    e.tag = item.name;                  // 原始
                    o.attr = item.attribs;
                    e.attr = JSON.parse(JSON.stringify(item.attribs));

问题原因

文件 /towxml/parse/index.js 中,当 item.type == 'directive'时,item.name为‘!doctype’,此时item.attribs为undefined。

解决方法

           e = {};
                o.type = e.type = item.type;
                o._e = e;
                if(item.type === 'text'){
                    o.text = e.text = item.data;
                }else if (item.name !== '!doctype'){
                    o.tag = getWxmlTag(item.name);      // 转换之后的标签
                    // o.tag = o.tag === 'text' ? 'view' : o.tag;
                    e.tag = item.name;                  // 原始
                    o.attr = item.attribs;
                    e.attr = JSON.parse(JSON.stringify(item.attribs));