From 2430a79487703f65212b9e8170cfeafb0f84c7a0 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Fri, 1 Oct 2021 11:14:18 +0200 Subject: [PATCH] Optimize replacements --- src/Dom/Document/Filter/ProtectEsiTags.php | 34 +++++++++++++++------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/Dom/Document/Filter/ProtectEsiTags.php b/src/Dom/Document/Filter/ProtectEsiTags.php index 6071429cf..b6f428285 100644 --- a/src/Dom/Document/Filter/ProtectEsiTags.php +++ b/src/Dom/Document/Filter/ProtectEsiTags.php @@ -14,7 +14,7 @@ final class ProtectEsiTags implements BeforeLoadFilter, AfterSaveFilter { /** - * List of self closing ESI tags. + * List of self-closing ESI tags. * * @link https://www.w3.org/TR/esi-lang/ * @@ -33,13 +33,19 @@ final class ProtectEsiTags implements BeforeLoadFilter, AfterSaveFilter */ public function beforeLoad($html) { - $selfClosingregex = '#<(' . implode('|', self::SELF_CLOSING_TAGS) . ')([^>]*?)(?>\s*(?(?!)#'; + $patterns = [ + '#<(' . implode('|', self::SELF_CLOSING_TAGS) . ')([^>]*?)(?>\s*(?(?!)#', + '/(', $html); - $html = preg_replace('/(', + '$1esi-src=', + '$1esi-', + ]; - return $html; + return preg_replace($patterns, $replacements, $html); } /** @@ -50,12 +56,18 @@ public function beforeLoad($html) */ public function afterSave($html) { - $selfClosingregex = '#>#i'; + $patterns = [ + '/(<\/?)esi-/', + '/(#i', + ]; - $html = preg_replace('/(<\/?)esi-/', '$1esi:', $html); - $html = preg_replace('/(', $html); + $replacements = [ + '$1esi:', + '$1src=', + '/>', + ]; - return $html; + return preg_replace($patterns, $replacements, $html); } }