-
-
Notifications
You must be signed in to change notification settings - Fork 374
/
htmlparser.js
230 lines (207 loc) · 7.94 KB
/
htmlparser.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
/**
* Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
* MIT Licensed
*/
var HTMLParser = (function(undefined){
var HTMLParser = function(){
var self = this;
self._init.apply(self,arguments);
};
HTMLParser.prototype = {
_init: function(){
var self = this;
self._listeners = {};
self._mapCdataTags = self.makeMap("script,style");
self._arrBlocks = [];
},
makeMap: function(str){
var obj = {}, items = str.split(",");
for ( var i = 0; i < items.length; i++ ){
obj[ items[i] ] = true;
}
return obj;
},
// parse html code
parse: function(html){
var self = this,
mapCdataTags = self._mapCdataTags;
var regTag=/<(?:\/([^\s>]+)\s*|!--([\s\S]*?)--|!([^>]*?)|([\w\-:]+)((?:"[^"]*"|'[^']*'|[^"'<>])*?)\s*(\/?))>/g,
regAttr = /\s*([\w\-:]+)(?:\s*=\s*(?:(")([^"]*)"|(')([^']*)'|([^\s]+)))?/g,
regLine = /\r?\n/g;
var match, matchIndex, lastIndex = 0, tagName, arrAttrs, tagCDATA, arrCDATA, lastCDATAIndex = 0, text;
var lastLineIndex = 0, line = 1;
var arrBlocks = self._arrBlocks;
self.fire('start', {
pos: 0,
line: 1,
col: 1
});
while((match = regTag.exec(html))){
matchIndex = match.index;
if(matchIndex > lastIndex){//保存前面的文本或者CDATA
text = html.substring(lastIndex, matchIndex);
if(tagCDATA){
arrCDATA.push(text);
}
else{//文本
saveBlock('text', text, lastIndex);
}
}
lastIndex = regTag.lastIndex;
if((tagName = match[1])){
if(tagCDATA && tagName === tagCDATA){//结束标签前输出CDATA
text = arrCDATA.join('');
saveBlock('cdata', text, lastCDATAIndex, {
'tagName': tagCDATA
});
tagCDATA = null;
arrCDATA = null;
}
if(!tagCDATA){
//标签结束
saveBlock('tagend', match[0], matchIndex, {
'tagName': tagName
});
continue;
}
}
if(tagCDATA){
arrCDATA.push(match[0]);
}
else{
if((tagName = match[4])){//标签开始
arrAttrs = [];
var attrs = match[5],
attrMatch,
attrMatchCount = 0;
while((attrMatch = regAttr.exec(attrs))){
var name = attrMatch[1],
quote = attrMatch[2] ? attrMatch[2] :
attrMatch[4] ? attrMatch[4] : '',
value = attrMatch[3] ? attrMatch[3] :
attrMatch[5] ? attrMatch[5] :
attrMatch[6] ? attrMatch[6] : '';
arrAttrs.push({'name': name, 'value': value, 'quote': quote, 'index': attrMatch.index, 'raw': attrMatch[0]});
attrMatchCount += attrMatch[0].length;
}
if(attrMatchCount === attrs.length){
saveBlock('tagstart', match[0], matchIndex, {
'tagName': tagName,
'attrs': arrAttrs,
'close': match[6]
});
if(mapCdataTags[tagName]){
tagCDATA = tagName;
arrCDATA = [];
lastCDATAIndex = lastIndex;
}
}
else{//如果出现漏匹配,则把当前内容匹配为text
saveBlock('text', match[0], matchIndex);
}
}
else if(match[2] || match[3]){//注释标签
saveBlock('comment', match[0], matchIndex, {
'content': match[2] || match[3],
'long': match[2]?true:false
});
}
}
}
if(html.length > lastIndex){
//结尾文本
text = html.substring(lastIndex, html.length);
saveBlock('text', text, lastIndex);
}
self.fire('end', {
pos: lastIndex,
line: line,
col: lastIndex - lastLineIndex + 1
});
//存储区块
function saveBlock(type, raw, pos, data){
var col = pos - lastLineIndex + 1;
if(data === undefined){
data = {};
}
data.raw = raw;
data.pos = pos;
data.line = line;
data.col = col;
arrBlocks.push(data);
self.fire(type, data);
var lineMatch;
while((lineMatch = regLine.exec(raw))){
line ++;
lastLineIndex = pos + regLine.lastIndex;
}
}
},
// add event
addListener: function(types, listener){
var _listeners = this._listeners;
var arrTypes = types.split(/[,\s]/), type;
for(var i=0, l = arrTypes.length;i<l;i++){
type = arrTypes[i];
if (_listeners[type] === undefined){
_listeners[type] = [];
}
_listeners[type].push(listener);
}
},
// fire event
fire: function(type, data){
if (data === undefined){
data = {};
}
data.type = type;
var self = this,
listeners = [],
listenersType = self._listeners[type],
listenersAll = self._listeners['all'];
if (listenersType !== undefined){
listeners = listeners.concat(listenersType);
}
if (listenersAll !== undefined){
listeners = listeners.concat(listenersAll);
}
for (var i = 0, l = listeners.length; i < l; i++){
listeners[i].call(self, data);
}
},
// remove event
removeListener: function(type, listener){
var listenersType = this._listeners[type];
if(listenersType !== undefined){
for (var i = 0, l = listenersType.length; i < l; i++){
if (listenersType[i] === listener){
listenersType.splice(i, 1);
break;
}
}
}
},
//fix pos if event.raw have \n
fixPos: function(event, index){
var text = event.raw.substr(0, index);
var arrLines = text.split(/\r?\n/),
lineCount = arrLines.length - 1,
line = event.line, col;
if(lineCount > 0){
line += lineCount;
col = arrLines[lineCount].length + 1;
}
else{
col = event.col + index;
}
return {
line: line,
col: col
};
}
};
return HTMLParser;
})();
if (typeof exports === 'object' && exports){
exports.HTMLParser = HTMLParser;
}