svg-parser.umd.js
7.51 KB
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
289
290
291
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.svgParser = {}));
}(this, (function (exports) { 'use strict';
function getLocator(source, options) {
if (options === void 0) { options = {}; }
var offsetLine = options.offsetLine || 0;
var offsetColumn = options.offsetColumn || 0;
var originalLines = source.split('\n');
var start = 0;
var lineRanges = originalLines.map(function (line, i) {
var end = start + line.length + 1;
var range = { start: start, end: end, line: i };
start = end;
return range;
});
var i = 0;
function rangeContains(range, index) {
return range.start <= index && index < range.end;
}
function getLocation(range, index) {
return { line: offsetLine + range.line, column: offsetColumn + index - range.start, character: index };
}
function locate(search, startIndex) {
if (typeof search === 'string') {
search = source.indexOf(search, startIndex || 0);
}
var range = lineRanges[i];
var d = search >= range.end ? 1 : -1;
while (range) {
if (rangeContains(range, search))
return getLocation(range, search);
i += d;
range = lineRanges[i];
}
}
return locate;
}
function locate(source, search, options) {
if (typeof options === 'number') {
throw new Error('locate takes a { startIndex, offsetLine, offsetColumn } object as the third argument');
}
return getLocator(source, options)(search, options && options.startIndex);
}
var validNameCharacters = /[a-zA-Z0-9:_-]/;
var whitespace = /[\s\t\r\n]/;
var quotemark = /['"]/;
function repeat(str, i) {
var result = '';
while (i--) { result += str; }
return result;
}
function parse(source) {
var header = '';
var stack = [];
var state = metadata;
var currentElement = null;
var root = null;
function error(message) {
var ref = locate(source, i);
var line = ref.line;
var column = ref.column;
var before = source.slice(0, i);
var beforeLine = /(^|\n).*$/.exec(before)[0].replace(/\t/g, ' ');
var after = source.slice(i);
var afterLine = /.*(\n|$)/.exec(after)[0];
var snippet = "" + beforeLine + afterLine + "\n" + (repeat(' ', beforeLine.length)) + "^";
throw new Error(
(message + " (" + line + ":" + column + "). If this is valid SVG, it's probably a bug in svg-parser. Please raise an issue at https://github.com/Rich-Harris/svg-parser/issues – thanks!\n\n" + snippet)
);
}
function metadata() {
while ((i < source.length && source[i] !== '<') || !validNameCharacters.test(source[i + 1])) {
header += source[i++];
}
return neutral();
}
function neutral() {
var text = '';
while (i < source.length && source[i] !== '<') { text += source[i++]; }
if (/\S/.test(text)) {
currentElement.children.push({ type: 'text', value: text });
}
if (source[i] === '<') {
return tag;
}
return neutral;
}
function tag() {
var char = source[i];
if (char === '?') { return neutral; } // <?xml...
if (char === '!') {
if (source.slice(i + 1, i + 3) === '--') { return comment; }
if (source.slice(i + 1, i + 8) === '[CDATA[') { return cdata; }
if (/doctype/i.test(source.slice(i + 1, i + 8))) { return neutral; }
}
if (char === '/') { return closingTag; }
var tagName = getName();
var element = {
type: 'element',
tagName: tagName,
properties: {},
children: []
};
if (currentElement) {
currentElement.children.push(element);
} else {
root = element;
}
var attribute;
while (i < source.length && (attribute = getAttribute())) {
element.properties[attribute.name] = attribute.value;
}
var selfClosing = false;
if (source[i] === '/') {
i += 1;
selfClosing = true;
}
if (source[i] !== '>') {
error('Expected >');
}
if (!selfClosing) {
currentElement = element;
stack.push(element);
}
return neutral;
}
function comment() {
var index = source.indexOf('-->', i);
if (!~index) { error('expected -->'); }
i = index + 2;
return neutral;
}
function cdata() {
var index = source.indexOf(']]>', i);
if (!~index) { error('expected ]]>'); }
currentElement.children.push(source.slice(i + 7, index));
i = index + 2;
return neutral;
}
function closingTag() {
var tagName = getName();
if (!tagName) { error('Expected tag name'); }
if (tagName !== currentElement.tagName) {
error(("Expected closing tag </" + tagName + "> to match opening tag <" + (currentElement.tagName) + ">"));
}
allowSpaces();
if (source[i] !== '>') {
error('Expected >');
}
stack.pop();
currentElement = stack[stack.length - 1];
return neutral;
}
function getName() {
var name = '';
while (i < source.length && validNameCharacters.test(source[i])) { name += source[i++]; }
return name;
}
function getAttribute() {
if (!whitespace.test(source[i])) { return null; }
allowSpaces();
var name = getName();
if (!name) { return null; }
var value = true;
allowSpaces();
if (source[i] === '=') {
i += 1;
allowSpaces();
value = getAttributeValue();
if (!isNaN(value) && value.trim() !== '') { value = +value; } // TODO whitelist numeric attributes?
}
return { name: name, value: value };
}
function getAttributeValue() {
return quotemark.test(source[i]) ? getQuotedAttributeValue() : getUnquotedAttributeValue();
}
function getUnquotedAttributeValue() {
var value = '';
do {
var char = source[i];
if (char === ' ' || char === '>' || char === '/') {
return value;
}
value += char;
i += 1;
} while (i < source.length);
return value;
}
function getQuotedAttributeValue() {
var quotemark = source[i++];
var value = '';
var escaped = false;
while (i < source.length) {
var char = source[i++];
if (char === quotemark && !escaped) {
return value;
}
if (char === '\\' && !escaped) {
escaped = true;
}
value += escaped ? ("\\" + char) : char;
escaped = false;
}
}
function allowSpaces() {
while (i < source.length && whitespace.test(source[i])) { i += 1; }
}
var i = metadata.length;
while (i < source.length) {
if (!state) { error('Unexpected character'); }
state = state();
i += 1;
}
if (state !== neutral) {
error('Unexpected end of input');
}
if (root.tagName === 'svg') { root.metadata = header; }
return {
type: 'root',
children: [root]
};
}
exports.parse = parse;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=svg-parser.umd.js.map