uview-pro 0.0.1
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 +6 -0
- package/components/u-action-sheet/u-action-sheet.vue +205 -0
- package/components/u-alert-tips/u-alert-tips.vue +241 -0
- package/components/u-avatar/u-avatar.vue +220 -0
- package/components/u-avatar-cropper/u-avatar-cropper.vue +329 -0
- package/components/u-avatar-cropper/weCropper.d.ts +54 -0
- package/components/u-avatar-cropper/weCropper.js +1267 -0
- package/components/u-avatar-cropper/weCropper.ts +1254 -0
- package/components/u-back-top/u-back-top.vue +156 -0
- package/components/u-badge/u-badge.vue +189 -0
- package/components/u-button/u-button.vue +562 -0
- package/components/u-calendar/u-calendar.vue +725 -0
- package/components/u-car-keyboard/u-car-keyboard.vue +236 -0
- package/components/u-card/u-card.vue +240 -0
- package/components/u-cell-group/u-cell-group.vue +56 -0
- package/components/u-cell-item/u-cell-item.vue +245 -0
- package/components/u-checkbox/u-checkbox.vue +310 -0
- package/components/u-checkbox-group/u-checkbox-group.vue +134 -0
- package/components/u-circle-progress/u-circle-progress.vue +210 -0
- package/components/u-col/u-col.vue +135 -0
- package/components/u-collapse/u-collapse.vue +82 -0
- package/components/u-collapse-item/u-collapse-item.vue +190 -0
- package/components/u-column-notice/u-column-notice.vue +264 -0
- package/components/u-count-down/u-count-down.vue +333 -0
- package/components/u-count-to/u-count-to.vue +297 -0
- package/components/u-divider/u-divider.vue +141 -0
- package/components/u-dropdown/u-dropdown.vue +311 -0
- package/components/u-dropdown-item/u-dropdown-item.vue +135 -0
- package/components/u-empty/u-empty.vue +111 -0
- package/components/u-field/u-field.vue +469 -0
- package/components/u-form/u-form.vue +162 -0
- package/components/u-form-item/u-form-item.vue +476 -0
- package/components/u-full-screen/u-full-screen.vue +80 -0
- package/components/u-gap/u-gap.vue +48 -0
- package/components/u-grid/u-grid.vue +101 -0
- package/components/u-grid-item/u-grid-item.vue +136 -0
- package/components/u-icon/u-icon.vue +389 -0
- package/components/u-image/types.ts +48 -0
- package/components/u-image/u-image.vue +218 -0
- package/components/u-index-anchor/u-index-anchor.vue +101 -0
- package/components/u-index-list/u-index-list.vue +376 -0
- package/components/u-input/u-input.vue +462 -0
- package/components/u-keyboard/u-keyboard.vue +188 -0
- package/components/u-lazy-load/u-lazy-load.vue +288 -0
- package/components/u-line/u-line.vue +71 -0
- package/components/u-line-progress/u-line-progress.vue +128 -0
- package/components/u-link/u-link.vue +87 -0
- package/components/u-loading/u-loading.vue +111 -0
- package/components/u-loadmore/u-loadmore.vue +205 -0
- package/components/u-mask/u-mask.vue +137 -0
- package/components/u-message-input/u-message-input.vue +315 -0
- package/components/u-modal/u-modal.vue +284 -0
- package/components/u-navbar/u-navbar.vue +314 -0
- package/components/u-no-network/image.ts +2 -0
- package/components/u-no-network/u-no-network.vue +311 -0
- package/components/u-notice-bar/u-notice-bar.vue +274 -0
- package/components/u-number-box/u-number-box.vue +344 -0
- package/components/u-number-keyboard/u-number-keyboard.vue +170 -0
- package/components/u-parse/libs/CssHandler.js +100 -0
- package/components/u-parse/libs/MpHtmlParser.js +580 -0
- package/components/u-parse/libs/config.js +80 -0
- package/components/u-parse/libs/handler.wxs +22 -0
- package/components/u-parse/libs/trees.vue +505 -0
- package/components/u-parse/u-parse.vue +645 -0
- package/components/u-picker/u-picker.vue +808 -0
- package/components/u-popup/u-popup.vue +404 -0
- package/components/u-radio/u-radio.vue +272 -0
- package/components/u-radio-group/u-radio-group.vue +116 -0
- package/components/u-rate/u-rate.vue +349 -0
- package/components/u-read-more/u-read-more.vue +199 -0
- package/components/u-row/u-row.vue +95 -0
- package/components/u-row-notice/u-row-notice.vue +273 -0
- package/components/u-search/u-search.vue +298 -0
- package/components/u-section/u-section.vue +175 -0
- package/components/u-select/u-select.vue +387 -0
- package/components/u-skeleton/u-skeleton.vue +230 -0
- package/components/u-slider/u-slider.vue +293 -0
- package/components/u-steps/u-steps.vue +200 -0
- package/components/u-sticky/u-sticky.vue +189 -0
- package/components/u-subsection/u-subsection.vue +388 -0
- package/components/u-swipe-action/u-swipe-action.vue +289 -0
- package/components/u-swiper/u-swiper.vue +305 -0
- package/components/u-switch/u-switch.vue +146 -0
- package/components/u-tabbar/u-tabbar.vue +347 -0
- package/components/u-table/u-table.vue +104 -0
- package/components/u-tabs/u-tabs.vue +322 -0
- package/components/u-tabs-swiper/u-tabs-swiper.vue +426 -0
- package/components/u-tag/u-tag.vue +270 -0
- package/components/u-td/u-td.vue +76 -0
- package/components/u-th/u-th.vue +70 -0
- package/components/u-time-line/u-time-line.vue +39 -0
- package/components/u-time-line-item/u-time-line-item.vue +88 -0
- package/components/u-toast/types.ts +4 -0
- package/components/u-toast/u-toast.vue +238 -0
- package/components/u-top-tips/u-top-tips.vue +118 -0
- package/components/u-tr/u-tr.vue +24 -0
- package/components/u-upload/u-upload.vue +600 -0
- package/components/u-verification-code/u-verification-code.vue +194 -0
- package/components/u-waterfall/u-waterfall.vue +186 -0
- package/iconfont.css +910 -0
- package/index.scss +23 -0
- package/index.ts +166 -0
- package/libs/config/config.ts +26 -0
- package/libs/config/zIndex.ts +37 -0
- package/libs/css/color.scss +155 -0
- package/libs/css/common.scss +176 -0
- package/libs/css/style.components.scss +7 -0
- package/libs/css/style.h5.scss +8 -0
- package/libs/css/style.mp.scss +72 -0
- package/libs/css/style.nvue.scss +3 -0
- package/libs/css/style.vue.scss +175 -0
- package/libs/function/$parent.ts +22 -0
- package/libs/function/addUnit.ts +13 -0
- package/libs/function/color.ts +37 -0
- package/libs/function/colorGradient.ts +123 -0
- package/libs/function/debounce.ts +28 -0
- package/libs/function/deepClone.ts +39 -0
- package/libs/function/deepMerge.ts +34 -0
- package/libs/function/getParent.ts +59 -0
- package/libs/function/getRect.ts +26 -0
- package/libs/function/guid.ts +42 -0
- package/libs/function/md5.ts +397 -0
- package/libs/function/parent.ts +21 -0
- package/libs/function/queryParams.ts +60 -0
- package/libs/function/random.ts +16 -0
- package/libs/function/randomArray.ts +11 -0
- package/libs/function/route.ts +118 -0
- package/libs/function/sys.ts +15 -0
- package/libs/function/test.ts +229 -0
- package/libs/function/throttle.ts +31 -0
- package/libs/function/timeFormat.ts +54 -0
- package/libs/function/timeFrom.ts +48 -0
- package/libs/function/toast.ts +14 -0
- package/libs/function/trim.ts +21 -0
- package/libs/function/type2icon.ts +36 -0
- package/libs/hooks/useEmitter.ts +77 -0
- package/libs/hooks/useParent.ts +29 -0
- package/libs/request/index.ts +237 -0
- package/libs/store/index.ts +88 -0
- package/libs/util/area.ts +1 -0
- package/libs/util/async-validator.js +1356 -0
- package/libs/util/city.ts +1 -0
- package/libs/util/emitter.ts +112 -0
- package/libs/util/mitt.ts +118 -0
- package/libs/util/parent.ts +20 -0
- package/libs/util/province.ts +1 -0
- package/package.json +98 -0
- package/readme.md +165 -0
- package/theme.scss +38 -0
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* html 解析器
|
|
3
|
+
* @tutorial https://github.com/jin-yufeng/Parser
|
|
4
|
+
* @version 20201029
|
|
5
|
+
* @author JinYufeng
|
|
6
|
+
* @listens MIT
|
|
7
|
+
*/
|
|
8
|
+
const cfg = require('./config.js'),
|
|
9
|
+
blankChar = cfg.blankChar,
|
|
10
|
+
CssHandler = require('./CssHandler.js'),
|
|
11
|
+
windowWidth = uni.getSystemInfoSync().windowWidth;
|
|
12
|
+
var emoji;
|
|
13
|
+
|
|
14
|
+
function MpHtmlParser(data, options = {}) {
|
|
15
|
+
this.attrs = {};
|
|
16
|
+
this.CssHandler = new CssHandler(options.tagStyle, windowWidth);
|
|
17
|
+
this.data = data;
|
|
18
|
+
this.domain = options.domain;
|
|
19
|
+
this.DOM = [];
|
|
20
|
+
this.i = this.start = this.audioNum = this.imgNum = this.videoNum = 0;
|
|
21
|
+
options.prot = (this.domain || '').includes('://') ? this.domain.split('://')[0] : 'http';
|
|
22
|
+
this.options = options;
|
|
23
|
+
this.state = this.Text;
|
|
24
|
+
this.STACK = [];
|
|
25
|
+
// 工具函数
|
|
26
|
+
this.bubble = () => {
|
|
27
|
+
for (var i = this.STACK.length, item; item = this.STACK[--i];) {
|
|
28
|
+
if (cfg.richOnlyTags[item.name]) return false;
|
|
29
|
+
item.c = 1;
|
|
30
|
+
}
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
this.decode = (val, amp) => {
|
|
34
|
+
var i = -1,
|
|
35
|
+
j, en;
|
|
36
|
+
while (1) {
|
|
37
|
+
if ((i = val.indexOf('&', i + 1)) == -1) break;
|
|
38
|
+
if ((j = val.indexOf(';', i + 2)) == -1) break;
|
|
39
|
+
if (val[i + 1] == '#') {
|
|
40
|
+
en = parseInt((val[i + 2] == 'x' ? '0' : '') + val.substring(i + 2, j));
|
|
41
|
+
if (!isNaN(en)) val = val.substr(0, i) + String.fromCharCode(en) + val.substr(j + 1);
|
|
42
|
+
} else {
|
|
43
|
+
en = val.substring(i + 1, j);
|
|
44
|
+
if (cfg.entities[en] || en == amp)
|
|
45
|
+
val = val.substr(0, i) + (cfg.entities[en] || '&') + val.substr(j + 1);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return val;
|
|
49
|
+
}
|
|
50
|
+
this.getUrl = url => {
|
|
51
|
+
if (url[0] == '/') {
|
|
52
|
+
if (url[1] == '/') url = this.options.prot + ':' + url;
|
|
53
|
+
else if (this.domain) url = this.domain + url;
|
|
54
|
+
} else if (this.domain && url.indexOf('data:') != 0 && !url.includes('://'))
|
|
55
|
+
url = this.domain + '/' + url;
|
|
56
|
+
return url;
|
|
57
|
+
}
|
|
58
|
+
this.isClose = () => this.data[this.i] == '>' || (this.data[this.i] == '/' && this.data[this.i + 1] == '>');
|
|
59
|
+
this.section = () => this.data.substring(this.start, this.i);
|
|
60
|
+
this.parent = () => this.STACK[this.STACK.length - 1];
|
|
61
|
+
this.siblings = () => this.STACK.length ? this.parent().children : this.DOM;
|
|
62
|
+
}
|
|
63
|
+
MpHtmlParser.prototype.parse = function() {
|
|
64
|
+
if (emoji) this.data = emoji.parseEmoji(this.data);
|
|
65
|
+
for (var c; c = this.data[this.i]; this.i++)
|
|
66
|
+
this.state(c);
|
|
67
|
+
if (this.state == this.Text) this.setText();
|
|
68
|
+
while (this.STACK.length) this.popNode(this.STACK.pop());
|
|
69
|
+
return this.DOM;
|
|
70
|
+
}
|
|
71
|
+
// 设置属性
|
|
72
|
+
MpHtmlParser.prototype.setAttr = function() {
|
|
73
|
+
var name = this.attrName.toLowerCase(),
|
|
74
|
+
val = this.attrVal;
|
|
75
|
+
if (cfg.boolAttrs[name]) this.attrs[name] = 'T';
|
|
76
|
+
else if (val) {
|
|
77
|
+
if (name == 'src' || (name == 'data-src' && !this.attrs.src)) this.attrs.src = this.getUrl(this.decode(val, 'amp'));
|
|
78
|
+
else if (name == 'href' || name == 'style') this.attrs[name] = this.decode(val, 'amp');
|
|
79
|
+
else if (name.substr(0, 5) != 'data-') this.attrs[name] = val;
|
|
80
|
+
}
|
|
81
|
+
this.attrVal = '';
|
|
82
|
+
while (blankChar[this.data[this.i]]) this.i++;
|
|
83
|
+
if (this.isClose()) this.setNode();
|
|
84
|
+
else {
|
|
85
|
+
this.start = this.i;
|
|
86
|
+
this.state = this.AttrName;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
// 设置文本节点
|
|
90
|
+
MpHtmlParser.prototype.setText = function() {
|
|
91
|
+
var back, text = this.section();
|
|
92
|
+
if (!text) return;
|
|
93
|
+
text = (cfg.onText && cfg.onText(text, () => back = true)) || text;
|
|
94
|
+
if (back) {
|
|
95
|
+
this.data = this.data.substr(0, this.start) + text + this.data.substr(this.i);
|
|
96
|
+
let j = this.start + text.length;
|
|
97
|
+
for (this.i = this.start; this.i < j; this.i++) this.state(this.data[this.i]);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (!this.pre) {
|
|
101
|
+
// 合并空白符
|
|
102
|
+
var flag, tmp = [];
|
|
103
|
+
for (let i = text.length, c; c = text[--i];)
|
|
104
|
+
if (!blankChar[c]) {
|
|
105
|
+
tmp.unshift(c);
|
|
106
|
+
if (!flag) flag = 1;
|
|
107
|
+
} else {
|
|
108
|
+
if (tmp[0] != ' ') tmp.unshift(' ');
|
|
109
|
+
if (c == '\n' && flag == void 0) flag = 0;
|
|
110
|
+
}
|
|
111
|
+
if (flag == 0) return;
|
|
112
|
+
text = tmp.join('');
|
|
113
|
+
}
|
|
114
|
+
this.siblings().push({
|
|
115
|
+
type: 'text',
|
|
116
|
+
text: this.decode(text)
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
// 设置元素节点
|
|
120
|
+
MpHtmlParser.prototype.setNode = function() {
|
|
121
|
+
var node = {
|
|
122
|
+
name: this.tagName.toLowerCase(),
|
|
123
|
+
attrs: this.attrs
|
|
124
|
+
},
|
|
125
|
+
close = cfg.selfClosingTags[node.name];
|
|
126
|
+
if (this.options.nodes.length) node.type = 'node';
|
|
127
|
+
this.attrs = {};
|
|
128
|
+
if (!cfg.ignoreTags[node.name]) {
|
|
129
|
+
// 处理属性
|
|
130
|
+
var attrs = node.attrs,
|
|
131
|
+
style = this.CssHandler.match(node.name, attrs, node) + (attrs.style || ''),
|
|
132
|
+
styleObj = {};
|
|
133
|
+
if (attrs.id) {
|
|
134
|
+
if (this.options.compress & 1) attrs.id = void 0;
|
|
135
|
+
else if (this.options.useAnchor) this.bubble();
|
|
136
|
+
}
|
|
137
|
+
if ((this.options.compress & 2) && attrs.class) attrs.class = void 0;
|
|
138
|
+
switch (node.name) {
|
|
139
|
+
case 'a':
|
|
140
|
+
case 'ad': // #ifdef APP-PLUS
|
|
141
|
+
case 'iframe':
|
|
142
|
+
// #endif
|
|
143
|
+
this.bubble();
|
|
144
|
+
break;
|
|
145
|
+
case 'font':
|
|
146
|
+
if (attrs.color) {
|
|
147
|
+
styleObj['color'] = attrs.color;
|
|
148
|
+
attrs.color = void 0;
|
|
149
|
+
}
|
|
150
|
+
if (attrs.face) {
|
|
151
|
+
styleObj['font-family'] = attrs.face;
|
|
152
|
+
attrs.face = void 0;
|
|
153
|
+
}
|
|
154
|
+
if (attrs.size) {
|
|
155
|
+
var size = parseInt(attrs.size);
|
|
156
|
+
if (size < 1) size = 1;
|
|
157
|
+
else if (size > 7) size = 7;
|
|
158
|
+
var map = ['xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'];
|
|
159
|
+
styleObj['font-size'] = map[size - 1];
|
|
160
|
+
attrs.size = void 0;
|
|
161
|
+
}
|
|
162
|
+
break;
|
|
163
|
+
case 'embed':
|
|
164
|
+
// #ifndef APP-PLUS
|
|
165
|
+
var src = node.attrs.src || '',
|
|
166
|
+
type = node.attrs.type || '';
|
|
167
|
+
if (type.includes('video') || src.includes('.mp4') || src.includes('.3gp') || src.includes('.m3u8'))
|
|
168
|
+
node.name = 'video';
|
|
169
|
+
else if (type.includes('audio') || src.includes('.m4a') || src.includes('.wav') || src.includes('.mp3') || src.includes(
|
|
170
|
+
'.aac'))
|
|
171
|
+
node.name = 'audio';
|
|
172
|
+
else break;
|
|
173
|
+
if (node.attrs.autostart)
|
|
174
|
+
node.attrs.autoplay = 'T';
|
|
175
|
+
node.attrs.controls = 'T';
|
|
176
|
+
// #endif
|
|
177
|
+
// #ifdef APP-PLUS
|
|
178
|
+
this.bubble();
|
|
179
|
+
break;
|
|
180
|
+
// #endif
|
|
181
|
+
case 'video':
|
|
182
|
+
case 'audio':
|
|
183
|
+
if (!attrs.id) attrs.id = node.name + (++this[`${node.name}Num`]);
|
|
184
|
+
else this[`${node.name}Num`]++;
|
|
185
|
+
if (node.name == 'video') {
|
|
186
|
+
if (this.videoNum > 3)
|
|
187
|
+
node.lazyLoad = 1;
|
|
188
|
+
if (attrs.width) {
|
|
189
|
+
styleObj.width = parseFloat(attrs.width) + (attrs.width.includes('%') ? '%' : 'px');
|
|
190
|
+
attrs.width = void 0;
|
|
191
|
+
}
|
|
192
|
+
if (attrs.height) {
|
|
193
|
+
styleObj.height = parseFloat(attrs.height) + (attrs.height.includes('%') ? '%' : 'px');
|
|
194
|
+
attrs.height = void 0;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (!attrs.controls && !attrs.autoplay) attrs.controls = 'T';
|
|
198
|
+
attrs.source = [];
|
|
199
|
+
if (attrs.src) {
|
|
200
|
+
attrs.source.push(attrs.src);
|
|
201
|
+
attrs.src = void 0;
|
|
202
|
+
}
|
|
203
|
+
this.bubble();
|
|
204
|
+
break;
|
|
205
|
+
case 'td':
|
|
206
|
+
case 'th':
|
|
207
|
+
if (attrs.colspan || attrs.rowspan)
|
|
208
|
+
for (var k = this.STACK.length, item; item = this.STACK[--k];)
|
|
209
|
+
if (item.name == 'table') {
|
|
210
|
+
item.flag = 1;
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (attrs.align) {
|
|
215
|
+
if (node.name == 'table') {
|
|
216
|
+
if (attrs.align == 'center') styleObj['margin-inline-start'] = styleObj['margin-inline-end'] = 'auto';
|
|
217
|
+
else styleObj['float'] = attrs.align;
|
|
218
|
+
} else styleObj['text-align'] = attrs.align;
|
|
219
|
+
attrs.align = void 0;
|
|
220
|
+
}
|
|
221
|
+
// 压缩 style
|
|
222
|
+
var styles = style.split(';');
|
|
223
|
+
style = '';
|
|
224
|
+
for (var i = 0, len = styles.length; i < len; i++) {
|
|
225
|
+
var info = styles[i].split(':');
|
|
226
|
+
if (info.length < 2) continue;
|
|
227
|
+
let key = info[0].trim().toLowerCase(),
|
|
228
|
+
value = info.slice(1).join(':').trim();
|
|
229
|
+
if (value[0] == '-' || value.includes('safe'))
|
|
230
|
+
style += `;${key}:${value}`;
|
|
231
|
+
else if (!styleObj[key] || value.includes('import') || !styleObj[key].includes('import'))
|
|
232
|
+
styleObj[key] = value;
|
|
233
|
+
}
|
|
234
|
+
if (node.name == 'img') {
|
|
235
|
+
if (attrs.src && !attrs.ignore) {
|
|
236
|
+
if (this.bubble())
|
|
237
|
+
attrs.i = (this.imgNum++).toString();
|
|
238
|
+
else attrs.ignore = 'T';
|
|
239
|
+
}
|
|
240
|
+
if (attrs.ignore) {
|
|
241
|
+
style += ';-webkit-touch-callout:none';
|
|
242
|
+
styleObj['max-width'] = '100%';
|
|
243
|
+
}
|
|
244
|
+
var width;
|
|
245
|
+
if (styleObj.width) width = styleObj.width;
|
|
246
|
+
else if (attrs.width) width = attrs.width.includes('%') ? attrs.width : parseFloat(attrs.width) + 'px';
|
|
247
|
+
if (width) {
|
|
248
|
+
styleObj.width = width;
|
|
249
|
+
attrs.width = '100%';
|
|
250
|
+
if (parseInt(width) > windowWidth) {
|
|
251
|
+
styleObj.height = '';
|
|
252
|
+
if (attrs.height) attrs.height = void 0;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
if (styleObj.height) {
|
|
256
|
+
attrs.height = styleObj.height;
|
|
257
|
+
styleObj.height = '';
|
|
258
|
+
} else if (attrs.height && !attrs.height.includes('%'))
|
|
259
|
+
attrs.height = parseFloat(attrs.height) + 'px';
|
|
260
|
+
}
|
|
261
|
+
for (var key in styleObj) {
|
|
262
|
+
var value = styleObj[key];
|
|
263
|
+
if (!value) continue;
|
|
264
|
+
if (key.includes('flex') || key == 'order' || key == 'self-align') node.c = 1;
|
|
265
|
+
// 填充链接
|
|
266
|
+
if (value.includes('url')) {
|
|
267
|
+
var j = value.indexOf('(');
|
|
268
|
+
if (j++ != -1) {
|
|
269
|
+
while (value[j] == '"' || value[j] == "'" || blankChar[value[j]]) j++;
|
|
270
|
+
value = value.substr(0, j) + this.getUrl(value.substr(j));
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
// 转换 rpx
|
|
274
|
+
else if (value.includes('rpx'))
|
|
275
|
+
value = value.replace(/[0-9.]+\s*rpx/g, $ => parseFloat($) * windowWidth / 750 + 'px');
|
|
276
|
+
else if (key == 'white-space' && value.includes('pre') && !close)
|
|
277
|
+
this.pre = node.pre = true;
|
|
278
|
+
style += `;${key}:${value}`;
|
|
279
|
+
}
|
|
280
|
+
style = style.substr(1);
|
|
281
|
+
if (style) attrs.style = style;
|
|
282
|
+
if (!close) {
|
|
283
|
+
node.children = [];
|
|
284
|
+
if (node.name == 'pre' && cfg.highlight) {
|
|
285
|
+
this.remove(node);
|
|
286
|
+
this.pre = node.pre = true;
|
|
287
|
+
}
|
|
288
|
+
this.siblings().push(node);
|
|
289
|
+
this.STACK.push(node);
|
|
290
|
+
} else if (!cfg.filter || cfg.filter(node, this) != false)
|
|
291
|
+
this.siblings().push(node);
|
|
292
|
+
} else {
|
|
293
|
+
if (!close) this.remove(node);
|
|
294
|
+
else if (node.name == 'source') {
|
|
295
|
+
var parent = this.parent();
|
|
296
|
+
if (parent && (parent.name == 'video' || parent.name == 'audio') && node.attrs.src)
|
|
297
|
+
parent.attrs.source.push(node.attrs.src);
|
|
298
|
+
} else if (node.name == 'base' && !this.domain) this.domain = node.attrs.href;
|
|
299
|
+
}
|
|
300
|
+
if (this.data[this.i] == '/') this.i++;
|
|
301
|
+
this.start = this.i + 1;
|
|
302
|
+
this.state = this.Text;
|
|
303
|
+
}
|
|
304
|
+
// 移除标签
|
|
305
|
+
MpHtmlParser.prototype.remove = function(node) {
|
|
306
|
+
var name = node.name,
|
|
307
|
+
j = this.i;
|
|
308
|
+
// 处理 svg
|
|
309
|
+
var handleSvg = () => {
|
|
310
|
+
var src = this.data.substring(j, this.i + 1);
|
|
311
|
+
node.attrs.xmlns = 'http://www.w3.org/2000/svg';
|
|
312
|
+
for (var key in node.attrs) {
|
|
313
|
+
if (key == 'viewbox') src = ` viewBox="${node.attrs.viewbox}"` + src;
|
|
314
|
+
else if (key != 'style') src = ` ${key}="${node.attrs[key]}"` + src;
|
|
315
|
+
}
|
|
316
|
+
src = '<svg' + src;
|
|
317
|
+
var parent = this.parent();
|
|
318
|
+
if (node.attrs.width == '100%' && parent && (parent.attrs.style || '').includes('inline'))
|
|
319
|
+
parent.attrs.style = 'width:300px;max-width:100%;' + parent.attrs.style;
|
|
320
|
+
this.siblings().push({
|
|
321
|
+
name: 'img',
|
|
322
|
+
attrs: {
|
|
323
|
+
src: 'data:image/svg+xml;utf8,' + src.replace(/#/g, '%23'),
|
|
324
|
+
style: node.attrs.style,
|
|
325
|
+
ignore: 'T'
|
|
326
|
+
}
|
|
327
|
+
})
|
|
328
|
+
}
|
|
329
|
+
if (node.name == 'svg' && this.data[j] == '/') return handleSvg(this.i++);
|
|
330
|
+
while (1) {
|
|
331
|
+
if ((this.i = this.data.indexOf('</', this.i + 1)) == -1) {
|
|
332
|
+
if (name == 'pre' || name == 'svg') this.i = j;
|
|
333
|
+
else this.i = this.data.length;
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
this.start = (this.i += 2);
|
|
337
|
+
while (!blankChar[this.data[this.i]] && !this.isClose()) this.i++;
|
|
338
|
+
if (this.section().toLowerCase() == name) {
|
|
339
|
+
// 代码块高亮
|
|
340
|
+
if (name == 'pre') {
|
|
341
|
+
this.data = this.data.substr(0, j + 1) + cfg.highlight(this.data.substring(j + 1, this.i - 5), node.attrs) + this.data
|
|
342
|
+
.substr(this.i - 5);
|
|
343
|
+
return this.i = j;
|
|
344
|
+
} else if (name == 'style')
|
|
345
|
+
this.CssHandler.getStyle(this.data.substring(j + 1, this.i - 7));
|
|
346
|
+
else if (name == 'title')
|
|
347
|
+
this.DOM.title = this.data.substring(j + 1, this.i - 7);
|
|
348
|
+
if ((this.i = this.data.indexOf('>', this.i)) == -1) this.i = this.data.length;
|
|
349
|
+
if (name == 'svg') handleSvg();
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
// 节点出栈处理
|
|
355
|
+
MpHtmlParser.prototype.popNode = function(node) {
|
|
356
|
+
// 空白符处理
|
|
357
|
+
if (node.pre) {
|
|
358
|
+
node.pre = this.pre = void 0;
|
|
359
|
+
for (let i = this.STACK.length; i--;)
|
|
360
|
+
if (this.STACK[i].pre)
|
|
361
|
+
this.pre = true;
|
|
362
|
+
}
|
|
363
|
+
var siblings = this.siblings(),
|
|
364
|
+
len = siblings.length,
|
|
365
|
+
childs = node.children;
|
|
366
|
+
if (node.name == 'head' || (cfg.filter && cfg.filter(node, this) == false))
|
|
367
|
+
return siblings.pop();
|
|
368
|
+
var attrs = node.attrs;
|
|
369
|
+
// 替换一些标签名
|
|
370
|
+
if (cfg.blockTags[node.name]) node.name = 'div';
|
|
371
|
+
else if (!cfg.trustTags[node.name]) node.name = 'span';
|
|
372
|
+
// 处理列表
|
|
373
|
+
if (node.c && (node.name == 'ul' || node.name == 'ol')) {
|
|
374
|
+
if ((node.attrs.style || '').includes('list-style:none')) {
|
|
375
|
+
for (let i = 0, child; child = childs[i++];)
|
|
376
|
+
if (child.name == 'li')
|
|
377
|
+
child.name = 'div';
|
|
378
|
+
} else if (node.name == 'ul') {
|
|
379
|
+
var floor = 1;
|
|
380
|
+
for (let i = this.STACK.length; i--;)
|
|
381
|
+
if (this.STACK[i].name == 'ul') floor++;
|
|
382
|
+
if (floor != 1)
|
|
383
|
+
for (let i = childs.length; i--;)
|
|
384
|
+
childs[i].floor = floor;
|
|
385
|
+
} else {
|
|
386
|
+
for (let i = 0, num = 1, child; child = childs[i++];)
|
|
387
|
+
if (child.name == 'li') {
|
|
388
|
+
child.type = 'ol';
|
|
389
|
+
child.num = ((num, type) => {
|
|
390
|
+
if (type == 'a') return String.fromCharCode(97 + (num - 1) % 26);
|
|
391
|
+
if (type == 'A') return String.fromCharCode(65 + (num - 1) % 26);
|
|
392
|
+
if (type == 'i' || type == 'I') {
|
|
393
|
+
num = (num - 1) % 99 + 1;
|
|
394
|
+
var one = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'],
|
|
395
|
+
ten = ['X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'],
|
|
396
|
+
res = (ten[Math.floor(num / 10) - 1] || '') + (one[num % 10 - 1] || '');
|
|
397
|
+
if (type == 'i') return res.toLowerCase();
|
|
398
|
+
return res;
|
|
399
|
+
}
|
|
400
|
+
return num;
|
|
401
|
+
})(num++, attrs.type) + '.';
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
// 处理表格
|
|
406
|
+
if (node.name == 'table') {
|
|
407
|
+
var padding = parseFloat(attrs.cellpadding),
|
|
408
|
+
spacing = parseFloat(attrs.cellspacing),
|
|
409
|
+
border = parseFloat(attrs.border);
|
|
410
|
+
if (node.c) {
|
|
411
|
+
if (isNaN(padding)) padding = 2;
|
|
412
|
+
if (isNaN(spacing)) spacing = 2;
|
|
413
|
+
}
|
|
414
|
+
if (border) attrs.style = `border:${border}px solid gray;${attrs.style || ''}`;
|
|
415
|
+
if (node.flag && node.c) {
|
|
416
|
+
// 有 colspan 或 rowspan 且含有链接的表格转为 grid 布局实现
|
|
417
|
+
attrs.style = `${attrs.style || ''};${spacing ? `;grid-gap:${spacing}px` : ';border-left:0;border-top:0'}`;
|
|
418
|
+
var row = 1,
|
|
419
|
+
col = 1,
|
|
420
|
+
colNum,
|
|
421
|
+
trs = [],
|
|
422
|
+
children = [],
|
|
423
|
+
map = {};
|
|
424
|
+
(function f(ns) {
|
|
425
|
+
for (var i = 0; i < ns.length; i++) {
|
|
426
|
+
if (ns[i].name == 'tr') trs.push(ns[i]);
|
|
427
|
+
else f(ns[i].children || []);
|
|
428
|
+
}
|
|
429
|
+
})(node.children)
|
|
430
|
+
for (let i = 0; i < trs.length; i++) {
|
|
431
|
+
for (let j = 0, td; td = trs[i].children[j]; j++) {
|
|
432
|
+
if (td.name == 'td' || td.name == 'th') {
|
|
433
|
+
while (map[row + '.' + col]) col++;
|
|
434
|
+
var cell = {
|
|
435
|
+
name: 'div',
|
|
436
|
+
c: 1,
|
|
437
|
+
attrs: {
|
|
438
|
+
style: (td.attrs.style || '') + (border ? `;border:${border}px solid gray` + (spacing ? '' :
|
|
439
|
+
';border-right:0;border-bottom:0') : '') + (padding ? `;padding:${padding}px` : '')
|
|
440
|
+
},
|
|
441
|
+
children: td.children
|
|
442
|
+
}
|
|
443
|
+
if (td.attrs.colspan) {
|
|
444
|
+
cell.attrs.style += ';grid-column-start:' + col + ';grid-column-end:' + (col + parseInt(td.attrs.colspan));
|
|
445
|
+
if (!td.attrs.rowspan) cell.attrs.style += ';grid-row-start:' + row + ';grid-row-end:' + (row + 1);
|
|
446
|
+
col += parseInt(td.attrs.colspan) - 1;
|
|
447
|
+
}
|
|
448
|
+
if (td.attrs.rowspan) {
|
|
449
|
+
cell.attrs.style += ';grid-row-start:' + row + ';grid-row-end:' + (row + parseInt(td.attrs.rowspan));
|
|
450
|
+
if (!td.attrs.colspan) cell.attrs.style += ';grid-column-start:' + col + ';grid-column-end:' + (col + 1);
|
|
451
|
+
for (var k = 1; k < td.attrs.rowspan; k++) map[(row + k) + '.' + col] = 1;
|
|
452
|
+
}
|
|
453
|
+
children.push(cell);
|
|
454
|
+
col++;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
if (!colNum) {
|
|
458
|
+
colNum = col - 1;
|
|
459
|
+
attrs.style += `;grid-template-columns:repeat(${colNum},auto)`
|
|
460
|
+
}
|
|
461
|
+
col = 1;
|
|
462
|
+
row++;
|
|
463
|
+
}
|
|
464
|
+
node.children = children;
|
|
465
|
+
} else {
|
|
466
|
+
attrs.style = `border-spacing:${spacing}px;${attrs.style || ''}`;
|
|
467
|
+
if (border || padding)
|
|
468
|
+
(function f(ns) {
|
|
469
|
+
for (var i = 0, n; n = ns[i]; i++) {
|
|
470
|
+
if (n.name == 'th' || n.name == 'td') {
|
|
471
|
+
if (border) n.attrs.style = `border:${border}px solid gray;${n.attrs.style || ''}`;
|
|
472
|
+
if (padding) n.attrs.style = `padding:${padding}px;${n.attrs.style || ''}`;
|
|
473
|
+
} else f(n.children || []);
|
|
474
|
+
}
|
|
475
|
+
})(childs)
|
|
476
|
+
}
|
|
477
|
+
if (this.options.autoscroll) {
|
|
478
|
+
var table = Object.assign({}, node);
|
|
479
|
+
node.name = 'div';
|
|
480
|
+
node.attrs = {
|
|
481
|
+
style: 'overflow:scroll'
|
|
482
|
+
}
|
|
483
|
+
node.children = [table];
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
this.CssHandler.pop && this.CssHandler.pop(node);
|
|
487
|
+
// 自动压缩
|
|
488
|
+
if (node.name == 'div' && !Object.keys(attrs).length && childs.length == 1 && childs[0].name == 'div')
|
|
489
|
+
siblings[len - 1] = childs[0];
|
|
490
|
+
}
|
|
491
|
+
// 状态机
|
|
492
|
+
MpHtmlParser.prototype.Text = function(c) {
|
|
493
|
+
if (c == '<') {
|
|
494
|
+
var next = this.data[this.i + 1],
|
|
495
|
+
isLetter = c => (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
|
|
496
|
+
if (isLetter(next)) {
|
|
497
|
+
this.setText();
|
|
498
|
+
this.start = this.i + 1;
|
|
499
|
+
this.state = this.TagName;
|
|
500
|
+
} else if (next == '/') {
|
|
501
|
+
this.setText();
|
|
502
|
+
if (isLetter(this.data[++this.i + 1])) {
|
|
503
|
+
this.start = this.i + 1;
|
|
504
|
+
this.state = this.EndTag;
|
|
505
|
+
} else this.Comment();
|
|
506
|
+
} else if (next == '!' || next == '?') {
|
|
507
|
+
this.setText();
|
|
508
|
+
this.Comment();
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
MpHtmlParser.prototype.Comment = function() {
|
|
513
|
+
var key;
|
|
514
|
+
if (this.data.substring(this.i + 2, this.i + 4) == '--') key = '-->';
|
|
515
|
+
else if (this.data.substring(this.i + 2, this.i + 9) == '[CDATA[') key = ']]>';
|
|
516
|
+
else key = '>';
|
|
517
|
+
if ((this.i = this.data.indexOf(key, this.i + 2)) == -1) this.i = this.data.length;
|
|
518
|
+
else this.i += key.length - 1;
|
|
519
|
+
this.start = this.i + 1;
|
|
520
|
+
this.state = this.Text;
|
|
521
|
+
}
|
|
522
|
+
MpHtmlParser.prototype.TagName = function(c) {
|
|
523
|
+
if (blankChar[c]) {
|
|
524
|
+
this.tagName = this.section();
|
|
525
|
+
while (blankChar[this.data[this.i]]) this.i++;
|
|
526
|
+
if (this.isClose()) this.setNode();
|
|
527
|
+
else {
|
|
528
|
+
this.start = this.i;
|
|
529
|
+
this.state = this.AttrName;
|
|
530
|
+
}
|
|
531
|
+
} else if (this.isClose()) {
|
|
532
|
+
this.tagName = this.section();
|
|
533
|
+
this.setNode();
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
MpHtmlParser.prototype.AttrName = function(c) {
|
|
537
|
+
if (c == '=' || blankChar[c] || this.isClose()) {
|
|
538
|
+
this.attrName = this.section();
|
|
539
|
+
if (blankChar[c])
|
|
540
|
+
while (blankChar[this.data[++this.i]]);
|
|
541
|
+
if (this.data[this.i] == '=') {
|
|
542
|
+
while (blankChar[this.data[++this.i]]);
|
|
543
|
+
this.start = this.i--;
|
|
544
|
+
this.state = this.AttrValue;
|
|
545
|
+
} else this.setAttr();
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
MpHtmlParser.prototype.AttrValue = function(c) {
|
|
549
|
+
if (c == '"' || c == "'") {
|
|
550
|
+
this.start++;
|
|
551
|
+
if ((this.i = this.data.indexOf(c, this.i + 1)) == -1) return this.i = this.data.length;
|
|
552
|
+
this.attrVal = this.section();
|
|
553
|
+
this.i++;
|
|
554
|
+
} else {
|
|
555
|
+
for (; !blankChar[this.data[this.i]] && !this.isClose(); this.i++);
|
|
556
|
+
this.attrVal = this.section();
|
|
557
|
+
}
|
|
558
|
+
this.setAttr();
|
|
559
|
+
}
|
|
560
|
+
MpHtmlParser.prototype.EndTag = function(c) {
|
|
561
|
+
if (blankChar[c] || c == '>' || c == '/') {
|
|
562
|
+
var name = this.section().toLowerCase();
|
|
563
|
+
for (var i = this.STACK.length; i--;)
|
|
564
|
+
if (this.STACK[i].name == name) break;
|
|
565
|
+
if (i != -1) {
|
|
566
|
+
var node;
|
|
567
|
+
while ((node = this.STACK.pop()).name != name) this.popNode(node);
|
|
568
|
+
this.popNode(node);
|
|
569
|
+
} else if (name == 'p' || name == 'br')
|
|
570
|
+
this.siblings().push({
|
|
571
|
+
name,
|
|
572
|
+
attrs: {}
|
|
573
|
+
});
|
|
574
|
+
this.i = this.data.indexOf('>', this.i);
|
|
575
|
+
this.start = this.i + 1;
|
|
576
|
+
if (this.i == -1) this.i = this.data.length;
|
|
577
|
+
else this.state = this.Text;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
module.exports = MpHtmlParser;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/* 配置文件 */
|
|
2
|
+
var cfg = {
|
|
3
|
+
// 出错占位图
|
|
4
|
+
errorImg: null,
|
|
5
|
+
// 过滤器函数
|
|
6
|
+
filter: null,
|
|
7
|
+
// 代码高亮函数
|
|
8
|
+
highlight: null,
|
|
9
|
+
// 文本处理函数
|
|
10
|
+
onText: null,
|
|
11
|
+
// 实体编码列表
|
|
12
|
+
entities: {
|
|
13
|
+
quot: '"',
|
|
14
|
+
apos: "'",
|
|
15
|
+
semi: ';',
|
|
16
|
+
nbsp: '\xA0',
|
|
17
|
+
ensp: '\u2002',
|
|
18
|
+
emsp: '\u2003',
|
|
19
|
+
ndash: '–',
|
|
20
|
+
mdash: '—',
|
|
21
|
+
middot: '·',
|
|
22
|
+
lsquo: '‘',
|
|
23
|
+
rsquo: '’',
|
|
24
|
+
ldquo: '“',
|
|
25
|
+
rdquo: '”',
|
|
26
|
+
bull: '•',
|
|
27
|
+
hellip: '…'
|
|
28
|
+
},
|
|
29
|
+
blankChar: makeMap(' ,\xA0,\t,\r,\n,\f'),
|
|
30
|
+
boolAttrs: makeMap('allowfullscreen,autoplay,autostart,controls,ignore,loop,muted'),
|
|
31
|
+
// 块级标签,将被转为 div
|
|
32
|
+
blockTags: makeMap('address,article,aside,body,caption,center,cite,footer,header,html,nav,pre,section'),
|
|
33
|
+
// 将被移除的标签
|
|
34
|
+
ignoreTags: makeMap('area,base,canvas,frame,iframe,input,link,map,meta,param,script,source,style,svg,textarea,title,track,wbr'),
|
|
35
|
+
// 只能被 rich-text 显示的标签
|
|
36
|
+
richOnlyTags: makeMap('a,colgroup,fieldset,legend'),
|
|
37
|
+
// 自闭合的标签
|
|
38
|
+
selfClosingTags: makeMap('area,base,br,col,circle,ellipse,embed,frame,hr,img,input,line,link,meta,param,path,polygon,rect,source,track,use,wbr'),
|
|
39
|
+
// 信任的标签
|
|
40
|
+
trustTags: makeMap('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,source,span,strong,sub,sup,table,tbody,td,tfoot,th,thead,tr,title,ul,video'),
|
|
41
|
+
// 默认的标签样式
|
|
42
|
+
userAgentStyles: {
|
|
43
|
+
address: 'font-style:italic',
|
|
44
|
+
big: 'display:inline;font-size:1.2em',
|
|
45
|
+
blockquote: 'background-color:#f6f6f6;border-left:3px solid #dbdbdb;color:#6c6c6c;padding:5px 0 5px 10px',
|
|
46
|
+
caption: 'display:table-caption;text-align:center',
|
|
47
|
+
center: 'text-align:center',
|
|
48
|
+
cite: 'font-style:italic',
|
|
49
|
+
dd: 'margin-left:40px',
|
|
50
|
+
mark: 'background-color:yellow',
|
|
51
|
+
pre: 'font-family:monospace;white-space:pre;overflow:scroll',
|
|
52
|
+
s: 'text-decoration:line-through',
|
|
53
|
+
small: 'display:inline;font-size:0.8em',
|
|
54
|
+
u: 'text-decoration:underline'
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function makeMap(str) {
|
|
59
|
+
var map = Object.create(null),
|
|
60
|
+
list = str.split(',');
|
|
61
|
+
for (var i = list.length; i--;)
|
|
62
|
+
map[list[i]] = true;
|
|
63
|
+
return map;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// #ifdef MP-WEIXIN
|
|
67
|
+
if (wx.canIUse('editor')) {
|
|
68
|
+
cfg.blockTags.pre = void 0;
|
|
69
|
+
cfg.ignoreTags.rp = true;
|
|
70
|
+
Object.assign(cfg.richOnlyTags, makeMap('bdi,bdo,caption,rt,ruby'));
|
|
71
|
+
Object.assign(cfg.trustTags, makeMap('bdi,bdo,caption,pre,rt,ruby'));
|
|
72
|
+
}
|
|
73
|
+
// #endif
|
|
74
|
+
|
|
75
|
+
// #ifdef APP-PLUS
|
|
76
|
+
cfg.ignoreTags.iframe = void 0;
|
|
77
|
+
Object.assign(cfg.trustTags, makeMap('embed,iframe'));
|
|
78
|
+
// #endif
|
|
79
|
+
|
|
80
|
+
module.exports = cfg;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
var inline = {
|
|
2
|
+
abbr: 1,
|
|
3
|
+
b: 1,
|
|
4
|
+
big: 1,
|
|
5
|
+
code: 1,
|
|
6
|
+
del: 1,
|
|
7
|
+
em: 1,
|
|
8
|
+
i: 1,
|
|
9
|
+
ins: 1,
|
|
10
|
+
label: 1,
|
|
11
|
+
q: 1,
|
|
12
|
+
small: 1,
|
|
13
|
+
span: 1,
|
|
14
|
+
strong: 1,
|
|
15
|
+
sub: 1,
|
|
16
|
+
sup: 1
|
|
17
|
+
}
|
|
18
|
+
module.exports = {
|
|
19
|
+
use: function(item) {
|
|
20
|
+
return !item.c && !inline[item.name] && (item.attrs.style || '').indexOf('display:inline') == -1
|
|
21
|
+
}
|
|
22
|
+
}
|