-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
279 lines (238 loc) · 8.37 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
const text = '你好<navigator text=点击跳转 path=/pages/store/store style=color:red;>,测试1234'
function parseContent2(originText) {
const startTag = /^<([-A-Za-z0-9_]+)((?:\s+[a-zA-Z_:][-a-zA-Z0-9_:.]*(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/
const endTag = /^<\/([-A-Za-z0-9_]+)[^>]*>/
const attr = /([a-zA-Z_:][-a-zA-Z0-9_:.]*)(?:\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|([^>\s]+)))?/g // Empty Elements - HTML 5
const empty = makeMap('navigator')
const block = makeMap()
const inline = makeMap()
const fillAttrs = makeMap()
const special = makeMap()
function HTMLParser(html, handler) {
let index
let chars
let match
const stack = []
let last = html
stack.last = function() {
return this[this.length - 1]
}
while (html) {
chars = true
if (!stack.last() || !special[stack.last()]) {
if (html.indexOf('<!--') === 0) {
index = html.indexOf('-->')
if (index >= 0) {
if (handler.comment) {
handler.comment(html.substring(4, index))
}
html = html.substring(index + 3)
chars = false
} // end tag
} else if (html.indexOf('</') === 0) {
match = html.match(endTag)
if (match) {
html = html.substring(match[0].length)
match[0].replace(endTag, parseEndTag)
chars = false
} // start tag
} else if (html.indexOf('<') === 0) {
match = html.match(startTag)
if (match && empty[match[1]]) {
html = html.substring(match[0].length)
match[0].replace(startTag, parseStartTag)
chars = false
}
}
if (chars) {
index = html.indexOf('<')
const text = index < 0 ? html : html.substring(0, index)
html = index < 0 ? '' : html.substring(index)
if (handler.chars) {
handler.chars(text)
}
}
} else {
html = html.replace(
new RegExp('([\\s\\S]*?)<\/' + stack.last() + '[^>]*>'),
function(all, text) {
text = text.replace(
/<!--([\s\S]*?)-->|<!\[CDATA\[([\s\S]*?)]]>/g,
'$1$2'
)
if (handler.chars) {
handler.chars(text)
}
return ''
}
)
parseEndTag('', stack.last())
}
if (html === last) {
throw new Error('Parse Error: ' + html)
}
last = html
}
parseEndTag()
function parseStartTag(tag, tagName, rest, unary) {
tagName = tagName.toLowerCase()
if (block[tagName]) {
while (stack.last() && inline[stack.last()]) {
parseEndTag('', stack.last())
}
}
parseEndTag('', tagName)
// if (closeSelf[tagName] && stack.last() === tagName) {
// }
unary = empty[tagName] || !!unary
if (!unary) {
stack.push(tagName)
}
if (handler.start) {
const attrs = []
rest.replace(attr, function(match, name) {
const value = arguments[2]
? arguments[2]
: arguments[3]
? arguments[3]
: arguments[4]
? arguments[4]
: fillAttrs[name]
? name
: ''
attrs.push({
name: name,
value: value,
escaped: value.replace(/(^|[^\\])"/g, '$1\\"') // "
})
})
if (handler.start) {
handler.start(tagName, attrs, unary)
}
}
}
function parseEndTag(tag, tagName) {
console.log(tagName)
// If no tag name is provided, clean shop
let pos
if (!tagName) {
pos = 0
} else {
for (let pos = stack.length - 1; pos >= 0; pos--) {
if (stack[pos] === tagName) {
break
}
}
}
if (pos >= 0) {
// Close all the open elements, up the stack
for (let i = stack.length - 1; i >= pos; i--) {
if (handler.end) {
handler.end(stack[i])
}
} // Remove the open elements from the stack
stack.length = pos
}
}
}
function makeMap(str) {
if (!str) {
return {}
}
const obj = {}
const items = str.split(',')
for (let i = 0; i < items.length; i++) {
obj[items[i]] = true
}
return obj
}
function removeDOCTYPE(html) {
return html
.replace(/<\?xml.*\?>\n/, '')
.replace(/<!doctype.*>\n/, '')
.replace(/<!DOCTYPE.*>\n/, '')
}
function parseAttrs(attrs) {
return attrs.reduce(function(pre, attr) {
const value = attr.value
const name = attr.name
if (pre[name]) {
pre[name] = pre[name] + ' ' + value
} else {
pre[name] = value
}
return pre
}, {})
}
function parseHtml(html) {
html = removeDOCTYPE(html)
const stacks = []
const results = {
node: 'root',
children: []
}
HTMLParser(html, {
start: function start(tag, attrs, unary) {
const node = {
name: tag
}
if (attrs.length !== 0) {
node.attrs = parseAttrs(attrs)
}
if (unary) {
const parent = stacks[0] || results
if (!parent.children) {
parent.children = []
}
parent.children.push(node)
} else {
stacks.unshift(node)
}
},
end: function end(tag) {
const node = stacks.shift()
if (node.name !== tag) {
console.error('invalid state: mismatch end tag')
}
if (stacks.length === 0) {
results.children.push(node)
} else {
const parent = stacks[0]
if (!parent.children) {
parent.children = []
}
parent.children.push(node)
}
},
chars: function chars(text) {
const node = {
type: 'text',
text: text
}
if (stacks.length === 0) {
results.children.push(node)
} else {
const parent = stacks[0]
if (!parent.children) {
parent.children = []
}
parent.children.push(node)
}
},
comment: function comment(text) {
const node = {
node: 'comment',
text: text
}
const parent = stacks[0]
if (!parent.children) {
parent.children = []
}
parent.children.push(node)
}
})
return results.children
}
return parseHtml(originText)
}
console.log(parseContent2(text))