first commit
This commit is contained in:
209
wp-content/plugins/wp-optimize/vendor/tubalmartin/cssmin/gui/index.php
vendored
Normal file
209
wp-content/plugins/wp-optimize/vendor/tubalmartin/cssmin/gui/index.php
vendored
Normal file
@@ -0,0 +1,209 @@
|
||||
<?php
|
||||
|
||||
require '../vendor/autoload.php';
|
||||
|
||||
use tubalmartin\CssMin\Minifier as CSSmin;
|
||||
|
||||
mb_internal_encoding('UTF-8');
|
||||
|
||||
/**
|
||||
* Navigates through an array and removes slashes from the values.
|
||||
*
|
||||
* If an array is passed, the array_map() function causes a callback to pass the
|
||||
* value back to the function. The slashes from this value will removed.
|
||||
*
|
||||
* @param array|string $value The array or string to be stripped.
|
||||
* @return array|string Stripped array (or string in the callback).
|
||||
*/
|
||||
function stripslashes_deep($value)
|
||||
{
|
||||
if (is_array($value)) {
|
||||
$value = array_map('stripslashes_deep', $value);
|
||||
} elseif (is_object($value)) {
|
||||
$vars = get_object_vars($value);
|
||||
foreach ($vars as $key => $data) {
|
||||
$value->{$key} = stripslashes_deep($data);
|
||||
}
|
||||
} else {
|
||||
$value = stripslashes($value);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
// Disable magic quotes at runtime.
|
||||
if (function_exists('ini_set')) {
|
||||
ini_set('magic_quotes_sybase', 0);
|
||||
ini_set('get_magic_quotes_runtime', 0);
|
||||
}
|
||||
|
||||
// If get_magic_quotes_gpc is active, strip slashes
|
||||
if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
|
||||
$_POST = stripslashes_deep($_POST);
|
||||
}
|
||||
|
||||
|
||||
if (!empty($_POST)) :
|
||||
// Form options
|
||||
parse_str($_POST['options']);
|
||||
|
||||
$linebreak_pos = trim($linebreak_pos) !== '' ? $linebreak_pos : false;
|
||||
$raise_php = isset($raise_php) ? true : false;
|
||||
|
||||
// Create a new CSSmin object and try to raise PHP settings
|
||||
$compressor = new CSSmin($raise_php);
|
||||
|
||||
if ($linebreak_pos !== false) {
|
||||
$compressor->setLineBreakPosition($linebreak_pos);
|
||||
}
|
||||
|
||||
if (isset($keep_sourcemap)) {
|
||||
$compressor->keepSourceMapComment();
|
||||
}
|
||||
|
||||
if (isset($remove_important_comments)) {
|
||||
$compressor->removeImportantComments();
|
||||
}
|
||||
|
||||
if ($raise_php) {
|
||||
$compressor->setMemoryLimit($memory_limit);
|
||||
$compressor->setMaxExecutionTime($max_execution_time);
|
||||
$compressor->setPcreBacktrackLimit(1000 * $pcre_backtrack_limit);
|
||||
$compressor->setPcreRecursionLimit(1000 * $pcre_recursion_limit);
|
||||
}
|
||||
|
||||
// Compress the CSS code and store data
|
||||
$output = array();
|
||||
$output['css'] = $compressor->run($_POST['css']);
|
||||
$output['originalSize'] = mb_strlen($_POST['css'], '8bit');
|
||||
$output['compressedSize'] = mb_strlen($output['css'], '8bit');
|
||||
$output['bytesSaved'] = $output['originalSize'] - $output['compressedSize'];
|
||||
$output['compressionRatio'] = round(($output['bytesSaved'] * 100) /
|
||||
($output['originalSize'] === 0 ? 1 : $output['originalSize']), 2);
|
||||
|
||||
// Output data
|
||||
echo json_encode($output);
|
||||
else :
|
||||
?>
|
||||
<!DOCTYPE HTML>
|
||||
<html lang="en-US">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>YUI CSS compressor - PHP</title>
|
||||
<link rel="stylesheet" type="text/css" href="third-party/bootstrap/css/bootstrap.min.css">
|
||||
<link rel="stylesheet" type="text/css" href="styles.css">
|
||||
<link rel="stylesheet/less" type="text/css" href="styles.less">
|
||||
</head>
|
||||
<body>
|
||||
<div class="navbar">
|
||||
<div class="navbar-inner">
|
||||
<div class="container-fluid">
|
||||
<a class="brand" href="https://github.com/tubalmartin/YUI-CSS-compressor-PHP-port">YUI CSS compressor PHP port</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="container-fluid">
|
||||
<div class="row-fluid">
|
||||
<div id="body" class="span9">
|
||||
<!--Body content-->
|
||||
<div id="less-error-message" class="less-error-message"></div>
|
||||
<div class="well">
|
||||
<div id="input-container">
|
||||
<label for="input-css">Paste a block of CSS to compress in the area below:</label>
|
||||
<textarea id="input-css" class="input-block-level" rows="10"></textarea>
|
||||
</div>
|
||||
<div id="output-container" class="hide">
|
||||
<label for="output-css">Here's your compressed CSS code:</label>
|
||||
<span class="help-block">Original size: <span id="original-size"></span> bytes | Compressed size: <span id="compressed-size"></span> bytes | Bytes saved: <span id="bytes-saved"></span> | Compression ratio: <span id="compression-ratio"></span>%</span>
|
||||
<textarea id="output-css" class="input-block-level" rows="10"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="sidebar" class="span3">
|
||||
<form id="options-form">
|
||||
<p class="submit">
|
||||
<button type="submit" id="compress-btn" class="btn btn-primary btn-large" data-loading-text="Compressing...">Compress!</button>
|
||||
</p>
|
||||
<fieldset>
|
||||
<legend>LESS</legend>
|
||||
<p class="control-group">
|
||||
<label class="checkbox">
|
||||
<input type="checkbox" id="enable-less" value="1"> Enable compiler <span class="version">v1.7.5</span>
|
||||
</label>
|
||||
</p>
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<legend>Compressor options</legend>
|
||||
<div class="control-group">
|
||||
<label>Linebreak after <i>n</i> columns</label>
|
||||
<input type="text" name="linebreak_pos" class="span1">
|
||||
</div>
|
||||
<div class="control-group">
|
||||
<label class="checkbox">
|
||||
<input type="checkbox" name="keep_sourcemap" value="1"> Keep CSS Sourcemap comment
|
||||
</label>
|
||||
</div>
|
||||
<div class="control-group">
|
||||
<label class="checkbox">
|
||||
<input type="checkbox" name="remove_important_comments" value="1"> Remove important comments
|
||||
</label>
|
||||
</div>
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<legend>PHP configuration options</legend>
|
||||
<div class="control-group">
|
||||
<label class="checkbox">
|
||||
<input type="checkbox" name="raise_php" value="1" checked="checked"> Raise PHP configuration options
|
||||
</label>
|
||||
<label>Memory limit</label>
|
||||
<select name="memory_limit" class="span2">
|
||||
<option value="32M">32M</option>
|
||||
<option value="64M">64M</option>
|
||||
<option value="128M" selected="selected">128M</option>
|
||||
<option value="256M">256M</option>
|
||||
<option value="512M">512M</option>
|
||||
<option value="1G">1G</option>
|
||||
<option value="-1">No limit</option>
|
||||
</select>
|
||||
<label>Max execution time</label>
|
||||
<select name="max_execution_time" class="span2">
|
||||
<option value="30">30 secs</option>
|
||||
<option value="60" selected="selected">1 min</option>
|
||||
<option value="120">2 mins</option>
|
||||
<option value="300">5 mins</option>
|
||||
</select>
|
||||
<label>PCRE backtrack limit</label>
|
||||
<select name="pcre_backtrack_limit" class="span2">
|
||||
<option value="100">100.000</option>
|
||||
<option value="1000" selected="selected">1.000.000</option>
|
||||
<option value="2000">2.000.000</option>
|
||||
<option value="5000">5.000.000</option>
|
||||
</select>
|
||||
<label>PCRE recursion limit</label>
|
||||
<select name="pcre_recursion_limit" class="span2">
|
||||
<option value="100">100.000</option>
|
||||
<option value="250">250.000</option>
|
||||
<option value="500" selected="selected">500.000</option>
|
||||
<option value="1000">1.000.000</option>
|
||||
</select>
|
||||
</div>
|
||||
</fieldset>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
less = {
|
||||
env: 'development'
|
||||
};
|
||||
</script>
|
||||
<script type="text/javascript" src="third-party/less-1.7.5.min.js"></script>
|
||||
<script type="text/javascript" src="third-party/jquery-1.12.4.min.js"></script>
|
||||
<script type="text/javascript" src="third-party/bootstrap/js/bootstrap.min.js"></script>
|
||||
<script type="text/javascript" src="scripts.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
<?php
|
||||
endif;
|
||||
?>
|
||||
104
wp-content/plugins/wp-optimize/vendor/tubalmartin/cssmin/gui/scripts.js
vendored
Normal file
104
wp-content/plugins/wp-optimize/vendor/tubalmartin/cssmin/gui/scripts.js
vendored
Normal file
@@ -0,0 +1,104 @@
|
||||
$(function(){
|
||||
|
||||
var inputCss = $('#input-css')
|
||||
, outputCss = $('#output-css')
|
||||
, outputContainer = $('#output-container')
|
||||
, originalSize = $('#original-size')
|
||||
, compressedSize = $('#compressed-size')
|
||||
, bytesSaved = $('#bytes-saved')
|
||||
, compressionRatio = $('#compression-ratio')
|
||||
, compressBtn = $('#compress-btn')
|
||||
, lessConsole = $('#less-error-message')
|
||||
|
||||
/**
|
||||
* Prints LESS compilation errors
|
||||
*/
|
||||
, lessError = function(e) {
|
||||
var content, errorline
|
||||
, template = '<li><label>{line}</label><pre class="{class}">{content}</pre></li>'
|
||||
, error = [];
|
||||
|
||||
content = '<h3>' + (e.type || "Syntax") + "Error: " + (e.message || 'There is an error in your .less file') +
|
||||
'</h3>' + '<p>';
|
||||
|
||||
errorline = function (e, i, classname) {
|
||||
if (e.extract[i] != undefined) {
|
||||
error.push(template.replace(/\{line\}/, (parseInt(e.line) || 0) + (i - 1))
|
||||
.replace(/\{class\}/, classname)
|
||||
.replace(/\{content\}/, e.extract[i]));
|
||||
}
|
||||
};
|
||||
|
||||
if (e.stack) {
|
||||
content += '<br/>' + e.stack.split('\n').slice(1).join('<br/>');
|
||||
} else if (e.extract) {
|
||||
errorline(e, 0, '');
|
||||
errorline(e, 1, 'line');
|
||||
errorline(e, 2, '');
|
||||
content += 'on line ' + e.line + ', column ' + (e.column + 1) + ':</p>' +
|
||||
'<ul>' + error.join('') + '</ul>';
|
||||
}
|
||||
|
||||
lessConsole.html(content).slideDown('fast');
|
||||
}
|
||||
|
||||
/**
|
||||
* Compresses user's CSS with the PHP port of the YUI compressor
|
||||
*/
|
||||
, compress = function(formData) {
|
||||
$.post(window.location.href, formData, function(data, textStatus, jqXHR){
|
||||
// Hide LESS error console
|
||||
lessConsole.slideUp('fast');
|
||||
|
||||
// Fill output & show
|
||||
outputCss.val(data.css);
|
||||
originalSize.html(data.originalSize);
|
||||
compressedSize.html(data.compressedSize);
|
||||
bytesSaved.html(data.bytesSaved);
|
||||
compressionRatio.html(data.compressionRatio);
|
||||
|
||||
outputContainer.slideDown('fast');
|
||||
|
||||
// Restore button state
|
||||
compressBtn.button('reset');
|
||||
}, 'json');
|
||||
};
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Controller
|
||||
*/
|
||||
$('#options-form').on('submit', function(e){
|
||||
e && e.preventDefault();
|
||||
|
||||
var data = {
|
||||
css: inputCss.val(),
|
||||
options: $(this).serialize()
|
||||
};
|
||||
|
||||
// Change button state
|
||||
compressBtn.button('loading');
|
||||
|
||||
// If LESS enabled, precompile CSS with LESS and then compress
|
||||
if (!!$('#enable-less:checked').val()) {
|
||||
try {
|
||||
new(less.Parser)().parse(data.css, function (err, tree) {
|
||||
if (err) {
|
||||
lessError(err);
|
||||
compressBtn.button('reset');
|
||||
} else {
|
||||
data.css = tree.toCSS();
|
||||
compress(data);
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
lessError(err);
|
||||
compressBtn.button('reset');
|
||||
}
|
||||
} else {
|
||||
compress(data);
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
68
wp-content/plugins/wp-optimize/vendor/tubalmartin/cssmin/gui/styles.css
vendored
Normal file
68
wp-content/plugins/wp-optimize/vendor/tubalmartin/cssmin/gui/styles.css
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
/* LESS error report styles */
|
||||
.less-error-message {
|
||||
font-family: Arial, sans-serif;
|
||||
border: 1px solid #e00;
|
||||
border: 1px solid rgba(238,0,0, 0.5);
|
||||
background-color: whiteSmoke;
|
||||
border-radius: 4px;
|
||||
-webkit-border-radius: 4px;
|
||||
-moz-border-radius: 4px;
|
||||
-webkit-box-shadow: inset 0 1px 1px rgba(0,0,0, 0.05);
|
||||
-moz-box-shadow: inset 0 1px 1px rgba(0,0,0, 0.05);
|
||||
box-shadow: inset 0 1px 1px rgba(0,0,0, 0.05);
|
||||
color: #e00;
|
||||
padding: 18px;
|
||||
margin-bottom: 18px;
|
||||
display:none;
|
||||
}
|
||||
.less-error-message ul, .less-error-message li {
|
||||
list-style-type: none;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
}
|
||||
.less-error-message label {
|
||||
font-size: 12px;
|
||||
margin-right: 15px;
|
||||
padding: 4px 0;
|
||||
color: #cc7777;
|
||||
display: inline;
|
||||
}
|
||||
.less-error-message pre {
|
||||
color: #dd6666;
|
||||
padding: 4px 0;
|
||||
margin: 0;
|
||||
display: inline-block;
|
||||
}
|
||||
.less-error-message pre.line {
|
||||
color: red;
|
||||
}
|
||||
.less-error-message h3 {
|
||||
font-size: 20px;
|
||||
font-weight: bold;
|
||||
padding: 0 0 5px 0;
|
||||
margin: 0;
|
||||
}
|
||||
.less-error-message a {
|
||||
color: #10a;
|
||||
}
|
||||
.less-error-message .error {
|
||||
color: red;
|
||||
font-weight: bold;
|
||||
padding-bottom: 2px;
|
||||
border-bottom: 1px dashed red;
|
||||
}
|
||||
|
||||
pre {
|
||||
white-space: pre-wrap; /* css-3 */
|
||||
white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */
|
||||
white-space: -pre-wrap; /* Opera 4-6 */
|
||||
white-space: -o-pre-wrap; /* Opera 7 */
|
||||
word-wrap: break-word; /* Internet Explorer 5.5+ */
|
||||
}
|
||||
|
||||
/* APP styles */
|
||||
.version{font-size:10px;font-style: italic;letter-spacing: 2px}
|
||||
|
||||
legend{font-size: 15px; line-height: 20px; margin-bottom:0}
|
||||
.control-group{margin-bottom:12px;}
|
||||
#output-container{margin-top:18px}
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* Bootstrap.js by @fat & @mdo
|
||||
* plugins: bootstrap-button.js
|
||||
* Copyright 2012 Twitter, Inc.
|
||||
* http://www.apache.org/licenses/LICENSE-2.0.txt
|
||||
*/
|
||||
!function(a){var b=function(b,c){this.$element=a(b),this.options=a.extend({},a.fn.button.defaults,c)};b.prototype={constructor:b,setState:function(a){var b="disabled",c=this.$element,d=c.data(),e=c.is("input")?"val":"html";a+="Text",d.resetText||c.data("resetText",c[e]()),c[e](d[a]||this.options[a]),setTimeout(function(){a=="loadingText"?c.addClass(b).attr(b,b):c.removeClass(b).removeAttr(b)},0)},toggle:function(){var a=this.$element.parent('[data-toggle="buttons-radio"]');a&&a.find(".active").removeClass("active"),this.$element.toggleClass("active")}},a.fn.button=function(c){return this.each(function(){var d=a(this),e=d.data("button"),f=typeof c=="object"&&c;e||d.data("button",e=new b(this,f)),c=="toggle"?e.toggle():c&&e.setState(c)})},a.fn.button.defaults={loadingText:"loading..."},a.fn.button.Constructor=b,a(function(){a("body").on("click.button.data-api","[data-toggle^=button]",function(b){var c=a(b.target);c.hasClass("btn")||(c=c.closest(".btn")),c.button("toggle")})})}(window.jQuery)
|
||||
File diff suppressed because one or more lines are too long
16
wp-content/plugins/wp-optimize/vendor/tubalmartin/cssmin/gui/third-party/less-1.7.5.min.js
vendored
Normal file
16
wp-content/plugins/wp-optimize/vendor/tubalmartin/cssmin/gui/third-party/less-1.7.5.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
155
wp-content/plugins/wp-optimize/vendor/tubalmartin/cssmin/src/Colors.php
vendored
Normal file
155
wp-content/plugins/wp-optimize/vendor/tubalmartin/cssmin/src/Colors.php
vendored
Normal file
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
namespace tubalmartin\CssMin;
|
||||
|
||||
class Colors
|
||||
{
|
||||
public static function getHexToNamedMap()
|
||||
{
|
||||
// Hex colors longer than named counterpart
|
||||
return array(
|
||||
'#f0ffff' => 'azure',
|
||||
'#f5f5dc' => 'beige',
|
||||
'#ffe4c4' => 'bisque',
|
||||
'#a52a2a' => 'brown',
|
||||
'#ff7f50' => 'coral',
|
||||
'#ffd700' => 'gold',
|
||||
'#808080' => 'gray',
|
||||
'#008000' => 'green',
|
||||
'#4b0082' => 'indigo',
|
||||
'#fffff0' => 'ivory',
|
||||
'#f0e68c' => 'khaki',
|
||||
'#faf0e6' => 'linen',
|
||||
'#800000' => 'maroon',
|
||||
'#000080' => 'navy',
|
||||
'#fdf5e6' => 'oldlace',
|
||||
'#808000' => 'olive',
|
||||
'#ffa500' => 'orange',
|
||||
'#da70d6' => 'orchid',
|
||||
'#cd853f' => 'peru',
|
||||
'#ffc0cb' => 'pink',
|
||||
'#dda0dd' => 'plum',
|
||||
'#800080' => 'purple',
|
||||
'#f00' => 'red',
|
||||
'#fa8072' => 'salmon',
|
||||
'#a0522d' => 'sienna',
|
||||
'#c0c0c0' => 'silver',
|
||||
'#fffafa' => 'snow',
|
||||
'#d2b48c' => 'tan',
|
||||
'#008080' => 'teal',
|
||||
'#ff6347' => 'tomato',
|
||||
'#ee82ee' => 'violet',
|
||||
'#f5deb3' => 'wheat'
|
||||
);
|
||||
}
|
||||
|
||||
public static function getNamedToHexMap()
|
||||
{
|
||||
// Named colors longer than hex counterpart
|
||||
return array(
|
||||
'aliceblue' => '#f0f8ff',
|
||||
'antiquewhite' => '#faebd7',
|
||||
'aquamarine' => '#7fffd4',
|
||||
'black' => '#000',
|
||||
'blanchedalmond' => '#ffebcd',
|
||||
'blueviolet' => '#8a2be2',
|
||||
'burlywood' => '#deb887',
|
||||
'cadetblue' => '#5f9ea0',
|
||||
'chartreuse' => '#7fff00',
|
||||
'chocolate' => '#d2691e',
|
||||
'cornflowerblue' => '#6495ed',
|
||||
'cornsilk' => '#fff8dc',
|
||||
'darkblue' => '#00008b',
|
||||
'darkcyan' => '#008b8b',
|
||||
'darkgoldenrod' => '#b8860b',
|
||||
'darkgray' => '#a9a9a9',
|
||||
'darkgreen' => '#006400',
|
||||
'darkgrey' => '#a9a9a9',
|
||||
'darkkhaki' => '#bdb76b',
|
||||
'darkmagenta' => '#8b008b',
|
||||
'darkolivegreen' => '#556b2f',
|
||||
'darkorange' => '#ff8c00',
|
||||
'darkorchid' => '#9932cc',
|
||||
'darksalmon' => '#e9967a',
|
||||
'darkseagreen' => '#8fbc8f',
|
||||
'darkslateblue' => '#483d8b',
|
||||
'darkslategray' => '#2f4f4f',
|
||||
'darkslategrey' => '#2f4f4f',
|
||||
'darkturquoise' => '#00ced1',
|
||||
'darkviolet' => '#9400d3',
|
||||
'deeppink' => '#ff1493',
|
||||
'deepskyblue' => '#00bfff',
|
||||
'dodgerblue' => '#1e90ff',
|
||||
'firebrick' => '#b22222',
|
||||
'floralwhite' => '#fffaf0',
|
||||
'forestgreen' => '#228b22',
|
||||
'fuchsia' => '#f0f',
|
||||
'gainsboro' => '#dcdcdc',
|
||||
'ghostwhite' => '#f8f8ff',
|
||||
'goldenrod' => '#daa520',
|
||||
'greenyellow' => '#adff2f',
|
||||
'honeydew' => '#f0fff0',
|
||||
'indianred' => '#cd5c5c',
|
||||
'lavender' => '#e6e6fa',
|
||||
'lavenderblush' => '#fff0f5',
|
||||
'lawngreen' => '#7cfc00',
|
||||
'lemonchiffon' => '#fffacd',
|
||||
'lightblue' => '#add8e6',
|
||||
'lightcoral' => '#f08080',
|
||||
'lightcyan' => '#e0ffff',
|
||||
'lightgoldenrodyellow' => '#fafad2',
|
||||
'lightgray' => '#d3d3d3',
|
||||
'lightgreen' => '#90ee90',
|
||||
'lightgrey' => '#d3d3d3',
|
||||
'lightpink' => '#ffb6c1',
|
||||
'lightsalmon' => '#ffa07a',
|
||||
'lightseagreen' => '#20b2aa',
|
||||
'lightskyblue' => '#87cefa',
|
||||
'lightslategray' => '#778899',
|
||||
'lightslategrey' => '#778899',
|
||||
'lightsteelblue' => '#b0c4de',
|
||||
'lightyellow' => '#ffffe0',
|
||||
'limegreen' => '#32cd32',
|
||||
'mediumaquamarine' => '#66cdaa',
|
||||
'mediumblue' => '#0000cd',
|
||||
'mediumorchid' => '#ba55d3',
|
||||
'mediumpurple' => '#9370db',
|
||||
'mediumseagreen' => '#3cb371',
|
||||
'mediumslateblue' => '#7b68ee',
|
||||
'mediumspringgreen' => '#00fa9a',
|
||||
'mediumturquoise' => '#48d1cc',
|
||||
'mediumvioletred' => '#c71585',
|
||||
'midnightblue' => '#191970',
|
||||
'mintcream' => '#f5fffa',
|
||||
'mistyrose' => '#ffe4e1',
|
||||
'moccasin' => '#ffe4b5',
|
||||
'navajowhite' => '#ffdead',
|
||||
'olivedrab' => '#6b8e23',
|
||||
'orangered' => '#ff4500',
|
||||
'palegoldenrod' => '#eee8aa',
|
||||
'palegreen' => '#98fb98',
|
||||
'paleturquoise' => '#afeeee',
|
||||
'palevioletred' => '#db7093',
|
||||
'papayawhip' => '#ffefd5',
|
||||
'peachpuff' => '#ffdab9',
|
||||
'powderblue' => '#b0e0e6',
|
||||
'rebeccapurple' => '#663399',
|
||||
'rosybrown' => '#bc8f8f',
|
||||
'royalblue' => '#4169e1',
|
||||
'saddlebrown' => '#8b4513',
|
||||
'sandybrown' => '#f4a460',
|
||||
'seagreen' => '#2e8b57',
|
||||
'seashell' => '#fff5ee',
|
||||
'slateblue' => '#6a5acd',
|
||||
'slategray' => '#708090',
|
||||
'slategrey' => '#708090',
|
||||
'springgreen' => '#00ff7f',
|
||||
'steelblue' => '#4682b4',
|
||||
'turquoise' => '#40e0d0',
|
||||
'white' => '#fff',
|
||||
'whitesmoke' => '#f5f5f5',
|
||||
'yellow' => '#ff0',
|
||||
'yellowgreen' => '#9acd32'
|
||||
);
|
||||
}
|
||||
}
|
||||
223
wp-content/plugins/wp-optimize/vendor/tubalmartin/cssmin/src/Command.php
vendored
Normal file
223
wp-content/plugins/wp-optimize/vendor/tubalmartin/cssmin/src/Command.php
vendored
Normal file
@@ -0,0 +1,223 @@
|
||||
<?php
|
||||
|
||||
namespace tubalmartin\CssMin;
|
||||
|
||||
class Command
|
||||
{
|
||||
const SUCCESS_EXIT = 0;
|
||||
const FAILURE_EXIT = 1;
|
||||
|
||||
protected $stats = array();
|
||||
|
||||
public static function main()
|
||||
{
|
||||
$command = new self;
|
||||
$command->run();
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
$opts = getopt(
|
||||
'hi:o:',
|
||||
array(
|
||||
'help',
|
||||
'input:',
|
||||
'output:',
|
||||
'dry-run',
|
||||
'keep-sourcemap',
|
||||
'keep-sourcemap-comment',
|
||||
'linebreak-position:',
|
||||
'memory-limit:',
|
||||
'pcre-backtrack-limit:',
|
||||
'pcre-recursion-limit:',
|
||||
'remove-important-comments'
|
||||
)
|
||||
);
|
||||
|
||||
$help = $this->getOpt(array('h', 'help'), $opts);
|
||||
$input = $this->getOpt(array('i', 'input'), $opts);
|
||||
$output = $this->getOpt(array('o', 'output'), $opts);
|
||||
$dryrun = $this->getOpt('dry-run', $opts);
|
||||
$keepSourceMapComment = $this->getOpt(array('keep-sourcemap', 'keep-sourcemap-comment'), $opts);
|
||||
$linebreakPosition = $this->getOpt('linebreak-position', $opts);
|
||||
$memoryLimit = $this->getOpt('memory-limit', $opts);
|
||||
$backtrackLimit = $this->getOpt('pcre-backtrack-limit', $opts);
|
||||
$recursionLimit = $this->getOpt('pcre-recursion-limit', $opts);
|
||||
$removeImportantComments = $this->getOpt('remove-important-comments', $opts);
|
||||
|
||||
if (!is_null($help)) {
|
||||
$this->showHelp();
|
||||
die(self::SUCCESS_EXIT);
|
||||
}
|
||||
|
||||
if (is_null($input)) {
|
||||
fwrite(STDERR, '-i <file> argument is missing' . PHP_EOL);
|
||||
$this->showHelp();
|
||||
die(self::FAILURE_EXIT);
|
||||
}
|
||||
|
||||
if (!is_readable($input)) {
|
||||
fwrite(STDERR, 'Input file is not readable' . PHP_EOL);
|
||||
die(self::FAILURE_EXIT);
|
||||
}
|
||||
|
||||
$css = file_get_contents($input);
|
||||
|
||||
if ($css === false) {
|
||||
fwrite(STDERR, 'Input CSS code could not be retrieved from input file' . PHP_EOL);
|
||||
die(self::FAILURE_EXIT);
|
||||
}
|
||||
|
||||
$this->setStat('original-size', strlen($css));
|
||||
|
||||
$cssmin = new Minifier;
|
||||
|
||||
if (!is_null($keepSourceMapComment)) {
|
||||
$cssmin->keepSourceMapComment();
|
||||
}
|
||||
|
||||
if (!is_null($removeImportantComments)) {
|
||||
$cssmin->removeImportantComments();
|
||||
}
|
||||
|
||||
if (!is_null($linebreakPosition)) {
|
||||
$cssmin->setLineBreakPosition($linebreakPosition);
|
||||
}
|
||||
|
||||
if (!is_null($memoryLimit)) {
|
||||
$cssmin->setMemoryLimit($memoryLimit);
|
||||
}
|
||||
|
||||
if (!is_null($backtrackLimit)) {
|
||||
$cssmin->setPcreBacktrackLimit($backtrackLimit);
|
||||
}
|
||||
|
||||
if (!is_null($recursionLimit)) {
|
||||
$cssmin->setPcreRecursionLimit($recursionLimit);
|
||||
}
|
||||
|
||||
$this->setStat('compression-time-start', microtime(true));
|
||||
|
||||
$css = $cssmin->run($css);
|
||||
|
||||
$this->setStat('compression-time-end', microtime(true));
|
||||
$this->setStat('peak-memory-usage', memory_get_peak_usage(true));
|
||||
$this->setStat('compressed-size', strlen($css));
|
||||
|
||||
if (!is_null($dryrun)) {
|
||||
$this->showStats();
|
||||
die(self::SUCCESS_EXIT);
|
||||
}
|
||||
|
||||
if (is_null($output)) {
|
||||
fwrite(STDOUT, $css . PHP_EOL);
|
||||
$this->showStats();
|
||||
die(self::SUCCESS_EXIT);
|
||||
}
|
||||
|
||||
if (!is_writable(dirname($output))) {
|
||||
fwrite(STDERR, 'Output file is not writable' . PHP_EOL);
|
||||
die(self::FAILURE_EXIT);
|
||||
}
|
||||
|
||||
if (file_put_contents($output, $css) === false) {
|
||||
fwrite(STDERR, 'Compressed CSS code could not be saved to output file' . PHP_EOL);
|
||||
die(self::FAILURE_EXIT);
|
||||
}
|
||||
|
||||
$this->showStats();
|
||||
|
||||
die(self::SUCCESS_EXIT);
|
||||
}
|
||||
|
||||
protected function getOpt($opts, $options)
|
||||
{
|
||||
$value = null;
|
||||
|
||||
if (is_string($opts)) {
|
||||
$opts = array($opts);
|
||||
}
|
||||
|
||||
foreach ($opts as $opt) {
|
||||
if (array_key_exists($opt, $options)) {
|
||||
$value = $options[$opt];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
protected function setStat($statName, $statValue)
|
||||
{
|
||||
$this->stats[$statName] = $statValue;
|
||||
}
|
||||
|
||||
protected function formatBytes($size, $precision = 2)
|
||||
{
|
||||
$base = log($size, 1024);
|
||||
$suffixes = array('B', 'K', 'M', 'G', 'T');
|
||||
return round(pow(1024, $base - floor($base)), $precision) .' '. $suffixes[floor($base)];
|
||||
}
|
||||
|
||||
protected function formatMicroSeconds($microSecs, $precision = 2)
|
||||
{
|
||||
// ms
|
||||
$time = round($microSecs * 1000, $precision);
|
||||
|
||||
if ($time >= 60 * 1000) {
|
||||
$time = round($time / 60 * 1000, $precision) .' m'; // m
|
||||
} elseif ($time >= 1000) {
|
||||
$time = round($time / 1000, $precision) .' s'; // s
|
||||
} else {
|
||||
$time .= ' ms';
|
||||
}
|
||||
|
||||
return $time;
|
||||
}
|
||||
|
||||
protected function showStats()
|
||||
{
|
||||
$spaceSavings = round((1 - ($this->stats['compressed-size'] / $this->stats['original-size'])) * 100, 2);
|
||||
$compressionRatio = round($this->stats['original-size'] / $this->stats['compressed-size'], 2);
|
||||
$compressionTime = $this->formatMicroSeconds(
|
||||
$this->stats['compression-time-end'] - $this->stats['compression-time-start']
|
||||
);
|
||||
$peakMemoryUsage = $this->formatBytes($this->stats['peak-memory-usage']);
|
||||
|
||||
print <<<EOT
|
||||
|
||||
------------------------------
|
||||
CSSMIN STATS
|
||||
------------------------------
|
||||
Space savings: {$spaceSavings} %
|
||||
Compression ratio: {$compressionRatio}:1
|
||||
Compression time: $compressionTime
|
||||
Peak memory usage: $peakMemoryUsage
|
||||
|
||||
|
||||
EOT;
|
||||
}
|
||||
|
||||
protected function showHelp()
|
||||
{
|
||||
print <<<'EOT'
|
||||
Usage: cssmin [options] -i <file> [-o <file>]
|
||||
|
||||
-i|--input <file> File containing uncompressed CSS code.
|
||||
-o|--output <file> File to use to save compressed CSS code.
|
||||
|
||||
Options:
|
||||
|
||||
-h|--help Prints this usage information.
|
||||
--dry-run Performs a dry run displaying statistics.
|
||||
--keep-sourcemap[-comment] Keeps the sourcemap special comment in the output.
|
||||
--linebreak-position <pos> Splits long lines after a specific column in the output.
|
||||
--memory-limit <limit> Sets the memory limit for this script.
|
||||
--pcre-backtrack-limit <limit> Sets the PCRE backtrack limit for this script.
|
||||
--pcre-recursion-limit <limit> Sets the PCRE recursion limit for this script.
|
||||
--remove-important-comments Removes !important comments from output.
|
||||
|
||||
EOT;
|
||||
}
|
||||
}
|
||||
895
wp-content/plugins/wp-optimize/vendor/tubalmartin/cssmin/src/Minifier.php
vendored
Normal file
895
wp-content/plugins/wp-optimize/vendor/tubalmartin/cssmin/src/Minifier.php
vendored
Normal file
@@ -0,0 +1,895 @@
|
||||
<?php
|
||||
|
||||
/*!
|
||||
* CssMin
|
||||
* Author: Tubal Martin - http://tubalmartin.me/
|
||||
* Repo: https://github.com/tubalmartin/YUI-CSS-compressor-PHP-port
|
||||
*
|
||||
* This is a PHP port of the CSS minification tool distributed with YUICompressor,
|
||||
* itself a port of the cssmin utility by Isaac Schlueter - http://foohack.com/
|
||||
* Permission is hereby granted to use the PHP version under the same
|
||||
* conditions as the YUICompressor.
|
||||
*/
|
||||
|
||||
/*!
|
||||
* YUI Compressor
|
||||
* http://developer.yahoo.com/yui/compressor/
|
||||
* Author: Julien Lecomte - http://www.julienlecomte.net/
|
||||
* Copyright (c) 2013 Yahoo! Inc. All rights reserved.
|
||||
* The copyrights embodied in the content of this file are licensed
|
||||
* by Yahoo! Inc. under the BSD (revised) open source license.
|
||||
*/
|
||||
|
||||
namespace tubalmartin\CssMin;
|
||||
|
||||
class Minifier
|
||||
{
|
||||
const QUERY_FRACTION = '_CSSMIN_QF_';
|
||||
const COMMENT_TOKEN = '_CSSMIN_CMT_%d_';
|
||||
const COMMENT_TOKEN_START = '_CSSMIN_CMT_';
|
||||
const RULE_BODY_TOKEN = '_CSSMIN_RBT_%d_';
|
||||
const PRESERVED_TOKEN = '_CSSMIN_PTK_%d_';
|
||||
|
||||
// Token lists
|
||||
private $comments = array();
|
||||
private $ruleBodies = array();
|
||||
private $preservedTokens = array();
|
||||
|
||||
// Output options
|
||||
private $keepImportantComments = true;
|
||||
private $keepSourceMapComment = false;
|
||||
private $linebreakPosition = 0;
|
||||
|
||||
// PHP ini limits
|
||||
private $raisePhpLimits;
|
||||
private $memoryLimit;
|
||||
private $maxExecutionTime = 60; // 1 min
|
||||
private $pcreBacktrackLimit;
|
||||
private $pcreRecursionLimit;
|
||||
|
||||
// Color maps
|
||||
private $hexToNamedColorsMap;
|
||||
private $namedToHexColorsMap;
|
||||
|
||||
// Regexes
|
||||
private $numRegex;
|
||||
private $charsetRegex = '/@charset [^;]+;/Si';
|
||||
private $importRegex = '/@import [^;]+;/Si';
|
||||
private $namespaceRegex = '/@namespace [^;]+;/Si';
|
||||
private $namedToHexColorsRegex;
|
||||
private $shortenOneZeroesRegex;
|
||||
private $shortenTwoZeroesRegex;
|
||||
private $shortenThreeZeroesRegex;
|
||||
private $shortenFourZeroesRegex;
|
||||
private $unitsGroupRegex = '(?:ch|cm|em|ex|gd|in|mm|px|pt|pc|q|rem|vh|vmax|vmin|vw|%)';
|
||||
|
||||
/**
|
||||
* @param bool|int $raisePhpLimits If true, PHP settings will be raised if needed
|
||||
*/
|
||||
public function __construct($raisePhpLimits = true)
|
||||
{
|
||||
$this->raisePhpLimits = (bool) $raisePhpLimits;
|
||||
$this->memoryLimit = 128 * 1048576; // 128MB in bytes
|
||||
$this->pcreBacktrackLimit = 1000 * 1000;
|
||||
$this->pcreRecursionLimit = 500 * 1000;
|
||||
$this->hexToNamedColorsMap = Colors::getHexToNamedMap();
|
||||
$this->namedToHexColorsMap = Colors::getNamedToHexMap();
|
||||
$this->namedToHexColorsRegex = sprintf(
|
||||
'/([:,( ])(%s)( |,|\)|;|$)/Si',
|
||||
implode('|', array_keys($this->namedToHexColorsMap))
|
||||
);
|
||||
$this->numRegex = sprintf('-?\d*\.?\d+%s?', $this->unitsGroupRegex);
|
||||
$this->setShortenZeroValuesRegexes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses & minifies the given input CSS string
|
||||
* @param string $css
|
||||
* @return string
|
||||
*/
|
||||
public function run($css = '')
|
||||
{
|
||||
if (empty($css) || !is_string($css)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$this->resetRunProperties();
|
||||
|
||||
if ($this->raisePhpLimits) {
|
||||
$this->doRaisePhpLimits();
|
||||
}
|
||||
|
||||
return $this->minify($css);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether to keep or remove sourcemap special comment.
|
||||
* Sourcemap comments are removed by default.
|
||||
* @param bool $keepSourceMapComment
|
||||
*/
|
||||
public function keepSourceMapComment($keepSourceMapComment = true)
|
||||
{
|
||||
$this->keepSourceMapComment = (bool) $keepSourceMapComment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether to keep or remove important comments.
|
||||
* Important comments outside of a declaration block are kept by default.
|
||||
* @param bool $removeImportantComments
|
||||
*/
|
||||
public function removeImportantComments($removeImportantComments = true)
|
||||
{
|
||||
$this->keepImportantComments = !(bool) $removeImportantComments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the approximate column after which long lines will be splitted in the output
|
||||
* with a linebreak.
|
||||
* @param int $position
|
||||
*/
|
||||
public function setLineBreakPosition($position)
|
||||
{
|
||||
$this->linebreakPosition = (int) $position;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the memory limit for this script
|
||||
* @param int|string $limit
|
||||
*/
|
||||
public function setMemoryLimit($limit)
|
||||
{
|
||||
$this->memoryLimit = Utils::normalizeInt($limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the maximum execution time for this script
|
||||
* @param int|string $seconds
|
||||
*/
|
||||
public function setMaxExecutionTime($seconds)
|
||||
{
|
||||
$this->maxExecutionTime = (int) $seconds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the PCRE backtrack limit for this script
|
||||
* @param int $limit
|
||||
*/
|
||||
public function setPcreBacktrackLimit($limit)
|
||||
{
|
||||
$this->pcreBacktrackLimit = (int) $limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the PCRE recursion limit for this script
|
||||
* @param int $limit
|
||||
*/
|
||||
public function setPcreRecursionLimit($limit)
|
||||
{
|
||||
$this->pcreRecursionLimit = (int) $limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds regular expressions needed for shortening zero values
|
||||
*/
|
||||
private function setShortenZeroValuesRegexes()
|
||||
{
|
||||
$zeroRegex = '0'. $this->unitsGroupRegex;
|
||||
$numOrPosRegex = '('. $this->numRegex .'|top|left|bottom|right|center) ';
|
||||
$oneZeroSafeProperties = array(
|
||||
'(?:line-)?height',
|
||||
'(?:(?:min|max)-)?width',
|
||||
'top',
|
||||
'left',
|
||||
'background-position',
|
||||
'bottom',
|
||||
'right',
|
||||
'border(?:-(?:top|left|bottom|right))?(?:-width)?',
|
||||
'border-(?:(?:top|bottom)-(?:left|right)-)?radius',
|
||||
'column-(?:gap|width)',
|
||||
'margin(?:-(?:top|left|bottom|right))?',
|
||||
'outline-width',
|
||||
'padding(?:-(?:top|left|bottom|right))?'
|
||||
);
|
||||
|
||||
// First zero regex
|
||||
$regex = '/(^|;)('. implode('|', $oneZeroSafeProperties) .'):%s/Si';
|
||||
$this->shortenOneZeroesRegex = sprintf($regex, $zeroRegex);
|
||||
|
||||
// Multiple zeroes regexes
|
||||
$regex = '/(^|;)(margin|padding|border-(?:width|radius)|background-position):%s/Si';
|
||||
$this->shortenTwoZeroesRegex = sprintf($regex, $numOrPosRegex . $zeroRegex);
|
||||
$this->shortenThreeZeroesRegex = sprintf($regex, $numOrPosRegex . $numOrPosRegex . $zeroRegex);
|
||||
$this->shortenFourZeroesRegex = sprintf($regex, $numOrPosRegex . $numOrPosRegex . $numOrPosRegex . $zeroRegex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets properties whose value may change between runs
|
||||
*/
|
||||
private function resetRunProperties()
|
||||
{
|
||||
$this->comments = array();
|
||||
$this->ruleBodies = array();
|
||||
$this->preservedTokens = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to configure PHP to use at least the suggested minimum settings
|
||||
* @return void
|
||||
*/
|
||||
private function doRaisePhpLimits()
|
||||
{
|
||||
$phpLimits = array(
|
||||
'memory_limit' => $this->memoryLimit,
|
||||
'max_execution_time' => $this->maxExecutionTime,
|
||||
'pcre.backtrack_limit' => $this->pcreBacktrackLimit,
|
||||
'pcre.recursion_limit' => $this->pcreRecursionLimit
|
||||
);
|
||||
|
||||
// If current settings are higher respect them.
|
||||
foreach ($phpLimits as $name => $suggested) {
|
||||
$current = Utils::normalizeInt(ini_get($name));
|
||||
|
||||
if ($current >= $suggested) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// memoryLimit exception: allow -1 for "no memory limit".
|
||||
if ($name === 'memory_limit' && $current === -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// maxExecutionTime exception: allow 0 for "no memory limit".
|
||||
if ($name === 'max_execution_time' && $current === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ini_set($name, $suggested);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a preserved token
|
||||
* @param string $token
|
||||
* @return string The token ID string
|
||||
*/
|
||||
private function registerPreservedToken($token)
|
||||
{
|
||||
$tokenId = sprintf(self::PRESERVED_TOKEN, count($this->preservedTokens));
|
||||
$this->preservedTokens[$tokenId] = $token;
|
||||
return $tokenId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a candidate comment token
|
||||
* @param string $comment
|
||||
* @return string The comment token ID string
|
||||
*/
|
||||
private function registerCommentToken($comment)
|
||||
{
|
||||
$tokenId = sprintf(self::COMMENT_TOKEN, count($this->comments));
|
||||
$this->comments[$tokenId] = $comment;
|
||||
return $tokenId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a rule body token
|
||||
* @param string $body the minified rule body
|
||||
* @return string The rule body token ID string
|
||||
*/
|
||||
private function registerRuleBodyToken($body)
|
||||
{
|
||||
if (empty($body)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$tokenId = sprintf(self::RULE_BODY_TOKEN, count($this->ruleBodies));
|
||||
$this->ruleBodies[$tokenId] = $body;
|
||||
return $tokenId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses & minifies the given input CSS string
|
||||
* @param string $css
|
||||
* @return string
|
||||
*/
|
||||
private function minify($css)
|
||||
{
|
||||
// Process data urls
|
||||
$css = $this->processDataUrls($css);
|
||||
|
||||
// Process comments
|
||||
$css = preg_replace_callback(
|
||||
'/(?<!\\\\)\/\*(.*?)\*(?<!\\\\)\//Ss',
|
||||
array($this, 'processCommentsCallback'),
|
||||
$css
|
||||
);
|
||||
|
||||
// IE7: Process Microsoft matrix filters (whitespaces between Matrix parameters). Can contain strings inside.
|
||||
$css = preg_replace_callback(
|
||||
'/filter:\s*progid:DXImageTransform\.Microsoft\.Matrix\(([^)]+)\)/Ss',
|
||||
array($this, 'processOldIeSpecificMatrixDefinitionCallback'),
|
||||
$css
|
||||
);
|
||||
|
||||
// Process quoted unquotable attribute selectors to unquote them. Covers most common cases.
|
||||
// Likelyhood of a quoted attribute selector being a substring in a string: Very very low.
|
||||
$css = preg_replace(
|
||||
'/\[\s*([a-z][a-z-]+)\s*([\*\|\^\$~]?=)\s*[\'"](-?[a-z_][a-z0-9-_]+)[\'"]\s*\]/Ssi',
|
||||
'[$1$2$3]',
|
||||
$css
|
||||
);
|
||||
|
||||
// Process strings so their content doesn't get accidentally minified
|
||||
$css = preg_replace_callback(
|
||||
'/(?:"(?:[^\\\\"]|\\\\.|\\\\)*")|'."(?:'(?:[^\\\\']|\\\\.|\\\\)*')/S",
|
||||
array($this, 'processStringsCallback'),
|
||||
$css
|
||||
);
|
||||
|
||||
// Normalize all whitespace strings to single spaces. Easier to work with that way.
|
||||
$css = preg_replace('/\s+/S', ' ', $css);
|
||||
|
||||
// Process import At-rules with unquoted URLs so URI reserved characters such as a semicolon may be used safely.
|
||||
$css = preg_replace_callback(
|
||||
'/@import url\(([^\'"]+?)\)( |;)/Si',
|
||||
array($this, 'processImportUnquotedUrlAtRulesCallback'),
|
||||
$css
|
||||
);
|
||||
|
||||
// Process comments
|
||||
$css = $this->processComments($css);
|
||||
|
||||
// Process rule bodies
|
||||
$css = $this->processRuleBodies($css);
|
||||
|
||||
// Process at-rules and selectors
|
||||
$css = $this->processAtRulesAndSelectors($css);
|
||||
|
||||
// Restore preserved rule bodies before splitting
|
||||
$css = strtr($css, $this->ruleBodies);
|
||||
|
||||
// Split long lines in output if required
|
||||
$css = $this->processLongLineSplitting($css);
|
||||
|
||||
// Restore preserved comments and strings
|
||||
$css = strtr($css, $this->preservedTokens);
|
||||
|
||||
return trim($css);
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches & replaces all data urls with tokens before we start compressing,
|
||||
* to avoid performance issues running some of the subsequent regexes against large string chunks.
|
||||
* @param string $css
|
||||
* @return string
|
||||
*/
|
||||
private function processDataUrls($css)
|
||||
{
|
||||
$ret = '';
|
||||
$searchOffset = $substrOffset = 0;
|
||||
|
||||
// Since we need to account for non-base64 data urls, we need to handle
|
||||
// ' and ) being part of the data string.
|
||||
while (preg_match('/url\(\s*(["\']?)data:/Si', $css, $m, PREG_OFFSET_CAPTURE, $searchOffset)) {
|
||||
$matchStartIndex = $m[0][1];
|
||||
$dataStartIndex = $matchStartIndex + 4; // url( length
|
||||
$searchOffset = $matchStartIndex + strlen($m[0][0]);
|
||||
$terminator = $m[1][0]; // ', " or empty (not quoted)
|
||||
$terminatorRegex = '/(?<!\\\\)'. (strlen($terminator) === 0 ? '' : $terminator.'\s*') .'(\))/S';
|
||||
|
||||
$ret .= substr($css, $substrOffset, $matchStartIndex - $substrOffset);
|
||||
|
||||
// Terminator found
|
||||
if (preg_match($terminatorRegex, $css, $matches, PREG_OFFSET_CAPTURE, $searchOffset)) {
|
||||
$matchEndIndex = $matches[1][1];
|
||||
$searchOffset = $matchEndIndex + 1;
|
||||
$token = substr($css, $dataStartIndex, $matchEndIndex - $dataStartIndex);
|
||||
|
||||
// Remove all spaces only for base64 encoded URLs.
|
||||
if (stripos($token, 'base64,') !== false) {
|
||||
$token = preg_replace('/\s+/S', '', $token);
|
||||
}
|
||||
|
||||
$ret .= 'url('. $this->registerPreservedToken(trim($token)) .')';
|
||||
// No end terminator found, re-add the whole match. Should we throw/warn here?
|
||||
} else {
|
||||
$ret .= substr($css, $matchStartIndex, $searchOffset - $matchStartIndex);
|
||||
}
|
||||
|
||||
$substrOffset = $searchOffset;
|
||||
}
|
||||
|
||||
$ret .= substr($css, $substrOffset);
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers all comments found as candidates to be preserved.
|
||||
* @param array $matches
|
||||
* @return string
|
||||
*/
|
||||
private function processCommentsCallback($matches)
|
||||
{
|
||||
return '/*'. $this->registerCommentToken($matches[1]) .'*/';
|
||||
}
|
||||
|
||||
/**
|
||||
* Preserves old IE Matrix string definition
|
||||
* @param array $matches
|
||||
* @return string
|
||||
*/
|
||||
private function processOldIeSpecificMatrixDefinitionCallback($matches)
|
||||
{
|
||||
return 'filter:progid:DXImageTransform.Microsoft.Matrix('. $this->registerPreservedToken($matches[1]) .')';
|
||||
}
|
||||
|
||||
/**
|
||||
* Preserves strings found
|
||||
* @param array $matches
|
||||
* @return string
|
||||
*/
|
||||
private function processStringsCallback($matches)
|
||||
{
|
||||
$match = $matches[0];
|
||||
$quote = substr($match, 0, 1);
|
||||
$match = substr($match, 1, -1);
|
||||
|
||||
// maybe the string contains a comment-like substring?
|
||||
// one, maybe more? put'em back then
|
||||
if (strpos($match, self::COMMENT_TOKEN_START) !== false) {
|
||||
$match = strtr($match, $this->comments);
|
||||
}
|
||||
|
||||
// minify alpha opacity in filter strings
|
||||
$match = str_ireplace('progid:DXImageTransform.Microsoft.Alpha(Opacity=', 'alpha(opacity=', $match);
|
||||
|
||||
return $quote . $this->registerPreservedToken($match) . $quote;
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches & replaces all import at-rule unquoted urls with tokens so URI reserved characters such as a semicolon
|
||||
* may be used safely in a URL.
|
||||
* @param array $matches
|
||||
* @return string
|
||||
*/
|
||||
private function processImportUnquotedUrlAtRulesCallback($matches)
|
||||
{
|
||||
return '@import url('. $this->registerPreservedToken($matches[1]) .')'. $matches[2];
|
||||
}
|
||||
|
||||
/**
|
||||
* Preserves or removes comments found.
|
||||
* @param string $css
|
||||
* @return string
|
||||
*/
|
||||
private function processComments($css)
|
||||
{
|
||||
foreach ($this->comments as $commentId => $comment) {
|
||||
$commentIdString = '/*'. $commentId .'*/';
|
||||
|
||||
// ! in the first position of the comment means preserve
|
||||
// so push to the preserved tokens keeping the !
|
||||
if ($this->keepImportantComments && strpos($comment, '!') === 0) {
|
||||
$preservedTokenId = $this->registerPreservedToken($comment);
|
||||
// Put new lines before and after /*! important comments
|
||||
$css = str_replace($commentIdString, "\n/*$preservedTokenId*/\n", $css);
|
||||
continue;
|
||||
}
|
||||
|
||||
// # sourceMappingURL= in the first position of the comment means sourcemap
|
||||
// so push to the preserved tokens if {$this->keepSourceMapComment} is truthy.
|
||||
if ($this->keepSourceMapComment && strpos($comment, '# sourceMappingURL=') === 0) {
|
||||
$preservedTokenId = $this->registerPreservedToken($comment);
|
||||
// Add new line before the sourcemap comment
|
||||
$css = str_replace($commentIdString, "\n/*$preservedTokenId*/", $css);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Keep empty comments after child selectors (IE7 hack)
|
||||
// e.g. html >/**/ body
|
||||
if (strlen($comment) === 0 && strpos($css, '>/*'.$commentId) !== false) {
|
||||
$css = str_replace($commentId, $this->registerPreservedToken(''), $css);
|
||||
continue;
|
||||
}
|
||||
|
||||
// in all other cases kill the comment
|
||||
$css = str_replace($commentIdString, '', $css);
|
||||
}
|
||||
|
||||
// Normalize whitespace again
|
||||
$css = preg_replace('/ +/S', ' ', $css);
|
||||
|
||||
return $css;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds, minifies & preserves all rule bodies.
|
||||
* @param string $css the whole stylesheet.
|
||||
* @return string
|
||||
*/
|
||||
private function processRuleBodies($css)
|
||||
{
|
||||
$ret = '';
|
||||
$searchOffset = $substrOffset = 0;
|
||||
|
||||
while (($blockStartPos = strpos($css, '{', $searchOffset)) !== false) {
|
||||
$blockEndPos = strpos($css, '}', $blockStartPos);
|
||||
$nextBlockStartPos = strpos($css, '{', $blockStartPos + 1);
|
||||
$ret .= substr($css, $substrOffset, $blockStartPos - $substrOffset);
|
||||
|
||||
if ($nextBlockStartPos !== false && $nextBlockStartPos < $blockEndPos) {
|
||||
$ret .= substr($css, $blockStartPos, $nextBlockStartPos - $blockStartPos);
|
||||
$searchOffset = $nextBlockStartPos;
|
||||
} else {
|
||||
$ruleBody = substr($css, $blockStartPos + 1, $blockEndPos - $blockStartPos - 1);
|
||||
$ruleBodyToken = $this->registerRuleBodyToken($this->processRuleBody($ruleBody));
|
||||
$ret .= '{'. $ruleBodyToken .'}';
|
||||
$searchOffset = $blockEndPos + 1;
|
||||
}
|
||||
|
||||
$substrOffset = $searchOffset;
|
||||
}
|
||||
|
||||
$ret .= substr($css, $substrOffset);
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compresses non-group rule bodies.
|
||||
* @param string $body The rule body without curly braces
|
||||
* @return string
|
||||
*/
|
||||
private function processRuleBody($body)
|
||||
{
|
||||
$body = trim($body);
|
||||
|
||||
// Remove spaces before the things that should not have spaces before them.
|
||||
$body = preg_replace('/ ([:=,)*\/;\n])/S', '$1', $body);
|
||||
|
||||
// Remove the spaces after the things that should not have spaces after them.
|
||||
$body = preg_replace('/([:=,(*\/!;\n]) /S', '$1', $body);
|
||||
|
||||
// Replace multiple semi-colons in a row by a single one
|
||||
$body = preg_replace('/;;+/S', ';', $body);
|
||||
|
||||
// Remove semicolon before closing brace except when:
|
||||
// - The last property is prefixed with a `*` (lte IE7 hack) to avoid issues on Symbian S60 3.x browsers.
|
||||
if (!preg_match('/\*[a-z0-9-]+:[^;]+;$/Si', $body)) {
|
||||
$body = rtrim($body, ';');
|
||||
}
|
||||
|
||||
// Remove important comments inside a rule body (because they make no sense here).
|
||||
if (strpos($body, '/*') !== false) {
|
||||
$body = preg_replace('/\n?\/\*[A-Z0-9_]+\*\/\n?/S', '', $body);
|
||||
}
|
||||
|
||||
// Empty rule body? Exit :)
|
||||
if (empty($body)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
// Shorten font-weight values
|
||||
$body = preg_replace(
|
||||
array('/(font-weight:)bold\b/Si', '/(font-weight:)normal\b/Si'),
|
||||
array('${1}700', '${1}400'),
|
||||
$body
|
||||
);
|
||||
|
||||
// Shorten background property
|
||||
$body = preg_replace('/(background:)(?:none|transparent)( !|;|$)/Si', '${1}0 0$2', $body);
|
||||
|
||||
// Shorten opacity IE filter
|
||||
$body = str_ireplace('progid:DXImageTransform.Microsoft.Alpha(Opacity=', 'alpha(opacity=', $body);
|
||||
|
||||
// Shorten colors from rgb(51,102,153) to #336699, rgb(100%,0%,0%) to #ff0000 (sRGB color space)
|
||||
// Shorten colors from hsl(0, 100%, 50%) to #ff0000 (sRGB color space)
|
||||
// This makes it more likely that it'll get further compressed in the next step.
|
||||
$body = preg_replace_callback(
|
||||
'/(rgb|hsl)\(([0-9,.% -]+)\)(.|$)/Si',
|
||||
array($this, 'shortenHslAndRgbToHexCallback'),
|
||||
$body
|
||||
);
|
||||
|
||||
// Shorten colors from #AABBCC to #ABC or shorter color name:
|
||||
// - Look for hex colors which don't have a "=" in front of them (to avoid MSIE filters)
|
||||
$body = preg_replace_callback(
|
||||
'/(?<!=)#([0-9a-f]{3,6})( |,|\)|;|$)/Si',
|
||||
array($this, 'shortenHexColorsCallback'),
|
||||
$body
|
||||
);
|
||||
|
||||
// Shorten long named colors with a shorter HEX counterpart: white -> #fff.
|
||||
// Run at least 2 times to cover most cases
|
||||
$body = preg_replace_callback(
|
||||
array($this->namedToHexColorsRegex, $this->namedToHexColorsRegex),
|
||||
array($this, 'shortenNamedColorsCallback'),
|
||||
$body
|
||||
);
|
||||
|
||||
// Replace positive sign from numbers before the leading space is removed.
|
||||
// +1.2em to 1.2em, +.8px to .8px, +2% to 2%
|
||||
$body = preg_replace('/([ :,(])\+(\.?\d+)/S', '$1$2', $body);
|
||||
|
||||
// shorten ms to s
|
||||
$body = preg_replace_callback('/([ :,(])(-?)(\d{3,})ms/Si', function ($matches) {
|
||||
return $matches[1] . $matches[2] . ((int) $matches[3] / 1000) .'s';
|
||||
}, $body);
|
||||
|
||||
// Remove leading zeros from integer and float numbers.
|
||||
// 000.6 to .6, -0.8 to -.8, 0050 to 50, -01.05 to -1.05
|
||||
$body = preg_replace('/([ :,(])(-?)0+([1-9]?\.?\d+)/S', '$1$2$3', $body);
|
||||
|
||||
// Remove trailing zeros from float numbers.
|
||||
// -6.0100em to -6.01em, .0100 to .01, 1.200px to 1.2px
|
||||
$body = preg_replace('/([ :,(])(-?\d?\.\d+?)0+([^\d])/S', '$1$2$3', $body);
|
||||
|
||||
// Remove trailing .0 -> -9.0 to -9
|
||||
$body = preg_replace('/([ :,(])(-?\d+)\.0([^\d])/S', '$1$2$3', $body);
|
||||
|
||||
// Replace 0 length numbers with 0
|
||||
$body = preg_replace('/([ :,(])-?\.?0+([^\d])/S', '${1}0$2', $body);
|
||||
|
||||
// Shorten zero values for safe properties only
|
||||
$body = preg_replace(
|
||||
array(
|
||||
$this->shortenOneZeroesRegex,
|
||||
$this->shortenTwoZeroesRegex,
|
||||
$this->shortenThreeZeroesRegex,
|
||||
$this->shortenFourZeroesRegex
|
||||
),
|
||||
array(
|
||||
'$1$2:0',
|
||||
'$1$2:$3 0',
|
||||
'$1$2:$3 $4 0',
|
||||
'$1$2:$3 $4 $5 0'
|
||||
),
|
||||
$body
|
||||
);
|
||||
|
||||
// Replace 0 0 0; or 0 0 0 0; with 0 0 for background-position property.
|
||||
$body = preg_replace('/(background-position):0(?: 0){2,3}( !|;|$)/Si', '$1:0 0$2', $body);
|
||||
|
||||
// Shorten suitable shorthand properties with repeated values
|
||||
$body = preg_replace(
|
||||
array(
|
||||
'/(margin|padding|border-(?:width|radius)):('.$this->numRegex.')(?: \2)+( !|;|$)/Si',
|
||||
'/(border-(?:style|color)):([#a-z0-9]+)(?: \2)+( !|;|$)/Si'
|
||||
),
|
||||
'$1:$2$3',
|
||||
$body
|
||||
);
|
||||
$body = preg_replace(
|
||||
array(
|
||||
'/(margin|padding|border-(?:width|radius)):'.
|
||||
'('.$this->numRegex.') ('.$this->numRegex.') \2 \3( !|;|$)/Si',
|
||||
'/(border-(?:style|color)):([#a-z0-9]+) ([#a-z0-9]+) \2 \3( !|;|$)/Si'
|
||||
),
|
||||
'$1:$2 $3$4',
|
||||
$body
|
||||
);
|
||||
$body = preg_replace(
|
||||
array(
|
||||
'/(margin|padding|border-(?:width|radius)):'.
|
||||
'('.$this->numRegex.') ('.$this->numRegex.') ('.$this->numRegex.') \3( !|;|$)/Si',
|
||||
'/(border-(?:style|color)):([#a-z0-9]+) ([#a-z0-9]+) ([#a-z0-9]+) \3( !|;|$)/Si'
|
||||
),
|
||||
'$1:$2 $3 $4$5',
|
||||
$body
|
||||
);
|
||||
|
||||
// Lowercase some common functions that can be values
|
||||
$body = preg_replace_callback(
|
||||
'/(?:attr|blur|brightness|circle|contrast|cubic-bezier|drop-shadow|ellipse|from|grayscale|'.
|
||||
'hsla?|hue-rotate|inset|invert|local|minmax|opacity|perspective|polygon|rgba?|rect|repeat|saturate|sepia|'.
|
||||
'steps|to|url|var|-webkit-gradient|'.
|
||||
'(?:-(?:atsc|khtml|moz|ms|o|wap|webkit)-)?(?:calc|(?:repeating-)?(?:linear|radial)-gradient))\(/Si',
|
||||
array($this, 'strtolowerCallback'),
|
||||
$body
|
||||
);
|
||||
|
||||
// Lowercase all uppercase properties
|
||||
$body = preg_replace_callback('/(?:^|;)[A-Z-]+:/S', array($this, 'strtolowerCallback'), $body);
|
||||
|
||||
return $body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compresses At-rules and selectors.
|
||||
* @param string $css the whole stylesheet with rule bodies tokenized.
|
||||
* @return string
|
||||
*/
|
||||
private function processAtRulesAndSelectors($css)
|
||||
{
|
||||
$charset = '';
|
||||
$imports = '';
|
||||
$namespaces = '';
|
||||
|
||||
// Remove spaces before the things that should not have spaces before them.
|
||||
$css = preg_replace('/ ([@{};>+)\]~=,\/\n])/S', '$1', $css);
|
||||
|
||||
// Remove the spaces after the things that should not have spaces after them.
|
||||
$css = preg_replace('/([{}:;>+(\[~=,\/\n]) /S', '$1', $css);
|
||||
|
||||
// Shorten shortable double colon (CSS3) pseudo-elements to single colon (CSS2)
|
||||
$css = preg_replace('/::(before|after|first-(?:line|letter))(\{|,)/Si', ':$1$2', $css);
|
||||
|
||||
// Retain space for special IE6 cases
|
||||
$css = preg_replace_callback('/:first-(line|letter)(\{|,)/Si', function ($matches) {
|
||||
return ':first-'. strtolower($matches[1]) .' '. $matches[2];
|
||||
}, $css);
|
||||
|
||||
// Find a fraction that may used in some @media queries such as: (min-aspect-ratio: 1/1)
|
||||
// Add token to add the "/" back in later
|
||||
$css = preg_replace('/\(([a-z-]+):([0-9]+)\/([0-9]+)\)/Si', '($1:$2'. self::QUERY_FRACTION .'$3)', $css);
|
||||
|
||||
// Remove empty rule blocks up to 2 levels deep.
|
||||
$css = preg_replace(array_fill(0, 2, '/(\{)[^{};\/\n]+\{\}/S'), '$1', $css);
|
||||
$css = preg_replace('/[^{};\/\n]+\{\}/S', '', $css);
|
||||
|
||||
// Two important comments next to each other? Remove extra newline.
|
||||
if ($this->keepImportantComments) {
|
||||
$css = str_replace("\n\n", "\n", $css);
|
||||
}
|
||||
|
||||
// Restore fraction
|
||||
$css = str_replace(self::QUERY_FRACTION, '/', $css);
|
||||
|
||||
// Lowercase some popular @directives
|
||||
$css = preg_replace_callback(
|
||||
'/(?<!\\\\)@(?:charset|document|font-face|import|(?:-(?:atsc|khtml|moz|ms|o|wap|webkit)-)?keyframes|media|'.
|
||||
'namespace|page|supports|viewport)/Si',
|
||||
array($this, 'strtolowerCallback'),
|
||||
$css
|
||||
);
|
||||
|
||||
// Lowercase some popular media types
|
||||
$css = preg_replace_callback(
|
||||
'/[ ,](?:all|aural|braille|handheld|print|projection|screen|tty|tv|embossed|speech)[ ,;{]/Si',
|
||||
array($this, 'strtolowerCallback'),
|
||||
$css
|
||||
);
|
||||
|
||||
// Lowercase some common pseudo-classes & pseudo-elements
|
||||
$css = preg_replace_callback(
|
||||
'/(?<!\\\\):(?:active|after|before|checked|default|disabled|empty|enabled|first-(?:child|of-type)|'.
|
||||
'focus(?:-within)?|hover|indeterminate|in-range|invalid|lang\(|last-(?:child|of-type)|left|link|not\(|'.
|
||||
'nth-(?:child|of-type)\(|nth-last-(?:child|of-type)\(|only-(?:child|of-type)|optional|out-of-range|'.
|
||||
'read-(?:only|write)|required|right|root|:selection|target|valid|visited)/Si',
|
||||
array($this, 'strtolowerCallback'),
|
||||
$css
|
||||
);
|
||||
|
||||
// @charset handling
|
||||
if (preg_match($this->charsetRegex, $css, $matches)) {
|
||||
// Keep the first @charset at-rule found
|
||||
$charset = $matches[0];
|
||||
// Delete all @charset at-rules
|
||||
$css = preg_replace($this->charsetRegex, '', $css);
|
||||
}
|
||||
|
||||
// @import handling
|
||||
$css = preg_replace_callback($this->importRegex, function ($matches) use (&$imports) {
|
||||
// Keep all @import at-rules found for later
|
||||
$imports .= $matches[0];
|
||||
// Delete all @import at-rules
|
||||
return '';
|
||||
}, $css);
|
||||
|
||||
// @namespace handling
|
||||
$css = preg_replace_callback($this->namespaceRegex, function ($matches) use (&$namespaces) {
|
||||
// Keep all @namespace at-rules found for later
|
||||
$namespaces .= $matches[0];
|
||||
// Delete all @namespace at-rules
|
||||
return '';
|
||||
}, $css);
|
||||
|
||||
// Order critical at-rules:
|
||||
// 1. @charset first
|
||||
// 2. @imports below @charset
|
||||
// 3. @namespaces below @imports
|
||||
$css = $charset . $imports . $namespaces . $css;
|
||||
|
||||
return $css;
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits long lines after a specific column.
|
||||
*
|
||||
* Some source control tools don't like it when files containing lines longer
|
||||
* than, say 8000 characters, are checked in. The linebreak option is used in
|
||||
* that case to split long lines after a specific column.
|
||||
*
|
||||
* @param string $css the whole stylesheet.
|
||||
* @return string
|
||||
*/
|
||||
private function processLongLineSplitting($css)
|
||||
{
|
||||
if ($this->linebreakPosition > 0) {
|
||||
$l = strlen($css);
|
||||
$offset = $this->linebreakPosition;
|
||||
while (preg_match('/(?<!\\\\)\}(?!\n)/S', $css, $matches, PREG_OFFSET_CAPTURE, $offset)) {
|
||||
$matchIndex = $matches[0][1];
|
||||
$css = substr_replace($css, "\n", $matchIndex + 1, 0);
|
||||
$offset = $matchIndex + 2 + $this->linebreakPosition;
|
||||
$l += 1;
|
||||
if ($offset > $l) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $css;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts hsl() & rgb() colors to HEX format.
|
||||
* @param $matches
|
||||
* @return string
|
||||
*/
|
||||
private function shortenHslAndRgbToHexCallback($matches)
|
||||
{
|
||||
$type = $matches[1];
|
||||
$values = explode(',', $matches[2]);
|
||||
$terminator = $matches[3];
|
||||
|
||||
if ($type === 'hsl') {
|
||||
$values = Utils::hslToRgb($values);
|
||||
}
|
||||
|
||||
$hexColors = Utils::rgbToHex($values);
|
||||
|
||||
// Restore space after rgb() or hsl() function in some cases such as:
|
||||
// background-image: linear-gradient(to bottom, rgb(210,180,140) 10%, rgb(255,0,0) 90%);
|
||||
if (!empty($terminator) && !preg_match('/[ ,);]/S', $terminator)) {
|
||||
$terminator = ' '. $terminator;
|
||||
}
|
||||
|
||||
return '#'. implode('', $hexColors) . $terminator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compresses HEX color values of the form #AABBCC to #ABC or short color name.
|
||||
* @param $matches
|
||||
* @return string
|
||||
*/
|
||||
private function shortenHexColorsCallback($matches)
|
||||
{
|
||||
$hex = $matches[1];
|
||||
|
||||
// Shorten suitable 6 chars HEX colors
|
||||
if (strlen($hex) === 6 && preg_match('/^([0-9a-f])\1([0-9a-f])\2([0-9a-f])\3$/Si', $hex, $m)) {
|
||||
$hex = $m[1] . $m[2] . $m[3];
|
||||
}
|
||||
|
||||
// Lowercase
|
||||
$hex = '#'. strtolower($hex);
|
||||
|
||||
// Replace Hex colors with shorter color names
|
||||
$color = array_key_exists($hex, $this->hexToNamedColorsMap) ? $this->hexToNamedColorsMap[$hex] : $hex;
|
||||
|
||||
return $color . $matches[2];
|
||||
}
|
||||
|
||||
/**
|
||||
* Shortens all named colors with a shorter HEX counterpart for a set of safe properties
|
||||
* e.g. white -> #fff
|
||||
* @param array $matches
|
||||
* @return string
|
||||
*/
|
||||
private function shortenNamedColorsCallback($matches)
|
||||
{
|
||||
return $matches[1] . $this->namedToHexColorsMap[strtolower($matches[2])] . $matches[3];
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes a string lowercase
|
||||
* @param array $matches
|
||||
* @return string
|
||||
*/
|
||||
private function strtolowerCallback($matches)
|
||||
{
|
||||
return strtolower($matches[0]);
|
||||
}
|
||||
}
|
||||
149
wp-content/plugins/wp-optimize/vendor/tubalmartin/cssmin/src/Utils.php
vendored
Normal file
149
wp-content/plugins/wp-optimize/vendor/tubalmartin/cssmin/src/Utils.php
vendored
Normal file
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
namespace tubalmartin\CssMin;
|
||||
|
||||
class Utils
|
||||
{
|
||||
/**
|
||||
* Clamps a number between a minimum and a maximum value.
|
||||
* @param int|float $n the number to clamp
|
||||
* @param int|float $min the lower end number allowed
|
||||
* @param int|float $max the higher end number allowed
|
||||
* @return int|float
|
||||
*/
|
||||
public static function clampNumber($n, $min, $max)
|
||||
{
|
||||
return min(max($n, $min), $max);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clamps a RGB color number outside the sRGB color space
|
||||
* @param int|float $n the number to clamp
|
||||
* @return int|float
|
||||
*/
|
||||
public static function clampNumberSrgb($n)
|
||||
{
|
||||
return self::clampNumber($n, 0, 255);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a HSL color into a RGB color
|
||||
* @param array $hslValues
|
||||
* @return array
|
||||
*/
|
||||
public static function hslToRgb($hslValues)
|
||||
{
|
||||
$h = floatval($hslValues[0]);
|
||||
$s = floatval(str_replace('%', '', $hslValues[1]));
|
||||
$l = floatval(str_replace('%', '', $hslValues[2]));
|
||||
|
||||
// Wrap and clamp, then fraction!
|
||||
$h = ((($h % 360) + 360) % 360) / 360;
|
||||
$s = self::clampNumber($s, 0, 100) / 100;
|
||||
$l = self::clampNumber($l, 0, 100) / 100;
|
||||
|
||||
if ($s == 0) {
|
||||
$r = $g = $b = self::roundNumber(255 * $l);
|
||||
} else {
|
||||
$v2 = $l < 0.5 ? $l * (1 + $s) : ($l + $s) - ($s * $l);
|
||||
$v1 = (2 * $l) - $v2;
|
||||
$r = self::roundNumber(255 * self::hueToRgb($v1, $v2, $h + (1/3)));
|
||||
$g = self::roundNumber(255 * self::hueToRgb($v1, $v2, $h));
|
||||
$b = self::roundNumber(255 * self::hueToRgb($v1, $v2, $h - (1/3)));
|
||||
}
|
||||
|
||||
return array($r, $g, $b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests and selects the correct formula for each RGB color channel
|
||||
* @param $v1
|
||||
* @param $v2
|
||||
* @param $vh
|
||||
* @return mixed
|
||||
*/
|
||||
public static function hueToRgb($v1, $v2, $vh)
|
||||
{
|
||||
$vh = $vh < 0 ? $vh + 1 : ($vh > 1 ? $vh - 1 : $vh);
|
||||
|
||||
if ($vh * 6 < 1) {
|
||||
return $v1 + ($v2 - $v1) * 6 * $vh;
|
||||
}
|
||||
|
||||
if ($vh * 2 < 1) {
|
||||
return $v2;
|
||||
}
|
||||
|
||||
if ($vh * 3 < 2) {
|
||||
return $v1 + ($v2 - $v1) * ((2 / 3) - $vh) * 6;
|
||||
}
|
||||
|
||||
return $v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert strings like "64M" or "30" to int values
|
||||
* @param mixed $size
|
||||
* @return int
|
||||
*/
|
||||
public static function normalizeInt($size)
|
||||
{
|
||||
if (is_string($size)) {
|
||||
$letter = substr($size, -1);
|
||||
$size = intval($size);
|
||||
switch ($letter) {
|
||||
case 'M':
|
||||
case 'm':
|
||||
return (int) $size * 1048576;
|
||||
case 'K':
|
||||
case 'k':
|
||||
return (int) $size * 1024;
|
||||
case 'G':
|
||||
case 'g':
|
||||
return (int) $size * 1073741824;
|
||||
}
|
||||
}
|
||||
return (int) $size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a string containing and RGB percentage value into a RGB integer value i.e. '90%' -> 229.5
|
||||
* @param $rgbPercentage
|
||||
* @return int
|
||||
*/
|
||||
public static function rgbPercentageToRgbInteger($rgbPercentage)
|
||||
{
|
||||
if (strpos($rgbPercentage, '%') !== false) {
|
||||
$rgbPercentage = self::roundNumber(floatval(str_replace('%', '', $rgbPercentage)) * 2.55);
|
||||
}
|
||||
|
||||
return intval($rgbPercentage, 10);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a RGB color into a HEX color
|
||||
* @param array $rgbColors
|
||||
* @return array
|
||||
*/
|
||||
public static function rgbToHex($rgbColors)
|
||||
{
|
||||
$hexColors = array();
|
||||
|
||||
// Values outside the sRGB color space should be clipped (0-255)
|
||||
for ($i = 0, $l = count($rgbColors); $i < $l; $i++) {
|
||||
$hexColors[$i] = sprintf("%02x", self::clampNumberSrgb(self::rgbPercentageToRgbInteger($rgbColors[$i])));
|
||||
}
|
||||
|
||||
return $hexColors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rounds a number to its closest integer
|
||||
* @param $n
|
||||
* @return int
|
||||
*/
|
||||
public static function roundNumber($n)
|
||||
{
|
||||
return intval(round(floatval($n)), 10);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user