Completed
Push — master ( 285fef...8cebaa )
by Josh
14:20
created

MinifyInlineCSS::getNodes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2017 The s9e Authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Configurator\TemplateNormalizations;
9
10
use DOMAttr;
11
use s9e\TextFormatter\Configurator\Helpers\TemplateHelper;
12
13
class MinifyInlineCSS extends AbstractNormalization
14
{
15
	/**
16
	* {@inheritdoc}
17
	*/
18
	protected $queries = ['//*[namespace-uri() != $XSL]/@style'];
19
20
	/**
21
	* {@inheritdoc}
22
	*/
23
	protected function normalizeAttribute(DOMAttr $attribute)
24
	{
25
		$css = $attribute->nodeValue;
26
27
		// Only minify if the value does not contain any XPath expression that's not an attribute
28
		if (!preg_match('(\\{(?!@\\w+\\}))', $css))
29
		{
30
			$attribute->nodeValue = $this->minify($css);
31
		}
32
	}
33
34
	/**
35
	* Minify a CSS string
36
	*
37
	* @param  string $css Original CSS
38
	* @return string      Minified CSS
39
	*/
40
	protected function minify($css)
41
	{
42
		$css = trim($css, " \n\t;");
43
		$css = preg_replace('(([:;])\\s+)', '$1', $css);
44
		$css = preg_replace('(#([0-9a-f])\\1([0-9a-f])\\2([0-9a-f])\\3)i', '#$1$2$3', $css);
45
		$css = str_replace(':0px', ':0', $css);
46
47
		return $css;
48
	}
49
}