@@ -459,191 +459,35 @@ namespace Sass {
459
459
460
460
void Inspect::operator ()(Number* n)
461
461
{
462
-
463
- string res;
464
-
465
- // init stuff
466
- n->normalize ();
467
- int precision = 5 ;
468
- double value = n->value ();
469
- // get option from optional context
470
- if (ctx) precision = ctx->precision ;
471
-
472
- // check if the fractional part of the value equals to zero
473
- // neat trick from http://stackoverflow.com/a/1521682/1550314
474
- // double int_part; bool is_int = modf(value, &int_part) == 0.0;
475
-
476
- // this all cannot be done with one run only, since fixed
477
- // output differs from normal output and regular output
478
- // can contain scientific notation which we do not want!
479
-
480
- // first sample
481
- stringstream ss;
482
- ss.precision (12 );
483
- ss << value;
484
-
485
- // check if we got scientific notation in result
486
- if (ss.str ().find_first_of (" e" ) != string::npos) {
487
- ss.clear (); ss.str (string ());
488
- ss.precision (max (12 , precision));
489
- ss << fixed << value;
490
- }
491
-
492
- string tmp = ss.str ();
493
- size_t pos_point = tmp.find_first_of (" .," );
494
- size_t pos_fract = tmp.find_last_not_of (" 0" );
495
- bool is_int = pos_point == pos_fract ||
496
- pos_point == string::npos;
497
-
498
- // reset stream for another run
499
- ss.clear (); ss.str (string ());
500
-
501
- // take a shortcut for integers
502
- if (is_int)
503
- {
504
- ss.precision (0 );
505
- ss << fixed << value;
506
- res = string (ss.str ());
507
- }
508
- // process floats
509
- else
510
- {
511
- // do we have have too much precision?
512
- if (pos_fract < precision + pos_point)
513
- { precision = pos_fract - pos_point; }
514
- // round value again
515
- ss.precision (precision);
516
- ss << fixed << value;
517
- res = string (ss.str ());
518
- // maybe we truncated up to decimal point
519
- size_t pos = res.find_last_not_of (" 0" );
520
- bool at_dec_point = res[pos] == ' .' ||
521
- res[pos] == ' ,' ;
522
- // don't leave a blank point
523
- if (at_dec_point) ++ pos;
524
- res.resize (pos + 1 );
525
- }
526
-
527
- // some final cosmetics
528
- if (res == " -0.0" ) res.erase (0 , 1 );
529
- else if (res == " -0" ) res.erase (0 , 1 );
530
-
531
- // add unit now
532
- res += n->unit ();
533
-
534
- // check for a valid unit here
535
- // includes result for reporting
536
- if (n->numerator_units ().size () > 1 ||
537
- n->denominator_units ().size () > 0 ||
538
- (n->numerator_units ().size () && n->numerator_units ()[0 ].find_first_of (' /' ) != string::npos) ||
539
- (n->numerator_units ().size () && n->numerator_units ()[0 ].find_first_of (' *' ) != string::npos)
540
- ) {
541
- error (res + " isn't a valid CSS value." , n->pstate ());
542
- }
543
-
462
+ // use values to_string facility
463
+ bool compressed = ctx->output_style == COMPRESSED;
464
+ string res = n->to_string (compressed, ctx->precision );
544
465
// output the final token
545
466
append_token (res, n);
546
-
547
- }
548
-
549
- // helper function for serializing colors
550
- template <size_t range>
551
- static double cap_channel (double c) {
552
- if (c > range) return range;
553
- else if (c < 0 ) return 0 ;
554
- else return c;
555
467
}
556
468
557
469
void Inspect::operator ()(Color* c)
558
470
{
559
- stringstream ss;
560
-
561
- // check if we prefer short hex colors
562
- bool want_short = output_style () == COMPRESSED;
563
-
564
- // original color name
565
- // maybe an unknown token
566
- string name = c->disp ();
567
-
568
- // resolved color
569
- string res_name = name;
570
-
571
- double r = round (cap_channel<0xff >(c->r ()));
572
- double g = round (cap_channel<0xff >(c->g ()));
573
- double b = round (cap_channel<0xff >(c->b ()));
574
- double a = cap_channel<1 > (c->a ());
575
-
576
- // get color from given name (if one was given at all)
577
- if (name != " " && names_to_colors.count (name)) {
578
- Color* n = name_to_color (name);
579
- r = round (cap_channel<0xff >(n->r ()));
580
- g = round (cap_channel<0xff >(n->g ()));
581
- b = round (cap_channel<0xff >(n->b ()));
582
- a = cap_channel<1 > (n->a ());
583
- }
584
- // otherwise get the possible resolved color name
585
- else {
586
- int numval = static_cast <int >(r) * 0x10000 + static_cast <int >(g) * 0x100 + static_cast <int >(b);
587
- if (colors_to_names.count (numval))
588
- res_name = color_to_name (numval);
589
- }
590
-
591
- stringstream hexlet;
592
- hexlet << ' #' << setw (1 ) << setfill (' 0' );
593
- // create a short color hexlet if there is any need for it
594
- if (want_short && is_color_doublet (r, g, b) && a == 1 ) {
595
- hexlet << hex << setw (1 ) << (static_cast <unsigned long >(r) >> 4 );
596
- hexlet << hex << setw (1 ) << (static_cast <unsigned long >(g) >> 4 );
597
- hexlet << hex << setw (1 ) << (static_cast <unsigned long >(b) >> 4 );
598
- } else {
599
- hexlet << hex << setw (2 ) << static_cast <unsigned long >(r);
600
- hexlet << hex << setw (2 ) << static_cast <unsigned long >(g);
601
- hexlet << hex << setw (2 ) << static_cast <unsigned long >(b);
602
- }
603
-
604
- if (want_short && !c->is_delayed ()) name = " " ;
605
-
606
- // retain the originally specified color definition if unchanged
607
- if (name != " " ) {
608
- ss << name;
609
- }
610
- else if (r == 0 && g == 0 && b == 0 && a == 0 ) {
611
- ss << " transparent" ;
612
- }
613
- else if (a >= 1 ) {
614
- if (res_name != " " ) {
615
- if (want_short && hexlet.str ().size () < res_name.size ()) {
616
- ss << hexlet.str ();
617
- } else {
618
- ss << res_name;
619
- }
620
- }
621
- else {
622
- ss << hexlet.str ();
623
- }
624
- }
625
- else {
626
- ss << " rgba(" ;
627
- ss << static_cast <unsigned long >(r) << " ," ;
628
- if (output_style () != COMPRESSED) ss << " " ;
629
- ss << static_cast <unsigned long >(g) << " ," ;
630
- if (output_style () != COMPRESSED) ss << " " ;
631
- ss << static_cast <unsigned long >(b) << " ," ;
632
- if (output_style () != COMPRESSED) ss << " " ;
633
- ss << a << ' )' ;
634
- }
635
- append_token (ss.str (), c);
471
+ // use values to_string facility
472
+ bool compressed = ctx->output_style == COMPRESSED;
473
+ string res = c->to_string (compressed, ctx->precision );
474
+ // output the final token
475
+ append_token (res, c);
636
476
}
637
477
638
478
void Inspect::operator ()(Boolean* b)
639
479
{
640
- append_token (b->value () ? " true" : " false" , b);
480
+ // use values to_string facility
481
+ bool compressed = ctx->output_style == COMPRESSED;
482
+ string res = b->to_string (compressed, ctx->precision );
483
+ // output the final token
484
+ append_token (res, b);
641
485
}
642
486
643
487
void Inspect::operator ()(String_Schema* ss)
644
488
{
645
- // Evaluation should turn these into String_Constants, so this method is
646
- // only for inspection purposes.
489
+ // Evaluation should turn these into String_Constants,
490
+ // so this method is only for inspection purposes.
647
491
for (size_t i = 0 , L = ss->length (); i < L; ++i) {
648
492
if ((*ss)[i]->is_interpolant ()) append_string (" #{" );
649
493
(*ss)[i]->perform (this );
@@ -653,16 +497,24 @@ namespace Sass {
653
497
654
498
void Inspect::operator ()(String_Constant* s)
655
499
{
656
- append_token (s->value (), s);
500
+ // get options from optional? context
501
+ int precision = ctx ? ctx->precision : 5 ;
502
+ bool compressed = ctx ? ctx->output_style == COMPRESSED : false ;
503
+ // use values to_string facility
504
+ string res (s->to_string (compressed, precision));
505
+ // output the final token
506
+ append_token (res, s);
657
507
}
658
508
659
509
void Inspect::operator ()(String_Quoted* s)
660
510
{
661
- if (s->quote_mark ()) {
662
- append_token (quote (s->value (), s->quote_mark (), true ), s);
663
- } else {
664
- append_token (s->value (), s);
665
- }
511
+ // get options from optional? context
512
+ int precision = ctx ? ctx->precision : 5 ;
513
+ bool compressed = ctx ? ctx->output_style == COMPRESSED : false ;
514
+ // use values to_string facility
515
+ string res (s->to_string (compressed, precision));
516
+ // output the final token
517
+ append_token (res, s);
666
518
}
667
519
668
520
void Inspect::operator ()(Supports_Query* fq)
@@ -754,7 +606,11 @@ namespace Sass {
754
606
755
607
void Inspect::operator ()(Null* n)
756
608
{
757
- append_token (" null" , n);
609
+ // use values to_string facility
610
+ bool compressed = ctx->output_style == COMPRESSED;
611
+ string res = n->to_string (compressed, ctx->precision );
612
+ // output the final token
613
+ append_token (res, n);
758
614
}
759
615
760
616
// parameters and arguments
0 commit comments