Skip to content

Commit

Permalink
Merge 90d5285 into 41db076
Browse files Browse the repository at this point in the history
  • Loading branch information
dtbaker committed Feb 10, 2017
2 parents 41db076 + 90d5285 commit 538091e
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions classes/autoptimizeStyles.php
Expand Up @@ -2,6 +2,9 @@
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

class autoptimizeStyles extends autoptimizeBase {

const ASSETS_REGEX = '/url\s*\(\s*(?!["\']?data:)([^)]+)\s*\)/i';

private $css = array();
private $csscode = array();
private $url = array();
Expand Down Expand Up @@ -303,10 +306,15 @@ public function minify() {

// Do the imaging!
$imgreplace = array();
preg_match_all('#(background[^;{}]*url\((?!\s?"?\'?\s?data)(.*)\)[^;}]*)(?:;|$|})#Usm',$code,$matches);

// dtbaker update. Instead of searching for the entire `background: url(), url(), url();` string we just search for the individual url() elements.
// this allows us to find/replace multiple background images with minimal code changes below.
// this is the old regex that searched for the entire background css rule, but it wouldn't match multiple background image url css rules.
// preg_match_all('#(background[^;{}]*url\((?!\s?"?\'?\s?data)(.*)\)[^;}]*)(?:;|$|})#Usm',$code,$matches);
// this new regex will be slightly faster too:
preg_match_all( self::ASSETS_REGEX, $code, $matches );

if(($this->datauris == true) && (function_exists('base64_encode')) && (is_array($matches))) {
foreach($matches[2] as $count => $quotedurl) {
foreach($matches[1] as $count => $quotedurl) {
$iurl = trim($quotedurl," \t\n\r\0\x0B\"'");

// if querystring, remove it from url
Expand Down Expand Up @@ -370,7 +378,7 @@ public function minify() {
unset($icheck);

// Add it to the list for replacement
$imgreplace[$matches[1][$count]] = str_replace($quotedurl,$headAndData,$matches[1][$count]).";\n*".str_replace($quotedurl,'mhtml:%%MHTML%%!'.$mhtmlcount,$matches[1][$count]).";\n_".$matches[1][$count].';';
$imgreplace[$matches[0][$count]] = str_replace($quotedurl,$headAndData,$matches[0][$count]).";\n*".str_replace($quotedurl,'mhtml:%%MHTML%%!'.$mhtmlcount,$matches[0][$count]).";\n_".$matches[0][$count].';';

// Store image on the mhtml document
$this->mhtml .= "--_\r\nContent-Location:{$mhtmlcount}\r\nContent-Transfer-Encoding:base64\r\n\r\n{$base64data}\r\n";
Expand All @@ -380,16 +388,16 @@ public function minify() {
if (!empty($this->cdn_url)) {
$url = trim($quotedurl," \t\n\r\0\x0B\"'");
$cdn_url=$this->url_replace_cdn($url);
$imgreplace[$matches[1][$count]] = str_replace($quotedurl,$cdn_url,$matches[1][$count]);
$imgreplace[$matches[0][$count]] = str_replace($quotedurl,$cdn_url,$matches[0][$count]);
}
}
}
} else if ((is_array($matches)) && (!empty($this->cdn_url))) {
// change background image urls to cdn-url
foreach($matches[2] as $count => $quotedurl) {
foreach($matches[1] as $count => $quotedurl) {
$url = trim($quotedurl," \t\n\r\0\x0B\"'");
$cdn_url=$this->url_replace_cdn($url);
$imgreplace[$matches[1][$count]] = str_replace($quotedurl,$cdn_url,$matches[1][$count]);
$imgreplace[$matches[0][$count]] = str_replace($quotedurl,$cdn_url,$matches[0][$count]);
}
}

Expand Down Expand Up @@ -583,7 +591,7 @@ static function fixurls($file,$code) {
// quick fix for import-troubles in e.g. arras theme
$code=preg_replace('#@import ("|\')(.+?)\.css("|\')#','@import url("${2}.css")',$code);

if(preg_match_all('#url\((?!data)(?!\#)(?!"\#)(.*)\)#Usi',$code,$matches)) {
if( preg_match_all( self::ASSETS_REGEX, $code, $matches ) ) {
$replace = array();
foreach($matches[1] as $k => $url) {
// Remove quotes
Expand Down

0 comments on commit 538091e

Please sign in to comment.