This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlMatcher.js
288 lines (257 loc) · 6.8 KB
/
htmlMatcher.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
280
281
282
283
284
285
286
287
288
/**
* HTML matcher: takes string and searches for HTML tag pairs for given position
*
* Unlike “classic” matchers, it parses content from the specified
* position, not from the start, so it may work even outside HTML documents
* (for example, inside strings of programming languages like JavaScript, Python
* etc.)
*/
if (typeof module === 'object' && typeof define !== 'function') {
var define = function (factory) {
module.exports = factory(require, exports, module);
};
}
define(function(require, exports, module) {
var range = require('./range');
// Regular Expressions for parsing tags and attributes
var reOpenTag = /^<([\w\:\-]+)((?:\s+[\w\-:]+(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/;
var reCloseTag = /^<\/([\w\:\-]+)[^>]*>/;
function openTag(i, match) {
return {
name: match[1],
selfClose: !!match[3],
/** @type Range */
range: range(i, match[0]),
type: 'open'
};
}
function closeTag(i, match) {
return {
name: match[1],
/** @type Range */
range: range(i, match[0]),
type: 'close'
};
}
function comment(i, match) {
return {
/** @type Range */
range: range(i, typeof match == 'number' ? match - i : match[0]),
type: 'comment'
};
}
/**
* Creates new tag matcher session
* @param {String} text
*/
function createMatcher(text) {
var memo = {}, m;
return {
/**
* Test if given position matches opening tag
* @param {Number} i
* @returns {Object} Matched tag object
*/
open: function(i) {
var m = this.matches(i);
return m && m.type == 'open' ? m : null;
},
/**
* Test if given position matches closing tag
* @param {Number} i
* @returns {Object} Matched tag object
*/
close: function(i) {
var m = this.matches(i);
return m && m.type == 'close' ? m : null;
},
/**
* Matches either opening or closing tag for given position
* @param i
* @returns
*/
matches: function(i) {
var key = 'p' + i;
if (!(key in memo)) {
memo[key] = false;
if (text.charAt(i) == '<') {
var substr = text.slice(i);
if ((m = substr.match(reOpenTag))) {
memo[key] = openTag(i, m);
} else if ((m = substr.match(reCloseTag))) {
memo[key] = closeTag(i, m);
}
}
}
return memo[key];
},
/**
* Returns original text
* @returns {String}
*/
text: function() {
return text;
},
clean: function() {
memo = text = m = null;
}
};
}
function matches(text, pos, pattern) {
return text.substring(pos, pos + pattern.length) == pattern;
}
/**
* Search for closing pair of opening tag
* @param {Object} open Open tag instance
* @param {Object} matcher Matcher instance
*/
function findClosingPair(open, matcher) {
var stack = [], tag = null;
var text = matcher.text();
for (var pos = open.range.end, len = text.length; pos < len; pos++) {
if (matches(text, pos, '<!--')) {
// skip to end of comment
for (var j = pos; j < len; j++) {
if (matches(text, j, '-->')) {
pos = j + 3;
break;
}
}
}
if ((tag = matcher.matches(pos))) {
if (tag.type == 'open' && !tag.selfClose) {
stack.push(tag.name);
} else if (tag.type == 'close') {
if (!stack.length) { // found valid pair?
return tag.name == open.name ? tag : null;
}
// check if current closing tag matches previously opened one
if (stack[stack.length - 1] == tag.name) {
stack.pop();
} else {
var found = false;
while (stack.length && !found) {
var last = stack.pop();
if (last == tag.name) {
found = true;
}
}
if (!stack.length && !found) {
return tag.name == open.name ? tag : null;
}
}
}
pos = tag.range.end - 1;
}
}
}
return {
/**
* Main function: search for tag pair in <code>text</code> for given
* position
* @memberOf htmlMatcher
* @param {String} text
* @param {Number} pos
* @returns {Object}
*/
find: function(text, pos) {
var matcher = createMatcher(text);
var open = null, close = null;
var j, jl;
for (var i = pos; i >= 0; i--) {
if ((open = matcher.open(i))) {
// found opening tag
if (open.selfClose) {
if (open.range.cmp(pos, 'lt', 'gt')) {
// inside self-closing tag, found match
break;
}
// outside self-closing tag, continue
continue;
}
close = findClosingPair(open, matcher);
if (close) {
// found closing tag.
var r = range.create2(open.range.start, close.range.end);
if (r.contains(pos)) {
break;
}
} else if (open.range.contains(pos)) {
// we inside empty HTML tag like <br>
break;
}
open = null;
} else if (matches(text, i, '-->')) {
// skip back to comment start
for (j = i - 1; j >= 0; j--) {
if (matches(text, j, '-->')) {
// found another comment end, do nothing
break;
} else if (matches(text, j, '<!--')) {
i = j;
break;
}
}
} else if (matches(text, i, '<!--')) {
// we're inside comment, match it
for (j = i + 4, jl = text.length; j < jl; j++) {
if (matches(text, j, '-->')) {
j += 3;
break;
}
}
open = comment(i, j);
break;
}
}
matcher.clean();
if (open) {
var outerRange = null;
var innerRange = null;
if (close) {
outerRange = range.create2(open.range.start, close.range.end);
innerRange = range.create2(open.range.end, close.range.start);
} else {
outerRange = innerRange = range.create2(open.range.start, open.range.end);
}
if (open.type == 'comment') {
// adjust positions of inner range for comment
var _c = outerRange.substring(text);
innerRange.start += _c.length - _c.replace(/^<\!--\s*/, '').length;
innerRange.end -= _c.length - _c.replace(/\s*-->$/, '').length;
}
return {
open: open,
close: close,
type: open.type == 'comment' ? 'comment' : 'tag',
innerRange: innerRange,
innerContent: function() {
return this.innerRange.substring(text);
},
outerRange: outerRange,
outerContent: function() {
return this.outerRange.substring(text);
},
range: !innerRange.length() || !innerRange.cmp(pos, 'lte', 'gte') ? outerRange : innerRange,
content: function() {
return this.range.substring(text);
},
source: text
};
}
},
/**
* The same as <code>find()</code> method, but restricts matched result
* to <code>tag</code> type
* @param {String} text
* @param {Number} pos
* @returns {Object}
*/
tag: function(text, pos) {
var result = this.find(text, pos);
if (result && result.type == 'tag') {
return result;
}
}
};
});