-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathparser.js
137 lines (120 loc) · 3.21 KB
/
parser.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
'use strict';
var he = require('he');
var lowercase = require('./lowercase');
var attributes = require('./attributes');
var elements = require('./elements');
var rstart = /^<\s*([\w:-]+)((?:\s+[\w:-]+(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)\s*>/;
var rend = /^<\s*\/\s*([\w:-]+)[^>]*>/;
var rattrs = /([\w:-]+)(?:\s*=\s*(?:(?:"((?:[^"])*)")|(?:'((?:[^'])*)')|([^>\s]+)))?/g;
var rtag = /^</;
var rtagend = /^<\s*\//;
function createStack () {
var stack = [];
stack.lastItem = function lastItem () {
return stack[stack.length - 1];
};
return stack;
}
function parser (html, handler) {
var stack = createStack();
var last = html;
var chars;
while (html) {
parsePart();
}
parseEndTag(); // clean up any remaining tags
function parsePart () {
chars = true;
parseTag();
var same = html === last;
last = html;
if (same) { // discard, because it's invalid
html = '';
}
}
function parseTag () {
if (html.substr(0, 4) === '<!--') { // comments
parseComment();
} else if (rtagend.test(html)) {
parseEdge(rend, parseEndTag);
} else if (rtag.test(html)) {
parseEdge(rstart, parseStartTag);
}
parseTagDecode();
}
function parseEdge (regex, parser) {
var match = html.match(regex);
if (match) {
html = html.substring(match[0].length);
match[0].replace(regex, parser);
chars = false;
}
}
function parseComment () {
var index = html.indexOf('-->');
if (index >= 0) {
if (handler.comment) {
handler.comment(html.substring(4, index));
}
html = html.substring(index + 3);
chars = false;
}
}
function parseTagDecode () {
if (!chars) {
return;
}
var text;
var index = html.indexOf('<');
if (index >= 0) {
text = html.substring(0, index);
html = html.substring(index);
} else {
text = html;
html = '';
}
if (handler.chars) {
handler.chars(text);
}
}
function parseStartTag (tag, tagName, rest, unary) {
var attrs = {};
var low = lowercase(tagName);
var u = elements.voids[low] || !!unary;
rest.replace(rattrs, attrReplacer);
if (!u) {
stack.push(low);
}
if (handler.start) {
handler.start(low, attrs, u);
}
function attrReplacer (match, name, doubleQuotedValue, singleQuotedValue, unquotedValue) {
if (doubleQuotedValue === void 0 && singleQuotedValue === void 0 && unquotedValue === void 0) {
attrs[name] = void 0; // attribute is like <button disabled></button>
} else {
attrs[name] = he.decode(doubleQuotedValue || singleQuotedValue || unquotedValue || '');
}
}
}
function parseEndTag (tag, tagName) {
var i;
var pos = 0;
var low = lowercase(tagName);
if (low) {
for (pos = stack.length - 1; pos >= 0; pos--) {
if (stack[pos] === low) {
break; // find the closest opened tag of the same type
}
}
}
if (pos >= 0) {
for (i = stack.length - 1; i >= pos; i--) {
if (handler.end) { // close all the open elements, up the stack
handler.end(stack[i]);
}
}
stack.length = pos;
}
}
}
module.exports = parser;