forked from Adleraci/adlerka.top
test inlining
This commit is contained in:
parent
882c055346
commit
d94a66ca2b
@ -1,64 +1,81 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
function inlineLocalStylesFromHref($inputString) {
|
function inlineLocalStylesFromHref($inputString) {
|
||||||
// Define the regular expression pattern to match <link> tags with href attribute for CSS files
|
|
||||||
$pattern = '/<link[^>]*?\srel=["\']?stylesheet["\'].*?\shref=["\']?\/(.*?)["\'][^>]*?>/i';
|
$pattern = '/<link[^>]*?\srel=["\']?stylesheet["\'].*?\shref=["\']?\/(.*?)["\'][^>]*?>/i';
|
||||||
|
|
||||||
// Use preg_replace_callback to replace matched link tags with inline styles
|
|
||||||
$outputString = preg_replace_callback($pattern, function($match) {
|
$outputString = preg_replace_callback($pattern, function($match) {
|
||||||
// Extract the href attribute value
|
|
||||||
$href = $match[1];
|
$href = $match[1];
|
||||||
|
|
||||||
$cssFilePath = $_SERVER['DOCUMENT_ROOT'] . '/' . $href;
|
$cssFilePath = $_SERVER['DOCUMENT_ROOT'] . '/' . $href;
|
||||||
|
|
||||||
// Get the content of the local CSS file
|
|
||||||
$cssContent = file_get_contents($cssFilePath);
|
$cssContent = file_get_contents($cssFilePath);
|
||||||
|
|
||||||
// Get the directory where the CSS file is located
|
|
||||||
$cssDir = dirname($cssFilePath);
|
$cssDir = dirname($cssFilePath);
|
||||||
|
|
||||||
// Replace relative paths in the CSS content to be relative to the CSS file's directory
|
|
||||||
$cssContent = preg_replace_callback('/url\(["\']?(\/.*?|.*?)["\']?\)/i', function($urlMatch) use ($cssDir) {
|
$cssContent = preg_replace_callback('/url\(["\']?(\/.*?|.*?)["\']?\)/i', function($urlMatch) use ($cssDir) {
|
||||||
// Extract the URL value
|
|
||||||
$url = $urlMatch[1];
|
$url = $urlMatch[1];
|
||||||
|
|
||||||
// Combine with the CSS file's directory to create an absolute path
|
|
||||||
$absolutePath = $cssDir . '/' . $url;
|
$absolutePath = $cssDir . '/' . $url;
|
||||||
|
|
||||||
// Make the path relative to the CSS file's directory
|
|
||||||
$relativePath = ltrim(substr($absolutePath, strlen($_SERVER['DOCUMENT_ROOT'])), '/');
|
$relativePath = ltrim(substr($absolutePath, strlen($_SERVER['DOCUMENT_ROOT'])), '/');
|
||||||
|
return 'url("' . $relativePath . '")';
|
||||||
// Create the updated url() declaration
|
|
||||||
$updatedUrlDeclaration = 'url("' . $relativePath . '")';
|
|
||||||
|
|
||||||
return $updatedUrlDeclaration;
|
|
||||||
}, $cssContent);
|
}, $cssContent);
|
||||||
|
|
||||||
// Create an inline style tag with the modified CSS content
|
// Minify the CSS content
|
||||||
return "<style>\n{$cssContent}\n</style>";
|
$cssContent = minifyCss($cssContent);
|
||||||
|
|
||||||
|
return "<style>{$cssContent}</style>";
|
||||||
}, $inputString);
|
}, $inputString);
|
||||||
|
|
||||||
return $outputString;
|
return $outputString;
|
||||||
}
|
}
|
||||||
|
|
||||||
function inlineScriptFromSrc($inputString) {
|
function inlineScriptFromSrc($inputString) {
|
||||||
// Define the regular expression pattern to match <script> tags with src attribute
|
|
||||||
$pattern = '/<script.*?src=["\']\/(.*?)["\'].*?>\s*<\/script>/i';
|
$pattern = '/<script.*?src=["\']\/(.*?)["\'].*?>\s*<\/script>/i';
|
||||||
|
|
||||||
// Use preg_replace_callback to replace matched script tags with inline script
|
|
||||||
$outputString = preg_replace_callback($pattern, function($match) {
|
$outputString = preg_replace_callback($pattern, function($match) {
|
||||||
// Extract the src attribute value
|
|
||||||
$src = $match[1];
|
$src = $match[1];
|
||||||
|
|
||||||
// Get the content of the JavaScript file
|
|
||||||
$jsContent = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/' . $src);
|
$jsContent = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/' . $src);
|
||||||
|
|
||||||
// Escape the JavaScript content for inline use
|
// Minify the JavaScript content
|
||||||
$escapedJsContent = htmlspecialchars($jsContent, ENT_QUOTES, 'UTF-8');
|
$jsContent = minifyJs($jsContent);
|
||||||
|
|
||||||
// Create an inline script with the escaped content
|
$escapedJsContent = htmlspecialchars($jsContent, ENT_QUOTES, 'UTF-8');
|
||||||
return "<script>\n{$escapedJsContent}\n</script>";
|
return "<script>{$escapedJsContent}</script>";
|
||||||
}, $inputString);
|
}, $inputString);
|
||||||
|
|
||||||
return $outputString;
|
return $outputString;
|
||||||
|
}
|
||||||
|
|
||||||
|
function minifyCss($css) {
|
||||||
|
// Remove comments
|
||||||
|
$css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css);
|
||||||
|
|
||||||
|
// Remove whitespace
|
||||||
|
$css = preg_replace('/\s+/', ' ', $css);
|
||||||
|
|
||||||
|
// Remove unnecessary semicolons
|
||||||
|
$css = str_replace(';}', '}', $css);
|
||||||
|
|
||||||
|
// Remove spaces around colons
|
||||||
|
$css = str_replace(': ', ':', $css);
|
||||||
|
|
||||||
|
// Remove spaces around commas
|
||||||
|
$css = str_replace(', ', ',', $css);
|
||||||
|
|
||||||
|
return trim($css);
|
||||||
|
}
|
||||||
|
|
||||||
|
function minifyJs($js) {
|
||||||
|
// Remove comments
|
||||||
|
$js = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $js);
|
||||||
|
|
||||||
|
// Remove newlines and tabs
|
||||||
|
$js = str_replace(array("\r\n", "\r", "\n", "\t"), '', $js);
|
||||||
|
|
||||||
|
// Remove unnecessary semicolons
|
||||||
|
$js = str_replace(';}', '}', $js);
|
||||||
|
|
||||||
|
// Remove spaces after function keyword
|
||||||
|
$js = str_replace('function ', 'function', $js);
|
||||||
|
|
||||||
|
// Remove spaces around operators
|
||||||
|
$js = preg_replace('/\s*([=+\-*/])\s*/', '$1', $js);
|
||||||
|
|
||||||
|
return trim($js);
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user