This repository has been archived by the owner on Mar 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path_SassyJSON.scss
1002 lines (860 loc) · 30.8 KB
/
_SassyJSON.scss
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
/*! sassyjson - v1.1.8 - 2015-09-24 */
/// Logs an error at `$pointer` with `$string` message
/// @access private
/// @param {String} $string - error message
/// @param {Number} $pointer - pointer position
@function _throw($string, $pointer) {
@error "ERROR::#{$pointer}::#{$string}";
}
/// Delay parsing to type-specific function
/// @access private
/// according to found character
/// @param {String} $source - JSON complete source
/// @param {Number} $pointer - current pointer
/// @throw Unexpected token $token.
/// @throw Empty JSON string.
/// @return {List} - (new pointer, parsed value)
/// @require {function} _throw
/// @require {function} _json-decode--map
/// @require {function} _json-decode--list
/// @require {function} _json-decode--true
/// @require {function} _json-decode--false
/// @require {function} _json-decode--string
/// @require {function} _json-decode--null
/// @require {function} _json-decode--number
@function _json-decode--value($source, $pointer) {
$length: str-length($source);
@while $pointer <= $length {
$token: str-slice($source, $pointer, $pointer);
$pointer: $pointer + 1;
@if $token == '{' {
@return _json-decode--map($source, $pointer);
} @else if $token == '[' {
@return _json-decode--list($source, $pointer);
} @else if $token == 't' {
@return _json-decode--true($source, $pointer);
} @else if $token == 'f' {
@return _json-decode--false($source, $pointer);
} @else if $token == '"' {
@return _json-decode--string($source, $pointer);
} @else if $token == 'n' {
@return _json-decode--null($source, $pointer);
} @else if index('1' '2' '3' '4' '5' '6' '7' '8' '9' '0' '-' '.', $token) {
@return _json-decode--number($source, $pointer);
} @else if $token == ' ' or $token == " " {
// @continue;
} @else {
@return _throw("Unexpected token `#{$token}`.", $pointer);
}
}
@return _throw("Empty JSON string.", $pointer);
}
/// Move pointer to position of token
/// @access private
/// @param {String} $source - JSON complete source
/// @param {Number} $pointer - current pointer
/// @param {String} $token - token to reach
/// @throw Expected $token; found $char.
/// @throw Expected $token but reached end of stream.
/// @return {Number} - new pointer
/// @require {function} _throw
@function _consume($source, $pointer, $token) {
$length: str-length($source);
@while $pointer <= $length {
$char: str-slice($source, $pointer, $pointer);
$pointer: $pointer + 1;
@if $char == $token {
@return $pointer;
} @else if $char == " " or $char == " " {
// @continue;
} @else {
@return _throw("Expected `#{$token}`; found `#{$char}`.", $pointer);
}
}
@return _throw("Expected `#{$token}` but reached end of stream.", $pointer);
}
/// Will find the first non escaped quote in a JSON String
/// @access private
/// @param {String} $string - to search in
/// @return {Number} - position of the first non escaped quote
@function _find-ending-quote($string) {
$backslash: str-slice('\\', 1, 1); // Dirty hack to have a single backslash
$escaped-chars: '"\/bfnrtu'; // Characters that can be escaped in JSON
$hexadecimal-chars: '0' '1' '2' '3' '4' '5' '6' '7' '8' '9' '0' 'a' 'b' 'c' 'd' 'e' 'f';
$pos: 1;
$length: str-length($string);
$backslash-found: false;
@while $pos <= $length {
$char: to-lower-case(str-slice($string, $pos, $pos));
// Previous char was a backslash
@if $backslash-found {
// Special case, the 'u' character
@if $char == 'u' {
// Next 4 characters must be hexadecimal
@if not index($hexadecimal-chars, str-slice($string, $pos + 1, $pos + 1)) or
not index($hexadecimal-chars, str-slice($string, $pos + 2, $pos + 2)) or
not index($hexadecimal-chars, str-slice($string, $pos + 3, $pos + 3)) or
not index($hexadecimal-chars, str-slice($string, $pos + 4, $pos + 4)) {
@return 0;
}
$pos: $pos + 4;
} @else if not str-index($escaped-chars, $char) {
// Invalid character escaped
@return 0;
}
$backslash-found: false;
} @else if $char == $backslash {
$backslash-found: true;
} @else if $char == '"' {
// Unescaped quote found
@return $pos;
}
$pos: $pos + 1;
}
// No end of string found
@return 0;
}
/// Strip special carriage return characters
/// @access private
/// @param {String} $string - string to parse
/// @param {String} $search - char to strip
/// @param {String} $replace ('') - new substring
/// @return {String} - new string
@function _strip-token($string, $search, $replace: '') {
$index: str-index($string, $search);
@if $index {
@return str-slice($string, 1, $index - 1) + $replace + _strip-token(str-slice($string, $index + str-length($search)), $search, $replace);
}
@return $string;
}
/// Parses a JSON encoded string to see if it's a CSS length
/// @access private
/// @param {String} $string - JSON string
/// @return {Number | String} - string or number, depending on the match
/// @require {function} _json-decode--number
@function _length($string) {
@if type-of($string) == "number" {
@return $string;
}
$strings: 'px' 'cm' 'mm' '%' 'ch' 'pica' 'in' 'em' 'rem' 'pt' 'pc' 'ex' 'vw' 'vh' 'vmin' 'vmax';
$units: 1px 1cm 1mm 1% 1ch 1pica 1in 1em 1rem 1pt 1pc 1ex 1vw 1vh 1vmin 1vmax;
$number: "";
$unit: "";
@for $i from 1 through str-length($string) {
$c: str-slice($string, $i, $i);
@if $c == ' ' or $c == " " {
@if $number != "" {
@return $string;
}
} @else if index('0' '1' '2' '3' '4' '5' '6' '7' '8' '9' '-' '.', $c) {
$number: $number + $c;
} @else {
@if $number == "" {
@return $string;
}
$unit: $unit + $c;
}
}
$number: nth(_json-decode--number($number, 2), 2);
$index: index($strings, to-lower-case($unit));
@if $index and $index > 0 {
@return $number * nth($units, $index);
}
@return $string;
}
/// Parses a JSON encoded string to see if it's a CSS color
/// @access private
/// @param {String} $string - JSON string
/// @return {Color | String} - string or number, depending on the match
/// @require {function} _from-hex
/// @require {function} _from-rgb
/// @require {function} _from-hsl
@function _color($string) {
@if type-of($string) == "color" {
@return $string;
}
$string-lower: to-lower-case($string);
$colors: transparent black silver gray white maroon red purple fuchsia green lime olive yellow navy blue teal aqua aliceblue antiquewhite aqua aquamarine azure beige bisque black blanchedalmond blue blueviolet brown burlywood cadetblue chartreuse chocolate coral cornflowerblue cornsilk crimson cyan darkblue darkcyan darkgoldenrod darkgray darkgreen darkgrey darkkhaki darkmagenta darkolivegreen darkorange darkorchid darkred darksalmon darkseagreen darkslateblue darkslategray darkslategrey darkturquoise darkviolet deeppink deepskyblue dimgray dimgrey dodgerblue firebrick floralwhite forestgreen fuchsia gainsboro ghostwhite gold goldenrod gray green greenyellow grey honeydew hotpink indianred indigo ivory khaki lavender lavenderblush lawngreen lemonchiffon lightblue lightcoral lightcyan lightgoldenrodyellow lightgray lightgreen lightgrey lightpink lightsalmon lightseagreen lightskyblue lightslategray lightslategrey lightsteelblue lightyellow lime limegreen linen magenta maroon mediumaquamarine mediumblue mediumorchid mediumpurple mediumseagreen mediumslateblue mediumspringgreen mediumturquoise mediumvioletred midnightblue mintcream mistyrose moccasin navajowhite navy oldlace olive olivedrab orange orangered orchid palegoldenrod palegreen paleturquoise palevioletred papayawhip peachpuff peru pink plum powderblue purple red rosybrown royalblue saddlebrown salmon sandybrown seagreen seashell sienna silver skyblue slateblue slategray slategrey snow springgreen steelblue tan teal thistle tomato turquoise violet wheat white whitesmoke yellow yellowgreen;
$keywords: ();
// Filling $keywords with stringified color keywords
@each $color in $colors {
$keywords: append($keywords, $color + "");
}
// Deal with inherit keyword
@if $string-lower == "inherit" {
@return unquote($string);
}
@if index($keywords, $string-lower) {
// Deal with color keywords
@return nth($colors, index($keywords, $string-lower));
} @else if str-slice($string-lower, 1, 1) == '#' {
// Deal with hexadecimal triplets
@return _from-hex($string);
} @else if str-slice($string-lower, 1, 3) == 'rgb' {
// Deal with rgb(a) colors
@return _from-rgb($string);
} @else if str-slice($string-lower, 1, 3) == 'hsl' {
// Deal with hsl(a) colors
@return _from-hsl($string);
} @else {
// Return string
@return $string;
}
}
/// Cast a stringified number / stringified percentage into number type
/// @access private
/// @param {String} $string - JSON string
/// @return {Number} - unitless number or percentage
/// @require {function} _json-decode--number
@function _get-color-value($string) {
$first: str-slice($string, 1, 1);
// Pad <1 values with a leading 0
@if $first == '.' {
$string: '0' + $string;
}
$last: str-slice($string, -1, -1);
@return if(
$last == '%',
nth(_json-decode--number(str-slice($string, 1, -2), 2), 2) * 1%,
nth(_json-decode--number($string, 2), 2)
);
}
/// Cast a JSON encoded string into a hsl(a) color
/// @access private
/// @param {String} $string - JSON string
/// @return {Color | String} - string or hsl(a) color, depending on the match
/// @require {function} _get-color-value
@function _from-hsl($string) {
$frags: ();
$string-lower: to-lower-case($string);
$is-alpha: str-slice($string-lower, 4, 4) == 'a';
$length: str-length($string);
$start: str-index($string, "(");
@for $i from $start through $length {
$token: str-slice($string-lower, $i, $i);
@if $token == ' ' or $token == " " {
// @continue;
} @else if $token == '(' or $token == ',' {
$frags: append($frags, "");
} @else if $token == ')' {
@if length($frags) != if($is-alpha, 4, 3) { @return $string; } // Parsing error
$hue: _get-color-value(nth($frags, 1));
$saturation: _get-color-value(nth($frags, 2));
$lightness: _get-color-value(nth($frags, 3));
@if not $hue or not $saturation or not $lightness {
@return $string;
}
@if $is-alpha {
@if length($frags) != 4 { @return $string; } // No alpha channel found
$alpha: _get-color-value(nth($frags, 4));
@if not $alpha { @return $string; } // Error parsing alpha channel
@return hsla($hue, $saturation, $lightness, $alpha);
}
@return hsl($hue, $saturation, $lightness);
} @else {
$frags: set-nth($frags, length($frags), nth($frags, length($frags)) + $token);
}
}
@return $string;
}
/// Cast a JSON encoded string into a rgb(a) color
/// @access private
/// @param {String} $string - JSON string
/// @return {Color | String} - string or rgb(a) color depending on the match
/// @require {function} _get-color-value
@function _from-rgb($string) {
$string-lower: to-lower-case($string);
$frags: ();
$is-alpha: str-slice($string-lower, 4, 4) == 'a';
$start: str-index($string, "(");
$length: str-length($string);
@for $i from $start through $length {
$token: str-slice($string-lower, $i, $i);
@if $token == ' ' or $token == " " {
// @continue;
} @else if $token == '(' or $token == ',' {
$frags: append($frags, "");
} @else if $token == ')' {
@if length($frags) != if($is-alpha, 4, 3) { @return $string; } // Parsing error
$red: _get-color-value(nth($frags, 1));
$green: _get-color-value(nth($frags, 2));
$blue: _get-color-value(nth($frags, 3));
@if not $red or not $green or not $blue {
@return $string;
}
@if $is-alpha {
@if length($frags) != 4 { @return $string; } // No alpha channel found
$alpha: _get-color-value(nth($frags, 4));
@if not $alpha { @return $string; } // Error parsing alpha channel
@return rgba($red, $green, $blue, $alpha);
}
@return rgb($red, $green, $blue);
} @else {
$frags: set-nth($frags, length($frags), nth($frags, length($frags)) + $token);
}
}
@return $string;
}
/// Cast a JSON encoded string into a hexadecimal color
/// @access private
/// @param {String} $string - JSON string
/// @return {Color | String} - string or hex color depending on the match
/// @require {function} _hex-to-dec
@function _from-hex($string) {
$string-lower: to-lower-case($string);
$r: ""; $g: ""; $b: "";
$hex: "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "a" "b" "c" "d" "e" "f";
$length: str-length($string);
$max: if($length == 4, 1, 2);
// Check for length accuracy
@if $length != 4 and $length != 7 {
@return $string;
}
// Loop from the second character (omitting #)
@for $i from 2 through $length {
$c: str-slice($string-lower, $i, $i);
// If wrong character, return
@if index($hex, $c) == null {
@return $string;
}
@if str-length($r) < $max {
$r: $r + $c;
} @else if str-length($g) < $max {
$g: $g + $c;
} @else if str-length($b) < $max {
$b: $b + $c;
}
}
@if $length == 4 {
$r: $r + $r;
$g: $g + $g;
$b: $b + $b;
}
@return rgb(_hex-to-dec($r), _hex-to-dec($g), _hex-to-dec($b));
}
/// Convert an hexadecimal number to a decimal number
/// @access private
/// @param {String} $string - hexadecimal value
/// @return {Number} - decimal number
@function _hex-to-dec($string) {
$hex: "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "a" "b" "c" "d" "e" "f";
$string: to-lower-case($string);
$length: str-length($string);
$dec: 0;
@for $i from 1 through $length {
$factor: 1 + (15 * ($length - $i));
$index: index($hex, str-slice($string, $i, $i));
$dec: $dec + $factor * ($index - 1);
}
@return $dec;
}
/// Power function
/// @access private
/// @param {Number} $x - number
/// @param {Number} $n - power
/// @return {Number}
@function _pow($x, $n) {
@if $n == 0 { @return 1; }
$ret: 1;
@if $n >= 0 {
@for $i from 1 through $n {
$ret: $ret * $x;
}
} @else {
@for $i from $n to 0 {
$ret: $ret / $x;
}
}
@return $ret;
}
/// Parses a JSON encoded number to find the integer part
/// @access private
/// @param {String} $source - JSON complete source
/// @param {Number} $pointer - current pointer
/// @throw Unexpected token $token.
/// @return {List} (new pointer, parsed number)
/// @require {function} _throw
@function _find-integer($source, $pointer) {
$source: to-lower-case($source);
$length: str-length($source);
$numbers: '0' '1' '2' '3' '4' '5' '6' '7' '8' '9';
$result: 0;
@while $pointer <= $length {
$token: str-slice($source, $pointer, $pointer);
$index: index($numbers, $token);
@if $token == '-' {
// do nothing
} @else if $index {
$result: $result * 10 + ($index - 1);
} @else {
@if index('e' '.' ' ' ',' ']' '}', $token) {
@return $pointer, $result;
}
@return _throw("Unexpected token `" + $token + "`.", $pointer);
}
$pointer: $pointer + 1;
}
@return $pointer, $result;
}
/// Parses a JSON encoded number to find the digits
/// @access private
/// @param {String} $source - JSON complete source
/// @param {Number} $pointer - current pointer
/// @throw Unexpected token $token.
/// @return {List} (new pointer, parsed number)
/// @require {function} _throw
@function _find-digits($source, $pointer) {
$source: to-lower-case($source);
$length: str-length($source);
$numbers: '0' '1' '2' '3' '4' '5' '6' '7' '8' '9';
$result: null;
$runs: 1;
@while $pointer <= $length {
$token: str-slice($source, $pointer, $pointer);
$index: index($numbers, $token);
@if $token == '.' {
// @continue;
} @else if $index and $index > 0 {
$runs: $runs * 10;
$result: if($result == null, ($index - 1), $result * 10 + ($index - 1));
} @else {
@if index('e' '.' ' ' ',' ']' '}', $token) {
@return $pointer, if($result != null, $result / $runs, $result);
}
@return _throw("Unexpected token `#{$token}`.", $pointer);
}
$pointer: $pointer + 1;
}
@return $pointer, if($result != null, $result / $runs, $result);
}
/// Parses a JSON encoded number to find the exponent part
/// @access private
/// @param {String} $source - JSON complete source
/// @param {Number} $pointer - current pointer
/// @throw Unexpected token $token.
/// @return {List} - (new pointer, parsed number)
/// @require {function} _throw
@function _find-exponent($source, $pointer) {
$source: to-lower-case($source);
$length: str-length($source);
$numbers: '0' '1' '2' '3' '4' '5' '6' '7' '8' '9';
$result: null;
$minus: null;
@while $pointer <= $length {
$token: str-slice($source, $pointer, $pointer);
$index: index($numbers, $token);
@if $token == 'e' {
// @continue;
} @else if $token == '-' {
@if $minus != null {
@return _throw("Unexpected token `-`.", $pointer);
}
$minus: true;
} @else if $token == '+' {
@if $minus != null {
@return _throw("Unexpected token `+`.", $pointer);
}
$minus: false;
} @else if $index and $index > 0 {
$result: if($result == null, ($index - 1), $result * 10 + ($index - 1));
} @else {
@if index(' ' ',' ']' '}', $token) == null {
@return _throw("Unexpected token `" + $token + "`.", $pointer);
}
@return $pointer, if($minus and $result != null, $result * -1, $result);
}
$pointer: $pointer + 1;
}
@return $pointer, if($minus and $result != null, $result * -1, $result);
}
/// Parses a JSON encoded string
/// @param {String} $source - JSON complete source
/// @param {Number} $pointer - current pointer
/// @throw Unterminated string.
/// @return {List} (new pointer, parsed string / color / length)
/// @require {function} _throw
/// @require {function} _find-ending-quote
/// @require {function} _strip-token
/// @require {function} _color
/// @require {function} _length
@function _json-decode--string($source, $pointer) {
// Check for the end of the string
$temp: str-slice($source, $pointer);
$end: _find-ending-quote($temp);
$string: "";
// If no end is found
@if not $end or $end == 0 {
@return _throw("Unterminated string.", $pointer);
} @else if $end > 1 {
// If string is not empty
$string: str-slice($temp, 1, $end - 1);
$cr: "
";
$string: _strip-token($string, "\\\r", $cr);
$string: _strip-token($string, "\\\n", $cr);
$string: _strip-token($string, '\\\"', '"');
// Test whether the string could be a CSS length
$string: _length($string);
// Test whether the string could be a CSS color
@if type-of($string) == "string" {
$string: _color($string);
}
}
@return ($pointer + $end, $string);
}
/// Parses a JSON encoded `true`
/// @param {String} $source - JSON complete source
/// @param {Number} $pointer - current pointer
/// @throw Unexpected token `t`.
/// @return {List} - (new pointer, true)
/// @require {function} _throw
@function _json-decode--true($source, $pointer) {
$length: str-length($source);
@if $length - $pointer < 2
or str-slice($source, $pointer, $pointer) != 'r'
or str-slice($source, $pointer + 1, $pointer + 1) != 'u'
or str-slice($source, $pointer + 2, $pointer + 2) != 'e' {
@return _throw("Unexpected token: `t`.", $pointer);
}
@return ($pointer + 3, true);
}
/// Parses a JSON encoded `false`
/// @param {String} $source - JSON complete source
/// @param {Number} $pointer - current pointer
/// @throw Unexpected token `f`.
/// @return {List} - (new pointer, false)
/// @require {function} _throw
@function _json-decode--false($source, $pointer) {
$length: str-length($source);
@if $length - $pointer < 3
or str-slice($source, $pointer, $pointer) != 'a'
or str-slice($source, $pointer + 1, $pointer + 1) != 'l'
or str-slice($source, $pointer + 2, $pointer + 2) != 's'
or str-slice($source, $pointer + 3, $pointer + 3) != 'e' {
@return _throw("Unexpected token: `f`.", $pointer);
}
@return ($pointer + 4, false);
}
/// Parses a JSON encoded `null`
/// @param {String} $source - JSON complete source
/// @param {Number} $pointer - current pointer
/// @throw "Unexpected token `n`."
/// @return {List} (new pointer, null)
/// @require {function} _throw
@function _json-decode--null($source, $pointer) {
$length: str-length($source);
@if $length - $pointer < 2
or str-slice($source, $pointer, $pointer) != 'u'
or str-slice($source, $pointer + 1, $pointer + 1) != 'l'
or str-slice($source, $pointer + 2, $pointer + 2) != 'l' {
@return _throw("Unexpected token: `n`.", $pointer);
}
@return ($pointer + 3, null);
}
/// Parses a JSON encoded number
/// @param {String} $source - JSON complete source
/// @param {Number} $pointer - current pointer
/// @throw "Unexpected token $token."
/// @throw "Unexpected end of stream."
/// @return {List} (new pointer, parsed number)
/// @require {function} _throw
/// @require {function} _find-integer
/// @require {function} _find-digits
/// @require {function} _find-exponent
/// @require {function} _pow
@function _json-decode--number($source, $pointer) {
$pointer: $pointer - 1; // Move back pointer to begininng of number
$allowed: '-' '0' '1' '2' '3' '4' '5' '6' '7' '8' '9'; // Allowed characted to start with
$first: str-slice($source, $pointer, $pointer); // First character of the number
$minus: $first == '-'; // Is it negative?
// Early check for errors
@if not index($allowed, $first) {
@return _throw("Unexpected token `" + $first + "`.", $pointer);
}
// Find the integer part
$find-integer: _find-integer($source, $pointer);
$pointer: nth($find-integer, 1);
$result: nth($find-integer, 2);
@if not $result { // Error occured
@return $find-integer;
}
// Find digits
@if str-slice($source, $pointer, $pointer) == '.' {
$find-digits: _find-digits($source, $pointer);
$pointer: nth($find-digits, 1);
$digits: nth($find-digits, 2);
@if $digits == null { // Empty digits, throw error
@return _throw("Unexpected end of stream.", $pointer);
} @else if $digits == false { // Error occured, return it
@return $find-digits;
}
$result: $result + $digits;
}
// Find exponent
@if to-lower-case(str-slice($source, $pointer, $pointer)) == 'e' {
$find-exponent: _find-exponent($source, $pointer);
$pointer: nth($find-exponent, 1);
$exponent: nth($find-exponent, 2);
@if $exponent == null { // Empty exponent, throw error
@return _throw("Unexpected end of stream.", $pointer);
} @else if $exponent == false { // Error occured, return it
@return $find-exponent;
}
$result: $result * _pow(10, $exponent);
}
@return ($pointer, if($minus, $result * -1, $result));
}
/// Parses a JSON encoded array
/// @param {String} $source - JSON complete source
/// @param {Number} $pointer - current pointer
/// @throw Unexpected comma in array literal.
/// @throw Missing comma in array literal.
/// @throw Unterminated array literal.
/// @return {List} (new pointer, parsed list)
/// @require {function} _throw
/// @require {function} _json-decode--value
@function _json-decode--list($source, $pointer) {
$length: str-length($source);
$list: ();
$needs-comma: false;
@if $pointer <= $length and str-slice($source, $pointer, $pointer) == "]" {
@return ($pointer + 1, $list);
}
@while $pointer <= $length {
$token: str-slice($source, $pointer, $pointer);
@if $token == "]" {
@if not $needs-comma and length($list) != 0 {
@return _throw("Unexpected comma in array literal.", $pointer);
}
// Do it the Sass way and destruct a single item array to an element.
@return ($pointer + 1, if(length($list) == 1, nth($list, 1), $list));
} @else if $token == " " or $token == " " {
$pointer: $pointer + 1;
} @else if $token == "," {
@if not $needs-comma {
@return _throw("Unexpected comma in array literal.", $pointer);
}
$needs-comma: false;
$pointer: $pointer + 1;
} @else {
@if $needs-comma {
@return _throw("Missing comma in array literal.", $pointer);
}
$read: _json-decode--value($source, $pointer);
$pointer: nth($read, 1);
$list: append($list, nth($read, 2));
$needs-comma: true;
}
}
@return _throw("Unterminated array literal.", $pointer);
}
/// Parses a JSON encoded object
/// @param {String} $source - JSON complete source
/// @param {Number} $pointer - current pointer
/// @throw Unexpected comma in object literal.
/// @throw Unexpected token $token in object literal.
/// @throw Missing comma in object literal.
/// @throw Unterminated object literal.
/// @throw Consuming token `:` failed.
/// @return {List} (new pointer, map)
/// @require {function} _throw
/// @require {function} _consume
/// @require {function} _json-decode--value
/// @require {function} _json-decode--string
@function _json-decode--map($source, $pointer) {
$length: str-length($source);
$map: ();
$needs-comma: false;
// Deal with empty map
@if $pointer <= $length and str-slice($source, $pointer, $pointer) == "}" {
@return ($pointer + 1, $map);
}
@while $pointer <= $length {
$token: str-slice($source, $pointer, $pointer);
$pointer: $pointer + 1;
@if $token == "}" {
@if not $needs-comma and length($map) != 0 {
@return _throw("Unexpected comma in object literal.", $pointer);
}
@return ($pointer, $map);
} @else if $token == " " or $token == " " {
// @continue;
} @else if $token == "," {
@if not $needs-comma {
@return _throw("Unexpected comma in object literal.", $pointer);
}
$needs-comma: false;
} @else if $token == '"' {
@if $needs-comma {
@return _throw("Missing comma in object literal.", $pointer);
}
// Read key
$read-key: _json-decode--string($source, $pointer);
$pointer: nth($read-key, 1);
$key: nth($read-key, 2);
// Remove colon
$pointer: _consume($source, $pointer, ":");
@if length($pointer) > 1 { // If consume has failed
@return _throw("Consuming token `:` failed.", 0);
}
// Read value
$read-value: _json-decode--value($source, $pointer);
$pointer: nth($read-value, 1);
$value: nth($read-value, 2);
// Add pair to map
$map: map-merge($map, ($key: $value));
$needs-comma: true;
} @else {
@return _throw("Unexpected token `" + $token + "` in object literal.", $pointer);
}
}
@return _throw("Unterminated object literal.", $pointer);
}
/// Parse a JSON string
/// @access public
/// @param {String} $json - JSON string to parse
/// @throw Input string may not be null.
/// @return {*}
/// @require {function} _json-decode--value
@function json-decode($json) {
$length: str-length($json);
$pointer: 1;
$value: null;
@if $json == null {
@return _throw("Input string may not be null.", $pointer);
}
@while $value != false // Stop if error
and $pointer <= $length {
$read: _json-decode--value($json, $pointer);
$pointer: nth($read, 1);
$value: nth($read, 2);
}
@return $value;
}
/// Proof quote a value
/// @access private
/// @param {*} $value - value to be quoted
/// @return {String} - quoted value
@function _proof-quote($value) {
// $value: to-string($value);
@return '"#{$value}"';
}
/// Encode a bool to JSON
/// @access private
/// @param {Bool} $bool - bool to be encoded
/// @return {Bool} - encoded bool
@function _json-encode--bool($boolean) {
@return $boolean;
}
/// Encode a color to JSON
/// @access private
/// @param {Color} $color - color to be encoded
/// @return {String} - encoded color
/// @require {function} _proof-quote
@function _json-encode--color($color) {
@return _proof-quote($color);
}
/// Encode a list to JSON
/// @access private
/// @param {List} $list - list to be encoded
/// @return {String} - encoded list
/// @require {function} json-encore
@function _json-encode--list($list) {
$str: "";
@each $item in $list {
$str: $str + ', ' + json-encode($item);
}
@return '[' + str-slice($str, 3) + ']';
}
/// Encode a map to JSON
/// @access private
/// @param {Map} $map - map to be encoded
/// @return {String} - encoded map
/// @require {function} _proof-quote
/// @require {function} json-encode
@function _json-encode--map($map) {
$str: "";
@each $key, $value in $map {
$str: $str + ', ' + _proof-quote($key) + ': ' + json-encode($value);
}
@return '{' + str-slice($str, 3) + '}';
}
/// Encode a number to JSON
/// @access private
/// @param {Number} $number - number to be encoded
/// @return {String} - encoded number
/// @require {function} _proof-quote
@function _json-encode--number($number) {
@return if(unitless($number), $number, _proof-quote($number));
}
/// Encode a string to JSON
/// @access private
/// @param {String} $string - string to be encoded
/// @return {String} - encoded string
/// @require {function} _proof-quote
@function _json-encode--string($string) {
@return _proof-quote($string);
}
/// Encode `null` to JSON
/// @access private
/// @param {Null} $null - `null`
/// @return {String}
@function _json-encode--null($null) {
@return "null";
}
/// JSON.stringify a value and pass it as a font-family of head element
/// @access public
/// @param {*} $value - value to be stringified
/// @param {String} $flag (all) - output driver
/// @require {function} json-encode
@mixin json-encode($value, $flag: "all") {
$flag: if(index("all" "regular" "media" "comment", $flag), $flag, "all");
$json: json-encode($value);
// Persistent comment
@if $flag == "comment" or $flag == "all" {
/*! json-encode: #{$json} */
}
// Regular property value pair
@if $flag == "regular" or $flag == "all" {
// All browsers except IE8-
body::before {
// This element must be in the render tree to get it via getComputedStyle(document.body, ':before');
content: json-encode($value);
display: block;
height: 0;
overflow: hidden;
width: 0;
}
// All browsers except Opera (Presto based)
head {
font-family: json-encode($value);
}
}
// Falsy media query
@if $flag == "media" or $flag == "all" {
@media -json-encode {
json {
json: $json;
}
}
}
}
/// Delay the encoding of ta literal to JSON to a type-specific method
/// @access public
/// @param {*} $value - value to be stringified
/// @throw Unknown type for #{$value} (#{$type}).
/// @return {String} - JSON encoded string
/// @require {function} _json-encode--string
/// @require {function} _json-encode--number
/// @require {function} _json-encode--list
/// @require {function} _json-encode--map
/// @require {function} _json-encode--null
/// @require {function} _json-encode--color
/// @require {function} _json-encode--bool
@function json-encode($value) {
$type: type-of($value);
@if function_exists('_json-encode--#{$type}') {
@return call('_json-encode--#{$type}', $value);
}