adlerka.top/lib/inliner.php

104 lines
3.7 KiB
PHP
Raw Normal View History

2024-02-04 10:21:56 +01:00
<?php
/**
* Processes an HTML string to inline all linked stylesheets by replacing <link> tags
* with corresponding <style> tags containing the CSS content.
* Might be broken, currently disabled in the config
*
* @param string $inputString The HTML content containing <link> tags to stylesheets.
* @return string The modified HTML content with stylesheets inlined within <style> tags.
*/
function inlineLocalStylesFromHref(string $inputString): string
2024-02-06 16:24:57 +01:00
{
2024-02-04 10:37:06 +01:00
$pattern = '/<link[^>]*?\srel=["\']?stylesheet["\'].*?\shref=["\']?\/(.*?)["\'][^>]*?>/i';
2024-02-04 10:21:56 +01:00
2024-02-06 16:24:57 +01:00
return preg_replace_callback($pattern, function($match) {
2024-02-04 10:37:06 +01:00
$href = $match[1];
2024-02-04 10:42:36 +01:00
$cssFilePath = $_SERVER['DOCUMENT_ROOT'] . '/' . $href;
$cssContent = file_get_contents($cssFilePath);
$cssDir = dirname($cssFilePath);
2024-02-04 10:21:56 +01:00
2024-02-04 11:11:03 +01:00
$cssContent = preg_replace_callback('/url\(["\']?(\/.*?|.*?)["\']?\)/i', function($urlMatch) use ($cssDir) {
$url = $urlMatch[1];
2024-02-04 11:15:54 +01:00
// Check if the URL starts with any protocol or //
if (!preg_match('/^([a-zA-Z]+:)?\/\//', $url)) {
$absolutePath = $cssDir . '/' . $url;
$relativePath = ltrim(substr($absolutePath, strlen($_SERVER['DOCUMENT_ROOT'])), '/');
return 'url("' . $relativePath . '")';
} else {
// If the URL starts with a protocol or //, leave it unchanged
return 'url("' . $url . '")';
}
2024-02-04 11:11:03 +01:00
}, $cssContent);
2024-02-04 10:32:08 +01:00
2024-02-04 10:45:44 +01:00
// Minify the CSS content
2024-02-04 11:11:48 +01:00
$cssContent = minifyCss($cssContent);
2024-02-04 10:45:44 +01:00
2024-02-22 18:16:42 +01:00
return "<style>$cssContent</style>";
2024-02-04 10:21:56 +01:00
}, $inputString);
}
/**
* Processes an HTML string to inline all external JavaScript files by replacing <script src="..."> tags
* with <script> tags containing the JavaScript content.
* Might be broken, currently disabled in the config
*
* @param string $inputString The HTML content containing <script src=""> tags.
* @return string The modified HTML content with external scripts inlined within <script> tags.
*/
function inlineScriptFromSrc(string $inputString): string
2024-02-06 16:24:57 +01:00
{
2024-02-04 10:24:52 +01:00
$pattern = '/<script.*?src=["\']\/(.*?)["\'].*?>\s*<\/script>/i';
2024-02-04 10:21:56 +01:00
2024-02-06 16:24:57 +01:00
return preg_replace_callback($pattern, function($match) {
2024-02-04 10:21:56 +01:00
$src = $match[1];
2024-02-04 10:24:52 +01:00
$jsContent = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/' . $src);
2024-02-04 10:21:56 +01:00
2024-02-04 10:45:44 +01:00
// Minify the JavaScript content
$jsContent = minifyJs($jsContent);
2024-02-22 18:16:42 +01:00
return "<script>$jsContent</script>";
2024-02-04 10:21:56 +01:00
}, $inputString);
2024-02-04 10:45:44 +01:00
}
/**
* Minifies CSS content by removing comments, unnecessary whitespaces, semicolons, and optimizing other aspects of the stylesheet.
* Might be broken, currently disabled in the config
*
* @param string $css The original CSS content.
* @return string The minified CSS content.
*/
function minifyCss(string $css): string
2024-02-06 16:24:57 +01:00
{
2024-02-04 10:45:44 +01:00
// Remove comments
2024-02-04 11:12:42 +01:00
$css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css);
2024-02-04 10:45:44 +01:00
// Remove whitespace
2024-02-04 11:12:54 +01:00
$css = preg_replace('/\s+/', ' ', $css);
2024-02-04 10:45:44 +01:00
2024-02-04 11:13:22 +01:00
// Remove unnecessary semicolons
$css = str_replace(';}', '}', $css);
2024-02-04 10:45:44 +01:00
// Remove spaces around colons
2024-02-04 11:13:22 +01:00
$css = str_replace(': ', ':', $css);
// Remove spaces around commas
$css = str_replace(', ', ',', $css);
2024-02-04 10:45:44 +01:00
return trim($css);
}
/**
* Minifies JavaScript content by removing comments, unnecessary whitespaces, and optimizing spaces around operators.
* Might be broken, currently disabled in the config
*
* @param string $js The original JavaScript content.
* @return string The minified JavaScript content.
*/
function minifyJs(string $js): string
2024-02-06 16:24:57 +01:00
{
2024-02-04 10:55:12 +01:00
2024-02-04 10:45:44 +01:00
// Remove newlines and tabs
2024-02-04 11:05:14 +01:00
$js = str_replace("\t", '', $js);
2024-02-04 10:45:44 +01:00
// Remove spaces around operators
2024-02-04 11:07:37 +01:00
$js = preg_replace('~\s*([=+\-*/])\s*~', '$1', $js);
2024-02-04 10:45:44 +01:00
return trim($js);
2024-02-04 10:21:56 +01:00
}