-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLorem-Chatum-v1.jsx
2102 lines (1865 loc) · 77.7 KB
/
Lorem-Chatum-v1.jsx
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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Lorem Chatum v1.0 for Adobe InDesign 2022 and older
// Copyright (c) 2023 by Adam Twardoch
// https://github.com/twardoch/lorem-chatum-for-indesign
// Licensed under the GNU General Public License v3.0
// Uses the [standalone JSON](https://github.com/indiscripts/extendscript/tree/master/JSON) code,
// Copyright (c) 2017-2022[Marc Autret](https://indiscripts.com/), licensed under the MIT license
// Uses the [Restix](https://github.com/grefel/restix/blob/master/restix.jsx) code
// by [Gregor Fellenz](http://www.publishingx.de), licensed under the GNU General Public License v3.0
// Create a new OpenAI API secret key at https://platform.openai.com/account/api-keys
// and paste it below
const OPENAI_API_KEY = 'sk-';
//// LIBS
/// https://raw.githubusercontent.com/indiscripts/extendscript/master/JSON/json.jsx
/*******************************************************************************
Name: JSON
Desc: JSON module for ExtendScript-based apps.
Path: json.jsx
Require: Nothing (IdExtenso is not needed here.)
Encoding: ÛȚF8
Kind: Standalone Version.
API: =lave() eval()
DOM-access: ---
Todo: ---
Created: 171005 (YYMMDD)
Modified: 171005 (YYMMDD)
*******************************************************************************/
//==========================================================================
// IMPORTANT NOTES
//==========================================================================
/*
This implementation is based on IdExtenso's JSON module, but it has been
packaged and slightly rewritten in a way that makes it independent. So
you can include [json.jsx] in projects that do not involve InDesign.
It creates a global JSON object (in fact, function) that provides two
methods:
- lave(any) : Take anything and return its stringified representation,
more ExtendScript-friendly than JSON.stringify() would be.
Note: this is an `automatic` method, meaning that
`JSON.lave(x)` can be shortened `JSON(x)`. Some options
are available, as detailed in the code.
- eval(str) : Basically equivalent to $.global.eval().
WARNING. - This module doesn't pretend to provide the original features
of `JSON.stringify()` and is entirely independent from D. Crockford's
implementation. It has been noticed that since ExtendScript is not
fully compliant with JS (=ECMA-262), the regular json code had various
issues in ExtendScript environments. The present code is an attempt to
fix this. It is *not supposed* to work in usual JavaScript contexts.
*/
if (!$.global.hasOwnProperty('JSON')) {
//==========================================================================
// Please, keep this shortcut to the localize function.
//==========================================================================
$.global['__'] = $.global.localize;
//==========================================================================
// OBJECT (temporary `setup` method, removed once JSON is installed.)
//==========================================================================
Object.prototype.setup = function (/*obj*/ o, k) {
for (k in o) o.hasOwnProperty(k) && (this[k] = o[k]);
return this;
};
//==========================================================================
// NUMBER Proto.
//==========================================================================
Number.prototype.toSource =
function toSource() //----------------------------------
// [TODO] May be improved to get shorter strings ("1e5" etc)
// => str
{
return String(this);
};
Number.prototype.toHexa = function toHexa(
/*str='0x'*/ prefix,
/*uint=0*/ zeroPad,
s
) //----------------------------------
// Return the "0xHHHH" representation of this number. (Uppercase hexa digits.)
// [ADD170423] `prefix` If provided (string), reset the prefix (default is "0x").
// [ADD170423] `zeroPad` If > 0, minimum length of the hexa representation.
// ---
// E.g toHexa(123456) => "0x1E240"
// E.g toHexa(123456, "U+") => "U+1E240"
// E.g toHexa(123, "") => "7B"
// E.g toHexa(123, "", 4) => "007B"
{
if (isNaN(this)) return 'NaN';
'string' == typeof prefix || (prefix = '0x');
('number' == typeof zeroPad && 0 < (zeroPad >>>= 0)) || (zeroPad = 0);
s = this.toString(16).toUpperCase();
return (
prefix +
(zeroPad && 0 < (zeroPad -= s.length)
? Array(1 + zeroPad).join('0')
: '') +
s
);
};
Number.prototype.toAdbe =
function toAdbe() //----------------------------------
// Return the Adobe's 4-char string this number encodes.
// [REM] Only makes sense if `this` is U32.
// E.g 0x414F5069 => "AOPi" ; 0x74787466 => "txtf"
{
return isNaN(this)
? 'NaN'
: String.fromCharCode(
0xff & (this >>> 24),
0xff & (this >>> 16),
0xff & (this >>> 8),
0xff & (this >>> 0)
);
};
Number.prototype.isAdbe = function isAdbe(
s
) //----------------------------------
// Whether this Number looks like an Adobe 4-char tag.
// [REM] Only makes sense if `this` is U32.
// E.g 1114394470 => 1 ; 12345 => 0
// ie 0x426C4F66 [OK] 0x3039 [KO]
{
return +(
8 == (s = this.toString(16).toUpperCase()).length && RegExp.ADBE.test(s)
);
};
//==========================================================================
// PATTERNS
//==========================================================================
// <BK> U+005C `\` Bakslash
// -------------------------------------------------------------------------
String.BK = '\u005C';
RegExp.BK = /\u005C/g; // /BK/
String._BK = String.BK + String.BK; // Regular esc `\\`
RegExp._BK = /\u005C(?:\u005C|u005[Cc]|[xX]05[Cc])/g; // /any-esc/
RegExp.BKBK = /\u005C\u005C/g; // /BKBK/ only
// <SL> U+002F `/` Slash (Solidus)
// -------------------------------------------------------------------------
String.SL = '\u002F';
RegExp.SL = /\u002F/g; // /SL/
String._SL = String.BK + String.SL; // Regular esc `\/`
RegExp._SL = /\u005C(?:\u002F|u002[Ff]|[xX]02[Ff])/g; // /any-esc/
RegExp.BKSL = /\u005C\u002F/g; // /BKSL/ only
RegExp.SLs = /\u002F+/g; // /SL+/
// <DQ> U+0022 `"` Double Quote
// <SQ> U+0027 `'` Single Quote
// -------------------------------------------------------------------------
String.DQ = '\u0022';
RegExp.DQ = /\u0022/g; // /DQ/
String._DQ = String.BKDQ = String.BK + String.DQ; // Regular esc `\"`
RegExp.BKDQ = /\u005C\u0022/g; // /BKDQ/ only
RegExp._DQ = /\u005C(?:\u0022|u0022|[xX]22)/g; // /any-esc/
// ---
String.SQ = '\u0027';
RegExp.SQ = /\u0027/g; // /SQ/
String._SQ = String.BKSQ = String.BK + String.SQ; // Regular esc `\"`
RegExp.BKSQ = /\u005C\u0027/g; // /BKSQ/ only
RegExp._SQ = /\u005C(?:\u0027|u0027|[xX]27)/g; // /any-esc/
// <CR> U+000D \r Carriage Return
// <LF> U+000A \n Line Feed
// <LS> U+2028 Line Separator
// <PS> U+2029 Paragraph Separator
// ---
// A LineTerminator character cannot appear in a string literal, even if
// preceded by a backslash. The correct way to cause a line terminator
// character to be part of the string value of a string literal is to use
// an escape sequence such as \n or \u000A.
// -------------------------------------------------------------------------
String.CR = '\u000D';
RegExp.CR = /\u000D/g; // /CR/
String._CR = String.BK + 'r'; // Regular esc `\r`
RegExp._CR = /\u005C(?:r|u000[Dd]|[xX]0[Dd])/g; // /any-esc/
// ---
String.LF = '\u000A';
RegExp.LF = /\u000A/g; // /LF/
String._LF = String.BK + 'n'; // Regular esc `\n`
RegExp._LF = /\u005C(?:n|u000[Aa]|[xX]0[Aa])/g; // /any-esc/
// ---
String.CRLF = String.CR + String.LF; // <CRLF>=<CR>+<LF>
RegExp.CRLF = RegExp(String.CRLF, 'g'); // /CRLF/
String._CRLF = String._CR + String._LF; // Regular esc `\r\n`
RegExp._CRLF =
/\u005C(?:r|u000[Dd]|[xX]0[Dd])\u005C(?:n|u000[Aa]|[xX]0[Aa])/g; // /any-esc/
// ---
String.LS = '\u2028';
RegExp.LS = /\u2028/g; // /LS/
// ---
String.PS = '\u2029';
RegExp.PS = /\u2029/g; // /PS/
// ---
RegExp.LINE = /\u000D|\u000A|\u000D\u000A/g; // /CR|LF|CRLF/
RegExp.LINEs = /[\u000D\u000A]+/g; // /CR|LF/+
RegExp.JSNLs = /[\u000D\u000A\u2028\u2029]+/g; // /CR|LF|LS|PS/+ (JS newline)
RegExp.TBNL = /[\u0009\u000D\u000A]/g; // /TB|CR|LF/
// <TB> U+0009 `\t` Horizontal Tab.
// <VT> U+000B `\v` Vertical Tab.
// <BS> U+0008 `\b` Backspace.
// <FF> U+000C `\f` Form Feed.
// -------------------------------------------------------------------------
String.TB = '\u0009';
RegExp.TB = /\u0009/g; // /TB/
String._TB = String.BK + 't'; // Regular esc `\t`
RegExp._TB = /\u005C(?:t|u0009|[xX]09)/g; // /any-esc/
// ---
String.VT = '\u000B';
RegExp.VT = /\u000B/g; // /VT/
String._VT = String.BK + 'v'; // Regular esc `\v`
RegExp._VT = /\u005C(?:v|u000[Bb]|[xX]0[Bb])/g; // /any-esc/
// ---
String.BS = '\u0008';
RegExp.BS = /\u0008/g; // /BS/
String._BS = String.BK + 'b'; // Regular esc `\b`
RegExp._BS = /\u005C(?:b|u0008|[xX]08)/g; // /any-esc/
// ---
String.FF = '\u000C';
RegExp.FF = /\u000C/g; // /FF/
String._FF = String.BK + 'f'; // Regular esc `\f`
RegExp._FF = /\u005C(?:f|u000[Cc]|[xX]0[Cc])/g; // /any-esc/
// Other ASCII controls.
// -------------------------------------------------------------------------
String.X0 = '\u0000'; // U+0000 Nul
RegExp.X0 = /\u0000/g; // /X0/
RegExp._X0 = /\u005C(?:u0000|[xX]00|(?:0(?!\d)))/g; // /any-esc/
// ---
String.X1 = '\u0001'; // U+0001 Start of Heading
RegExp.X1 = /\u0001/g; // /X1/
RegExp._X1 = /\u005C(?:u0001|[xX]01)/g; // /any-esc/
// ---
String.X2 = '\u0002'; // U+0002 Start of Text
RegExp.X2 = /\u0002/g; // /X2/
// ---
String.X3 = '\u0003'; // U+0003 End of Text [used in ID]
RegExp.X3 = /\u0003/g; // /X3/
// ---
String.X4 = '\u0004'; // U+0004 End of Transm
RegExp.X4 = /\u0004/g; // /X4/
// ---
String.X5 = '\u0005'; // U+0005 Enquiry
RegExp.X5 = /\u0005/g; // /X5/
// ---
String.X6 = '\u0006'; // U+0006 Acknowledge
RegExp.X6 = /\u0006/g; // /X6/
// ---
String.X7 = '\u0007'; // U+0007 Alert [used in ID]
RegExp.X7 = /\u0007/g; // /X7/
// ---
String.XE = '\u000E'; // U+000E Locking-Shift-1
RegExp.XE = /\u000E/g; // /XE/
// ---
String.XF = '\u000F'; // U+000F Locking-Shift-0
RegExp.XF = /\u000F/g; // /XF/
RegExp.BKuZ = /\u005Cu00/g; // `\u00`
//==========================================================================
// STRING Proto.
//==========================================================================
String.SourceEscapes = (function (
/*str[]&*/ a,
i,
n,
k
) //----------------------------------
// Database entity used by toSource().
// index => <CHAR> ( = String.<CHAR_KEY> )
// <CHAR> => <CHAR_KEY>
{
for (n = a.length, i = -1; ++i < n; k = a[i], a[(a[i] = String[k])] = k);
return a;
})(['BK', 'CR', 'LF', 'DQ', 'SQ', 'TB', 'BS', 'VT', 'FF']);
// Backup of the native method
// => String.__toSource__
// ---
'function' == typeof String.__toSource__ ||
(String.__toSource__ = String.prototype.toSource);
String.prototype.toSource = function toSource(
/*<DQ>|<SQ>=<DQ>*/ quote,
/*bool=0*/ allowUTF16,
q,
o,
altQuote,
r,
i,
s,
k
) //----------------------------------
// Return a quote-nested string so that eval(this.toSource())===this. This function
// overrides the original toSource method in order to get shorter outputs. Examples:
//
// INPUT STRING (JS) | OUTPUT CHARS (NATIVE METHOD) | OUTPUT CHARS (NEW VERSION)
// -------------------------------------------------------------------------------------------
// "abc" | (new String("abc")) | "abc"
// -------------------------------------------------------------------------------------------
// "\\\r\n\t\v\f\0" | (new String("\\\r\n\t\x0B\f\x00")) | "\\\r\n\t\v\f\0"
// -------------------------------------------------------------------------------------------
// "àbçdé" | (new String("\u00E0b\u00E7d\u00E9")) | "\xE0b\xE7d\xE9"
//
// [REM] Tested on a jpeg file owning 23,052 bytes, the new method returns 60,433 characters
// while the native method outputs 89,653 characters. Result is 67% lighter.
//
// `quote` : Either <DQ> (default) or <SQ>. Use myStr.toSource("'") to get single quot nesting.
// `allowUTF16` : Reserved. (For the time being, output is always a full ASCII string.)
{
// Aliases.
// ---
q = callee.Q;
o = String.SourceEscapes;
// Defaut quote is <DQ>, unless <SQ> is supplied.
// altQuote :: quote==<DQ> ? <SQ> : <DQ>
// ---
String.SQ == quote || (quote = String.DQ);
altQuote = String[String.SQ == quote ? 'DQ' : 'SQ'];
// Take advantage of native toSource() performance.
// ---
i = (r = String.__toSource__.call(this)).length;
// ---
// [REM] `s` cannot contain non-ASCII char at this stage
// so we can temporarily replace any escaped <BK> by <X1>,
// then safely work on instances of other escaped chars.
// ---
r = r.substring(q.start, i + q.end).replace(RegExp._BK, String.X1);
// `\u00` => `\x`
// ---
/// q.longUni && (r = r.replace(RegExp.BKuZ, String.BK + 'x'));
// Improve native escapes according to improveEsc.
// E.g. `\x0B` => `\v`
// ---
for (
s = q.improvable, i = s.length;
i--;
k = '_' + o[s.charAt(i)], r = r.replace(RegExp[k], String[k])
);
// Manage alternate quote character. (Normal case is unsafeQuote==<SQ>.)
// If( quote != unsafe ), nothing to do (quote is already escaped and unsafe is OK here.)
// If( quote == unsafe ), unsafe => \unsafe ; \altQuote => altQuote
// If unsafe is empty (unusual!) all quotes are escaped, therefore
// we need to remove escape seq for non-quote: \altQuote => altQuote
// ---
if (quote == q.unsafeQuote) {
k = o[quote];
r = r.replace(RegExp[k], String['_' + k]); // Repl. quote by its regular escape form
k = o[altQuote];
r = r.replace(RegExp['_' + k], altQuote); // Repl. any \altQuote by altQuote
} else {
q.unsafeQuote || (r = r.replace(RegExp['_' + o[altQuote]], altQuote));
}
// Special treatment for escaped NUL char. (Tricky!)
// `\x00` --> `\0` (lookahead != decimalDigit)
// ---
for (
i = -2;
-1 != (i = r.indexOf('\\x00', (i += 2)));
isNaN(+('-' + r.charAt(4 + i))) &&
(r = r.substr(0, 1 + i) + '0' + r.substr(4 + i))
);
// Final result.
// ---
return quote + r.replace(RegExp.X1, String._BK) + quote;
};
String.prototype.toSource.Q = (function (
/*obj&*/ R,
err,
o,
t,
s,
i,
n,
a
) //----------------------------------
// Cached structure (-> toSource.)
// => { start: int>0, end: int<0, longUni: 0|1, unsafeQuote: <DQ>|<SQ>|'', improvable: char* }
// [REM] The once-call function parses ExtendScript's native toSource behavior.
{
err = '';
o = String.SourceEscapes;
// Create a test string for analyzing the original toSource().
// ExtendScript produces verbose results looking like
// s :: `(new String("ZZ\\ZZ\rZZ\nZZ\"...\x0BZZ\fZZ\u00C0ZZ"))`
// ---
t = 'ZZ';
s = t + (o.join('') + '\xC0').split('').join(t) + t;
s = String.__toSource__.call(s);
// Compute magic offsets (start,end) so that string inside quotes
// *always* matches src.substring(start, end+src.length).
// ---
R.start = s.indexOf(t);
R.end = (i = t.length) + s.lastIndexOf(t) - (n = s.length);
// Store escapes in array. Desired result is
// a :: [<_BK>,<_CR>,<_LF>,<_DQ>,<SQ>,<_TB>,<_BS>,<_VT>,<_FF>,`\u00C0`]
// ---
a = s.substring(i + R.start, -i + n + R.end).split(t);
// ---
// Almost every character is reduced to its shortest escape sequence.
// However, `\u00HH` can be shortened `\xHH` and `\x0B` is in fact `\v`.
// Also, <DQ> is usually escaped while <SQ> is not. We now have enough
// information to create a better toSource converter.
// [REM] We assume ExtendScript *may change* the native method,
// so we have to keep the algorithm fully agnostic.
// ---
// Whether we have to reduce `\u00HH`.
// ---
R.longUni = +(0 <= a.pop().indexOf('u00'));
// Check that String.SourceEscapes and `a` have now the same length.
// ---
if (a.length != o.length) {
err = 'A fatal error occured while analyzing String.prototype.toSource.';
}
// Check whether each escapable character is reduced as desired,
// that is, a[i] == String['_'+o[o[i]]].
// OK => Change a[i] into '' (nothing to do, skip.)
// KO => Change a[i] into o[i] (character whose esc must be improved.)
// ---
// [REM] For the sake of abstraction we allow unsafeQuote=='', which would
// reflect native toSource() method to escape all quotation marks.
// Such case wouldn't lead to errors in the present implementation :-)
for (R.unsafeQuote = '', i = a.length; !err && i--;) {
t = a[i]; // Native escape sequence, e.g `\r`
s = o[o[i]]; // Key, e.g 'CR'.
// Match case :-)
// ---
if (t == String['_' + s]) {
a[i] = '';
continue;
}
// Case where ExtendScript doesn't escape a quote,
// usually t==<SQ>. That's OK anyway.
// ---
if (t == String.SQ || t == String.DQ) {
R.unsafeQuote = t;
a[i] = '';
continue;
}
// ExtendScript does not escape <BK> the right way :-(
// ---
if ('BK' == s) {
err = __(
'Unexpected escape sequence %1 for the backslash.',
String.__toSource__call(t)
);
break;
}
// ExtendScript does not output a capturable escape sequence :-(
// ---
if (!RegExp['_' + s].test(t)) {
err = __(
'Cannot capture the escape sequence %1 for the character <%2>.',
String.__toSource__call(t),
s
);
break;
}
// Finally, we provide a better escape sequence. Typical case is <VT>
// treated `\x0B` while it could be shortened `\v`. (We set a[i] to
// o[i] only if our own esc is shorter.)
// ---
a[i] = 2 < t.length ? o[i] : '';
}
if (err) {
throw Error(__('%1 > %2', 'Ext/string', err));
}
R.improvable = [a.join(''), (a.length = 0)][0];
return R;
})({});
//==========================================================================
// REGEXP Proto.
//==========================================================================
RegExp.prototype.flags = function flags() //----------------------------------
// Flag-string of the regex in g-i-m order. See [ECMA].
// => '' | 'g' | 'i' | 'm' | 'gi' | 'gm' | 'im' | 'gim'
{
return (
(this.global ? 'g' : '') +
(this.ignoreCase ? 'i' : '') +
(this.multiline ? 'm' : '')
);
};
RegExp.prototype.toSource =
function toSource() //----------------------------------
// Return a valid, ASCII-safe, uneval-string S for this
// regex such as `this` is equivalent to eval(S).
// ---
// [REM] `S` has the form 'RegExp("str","flg")' where
// "str" comes from String.prototype.toSource(),
// so the full result is ASCII-safe and one-line.
{
// ALGORITHM IN USE.
// ---
// Problem. - `this` is a valid RegExp, but we don't
// know whether it has been created thru a string,
// RegExp("xyz"), or from a literal /xyz/. In either
// cases this.source matches `xyz` but:
// (a) "xyz" MAY contain '/' unescaped,
// (b) /xyz/ CANNOT contain '/' unescaped,
// Therefore the `source` is not consistent if
// the declaration involved the '/' character.
// For example, say source == `a\/b`
// in case (a) the pattern is /a\\\/b/, matching `a\/b`
// in case (b) the pattern is /a\/b/ , matching `a/b`
// (a) RegExp("a\\/b","g")
// ---
// Solution:
// 1. Replace /BKBK/ by `\x5C` { `\\` } => `\x5C`
// 2. Replace /BKSL/ by <SL> { `\/` } => `/`
// 3. Replace /SL/ by `\x2F` { `/` } => `\x2F`
return $.global.localize(
'RegExp(%1,"%2")',
(this.source || '(?:)') // As [ECMA] suggests for ''.
.replace(RegExp.BKBK, '\\x5C') // Step 1.
.replace(RegExp.BKSL, String.SL) // Step 2.
.replace(RegExp.SL, '\\x2F') // Step 3.
.toSource(),
this.flags()
);
};
//==========================================================================
// JSON (Core.)
//==========================================================================
$.global.JSON = function JSON(x, y, z, t) {
return callee[callee.__auto__].call(callee, x, y, z, t);
};
$.global.JSON.setup({
__auto__: 'lave',
toString: function toString() {
return 'JSON';
},
toSource: function toSource() {
return '$.global["JSON"]';
},
'~': {
NEWL: '\r',
GLOB: function () {
return this;
}.call(null),
GSTR: function () {
return this.String;
}.call(null),
GOBJ: function () {
return this.Object;
}.call(null),
GREG: function () {
return this.RegExp;
}.call(null),
GNUM: function () {
return this.Number;
}.call(null),
GFCT: function () {
return this.Function;
}.call(null),
ANOF: '' + function () { }.name,
},
});
//==========================================================================
// JSON (Private tools.)
//==========================================================================
$.global.JSON['~'].setup({
ODEL: function (/*obj&*/ o, k) //----------------------------------
// Non-recursive Key Deleter.
// => `o`
{
for (k in o) o.hasOwnProperty(k) && delete o[k];
return o;
},
BRKN: function (/*str*/ s) //----------------------------------
// Whether s is a 'broken' spec path.
// => 1 (broken) | 0 (not-broken)
{
return +(
0 < s.indexOf('script-preferences') ||
(callee.Q || (callee.Q = /@(find|change).+shadow-settings$/)).test(s)
);
},
EST$: function ($0) //----------------------------------
// ESTN Replacer.
// [REM] `this` is not the private zone, so we load
// the String ctor in callee.Q.
{
return callee.Q['_' + callee.Q.SourceEscapes[$0]];
}.setup({ Q: JSON['~'].GSTR }),
ESTN: function (/*str*/ s) //----------------------------------
// Escape '\t', '\r' and '\n'
// XXX<TB>YYY<CR><LF>ZZZ => `XXX\tYYY\r\nZZZ`
{
return s.replace(this.GREG.TBNL, this.EST$);
},
OTOS: function (/*obj*/ o) //----------------------------------
// E.g: => "[object RegExp]", "[object Array]" "[object global]" etc
{
return this.GOBJ.prototype.toString.call(o);
},
NASO: function (/*obj*/ o, /*str*/ k) //----------------------------------
// Tell whether o has-NOT-OwnProperty k (secure.)
// => 1 | 0
{
return 1 - this.GOBJ.prototype.hasOwnProperty.call(o, k);
},
NATV: function (/*fct*/ f, q, s, p) //----------------------------------
// Tell whether `f` is native.
// [REM] String(Object) is something like "{...[native code]...}"
{
return (
(q =
callee.Q ||
(callee.Q = this.GOBJ.toSource().substr(
this.GOBJ.toSource().indexOf('{')
))),
0 > (p = (s = f.toSource()).indexOf(q))
? 0
: +(s.length == p + q.length)
);
},
OKID: function (/*str*/ s) //----------------------------------
// Tell whether `s` is a valid JS identifier.
{
try {
this.GFCT('var ' + s);
return 1;
} catch (_) {
return 0;
}
},
REFS: function (/*ref*/ o, q, n, i) //----------------------------------
// Reference Stack Manager (prevent cycles), init in lave()
// => 1 (already in stack) | 0 (just added)
{
i = n = (q = callee.Q).length;
while (i--) {
if (o === q[i]) return 1;
}
return (q[n] = o), 0;
}.setup({ Q: [] }),
DOMS: function (/*dom*/ o, q, k, s, t) //----------------------------------
// DOM Spec Cache Manager (prevent cycles), init. in lave()
// => 1 (already-in-stack) | 0 (just-added) | -1 (dont-browse)
{
if ((q = callee.Q).hasOwnProperty((k = o.toSpecifier()))) return q[k];
s = this.KDOM ? 'resolve(' + k.toSource() + ')' : k.toSource();
t =
(this.BRKN(k) && '/*broken*/ ') ||
(!o.hasOwnProperty('properties') && '/*no-prop*/');
q[k] = (this.SPCE && this.KDOM ? t || '/*cycle*/ ' : '') + s;
return t ? q[k] : '';
}.setup({ Q: {} }),
});
//==========================================================================
// JSON (ExtendScript & ScriptUI natives.)
//==========================================================================
$.global.JSON['~'].setup({
NTVF: {
'_!': 'UnitValue()|',
'_%': 'UnitValue()|',
'_*': 'UnitValue()|',
'_+': 'UnitValue()|',
'_-': 'UnitValue()|',
'_/': 'UnitValue()|',
'_<': 'UnitValue()|',
'_<=': 'UnitValue()|',
'_==': 'UnitValue()|',
'_===': 'UnitValue()|',
'_~': 'UnitValue()|',
// ---
_Array: '|',
_Boolean: '|',
_Button: '|',
_Checkbox: '|',
_Date: '|',
_Dictionary: '|',
_DropDownList: '|',
_EditText: '|',
_Error: '|',
_Event: '|',
_ExternalObject: '|',
_File: '|',
_Folder: '|',
_Function: '|',
_Group: '|',
_IconButton: '|',
_ListBox: '|',
_ListItem: '|',
_Namespace: '|',
_Number: '|',
_Object: '|',
_Panel: '|',
_Progressbar: '|',
_QName: '|',
_RadioButton: '|',
_Reflection: '|',
_ReflectionInfo: '|',
_RegExp: '|',
_ScriptUI: '|',
_Scrollbar: '|',
_Slider: '|',
_Socket: '|',
_StaticText: '|',
_String: '|',
_TreeView: '|',
_UIEvent: '|',
_UnitValue: '|',
_Window: '|',
_XML: '|',
_XMLList: '|',
// ---
_about: '$|',
_abs: 'Math|',
_acos: 'Math|',
_add: 'Window|TreeView|Panel|ListBox|Group|DropDownList|',
_addEventListener:
'Window|TreeView|StaticText|Slider|Scrollbar|RadioButton|Progressbar|Panel|' +
'ListItem|ListBox|IconButton|Group|EditText|DropDownList|Checkbox|Button|',
_alert: '|Window|',
_anchor: 'String.prototype|',
_as: 'UnitValue()|',
_asin: 'Math|',
_atan: 'Math|',
_atan2: 'Math|',
_beep: '|',
_big: 'String.prototype|',
_blink: 'String.prototype|',
_bold: 'String.prototype|',
_bp: '$|',
_ceil: 'Math|',
_center: 'Window|',
_changePath: 'Folder.prototype|File.prototype|',
_charAt: 'String.prototype|',
_charCodeAt: 'String.prototype|',
_close: 'Window|Socket.prototype|File.prototype|',
_colorPicker: '$|',
_compile: 'RegExp.prototype|',
_concat: 'Array.prototype|String.prototype|',
_confirm: '|Window|',
_convert: 'UnitValue()|',
_copy: 'File.prototype|',
_cos: 'Math|',
_create: 'Folder.prototype|',
_createAlias: 'File.prototype|',
_decode: 'Folder|File|',
_decodeURI: '|',
_decodeURIComponent: '|',
_defaultSettings: 'XML|',
_dispatchEvent:
'Window|TreeView|StaticText|Slider|Scrollbar|RadioButton|Progressbar|Panel|' +
'ListItem|ListBox|IconButton|Group|EditText|DropDownList|Checkbox|Button|',
_encode: 'Folder|File|',
_encodeURI: '|',
_encodeURIComponent: '|',
_escape: '|',
_eval: '|',
_evalFile: '$|',
_exec: 'RegExp.prototype|',
_execute: 'Folder.prototype|File.prototype|',
_exit: '|',
_exp: 'Math|',
_find: 'Window|TreeView|ListBox|DropDownList|',
_findElement: 'Window|',
_fixed: 'String.prototype|',
_floor: 'Math|',
_fontcolor: 'String.prototype|',
_fontsize: 'String.prototype|',
_fromCharCode: 'String|',
_gc: '$|',
_getClass: 'Dictionary.prototype|',
_getClasses: 'Dictionary.prototype|',
_getDate: 'Date.prototype|',
_getDay: 'Date.prototype|',
_getFiles: 'Folder.prototype|',
_getFullYear: 'Date.prototype|',
_getGroups: 'Dictionary.prototype|',
_getHours: 'Date.prototype|',
_getMilliseconds: 'Date.prototype|',
_getMinutes: 'Date.prototype|',
_getMonth: 'Date.prototype|',
_getRelativeURI: 'Folder.prototype|File.prototype|',
_getResourceText: 'ScriptUI|',
_getSeconds: 'Date.prototype|',
_getTime: 'Date.prototype|',
_getTimezoneOffset: 'Date.prototype|',
_getUTCDate: 'Date.prototype|',
_getUTCDay: 'Date.prototype|',
_getUTCFullYear: 'Date.prototype|',
_getUTCHours: 'Date.prototype|',
_getUTCMilliseconds: 'Date.prototype|',
_getUTCMinutes: 'Date.prototype|',
_getUTCMonth: 'Date.prototype|',
_getUTCSeconds: 'Date.prototype|',
_getYear: 'Date.prototype|',
_getenv: '$|',
_hasOwnProperty: 'Object.prototype|',
_hide:
'Window|TreeView|StaticText|Slider|Scrollbar|RadioButton|Progressbar|Panel|' +
'ListBox|IconButton|Group|EditText|DropDownList|Checkbox|Button|',
_indexOf: 'String.prototype|',
_isEncodingAvailable: 'Folder|File|',
_isFinite: '|',
_isNaN: '|',
_isPrototypeOf: 'Object.prototype|',
_isValid: 'Object|',
_isXMLName: '|',
_italics: 'String.prototype|',
_join: 'Array.prototype|',
_lastIndexOf: 'String.prototype|',
_link: 'String.prototype|',
_list: '$|',
_listLO: '$|',
_listen: 'Socket.prototype|',
_localeCompare: 'String.prototype|',
_localize: '|',
_log: 'Math|',
_match: 'String.prototype|',
_max: 'Math|',
_min: 'Math|',
_newImage: 'ScriptUI|',
_newFont: 'ScriptUI|',
_notify:
'Window|TreeView|StaticText|Slider|Scrollbar|RadioButton|' +
'ListBox|IconButton|EditText|DropDownList|Checkbox|Button|',
_now: 'Date|',
_open: 'Socket.prototype|File.prototype|',
_openDialog: 'File|',
_openDlg: 'Folder.prototype|File.prototype|',
_parse: 'Date|',
_parseFloat: '|',
_parseInt: '|',
_poll: 'Socket.prototype|',
_pop: 'Array.prototype|',
_pow: 'Math|',
_print: 'File.prototype|',
_prompt: '|Window|',
_propertyIsEnumerable: 'Object.prototype|',
_push: 'Array.prototype|',
_random: 'Math|',
_read: 'Socket.prototype|File.prototype|',
_readch: 'File.prototype|',
_readln: 'Socket.prototype|File.prototype|',
_remove:
'Window|TreeView|Panel|ListBox|Group|DropDownList|Folder.prototype|File.prototype|',
_removeAll: 'TreeView|ListBox|DropDownList|',
_removeEventListener:
'Window|TreeView|StaticText|Slider|Scrollbar|RadioButton|Progressbar|Panel|' +
'ListItem|ListBox|IconButton|Group|EditText|DropDownList|Checkbox|Button|',
_rename: 'Folder.prototype|File.prototype|',
_replace: 'String.prototype|',
_resolve: '|Folder.prototype|File.prototype|',
_revealItem: 'ListBox|',
_reverse: 'Array.prototype|',
_round: 'Math|',
_runtimeError: 'Error|',
_saveDialog: 'File|',
_saveDlg: 'Folder.prototype|File.prototype|',
_search: 'String.prototype|ExternalObject|',
_seek: 'File.prototype|',
_selectDialog: 'Folder|',
_selectDlg: 'Folder.prototype|',
_setDate: 'Date.prototype|',
_setDefaultXMLNamespace: '|',
_setFullYear: 'Date.prototype|',
_setHours: 'Date.prototype|',
_setMilliseconds: 'Date.prototype|',
_setMinutes: 'Date.prototype|',
_setMonth: 'Date.prototype|',
_setSeconds: 'Date.prototype|',
_setSettings: 'XML|',
_setTime: 'Date.prototype|',
_setUTCDate: 'Date.prototype|',
_setUTCFullYear: 'Date.prototype|',
_setUTCHours: 'Date.prototype|',
_setUTCMilliseconds: 'Date.prototype|',
_setUTCMinutes: 'Date.prototype|',
_setUTCMonth: 'Date.prototype|',
_setUTCSeconds: 'Date.prototype|',
_setYear: 'Date.prototype|',
_setenv: '$|',
_settings: 'XML|',
_shift: 'Array.prototype|',
_show:
'Window|TreeView|StaticText|Slider|Scrollbar|RadioButton|Progressbar|Panel|' +
'ListBox|IconButton|Group|EditText|DropDownList|Checkbox|Button|',
_sin: 'Math|',
_sleep: '$|',
_slice: 'Array.prototype|String.prototype|',
_small: 'String.prototype|',
_sort: 'Array.prototype|',
_splice: 'Array.prototype|',
_split: 'String.prototype|',
_sqrt: 'Math|',
_strike: 'String.prototype|',
_sub: 'String.prototype|',
_substr: 'String.prototype|',
_substring: 'String.prototype|',
_summary: '$|',
_sup: 'String.prototype|',
_tan: 'Math|',
_tell: 'File.prototype|',
_test: 'RegExp.prototype|',
_toDateString: 'Date.prototype|',
_toExponential: 'Number.prototype|',
_toFixed: 'Number.prototype|',
_toGMTString: 'Date.prototype|',
_toLocaleDateString: 'Date.prototype|',
_toLocaleLowerCase: 'String.prototype|',
_toLocaleString:
'Array.prototype|Object.prototype|Date.prototype|Number.prototype|',
_toLocaleTimeString: 'Date.prototype|',
_toLocaleUpperCase: 'String.prototype|',
_toLowerCase: 'String.prototype|',
_toPrecision: 'Number.prototype|',
_toSource:
'Window|UIEvent|TreeView|StaticText|Slider|Scrollbar|RadioButton|Progressbar|Panel|' +
'ListItem|ListBox|IconButton|Group|Event|EditText|DropDownList|Checkbox|Button|' +
'Array.prototype|Object.prototype|Function.prototype|Folder.prototype|File.prototype|' +
'Error.prototype|RegExp.prototype|Date.prototype|String.prototype|Number.prototype|' +
'ExternalObject|Array|Object|Function|Socket|ReflectionInfo|Reflection|' +
'UnitValue|QName|Namespace|Folder|File|Error|XMLList|XML|RegExp|Date|String|Number|',
_toString:
'Array.prototype|Object.prototype|Function.prototype|Folder.prototype|File.prototype|' +
'Error.prototype|RegExp.prototype|Date.prototype|String.prototype|Number.prototype|' +
'Array|Object|Function|Socket|ReflectionInfo|Reflection|Folder|File|Error|' +
'RegExp|Date|String|Number|$|',