csscaffold /scaffold/libraries/Scaffold/CSS.php

Language PHP Lines 371
MD5 Hash 36d299a2391d58e4c2e8ebda8b1bd029
Repository https://github.com/rmdort/csscaffold.git View Raw File View Project SPDX
  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
<?php

/**
 * CSS Utilities
 *
 * Has methods for interacting with the CSS string
 * and makes it very easy to find properties and values within the css
 * 
 * @package CSScaffold
 * @author Anthony Short
 */
class Scaffold_CSS
{
	/**
	 * Server path to this CSS file
	 *
	 * @var string
	 */
	public $path;
	
	/**
	 * The name of this CSS file
	 *
	 * @var string
	 */
	public $file;
	
	/**
	 * The string of CSS code
	 *
	 * @var string
	 */
	public $string;
	
	/**
	 * Constructor
	 *
	 * @param $file
	 * @return void
	 */
	public function __construct($file)
	{
		$this->path = dirname($file);
		$this->file = $file;
		$this->string = $this->remove_inline_comments(file_get_contents($file));
	}
	
	/**
	 * Returns the CSS string when treated as a string
	 *
	 * @return string
	 */
	public function __toString()
	{
		return $this->string;
	}

	/**
	 * Compresses down the CSS file. Not a complete compression,
	 * but enough to minimize parsing time.
	 *
	 * @return string $css
	 */	
	public function compress($css)
	{		
		# Remove comments
		$this->string = $this->remove_comments($this->string);

		# Remove extra white space
		$this->string = preg_replace('/\s+/', ' ', $css);
		
		# Remove line breaks
		$this->string = preg_replace('/\n|\r/', '', $css);
	}
	
	/**
	 * Removes inline comments
	 *
	 * @return return type
	 */
	public function remove_inline_comments($css)
	{
		 return preg_replace('#(\s|^)//.*$#Umsi', '', $css);
	}

	/**
	 * Removes css comments
	 *
	 * @return string $css
	 */
	public function remove_comments($css)
	{
		$css = $this->convert_entities('encode', $css);
		$css = trim(preg_replace('#/\*[^*]*\*+([^/*][^*]*\*+)*/#', '', $css));
		$css = $this->convert_entities('decode', $css);
		$css = $this->remove_inline_comments($css);

		return $css;
	}

	/**
	 * Finds CSS 'functions'. These are things like url(), embed() etc.
	 *
	 * @author Anthony Short
	 * @param $name
	 * @param $capture_group
	 * @return array
	 */
	public function find_functions($name, $capture_group = "")
	{
		$regex =
		"/
			{$name}
			(
				\s*\(\s*
					( (?: (?1) | [^()]+ )* )
				\s*\)\s*
			)
		/sx";

		if(preg_match_all($regex, $this->string, $match))
		{
			return ($capture_group == "") ? $match : $match[$capture_group];
		}
		else
		{
			return array();
		}
	}

	/**
	 * Finds @groups within the css and returns
	 * an array with the values, and groups.
	 *
	 * @author Anthony Short
	 * @param $group string
	 * @param $css string
	 */
	public function find_at_group($group, $remove = true)
	{
		$found = array();
		
		$regex = 
		"/
			# Group name
			@{$group}
			
			# Flag
			(?:
				\(( [^)]*? )\)
			)?
			
