-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
150 lines (125 loc) · 3.7 KB
/
index.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
var sax = require('sax-pausable');
var select = require('./lib/select');
var map = require('./lib/map');
var BufferedStream = require('bufferedstream');
var Middle = require('middle');
module.exports = function (opts) {
if (!opts) opts = {};
if (!opts.special) {
opts.special = [
'area', 'base', 'basefont', 'br', 'col',
'hr', 'input', 'img', 'link', 'meta'
];
}
opts.special = opts.special.map(function (x) { return x.toUpperCase() });
if (! opts.bufferSize) { opts.bufferSize = 1024; }
var parser = sax.parser(false);
var stream = select(parser, opts);
var bufferedStream = new BufferedStream(opts.bufferSize);
var middle = new Middle(bufferedStream, stream);
middle.select = function() {
return stream.select.apply(stream, arguments);
};
middle.update = function() {
return stream.update.apply(stream, arguments);
};
middle.remove = function() {
return stream.remove.apply(stream, arguments);
};
middle.replace = function() {
return stream.replace.apply(stream, arguments);
};
parser.onerror = function (err) {
stream.emit("error", err)
}
function write (buf) {
stream.emit('data', buf);
}
var buffered = '';
var pos = 0;
var update = function (type, tag, done) {
if (typeof tag === 'function') {
done = tag;
tag = undefined;
}
if (type === 'script') {
var len = tag.length;
}
else if (type === 'text') {
var len = parser.startTagPosition - pos - 1;
}
else if (type === 'open' && tag && tag.name === 'SCRIPT'
&& tag.attributes.src) {
var len = 0;
}
else {
var len = parser.position - parser.startTagPosition + 1;
}
pos = parser.position;
var src = buffered.slice(0, len);
buffered = buffered.slice(len);
stream.raw(src, done);
return src;
};
stream.write = function (buf) {
var s = buf.toString();
buffered += s;
parser.write(buf.toString());
};
stream.end = function (buf, next) {
if (buf !== undefined) stream.write(buf);
if (pos < parser.position) {
var s = buffered.slice(0, parser.position - pos);
stream.raw(s);
}
parser.close();
};
parser.onopentag = function (tag) {
stream.pre('open', tag);
update('open', tag, function() {
stream.post('open', tag);
});
};
parser.onend = function() {
stream.emit('end');
};
//
// Pausing and resuming
//
var paused = false;
oldResume = parser.resume;
parser.resume = function() {
paused = false;
oldResume.call(parser);
bufferedStream.resume();
};
oldPause = parser.pause;
parser.pause = function() {
bufferedStream.pause();
paused = true;
oldPause.call(parser);
};
parser.onclosetag = function (name) {
parser.pause();
stream.pre('close', name, function() {
update('close', function() {
stream.post('close', name);
parser.resume();
});
});
};
parser.ontext = function (text) {
stream.pre('text', text);
update('text', function() {
stream.post('text', text);
});
};
parser.onscript = function (src) {
stream.pre('script', src);
update('script', src, function() {
stream.post('script', src);
});
};
middle.map = map;
return middle;
};