This commit is contained in:
2024-02-06 16:24:57 +01:00
parent 38895b1502
commit 72bd8b8bd1
15 changed files with 278 additions and 361 deletions

View File

@@ -1,9 +1,10 @@
<?php
function inlineLocalStylesFromHref($inputString) {
function inlineLocalStylesFromHref($inputString): string
{
$pattern = '/<link[^>]*?\srel=["\']?stylesheet["\'].*?\shref=["\']?\/(.*?)["\'][^>]*?>/i';
$outputString = preg_replace_callback($pattern, function($match) {
return preg_replace_callback($pattern, function($match) {
$href = $match[1];
$cssFilePath = $_SERVER['DOCUMENT_ROOT'] . '/' . $href;
$cssContent = file_get_contents($cssFilePath);
@@ -28,14 +29,13 @@ function inlineLocalStylesFromHref($inputString) {
return "<style>{$cssContent}</style>";
}, $inputString);
return $outputString;
}
function inlineScriptFromSrc($inputString) {
function inlineScriptFromSrc($inputString): string
{
$pattern = '/<script.*?src=["\']\/(.*?)["\'].*?>\s*<\/script>/i';
$outputString = preg_replace_callback($pattern, function($match) {
return preg_replace_callback($pattern, function($match) {
$src = $match[1];
$jsContent = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/' . $src);
@@ -43,11 +43,10 @@ function inlineScriptFromSrc($inputString) {
$jsContent = minifyJs($jsContent);
return "<script>{$jsContent}</script>";
}, $inputString);
return $outputString;
}
function minifyCss($css) {
function minifyCss($css): string
{
// Remove comments
$css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css);
@@ -66,7 +65,8 @@ function minifyCss($css) {
return trim($css);
}
function minifyJs($js) {
function minifyJs($js): string
{
// Remove newlines and tabs
$js = str_replace("\t", '', $js);