forked from derekantrican/GAS-ICS-Sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ical.js.gs
9506 lines (8275 loc) · 273 KB
/
ical.js.gs
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* Portions Copyright (C) Philipp Kewisch, 2011-2015 */
/* istanbul ignore next */
/* jshint ignore:start */
if (typeof module === 'object') {
// CommonJS, where exports may be different each time.
ICAL = module.exports;
} else if (typeof ICAL !== 'object') {/* istanbul ignore next */
/** @ignore */
this.ICAL = {};
}
/* jshint ignore:end */
/**
* The number of characters before iCalendar line folding should occur
* @type {Number}
* @default 75
*/
ICAL.foldLength = 75;
/**
* The character(s) to be used for a newline. The default value is provided by
* rfc5545.
* @type {String}
* @default "\r\n"
*/
ICAL.newLineChar = '\r\n';
/**
* Helper functions used in various places within ical.js
* @namespace
*/
ICAL.helpers = {
/**
* Compiles a list of all referenced TZIDs in all subcomponents and
* removes any extra VTIMEZONE subcomponents. In addition, if any TZIDs
* are referenced by a component, but a VTIMEZONE does not exist,
* an attempt will be made to generate a VTIMEZONE using ICAL.TimezoneService.
*
* @param {ICAL.Component} vcal The top-level VCALENDAR component.
* @return {ICAL.Component} The ICAL.Component that was passed in.
*/
updateTimezones: function(vcal) {
var allsubs, properties, vtimezones, reqTzid, i, tzid;
if (!vcal || vcal.name !== "vcalendar") {
//not a top-level vcalendar component
return vcal;
}
//Store vtimezone subcomponents in an object reference by tzid.
//Store properties from everything else in another array
allsubs = vcal.getAllSubcomponents();
properties = [];
vtimezones = {};
for (i = 0; i < allsubs.length; i++) {
if (allsubs[i].name === "vtimezone") {
tzid = allsubs[i].getFirstProperty("tzid").getFirstValue();
vtimezones[tzid] = allsubs[i];
} else {
properties = properties.concat(allsubs[i].getAllProperties());
}
}
//create an object with one entry for each required tz
reqTzid = {};
for (i = 0; i < properties.length; i++) {
if ((tzid = properties[i].getParameter("tzid"))) {
reqTzid[tzid] = true;
}
}
//delete any vtimezones that are not on the reqTzid list.
for (i in vtimezones) {
if (vtimezones.hasOwnProperty(i) && !reqTzid[i]) {
vcal.removeSubcomponent(vtimezones[i]);
}
}
//create any missing, but registered timezones
for (i in reqTzid) {
if (
reqTzid.hasOwnProperty(i) &&
!vtimezones[i] &&
ICAL.TimezoneService.has(i)
) {
vcal.addSubcomponent(ICAL.TimezoneService.get(i).component);
}
}
return vcal;
},
/**
* Checks if the given type is of the number type and also NaN.
*
* @param {Number} number The number to check
* @return {Boolean} True, if the number is strictly NaN
*/
isStrictlyNaN: function(number) {
return typeof(number) === 'number' && isNaN(number);
},
/**
* Parses a string value that is expected to be an integer, when the valid is
* not an integer throws a decoration error.
*
* @param {String} string Raw string input
* @return {Number} Parsed integer
*/
strictParseInt: function(string) {
var result = parseInt(string, 10);
if (ICAL.helpers.isStrictlyNaN(result)) {
throw new Error(
'Could not extract integer from "' + string + '"'
);
}
return result;
},
/**
* Creates or returns a class instance of a given type with the initialization
* data if the data is not already an instance of the given type.
*
* @example
* var time = new ICAL.Time(...);
* var result = ICAL.helpers.formatClassType(time, ICAL.Time);
*
* (result instanceof ICAL.Time)
* // => true
*
* result = ICAL.helpers.formatClassType({}, ICAL.Time);
* (result isntanceof ICAL.Time)
* // => true
*
*
* @param {Object} data object initialization data
* @param {Object} type object type (like ICAL.Time)
* @return {?} An instance of the found type.
*/
formatClassType: function formatClassType(data, type) {
if (typeof(data) === 'undefined') {
return undefined;
}
if (data instanceof type) {
return data;
}
return new type(data);
},
/**
* Identical to indexOf but will only match values when they are not preceded
* by a backslash character.
*
* @param {String} buffer String to search
* @param {String} search Value to look for
* @param {Number} pos Start position
* @return {Number} The position, or -1 if not found
*/
unescapedIndexOf: function(buffer, search, pos) {
while ((pos = buffer.indexOf(search, pos)) !== -1) {
if (pos > 0 && buffer[pos - 1] === '\\') {
pos += 1;
} else {
return pos;
}
}
return -1;
},
/**
* Find the index for insertion using binary search.
*
* @param {Array} list The list to search
* @param {?} seekVal The value to insert
* @param {function(?,?)} cmpfunc The comparison func, that can
* compare two seekVals
* @return {Number} The insert position
*/
binsearchInsert: function(list, seekVal, cmpfunc) {
if (!list.length)
return 0;
var low = 0, high = list.length - 1,
mid, cmpval;
while (low <= high) {
mid = low + Math.floor((high - low) / 2);
cmpval = cmpfunc(seekVal, list[mid]);
if (cmpval < 0)
high = mid - 1;
else if (cmpval > 0)
low = mid + 1;
else
break;
}
if (cmpval < 0)
return mid; // insertion is displacing, so use mid outright.
else if (cmpval > 0)
return mid + 1;
else
return mid;
},
/**
* Convenience function for debug output
* @private
*/
dumpn: /* istanbul ignore next */ function() {
if (!ICAL.debug) {
return;
}
if (typeof (console) !== 'undefined' && 'log' in console) {
ICAL.helpers.dumpn = function consoleDumpn(input) {
console.log(input);
};
} else {
ICAL.helpers.dumpn = function geckoDumpn(input) {
dump(input + '\n');
};
}
ICAL.helpers.dumpn(arguments[0]);
},
/**
* Clone the passed object or primitive. By default a shallow clone will be
* executed.
*
* @param {*} aSrc The thing to clone
* @param {Boolean=} aDeep If true, a deep clone will be performed
* @return {*} The copy of the thing
*/
clone: function(aSrc, aDeep) {
if (!aSrc || typeof aSrc != "object") {
return aSrc;
} else if (aSrc instanceof Date) {
return new Date(aSrc.getTime());
} else if ("clone" in aSrc) {
return aSrc.clone();
} else if (Array.isArray(aSrc)) {
var arr = [];
for (var i = 0; i < aSrc.length; i++) {
arr.push(aDeep ? ICAL.helpers.clone(aSrc[i], true) : aSrc[i]);
}
return arr;
} else {
var obj = {};
for (var name in aSrc) {
// uses prototype method to allow use of Object.create(null);
/* istanbul ignore else */
if (Object.prototype.hasOwnProperty.call(aSrc, name)) {
if (aDeep) {
obj[name] = ICAL.helpers.clone(aSrc[name], true);
} else {
obj[name] = aSrc[name];
}
}
}
return obj;
}
},
/**
* Performs iCalendar line folding. A line ending character is inserted and
* the next line begins with a whitespace.
*
* @example
* SUMMARY:This line will be fold
* ed right in the middle of a word.
*
* @param {String} aLine The line to fold
* @return {String} The folded line
*/
foldline: function foldline(aLine) {
var result = "";
var line = aLine || "";
while (line.length) {
result += ICAL.newLineChar + " " + line.substr(0, ICAL.foldLength);
line = line.substr(ICAL.foldLength);
}
return result.substr(ICAL.newLineChar.length + 1);
},
/**
* Pads the given string or number with zeros so it will have at least two
* characters.
*
* @param {String|Number} data The string or number to pad
* @return {String} The number padded as a string
*/
pad2: function pad(data) {
if (typeof(data) !== 'string') {
// handle fractions.
if (typeof(data) === 'number') {
data = parseInt(data);
}
data = String(data);
}
var len = data.length;
switch (len) {
case 0:
return '00';
case 1:
return '0' + data;
default:
return data;
}
},
/**
* Truncates the given number, correctly handling negative numbers.
*
* @param {Number} number The number to truncate
* @return {Number} The truncated number
*/
trunc: function trunc(number) {
return (number < 0 ? Math.ceil(number) : Math.floor(number));
},
/**
* Poor-man's cross-browser inheritance for JavaScript. Doesn't support all
* the features, but enough for our usage.
*
* @param {Function} base The base class constructor function.
* @param {Function} child The child class constructor function.
* @param {Object} extra Extends the prototype with extra properties
* and methods
*/
inherits: function(base, child, extra) {
function F() {}
F.prototype = base.prototype;
child.prototype = new F();
if (extra) {
ICAL.helpers.extend(extra, child.prototype);
}
},
/**
* Poor-man's cross-browser object extension. Doesn't support all the
* features, but enough for our usage. Note that the target's properties are
* not overwritten with the source properties.
*
* @example
* var child = ICAL.helpers.extend(parent, {
* "bar": 123
* });
*
* @param {Object} source The object to extend
* @param {Object} target The object to extend with
* @return {Object} Returns the target.
*/
extend: function(source, target) {
for (var key in source) {
var descr = Object.getOwnPropertyDescriptor(source, key);
if (descr && !Object.getOwnPropertyDescriptor(target, key)) {
Object.defineProperty(target, key, descr);
}
}
return target;
}
};
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* Portions Copyright (C) Philipp Kewisch, 2011-2015 */
/** @namespace ICAL */
/**
* This symbol is further described later on
* @ignore
*/
ICAL.design = (function() {
'use strict';
var FROM_ICAL_NEWLINE = /\\\\|\\;|\\,|\\[Nn]/g;
var TO_ICAL_NEWLINE = /\\|;|,|\n/g;
var FROM_VCARD_NEWLINE = /\\\\|\\,|\\[Nn]/g;
var TO_VCARD_NEWLINE = /\\|,|\n/g;
function createTextType(fromNewline, toNewline) {
var result = {
matches: /.*/,
fromICAL: function(aValue, structuredEscape) {
return replaceNewline(aValue, fromNewline, structuredEscape);
},
toICAL: function(aValue, structuredEscape) {
var regEx = toNewline;
if (structuredEscape)
regEx = new RegExp(regEx.source + '|' + structuredEscape);
return aValue.replace(regEx, function(str) {
switch (str) {
case "\\":
return "\\\\";
case ";":
return "\\;";
case ",":
return "\\,";
case "\n":
return "\\n";
/* istanbul ignore next */
default:
return str;
}
});
}
};
return result;
}
// default types used multiple times
var DEFAULT_TYPE_TEXT = { defaultType: "text" };
var DEFAULT_TYPE_TEXT_MULTI = { defaultType: "text", multiValue: "," };
var DEFAULT_TYPE_TEXT_STRUCTURED = { defaultType: "text", structuredValue: ";" };
var DEFAULT_TYPE_INTEGER = { defaultType: "integer" };
var DEFAULT_TYPE_DATETIME_DATE = { defaultType: "date-time", allowedTypes: ["date-time", "date"] };
var DEFAULT_TYPE_DATETIME = { defaultType: "date-time" };
var DEFAULT_TYPE_URI = { defaultType: "uri" };
var DEFAULT_TYPE_UTCOFFSET = { defaultType: "utc-offset" };
var DEFAULT_TYPE_RECUR = { defaultType: "recur" };
var DEFAULT_TYPE_DATE_ANDOR_TIME = { defaultType: "date-and-or-time", allowedTypes: ["date-time", "date", "text"] };
function replaceNewlineReplace(string) {
switch (string) {
case "\\\\":
return "\\";
case "\\;":
return ";";
case "\\,":
return ",";
case "\\n":
case "\\N":
return "\n";
/* istanbul ignore next */
default:
return string;
}
}
function replaceNewline(value, newline, structuredEscape) {
// avoid regex when possible.
if (value.indexOf('\\') === -1) {
return value;
}
if (structuredEscape)
newline = new RegExp(newline.source + '|\\\\' + structuredEscape);
return value.replace(newline, replaceNewlineReplace);
}
var commonProperties = {
"categories": DEFAULT_TYPE_TEXT_MULTI,
"url": DEFAULT_TYPE_URI,
"version": DEFAULT_TYPE_TEXT,
"uid": DEFAULT_TYPE_TEXT
};
var commonValues = {
"boolean": {
values: ["TRUE", "FALSE"],
fromICAL: function(aValue) {
switch (aValue) {
case 'TRUE':
return true;
case 'FALSE':
return false;
default:
//TODO: parser warning
return false;
}
},
toICAL: function(aValue) {
if (aValue) {
return 'TRUE';
}
return 'FALSE';
}
},
float: {
matches: /^[+-]?\d+\.\d+$/,
fromICAL: function(aValue) {
var parsed = parseFloat(aValue);
if (ICAL.helpers.isStrictlyNaN(parsed)) {
// TODO: parser warning
return 0.0;
}
return parsed;
},
toICAL: function(aValue) {
return String(aValue);
}
},
integer: {
fromICAL: function(aValue) {
var parsed = parseInt(aValue);
if (ICAL.helpers.isStrictlyNaN(parsed)) {
return 0;
}
return parsed;
},
toICAL: function(aValue) {
return String(aValue);
}
},
"utc-offset": {
toICAL: function(aValue) {
if (aValue.length < 7) {
// no seconds
// -0500
return aValue.substr(0, 3) +
aValue.substr(4, 2);
} else {
// seconds
// -050000
return aValue.substr(0, 3) +
aValue.substr(4, 2) +
aValue.substr(7, 2);
}
},
fromICAL: function(aValue) {
if (aValue.length < 6) {
// no seconds
// -05:00
return aValue.substr(0, 3) + ':' +
aValue.substr(3, 2);
} else {
// seconds
// -05:00:00
return aValue.substr(0, 3) + ':' +
aValue.substr(3, 2) + ':' +
aValue.substr(5, 2);
}
},
decorate: function(aValue) {
return ICAL.UtcOffset.fromString(aValue);
},
undecorate: function(aValue) {
return aValue.toString();
}
}
};
var icalParams = {
// Although the syntax is DQUOTE uri DQUOTE, I don't think we should
// enfoce anything aside from it being a valid content line.
//
// At least some params require - if multi values are used - DQUOTEs
// for each of its values - e.g. delegated-from="uri1","uri2"
// To indicate this, I introduced the new k/v pair
// multiValueSeparateDQuote: true
//
// "ALTREP": { ... },
// CN just wants a param-value
// "CN": { ... }
"cutype": {
values: ["INDIVIDUAL", "GROUP", "RESOURCE", "ROOM", "UNKNOWN"],
allowXName: true,
allowIanaToken: true
},
"delegated-from": {
valueType: "cal-address",
multiValue: ",",
multiValueSeparateDQuote: true
},
"delegated-to": {
valueType: "cal-address",
multiValue: ",",
multiValueSeparateDQuote: true
},
// "DIR": { ... }, // See ALTREP
"encoding": {
values: ["8BIT", "BASE64"]
},
// "FMTTYPE": { ... }, // See ALTREP
"fbtype": {
values: ["FREE", "BUSY", "BUSY-UNAVAILABLE", "BUSY-TENTATIVE"],
allowXName: true,
allowIanaToken: true
},
// "LANGUAGE": { ... }, // See ALTREP
"member": {
valueType: "cal-address",
multiValue: ",",
multiValueSeparateDQuote: true
},
"partstat": {
// TODO These values are actually different per-component
values: ["NEEDS-ACTION", "ACCEPTED", "DECLINED", "TENTATIVE",
"DELEGATED", "COMPLETED", "IN-PROCESS"],
allowXName: true,
allowIanaToken: true
},
"range": {
values: ["THISLANDFUTURE"]
},
"related": {
values: ["START", "END"]
},
"reltype": {
values: ["PARENT", "CHILD", "SIBLING"],
allowXName: true,
allowIanaToken: true
},
"role": {
values: ["REQ-PARTICIPANT", "CHAIR",
"OPT-PARTICIPANT", "NON-PARTICIPANT"],
allowXName: true,
allowIanaToken: true
},
"rsvp": {
values: ["TRUE", "FALSE"]
},
"sent-by": {
valueType: "cal-address"
},
"tzid": {
matches: /^\//
},
"value": {
// since the value here is a 'type' lowercase is used.
values: ["binary", "boolean", "cal-address", "date", "date-time",
"duration", "float", "integer", "period", "recur", "text",
"time", "uri", "utc-offset"],
allowXName: true,
allowIanaToken: true
}
};
// When adding a value here, be sure to add it to the parameter types!
var icalValues = ICAL.helpers.extend(commonValues, {
text: createTextType(FROM_ICAL_NEWLINE, TO_ICAL_NEWLINE),
uri: {
// TODO
/* ... */
},
"binary": {
decorate: function(aString) {
return ICAL.Binary.fromString(aString);
},
undecorate: function(aBinary) {
return aBinary.toString();
}
},
"cal-address": {
// needs to be an uri
},
"date": {
decorate: function(aValue, aProp) {
if (design.strict) {
return ICAL.Time.fromDateString(aValue, aProp);
} else {
return ICAL.Time.fromString(aValue, aProp);
}
},
/**
* undecorates a time object.
*/
undecorate: function(aValue) {
return aValue.toString();
},
fromICAL: function(aValue) {
// from: 20120901
// to: 2012-09-01
if (!design.strict && aValue.length >= 15) {
// This is probably a date-time, e.g. 20120901T130000Z
return icalValues["date-time"].fromICAL(aValue);
} else {
return aValue.substr(0, 4) + '-' +
aValue.substr(4, 2) + '-' +
aValue.substr(6, 2);
}
},
toICAL: function(aValue) {
// from: 2012-09-01
// to: 20120901
var len = aValue.length;
if (len == 10) {
return aValue.substr(0, 4) +
aValue.substr(5, 2) +
aValue.substr(8, 2);
} else if (len >= 19) {
return icalValues["date-time"].toICAL(aValue);
} else {
//TODO: serialize warning?
return aValue;
}
}
},
"date-time": {
fromICAL: function(aValue) {
// from: 20120901T130000
// to: 2012-09-01T13:00:00
if (!design.strict && aValue.length == 8) {
// This is probably a date, e.g. 20120901
return icalValues.date.fromICAL(aValue);
} else {
var result = aValue.substr(0, 4) + '-' +
aValue.substr(4, 2) + '-' +
aValue.substr(6, 2) + 'T' +
aValue.substr(9, 2) + ':' +
aValue.substr(11, 2) + ':' +
aValue.substr(13, 2);
if (aValue[15] && aValue[15] === 'Z') {
result += 'Z';
}
return result;
}
},
toICAL: function(aValue) {
// from: 2012-09-01T13:00:00
// to: 20120901T130000
var len = aValue.length;
if (len == 10 && !design.strict) {
return icalValues.date.toICAL(aValue);
} else if (len >= 19) {
var result = aValue.substr(0, 4) +
aValue.substr(5, 2) +
// grab the (DDTHH) segment
aValue.substr(8, 5) +
// MM
aValue.substr(14, 2) +
// SS
aValue.substr(17, 2);
if (aValue[19] && aValue[19] === 'Z') {
result += 'Z';
}
return result;
} else {
// TODO: error
return aValue;
}
},
decorate: function(aValue, aProp) {
if (design.strict) {
return ICAL.Time.fromDateTimeString(aValue, aProp);
} else {
return ICAL.Time.fromString(aValue, aProp);
}
},
undecorate: function(aValue) {
return aValue.toString();
}
},
duration: {
decorate: function(aValue) {
return ICAL.Duration.fromString(aValue);
},
undecorate: function(aValue) {
return aValue.toString();
}
},
period: {
fromICAL: function(string) {
var parts = string.split('/');
parts[0] = icalValues['date-time'].fromICAL(parts[0]);
if (!ICAL.Duration.isValueString(parts[1])) {
parts[1] = icalValues['date-time'].fromICAL(parts[1]);
}
return parts;
},
toICAL: function(parts) {
if (!design.strict && parts[0].length == 10) {
parts[0] = icalValues.date.toICAL(parts[0]);
} else {
parts[0] = icalValues['date-time'].toICAL(parts[0]);
}
if (!ICAL.Duration.isValueString(parts[1])) {
if (!design.strict && parts[1].length == 10) {
parts[1] = icalValues.date.toICAL(parts[1]);
} else {
parts[1] = icalValues['date-time'].toICAL(parts[1]);
}
}
return parts.join("/");
},
decorate: function(aValue, aProp) {
return ICAL.Period.fromJSON(aValue, aProp, !design.strict);
},
undecorate: function(aValue) {
return aValue.toJSON();
}
},
recur: {
fromICAL: function(string) {
return ICAL.Recur._stringToData(string, true);
},
toICAL: function(data) {
var str = "";
for (var k in data) {
/* istanbul ignore if */
if (!Object.prototype.hasOwnProperty.call(data, k)) {
continue;
}
var val = data[k];
if (k == "until") {
if (val.length > 10) {
val = icalValues['date-time'].toICAL(val);
} else {
val = icalValues.date.toICAL(val);
}
} else if (k == "wkst") {
if (typeof val === 'number') {
val = ICAL.Recur.numericDayToIcalDay(val);
}
} else if (Array.isArray(val)) {
val = val.join(",");
}
str += k.toUpperCase() + "=" + val + ";";
}
return str.substr(0, str.length - 1);
},
decorate: function decorate(aValue) {
return ICAL.Recur.fromData(aValue);
},
undecorate: function(aRecur) {
return aRecur.toJSON();
}
},
time: {
fromICAL: function(aValue) {
// from: MMHHSS(Z)?
// to: HH:MM:SS(Z)?
if (aValue.length < 6) {
// TODO: parser exception?
return aValue;
}
// HH::MM::SSZ?
var result = aValue.substr(0, 2) + ':' +
aValue.substr(2, 2) + ':' +
aValue.substr(4, 2);
if (aValue[6] === 'Z') {
result += 'Z';
}
return result;
},
toICAL: function(aValue) {
// from: HH:MM:SS(Z)?
// to: MMHHSS(Z)?
if (aValue.length < 8) {
//TODO: error
return aValue;
}
var result = aValue.substr(0, 2) +
aValue.substr(3, 2) +
aValue.substr(6, 2);
if (aValue[8] === 'Z') {
result += 'Z';
}
return result;
}
}
});
var icalProperties = ICAL.helpers.extend(commonProperties, {
"action": DEFAULT_TYPE_TEXT,
"attach": { defaultType: "uri" },
"attendee": { defaultType: "cal-address" },
"calscale": DEFAULT_TYPE_TEXT,
"class": DEFAULT_TYPE_TEXT,
"comment": DEFAULT_TYPE_TEXT,
"completed": DEFAULT_TYPE_DATETIME,
"contact": DEFAULT_TYPE_TEXT,
"created": DEFAULT_TYPE_DATETIME,
"description": DEFAULT_TYPE_TEXT,
"dtend": DEFAULT_TYPE_DATETIME_DATE,
"dtstamp": DEFAULT_TYPE_DATETIME,
"dtstart": DEFAULT_TYPE_DATETIME_DATE,
"due": DEFAULT_TYPE_DATETIME_DATE,
"duration": { defaultType: "duration" },
"exdate": {
defaultType: "date-time",
allowedTypes: ["date-time", "date"],
multiValue: ','
},
"exrule": DEFAULT_TYPE_RECUR,
"freebusy": { defaultType: "period", multiValue: "," },
"geo": { defaultType: "float", structuredValue: ";" },
"last-modified": DEFAULT_TYPE_DATETIME,
"location": DEFAULT_TYPE_TEXT,
"method": DEFAULT_TYPE_TEXT,
"organizer": { defaultType: "cal-address" },
"percent-complete": DEFAULT_TYPE_INTEGER,
"priority": DEFAULT_TYPE_INTEGER,
"prodid": DEFAULT_TYPE_TEXT,
"related-to": DEFAULT_TYPE_TEXT,
"repeat": DEFAULT_TYPE_INTEGER,
"rdate": {
defaultType: "date-time",
allowedTypes: ["date-time", "date", "period"],
multiValue: ',',
detectType: function(string) {
if (string.indexOf('/') !== -1) {
return 'period';
}
return (string.indexOf('T') === -1) ? 'date' : 'date-time';
}
},
"recurrence-id": DEFAULT_TYPE_DATETIME_DATE,
"resources": DEFAULT_TYPE_TEXT_MULTI,
"request-status": DEFAULT_TYPE_TEXT_STRUCTURED,
"rrule": DEFAULT_TYPE_RECUR,
"sequence": DEFAULT_TYPE_INTEGER,
"status": DEFAULT_TYPE_TEXT,
"summary": DEFAULT_TYPE_TEXT,
"transp": DEFAULT_TYPE_TEXT,
"trigger": { defaultType: "duration", allowedTypes: ["duration", "date-time"] },
"tzoffsetfrom": DEFAULT_TYPE_UTCOFFSET,
"tzoffsetto": DEFAULT_TYPE_UTCOFFSET,
"tzurl": DEFAULT_TYPE_URI,
"tzid": DEFAULT_TYPE_TEXT,
"tzname": DEFAULT_TYPE_TEXT
});
// When adding a value here, be sure to add it to the parameter types!
var vcardValues = ICAL.helpers.extend(commonValues, {
text: createTextType(FROM_VCARD_NEWLINE, TO_VCARD_NEWLINE),
uri: createTextType(FROM_VCARD_NEWLINE, TO_VCARD_NEWLINE),
date: {
decorate: function(aValue) {
return ICAL.VCardTime.fromDateAndOrTimeString(aValue, "date");
},
undecorate: function(aValue) {
return aValue.toString();
},
fromICAL: function(aValue) {
if (aValue.length == 8) {
return icalValues.date.fromICAL(aValue);
} else if (aValue[0] == '-' && aValue.length == 6) {
return aValue.substr(0, 4) + '-' + aValue.substr(4);
} else {
return aValue;
}