Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 104... Línea 104...
104
    }
104
    }
Línea 105... Línea 105...
105
 
105
 
106
    /**
106
    /**
107
     * Combine CSS from import statements.
107
     * Combine CSS from import statements.
108
     *
108
     *
109
     * Import statements will be loaded and their content merged into the original
109
     * \@import's will be loaded and their content merged into the original file,
110
     * file, to save HTTP requests.
110
     * to save HTTP requests.
111
     *
111
     *
112
     * @param string   $source  The file to combine imports for
112
     * @param string   $source  The file to combine imports for
113
     * @param string   $content The CSS content to combine imports for
113
     * @param string   $content The CSS content to combine imports for
114
     * @param string[] $parents Parent paths, for circular reference checks
114
     * @param string[] $parents Parent paths, for circular reference checks
Línea 314... Línea 314...
314
            $this->extractMath();
314
            $this->extractMath();
315
            $this->extractCustomProperties();
315
            $this->extractCustomProperties();
316
            $css = $this->replace($css);
316
            $css = $this->replace($css);
Línea 317... Línea 317...
317
 
317
 
-
 
318
            $css = $this->stripWhitespace($css);
-
 
319
            $css = $this->convertLegacyColors($css);
318
            $css = $this->stripWhitespace($css);
320
            $css = $this->cleanupModernColors($css);
319
            $css = $this->shortenColors($css);
321
            $css = $this->shortenHEXColors($css);
320
            $css = $this->shortenZeroes($css);
322
            $css = $this->shortenZeroes($css);
321
            $css = $this->shortenFontWeights($css);
323
            $css = $this->shortenFontWeights($css);
Línea 322... Línea 324...
322
            $css = $this->stripEmptyTags($css);
324
            $css = $this->stripEmptyTags($css);
Línea 478... Línea 480...
478
        // replace urls
480
        // replace urls
479
        return str_replace($search, $replace, $content);
481
        return str_replace($search, $replace, $content);
480
    }
482
    }
Línea 481... Línea 483...
481
 
483
 
482
    /**
484
    /**
483
     * Shorthand hex color codes.
485
     * Shorthand HEX color codes.
-
 
486
     * #FF0000FF -> #f00 -> red
484
     * #FF0000 -> #F00.
487
     * #FF00FF00 -> transparent.
485
     *
488
     *
486
     * @param string $content The CSS content to shorten the hex color codes for
489
     * @param string $content The CSS content to shorten the HEX color codes for
487
     *
490
     *
488
     * @return string
491
     * @return string
489
     */
492
     */
490
    protected function shortenColors($content)
493
    protected function shortenHexColors($content)
-
 
