1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Inliner for CSS |
4
|
|
|
* |
5
|
|
|
* @author Tim Lochmüller |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace FRUIT\Ink\Postprocessing; |
9
|
|
|
|
10
|
|
|
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles; |
11
|
|
|
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility; |
12
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
13
|
|
|
use TYPO3\CMS\Core\Utility\HttpUtility; |
14
|
|
|
use TYPO3\CMS\Extbase\Utility\DebuggerUtility; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Inliner for CSS |
18
|
|
|
*/ |
19
|
|
|
class InlineCss implements PostprocessingInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Call the inliner CSS processor |
24
|
|
|
* |
25
|
|
|
* @param string $content |
26
|
|
|
* |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
public function process($content) |
30
|
|
|
{ |
31
|
|
|
GeneralUtility::requireOnce(ExtensionManagementUtility::extPath('ink', 'Resources/Private/Php/vendor/autoload.php')); |
32
|
|
|
$pattern = '%<(link|style)(?=[^<>]*?(?:type="(text/css)"|>))(?=[^<>]*?(?:media="([^<>"]*)"|>))(?=[^<>]*?(?:href="(.*?)"|>))(?=[^<>]*(?:rel="([^<>"]*)"|>))(?:.*?</\1>|[^<>]*>)%si'; |
33
|
|
|
$matches = array(); |
34
|
|
|
$css = ''; |
35
|
|
|
preg_match_all($pattern, $content, $matches); |
36
|
|
|
if (isset($matches[0])) { |
37
|
|
|
foreach ($matches[0] as $key => $match) { |
38
|
|
|
if ($matches[1][$key] === 'style') { |
39
|
|
|
$css .= strip_tags($match); |
40
|
|
|
} elseif (strpos($match, 'type="text/css"') !== false) { |
41
|
|
|
$file = preg_replace('/^(.+)\.(\d+)\.css$/', '$1.css', $matches[4][$key]); |
42
|
|
|
$parts = parse_url($file); |
43
|
|
|
if (isset($parts['query'])) { |
44
|
|
|
unset($parts['query']); |
45
|
|
|
} |
46
|
|
|
if (!isset($parts['host'])) { |
47
|
|
|
$parts['path'] = ltrim($parts['path'], '/'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($parts['host'] === GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY')) { |
51
|
|
|
unset($parts['scheme']); |
52
|
|
|
unset($parts['host']); |
53
|
|
|
$parts['path'] = ltrim($parts['path'], '/'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$file = HttpUtility::buildUrl($parts); |
|
|
|
|
57
|
|
|
$css .= GeneralUtility::getUrl($file); |
58
|
|
|
} else { |
59
|
|
|
continue; |
60
|
|
|
} |
61
|
|
|
$content = str_replace($match, '', $content); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
$format = new CssToInlineStyles($content, $css); |
65
|
|
|
return $format->convert(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.