uview-plus 3.1.12 → 3.1.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/changelog.md
CHANGED
|
@@ -0,0 +1,1122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview html 解析器
|
|
5
|
+
*/
|
|
6
|
+
// 配置
|
|
7
|
+
const config = {
|
|
8
|
+
// 信任的标签(保持标签名不变)
|
|
9
|
+
trustTags: makeMap(
|
|
10
|
+
"a,abbr,ad,audio,b,blockquote,br,code,col,colgroup,dd,del,dl,dt,div,em,fieldset,h1,h2,h3,h4,h5,h6,hr,i,img,ins,label,legend,li,ol,p,q,ruby,rt,source,span,strong,sub,sup,table,tbody,td,tfoot,th,thead,tr,title,ul,video"
|
|
11
|
+
),
|
|
12
|
+
// 块级标签(转为 div,其他的非信任标签转为 span)
|
|
13
|
+
blockTags: makeMap("address,article,aside,body,caption,center,cite,footer,header,html,nav,pre,section"),
|
|
14
|
+
// 要移除的标签
|
|
15
|
+
ignoreTags: makeMap(
|
|
16
|
+
"area,base,canvas,embed,frame,head,iframe,input,link,map,meta,param,rp,script,source,style,textarea,title,track,wbr"
|
|
17
|
+
),
|
|
18
|
+
// 自闭合的标签
|
|
19
|
+
voidTags: makeMap(
|
|
20
|
+
"area,base,br,col,circle,ellipse,embed,frame,hr,img,input,line,link,meta,param,path,polygon,rect,source,track,use,wbr"
|
|
21
|
+
),
|
|
22
|
+
// html 实体
|
|
23
|
+
entities: {
|
|
24
|
+
lt: "<",
|
|
25
|
+
gt: ">",
|
|
26
|
+
quot: '"',
|
|
27
|
+
apos: "'",
|
|
28
|
+
ensp: "\u2002",
|
|
29
|
+
emsp: "\u2003",
|
|
30
|
+
nbsp: "\xA0",
|
|
31
|
+
semi: ";",
|
|
32
|
+
ndash: "–",
|
|
33
|
+
mdash: "—",
|
|
34
|
+
middot: "·",
|
|
35
|
+
lsquo: "‘",
|
|
36
|
+
rsquo: "’",
|
|
37
|
+
ldquo: "“",
|
|
38
|
+
rdquo: "”",
|
|
39
|
+
bull: "•",
|
|
40
|
+
hellip: "…",
|
|
41
|
+
},
|
|
42
|
+
// 默认的标签样式
|
|
43
|
+
tagStyle: {
|
|
44
|
+
// #ifndef APP-PLUS-NVUE
|
|
45
|
+
address: "font-style:italic",
|
|
46
|
+
big: "display:inline;font-size:1.2em",
|
|
47
|
+
caption: "display:table-caption;text-align:center",
|
|
48
|
+
center: "text-align:center",
|
|
49
|
+
cite: "font-style:italic",
|
|
50
|
+
dd: "margin-left:40px",
|
|
51
|
+
mark: "background-color:yellow",
|
|
52
|
+
pre: "font-family:monospace;white-space:pre",
|
|
53
|
+
s: "text-decoration:line-through",
|
|
54
|
+
small: "display:inline;font-size:0.8em",
|
|
55
|
+
u: "text-decoration:underline", // #endif
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
const { windowWidth } = uni.getSystemInfoSync();
|
|
59
|
+
const blankChar = makeMap(" ,\r,\n,\t,\f");
|
|
60
|
+
let idIndex = 0; // #ifdef H5 || APP-PLUS
|
|
61
|
+
|
|
62
|
+
config.ignoreTags.iframe = void 0;
|
|
63
|
+
config.trustTags.iframe = true;
|
|
64
|
+
config.ignoreTags.embed = void 0;
|
|
65
|
+
config.trustTags.embed = true; // #endif
|
|
66
|
+
// #ifdef APP-PLUS-NVUE
|
|
67
|
+
|
|
68
|
+
config.ignoreTags.source = void 0;
|
|
69
|
+
config.ignoreTags.style = void 0; // #endif
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @description 创建 map
|
|
73
|
+
* @param {String} str 逗号分隔
|
|
74
|
+
*/
|
|
75
|
+
|
|
76
|
+
function makeMap(str) {
|
|
77
|
+
const map = Object.create(null);
|
|
78
|
+
const list = str.split(",");
|
|
79
|
+
|
|
80
|
+
for (let i = list.length; i--;) {
|
|
81
|
+
map[list[i]] = true;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return map;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* @description 解码 html 实体
|
|
88
|
+
* @param {String} str 要解码的字符串
|
|
89
|
+
* @param {Boolean} amp 要不要解码 &
|
|
90
|
+
* @returns {String} 解码后的字符串
|
|
91
|
+
*/
|
|
92
|
+
|
|
93
|
+
function decodeEntity(str, amp) {
|
|
94
|
+
let i = str.indexOf("&");
|
|
95
|
+
|
|
96
|
+
while (i != -1) {
|
|
97
|
+
const j = str.indexOf(";", i + 3);
|
|
98
|
+
let code = void 0;
|
|
99
|
+
if (j == -1) break;
|
|
100
|
+
|
|
101
|
+
if (str[i + 1] == "#") {
|
|
102
|
+
// { 形式的实体
|
|
103
|
+
code = parseInt((str[i + 2] == "x" ? "0" : "") + str.substring(i + 2, j));
|
|
104
|
+
if (!isNaN(code)) str = str.substr(0, i) + String.fromCharCode(code) + str.substr(j + 1);
|
|
105
|
+
} else {
|
|
106
|
+
// 形式的实体
|
|
107
|
+
code = str.substring(i + 1, j);
|
|
108
|
+
if (config.entities[code] || (code == "amp" && amp))
|
|
109
|
+
str = str.substr(0, i) + (config.entities[code] || "&") + str.substr(j + 1);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
i = str.indexOf("&", i + 1);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return str;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* @description html 解析器
|
|
119
|
+
* @param {Object} vm 组件实例
|
|
120
|
+
*/
|
|
121
|
+
|
|
122
|
+
class parser {
|
|
123
|
+
constructor(vm) {
|
|
124
|
+
this.options = vm || {};
|
|
125
|
+
this.tagStyle = Object.assign(config.tagStyle, this.options.tagStyle);
|
|
126
|
+
this.imgList = vm.imgList || [];
|
|
127
|
+
this.plugins = vm.plugins || [];
|
|
128
|
+
this.attrs = Object.create(null);
|
|
129
|
+
this.stack = [];
|
|
130
|
+
this.nodes = [];
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* @description 执行解析
|
|
134
|
+
* @param {String} content 要解析的文本
|
|
135
|
+
*/
|
|
136
|
+
parse(content) {
|
|
137
|
+
// 插件处理
|
|
138
|
+
for (let i = this.plugins.length; i--;) {
|
|
139
|
+
if (this.plugins[i].onUpdate) content = this.plugins[i].onUpdate(content, config) || content;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
new lexer(this).parse(content); // 出栈未闭合的标签
|
|
143
|
+
|
|
144
|
+
while (this.stack.length) {
|
|
145
|
+
this.popNode();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return this.nodes;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* @description 将标签暴露出来(不被 rich-text 包含)
|
|
152
|
+
*/
|
|
153
|
+
expose() {
|
|
154
|
+
// #ifndef APP-PLUS-NVUE
|
|
155
|
+
for (let i = this.stack.length; i--;) {
|
|
156
|
+
const item = this.stack[i];
|
|
157
|
+
if (item.name == "a" || item.c) return;
|
|
158
|
+
item.c = 1;
|
|
159
|
+
} // #endif
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* @description 处理插件
|
|
164
|
+
* @param {Object} node 要处理的标签
|
|
165
|
+
* @returns {Boolean} 是否要移除此标签
|
|
166
|
+
*/
|
|
167
|
+
|
|
168
|
+
hook(node) {
|
|
169
|
+
for (let i = this.plugins.length; i--;) {
|
|
170
|
+
if (this.plugins[i].onParse && this.plugins[i].onParse(node, this) == false) return false;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* @description 将链接拼接上主域名
|
|
177
|
+
* @param {String} url 需要拼接的链接
|
|
178
|
+
* @returns {String} 拼接后的链接
|
|
179
|
+
*/
|
|
180
|
+
|
|
181
|
+
getUrl(url) {
|
|
182
|
+
const { domain } = this.options;
|
|
183
|
+
|
|
184
|
+
if (url[0] == "/") {
|
|
185
|
+
// // 开头的补充协议名
|
|
186
|
+
if (url[1] == "/") url = `${domain ? domain.split("://")[0] : "http"}:${url}`; // 否则补充整个域名
|
|
187
|
+
else if (domain) url = domain + url;
|
|
188
|
+
} else if (domain && !url.includes("data:") && !url.includes("://")) url = `${domain}/${url}`;
|
|
189
|
+
|
|
190
|
+
return url;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* @description 解析样式表
|
|
194
|
+
* @param {Object} node 标签
|
|
195
|
+
* @returns {Object}
|
|
196
|
+
*/
|
|
197
|
+
|
|
198
|
+
parseStyle(node) {
|
|
199
|
+
const { attrs } = node;
|
|
200
|
+
const list = (this.tagStyle[node.name] || "").split(";").concat((attrs.style || "").split(";"));
|
|
201
|
+
const styleObj = {};
|
|
202
|
+
let tmp = "";
|
|
203
|
+
|
|
204
|
+
if (attrs.id) {
|
|
205
|
+
// 暴露锚点
|
|
206
|
+
if (this.options.useAnchor) this.expose();
|
|
207
|
+
else if (node.name != "img" && node.name != "a" && node.name != "video" && node.name != "audio")
|
|
208
|
+
attrs.id = void 0;
|
|
209
|
+
} // 转换 width 和 height 属性
|
|
210
|
+
|
|
211
|
+
if (attrs.width) {
|
|
212
|
+
styleObj.width = parseFloat(attrs.width) + (attrs.width.includes("%") ? "%" : "px");
|
|
213
|
+
attrs.width = void 0;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (attrs.height) {
|
|
217
|
+
styleObj.height = parseFloat(attrs.height) + (attrs.height.includes("%") ? "%" : "px");
|
|
218
|
+
attrs.height = void 0;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
for (let i = 0, len = list.length; i < len; i++) {
|
|
222
|
+
const info = list[i].split(":");
|
|
223
|
+
if (info.length < 2) continue;
|
|
224
|
+
const key = info.shift().trim().toLowerCase();
|
|
225
|
+
let value = info.join(":").trim(); // 兼容性的 css 不压缩
|
|
226
|
+
|
|
227
|
+
if ((value[0] == "-" && value.lastIndexOf("-") > 0) || value.includes("safe"))
|
|
228
|
+
tmp += ";".concat(key, ":").concat(value); // 重复的样式进行覆盖
|
|
229
|
+
else if (!styleObj[key] || value.includes("import") || !styleObj[key].includes("import")) {
|
|
230
|
+
// 填充链接
|
|
231
|
+
if (value.includes("url")) {
|
|
232
|
+
let j = value.indexOf("(") + 1;
|
|
233
|
+
|
|
234
|
+
if (j) {
|
|
235
|
+
while (value[j] == '"' || value[j] == "'" || blankChar[value[j]]) {
|
|
236
|
+
j++;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
value = value.substr(0, j) + this.getUrl(value.substr(j));
|
|
240
|
+
}
|
|
241
|
+
} // 转换 rpx(rich-text 内部不支持 rpx)
|
|
242
|
+
else if (value.includes("rpx")) {
|
|
243
|
+
value = value.replace(/[0-9.]+\s*rpx/g, ($) => `${(parseFloat($) * windowWidth) / 750}px`);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
styleObj[key] = value;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
node.attrs.style = tmp;
|
|
251
|
+
return styleObj;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* @description 解析到标签名
|
|
255
|
+
* @param {String} name 标签名
|
|
256
|
+
* @private
|
|
257
|
+
*/
|
|
258
|
+
onTagName(name) {
|
|
259
|
+
this.tagName = this.xml ? name : name.toLowerCase();
|
|
260
|
+
if (this.tagName == "svg") this.xml = true; // svg 标签内大小写敏感
|
|
261
|
+
};
|
|
262
|
+
/**
|
|
263
|
+
* @description 解析到属性名
|
|
264
|
+
* @param {String} name 属性名
|
|
265
|
+
* @private
|
|
266
|
+
*/
|
|
267
|
+
onAttrName(name) {
|
|
268
|
+
name = this.xml ? name : name.toLowerCase();
|
|
269
|
+
|
|
270
|
+
if (name.substr(0, 5) == "data-") {
|
|
271
|
+
// data-src 自动转为 src
|
|
272
|
+
if (name == "data-src" && !this.attrs.src)
|
|
273
|
+
this.attrName = "src"; // a 和 img 标签保留 data- 的属性,可以在 imgtap 和 linktap 事件中使用
|
|
274
|
+
else if (this.tagName == "img" || this.tagName == "a") this.attrName = name; // 剩余的移除以减小大小
|
|
275
|
+
else this.attrName = void 0;
|
|
276
|
+
} else {
|
|
277
|
+
this.attrName = name;
|
|
278
|
+
this.attrs[name] = "T"; // boolean 型属性缺省设置
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
/**
|
|
282
|
+
* @description 解析到属性值
|
|
283
|
+
* @param {String} val 属性值
|
|
284
|
+
* @private
|
|
285
|
+
*/
|
|
286
|
+
onAttrVal(val) {
|
|
287
|
+
const name = this.attrName || ""; // 部分属性进行实体解码
|
|
288
|
+
|
|
289
|
+
if (name == "style" || name == "href") this.attrs[name] = decodeEntity(val, true); // 拼接主域名
|
|
290
|
+
else if (name.includes("src")) this.attrs[name] = this.getUrl(decodeEntity(val, true));
|
|
291
|
+
else if (name) this.attrs[name] = val;
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* @description 解析到标签开始
|
|
296
|
+
* @param {Boolean} selfClose 是否有自闭合标识 />
|
|
297
|
+
* @private
|
|
298
|
+
*/
|
|
299
|
+
onOpenTag(selfClose) {
|
|
300
|
+
// 拼装 node
|
|
301
|
+
const node = Object.create(null);
|
|
302
|
+
node.name = this.tagName;
|
|
303
|
+
node.attrs = this.attrs;
|
|
304
|
+
this.attrs = Object.create(null);
|
|
305
|
+
const { attrs } = node;
|
|
306
|
+
const parent = this.stack[this.stack.length - 1];
|
|
307
|
+
const siblings = parent ? parent.children : this.nodes;
|
|
308
|
+
const close = this.xml ? selfClose : config.voidTags[node.name]; // 转换 embed 标签
|
|
309
|
+
|
|
310
|
+
if (node.name == "embed") {
|
|
311
|
+
// #ifndef H5 || APP-PLUS
|
|
312
|
+
const src = attrs.src || ""; // 按照后缀名和 type 将 embed 转为 video 或 audio
|
|
313
|
+
|
|
314
|
+
if (src.includes(".mp4") || src.includes(".3gp") || src.includes(".m3u8") || (attrs.type || "").includes("video"))
|
|
315
|
+
node.name = "video";
|
|
316
|
+
else if (
|
|
317
|
+
src.includes(".mp3") ||
|
|
318
|
+
src.includes(".wav") ||
|
|
319
|
+
src.includes(".aac") ||
|
|
320
|
+
src.includes(".m4a") ||
|
|
321
|
+
(attrs.type || "").includes("audio")
|
|
322
|
+
)
|
|
323
|
+
node.name = "audio";
|
|
324
|
+
if (attrs.autostart) attrs.autoplay = "T";
|
|
325
|
+
attrs.controls = "T"; // #endif
|
|
326
|
+
// #ifdef H5 || APP-PLUS
|
|
327
|
+
|
|
328
|
+
this.expose(); // #endif
|
|
329
|
+
} // #ifndef APP-PLUS-NVUE
|
|
330
|
+
// 处理音视频
|
|
331
|
+
|
|
332
|
+
if (node.name == "video" || node.name == "audio") {
|
|
333
|
+
// 设置 id 以便获取 context
|
|
334
|
+
if (node.name == "video" && !attrs.id) attrs.id = `v${idIndex++}`; // 没有设置 controls 也没有设置 autoplay 的自动设置 controls
|
|
335
|
+
|
|
336
|
+
if (!attrs.controls && !attrs.autoplay) attrs.controls = "T"; // 用数组存储所有可用的 source
|
|
337
|
+
|
|
338
|
+
node.src = [];
|
|
339
|
+
|
|
340
|
+
if (attrs.src) {
|
|
341
|
+
node.src.push(attrs.src);
|
|
342
|
+
attrs.src = void 0;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
this.expose();
|
|
346
|
+
} // #endif
|
|
347
|
+
// 处理自闭合标签
|
|
348
|
+
|
|
349
|
+
if (close) {
|
|
350
|
+
if (!this.hook(node) || config.ignoreTags[node.name]) {
|
|
351
|
+
// 通过 base 标签设置主域名
|
|
352
|
+
if (node.name == "base" && !this.options.domain) this.options.domain = attrs.href; // #ifndef APP-PLUS-NVUE
|
|
353
|
+
// 设置 source 标签(仅父节点为 video 或 audio 时有效)
|
|
354
|
+
else if (node.name == "source" && parent && (parent.name == "video" || parent.name == "audio") && attrs.src)
|
|
355
|
+
parent.src.push(attrs.src); // #endif
|
|
356
|
+
|
|
357
|
+
return;
|
|
358
|
+
} // 解析 style
|
|
359
|
+
|
|
360
|
+
const styleObj = this.parseStyle(node); // 处理图片
|
|
361
|
+
|
|
362
|
+
if (node.name == "img") {
|
|
363
|
+
if (attrs.src) {
|
|
364
|
+
// 标记 webp
|
|
365
|
+
if (attrs.src.includes("webp")) node.webp = "T"; // data url 图片如果没有设置 original-src 默认为不可预览的小图片
|
|
366
|
+
|
|
367
|
+
if (attrs.src.includes("data:") && !attrs["original-src"]) attrs.ignore = "T";
|
|
368
|
+
|
|
369
|
+
if (!attrs.ignore || node.webp || attrs.src.includes("cloud://")) {
|
|
370
|
+
for (let i = this.stack.length; i--;) {
|
|
371
|
+
const item = this.stack[i];
|
|
372
|
+
|
|
373
|
+
if (item.name == "a") {
|
|
374
|
+
node.a = item.attrs;
|
|
375
|
+
break;
|
|
376
|
+
} // #ifndef H5 || APP-PLUS
|
|
377
|
+
|
|
378
|
+
const style = item.attrs.style || "";
|
|
379
|
+
|
|
380
|
+
if (
|
|
381
|
+
style.includes("flex:") &&
|
|
382
|
+
!style.includes("flex:0") &&
|
|
383
|
+
!style.includes("flex: 0") &&
|
|
384
|
+
(!styleObj.width || !styleObj.width.includes("%"))
|
|
385
|
+
) {
|
|
386
|
+
styleObj.width = "100% !important";
|
|
387
|
+
styleObj.height = "";
|
|
388
|
+
|
|
389
|
+
for (let j = i + 1; j < this.stack.length; j++) {
|
|
390
|
+
this.stack[j].attrs.style = (this.stack[j].attrs.style || "").replace("inline-", "");
|
|
391
|
+
}
|
|
392
|
+
} else if (style.includes("flex") && styleObj.width == "100%") {
|
|
393
|
+
for (let _j = i + 1; _j < this.stack.length; _j++) {
|
|
394
|
+
const _style = this.stack[_j].attrs.style || "";
|
|
395
|
+
|
|
396
|
+
if (!_style.includes(";width") && !_style.includes(" width") && _style.indexOf("width") != 0) {
|
|
397
|
+
styleObj.width = "";
|
|
398
|
+
break;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
} else if (style.includes("inline-block")) {
|
|
402
|
+
if (styleObj.width && styleObj.width[styleObj.width.length - 1] == "%") {
|
|
403
|
+
item.attrs.style += `;max-width:${styleObj.width}`;
|
|
404
|
+
styleObj.width = "";
|
|
405
|
+
} else item.attrs.style += ";max-width:100%";
|
|
406
|
+
} // #endif
|
|
407
|
+
|
|
408
|
+
item.c = 1;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
attrs.i = this.imgList.length.toString();
|
|
412
|
+
|
|
413
|
+
let _src = attrs["original-src"] || attrs.src; // #ifndef H5 || MP-ALIPAY || APP-PLUS || MP-360
|
|
414
|
+
|
|
415
|
+
if (this.imgList.includes(_src)) {
|
|
416
|
+
// 如果有重复的链接则对域名进行随机大小写变换避免预览时错位
|
|
417
|
+
let _i = _src.indexOf("://");
|
|
418
|
+
|
|
419
|
+
if (_i != -1) {
|
|
420
|
+
_i += 3;
|
|
421
|
+
|
|
422
|
+
let newSrc = _src.substr(0, _i);
|
|
423
|
+
|
|
424
|
+
for (; _i < _src.length; _i++) {
|
|
425
|
+
if (_src[_i] == "/") break;
|
|
426
|
+
newSrc += Math.random() > 0.5 ? _src[_i].toUpperCase() : _src[_i];
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
newSrc += _src.substr(_i);
|
|
430
|
+
_src = newSrc;
|
|
431
|
+
}
|
|
432
|
+
} // #endif
|
|
433
|
+
|
|
434
|
+
this.imgList.push(_src); // #ifdef H5 || APP-PLUS
|
|
435
|
+
|
|
436
|
+
if (this.options.lazyLoad) {
|
|
437
|
+
attrs["data-src"] = attrs.src;
|
|
438
|
+
attrs.src = void 0;
|
|
439
|
+
} // #endif
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
if (styleObj.display == "inline") styleObj.display = ""; // #ifndef APP-PLUS-NVUE
|
|
444
|
+
|
|
445
|
+
if (attrs.ignore) {
|
|
446
|
+
styleObj["max-width"] = styleObj["max-width"] || "100%";
|
|
447
|
+
attrs.style += ";-webkit-touch-callout:none";
|
|
448
|
+
} // #endif
|
|
449
|
+
// 设置的宽度超出屏幕,为避免变形,高度转为自动
|
|
450
|
+
|
|
451
|
+
if (parseInt(styleObj.width) > windowWidth) styleObj.height = void 0; // 记录是否设置了宽高
|
|
452
|
+
|
|
453
|
+
if (styleObj.width) {
|
|
454
|
+
if (styleObj.width.includes("auto")) styleObj.width = "";
|
|
455
|
+
else {
|
|
456
|
+
node.w = "T";
|
|
457
|
+
if (styleObj.height && !styleObj.height.includes("auto")) node.h = "T";
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
} else if (node.name == "svg") {
|
|
461
|
+
siblings.push(node);
|
|
462
|
+
this.stack.push(node);
|
|
463
|
+
this.popNode();
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
for (const key in styleObj) {
|
|
468
|
+
if (styleObj[key]) attrs.style += ";".concat(key, ":").concat(styleObj[key].replace(" !important", ""));
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
attrs.style = attrs.style.substr(1) || void 0;
|
|
472
|
+
} else {
|
|
473
|
+
if (node.name == "pre" || ((attrs.style || "").includes("white-space") && attrs.style.includes("pre")))
|
|
474
|
+
this.pre = node.pre = true;
|
|
475
|
+
node.children = [];
|
|
476
|
+
this.stack.push(node);
|
|
477
|
+
} // 加入节点树
|
|
478
|
+
|
|
479
|
+
siblings.push(node);
|
|
480
|
+
};
|
|
481
|
+
/**
|
|
482
|
+
* @description 解析到标签结束
|
|
483
|
+
* @param {String} name 标签名
|
|
484
|
+
* @private
|
|
485
|
+
*/
|
|
486
|
+
|
|
487
|
+
onCloseTag(name) {
|
|
488
|
+
// 依次出栈到匹配为止
|
|
489
|
+
name = this.xml ? name : name.toLowerCase();
|
|
490
|
+
let i;
|
|
491
|
+
|
|
492
|
+
for (i = this.stack.length; i--;) {
|
|
493
|
+
if (this.stack[i].name == name) break;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
if (i != -1) {
|
|
497
|
+
while (this.stack.length > i) {
|
|
498
|
+
this.popNode();
|
|
499
|
+
}
|
|
500
|
+
} else if (name == "p" || name == "br") {
|
|
501
|
+
const siblings = this.stack.length ? this.stack[this.stack.length - 1].children : this.nodes;
|
|
502
|
+
siblings.push({
|
|
503
|
+
name,
|
|
504
|
+
attrs: {},
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
/**
|
|
509
|
+
* @description 处理标签出栈
|
|
510
|
+
* @private
|
|
511
|
+
*/
|
|
512
|
+
|
|
513
|
+
popNode() {
|
|
514
|
+
const node = this.stack.pop();
|
|
515
|
+
let { attrs } = node;
|
|
516
|
+
const { children } = node;
|
|
517
|
+
const parent = this.stack[this.stack.length - 1];
|
|
518
|
+
const siblings = parent ? parent.children : this.nodes;
|
|
519
|
+
|
|
520
|
+
if (!this.hook(node) || config.ignoreTags[node.name]) {
|
|
521
|
+
// 获取标题
|
|
522
|
+
if (node.name == "title" && children.length && children[0].type == "text" && this.options.setTitle) {
|
|
523
|
+
uni.setNavigationBarTitle({
|
|
524
|
+
title: children[0].text,
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
siblings.pop();
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
if (node.pre) {
|
|
532
|
+
// 是否合并空白符标识
|
|
533
|
+
node.pre = this.pre = void 0;
|
|
534
|
+
|
|
535
|
+
for (let i = this.stack.length; i--;) {
|
|
536
|
+
if (this.stack[i].pre) this.pre = true;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
const styleObj = {}; // 转换 svg
|
|
541
|
+
|
|
542
|
+
if (node.name == "svg") {
|
|
543
|
+
// #ifndef APP-PLUS-NVUE
|
|
544
|
+
let src = "";
|
|
545
|
+
const { style } = attrs;
|
|
546
|
+
attrs.style = "";
|
|
547
|
+
attrs.xmlns = "http://www.w3.org/2000/svg";
|
|
548
|
+
|
|
549
|
+
(function traversal(node) {
|
|
550
|
+
src += `<${node.name}`;
|
|
551
|
+
|
|
552
|
+
for (let item in node.attrs) {
|
|
553
|
+
const val = node.attrs[item];
|
|
554
|
+
|
|
555
|
+
if (val) {
|
|
556
|
+
if (item == "viewbox") item = "viewBox";
|
|
557
|
+
src += " ".concat(item, '="').concat(val, '"');
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
if (!node.children) src += "/>";
|
|
562
|
+
else {
|
|
563
|
+
src += ">";
|
|
564
|
+
|
|
565
|
+
for (let _i2 = 0; _i2 < node.children.length; _i2++) {
|
|
566
|
+
traversal(node.children[_i2]);
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
src += `</${node.name}>`;
|
|
570
|
+
}
|
|
571
|
+
})(node);
|
|
572
|
+
|
|
573
|
+
node.name = "img";
|
|
574
|
+
node.attrs = {
|
|
575
|
+
src: `data:image/svg+xml;utf8,${src.replace(/#/g, "%23")}`,
|
|
576
|
+
style,
|
|
577
|
+
ignore: "T",
|
|
578
|
+
};
|
|
579
|
+
node.children = void 0; // #endif
|
|
580
|
+
|
|
581
|
+
this.xml = false;
|
|
582
|
+
return;
|
|
583
|
+
} // #ifndef APP-PLUS-NVUE
|
|
584
|
+
// 转换 align 属性
|
|
585
|
+
|
|
586
|
+
if (attrs.align) {
|
|
587
|
+
if (node.name == "table") {
|
|
588
|
+
if (attrs.align == "center") styleObj["margin-inline-start"] = styleObj["margin-inline-end"] = "auto";
|
|
589
|
+
else styleObj.float = attrs.align;
|
|
590
|
+
} else styleObj["text-align"] = attrs.align;
|
|
591
|
+
|
|
592
|
+
attrs.align = void 0;
|
|
593
|
+
} // 转换 font 标签的属性
|
|
594
|
+
|
|
595
|
+
if (node.name == "font") {
|
|
596
|
+
if (attrs.color) {
|
|
597
|
+
styleObj.color = attrs.color;
|
|
598
|
+
attrs.color = void 0;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
if (attrs.face) {
|
|
602
|
+
styleObj["font-family"] = attrs.face;
|
|
603
|
+
attrs.face = void 0;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
if (attrs.size) {
|
|
607
|
+
let size = parseInt(attrs.size);
|
|
608
|
+
|
|
609
|
+
if (!isNaN(size)) {
|
|
610
|
+
if (size < 1) size = 1;
|
|
611
|
+
else if (size > 7) size = 7;
|
|
612
|
+
styleObj["font-size"] = ["xx-small", "x-small", "small", "medium", "large", "x-large", "xx-large"][size - 1];
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
attrs.size = void 0;
|
|
616
|
+
}
|
|
617
|
+
} // #endif
|
|
618
|
+
// 一些编辑器的自带 class
|
|
619
|
+
|
|
620
|
+
if ((attrs.class || "").includes("align-center")) styleObj["text-align"] = "center";
|
|
621
|
+
Object.assign(styleObj, this.parseStyle(node));
|
|
622
|
+
|
|
623
|
+
if (parseInt(styleObj.width) > windowWidth) {
|
|
624
|
+
styleObj["max-width"] = "100%";
|
|
625
|
+
styleObj["box-sizing"] = "border-box";
|
|
626
|
+
} // #ifndef APP-PLUS-NVUE
|
|
627
|
+
|
|
628
|
+
if (config.blockTags[node.name]) node.name = "div"; // 未知标签转为 span,避免无法显示
|
|
629
|
+
else if (!config.trustTags[node.name] && !this.xml) node.name = "span";
|
|
630
|
+
if (node.name == 'a' || node.name == 'ad' // #ifdef H5 || APP-PLUS
|
|
631
|
+
|| node.name == 'iframe' // #endif
|
|
632
|
+
) this.expose() // #ifdef APP-PLUS
|
|
633
|
+
else if (node.name == "video") {
|
|
634
|
+
let str = '<video style="width:100%;height:100%"'; // 空白图占位
|
|
635
|
+
|
|
636
|
+
if (!attrs.poster && !attrs.autoplay)
|
|
637
|
+
attrs.poster = "data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg'/>";
|
|
638
|
+
|
|
639
|
+
for (const item in attrs) {
|
|
640
|
+
if (attrs[item]) str += ` ${item}="${attrs[item]}"`;
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
if (this.options.pauseVideo)
|
|
644
|
+
str += " onplay=\"for(var e=document.getElementsByTagName('video'),t=0;t<e.length;t++)e[t]!=this&&e[t].pause()\"";
|
|
645
|
+
str += ">";
|
|
646
|
+
|
|
647
|
+
for (let _i3 = 0; _i3 < node.src.length; _i3++) {
|
|
648
|
+
str += `<source src="${node.src[_i3]}">`;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
str += "</video>";
|
|
652
|
+
node.html = str;
|
|
653
|
+
} // #endif
|
|
654
|
+
// 列表处理
|
|
655
|
+
else if ((node.name == "ul" || node.name == "ol") && node.c) {
|
|
656
|
+
const types = {
|
|
657
|
+
a: "lower-alpha",
|
|
658
|
+
A: "upper-alpha",
|
|
659
|
+
i: "lower-roman",
|
|
660
|
+
I: "upper-roman",
|
|
661
|
+
};
|
|
662
|
+
|
|
663
|
+
if (types[attrs.type]) {
|
|
664
|
+
attrs.style += `;list-style-type:${types[attrs.type]}`;
|
|
665
|
+
attrs.type = void 0;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
for (let _i4 = children.length; _i4--;) {
|
|
669
|
+
if (children[_i4].name == "li") children[_i4].c = 1;
|
|
670
|
+
}
|
|
671
|
+
} // 表格处理
|
|
672
|
+
else if (node.name == "table") {
|
|
673
|
+
// cellpadding、cellspacing、border 这几个常用表格属性需要通过转换实现
|
|
674
|
+
let padding = parseFloat(attrs.cellpadding);
|
|
675
|
+
let spacing = parseFloat(attrs.cellspacing);
|
|
676
|
+
const border = parseFloat(attrs.border);
|
|
677
|
+
|
|
678
|
+
if (node.c) {
|
|
679
|
+
// padding 和 spacing 默认 2
|
|
680
|
+
if (isNaN(padding)) padding = 2;
|
|
681
|
+
if (isNaN(spacing)) spacing = 2;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
if (border) attrs.style += `;border:${border}px solid gray`;
|
|
685
|
+
|
|
686
|
+
if (node.flag && node.c) {
|
|
687
|
+
// 有 colspan 或 rowspan 且含有链接的表格通过 grid 布局实现
|
|
688
|
+
styleObj.display = "grid";
|
|
689
|
+
|
|
690
|
+
if (spacing) {
|
|
691
|
+
styleObj["grid-gap"] = `${spacing}px`;
|
|
692
|
+
styleObj.padding = `${spacing}px`;
|
|
693
|
+
} // 无间隔的情况下避免边框重叠
|
|
694
|
+
else if (border) attrs.style += ";border-left:0;border-top:0";
|
|
695
|
+
|
|
696
|
+
const width = [];
|
|
697
|
+
// 表格的列宽
|
|
698
|
+
const trList = [];
|
|
699
|
+
// tr 列表
|
|
700
|
+
const cells = [];
|
|
701
|
+
// 保存新的单元格
|
|
702
|
+
const map = {}; // 被合并单元格占用的格子
|
|
703
|
+
|
|
704
|
+
(function traversal(nodes) {
|
|
705
|
+
for (let _i5 = 0; _i5 < nodes.length; _i5++) {
|
|
706
|
+
if (nodes[_i5].name == "tr") trList.push(nodes[_i5]);
|
|
707
|
+
else traversal(nodes[_i5].children || []);
|
|
708
|
+
}
|
|
709
|
+
})(children);
|
|
710
|
+
|
|
711
|
+
for (let row = 1; row <= trList.length; row++) {
|
|
712
|
+
let col = 1;
|
|
713
|
+
|
|
714
|
+
for (let j = 0; j < trList[row - 1].children.length; j++, col++) {
|
|
715
|
+
const td = trList[row - 1].children[j];
|
|
716
|
+
|
|
717
|
+
if (td.name == "td" || td.name == "th") {
|
|
718
|
+
// 这个格子被上面的单元格占用,则列号++
|
|
719
|
+
while (map[`${row}.${col}`]) {
|
|
720
|
+
col++;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
let _style2 = td.attrs.style || "";
|
|
724
|
+
const start = _style2.indexOf("width") ? _style2.indexOf(";width") : 0; // 提取出 td 的宽度
|
|
725
|
+
|
|
726
|
+
if (start != -1) {
|
|
727
|
+
let end = _style2.indexOf(";", start + 6);
|
|
728
|
+
|
|
729
|
+
if (end == -1) end = _style2.length;
|
|
730
|
+
if (!td.attrs.colspan) width[col] = _style2.substring(start ? start + 7 : 6, end);
|
|
731
|
+
_style2 = _style2.substr(0, start) + _style2.substr(end);
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
_style2 +=
|
|
735
|
+
(border
|
|
736
|
+
? ";border:".concat(border, "px solid gray") + (spacing ? "" : ";border-right:0;border-bottom:0")
|
|
737
|
+
: "") + (padding ? ";padding:".concat(padding, "px") : ""); // 处理列合并
|
|
738
|
+
|
|
739
|
+
if (td.attrs.colspan) {
|
|
740
|
+
_style2 += ";grid-column-start:"
|
|
741
|
+
.concat(col, ";grid-column-end:")
|
|
742
|
+
.concat(col + parseInt(td.attrs.colspan));
|
|
743
|
+
if (!td.attrs.rowspan) _style2 += ";grid-row-start:".concat(row, ";grid-row-end:").concat(row + 1);
|
|
744
|
+
col += parseInt(td.attrs.colspan) - 1;
|
|
745
|
+
} // 处理行合并
|
|
746
|
+
|
|
747
|
+
if (td.attrs.rowspan) {
|
|
748
|
+
_style2 += ";grid-row-start:".concat(row, ";grid-row-end:").concat(row + parseInt(td.attrs.rowspan));
|
|
749
|
+
if (!td.attrs.colspan) _style2 += ";grid-column-start:".concat(col, ";grid-column-end:").concat(col + 1); // 记录下方单元格被占用
|
|
750
|
+
|
|
751
|
+
for (let k = 1; k < td.attrs.rowspan; k++) {
|
|
752
|
+
map[`${row + k}.${col}`] = 1;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
if (_style2) td.attrs.style = _style2;
|
|
757
|
+
cells.push(td);
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
if (row == 1) {
|
|
762
|
+
let temp = "";
|
|
763
|
+
|
|
764
|
+
for (let _i6 = 1; _i6 < col; _i6++) {
|
|
765
|
+
temp += `${width[_i6] ? width[_i6] : "auto"} `;
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
styleObj["grid-template-columns"] = temp;
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
node.children = cells;
|
|
773
|
+
} else {
|
|
774
|
+
// 没有使用合并单元格的表格通过 table 布局实现
|
|
775
|
+
if (node.c) styleObj.display = "table";
|
|
776
|
+
if (!isNaN(spacing)) styleObj["border-spacing"] = `${spacing}px`;
|
|
777
|
+
|
|
778
|
+
if (border || padding) {
|
|
779
|
+
// 遍历
|
|
780
|
+
(function traversal(nodes) {
|
|
781
|
+
for (let _i7 = 0; _i7 < nodes.length; _i7++) {
|
|
782
|
+
const _td = nodes[_i7];
|
|
783
|
+
|
|
784
|
+
if (_td.name == "th" || _td.name == "td") {
|
|
785
|
+
if (border) _td.attrs.style = "border:".concat(border, "px solid gray;").concat(_td.attrs.style || "");
|
|
786
|
+
if (padding) _td.attrs.style = "padding:".concat(padding, "px;").concat(_td.attrs.style || "");
|
|
787
|
+
} else if (_td.children) traversal(_td.children);
|
|
788
|
+
}
|
|
789
|
+
})(children);
|
|
790
|
+
}
|
|
791
|
+
} // 给表格添加一个单独的横向滚动层
|
|
792
|
+
|
|
793
|
+
if (this.options.scrollTable && !(attrs.style || "").includes("inline")) {
|
|
794
|
+
const table = { ...node };
|
|
795
|
+
node.name = "div";
|
|
796
|
+
node.attrs = {
|
|
797
|
+
style: "overflow:auto",
|
|
798
|
+
};
|
|
799
|
+
node.children = [table];
|
|
800
|
+
attrs = table.attrs;
|
|
801
|
+
}
|
|
802
|
+
} else if ((node.name == "td" || node.name == "th") && (attrs.colspan || attrs.rowspan)) {
|
|
803
|
+
for (let _i8 = this.stack.length; _i8--;) {
|
|
804
|
+
if (this.stack[_i8].name == "table") {
|
|
805
|
+
this.stack[_i8].flag = 1; // 指示含有合并单元格
|
|
806
|
+
|
|
807
|
+
break;
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
} // 转换 ruby
|
|
811
|
+
else if (node.name == "ruby") {
|
|
812
|
+
node.name = "span";
|
|
813
|
+
|
|
814
|
+
for (let _i9 = 0; _i9 < children.length - 1; _i9++) {
|
|
815
|
+
if (children[_i9].type == "text" && children[_i9 + 1].name == "rt") {
|
|
816
|
+
children[_i9] = {
|
|
817
|
+
name: "div",
|
|
818
|
+
attrs: {
|
|
819
|
+
style: "display:inline-block",
|
|
820
|
+
},
|
|
821
|
+
children: [
|
|
822
|
+
{
|
|
823
|
+
name: "div",
|
|
824
|
+
attrs: {
|
|
825
|
+
style: "font-size:50%;text-align:start",
|
|
826
|
+
},
|
|
827
|
+
children: children[_i9 + 1].children,
|
|
828
|
+
},
|
|
829
|
+
children[_i9],
|
|
830
|
+
],
|
|
831
|
+
};
|
|
832
|
+
children.splice(_i9 + 1, 1);
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
} else if (node.c) {
|
|
836
|
+
node.c = 2;
|
|
837
|
+
|
|
838
|
+
for (let _i10 = node.children.length; _i10--;) {
|
|
839
|
+
if (!node.children[_i10].c || node.children[_i10].name == "table") node.c = 1;
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
if ((styleObj.display || "").includes("flex") && !node.c) {
|
|
843
|
+
for (let _i11 = children.length; _i11--;) {
|
|
844
|
+
const _item = children[_i11];
|
|
845
|
+
|
|
846
|
+
if (_item.f) {
|
|
847
|
+
_item.attrs.style = (_item.attrs.style || "") + _item.f;
|
|
848
|
+
_item.f = void 0;
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
} // flex 布局时部分样式需要提取到 rich-text 外层
|
|
852
|
+
|
|
853
|
+
const flex = parent && (parent.attrs.style || '').includes('flex') // #ifdef MP-WEIXIN
|
|
854
|
+
// 检查基础库版本 virtualHost 是否可用
|
|
855
|
+
&& !(node.c && wx.getNFCAdapter) // #endif
|
|
856
|
+
// #ifndef MP-WEIXIN || MP-QQ || MP-BAIDU || MP-TOUTIAO
|
|
857
|
+
&& !node.c // #endif
|
|
858
|
+
|
|
859
|
+
if (flex) node.f = ';max-width:100%' // #endif
|
|
860
|
+
|
|
861
|
+
for (const key in styleObj) {
|
|
862
|
+
if (styleObj[key]) {
|
|
863
|
+
const val = ";".concat(key, ":").concat(styleObj[key].replace(" !important", "")); // #ifndef APP-PLUS-NVUE
|
|
864
|
+
|
|
865
|
+
if (
|
|
866
|
+
flex &&
|
|
867
|
+
((key.includes("flex") && key != "flex-direction") ||
|
|
868
|
+
key == "align-self" ||
|
|
869
|
+
styleObj[key][0] == "-" ||
|
|
870
|
+
(key == "width" && val.includes("%")))
|
|
871
|
+
) {
|
|
872
|
+
node.f += val;
|
|
873
|
+
if (key == "width") attrs.style += ";width:100%";
|
|
874
|
+
} // #endif
|
|
875
|
+
else {
|
|
876
|
+
attrs.style += val;
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
attrs.style = attrs.style.substr(1) || void 0;
|
|
882
|
+
};
|
|
883
|
+
/**
|
|
884
|
+
* @description 解析到文本
|
|
885
|
+
* @param {String} text 文本内容
|
|
886
|
+
*/
|
|
887
|
+
|
|
888
|
+
onText(text) {
|
|
889
|
+
if (!this.pre) {
|
|
890
|
+
// 合并空白符
|
|
891
|
+
let trim = "";
|
|
892
|
+
let flag;
|
|
893
|
+
|
|
894
|
+
for (let i = 0, len = text.length; i < len; i++) {
|
|
895
|
+
if (!blankChar[text[i]]) trim += text[i];
|
|
896
|
+
else {
|
|
897
|
+
if (trim[trim.length - 1] != " ") trim += " ";
|
|
898
|
+
if (text[i] == "\n" && !flag) flag = true;
|
|
899
|
+
}
|
|
900
|
+
} // 去除含有换行符的空串
|
|
901
|
+
|
|
902
|
+
if (trim == " " && flag) return;
|
|
903
|
+
text = trim;
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
const node = Object.create(null);
|
|
907
|
+
node.type = "text";
|
|
908
|
+
node.text = decodeEntity(text);
|
|
909
|
+
|
|
910
|
+
if (this.hook(node)) {
|
|
911
|
+
const siblings = this.stack.length ? this.stack[this.stack.length - 1].children : this.nodes;
|
|
912
|
+
siblings.push(node);
|
|
913
|
+
}
|
|
914
|
+
};
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
/**
|
|
918
|
+
* @description html 词法分析器
|
|
919
|
+
* @param {Object} handler 高层处理器
|
|
920
|
+
*/
|
|
921
|
+
|
|
922
|
+
function lexer(handler) {
|
|
923
|
+
this.handler = handler;
|
|
924
|
+
}
|
|
925
|
+
/**
|
|
926
|
+
* @description 执行解析
|
|
927
|
+
* @param {String} content 要解析的文本
|
|
928
|
+
*/
|
|
929
|
+
|
|
930
|
+
lexer.prototype.parse = function (content) {
|
|
931
|
+
this.content = content || "";
|
|
932
|
+
this.i = 0; // 标记解析位置
|
|
933
|
+
|
|
934
|
+
this.start = 0; // 标记一个单词的开始位置
|
|
935
|
+
|
|
936
|
+
this.state = this.text; // 当前状态
|
|
937
|
+
|
|
938
|
+
for (let len = this.content.length; this.i != -1 && this.i < len;) {
|
|
939
|
+
this.state();
|
|
940
|
+
}
|
|
941
|
+
};
|
|
942
|
+
/**
|
|
943
|
+
* @description 检查标签是否闭合
|
|
944
|
+
* @param {String} method 如果闭合要进行的操作
|
|
945
|
+
* @returns {Boolean} 是否闭合
|
|
946
|
+
* @private
|
|
947
|
+
*/
|
|
948
|
+
|
|
949
|
+
lexer.prototype.checkClose = function (method) {
|
|
950
|
+
const selfClose = this.content[this.i] == "/";
|
|
951
|
+
|
|
952
|
+
if (this.content[this.i] == ">" || (selfClose && this.content[this.i + 1] == ">")) {
|
|
953
|
+
if (method) this.handler[method](this.content.substring(this.start, this.i));
|
|
954
|
+
this.i += selfClose ? 2 : 1;
|
|
955
|
+
this.start = this.i;
|
|
956
|
+
this.handler.onOpenTag(selfClose);
|
|
957
|
+
|
|
958
|
+
if (this.handler.tagName == "script") {
|
|
959
|
+
this.i = this.content.indexOf("</", this.i);
|
|
960
|
+
|
|
961
|
+
if (this.i != -1) {
|
|
962
|
+
this.i += 2;
|
|
963
|
+
this.start = this.i;
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
this.state = this.endTag;
|
|
967
|
+
} else this.state = this.text;
|
|
968
|
+
|
|
969
|
+
return true;
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
return false;
|
|
973
|
+
};
|
|
974
|
+
/**
|
|
975
|
+
* @description 文本状态
|
|
976
|
+
* @private
|
|
977
|
+
*/
|
|
978
|
+
|
|
979
|
+
lexer.prototype.text = function () {
|
|
980
|
+
this.i = this.content.indexOf("<", this.i); // 查找最近的标签
|
|
981
|
+
|
|
982
|
+
if (this.i == -1) {
|
|
983
|
+
// 没有标签了
|
|
984
|
+
if (this.start < this.content.length) this.handler.onText(this.content.substring(this.start, this.content.length));
|
|
985
|
+
return;
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
const c = this.content[this.i + 1];
|
|
989
|
+
|
|
990
|
+
if ((c >= "a" && c <= "z") || (c >= "A" && c <= "Z")) {
|
|
991
|
+
// 标签开头
|
|
992
|
+
if (this.start != this.i) this.handler.onText(this.content.substring(this.start, this.i));
|
|
993
|
+
this.start = ++this.i;
|
|
994
|
+
this.state = this.tagName;
|
|
995
|
+
} else if (c == "/" || c == "!" || c == "?") {
|
|
996
|
+
if (this.start != this.i) this.handler.onText(this.content.substring(this.start, this.i));
|
|
997
|
+
const next = this.content[this.i + 2];
|
|
998
|
+
|
|
999
|
+
if (c == "/" && ((next >= "a" && next <= "z") || (next >= "A" && next <= "Z"))) {
|
|
1000
|
+
// 标签结尾
|
|
1001
|
+
this.i += 2;
|
|
1002
|
+
this.start = this.i;
|
|
1003
|
+
return (this.state = this.endTag);
|
|
1004
|
+
} // 处理注释
|
|
1005
|
+
|
|
1006
|
+
let end = "-->";
|
|
1007
|
+
if (c != "!" || this.content[this.i + 2] != "-" || this.content[this.i + 3] != "-") end = ">";
|
|
1008
|
+
this.i = this.content.indexOf(end, this.i);
|
|
1009
|
+
|
|
1010
|
+
if (this.i != -1) {
|
|
1011
|
+
this.i += end.length;
|
|
1012
|
+
this.start = this.i;
|
|
1013
|
+
}
|
|
1014
|
+
} else this.i++;
|
|
1015
|
+
};
|
|
1016
|
+
/**
|
|
1017
|
+
* @description 标签名状态
|
|
1018
|
+
* @private
|
|
1019
|
+
*/
|
|
1020
|
+
|
|
1021
|
+
lexer.prototype.tagName = function () {
|
|
1022
|
+
if (blankChar[this.content[this.i]]) {
|
|
1023
|
+
// 解析到标签名
|
|
1024
|
+
this.handler.onTagName(this.content.substring(this.start, this.i));
|
|
1025
|
+
|
|
1026
|
+
while (blankChar[this.content[++this.i]]) { }
|
|
1027
|
+
|
|
1028
|
+
if (this.i < this.content.length && !this.checkClose()) {
|
|
1029
|
+
this.start = this.i;
|
|
1030
|
+
this.state = this.attrName;
|
|
1031
|
+
}
|
|
1032
|
+
} else if (!this.checkClose("onTagName")) this.i++;
|
|
1033
|
+
};
|
|
1034
|
+
/**
|
|
1035
|
+
* @description 属性名状态
|
|
1036
|
+
* @private
|
|
1037
|
+
*/
|
|
1038
|
+
|
|
1039
|
+
lexer.prototype.attrName = function () {
|
|
1040
|
+
let c = this.content[this.i];
|
|
1041
|
+
|
|
1042
|
+
if (blankChar[c] || c == "=") {
|
|
1043
|
+
// 解析到属性名
|
|
1044
|
+
this.handler.onAttrName(this.content.substring(this.start, this.i));
|
|
1045
|
+
let needVal = c == "=";
|
|
1046
|
+
const len = this.content.length;
|
|
1047
|
+
|
|
1048
|
+
while (++this.i < len) {
|
|
1049
|
+
c = this.content[this.i];
|
|
1050
|
+
|
|
1051
|
+
if (!blankChar[c]) {
|
|
1052
|
+
if (this.checkClose()) return;
|
|
1053
|
+
|
|
1054
|
+
if (needVal) {
|
|
1055
|
+
// 等号后遇到第一个非空字符
|
|
1056
|
+
this.start = this.i;
|
|
1057
|
+
return (this.state = this.attrVal);
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
if (this.content[this.i] == "=") needVal = true;
|
|
1061
|
+
else {
|
|
1062
|
+
this.start = this.i;
|
|
1063
|
+
return (this.state = this.attrName);
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
} else if (!this.checkClose("onAttrName")) this.i++;
|
|
1068
|
+
};
|
|
1069
|
+
/**
|
|
1070
|
+
* @description 属性值状态
|
|
1071
|
+
* @private
|
|
1072
|
+
*/
|
|
1073
|
+
|
|
1074
|
+
lexer.prototype.attrVal = function () {
|
|
1075
|
+
const c = this.content[this.i];
|
|
1076
|
+
const len = this.content.length; // 有冒号的属性
|
|
1077
|
+
|
|
1078
|
+
if (c == '"' || c == "'") {
|
|
1079
|
+
this.start = ++this.i;
|
|
1080
|
+
this.i = this.content.indexOf(c, this.i);
|
|
1081
|
+
if (this.i == -1) return;
|
|
1082
|
+
this.handler.onAttrVal(this.content.substring(this.start, this.i));
|
|
1083
|
+
} // 没有冒号的属性
|
|
1084
|
+
else {
|
|
1085
|
+
for (; this.i < len; this.i++) {
|
|
1086
|
+
if (blankChar[this.content[this.i]]) {
|
|
1087
|
+
this.handler.onAttrVal(this.content.substring(this.start, this.i));
|
|
1088
|
+
break;
|
|
1089
|
+
} else if (this.checkClose("onAttrVal")) return;
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
while (blankChar[this.content[++this.i]]) { }
|
|
1094
|
+
|
|
1095
|
+
if (this.i < len && !this.checkClose()) {
|
|
1096
|
+
this.start = this.i;
|
|
1097
|
+
this.state = this.attrName;
|
|
1098
|
+
}
|
|
1099
|
+
};
|
|
1100
|
+
/**
|
|
1101
|
+
* @description 结束标签状态
|
|
1102
|
+
* @returns {String} 结束的标签名
|
|
1103
|
+
* @private
|
|
1104
|
+
*/
|
|
1105
|
+
|
|
1106
|
+
lexer.prototype.endTag = function () {
|
|
1107
|
+
const c = this.content[this.i];
|
|
1108
|
+
|
|
1109
|
+
if (blankChar[c] || c == ">" || c == "/") {
|
|
1110
|
+
this.handler.onCloseTag(this.content.substring(this.start, this.i));
|
|
1111
|
+
|
|
1112
|
+
if (c != ">") {
|
|
1113
|
+
this.i = this.content.indexOf(">", this.i);
|
|
1114
|
+
if (this.i == -1) return;
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
this.start = ++this.i;
|
|
1118
|
+
this.state = this.text;
|
|
1119
|
+
} else this.i++;
|
|
1120
|
+
};
|
|
1121
|
+
|
|
1122
|
+
export default parser
|
|
@@ -39,7 +39,15 @@
|
|
|
39
39
|
* @event {Function} error 媒体加载出错时触发
|
|
40
40
|
*/
|
|
41
41
|
const plugins=[]
|
|
42
|
-
|
|
42
|
+
// TODO
|
|
43
|
+
// 暂时先这样 之后在进行优化
|
|
44
|
+
// #ifdef VUE2
|
|
45
|
+
const parser = import("./parser");
|
|
46
|
+
// #endif
|
|
47
|
+
// #ifdef VUE3
|
|
48
|
+
import parser from "./parser-es-module";
|
|
49
|
+
// #endif
|
|
50
|
+
|
|
43
51
|
// #ifndef APP-PLUS-NVUE
|
|
44
52
|
import node from './node/node'
|
|
45
53
|
// #endif
|
|
@@ -96,7 +96,7 @@
|
|
|
96
96
|
for (let i = 0; i < this.rows; i++) {
|
|
97
97
|
let item = {},
|
|
98
98
|
// 需要预防超出数组边界的情况
|
|
99
|
-
rowWidth = uni.$u.test.array(this.rowsWidth) ? (this.rowsWidth[i] || (i === this.
|
|
99
|
+
rowWidth = uni.$u.test.array(this.rowsWidth) ? (this.rowsWidth[i] || (i === this.rows - 1 ? '70%' : '100%')) : i ===
|
|
100
100
|
this.rows - 1 ? '70%' : this.rowsWidth,
|
|
101
101
|
rowHeight = uni.$u.test.array(this.rowsHeight) ? (this.rowsHeight[i] || '18px') : this.rowsHeight
|
|
102
102
|
// 如果有title占位图,第一个段落占位图的外边距需要大一些,如果没有title占位图,第一个段落占位图则无需外边距
|