/** * Uses regex to find css declarations containing background images to include in sprite * * @param string $sCss Aggregated css file * @return array Array of css declarations and image urls to replace with sprite */ public function processCssUrls($sCss, $bBackend = FALSE) { //JCH_DEBUG ? JchPlatformProfiler::mark('beforeProcessCssUrls plgSystem (JCH Optimize)') : null; $params = $this->params; $aRegexStart = array(); $aRegexStart[0] = ' #(?:{ (?=\\s*+(?>[^}\\s:]++[:\\s]++)*?url\\( (?=[^)]+\\.(?:png|gif|jpe?g)) ([^)]+)\\))'; $aRegexStart[1] = ' (?=\\s*+(?>[^}\\s:]++[:\\s]++)*?no-repeat)'; $aRegexStart[2] = ' (?(?=\\s*(?>[^};]++[;\\s]++)*?background(?:-position)?\\s*+:\\s*+(?:[^;}\\s]++[^}\\S]++)*? (?<p>(?:[tblrc](?:op|ottom|eft|ight|enter)|-?[0-9]+(?:%|[c-x]{2})?))(?:\\s+(?&p))?) (?=\\s*(?>[^};]++[;\\s]++)*?background(?:-position)?\\s*+:\\s*+(?>[^;}\\s]++[\\s]++)*? (?:left|top|0(?:%|[c-x]{2})?)\\s+(?:left|top|0(?:%|[c-x]{2})?) ) )'; $sRegexMiddle = ' [^{}]++} )'; $sRegexEnd = '#isx'; $aIncludeImages = JchOptimize::getArray($params->get('csg_include_images')); $aExcludeImages = JchOptimize::getArray($params->get('csg_exclude_images')); $sIncImagesRegex = ''; if (!empty($aIncludeImages[0])) { foreach ($aIncludeImages as &$sImage) { $sImage = preg_quote($sImage, '#'); } $sIncImagesRegex .= ' |(?:{ (?=\\s*+(?>[^}\\s:]++[:\\s]++)*?url'; $sIncImagesRegex .= ' (?=[^)]* [/(](?:' . implode('|', $aIncludeImages) . ')\\))'; $sIncImagesRegex .= '\\(([^)]+)\\) ) [^{}]++ })'; } $sExImagesRegex = ''; if (!empty($aExcludeImages[0])) { $sExImagesRegex .= '(?=\\s*+(?>[^}\\s:]++[:\\s]++)*?url\\( [^)]++ (?<!'; foreach ($aExcludeImages as &$sImage) { $sImage = preg_quote($sImage, '#'); } $sExImagesRegex .= implode('|', $aExcludeImages) . ')\\) )'; } $sRegexStart = implode('', $aRegexStart); $sRegex = $sRegexStart . $sExImagesRegex . $sRegexMiddle . $sIncImagesRegex . $sRegexEnd; if (preg_match_all($sRegex, $sCss, $aMatches) === FALSE) { throw new Exception(JchPlatformUtility::translate('Error occurred matching for images to sprite')); } if (isset($aMatches[3])) { $total = count($aMatches[1]); for ($i = 0; $i < $total; $i++) { $aMatches[1][$i] = trim($aMatches[1][$i]) ? $aMatches[1][$i] : $aMatches[3][$i]; } } if ($bBackend) { $aImages = array(); $aImagesMatches = array(); $aImgRegex = array(); $aImgRegex[0] = $aRegexStart[0]; $aImgRegex[2] = $aRegexStart[1]; $aImgRegex[4] = $sRegexMiddle; $aImgRegex[5] = $sRegexEnd; $sImgRegex = implode('', $aImgRegex); if (preg_match_all($sImgRegex, $sCss, $aImagesMatches) === FALSE) { throw new Exception(JchPlatformUtility::translate('Error occurred matching for images to include')); } $aImagesMatches[0] = array_diff($aImagesMatches[0], $aMatches[0]); $aImagesMatches[1] = array_diff($aImagesMatches[1], $aMatches[1]); $oCssSpriteGen = new CssSpriteGen($this->getImageLibrary(), $this->params, $bBackend); $aImages['include'] = $oCssSpriteGen->CreateSprite($aImagesMatches[1]); $aImages['exclude'] = $oCssSpriteGen->CreateSprite($aMatches[1]); return $aImages; } //JCH_DEBUG ? JchPlatformProfiler::mark('afterProcessCssUrls plgSystem (JCH Optimize)') : null; return $aMatches; }
/** * Uses regex to find css declarations containing background images to include in sprite * * @param string $sCss Aggregated css file * @return array Array of css declarations and image urls to replace with sprite */ public function processCssUrls($sCss, $bBackend = FALSE) { $params = $this->params; $aRegexStart = array(); $aRegexStart[0] = ' #(?:{ (?=\\s*+(?>[^}\\s:]++[:\\s]++)*?url\\( (?=[^)]+\\.(?:png|gif|jpe?g)) ([^)]+)\\))'; $aRegexStart[1] = ' (?=\\s*+(?>[^}\\s:]++[:\\s]++)*?no-repeat)'; $aRegexStart[2] = ' (?(?=\\s*(?>[^};]++[;\\s]++)*?background(?:-position)?\\s*+:\\s*+(?:[^;}\\s]++[^}\\S]++)*? (?<p>(?:[tblrc](?:op|ottom|eft|ight|enter)|-?[0-9]+(?:%|[c-x]{2})?))(?:\\s+(?&p))?) (?=\\s*(?>[^};]++[;\\s]++)*?background(?:-position)?\\s*+:\\s*+(?>[^;}\\s]++[\\s]++)*? (?:left|top|0(?:%|[c-x]{2})?)\\s+(?:left|top|0(?:%|[c-x]{2})?) ) )'; $sRegexMiddle = ' [^{}]++} )'; $sRegexEnd = '#isx'; $aIncludeImages = JchOptimize::getArray($params->get('csg_include_images')); $aExcludeImages = JchOptimize::getArray($params->get('csg_exclude_images')); $sIncImagesRegex = ''; if (!empty($aIncludeImages[0])) { foreach ($aIncludeImages as &$sImage) { $sImage = preg_quote($sImage, '#'); } $sIncImagesRegex .= ' |(?:{ (?=\\s*+(?>[^}\\s:]++[:\\s]++)*?url'; $sIncImagesRegex .= ' (?=[^)]* [/(](?:' . implode('|', $aIncludeImages) . ')\\))'; $sIncImagesRegex .= '\\(([^)]+)\\) ) [^{}]++ })'; } $sExImagesRegex = ''; if (!empty($aExcludeImages[0])) { $sExImagesRegex .= '(?=\\s*+(?>[^}\\s:]++[:\\s]++)*?url\\( [^)]++ (?<!'; foreach ($aExcludeImages as &$sImage) { $sImage = preg_quote($sImage, '#'); } $sExImagesRegex .= implode('|', $aExcludeImages) . ')\\) )'; } $sRegexStart = implode('', $aRegexStart); $sRegex = $sRegexStart . $sExImagesRegex . $sRegexMiddle . $sIncImagesRegex . $sRegexEnd; if (preg_match_all($sRegex, $sCss, $aMatches) === FALSE) { throw new Exception(JText::_('Error occurred matching for images to sprite')); } if (isset($aMatches[3])) { $aMatches[1] = array_merge(array_filter($aMatches[1]), array_filter($aMatches[3])); } if ($bBackend) { $aImages = array(); $aImagesMatches = array(); $aImgRegex = array(); $aImgRegex[0] = $aRegexStart[0]; $aImgRegex[2] = $aRegexStart[1]; $aImgRegex[4] = $sRegexMiddle; $aImgRegex[5] = $sRegexEnd; $sImgRegex = implode('', $aImgRegex); if (preg_match_all($sImgRegex, $sCss, $aImagesMatches) === FALSE) { throw new Exception(JText::_('Error occurred matching for images to include')); } $aImagesMatches[0] = array_diff($aImagesMatches[0], $aMatches[0]); $aImagesMatches[1] = array_diff($aImagesMatches[1], $aMatches[1]); $aImages['include'] = $aImagesMatches; $aImages['exclude'] = $aMatches; return $aImages; } return $aMatches; }
/** * Generates regex for excluding components set in plugin params * * @param string $param * @return string */ protected function getExCompRegex($sExComParam) { $aComponents = array_filter(JchOptimize::getArray($sExComParam)); $aExComp = array(); if (!empty($aComponents)) { $aExComp = array_map(function ($sValue) { return $sValue . '/'; }, $aComponents); } return $aExComp; }