CssUtils.php
4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/*
* This file is part of the Assetic package, an OpenSky project.
*
* (c) 2010-2014 OpenSky Project Inc
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Assetic\Util;
/**
* CSS Utils.
*
* @author Kris Wallsmith <kris.wallsmith@gmail.com>
*/
abstract class CssUtils
{
const REGEX_URLS = '/url\((["\']?)(?P<url>.*?)(\\1)\)/';
const REGEX_IMPORTS = '/@import (?:url\()?(\'|"|)(?P<url>[^\'"\)\n\r]*)\1\)?;?/';
const REGEX_IMPORTS_NO_URLS = '/@import (?!url\()(\'|"|)(?P<url>[^\'"\)\n\r]*)\1;?/';
const REGEX_IE_FILTERS = '/src=(["\']?)(?P<url>.*?)\\1/';
const REGEX_COMMENTS = '/(\/\*[^*]*\*+(?:[^\/][^*]*\*+)*\/)/';
/**
* Filters all references -- url() and "@import" -- through a callable.
*
* @param string $content The CSS
* @param callable $callback A PHP callable
*
* @return string The filtered CSS
*/
public static function filterReferences($content, $callback)
{
$content = static::filterUrls($content, $callback);
$content = static::filterImports($content, $callback, false);
$content = static::filterIEFilters($content, $callback);
return $content;
}
/**
* Filters all CSS url()'s through a callable.
*
* @param string $content The CSS
* @param callable $callback A PHP callable
*
* @return string The filtered CSS
*/
public static function filterUrls($content, $callback)
{
$pattern = static::REGEX_URLS;
return static::filterCommentless($content, function($part) use(& $callback, $pattern) {
return preg_replace_callback($pattern, $callback, $part);
});
}
/**
* Filters all CSS imports through a callable.
*
* @param string $content The CSS
* @param callable $callback A PHP callable
* @param Boolean $includeUrl Whether to include url() in the pattern
*
* @return string The filtered CSS
*/
public static function filterImports($content, $callback, $includeUrl = true)
{
$pattern = $includeUrl ? static::REGEX_IMPORTS : static::REGEX_IMPORTS_NO_URLS;
return static::filterCommentless($content, function($part) use(& $callback, $pattern) {
return preg_replace_callback($pattern, $callback, $part);
});
}
/**
* Filters all IE filters (AlphaImageLoader filter) through a callable.
*
* @param string $content The CSS
* @param callable $callback A PHP callable
*
* @return string The filtered CSS
*/
public static function filterIEFilters($content, $callback)
{
$pattern = static::REGEX_IE_FILTERS;
return static::filterCommentless($content, function($part) use (& $callback, $pattern) {
return preg_replace_callback($pattern, $callback, $part);
});
}
/**
* Filters each non-comment part through a callable.
*
* @param string $content The CSS
* @param callable $callback A PHP callable
*
* @return string The filtered CSS
*/
public static function filterCommentless($content, $callback)
{
$result = '';
foreach (preg_split(static::REGEX_COMMENTS, $content, -1, PREG_SPLIT_DELIM_CAPTURE) as $part) {
if (!preg_match(static::REGEX_COMMENTS, $part, $match) || $part != $match[0]) {
$part = call_user_func($callback, $part);
}
$result .= $part;
}
return $result;
}
/**
* Extracts all references from the supplied CSS content.
*
* @param string $content The CSS content
*
* @return array An array of unique URLs
*/
public static function extractImports($content)
{
$imports = array();
static::filterImports($content, function($matches) use(& $imports) {
$imports[] = $matches['url'];
});
return array_unique($imports);
}
final private function __construct() { }
}