From 8b1da6a98a43981791eed3a1ff37cbf7baa959c3 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Tue, 6 Jan 2026 21:36:10 +0900 Subject: [PATCH] Fix incorrect handling of nested context switches (CSS inside HTML inside JS) in template v2 #2646 --- .../parsers/template/TemplateParser_v2.php | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/common/framework/parsers/template/TemplateParser_v2.php b/common/framework/parsers/template/TemplateParser_v2.php index f85f30ccc..d71af7947 100644 --- a/common/framework/parsers/template/TemplateParser_v2.php +++ b/common/framework/parsers/template/TemplateParser_v2.php @@ -179,14 +179,18 @@ class TemplateParser_v2 */ protected function _addContextSwitches(string $content): string { + $context_index = random_int(12000, 99000); + // Inline styles. $content = preg_replace_callback('#(?<=\s)(style=")([^"]*?)"#i', function($match) { return $match[1] . 'config->context = \'CSS\'; ?>' . $match[2] . 'config->context = \'HTML\'; ?>"'; }, $content); // Inline scripts. - $content = preg_replace_callback('#(?<=\s)(href="javascript:|pattern="|on[a-z]+=")([^"]*?)"#i', function($match) { - return $match[1] . 'config->context = \'JS\'; ?>' . $match[2] . 'config->context = \'HTML\'; ?>"'; + $content = preg_replace_callback('#(?<=\s)(href="javascript:|pattern="|on[a-z]+=")([^"]*?)"#i', function($match) use(&$context_index) { + $context_index++; + return $match[1] . 'config->context = \'JS\'; /* !CTX' . $context_index . '! */?>' . + $match[2] . 'config->context = \'HTML\'; /* !CTX' . $context_index . '! */?>"'; }, $content); //