Completed
Push — master ( d2df97...a42271 )
by Josh
12:51
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 function getNodes()
19
	{
20
		return TemplateHelper::getCSSNodes($this->ownerDocument);
21
	}
22
23
	/**
24
	* {@inheritdoc}
25
	*/
26
	protected function normalizeAttribute(DOMAttr $attribute)
27
	{
28
		$css = $attribute->nodeValue;
29
30
		// Only minify if the value does not contain any XPath expression that's not an attribute
31
		if (!preg_match('(\\{(?!@\\w+\\}))', $css))
32
		{
33
			$attribute->nodeValue = $this->minify($css);
34
		}
35
	}
36
37
	/**
38
	* Minify a CSS string
39
	*
40
	* @param  string $css Original CSS
41
	* @return string      Minified CSS
42
	*/
43
	protected function minify($css)
44
	{
45
		$css = trim($css, " \n\t;");
46
		$css = preg_replace('(([:;])\\s+)', '$1', $css);
47
		$css = preg_replace('(#([0-9a-f])\\1([0-9a-f])\\2([0-9a-f])\\3)i', '#$1$2$3', $css);
48
		$css = str_replace(':0px', ':0', $css);
49
50
		return $css;
51
	}
52
}