Same name in other branches
- 6.0.x advagg_mod/src/Asset/TranslateCss.php \Drupal\advagg_mod\Asset\TranslateCss
- 8.x-3.x advagg_mod/src/Asset/TranslateCss.php \Drupal\advagg_mod\Asset\TranslateCss
- 8.x-4.x advagg_mod/src/Asset/TranslateCss.php \Drupal\advagg_mod\Asset\TranslateCss
Applies the t() function to strings in CSS assets.
Hierarchy
- class \Drupal\advagg\Asset\SingleAssetOptimizerBase
- class \Drupal\advagg_mod\Asset\TranslateCss extends \Drupal\advagg\Asset\SingleAssetOptimizerBase uses \Drupal\Core\StringTranslation\StringTranslationTrait
Expanded class hierarchy of TranslateCss
1 file declares its use of TranslateCss
- InitSubscriber.php in advagg_mod/
src/ EventSubscriber/ InitSubscriber.php
2 string references to 'TranslateCss'
- advagg_mod.services.yml in advagg_mod/
advagg_mod.services.yml - advagg_mod/advagg_mod.services.yml
- InitSubscriber::getSubscribedEvents in advagg_mod/
src/ EventSubscriber/ InitSubscriber.php
1 service uses TranslateCss
File
-
advagg_mod/
src/ Asset/ TranslateCss.php, line 13
Namespace
Drupal\advagg_mod\AssetView source
class TranslateCss extends SingleAssetOptimizerBase {
use StringTranslationTrait;
/**
* Construct the optimizer instance.
*
* @param \Psr\Log\LoggerInterface $logger
* The logger service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* A config factory for retrieving required config objects.
*/
public function __construct(LoggerInterface $logger, ConfigFactoryInterface $config_factory) {
parent::__construct($logger);
$this->config = $config_factory->get('advagg_mod.settings');
}
/**
* {@inheritdoc}
*/
public function optimize($contents, array $asset, array $data) {
// Code taken from \Drupal\Core\Asset\CssOptimizer::processCss().
// Regexp to match double quoted strings.
$double_quot = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"';
// Regexp to match single quoted strings.
$single_quot = "'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'";
// Extract all content inside of quotes.
$pattern = "/content:.*?({$double_quot}|{$single_quot}|(?:\\;|\\})).*?(?:\\;|\\})/";
// Run strings inside of quotes of the content attribute through the t
// function.
return preg_replace_callback($pattern, [
$this,
'translateCallback',
], $contents);
}
/**
* Run preg matches through the t() function.
*
* @param array $matches
* Array of matches from preg_replace_callback().
*
* @return string
* Replaced string.
*/
protected function translateCallback(array $matches) {
// Skip if equal to ; or }.
if ($matches[1] === ';' || $matches[1] === '}') {
return $matches[0];
}
// Remove quotes for t function.
$before = substr($matches[1], 1, -1);
// Only run if it contains A-Za-z.
if (!preg_match('/[A-Za-z]/', $before)) {
return $matches[0];
}
// Only run if it contains characters other than unicode.
$css_unicode_pattern = '/\\\\[0-9a-fA-F]{1,6}(?:\\r\\n|[ \\t\\r\\n\\f])?/';
$unicode_removed = preg_replace($css_unicode_pattern, '', $before);
if (empty($unicode_removed)) {
return $matches[0];
}
// Run t function and put back into string.
return str_replace($before, (string) $this->t('@before', [
'@before' => $before,
]), $matches[0]);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
SingleAssetOptimizerBase::$config | protected | property | A config object for optimizer. | |
SingleAssetOptimizerBase::$logger | protected | property | Logger service. | |
SingleAssetOptimizerBase::addLicense | public | function | If configured, add licence string to top/bottom of file. | |
SingleAssetOptimizerBase::isMinificationSuccess | protected | function | Check if minification was successful before saving changes. | |
SingleAssetOptimizerBase::isMinified | protected | function | Check if the asset is already minified. | |
SingleAssetOptimizerBase::stringContainsMultibyteCharacters | protected | function | Checks if string contains multibyte characters. | |
TranslateCss::optimize | public | function | Optimize the asset's content. | Overrides SingleAssetOptimizerBase::optimize |
TranslateCss::translateCallback | protected | function | Run preg matches through the t() function. | |
TranslateCss::__construct | public | function | Construct the optimizer instance. | Overrides SingleAssetOptimizerBase::__construct |