1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | * This file is part of the Assetic package, an OpenSky project. |
---|
5 | * |
---|
6 | * (c) 2010-2012 OpenSky Project Inc |
---|
7 | * |
---|
8 | * For the full copyright and license information, please view the LICENSE |
---|
9 | * file that was distributed with this source code. |
---|
10 | */ |
---|
11 | |
---|
12 | namespace Assetic\Filter; |
---|
13 | |
---|
14 | /** |
---|
15 | * An abstract filter for dealing with CSS. |
---|
16 | * |
---|
17 | * @author Kris Wallsmith <kris.wallsmith@gmail.com> |
---|
18 | */ |
---|
19 | abstract class BaseCssFilter implements FilterInterface |
---|
20 | { |
---|
21 | /** |
---|
22 | * Filters all references -- url() and "@import" -- through a callable. |
---|
23 | * |
---|
24 | * @param string $content The CSS |
---|
25 | * @param callable $callback A PHP callable |
---|
26 | * @param integer $limit |
---|
27 | * @param integer $count |
---|
28 | * |
---|
29 | * @return string The filtered CSS |
---|
30 | */ |
---|
31 | protected function filterReferences($content, $callback, $limit = -1, &$count = 0) |
---|
32 | { |
---|
33 | $content = $this->filterUrls($content, $callback, $limit, $count); |
---|
34 | $content = $this->filterImports($content, $callback, $limit, $count, false); |
---|
35 | $content = $this->filterIEFilters($content, $callback, $limit, $count); |
---|
36 | |
---|
37 | return $content; |
---|
38 | } |
---|
39 | |
---|
40 | /** |
---|
41 | * Filters all CSS url()'s through a callable. |
---|
42 | * |
---|
43 | * @param string $content The CSS |
---|
44 | * @param callable $callback A PHP callable |
---|
45 | * @param integer $limit Limit the number of replacements |
---|
46 | * @param integer $count Will be populated with the count |
---|
47 | * |
---|
48 | * @return string The filtered CSS |
---|
49 | */ |
---|
50 | protected function filterUrls($content, $callback, $limit = -1, &$count = 0) |
---|
51 | { |
---|
52 | return preg_replace_callback('/url\((["\']?)(?P<url>.*?)(\\1)\)/', $callback, $content, $limit, $count); |
---|
53 | } |
---|
54 | |
---|
55 | /** |
---|
56 | * Filters all CSS imports through a callable. |
---|
57 | * |
---|
58 | * @param string $content The CSS |
---|
59 | * @param callable $callback A PHP callable |
---|
60 | * @param integer $limit Limit the number of replacements |
---|
61 | * @param integer $count Will be populated with the count |
---|
62 | * @param Boolean $includeUrl Whether to include url() in the pattern |
---|
63 | * |
---|
64 | * @return string The filtered CSS |
---|
65 | */ |
---|
66 | protected function filterImports($content, $callback, $limit = -1, &$count = 0, $includeUrl = true) |
---|
67 | { |
---|
68 | $pattern = $includeUrl |
---|
69 | ? '/@import (?:url\()?(\'|"|)(?P<url>[^\'"\)\n\r]*)\1\)?;?/' |
---|
70 | : '/@import (?!url\()(\'|"|)(?P<url>[^\'"\)\n\r]*)\1;?/'; |
---|
71 | |
---|
72 | return preg_replace_callback($pattern, $callback, $content, $limit, $count); |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * Filters all IE filters (AlphaImageLoader filter) through a callable. |
---|
77 | * |
---|
78 | * @param string $content The CSS |
---|
79 | * @param callable $callback A PHP callable |
---|
80 | * @param integer $limit Limit the number of replacements |
---|
81 | * @param integer $count Will be populated with the count |
---|
82 | * |
---|
83 | * @return string The filtered CSS |
---|
84 | */ |
---|
85 | protected function filterIEFilters($content, $callback, $limit = -1, &$count = 0) |
---|
86 | { |
---|
87 | return preg_replace_callback('/src=(["\']?)(?P<url>.*?)\\1/', $callback, $content, $limit, $count); |
---|
88 | } |
---|
89 | } |
---|