forked from emmetio/emmet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.js
684 lines (591 loc) · 16 KB
/
common.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
/**
* Common utility helper for Emmet
*/
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('../assets/range');
/**
* Special token used as a placeholder for caret positions inside
* generated output
*/
var caretPlaceholder = '${0}';
return {
reTag: /<\/?[\w:\-]+(?:\s+[\w\-:]+(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*\s*(\/?)>$/,
defaultSyntax: function() {
return 'html';
},
defaultProfile: function() {
return 'plain';
},
/**
* Test if passed string ends with XHTML tag. This method is used for testing
* '>' character: it belongs to tag or it's a part of abbreviation?
* @param {String} str
* @return {Boolean}
*/
endsWithTag: function(str) {
return this.reTag.test(str);
},
/**
* Check if passed symbol is a number
* @param {String} ch
* @returns {Boolean}
*/
isNumeric: function(ch) {
if (typeof(ch) == 'string')
ch = ch.charCodeAt(0);
return (ch && ch > 47 && ch < 58);
},
/**
* Trim whitespace from string
* @param {String} text
* @return {String}
*/
trim: (function() {
if (String.prototype.trim) {
return function(text) {
return text ? text.trim() : '';
};
}
return function(text) {
return (text || "").replace(/^\s+|\s+$/g, "");
}
})(),
/**
* Split text into lines. Set <code>remove_empty</code> to true to filter
* empty lines
* @param {String} text Text to split
* @param {Boolean} removeEmpty Remove empty lines from result
* @return {Array}
*/
splitByLines: function(text, removeEmpty) {
// IE fails to split string by regexp,
// need to normalize newlines first
// Also, Mozilla's Rhiho JS engine has a weird newline bug
var nl = '\n';
var lines = (text || '')
.replace(/\r\n/g, '\n')
.replace(/\n\r/g, '\n')
.replace(/\r/g, '\n')
.replace(/\n/g, nl)
.split(nl);
if (removeEmpty) {
lines = lines.filter(function(line) {
return line.length && !!this.trim(line);
}, this);
}
return lines;
},
/**
* Repeats string <code>howMany</code> times
* @param {String} str
* @param {Number} how_many
* @return {String}
*/
repeatString: function(str, howMany) {
var out = '';
while (howMany--) {
out += str;
}
return out;
},
/**
* Returns list of paddings that should be used to align passed string
* @param {Array} strings
* @returns {Array}
*/
getStringsPads: function(strings) {
var lengths = strings.map(function(s) {
return typeof s === 'string' ? s.length : +s;
});
var max = lengths.reduce(function(prev, cur) {
return typeof prev === 'undefined' ? cur : Math.max(prev, cur);
});
return lengths.map(function(l) {
var pad = max - l;
return pad ? this.repeatString(' ', pad) : '';
}, this);
},
/**
* Indents text with padding
* @param {String} text Text to indent
* @param {String} pad Padding size (number) or padding itself (string)
* @return {String}
*/
padString: function(text, pad) {
var result = [];
var lines = this.splitByLines(text);
var nl = '\n';
result.push(lines[0]);
for (var j = 1; j < lines.length; j++)
result.push(nl + pad + lines[j]);
return result.join('');
},
/**
* Pad string with zeroes
* @param {String} str String to pad
* @param {Number} pad Desired string length
* @return {String}
*/
zeroPadString: function(str, pad) {
var padding = '';
var il = str.length;
while (pad > il++) padding += '0';
return padding + str;
},
/**
* Removes padding at the beginning of each text's line
* @param {String} text
* @param {String} pad
*/
unindentString: function(text, pad) {
var lines = this.splitByLines(text);
var pl = pad.length;
for (var i = 0, il = lines.length, line; i < il; i++) {
line = lines[i];
if (line.substr(0, pl) === pad) {
lines[i] = line.substr(pl);
}
}
return lines.join('\n');
},
/**
* Replaces unescaped symbols in <code>str</code>. For example, the '$' symbol
* will be replaced in 'item$count', but not in 'item\$count'.
* @param {String} str Original string
* @param {String} symbol Symbol to replace
* @param {String} replace Symbol replacement. Might be a function that
* returns new value
* @return {String}
*/
replaceUnescapedSymbol: function(str, symbol, replace) {
var i = 0;
var il = str.length;
var sl = symbol.length;
var matchCount = 0;
while (i < il) {
if (str.charAt(i) == '\\') {
// escaped symbol, skip next character
i += sl + 1;
} else if (str.substr(i, sl) == symbol) {
// have match
var curSl = sl;
matchCount++;
var newValue = replace;
if (typeof replace === 'function') {
var replaceData = replace(str, symbol, i, matchCount);
if (replaceData) {
curSl = replaceData[0].length;
newValue = replaceData[1];
} else {
newValue = false;
}
}
if (newValue === false) { // skip replacement
i++;
continue;
}
str = str.substring(0, i) + newValue + str.substring(i + curSl);
// adjust indexes
il = str.length;
i += newValue.length;
} else {
i++;
}
}
return str;
},
/**
* Replaces '$' character in string assuming it might be escaped with '\'
* @param {String} str String where character should be replaced
* @param {String} value New value
* @return {String}
*/
replaceCounter: function(str, value, total) {
var symbol = '$';
// in case we received strings from Java, convert the to native strings
str = String(str);
value = String(value);
if (/^\-?\d+$/.test(value)) {
value = +value;
}
var that = this;
return this.replaceUnescapedSymbol(str, symbol, function(str, symbol, pos, matchNum){
if (str.charAt(pos + 1) == '{' || that.isNumeric(str.charAt(pos + 1)) ) {
// it's a variable, skip it
return false;
}
// replace sequense of $ symbols with padded number
var j = pos + 1;
while(str.charAt(j) == '$' && str.charAt(j + 1) != '{') j++;
var pad = j - pos;
// get counter base
var base = 0, decrement = false, m;
if ((m = str.substr(j).match(/^@(\-?)(\d*)/))) {
j += m[0].length;
if (m[1]) {
decrement = true;
}
base = parseInt(m[2] || 1, 10) - 1;
}
if (decrement && total && typeof value === 'number') {
value = total - value + 1;
}
value += base;
return [str.substring(pos, j), that.zeroPadString(value + '', pad)];
});
},
/**
* Check if string matches against <code>reTag</code> regexp. This
* function may be used to test if provided string contains HTML tags
* @param {String} str
* @returns {Boolean}
*/
matchesTag: function(str) {
return this.reTag.test(str || '');
},
/**
* Escapes special characters used in Emmet, like '$', '|', etc.
* Use this method before passing to actions like "Wrap with Abbreviation"
* to make sure that existing special characters won't be altered
* @param {String} text
* @return {String}
*/
escapeText: function(text) {
return text.replace(/([\$\\])/g, '\\$1');
},
/**
* Unescapes special characters used in Emmet, like '$', '|', etc.
* @param {String} text
* @return {String}
*/
unescapeText: function(text) {
return text.replace(/\\(.)/g, '$1');
},
/**
* Returns caret placeholder
* @returns {String}
*/
getCaretPlaceholder: function() {
return typeof caretPlaceholder === 'function'
? caretPlaceholder.apply(this, arguments)
: caretPlaceholder;
},
/**
* Sets new representation for carets in generated output
* @param {String} value New caret placeholder. Might be a
* <code>Function</code>
*/
setCaretPlaceholder: function(value) {
caretPlaceholder = value;
},
/**
* Returns line padding
* @param {String} line
* @return {String}
*/
getLinePadding: function(line) {
return (line.match(/^(\s+)/) || [''])[0];
},
/**
* Helper function that returns padding of line of <code>pos</code>
* position in <code>content</code>
* @param {String} content
* @param {Number} pos
* @returns {String}
*/
getLinePaddingFromPosition: function(content, pos) {
var lineRange = this.findNewlineBounds(content, pos);
return this.getLinePadding(lineRange.substring(content));
},
/**
* Escape special regexp chars in string, making it usable for creating dynamic
* regular expressions
* @param {String} str
* @return {String}
*/
escapeForRegexp: function(str) {
var specials = new RegExp("[.*+?|()\\[\\]{}\\\\]", "g"); // .*+?|()[]{}\
return str.replace(specials, "\\$&");
},
/**
* Make decimal number look good: convert it to fixed precision end remove
* traling zeroes
* @param {Number} num
* @param {Number} fracion Fraction numbers (default is 2)
* @return {String}
*/
prettifyNumber: function(num, fraction) {
return num.toFixed(typeof fraction == 'undefined' ? 2 : fraction).replace(/\.?0+$/, '');
},
/**
* Replace substring of <code>str</code> with <code>value</code>
* @param {String} str String where to replace substring
* @param {String} value New substring value
* @param {Number} start Start index of substring to replace. May also
* be a <code>Range</code> object: in this case, the <code>end</code>
* argument is not required
* @param {Number} end End index of substring to replace. If ommited,
* <code>start</code> argument is used
*/
replaceSubstring: function(str, value, start, end) {
if (typeof start === 'object' && 'end' in start) {
end = start.end;
start = start.start;
}
if (typeof end === 'string') {
end = start + end.length;
}
if (typeof end === 'undefined') {
end = start;
}
if (start < 0 || start > str.length)
return str;
return str.substring(0, start) + value + str.substring(end);
},
/**
* Fills substrings in `content`, defined by given ranges,
* wich `ch` character
* @param {String} content
* @param {Array} ranges
* @return {String}
*/
replaceWith: function(content, ranges, ch, noRepeat) {
if (ranges.length) {
var offset = 0, fragments = [];
ranges.forEach(function(r) {
var repl = noRepeat ? ch : this.repeatString(ch, r[1] - r[0]);
fragments.push(content.substring(offset, r[0]), repl);
offset = r[1];
}, this);
content = fragments.join('') + content.substring(offset);
}
return content;
},
/**
* Narrows down text range, adjusting selection to non-space characters
* @param {String} text
* @param {Number} start Starting range in <code>text</code> where
* slection should be adjusted. Can also be any object that is accepted
* by <code>Range</code> class
* @return {Range}
*/
narrowToNonSpace: function(text, start, end) {
var rng = range.create(start, end);
var reSpace = /[\s\n\r\u00a0]/;
// narrow down selection until first non-space character
while (rng.start < rng.end) {
if (!reSpace.test(text.charAt(rng.start)))
break;
rng.start++;
}
while (rng.end > rng.start) {
rng.end--;
if (!reSpace.test(text.charAt(rng.end))) {
rng.end++;
break;
}
}
return rng;
},
/**
* Find start and end index of text line for <code>from</code> index
* @param {String} text
* @param {Number} from
*/
findNewlineBounds: function(text, from) {
var len = text.length,
start = 0,
end = len - 1,
ch;
// search left
for (var i = from - 1; i > 0; i--) {
ch = text.charAt(i);
if (ch == '\n' || ch == '\r') {
start = i + 1;
break;
}
}
// search right
for (var j = from; j < len; j++) {
ch = text.charAt(j);
if (ch == '\n' || ch == '\r') {
end = j;
break;
}
}
return range.create(start, end - start);
},
/**
* Deep merge of two or more objects. Taken from jQuery.extend()
*/
deepMerge: function() {
var options, name, src, copy, copyIsArray, clone,
target = arguments[0] || {},
i = 1,
length = arguments.length;
// Handle case when target is a string or something (possible in deep copy)
if (typeof target !== 'object' && typeof target !== 'function') {
target = {};
}
for ( ; i < length; i++ ) {
// Only deal with non-null/undefined values
if ( (options = arguments[ i ]) !== null ) {
// Extend the base object
for ( name in options ) {
src = target[ name ];
copy = options[ name ];
// Prevent never-ending loop
if ( target === copy ) {
continue;
}
// Recurse if we're merging plain objects or arrays
if ( copy && ( typeof copy === 'object' || (copyIsArray = Array.isArray(copy)) ) ) {
if ( copyIsArray ) {
copyIsArray = false;
clone = src && Array.isArray(src) ? src : [];
} else {
clone = src && typeof src === 'object' ? src : {};
}
// Never move original objects, clone them
target[ name ] = this.deepMerge(clone, copy );
// Don't bring in undefined values
} else if ( copy !== undefined ) {
target[ name ] = copy;
}
}
}
}
// Return the modified object
return target;
},
/**
* Dead simple string-to-JSON parser
* @param {String} str
* @returns {Object}
*/
parseJSON: function(str) {
if (typeof str == 'object') {
return str;
}
try {
return JSON.parse(str);
} catch(e) {
return {};
}
},
/**************************
* Utility belt
**************************/
unique: function(arr, comparator) {
var lookup = [];
return arr.filter(function(item) {
var val = comparator ? comparator(item) : item;
if (lookup.indexOf(val) < 0) {
lookup.push(val);
return true;
}
});
},
/**
* Return a copy of the object, filtered to only have values for
* the whitelisted keys.
* @param {Object} obj
* @return {Object}
*/
pick: function(obj) {
var result = {};
var keys = this.toArray(arguments, 1);
Object.keys(obj).forEach(function(key) {
if (~keys.indexOf(key)) {
result[key] = obj[key];
}
});
return result;
},
find: function(arr, comparator, ctx) {
var result;
if (ctx) {
comparator = comparator.bind(ctx);
}
if (Array.isArray(arr)) {
arr.some(function(item, i) {
if (comparator(item, i)) {
return result = item;
}
});
} else {
Object.keys(arr).some(function(key, i) {
if (comparator(arr[key], i)) {
return result = arr[key];
}
});
}
return result;
},
toArray: function(obj, sliceIx) {
if (Array.isArray(obj) && !sliceIx) {
return obj;
}
return Array.prototype.slice.call(obj, sliceIx || 0);
},
extend: function(obj) {
for (var i = 1, il = arguments.length, a; i < il; i++) {
a = arguments[i];
if (a) {
Object.keys(a).forEach(function(key) {
obj[key] = a[key];
});
}
}
return obj;
},
defaults: function(obj) {
for (var i = 1, il = arguments.length, a; i < il; i++) {
a = arguments[i];
if (a) {
Object.keys(a).forEach(function(key) {
if (!(key in obj)) {
obj[key] = a[key];
}
});
}
}
return obj;
},
flatten: function(arr, out) {
out = out || [];
var self = this;
self.toArray(arr).forEach(function(item) {
if (Array.isArray(item)) {
self.flatten(item, out);
} else {
out.push(item);
}
});
return out;
},
clone: function(obj) {
if (Array.isArray(obj)) {
return obj.slice(0);
}
return this.extend({}, obj);
},
without: function(arr) {
this.toArray(arguments, 1).forEach(function(item) {
var ix;
while (~(ix = arr.indexOf(item))) {
arr.splice(ix, 1);
}
});
return arr;
},
last: function(arr) {
return arr[arr.length - 1];
}
};
});