5 * Advanced CSS/JS aggregation modifier module.
9 * Implements hook_advagg_css_contents_alter().
11 * Used to run strings inside of quotes of the content attribute through the t
14 * @see \Drupal\Core\Asset\CssOptimizer::processCss
16 function advagg_mod_advagg_css_contents_alter(&$data, $css_asset) {
17 $config = \Drupal::config('advagg_mod.settings');
18 if (!$config->get('css_translate')) {
22 // Code taken from \Drupal\Core\Asset\CssOptimizer::processCss().
23 // Regexp to match double quoted strings.
24 $double_quot = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"';
25 // Regexp to match single quoted strings.
26 $single_quot = "'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'";
27 // Extract all content inside of quotes.
28 $css_content_pattern = "/content:.*?($double_quot|$single_quot|(?:\\;|\\})).*?(?:\\;|\\})/";
30 // Run strings inside of quotes of the content attribute through the t
32 $data = preg_replace_callback($css_content_pattern, 'advagg_mod_advagg_css_content_t_replace_callback', $data);
36 * Run preg matches through the t() function.
38 * @param array $matches
39 * Array of matches from preg_replace_callback().
44 function advagg_mod_advagg_css_content_t_replace_callback(array $matches) {
45 // Skip if equal to ; or }.
46 if ($matches[1] === ';' || $matches[1] === '}') {
49 // Remove quotes for t function.
50 $before = substr($matches[1], 1, -1);
51 // Only run if it contains A-Za-z.
52 if (!preg_match('/[A-Za-z]/', $before)) {
55 // Only run if it contains characters other than unicode.
56 $css_unicode_pattern = '/\\\\[0-9a-fA-F]{1,6}(?:\\r\\n|[ \\t\\r\\n\\f])?/';
57 $unicode_removed = preg_replace($css_unicode_pattern, '', $before);
58 if (empty($unicode_removed)) {
63 // @ignore sniffer_semantics_functioncall_notliteralstring
64 $after = (string) t($before);
66 return str_replace($before, $after, $matches[0]);