/
em_javascript.js
2515 lines (2377 loc) · 98.2 KB
/
em_javascript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
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
/* Core JavaScript functions needed by ExpressionManager
* @author Thomas M. White (TMSWhite)
*/
function LEMcount()
{
// takes variable number of arguments - returns count of those arguments that are not null/empty
var result=0;
for (i=0;i<arguments.length;++i) {
var arg = arguments[i];
if (arg !== '') {
++result;
}
}
return result;
}
function LEMpi()
{
return Math.PI;
}
function LEMsum()
{
// takes variable number of arguments, returns their sum
var result=0;
for (i=0;i<arguments.length;++i) {
var arg = arguments[i];
if (!isNaN(arg)) {
result += (+arg);
}
}
return result;
}
function LEMintval(a)
{
if (isNaN(a)) {
return NaN;
}
return Math.floor(+a);
}
function LEMis_null(a)
{
return (a == null);
}
function LEMis_float(a)
{
if (isNaN(a))
{
return false;
}
var num = new Number(a);
// should this only return true if there is a non-zero decimal part to the number?
return (Math.floor(num) != num);
}
function LEMis_int(a)
{
if (isNaN(a))
{
return false;
}
var num = new Number(a);
// should this only return true if there is a non-zero decimal part to the number?
return (Math.floor(num) == num);
}
function LEMis_numeric(a)
{
if (a === '') {
return false; // to make consistent with PHP
}
return !(isNaN(a));
}
function LEMis_string(a)
{
return isNaN(a);
}
function LEMif(a,b,c)
{
// implements conditional logic. Note double negation of a to ensure it is cast to Boolean
if (a === '0') { return c; } // so consistent with PHP
return (!!a) ? b : c;
}
/**
* Returns comma separated list of non-null values
*/
function LEMlist()
{
// takes variable number of arguments
var result="";
var joiner = ', ';
j=1; // keep track of how many non-null values seen
for (i=0;i<arguments.length;++i) {
var arg = arguments[i];
if (arg !== '') {
if (j > 1) {
result += joiner + arg;
}
else {
result += arg;
}
++j;
}
}
return result;
}
/**
* Returns concatenates list with first argument as the separator
*/
function LEMimplode()
{
// takes variable number of arguments
var result="";
if (arguments.length <= 1) {
return "";
}
var joiner = arguments[0];
for (i=1;i<arguments.length;++i) {
var arg = arguments[i];
if (i > 1) {
result += joiner + arg;
}
else {
result += arg;
}
}
return result;
}
/*
* Returns true if within matches the pattern. Pattern must start and end with the '/' character
*/
function LEMregexMatch(pattern,within)
{
try {
var reg = new RegExp(pattern.substr(1,pattern.length-2));
return reg.test(within);
}
catch (err) {
return false;
}
}
function LEMstrlen(a)
{
var str = new String(a);
return str.length;
}
function LEMstr_replace(needle, replace, haystack)
{
var str = new String(haystack);
return str.replace(needle, replace);
}
function LEMstrpos(haystack,needle)
{
var str = new String(haystack);
return str.search(needle);
}
function LEMempty(v)
{
if (v === "" || v === false) {
return true;
}
return false;
}
function LEMbool(v)
{
bool = new Boolean(v);
if (v.valueOf() && v != 'false') {
return true; // fix for JavaScript native processing that considers the value "false" to be true
}
return false;
}
/**
* Match even if different types - especially true vs. 1, and false vs. 0 or blank
*/
function LEMeq(a,b)
{
if ((a==="true" && b==="1") || (a==="1" && b==="true")) {
return true;
}
if ((a==="false" && (b==="0" || b==="")) || ((a==="0" || a==="") && b ==="false")) {
return true;
}
return a==b;
}
/**
* Return the value for data element jsName, treating it as blank if its question is irrelevant on the current page.
* Also convert the string 'false' to '' to cope with a JavaScript type casting issue
*/
function LEMval(alias)
{
// first find out whether it is using a suffix
var str = new String(alias);
var varName = alias;
var suffix = 'code'; // the default
if (str.match(/^INSERTANS:/)) {
suffix = 'shown';
varName = varName.substr(10);
}
else if (str.match(/\.(code|gid|grelevance|gseq|jsName|mandatory|NAOK|qid|qseq|question|readWrite|relevanceStatus|relevance|rowdivid|sgqa|shown|type|valueNAOK|value)$/)) {
varName = str.replace(/\.(code|gid|grelevance|gseq|jsName|mandatory|NAOK|qid|qseq|question|readWrite|relevanceStatus|relevance|rowdivid|sgqa|shown|type|valueNAOK|value)$/,'')
suffix = str.replace(/^(.+)\./,'');
}
jsName = LEMalias2varName[varName];
attr = LEMvarNameAttr[jsName];
if ((suffix.match(/^code|NAOK|shown|valueNAOK|value$/)) && attr.qid!='') {
if (!LEMval(varName + '.relevanceStatus')) {
return '';
}
}
var whichJsName; // correct name whether on- or off-page
if (LEMmode=='survey' || (LEMmode=='group' && attr.gseq == LEMgseq) || (LEMmode=='question' && attr.qid == LEMqid)) {
whichJsName = (typeof attr.jsName_on === 'undefined') ? attr.jsName : attr.jsName_on;
}
else {
whichJsName = attr.jsName;
}
if (whichJsName === null || typeof document.getElementById(whichJsName) === 'undefined' || document.getElementById(whichJsName) === null) {
an_error = true; // this line is here to make debugging easier
return '';
}
// values should always be stored encoded with htmlspecialchars()
switch (suffix) {
case 'relevanceStatus': {
grel = qrel = sgqarel = 1;
if (!(typeof attr.gseq === 'undefined') && !(document.getElementById('relevanceG' + attr.gseq) === null)) {
grel = parseInt(document.getElementById('relevanceG' + attr.gseq).value);
}
if (!(typeof attr.qid === 'undefined') && !(document.getElementById('relevance' + attr.qid) === null)) {
qrel = parseInt(document.getElementById('relevance' + attr.qid).value);
}
if (!(typeof attr.rowdivid === 'undefined') && !(document.getElementById('relevance' + attr.rowdivid) === null)) {
sgqarel = parseInt(document.getElementById('relevance' + attr.rowdivid).value);
}
return (grel && qrel && sgqarel);
}
case 'shown': {
value = htmlspecialchars_decode(document.getElementById(whichJsName).value);
switch(attr.type)
{
case 'G': //GENDER drop-down list
case 'Y': //YES/NO radio-buttons
case 'C': //ARRAY (YES/UNCERTAIN/NO) radio-buttons
case 'E': //ARRAY (Increase/Same/Decrease) radio-buttons
shown = (typeof attr.answers[value] === 'undefined') ? '' : attr.answers[value];
break;
case '!': //List - dropdown
case 'L': //LIST drop-down/radio-button list
case 'O': //LIST WITH COMMENT drop-down/radio-button list + textarea
case 'H': //ARRAY (Flexible) - Column Format
case 'F': //ARRAY (Flexible) - Row Format
case 'R': //RANKING STYLE
if (attr.type == 'O' && varName.match(/comment$/)) {
answer = value;
}
else if ((attr.type == 'L' || attr.type == '!') && varName.match(/_other$/)) {
answer = value;
}
else {
which_ans = '0~' + value;
if (typeof attr.answers[which_ans] === 'undefined') {
answer = value;
}
else {
answerParts = attr.answers[which_ans].split('|');
answerParts.shift(); // remove the first element
answer = answerParts.join('|');
}
}
shown = answer;
break;
case '1': //Array (Flexible Labels) dual scale // need scale
prefix = (attr.jsName.match(/#1$/)) ? '1' : '0';
which_ans = prefix + '~' + value;
if (typeof attr.answers[which_ans] === 'undefined') {
answer = '';
}
else {
answerParts = attr.answers[which_ans].split('|');
answerParts.shift(); // remove the first element
answer = answerParts.join('|');
}
shown = answer;
break;
case 'A': //ARRAY (5 POINT CHOICE) radio-buttons
case 'B': //ARRAY (10 POINT CHOICE) radio-buttons
case ':': //ARRAY (Multi Flexi) 1 to 10
case '5': //5 POINT CHOICE radio-buttons
case 'N': //NUMERICAL QUESTION TYPE
case 'K': //MULTIPLE NUMERICAL QUESTION
case 'Q': //MULTIPLE SHORT TEXT
case ';': //ARRAY (Multi Flexi) Text
case 'S': //SHORT FREE TEXT
case 'T': //LONG FREE TEXT
case 'U': //HUGE FREE TEXT
case 'D': //DATE
case '*': //Equation
case 'I': //Language Question
case '|': //File Upload
case 'X': //BOILERPLATE QUESTION
shown = value; // what about "no answer"?
break;
case 'M': //Multiple choice checkbox
case 'P': //Multiple choice with comments checkbox + text
if (typeof attr.question === 'undefined' || value == '') {
shown = '';
}
else {
if (attr.type == 'P' && varName.match(/comment$/)) {
shown = value;
}
else {
shown = htmlspecialchars_decode(attr.question);
}
}
break;
}
}
return htmlspecialchars_decode(shown);
case 'gid':
return attr.gid;
case 'grelevance':
return attr.grelevance;
case 'mandatory':
return attr.mandatory;
case 'qid':
return attr.qid;
case 'question':
return htmlspecialchars_decode(attr.question);
case 'readWrite':
return attr.readWrite;
case 'relevance':
return htmlspecialchars_decode(attr.relevance);
case 'sgqa':
return attr.sgqa;
case 'type':
return attr.type;
case 'gseq':
return attr.gseq;
case 'qseq':
return attr.qseq;
case 'jsName':
return whichJsName;
case 'code':
case 'NAOK':
case 'value':
case 'valueNAOK':
{
value = htmlspecialchars_decode(document.getElementById(whichJsName).value);
if (value === '') {
return '';
}
if (suffix == 'value' || suffix == 'valueNAOK') {
// if in assessment mode, this returns the assessment value
// in non-assessment mode, this is identical to .code
switch (attr.type) {
case '!': //List - dropdown
case 'L': //LIST drop-down/radio-button list
case 'O': //LIST WITH COMMENT drop-down/radio-button list + textarea
case 'H': //ARRAY (Flexible) - Column Format
case 'F': //ARRAY (Flexible) - Row Format
case 'R': //RANKING STYLE
if (attr.type == 'O' && varName.match(/comment$/)) {
// value = value;
}
else if ((attr.type == 'L' || attr.type == '!') && varName.match(/_other$/)) {
// value = value;
}
else {
which_ans = '0~' + value;
if (typeof attr.answers[which_ans] === 'undefined') {
value = '';
}
else {
answerParts = attr.answers[which_ans].split('|');
value = answerParts[0];
}
}
break;
case '1': //Array (Flexible Labels) dual scale // need scale
prefix = (attr.jsName.match(/#1$/)) ? '1' : '0';
which_ans = prefix + '~' + value;
if (typeof attr.answers[which_ans] === 'undefined') {
value = '';
}
else {
answerParts = attr.answers[which_ans].split('|');
value = answerParts[0];
}
break;
}
}
if (typeof attr.onlynum !== 'undefined' && attr.onlynum==1) {
newval = value;
if (LEMradix === ',') {
newval = value.split(',').join('.');
}
if (newval != parseFloat(newval)) {
newval = '';
}
return +newval;
}
else if (isNaN(value)) {
if (value==='false') {
return ''; // so Boolean operations will treat it as false. In JavaScript, Boolean("false") is true since "false" is not a zero-length string
}
return value;
}
else {
return +value; // convert it to numeric return type
}
}
case 'rowdivid':
if (typeof attr.rowdivid === 'undefined' || attr.rowdivid == '') {
return '';
}
return attr.rowdivid;
default:
return 'Unknown Attribute: ' . suffix;
}
}
/** Display number with comma as radix separator, if needed
*/
function LEMfixnum(value)
{
var newval = String(value);
if (parseFloat(newval) != value) {
return value; // unchanged
}
if (LEMradix===',') {
newval = newval.split('.').join(',');
return newval;
}
return value;
}
/*
* Remove HTML and PHP tags from string
*/
function LEMstrip_tags(htmlString)
{
var tmp = document.createElement("DIV");
tmp.innerHTML = htmlString;
return tmp.textContent||tmp.innerText;
}
/**
* Compute sample standard deviation of the input variables
*/
function LEMstddev()
{
vals = new Array();
j = 0;
for (i=0;i<arguments.length;++i) {
if (LEMis_numeric(arguments[i])) {
vals[j++] = arguments[i];
}
}
count = vals.length;
if (count <= 1) {
return 0; // what should default value be?
}
sum = 0;
for (i=0;i<vals.length;++i) {
sum += vals[i];
}
mean = sum / count;
sumsqmeans = 0;
for (i=0;i<vals.length;++i) {
sumsqmeans += (vals[i] - mean) * (vals[i] - mean);
}
stddev = Math.sqrt(sumsqmeans / (count-1));
return stddev;
}
function LEMstrtoupper(s)
{
return s.toUpperCase();
}
function LEMstrtolower(s)
{
return s.toLowerCase();
}
/*
* return true if any of the arguments are not relevant
*/
function LEManyNA()
{
for (i=0;i<arguments.length;++i) {
var arg = arguments[i];
if (arg.match(/\.NAOK$/)) {
continue;
}
if (typeof LEMalias2varName[arg] === 'undefined') {
continue; // default is OK (e.g. for questions with dot notation suffix
}
jsName = LEMalias2varName[arg];
if (typeof LEMvarNameAttr[jsName] === 'undefined') {
continue; // default is OK (e.g. for questions with dot notation suffix)
}
attr = LEMvarNameAttr[jsName];
if (!LEMval(attr.sgqa + '.relevanceStatus')) {
return true;
}
}
return false;
}
/** Set the tabIndex for all potentially visible form elements, and capture the TAB and SHIFT-TAB keys so can
* control navigation when elements appear and disappear.
*/
function LEMsetTabIndexes()
{
if (typeof tabIndexesSet == 'undefined') {
$('#limesurvey :input[type!=hidden][id!=runonce]').each(function(index){
$(this).bind('keydown',function(e) {
if (e.keyCode == 9) {
checkconditions($(this).attr('value'), $(this).attr('name'), $(this).attr('type'), 'TAB');
$(this).focus();
return true;
}
return true;
})
}) // MUST DO THIS FIRST
tabIndexesSet = true;
}
}
/** Function to set color coding for Other option in multiple choice questions so that it gets filled in if selected
*/
function LEMflagMandOther(sgqa,checked)
{
if (checked) {
if ($.trim($('#java'+sgqa).val()) == '') {
$('#answer'+sgqa).addClass('em_sq_validation error').removeClass('good');
}
else {
$('#answer'+sgqa).addClass('em_sq_validation good').removeClass('error');
}
}
else {
$('#answer'+sgqa).addClass('em_sq_validation good').removeClass('error');
}
}
/* The following functions are courtesy of phpjs.org */
/*
* More info at: http://phpjs.org
*
* This is version: 3.26
* php.js is copyright 2011 Kevin van Zonneveld.
*
* Portions copyright Brett Zamir (http://brett-zamir.me), Kevin van Zonneveld
* (http://kevin.vanzonneveld.net), Onno Marsman, Theriault, Michael White
* (http://getsprink.com), Waldo Malqui Silva, Paulo Freitas, Jack, Jonas
* Raoni Soares Silva (http://www.jsfromhell.com), Philip Peterson, Legaev
* Andrey, Ates Goral (http://magnetiq.com), Alex, Ratheous, Martijn Wieringa,
* Rafal Kukawski (http://blog.kukawski.pl), lmeyrick
* (https://sourceforge.net/projects/bcmath-js/), Nate, Philippe Baumann,
* Enrique Gonzalez, Webtoolkit.info (http://www.webtoolkit.info/), Carlos R.
* L. Rodrigues (http://www.jsfromhell.com), Ash Searle
* (http://hexmen.com/blog/), Jani Hartikainen, travc, Ole Vrijenhoek,
* Erkekjetter, Michael Grier, Rafal Kukawski (http://kukawski.pl), Johnny
* Mast (http://www.phpvrouwen.nl), T.Wild, d3x,
* http://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript,
* Rafal Kukawski (http://blog.kukawski.pl/), stag019, pilus, WebDevHobo
* (http://webdevhobo.blogspot.com/), marrtins, GeekFG
* (http://geekfg.blogspot.com), Andrea Giammarchi
* (http://webreflection.blogspot.com), Arpad Ray (mailto:arpad@php.net),
* gorthaur, Paul Smith, Tim de Koning (http://www.kingsquare.nl), Joris, Oleg
* Eremeev, Steve Hilder, majak, gettimeofday, KELAN, Josh Fraser
* (http://onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/),
* Marc Palau, Kevin van Zonneveld (http://kevin.vanzonneveld.net/), Martin
* (http://www.erlenwiese.de/), Breaking Par Consulting Inc
* (http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256CFB006C45F7),
* Chris, Mirek Slugen, saulius, Alfonso Jimenez
* (http://www.alfonsojimenez.com), Diplom@t (http://difane.com/), felix,
* Mailfaker (http://www.weedem.fr/), Tyler Akins (http://rumkin.com), Caio
* Ariede (http://caioariede.com), Robin, Kankrelune
* (http://www.webfaktory.info/), Karol Kowalski, Imgen Tata
* (http://www.myipdf.com/), mdsjack (http://www.mdsjack.bo.it), Dreamer,
* Felix Geisendoerfer (http://www.debuggable.com/felix), Lars Fischer, AJ,
* David, Aman Gupta, Michael White, Public Domain
* (http://www.json.org/json2.js), Steven Levithan
* (http://blog.stevenlevithan.com), Sakimori, Pellentesque Malesuada,
* Thunder.m, Dj (http://phpjs.org/functions/htmlentities:425#comment_134018),
* Steve Clay, David James, Francois, class_exists, nobbler, T. Wild, Itsacon
* (http://www.itsacon.net/), date, Ole Vrijenhoek (http://www.nervous.nl/),
* Fox, Raphael (Ao RUDLER), Marco, noname, Mateusz "loonquawl" Zalega, Frank
* Forte, Arno, ger, mktime, john (http://www.jd-tech.net), Nick Kolosov
* (http://sammy.ru), marc andreu, Scott Cariss, Douglas Crockford
* (http://javascript.crockford.com), madipta, Slawomir Kaniecki,
* ReverseSyntax, Nathan, Alex Wilson, kenneth, Bayron Guevara, Adam Wallner
* (http://web2.bitbaro.hu/), paulo kuong, jmweb, Lincoln Ramsay, djmix,
* Pyerre, Jon Hohle, Thiago Mata (http://thiagomata.blog.com), lmeyrick
* (https://sourceforge.net/projects/bcmath-js/this.), Linuxworld, duncan,
* Gilbert, Sanjoy Roy, Shingo, sankai, Oskar Larsson Högfeldt
* (http://oskar-lh.name/), Denny Wardhana, 0m3r, Everlasto, Subhasis Deb,
* josh, jd, Pier Paolo Ramon (http://www.mastersoup.com/), P, merabi, Soren
* Hansen, Eugene Bulkin (http://doubleaw.com/), Der Simon
* (http://innerdom.sourceforge.net/), echo is bad, Ozh, XoraX
* (http://www.xorax.info), EdorFaus, JB, J A R, Marc Jansen, Francesco, LH,
* Stoyan Kyosev (http://www.svest.org/), nord_ua, omid
* (http://phpjs.org/functions/380:380#comment_137122), Brad Touesnard, MeEtc
* (http://yass.meetcweb.com), Peter-Paul Koch
* (http://www.quirksmode.org/js/beat.html), Olivier Louvignes
* (http://mg-crea.com/), T0bsn, Tim Wiel, Bryan Elliott, Jalal Berrami,
* Martin, JT, David Randall, Thomas Beaucourt (http://www.webapp.fr), taith,
* vlado houba, Pierre-Luc Paour, Kristof Coomans (SCK-CEN Belgian Nucleair
* Research Centre), Martin Pool, Kirk Strobeck, Rick Waldron, Brant Messenger
* (http://www.brantmessenger.com/), Devan Penner-Woelk, Saulo Vallory, Wagner
* B. Soares, Artur Tchernychev, Valentina De Rosa, Jason Wong
* (http://carrot.org/), Christoph, Daniel Esteban, strftime, Mick@el, rezna,
* Simon Willison (http://simonwillison.net), Anton Ongson, Gabriel Paderni,
* Marco van Oort, penutbutterjelly, Philipp Lenssen, Bjorn Roesbeke
* (http://www.bjornroesbeke.be/), Bug?, Eric Nagel, Tomasz Wesolowski,
* Evertjan Garretsen, Bobby Drake, Blues (http://tech.bluesmoon.info/), Luke
* Godfrey, Pul, uestla, Alan C, Ulrich, Rafal Kukawski, Yves Sucaet,
* sowberry, Norman "zEh" Fuchs, hitwork, Zahlii, johnrembo, Nick Callen,
* Steven Levithan (stevenlevithan.com), ejsanders, Scott Baker, Brian Tafoya
* (http://www.premasolutions.com/), Philippe Jausions
* (http://pear.php.net/user/jausions), Aidan Lister
* (http://aidanlister.com/), Rob, e-mike, HKM, ChaosNo1, metjay, strcasecmp,
* strcmp, Taras Bogach, jpfle, Alexander Ermolaev
* (http://snippets.dzone.com/user/AlexanderErmolaev), DxGx, kilops, Orlando,
* dptr1988, Le Torbi, James (http://www.james-bell.co.uk/), Pedro Tainha
* (http://www.pedrotainha.com), James, Arnout Kazemier
* (http://www.3rd-Eden.com), Chris McMacken, gabriel paderni, Yannoo,
* FGFEmperor, baris ozdil, Tod Gentille, Greg Frazier, jakes, 3D-GRAF, Allan
* Jensen (http://www.winternet.no), Howard Yeend, Benjamin Lupton, davook,
* daniel airton wermann (http://wermann.com.br), Atli Þór, Maximusya, Ryan
* W Tenney (http://ryan.10e.us), Alexander M Beedie, fearphage
* (http://http/my.opera.com/fearphage/), Nathan Sepulveda, Victor, Matteo,
* Billy, stensi, Cord, Manish, T.J. Leahy, Riddler
* (http://www.frontierwebdev.com/), Rafal Kukawski, FremyCompany, Matt
* Bradley, Tim de Koning, Luis Salazar (http://www.freaky-media.com/), Diogo
* Resende, Rival, Andrej Pavlovic, Garagoth, Le Torbi
* (http://www.letorbi.de/), Dino, Josep Sanz (http://www.ws3.es/), rem,
* Russell Walker (http://www.nbill.co.uk/), Jamie Beck
* (http://www.terabit.ca/), setcookie, Michael, YUI Library:
* http://developer.yahoo.com/yui/docs/YAHOO.util.DateLocale.html, Blues at
* http://hacks.bluesmoon.info/strftime/strftime.js, Ben
* (http://benblume.co.uk/), DtTvB
* (http://dt.in.th/2008-09-16.string-length-in-bytes.html), Andreas, William,
* meo, incidence, Cagri Ekin, Amirouche, Amir Habibi
* (http://www.residence-mixte.com/), Luke Smith (http://lucassmith.name),
* Kheang Hok Chin (http://www.distantia.ca/), Jay Klehr, Lorenzo Pisani,
* Tony, Yen-Wei Liu, Greenseed, mk.keck, Leslie Hoare, dude, booeyOH, Ben
* Bryan
*
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL KEVIN VAN ZONNEVELD BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
function is_bool (mixed_var) {
// Returns true if variable is a boolean
//
// version: 1107.2516
// discuss at: http://phpjs.org/functions/is_bool
// + original by: Onno Marsman
// * example 1: is_bool(false);
// * returns 1: true
// * example 2: is_bool(0);
// * returns 2: false
return (typeof mixed_var === 'boolean');
}
function addslashes (str) {
// Escapes single quote, double quotes and backslash characters in a string with backslashes
//
// version: 1107.2516
// discuss at: http://phpjs.org/functions/addslashes
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Ates Goral (http://magnetiq.com)
// + improved by: marrtins
// + improved by: Nate
// + improved by: Onno Marsman
// + input by: Denny Wardhana
// + improved by: Brett Zamir (http://brett-zamir.me)
// + improved by: Oskar Larsson Högfeldt (http://oskar-lh.name/)
// * example 1: addslashes("kevin's birthday");
// * returns 1: 'kevin\'s birthday'
return (str + '').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
}
function html_entity_decode (string, quote_style) {
// Convert all HTML entities to their applicable characters
//
// version: 1107.2516
// discuss at: http://phpjs.org/functions/html_entity_decode
// + original by: john (http://www.jd-tech.net)
// + input by: ger
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Onno Marsman
// + improved by: marc andreu
// + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: Ratheous
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// + input by: Nick Kolosov (http://sammy.ru)
// + bugfixed by: Fox
// - depends on: get_html_translation_table
// * example 1: html_entity_decode('Kevin & van Zonneveld');
// * returns 1: 'Kevin & van Zonneveld'
// * example 2: html_entity_decode('&lt;');
// * returns 2: '<'
var hash_map = {},
symbol = '',
tmp_str = '',
entity = '';
tmp_str = string.toString();
if (false === (hash_map = this.get_html_translation_table('HTML_ENTITIES', quote_style))) {
return false;
}
// fix & problem
// http://phpjs.org/functions/get_html_translation_table:416#comment_97660
delete(hash_map['&']);
hash_map['&'] = '&';
for (symbol in hash_map) {
entity = hash_map[symbol];
tmp_str = tmp_str.split(entity).join(symbol);
}
tmp_str = tmp_str.split(''').join("'");
return tmp_str;
}
function htmlentities (string, quote_style, charset, double_encode) {
// Convert all applicable characters to HTML entities
//
// version: 1107.2516
// discuss at: http://phpjs.org/functions/htmlentities
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: nobbler
// + tweaked by: Jack
// + bugfixed by: Onno Marsman
// + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// + input by: Ratheous
// + improved by: Rafał Kukawski (http://blog.kukawski.pl)
// - depends on: get_html_translation_table
// * example 1: htmlentities('Kevin & van Zonneveld');
// * returns 1: 'Kevin & van Zonneveld'
// * example 2: htmlentities("foo'bar","ENT_QUOTES");
// * returns 2: 'foo'bar'
var hash_map = {},
symbol = '',
entity = '',
self = this;
string += '';
double_encode = !!double_encode || double_encode == null;
if (false === (hash_map = this.get_html_translation_table('HTML_ENTITIES', quote_style))) {
return false;
}
hash_map["'"] = ''';
if (double_encode) {
for (symbol in hash_map) {
entity = hash_map[symbol];
string = string.split(symbol).join(entity);
}
} else {
string = string.replace(/([\s\S]*?)(&(?:#\d+|#x[\da-f]+|[a-z][\da-z]*);|$)/g, function (ignore, text, entity) {
return self.htmlentities(text, quote_style, charset) + entity;
});
}
return string;
}
function get_html_translation_table (table, quote_style) {
// Returns the internal translation table used by htmlspecialchars and htmlentities
//
// version: 1107.2516
// discuss at: http://phpjs.org/functions/get_html_translation_table
// + original by: Philip Peterson
// + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: noname
// + bugfixed by: Alex
// + bugfixed by: Marco
// + bugfixed by: madipta
// + improved by: KELAN
// + improved by: Brett Zamir (http://brett-zamir.me)
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// + input by: Frank Forte
// + bugfixed by: T.Wild
// + input by: Ratheous
// % note: It has been decided that we're not going to add global
// % note: dependencies to php.js, meaning the constants are not
// % note: real constants, but strings instead. Integers are also supported if someone
// % note: chooses to create the constants themselves.
// * example 1: get_html_translation_table('HTML_SPECIALCHARS');
// * returns 1: {'"': '"', '&': '&', '<': '<', '>': '>'}
var entities = {},
hash_map = {},
decimal = 0,
symbol = '';
var constMappingTable = {},
constMappingQuoteStyle = {};
var useTable = {},
useQuoteStyle = {};
// Translate arguments
constMappingTable[0] = 'HTML_SPECIALCHARS';
constMappingTable[1] = 'HTML_ENTITIES';
constMappingQuoteStyle[0] = 'ENT_NOQUOTES';
constMappingQuoteStyle[2] = 'ENT_COMPAT';
constMappingQuoteStyle[3] = 'ENT_QUOTES';
useTable = !isNaN(table) ? constMappingTable[table] : table ? table.toUpperCase() : 'HTML_SPECIALCHARS';
useQuoteStyle = !isNaN(quote_style) ? constMappingQuoteStyle[quote_style] : quote_style ? quote_style.toUpperCase() : 'ENT_COMPAT';
if (useTable !== 'HTML_SPECIALCHARS' && useTable !== 'HTML_ENTITIES') {
throw new Error("Table: " + useTable + ' not supported');
// return false;
}
entities['38'] = '&';
if (useTable === 'HTML_ENTITIES') {
entities['160'] = ' ';
entities['161'] = '¡';
entities['162'] = '¢';
entities['163'] = '£';
entities['164'] = '¤';
entities['165'] = '¥';
entities['166'] = '¦';
entities['167'] = '§';
entities['168'] = '¨';
entities['169'] = '©';
entities['170'] = 'ª';
entities['171'] = '«';
entities['172'] = '¬';
entities['173'] = '­';
entities['174'] = '®';
entities['175'] = '¯';
entities['176'] = '°';
entities['177'] = '±';
entities['178'] = '²';
entities['179'] = '³';
entities['180'] = '´';
entities['181'] = 'µ';
entities['182'] = '¶';
entities['183'] = '·';
entities['184'] = '¸';
entities['185'] = '¹';
entities['186'] = 'º';
entities['187'] = '»';
entities['188'] = '¼';
entities['189'] = '½';
entities['190'] = '¾';
entities['191'] = '¿';
entities['192'] = 'À';
entities['193'] = 'Á';
entities['194'] = 'Â';
entities['195'] = 'Ã';
entities['196'] = 'Ä';
entities['197'] = 'Å';
entities['198'] = 'Æ';
entities['199'] = 'Ç';
entities['200'] = 'È';
entities['201'] = 'É';
entities['202'] = 'Ê';
entities['203'] = 'Ë';
entities['204'] = 'Ì';
entities['205'] = 'Í';
entities['206'] = 'Î';
entities['207'] = 'Ï';
entities['208'] = 'Ð';
entities['209'] = 'Ñ';
entities['210'] = 'Ò';
entities['211'] = 'Ó';
entities['212'] = 'Ô';
entities['213'] = 'Õ';
entities['214'] = 'Ö';
entities['215'] = '×';
entities['216'] = 'Ø';
entities['217'] = 'Ù';
entities['218'] = 'Ú';
entities['219'] = 'Û';
entities['220'] = 'Ü';
entities['221'] = 'Ý';
entities['222'] = 'Þ';
entities['223'] = 'ß';
entities['224'] = 'à';
entities['225'] = 'á';
entities['226'] = 'â';
entities['227'] = 'ã';
entities['228'] = 'ä';
entities['229'] = 'å';
entities['230'] = 'æ';
entities['231'] = 'ç';
entities['232'] = 'è';
entities['233'] = 'é';
entities['234'] = 'ê';
entities['235'] = 'ë';
entities['236'] = 'ì';
entities['237'] = 'í';
entities['238'] = 'î';
entities['239'] = 'ï';
entities['240'] = 'ð';
entities['241'] = 'ñ';
entities['242'] = 'ò';
entities['243'] = 'ó';
entities['244'] = 'ô';
entities['245'] = 'õ';
entities['246'] = 'ö';
entities['247'] = '÷';
entities['248'] = 'ø';
entities['249'] = 'ù';
entities['250'] = 'ú';
entities['251'] = 'û';
entities['252'] = 'ü';
entities['253'] = 'ý';
entities['254'] = 'þ';
entities['255'] = 'ÿ';
}
if (useQuoteStyle !== 'ENT_NOQUOTES') {
entities['34'] = '"';
}
if (useQuoteStyle === 'ENT_QUOTES') {
entities['39'] = ''';
}
entities['60'] = '<';
entities['62'] = '>';
// ascii decimals to real symbols
for (decimal in entities) {
symbol = String.fromCharCode(decimal);
hash_map[symbol] = entities[decimal];
}
return hash_map;
}
function htmlspecialchars (string, quote_style, charset, double_encode) {
// Convert special characters to HTML entities
//
// version: 1107.2516
// discuss at: http://phpjs.org/functions/htmlspecialchars
// + original by: Mirek Slugen
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Nathan
// + bugfixed by: Arno
// + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// + input by: Ratheous
// + input by: Mailfaker (http://www.weedem.fr/)
// + reimplemented by: Brett Zamir (http://brett-zamir.me)
// + input by: felix
// + bugfixed by: Brett Zamir (http://brett-zamir.me)