source: frontend/node_modules/magic-string/dist/magic-string.cjs.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 33.1 KB
Line 
1'use strict';
2
3var sourcemapCodec = require('sourcemap-codec');
4
5var BitSet = function BitSet(arg) {
6 this.bits = arg instanceof BitSet ? arg.bits.slice() : [];
7};
8
9BitSet.prototype.add = function add (n) {
10 this.bits[n >> 5] |= 1 << (n & 31);
11};
12
13BitSet.prototype.has = function has (n) {
14 return !!(this.bits[n >> 5] & (1 << (n & 31)));
15};
16
17var Chunk = function Chunk(start, end, content) {
18 this.start = start;
19 this.end = end;
20 this.original = content;
21
22 this.intro = '';
23 this.outro = '';
24
25 this.content = content;
26 this.storeName = false;
27 this.edited = false;
28
29 // we make these non-enumerable, for sanity while debugging
30 Object.defineProperties(this, {
31 previous: { writable: true, value: null },
32 next: { writable: true, value: null },
33 });
34};
35
36Chunk.prototype.appendLeft = function appendLeft (content) {
37 this.outro += content;
38};
39
40Chunk.prototype.appendRight = function appendRight (content) {
41 this.intro = this.intro + content;
42};
43
44Chunk.prototype.clone = function clone () {
45 var chunk = new Chunk(this.start, this.end, this.original);
46
47 chunk.intro = this.intro;
48 chunk.outro = this.outro;
49 chunk.content = this.content;
50 chunk.storeName = this.storeName;
51 chunk.edited = this.edited;
52
53 return chunk;
54};
55
56Chunk.prototype.contains = function contains (index) {
57 return this.start < index && index < this.end;
58};
59
60Chunk.prototype.eachNext = function eachNext (fn) {
61 var chunk = this;
62 while (chunk) {
63 fn(chunk);
64 chunk = chunk.next;
65 }
66};
67
68Chunk.prototype.eachPrevious = function eachPrevious (fn) {
69 var chunk = this;
70 while (chunk) {
71 fn(chunk);
72 chunk = chunk.previous;
73 }
74};
75
76Chunk.prototype.edit = function edit (content, storeName, contentOnly) {
77 this.content = content;
78 if (!contentOnly) {
79 this.intro = '';
80 this.outro = '';
81 }
82 this.storeName = storeName;
83
84 this.edited = true;
85
86 return this;
87};
88
89Chunk.prototype.prependLeft = function prependLeft (content) {
90 this.outro = content + this.outro;
91};
92
93Chunk.prototype.prependRight = function prependRight (content) {
94 this.intro = content + this.intro;
95};
96
97Chunk.prototype.split = function split (index) {
98 var sliceIndex = index - this.start;
99
100 var originalBefore = this.original.slice(0, sliceIndex);
101 var originalAfter = this.original.slice(sliceIndex);
102
103 this.original = originalBefore;
104
105 var newChunk = new Chunk(index, this.end, originalAfter);
106 newChunk.outro = this.outro;
107 this.outro = '';
108
109 this.end = index;
110
111 if (this.edited) {
112 // TODO is this block necessary?...
113 newChunk.edit('', false);
114 this.content = '';
115 } else {
116 this.content = originalBefore;
117 }
118
119 newChunk.next = this.next;
120 if (newChunk.next) { newChunk.next.previous = newChunk; }
121 newChunk.previous = this;
122 this.next = newChunk;
123
124 return newChunk;
125};
126
127Chunk.prototype.toString = function toString () {
128 return this.intro + this.content + this.outro;
129};
130
131Chunk.prototype.trimEnd = function trimEnd (rx) {
132 this.outro = this.outro.replace(rx, '');
133 if (this.outro.length) { return true; }
134
135 var trimmed = this.content.replace(rx, '');
136
137 if (trimmed.length) {
138 if (trimmed !== this.content) {
139 this.split(this.start + trimmed.length).edit('', undefined, true);
140 }
141 return true;
142 } else {
143 this.edit('', undefined, true);
144
145 this.intro = this.intro.replace(rx, '');
146 if (this.intro.length) { return true; }
147 }
148};
149
150Chunk.prototype.trimStart = function trimStart (rx) {
151 this.intro = this.intro.replace(rx, '');
152 if (this.intro.length) { return true; }
153
154 var trimmed = this.content.replace(rx, '');
155
156 if (trimmed.length) {
157 if (trimmed !== this.content) {
158 this.split(this.end - trimmed.length);
159 this.edit('', undefined, true);
160 }
161 return true;
162 } else {
163 this.edit('', undefined, true);
164
165 this.outro = this.outro.replace(rx, '');
166 if (this.outro.length) { return true; }
167 }
168};
169
170var btoa = function () {
171 throw new Error('Unsupported environment: `window.btoa` or `Buffer` should be supported.');
172};
173if (typeof window !== 'undefined' && typeof window.btoa === 'function') {
174 btoa = function (str) { return window.btoa(unescape(encodeURIComponent(str))); };
175} else if (typeof Buffer === 'function') {
176 btoa = function (str) { return Buffer.from(str, 'utf-8').toString('base64'); };
177}
178
179var SourceMap = function SourceMap(properties) {
180 this.version = 3;
181 this.file = properties.file;
182 this.sources = properties.sources;
183 this.sourcesContent = properties.sourcesContent;
184 this.names = properties.names;
185 this.mappings = sourcemapCodec.encode(properties.mappings);
186};
187
188SourceMap.prototype.toString = function toString () {
189 return JSON.stringify(this);
190};
191
192SourceMap.prototype.toUrl = function toUrl () {
193 return 'data:application/json;charset=utf-8;base64,' + btoa(this.toString());
194};
195
196function guessIndent(code) {
197 var lines = code.split('\n');
198
199 var tabbed = lines.filter(function (line) { return /^\t+/.test(line); });
200 var spaced = lines.filter(function (line) { return /^ {2,}/.test(line); });
201
202 if (tabbed.length === 0 && spaced.length === 0) {
203 return null;
204 }
205
206 // More lines tabbed than spaced? Assume tabs, and
207 // default to tabs in the case of a tie (or nothing
208 // to go on)
209 if (tabbed.length >= spaced.length) {
210 return '\t';
211 }
212
213 // Otherwise, we need to guess the multiple
214 var min = spaced.reduce(function (previous, current) {
215 var numSpaces = /^ +/.exec(current)[0].length;
216 return Math.min(numSpaces, previous);
217 }, Infinity);
218
219 return new Array(min + 1).join(' ');
220}
221
222function getRelativePath(from, to) {
223 var fromParts = from.split(/[/\\]/);
224 var toParts = to.split(/[/\\]/);
225
226 fromParts.pop(); // get dirname
227
228 while (fromParts[0] === toParts[0]) {
229 fromParts.shift();
230 toParts.shift();
231 }
232
233 if (fromParts.length) {
234 var i = fromParts.length;
235 while (i--) { fromParts[i] = '..'; }
236 }
237
238 return fromParts.concat(toParts).join('/');
239}
240
241var toString = Object.prototype.toString;
242
243function isObject(thing) {
244 return toString.call(thing) === '[object Object]';
245}
246
247function getLocator(source) {
248 var originalLines = source.split('\n');
249 var lineOffsets = [];
250
251 for (var i = 0, pos = 0; i < originalLines.length; i++) {
252 lineOffsets.push(pos);
253 pos += originalLines[i].length + 1;
254 }
255
256 return function locate(index) {
257 var i = 0;
258 var j = lineOffsets.length;
259 while (i < j) {
260 var m = (i + j) >> 1;
261 if (index < lineOffsets[m]) {
262 j = m;
263 } else {
264 i = m + 1;
265 }
266 }
267 var line = i - 1;
268 var column = index - lineOffsets[line];
269 return { line: line, column: column };
270 };
271}
272
273var Mappings = function Mappings(hires) {
274 this.hires = hires;
275 this.generatedCodeLine = 0;
276 this.generatedCodeColumn = 0;
277 this.raw = [];
278 this.rawSegments = this.raw[this.generatedCodeLine] = [];
279 this.pending = null;
280};
281
282Mappings.prototype.addEdit = function addEdit (sourceIndex, content, loc, nameIndex) {
283 if (content.length) {
284 var segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
285 if (nameIndex >= 0) {
286 segment.push(nameIndex);
287 }
288 this.rawSegments.push(segment);
289 } else if (this.pending) {
290 this.rawSegments.push(this.pending);
291 }
292
293 this.advance(content);
294 this.pending = null;
295};
296
297Mappings.prototype.addUneditedChunk = function addUneditedChunk (sourceIndex, chunk, original, loc, sourcemapLocations) {
298 var originalCharIndex = chunk.start;
299 var first = true;
300
301 while (originalCharIndex < chunk.end) {
302 if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
303 this.rawSegments.push([this.generatedCodeColumn, sourceIndex, loc.line, loc.column]);
304 }
305
306 if (original[originalCharIndex] === '\n') {
307 loc.line += 1;
308 loc.column = 0;
309 this.generatedCodeLine += 1;
310 this.raw[this.generatedCodeLine] = this.rawSegments = [];
311 this.generatedCodeColumn = 0;
312 first = true;
313 } else {
314 loc.column += 1;
315 this.generatedCodeColumn += 1;
316 first = false;
317 }
318
319 originalCharIndex += 1;
320 }
321
322 this.pending = null;
323};
324
325Mappings.prototype.advance = function advance (str) {
326 if (!str) { return; }
327
328 var lines = str.split('\n');
329
330 if (lines.length > 1) {
331 for (var i = 0; i < lines.length - 1; i++) {
332 this.generatedCodeLine++;
333 this.raw[this.generatedCodeLine] = this.rawSegments = [];
334 }
335 this.generatedCodeColumn = 0;
336 }
337
338 this.generatedCodeColumn += lines[lines.length - 1].length;
339};
340
341var n = '\n';
342
343var warned = {
344 insertLeft: false,
345 insertRight: false,
346 storeName: false,
347};
348
349var MagicString = function MagicString(string, options) {
350 if ( options === void 0 ) options = {};
351
352 var chunk = new Chunk(0, string.length, string);
353
354 Object.defineProperties(this, {
355 original: { writable: true, value: string },
356 outro: { writable: true, value: '' },
357 intro: { writable: true, value: '' },
358 firstChunk: { writable: true, value: chunk },
359 lastChunk: { writable: true, value: chunk },
360 lastSearchedChunk: { writable: true, value: chunk },
361 byStart: { writable: true, value: {} },
362 byEnd: { writable: true, value: {} },
363 filename: { writable: true, value: options.filename },
364 indentExclusionRanges: { writable: true, value: options.indentExclusionRanges },
365 sourcemapLocations: { writable: true, value: new BitSet() },
366 storedNames: { writable: true, value: {} },
367 indentStr: { writable: true, value: guessIndent(string) },
368 });
369
370 this.byStart[0] = chunk;
371 this.byEnd[string.length] = chunk;
372};
373
374MagicString.prototype.addSourcemapLocation = function addSourcemapLocation (char) {
375 this.sourcemapLocations.add(char);
376};
377
378MagicString.prototype.append = function append (content) {
379 if (typeof content !== 'string') { throw new TypeError('outro content must be a string'); }
380
381 this.outro += content;
382 return this;
383};
384
385MagicString.prototype.appendLeft = function appendLeft (index, content) {
386 if (typeof content !== 'string') { throw new TypeError('inserted content must be a string'); }
387
388 this._split(index);
389
390 var chunk = this.byEnd[index];
391
392 if (chunk) {
393 chunk.appendLeft(content);
394 } else {
395 this.intro += content;
396 }
397 return this;
398};
399
400MagicString.prototype.appendRight = function appendRight (index, content) {
401 if (typeof content !== 'string') { throw new TypeError('inserted content must be a string'); }
402
403 this._split(index);
404
405 var chunk = this.byStart[index];
406
407 if (chunk) {
408 chunk.appendRight(content);
409 } else {
410 this.outro += content;
411 }
412 return this;
413};
414
415MagicString.prototype.clone = function clone () {
416 var cloned = new MagicString(this.original, { filename: this.filename });
417
418 var originalChunk = this.firstChunk;
419 var clonedChunk = (cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone());
420
421 while (originalChunk) {
422 cloned.byStart[clonedChunk.start] = clonedChunk;
423 cloned.byEnd[clonedChunk.end] = clonedChunk;
424
425 var nextOriginalChunk = originalChunk.next;
426 var nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone();
427
428 if (nextClonedChunk) {
429 clonedChunk.next = nextClonedChunk;
430 nextClonedChunk.previous = clonedChunk;
431
432 clonedChunk = nextClonedChunk;
433 }
434
435 originalChunk = nextOriginalChunk;
436 }
437
438 cloned.lastChunk = clonedChunk;
439
440 if (this.indentExclusionRanges) {
441 cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
442 }
443
444 cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
445
446 cloned.intro = this.intro;
447 cloned.outro = this.outro;
448
449 return cloned;
450};
451
452MagicString.prototype.generateDecodedMap = function generateDecodedMap (options) {
453 var this$1$1 = this;
454
455 options = options || {};
456
457 var sourceIndex = 0;
458 var names = Object.keys(this.storedNames);
459 var mappings = new Mappings(options.hires);
460
461 var locate = getLocator(this.original);
462
463 if (this.intro) {
464 mappings.advance(this.intro);
465 }
466
467 this.firstChunk.eachNext(function (chunk) {
468 var loc = locate(chunk.start);
469
470 if (chunk.intro.length) { mappings.advance(chunk.intro); }
471
472 if (chunk.edited) {
473 mappings.addEdit(
474 sourceIndex,
475 chunk.content,
476 loc,
477 chunk.storeName ? names.indexOf(chunk.original) : -1
478 );
479 } else {
480 mappings.addUneditedChunk(sourceIndex, chunk, this$1$1.original, loc, this$1$1.sourcemapLocations);
481 }
482
483 if (chunk.outro.length) { mappings.advance(chunk.outro); }
484 });
485
486 return {
487 file: options.file ? options.file.split(/[/\\]/).pop() : null,
488 sources: [options.source ? getRelativePath(options.file || '', options.source) : null],
489 sourcesContent: options.includeContent ? [this.original] : [null],
490 names: names,
491 mappings: mappings.raw,
492 };
493};
494
495MagicString.prototype.generateMap = function generateMap (options) {
496 return new SourceMap(this.generateDecodedMap(options));
497};
498
499MagicString.prototype.getIndentString = function getIndentString () {
500 return this.indentStr === null ? '\t' : this.indentStr;
501};
502
503MagicString.prototype.indent = function indent (indentStr, options) {
504 var pattern = /^[^\r\n]/gm;
505
506 if (isObject(indentStr)) {
507 options = indentStr;
508 indentStr = undefined;
509 }
510
511 indentStr = indentStr !== undefined ? indentStr : this.indentStr || '\t';
512
513 if (indentStr === '') { return this; } // noop
514
515 options = options || {};
516
517 // Process exclusion ranges
518 var isExcluded = {};
519
520 if (options.exclude) {
521 var exclusions =
522 typeof options.exclude[0] === 'number' ? [options.exclude] : options.exclude;
523 exclusions.forEach(function (exclusion) {
524 for (var i = exclusion[0]; i < exclusion[1]; i += 1) {
525 isExcluded[i] = true;
526 }
527 });
528 }
529
530 var shouldIndentNextCharacter = options.indentStart !== false;
531 var replacer = function (match) {
532 if (shouldIndentNextCharacter) { return ("" + indentStr + match); }
533 shouldIndentNextCharacter = true;
534 return match;
535 };
536
537 this.intro = this.intro.replace(pattern, replacer);
538
539 var charIndex = 0;
540 var chunk = this.firstChunk;
541
542 while (chunk) {
543 var end = chunk.end;
544
545 if (chunk.edited) {
546 if (!isExcluded[charIndex]) {
547 chunk.content = chunk.content.replace(pattern, replacer);
548
549 if (chunk.content.length) {
550 shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === '\n';
551 }
552 }
553 } else {
554 charIndex = chunk.start;
555
556 while (charIndex < end) {
557 if (!isExcluded[charIndex]) {
558 var char = this.original[charIndex];
559
560 if (char === '\n') {
561 shouldIndentNextCharacter = true;
562 } else if (char !== '\r' && shouldIndentNextCharacter) {
563 shouldIndentNextCharacter = false;
564
565 if (charIndex === chunk.start) {
566 chunk.prependRight(indentStr);
567 } else {
568 this._splitChunk(chunk, charIndex);
569 chunk = chunk.next;
570 chunk.prependRight(indentStr);
571 }
572 }
573 }
574
575 charIndex += 1;
576 }
577 }
578
579 charIndex = chunk.end;
580 chunk = chunk.next;
581 }
582
583 this.outro = this.outro.replace(pattern, replacer);
584
585 return this;
586};
587
588MagicString.prototype.insert = function insert () {
589 throw new Error(
590 'magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)'
591 );
592};
593
594MagicString.prototype.insertLeft = function insertLeft (index, content) {
595 if (!warned.insertLeft) {
596 console.warn(
597 'magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead'
598 ); // eslint-disable-line no-console
599 warned.insertLeft = true;
600 }
601
602 return this.appendLeft(index, content);
603};
604
605MagicString.prototype.insertRight = function insertRight (index, content) {
606 if (!warned.insertRight) {
607 console.warn(
608 'magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead'
609 ); // eslint-disable-line no-console
610 warned.insertRight = true;
611 }
612
613 return this.prependRight(index, content);
614};
615
616MagicString.prototype.move = function move (start, end, index) {
617 if (index >= start && index <= end) { throw new Error('Cannot move a selection inside itself'); }
618
619 this._split(start);
620 this._split(end);
621 this._split(index);
622
623 var first = this.byStart[start];
624 var last = this.byEnd[end];
625
626 var oldLeft = first.previous;
627 var oldRight = last.next;
628
629 var newRight = this.byStart[index];
630 if (!newRight && last === this.lastChunk) { return this; }
631 var newLeft = newRight ? newRight.previous : this.lastChunk;
632
633 if (oldLeft) { oldLeft.next = oldRight; }
634 if (oldRight) { oldRight.previous = oldLeft; }
635
636 if (newLeft) { newLeft.next = first; }
637 if (newRight) { newRight.previous = last; }
638
639 if (!first.previous) { this.firstChunk = last.next; }
640 if (!last.next) {
641 this.lastChunk = first.previous;
642 this.lastChunk.next = null;
643 }
644
645 first.previous = newLeft;
646 last.next = newRight || null;
647
648 if (!newLeft) { this.firstChunk = first; }
649 if (!newRight) { this.lastChunk = last; }
650 return this;
651};
652
653MagicString.prototype.overwrite = function overwrite (start, end, content, options) {
654 if (typeof content !== 'string') { throw new TypeError('replacement content must be a string'); }
655
656 while (start < 0) { start += this.original.length; }
657 while (end < 0) { end += this.original.length; }
658
659 if (end > this.original.length) { throw new Error('end is out of bounds'); }
660 if (start === end)
661 { throw new Error(
662 'Cannot overwrite a zero-length range – use appendLeft or prependRight instead'
663 ); }
664
665 this._split(start);
666 this._split(end);
667
668 if (options === true) {
669 if (!warned.storeName) {
670 console.warn(
671 'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string'
672 ); // eslint-disable-line no-console
673 warned.storeName = true;
674 }
675
676 options = { storeName: true };
677 }
678 var storeName = options !== undefined ? options.storeName : false;
679 var contentOnly = options !== undefined ? options.contentOnly : false;
680
681 if (storeName) {
682 var original = this.original.slice(start, end);
683 Object.defineProperty(this.storedNames, original, { writable: true, value: true, enumerable: true });
684 }
685
686 var first = this.byStart[start];
687 var last = this.byEnd[end];
688
689 if (first) {
690 var chunk = first;
691 while (chunk !== last) {
692 if (chunk.next !== this.byStart[chunk.end]) {
693 throw new Error('Cannot overwrite across a split point');
694 }
695 chunk = chunk.next;
696 chunk.edit('', false);
697 }
698
699 first.edit(content, storeName, contentOnly);
700 } else {
701 // must be inserting at the end
702 var newChunk = new Chunk(start, end, '').edit(content, storeName);
703
704 // TODO last chunk in the array may not be the last chunk, if it's moved...
705 last.next = newChunk;
706 newChunk.previous = last;
707 }
708 return this;
709};
710
711MagicString.prototype.prepend = function prepend (content) {
712 if (typeof content !== 'string') { throw new TypeError('outro content must be a string'); }
713
714 this.intro = content + this.intro;
715 return this;
716};
717
718MagicString.prototype.prependLeft = function prependLeft (index, content) {
719 if (typeof content !== 'string') { throw new TypeError('inserted content must be a string'); }
720
721 this._split(index);
722
723 var chunk = this.byEnd[index];
724
725 if (chunk) {
726 chunk.prependLeft(content);
727 } else {
728 this.intro = content + this.intro;
729 }
730 return this;
731};
732
733MagicString.prototype.prependRight = function prependRight (index, content) {
734 if (typeof content !== 'string') { throw new TypeError('inserted content must be a string'); }
735
736 this._split(index);
737
738 var chunk = this.byStart[index];
739
740 if (chunk) {
741 chunk.prependRight(content);
742 } else {
743 this.outro = content + this.outro;
744 }
745 return this;
746};
747
748MagicString.prototype.remove = function remove (start, end) {
749 while (start < 0) { start += this.original.length; }
750 while (end < 0) { end += this.original.length; }
751
752 if (start === end) { return this; }
753
754 if (start < 0 || end > this.original.length) { throw new Error('Character is out of bounds'); }
755 if (start > end) { throw new Error('end must be greater than start'); }
756
757 this._split(start);
758 this._split(end);
759
760 var chunk = this.byStart[start];
761
762 while (chunk) {
763 chunk.intro = '';
764 chunk.outro = '';
765 chunk.edit('');
766
767 chunk = end > chunk.end ? this.byStart[chunk.end] : null;
768 }
769 return this;
770};
771
772MagicString.prototype.lastChar = function lastChar () {
773 if (this.outro.length) { return this.outro[this.outro.length - 1]; }
774 var chunk = this.lastChunk;
775 do {
776 if (chunk.outro.length) { return chunk.outro[chunk.outro.length - 1]; }
777 if (chunk.content.length) { return chunk.content[chunk.content.length - 1]; }
778 if (chunk.intro.length) { return chunk.intro[chunk.intro.length - 1]; }
779 } while ((chunk = chunk.previous));
780 if (this.intro.length) { return this.intro[this.intro.length - 1]; }
781 return '';
782};
783
784MagicString.prototype.lastLine = function lastLine () {
785 var lineIndex = this.outro.lastIndexOf(n);
786 if (lineIndex !== -1) { return this.outro.substr(lineIndex + 1); }
787 var lineStr = this.outro;
788 var chunk = this.lastChunk;
789 do {
790 if (chunk.outro.length > 0) {
791 lineIndex = chunk.outro.lastIndexOf(n);
792 if (lineIndex !== -1) { return chunk.outro.substr(lineIndex + 1) + lineStr; }
793 lineStr = chunk.outro + lineStr;
794 }
795
796 if (chunk.content.length > 0) {
797 lineIndex = chunk.content.lastIndexOf(n);
798 if (lineIndex !== -1) { return chunk.content.substr(lineIndex + 1) + lineStr; }
799 lineStr = chunk.content + lineStr;
800 }
801
802 if (chunk.intro.length > 0) {
803 lineIndex = chunk.intro.lastIndexOf(n);
804 if (lineIndex !== -1) { return chunk.intro.substr(lineIndex + 1) + lineStr; }
805 lineStr = chunk.intro + lineStr;
806 }
807 } while ((chunk = chunk.previous));
808 lineIndex = this.intro.lastIndexOf(n);
809 if (lineIndex !== -1) { return this.intro.substr(lineIndex + 1) + lineStr; }
810 return this.intro + lineStr;
811};
812
813MagicString.prototype.slice = function slice (start, end) {
814 if ( start === void 0 ) start = 0;
815 if ( end === void 0 ) end = this.original.length;
816
817 while (start < 0) { start += this.original.length; }
818 while (end < 0) { end += this.original.length; }
819
820 var result = '';
821
822 // find start chunk
823 var chunk = this.firstChunk;
824 while (chunk && (chunk.start > start || chunk.end <= start)) {
825 // found end chunk before start
826 if (chunk.start < end && chunk.end >= end) {
827 return result;
828 }
829
830 chunk = chunk.next;
831 }
832
833 if (chunk && chunk.edited && chunk.start !== start)
834 { throw new Error(("Cannot use replaced character " + start + " as slice start anchor.")); }
835
836 var startChunk = chunk;
837 while (chunk) {
838 if (chunk.intro && (startChunk !== chunk || chunk.start === start)) {
839 result += chunk.intro;
840 }
841
842 var containsEnd = chunk.start < end && chunk.end >= end;
843 if (containsEnd && chunk.edited && chunk.end !== end)
844 { throw new Error(("Cannot use replaced character " + end + " as slice end anchor.")); }
845
846 var sliceStart = startChunk === chunk ? start - chunk.start : 0;
847 var sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
848
849 result += chunk.content.slice(sliceStart, sliceEnd);
850
851 if (chunk.outro && (!containsEnd || chunk.end === end)) {
852 result += chunk.outro;
853 }
854
855 if (containsEnd) {
856 break;
857 }
858
859 chunk = chunk.next;
860 }
861
862 return result;
863};
864
865// TODO deprecate this? not really very useful
866MagicString.prototype.snip = function snip (start, end) {
867 var clone = this.clone();
868 clone.remove(0, start);
869 clone.remove(end, clone.original.length);
870
871 return clone;
872};
873
874MagicString.prototype._split = function _split (index) {
875 if (this.byStart[index] || this.byEnd[index]) { return; }
876
877 var chunk = this.lastSearchedChunk;
878 var searchForward = index > chunk.end;
879
880 while (chunk) {
881 if (chunk.contains(index)) { return this._splitChunk(chunk, index); }
882
883 chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start];
884 }
885};
886
887MagicString.prototype._splitChunk = function _splitChunk (chunk, index) {
888 if (chunk.edited && chunk.content.length) {
889 // zero-length edited chunks are a special case (overlapping replacements)
890 var loc = getLocator(this.original)(index);
891 throw new Error(
892 ("Cannot split a chunk that has already been edited (" + (loc.line) + ":" + (loc.column) + " – \"" + (chunk.original) + "\")")
893 );
894 }
895
896 var newChunk = chunk.split(index);
897
898 this.byEnd[index] = chunk;
899 this.byStart[index] = newChunk;
900 this.byEnd[newChunk.end] = newChunk;
901
902 if (chunk === this.lastChunk) { this.lastChunk = newChunk; }
903
904 this.lastSearchedChunk = chunk;
905 return true;
906};
907
908MagicString.prototype.toString = function toString () {
909 var str = this.intro;
910
911 var chunk = this.firstChunk;
912 while (chunk) {
913 str += chunk.toString();
914 chunk = chunk.next;
915 }
916
917 return str + this.outro;
918};
919
920MagicString.prototype.isEmpty = function isEmpty () {
921 var chunk = this.firstChunk;
922 do {
923 if (
924 (chunk.intro.length && chunk.intro.trim()) ||
925 (chunk.content.length && chunk.content.trim()) ||
926 (chunk.outro.length && chunk.outro.trim())
927 )
928 { return false; }
929 } while ((chunk = chunk.next));
930 return true;
931};
932
933MagicString.prototype.length = function length () {
934 var chunk = this.firstChunk;
935 var length = 0;
936 do {
937 length += chunk.intro.length + chunk.content.length + chunk.outro.length;
938 } while ((chunk = chunk.next));
939 return length;
940};
941
942MagicString.prototype.trimLines = function trimLines () {
943 return this.trim('[\\r\\n]');
944};
945
946MagicString.prototype.trim = function trim (charType) {
947 return this.trimStart(charType).trimEnd(charType);
948};
949
950MagicString.prototype.trimEndAborted = function trimEndAborted (charType) {
951 var rx = new RegExp((charType || '\\s') + '+$');
952
953 this.outro = this.outro.replace(rx, '');
954 if (this.outro.length) { return true; }
955
956 var chunk = this.lastChunk;
957
958 do {
959 var end = chunk.end;
960 var aborted = chunk.trimEnd(rx);
961
962 // if chunk was trimmed, we have a new lastChunk
963 if (chunk.end !== end) {
964 if (this.lastChunk === chunk) {
965 this.lastChunk = chunk.next;
966 }
967
968 this.byEnd[chunk.end] = chunk;
969 this.byStart[chunk.next.start] = chunk.next;
970 this.byEnd[chunk.next.end] = chunk.next;
971 }
972
973 if (aborted) { return true; }
974 chunk = chunk.previous;
975 } while (chunk);
976
977 return false;
978};
979
980MagicString.prototype.trimEnd = function trimEnd (charType) {
981 this.trimEndAborted(charType);
982 return this;
983};
984MagicString.prototype.trimStartAborted = function trimStartAborted (charType) {
985 var rx = new RegExp('^' + (charType || '\\s') + '+');
986
987 this.intro = this.intro.replace(rx, '');
988 if (this.intro.length) { return true; }
989
990 var chunk = this.firstChunk;
991
992 do {
993 var end = chunk.end;
994 var aborted = chunk.trimStart(rx);
995
996 if (chunk.end !== end) {
997 // special case...
998 if (chunk === this.lastChunk) { this.lastChunk = chunk.next; }
999
1000 this.byEnd[chunk.end] = chunk;
1001 this.byStart[chunk.next.start] = chunk.next;
1002 this.byEnd[chunk.next.end] = chunk.next;
1003 }
1004
1005 if (aborted) { return true; }
1006 chunk = chunk.next;
1007 } while (chunk);
1008
1009 return false;
1010};
1011
1012MagicString.prototype.trimStart = function trimStart (charType) {
1013 this.trimStartAborted(charType);
1014 return this;
1015};
1016
1017var hasOwnProp = Object.prototype.hasOwnProperty;
1018
1019var Bundle = function Bundle(options) {
1020 if ( options === void 0 ) options = {};
1021
1022 this.intro = options.intro || '';
1023 this.separator = options.separator !== undefined ? options.separator : '\n';
1024 this.sources = [];
1025 this.uniqueSources = [];
1026 this.uniqueSourceIndexByFilename = {};
1027};
1028
1029Bundle.prototype.addSource = function addSource (source) {
1030 if (source instanceof MagicString) {
1031 return this.addSource({
1032 content: source,
1033 filename: source.filename,
1034 separator: this.separator,
1035 });
1036 }
1037
1038 if (!isObject(source) || !source.content) {
1039 throw new Error(
1040 'bundle.addSource() takes an object with a `content` property, which should be an instance of MagicString, and an optional `filename`'
1041 );
1042 }
1043
1044 ['filename', 'indentExclusionRanges', 'separator'].forEach(function (option) {
1045 if (!hasOwnProp.call(source, option)) { source[option] = source.content[option]; }
1046 });
1047
1048 if (source.separator === undefined) {
1049 // TODO there's a bunch of this sort of thing, needs cleaning up
1050 source.separator = this.separator;
1051 }
1052
1053 if (source.filename) {
1054 if (!hasOwnProp.call(this.uniqueSourceIndexByFilename, source.filename)) {
1055 this.uniqueSourceIndexByFilename[source.filename] = this.uniqueSources.length;
1056 this.uniqueSources.push({ filename: source.filename, content: source.content.original });
1057 } else {
1058 var uniqueSource = this.uniqueSources[this.uniqueSourceIndexByFilename[source.filename]];
1059 if (source.content.original !== uniqueSource.content) {
1060 throw new Error(("Illegal source: same filename (" + (source.filename) + "), different contents"));
1061 }
1062 }
1063 }
1064
1065 this.sources.push(source);
1066 return this;
1067};
1068
1069Bundle.prototype.append = function append (str, options) {
1070 this.addSource({
1071 content: new MagicString(str),
1072 separator: (options && options.separator) || '',
1073 });
1074
1075 return this;
1076};
1077
1078Bundle.prototype.clone = function clone () {
1079 var bundle = new Bundle({
1080 intro: this.intro,
1081 separator: this.separator,
1082 });
1083
1084 this.sources.forEach(function (source) {
1085 bundle.addSource({
1086 filename: source.filename,
1087 content: source.content.clone(),
1088 separator: source.separator,
1089 });
1090 });
1091
1092 return bundle;
1093};
1094
1095Bundle.prototype.generateDecodedMap = function generateDecodedMap (options) {
1096 var this$1$1 = this;
1097 if ( options === void 0 ) options = {};
1098
1099 var names = [];
1100 this.sources.forEach(function (source) {
1101 Object.keys(source.content.storedNames).forEach(function (name) {
1102 if (!~names.indexOf(name)) { names.push(name); }
1103 });
1104 });
1105
1106 var mappings = new Mappings(options.hires);
1107
1108 if (this.intro) {
1109 mappings.advance(this.intro);
1110 }
1111
1112 this.sources.forEach(function (source, i) {
1113 if (i > 0) {
1114 mappings.advance(this$1$1.separator);
1115 }
1116
1117 var sourceIndex = source.filename ? this$1$1.uniqueSourceIndexByFilename[source.filename] : -1;
1118 var magicString = source.content;
1119 var locate = getLocator(magicString.original);
1120
1121 if (magicString.intro) {
1122 mappings.advance(magicString.intro);
1123 }
1124
1125 magicString.firstChunk.eachNext(function (chunk) {
1126 var loc = locate(chunk.start);
1127
1128 if (chunk.intro.length) { mappings.advance(chunk.intro); }
1129
1130 if (source.filename) {
1131 if (chunk.edited) {
1132 mappings.addEdit(
1133 sourceIndex,
1134 chunk.content,
1135 loc,
1136 chunk.storeName ? names.indexOf(chunk.original) : -1
1137 );
1138 } else {
1139 mappings.addUneditedChunk(
1140 sourceIndex,
1141 chunk,
1142 magicString.original,
1143 loc,
1144 magicString.sourcemapLocations
1145 );
1146 }
1147 } else {
1148 mappings.advance(chunk.content);
1149 }
1150
1151 if (chunk.outro.length) { mappings.advance(chunk.outro); }
1152 });
1153
1154 if (magicString.outro) {
1155 mappings.advance(magicString.outro);
1156 }
1157 });
1158
1159 return {
1160 file: options.file ? options.file.split(/[/\\]/).pop() : null,
1161 sources: this.uniqueSources.map(function (source) {
1162 return options.file ? getRelativePath(options.file, source.filename) : source.filename;
1163 }),
1164 sourcesContent: this.uniqueSources.map(function (source) {
1165 return options.includeContent ? source.content : null;
1166 }),
1167 names: names,
1168 mappings: mappings.raw,
1169 };
1170};
1171
1172Bundle.prototype.generateMap = function generateMap (options) {
1173 return new SourceMap(this.generateDecodedMap(options));
1174};
1175
1176Bundle.prototype.getIndentString = function getIndentString () {
1177 var indentStringCounts = {};
1178
1179 this.sources.forEach(function (source) {
1180 var indentStr = source.content.indentStr;
1181
1182 if (indentStr === null) { return; }
1183
1184 if (!indentStringCounts[indentStr]) { indentStringCounts[indentStr] = 0; }
1185 indentStringCounts[indentStr] += 1;
1186 });
1187
1188 return (
1189 Object.keys(indentStringCounts).sort(function (a, b) {
1190 return indentStringCounts[a] - indentStringCounts[b];
1191 })[0] || '\t'
1192 );
1193};
1194
1195Bundle.prototype.indent = function indent (indentStr) {
1196 var this$1$1 = this;
1197
1198 if (!arguments.length) {
1199 indentStr = this.getIndentString();
1200 }
1201
1202 if (indentStr === '') { return this; } // noop
1203
1204 var trailingNewline = !this.intro || this.intro.slice(-1) === '\n';
1205
1206 this.sources.forEach(function (source, i) {
1207 var separator = source.separator !== undefined ? source.separator : this$1$1.separator;
1208 var indentStart = trailingNewline || (i > 0 && /\r?\n$/.test(separator));
1209
1210 source.content.indent(indentStr, {
1211 exclude: source.indentExclusionRanges,
1212 indentStart: indentStart, //: trailingNewline || /\r?\n$/.test( separator ) //true///\r?\n/.test( separator )
1213 });
1214
1215 trailingNewline = source.content.lastChar() === '\n';
1216 });
1217
1218 if (this.intro) {
1219 this.intro =
1220 indentStr +
1221 this.intro.replace(/^[^\n]/gm, function (match, index) {
1222 return index > 0 ? indentStr + match : match;
1223 });
1224 }
1225
1226 return this;
1227};
1228
1229Bundle.prototype.prepend = function prepend (str) {
1230 this.intro = str + this.intro;
1231 return this;
1232};
1233
1234Bundle.prototype.toString = function toString () {
1235 var this$1$1 = this;
1236
1237 var body = this.sources
1238 .map(function (source, i) {
1239 var separator = source.separator !== undefined ? source.separator : this$1$1.separator;
1240 var str = (i > 0 ? separator : '') + source.content.toString();
1241
1242 return str;
1243 })
1244 .join('');
1245
1246 return this.intro + body;
1247};
1248
1249Bundle.prototype.isEmpty = function isEmpty () {
1250 if (this.intro.length && this.intro.trim()) { return false; }
1251 if (this.sources.some(function (source) { return !source.content.isEmpty(); })) { return false; }
1252 return true;
1253};
1254
1255Bundle.prototype.length = function length () {
1256 return this.sources.reduce(
1257 function (length, source) { return length + source.content.length(); },
1258 this.intro.length
1259 );
1260};
1261
1262Bundle.prototype.trimLines = function trimLines () {
1263 return this.trim('[\\r\\n]');
1264};
1265
1266Bundle.prototype.trim = function trim (charType) {
1267 return this.trimStart(charType).trimEnd(charType);
1268};
1269
1270Bundle.prototype.trimStart = function trimStart (charType) {
1271 var rx = new RegExp('^' + (charType || '\\s') + '+');
1272 this.intro = this.intro.replace(rx, '');
1273
1274 if (!this.intro) {
1275 var source;
1276 var i = 0;
1277
1278 do {
1279 source = this.sources[i++];
1280 if (!source) {
1281 break;
1282 }
1283 } while (!source.content.trimStartAborted(charType));
1284 }
1285
1286 return this;
1287};
1288
1289Bundle.prototype.trimEnd = function trimEnd (charType) {
1290 var rx = new RegExp((charType || '\\s') + '+$');
1291
1292 var source;
1293 var i = this.sources.length - 1;
1294
1295 do {
1296 source = this.sources[i--];
1297 if (!source) {
1298 this.intro = this.intro.replace(rx, '');
1299 break;
1300 }
1301 } while (!source.content.trimEndAborted(charType));
1302
1303 return this;
1304};
1305
1306MagicString.Bundle = Bundle;
1307MagicString.SourceMap = SourceMap;
1308MagicString.default = MagicString; // work around TypeScript bug https://github.com/Rich-Harris/magic-string/pull/121
1309
1310module.exports = MagicString;
1311//# sourceMappingURL=magic-string.cjs.js.map
Note: See TracBrowser for help on using the repository browser.