			[^{]*?

			(
				([0-9a-zA-Z\_\-\@*&]*?)\s*
				\{	
					( (?: [^{}]+ | (?2) )*)
				\}
			)

		/ixs";
			
		if(preg_match_all($regex, $this->string, $matches))
		{
			$found['groups'] = $matches[0];
			$found['flag'] = $matches[1];
			$found['content'] = $matches[4];
						
			foreach($matches[4] as $key => $value)
			{
				// Remove comments to prevent breaking it
				$value = $this->remove_comments($value);

				foreach(explode(";", substr($value, 0, -1)) 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]))
					{
						$found['values'][trim($value[0])] = str_replace('#COLON#', ':', Scaffold::unquote($value[1]));
					}
				}
			}
			
			// Remove the found @ groups
			if($remove === true)
			{
				$this->string = str_replace($found['groups'], array(), $this->string);	
			}

			return $found;		
		}
		
		return false;
	}
	
	/**
	 * Finds selectors which contain a particular property
	 *
	 * @author Anthony Short
	 * @param $css
	 * @param $property string
	 * @param $value string
	 */
	public function find_selectors_with_property($property, $value = ".*?")
	{		
		if(preg_match_all("/([^{}]*)\s*\{\s*[^}]*(".$property."\s*\:\s*(".$value.")\s*\;).*?\s*\}/sx", $this->string, $match))
		{
			return $match;
		}
		else
		{
			return array();
		}
	}
	
	/**
	 * Finds all properties with a particular value
	 *
	 * @author Anthony Short
	 * @param $property
	 * @param $value
	 * @param $css
	 * @return array
	 */
	public function find_properties_with_value($property, $value = ".*?")
	{		
		# Make the property name regex-friendly
		$property = Scaffold_Utils::preg_quote($property);
		$regex = "/ ({$property}) \s*\:\s* ({$value}) /sx";
			
		if(preg_match_all($regex, $this->string, $match))
		{
			return $match;
		}
		else
		{
			return array();
		}
	}
		
	/**
	 * Finds a selector and returns it as string
	 *
	 * @author Anthony Short
	 * @param $selector string
	 * @param $css string
	 */
	public function find_selectors($selector, $recursive = "")
	{		
		if($recursive != "")
		{
			$recursive = "|(?{$recursive})";
		}

		$regex = 
			"/
				
				# This is the selector we're looking for
				({$selector})
				
				# Return all inner selectors and properties
				(
					([0-9a-zA-Z\_\-\*&]*?)\s*
					\{	
						(?P<properties>(?:[^{}]+{$recursive})*)
					\}
				)
				
			/xs";
		
		if(preg_match_all($regex, $this->string, $match))
		{
			return $match;
		}
		else
		{
			return array();
		}
	}
	
	/**
	 * Finds all properties within a css string
	 *
	 * @author Anthony Short
	 * @param $property string
	 * @param $css string
	 */
	public function find_property($property)
	{ 		
		if(preg_match_all('/('.Scaffold_Utils::preg_quote($property).')\s*\:\s*(.*?)\s*\;/sx', $this->string, $matches))
		{
			return (array)$matches;
		}
		else
		{
			return array();
		}
	}
	
	/**
	 * Check if a selector exists
	 *
	 * @param $name
	 * @return boolean
	 */
	public function selector_exists($name)
	{
		return preg_match('/'.preg_quote($name).'\s*?({|,)/', $this->string);
	}
		
	/**
	 * Removes all instances of a particular property from the css string
	 *
	 * @author Anthony Short
	 * @param $property string
	 * @param $value string
	 * @param $css string
	 */
	public function remove_properties($property, $value)
	{
		return preg_replace('/'.$property.'\s*\:\s*'.$value.'\s*\;/', '', $this->string);
	}
	
	/**
	 * Encodes or decodes parts of the css that break the xml
	 *
	 * @author Anthony Short
	 * @param $css
	 * @return string
	 */
	public function convert_entities($action = 'encode', $css = false)
	{
		if($css === false)
			$css =& $this->string;
		
		$css_replacements = array(
			'"' => '#SCAFFOLD-QUOTE#',
			'>' => '#SCAFFOLD-GREATER#',
			'&' => '#SCAFFOLD-PARENT#',
			'?' => '#SCAFFOLD-NESTED-PARENT#',
			'data:image/PNG;' => '#SCAFFOLD-IMGDATA-PNG#',
			'data:image/JPG;' => "#SCAFFOLD-IMGDATA-JPG#",
			'data:image/png;' => '#SCAFFOLD-IMGDATA-PNG#',
			'data:image/jpg;' => "#SCAFFOLD-IMGDATA-JPG#",
			'http://' => "#SCAFFOLD-HTTP#",
		);
		
		switch ($action)
		{
		    case 'decode':
		        $this->string = str_replace(array_values($css_replacements),array_keys($css_replacements), $this->string);
		        break;
		    
		    case 'encode':
		        $this->string = str_replace(array_keys($css_replacements),array_values($css_replacements), $this->string);
		        break;  
		}
		
		return $css;
	}

}
Back to Top