Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified assets/img/logo-sucuri.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 14 additions & 6 deletions inc/Engine/Media/PreloadFonts/Frontend/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,25 @@ public function maybe_remove_existing_preloaded_fonts( string $html ): string {
return $html;
}

// One regex to skip scripts and remove any <link rel=preload as=font…> tag (entire line, including indentation and newline):.
$html = preg_replace(
// One regex to skip scripts and remove any <link rel=preload as=font…> tag (entire line, including indentation and newline).
$result = preg_replace(
'#<script\b[^>]*>[\s\S]*?<\/script\b[^>]*>(*SKIP)(*FAIL)' // skip <script> blocks.
. '|^[ \t]*<link\b' // OR match a <link at line start (with optional indent).
. '(?=[^>]*\brel\s*=\s*(["\']?)preload\1)' // lookahead rel=preload.
. '(?=[^>]*\bas\s*=\s*(["\']?)font\2)' // lookahead as=font.
. '[^>]*?\/?>[ \t]*(?:\r?\n|$)#im', // up to /> or >, then trim whitespace and newline.
. '|^[ \t]*<link\b' // OR match a <link at line start (with optional indent).
. '(?=[^>]*\brel\s*=\s*(["\']?)preload\1)' // lookahead rel=preload.
. '(?=[^>]*\bas\s*=\s*(["\']?)font\2)' // lookahead as=font.
. '[^>]*?\/?>[ \t]*(?:\r?\n|$)#im', // up to /> or >, then trim whitespace and newline.
'',
$html
);

// If the regex fails (e.g. backtrack limit), preg_replace() returns null in PHP 8+.
// In that case, keep the original HTML to avoid a TypeError from the string return type.
if ( null === $result ) {
return $html;
}

$html = $result;

return $html;
}
}