494
    {
491
    {
495
        // shorten repeating patterns within HEX ..
Línea 492... Línea 496...
492
        $content = preg_replace('/(?<=[: ])#([0-9a-z])\\1([0-9a-z])\\2([0-9a-z])\\3(?:([0-9a-z])\\4)?(?=[; }])/i', '#$1$2$3$4', $content);
496
        $content = preg_replace('/(?<=[: ])#([0-9a-f])\\1([0-9a-f])\\2([0-9a-f])\\3(?:([0-9a-f])\\4)?(?=[; }])/i', '#$1$2$3$4', $content);
493
 
497
 
494
        // remove alpha channel if it's pointless...
498
        // remove alpha channel if it's pointless ..
-
 
499
        $content = preg_replace('/(?<=[: ])#([0-9a-f]{6})ff(?=[; }])/i', '#$1', $content);
-
 
500
        $content = preg_replace('/(?<=[: ])#([0-9a-f]{3})f(?=[; }])/i', '#$1', $content);
-
 
501
 
Línea 495... Línea 502...
495
        $content = preg_replace('/(?<=[: ])#([0-9a-z]{6})ff?(?=[; }])/i', '#$1', $content);
502
        // replace `transparent` with shortcut ..
-
 
503
        $content = preg_replace('/(?<=[: ])#[0-9a-f]{6}00(?=[; }])/i', '#fff0', $content);
-
 
504
 
-
 
505
        $colors = array(
-
 
506
            // make these more readable
-
 
507
            '#00f' => 'blue',
-
 
508
            '#dc143c' => 'crimson',
-
 
509
            '#0ff' => 'cyan',
-
 
510
            '#8b0000' => 'darkred',
-
 
511
            '#696969' => 'dimgray',
-
 
512
            '#ff69b4' => 'hotpink',
-
 
513
            '#0f0' => 'lime',
496
        $content = preg_replace('/(?<=[: ])#([0-9a-z]{3})f?(?=[; }])/i', '#$1', $content);
514
            '#fdf5e6' => 'oldlace',
497
 
515
            '#87ceeb' => 'skyblue',
498
        $colors = array(
516
            '#d8bfd8' => 'thistle',
-
 
517
            // we can shorten some even more by replacing them with their color name
499
            // we can shorten some even more by replacing them with their color name
518
            '#f0ffff' => 'azure',
500
            '#F0FFFF' => 'azure',
519
            '#f5f5dc' => 'beige',
501
            '#F5F5DC' => 'beige',
520
            '#ffe4c4' => 'bisque',
502
            '#A52A2A' => 'brown',
521
            '#a52a2a' => 'brown',
503
            '#FF7F50' => 'coral',
522
            '#ff7f50' => 'coral',
504
            '#FFD700' => 'gold',
523
            '#ffd700' => 'gold',
505
            '#808080' => 'gray',
524
            '#808080' => 'gray',
506
            '#008000' => 'green',
525
            '#008000' => 'green',
507
            '#4B0082' => 'indigo',
526
            '#4b0082' => 'indigo',
508
            '#FFFFF0' => 'ivory',
527
            '#fffff0' => 'ivory',
509
            '#F0E68C' => 'khaki',
528
            '#f0e68c' => 'khaki',
510
            '#FAF0E6' => 'linen',
529
            '#faf0e6' => 'linen',
-
 
530
            '#800000' => 'maroon',
-
 
531
            '#000080' => 'navy',
511
            '#800000' => 'maroon',
532
            '#808000' => 'olive',
512
            '#000080' => 'navy',
533
            '#ffa500' => 'orange',
513
            '#808000' => 'olive',
534
            '#da70d6' => 'orchid',
514
            '#CD853F' => 'peru',
535
            '#cd853f' => 'peru',
515
            '#FFC0CB' => 'pink',
536
            '#ffc0cb' => 'pink',
516
            '#DDA0DD' => 'plum',
537
            '#dda0dd' => 'plum',
517
            '#800080' => 'purple',
538
            '#800080' => 'purple',
518
            '#F00' => 'red',
539
            '#f00' => 'red',
519
            '#FA8072' => 'salmon',
540
            '#fa8072' => 'salmon',
520
            '#A0522D' => 'sienna',
541
            '#a0522d' => 'sienna',
-
 
542
            '#c0c0c0' => 'silver',
521
            '#C0C0C0' => 'silver',
543
            '#fffafa' => 'snow',
522
            '#FFFAFA' => 'snow',
544
            '#d2b48c' => 'tan',
523
            '#D2B48C' => 'tan',
545
            '#008080' => 'teal',
524
            '#FF6347' => 'tomato',
546
            '#ff6347' => 'tomato',
-
 
547
            '#ee82ee' => 'violet',
-
 
548
            '#f5deb3' => 'wheat',
-
 
549
            // or the other way around
525
            '#EE82EE' => 'violet',
550
            'black' => '#000',
526
            '#F5DEB3' => 'wheat',
551
            'fuchsia' => '#f0f',
-
 
552
            'magenta' => '#f0f',
-
 
553
            'white' => '#fff',
527
            // or the other way around
554
            'yellow' => '#ff0',
Línea 528... Línea 555...
528
            'WHITE' => '#fff',
555
            // and also `transparent`
529
            'BLACK' => '#000',
556
            'transparent' => '#fff0',
530
        );
557
        );
531
 
558
 
-
 
559
        return preg_replace_callback(
-
 
560
            '/(?<=[: ])(' . implode('|', array_keys($colors)) . ')(?=[; }])/i',
-
 
561
            function ($match) use ($colors) {
-
 
562
                return $colors[strtolower($match[0])];
-
 
563
            },
-
 
564
            $content
-
 
565
        );
-
 
566
    }
-
 
567
 
-
 
568
    /**
-
 
569
     * Convert RGB|HSL color codes.
-
 
570
     * rgb(255,0,0,.5) -> rgb(255 0 0 / .5).
-
 
571
     * rgb(255,0,0) -> #f00.
-
 
572
     *
-
 
573
     * @param string $content The CSS content to shorten the RGB color codes for
-
 
574
     *
-
 
575
     * @return string
-
 
576
     */
-
 
577
    protected function convertLegacyColors($content)
-
 
578
    {
-
 
579
        /*
-
 
580
          https://drafts.csswg.org/css-color/#color-syntax-legacy
-
 
581
          https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/rgb
-
 
582
          https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/hsl
-
 
583
        */
-
 
584
 
-
 
585
        // convert legacy color syntax
-
 
586
        $content = preg_replace('/(rgb)a?\(\s*([0-9]{1,3}%?)\s*,\s*([0-9]{1,3}%?)\s*,\s*([0-9]{1,3}%?)\s*,\s*([0,1]?(?:\.[0-9]*)?)\s*\)/i', '$1($2 $3 $4 / $5)', $content);
-
 
587
        $content = preg_replace('/(rgb)a?\(\s*([0-9]{1,3}%?)\s*,\s*([0-9]{1,3}%?)\s*,\s*([0-9]{1,3}%?)\s*\)/i', '$1($2 $3 $4)', $content);
-
 
588
        $content = preg_replace('/(hsl)a?\(\s*([0-9]+(?:deg|grad|rad|turn)?)\s*,\s*([0-9]{1,3}%)\s*,\s*([0-9]{1,3}%)\s*,\s*([0,1]?(?:\.[0-9]*)?)\s*\)/i', '$1($2 $3 $4 / $5)', $content);
-
 
589
        $content = preg_replace('/(hsl)a?\(\s*([0-9]+(?:deg|grad|rad|turn)?)\s*,\s*([0-9]{1,3}%)\s*,\s*([0-9]{1,3}%)\s*\)/i', '$1($2 $3 $4)', $content);
-
 
590
 
-
 
591
        // convert `rgb` to `hex`
-
 
592
        $dec = '([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])';
532
        return preg_replace_callback(
593
        return preg_replace_callback(
533
            '/(?<=[: ])(' . implode('|', array_keys($colors)) . ')(?=[; }])/i',
594
            "/rgb\($dec $dec $dec\)/i",
534
            function ($match) use ($colors) {
595
            function ($match) {
535
                return $colors[strtoupper($match[0])];
596
                return sprintf('#%02x%02x%02x', $match[1], $match[2], $match[3]);
Línea 536... Línea 597...
536
            },
597
            },
-
 
598
            $content
-
 
599
        );
-
 
600
    }
-
 
601
 
-
 
602
    /**
-
 
603
     * Cleanup RGB|HSL|HWB|LCH|LAB
-
 
604
     * rgb(255 0 0 / 1) -> rgb(255 0 0).
-
 
605
     * rgb(255 0 0 / 0) -> transparent.
-
 
606
     *
-
 
607
     * @param string $content The CSS content to cleanup HSL|HWB|LCH|LAB
-
 
608
     *
-
 
609
     * @return string
-
 
610
     */
-
 
611
    protected function cleanupModernColors($content)
-
 
612
    {
-
 
613
        /*
-
 
614
          https://drafts.csswg.org/css-color/#color-syntax-modern
-
 
615
          https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/hwb
-
 
616
          https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/lch
-
 
617
          https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/lab
-
 
618
          https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/oklch
-
 
619
          https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/oklab
-
 
620
        */
-
 
621
        $tag = '(rgb|hsl|hwb|(?:(?:ok)?(?:lch|lab)))';
-
 
622
 
-
 
623
        // remove alpha channel if it's pointless ..
-
 
624
        $content = preg_replace('/' . $tag . '\(\s*([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+\/\s+1(?:(?:\.\d?)*|00%)?\s*\)/i', '$1($2 $3 $4)', $content);
-
 
625
 
-
 
626
        // replace `transparent` with shortcut ..
-
 
627
        $content = preg_replace('/' . $tag . '\(\s*[^\s]+\s+[^\s]+\s+[^\s]+\s+\/\s+0(?:[\.0%]*)?\s*\)/i', '#fff0', $content);
537
            $content
628
 
538
        );
629
        return $content;
539
    }
630
    }
540
 
631
 
541
    /**
632
    /**