-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathCssResolver.php
112 lines (105 loc) · 3.47 KB
/
CssResolver.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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\View\Url;
use Magento\Framework\View\FileSystem;
/**
* CSS URLs resolver class.
* This utility class provides a set of methods to work with CSS files.
* @api
* @since 100.0.2
*/
class CssResolver
{
/**
* PCRE that matches non-absolute URLs in CSS content
*/
const REGEX_CSS_RELATIVE_URLS =
'#url\s*\(\s*(?(?=\'|").)(?!http\://|https\://|/|data\:)(.+?)(?:[\#\?].*?|[\'"])?\s*\)#';
/**
* Adjust relative URLs in CSS content as if the file with this content is to be moved to new location
*
* @param string $cssContent
* @param string $relatedPath
* @param string $filePath
* @return mixed
*/
public function relocateRelativeUrls($cssContent, $relatedPath, $filePath)
{
$offset = FileSystem::offsetPath($relatedPath, $filePath);
$callback = function ($path) use ($offset) {
return FileSystem::normalizePath($offset . '/' . $path);
};
return $this->replaceRelativeUrls($cssContent, $callback);
}
/**
* A generic method for applying certain callback to all found relative URLs in CSS content
*
* Traverse through all relative URLs and apply a callback to each path
* The $inlineCallback is a user function that obtains the URL value and must return a replacement
*
* @param string $cssContent
* @param callback $inlineCallback
* @return string
*/
public function replaceRelativeUrls($cssContent, $inlineCallback)
{
$patterns = self::extractRelativeUrls($cssContent);
if ($patterns) {
$replace = [];
foreach ($patterns as $pattern => $path) {
if (!isset($replace[$pattern])) {
$newPath = call_user_func($inlineCallback, $path);
$newPattern = str_replace($path, $newPath, $pattern);
$replace[$pattern] = $newPattern;
}
}
if ($replace) {
$cssContent = str_replace(array_keys($replace), array_values($replace), $cssContent);
}
}
return $cssContent;
}
/**
* Extract all "import" directives from CSS-content and put them to the top of document
*
* @param string $cssContent
* @return string
*/
public function aggregateImportDirectives($cssContent)
{
$parts = preg_split('/(@import\s.+?;\s*)/', $cssContent, -1, PREG_SPLIT_DELIM_CAPTURE);
$imports = [];
$css = [];
foreach ($parts as $part) {
if (0 === strpos($part, '@import', 0)) {
$imports[] = trim($part);
} else {
$css[] = $part;
}
}
$result = implode($css);
if ($imports) {
$result = implode("\n", $imports)
. "\n/* The above import directives are aggregated from content. */\n"
. $result;
}
return $result;
}
/**
* Subroutine for obtaining url() fragments from the CSS content
*
* @param string $cssContent
* @return array
*/
private static function extractRelativeUrls($cssContent)
{
preg_match_all(self::REGEX_CSS_RELATIVE_URLS, $cssContent, $matches);
if (!empty($matches[0]) && !empty($matches[1])) {
return array_combine($matches[0], $matches[1]);
}
return [];
}
}