-
Notifications
You must be signed in to change notification settings - Fork 19
/
CSS.php
executable file
·376 lines (323 loc) · 8.08 KB
/
CSS.php
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
/**
* Scaffold_CSS
*
* Helper methods for dealing with CSS
*
* @package Scaffold
* @author Anthony Short <anthonyshort@me.com>
* @copyright 2009-2010 Anthony Short. All rights reserved.
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @link https://github.com/anthonyshort/csscaffold/master
*/
class Scaffold_Helper_CSS
{
/**
* Regex for a valid selector
* @var string
*/
private static $_identifier = '[0-9a-zA-Z\_\-\*&\#\[\]~=|\"\'\^\$\:\>\+\(\)]';
/**
* Removes single-line comments from a string
* @access public
* @param string
* @return string
*/
public static function remove_inline_comments($string)
{
return preg_replace('#($|\s)//.*$#Umsi', '', $string);
}
/**
* Removes line breaks and tabs
* @access public
* @param $string
* @return string
*/
public static function remove_newlines($string)
{
return preg_replace('/\n+|\r+|\t+/', '', $string);
}
/**
* Removes css comments
* @access public
* @param $string
* @return string
*/
public static function remove_comments($string)
{
return preg_replace('#/\*[^*]*\*+([^/*][^*]*\*+)*/#', '', $string);
}
/**
* Encodes a selector so that it's able to be used in regular expressions
* @access public
* @param $selector
* @return string
*/
public static function escape_regex($selector)
{
$selector = preg_quote($selector,'-');
$selector = str_replace('#','\#',$selector);
$selector = preg_replace('/\s+/',"\s+",$selector);
return $selector;
}
// ============================
// = Function Methods =
// ============================
/**
* Finds CSS 'functions'. These are things like url(), embed() etc.
* Handles interior brackets as well by using recursion.
* @access public
* @param $name
* @param $string
* @param $type int Which array to return
* @return array
*/
public static function find_functions($name,$string)
{
$return = array();
$regex ="/{$name}(\s*\(\s*((?:(?1)|[^()]+)*)\s*\)\s*)/sx";
if(preg_match_all($regex, $string, $match))
{
foreach($match[0] as $key => $value)
{
$return[$key] = array(
'string' => $value,
'param' => $match[2][$key]
);
}
}
return $return;
}
// ============================
// = Ruleset Methods =
// ============================
/**
* Takes a string of a CSS rule:
*
* property:value;
* property:value;
* etc.
*
* And breaks it into an array:
*
* array('property'=>'value');
*
* @param $string
* @return array
*/
public static function ruleset_to_array($string)
{
$return = array();
foreach(explode(";", $string) as $value)
{
// Encode any colons inside quotations
if(preg_match_all('/[\'"](.*?\:.*?)[\'"]/',$value,$m) )
{
$value = str_replace($m[0][0],str_replace(':','#COLON#',$m[0][0]),$value);
}
$value = explode(":", $value);
// Make sure it's set
if(isset($value[1]))
{
$return[trim($value[0])] = str_replace('#COLON#', ':', trim($value[1]));
}
}
return $return;
}
// ============================
// = Selector Methods =
// ============================
/**
* Finds selectors which contain a particular property
* @access public
* @param $property string
* @param $css string
* @return array
*/
public static function find_selectors_with_property($property,$css)
{
$return = array();
$regex = "/([^{}]*)\s*\{\s*($property|[^}]*[\s\;]$property)\s*:[^}]*}/sx";
if(preg_match_all($regex,$css,$match))
{
foreach($match[0] as $key => $value)
{
$return[$key] = array(
'string' => $value,
'selector' => $match[1][$key]
);
}
}
return $return;
}
/**
* Finds a selector and returns it as string
* @access public
* @param $selector string
* @param $string string
* @todo This will break if the selector they try and find is actually part of another selector
*/
public static function find_selectors($selector,$string)
{
$selector = self::escape_regex($selector);
$regex = "/(^|[}\s;])(($selector)\s*\{[^}]*\})/sx";
return preg_match_all($regex, $string, $match) ? $match[2] : array();
}
/**
* Check if a selector exists
* @param $name
* @param $string
* @return boolean
*/
public static function selector_exists($name,$string)
{
return preg_match('/(^|})\s*'.$name.'\s*\{/', $string) ? true : false;
}
/**
* Checks if a selector is valid
* @param $string
* @return boolean
*/
public static function valid_selector($string)
{
return preg_match(self::$_identifier,$string);
}
// ============================
// = Property Methods =
// ============================
/**
* Finds all properties with a particular value
* @access public
* @param $property Regex formatted string for a property
* @param $value Regex formatted string for a value
* @param $css
* @return array
*/
public static function find_properties_with_value($property,$value,$css,$escape_value=true)
{
$return = array();
$escaped_property = self::escape_regex($property);
$value = $escape_value ? self::escape_regex($value) : $value;
$regex = "/
\s*
([^{}]*) (?# selector)
\s*
\{
(?: (?# property is complicated, as we may be first or intermediate - dbackground instance)
[^}]*[\s\;]
|
\s*
)
($escaped_property)
\s*
: (?# property value seperator)
\s*
($value)
;?
[^}]* (?# everything after our property of interest)
\} (?# end content)
/sx";
if(preg_match_all($regex,$css,$matches,PREG_SET_ORDER))
{
foreach ( $matches as $key => $match )
{
$return[$key] = array(
'string' => trim($match[0]),
'selector' => $match[1],
'property' => $match[2].':'.$match[3]
);
}
}
return $return;
}
/**
* Removes all instances of a particular property from the css string
* @access public
* @param $property string
* @param $value string
* @param $css string
*/
public static function remove_properties_with_value($property,$value,$string,$escape_value=true)
{
# Prepare
$escaped_property = self::escape_regex($property);
$value = $escape_value ? self::escape_regex($value) : $value;
# Generate regex
$regex = "/
(
\{
(?: (?# property is complicated, as we may be first or intermediate - dbackground instance)
\s*
|
[^}]*[\s\;]
)
)
($escaped_property)
\s*
: (?# property value seperator)
\s*
($value)
;?
/sx";
# Remove property
$string = preg_replace($regex, '$1', $string);
# Return string
return $string;
}
/**
* Finds all properties within a css string
* @access public
* @param $property string Regex formatted string
* @param $string string
*/
public static function find_properties($property,$string)
{
return self::find_properties_with_value($property,'[^;}]*',$string,false);
}
/**
* Removes all instances of a particular property from the css string
* @access public
* @param $property string
* @param $string string
*/
public static function remove_properties($property,$string)
{
return self::remove_properties_with_value($property,'[^;}]*',$string,false);
}
// ============================
// = @ Rule Methods =
// ============================
/**
* Finds @groups within the css and returns
* an array with the values, and groups.
* @access public
* @param $name
* @param $string
* @return array
*/
public static function find_atrule($name,$string)
{
$name = self::escape_regex($name);
$regex = "/
@{$name} (?# the name to find)
([^{]*?) (?# atrules params)
\{ (?# start atrule content)
(.*?)
\} (?# end atrule content)
/xs";
$result = preg_match_all($regex, $string, $matches, PREG_SET_ORDER) ? $matches : array();
return $result;
}
/**
* Removes an atrule from a CSS string
* @access public
* @param $name
* @param $css
* @return string
*/
public static function remove_atrule($name,$css)
{
$rules = self::find_atrule($name,$css);
return str_replace($rules[0],'',$css);
}
}