From db9e6050b2eaa2f2c675361c673d229e88fee1e1 Mon Sep 17 00:00:00 2001 From: Chanyoung Oh Date: Tue, 5 Jan 2021 15:00:30 +0000 Subject: [PATCH 1/4] Fix #1543 - Convert relative path in srcset attribute --- classes/template/TemplateHandler.class.php | 45 +++++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/classes/template/TemplateHandler.class.php b/classes/template/TemplateHandler.class.php index e4024beec..efd19d7d2 100644 --- a/classes/template/TemplateHandler.class.php +++ b/classes/template/TemplateHandler.class.php @@ -235,6 +235,9 @@ class TemplateHandler // replace value of src in img/input/script tag $buff = preg_replace_callback('/<(?:img|input|script)(?:[^<>]*?)(?(?=cond=")(?:cond="[^"]+"[^<>]*)+|)[^<>]* src="(?!(?:https?|file):\/\/|[\/\{])([^"]+)"/is', array($this, '_replacePath'), $buff); + // replace value of srcset in img/source/link tag + $buff = preg_replace_callback('/<(?:img|source|link)(?:[^<>]*?)(?(?=cond=")(?:cond="[^"]+"[^<>]*)+|)[^<>]* srcset="([^"]+)"/is', array($this, '_replaceSrcsetPath'), $buff); + // replace loop and cond template syntax $buff = $this->_parseInline($buff); @@ -390,7 +393,19 @@ class TemplateHandler */ private function _replacePath($match) { - //return origin conde when src value started '${'. + $src = $this->_replaceRelativePath($match); + return substr($match[0], 0, -strlen($match[1]) - 6) . "src=\"{$src}\""; + } + + /** + * replace relative path + * @param array $match + * + * @return string changed result + */ + private function _replaceRelativePath($match) + { + //return origin code when src value started '${'. if(preg_match('@^\${@', $match[1])) { return $match[0]; @@ -415,7 +430,33 @@ class TemplateHandler $src = $tmp; } - return substr($match[0], 0, -strlen($match[1]) - 6) . "src=\"{$src}\""; + return $src; + } + + /** + * preg_replace_callback hanlder + * + * replace srcset string with multiple paths + * @param array $match + * + * @return string changed result + */ + private function _replaceSrcsetPath($match) + { + // explode urls by comma + $url_list = explode(",", $match[1]); + + foreach ($url_list as &$url) { + // replace if url is not starting with the pattern + $url = preg_replace_callback( + '/^(?!(?:https?|file):\/\/|[\/\{])([^"]+)/is', + array($this, '_replaceRelativePath'), + trim($url) + ); + } + $srcset = implode(", ", $url_list); + + return substr($match[0], 0, -strlen($match[1]) - 9) . "srcset=\"{$srcset}\""; } /** From 96bbbcf4bc09ca2e392287e89288ba13ca2a4ae9 Mon Sep 17 00:00:00 2001 From: Chanyoung Oh Date: Thu, 7 Jan 2021 13:09:20 +0000 Subject: [PATCH 2/4] Fix typo --- classes/template/TemplateHandler.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/classes/template/TemplateHandler.class.php b/classes/template/TemplateHandler.class.php index efd19d7d2..2e2935412 100644 --- a/classes/template/TemplateHandler.class.php +++ b/classes/template/TemplateHandler.class.php @@ -384,7 +384,7 @@ class TemplateHandler } /** - * preg_replace_callback hanlder + * preg_replace_callback handler * * replace image path * @param array $match @@ -434,7 +434,7 @@ class TemplateHandler } /** - * preg_replace_callback hanlder + * preg_replace_callback handler * * replace srcset string with multiple paths * @param array $match @@ -577,7 +577,7 @@ class TemplateHandler } /** - * preg_replace_callback hanlder + * preg_replace_callback handler * replace php code. * @param array $m * @return string changed result From adcbe248c555427b14f99e3174ee6daab40b467a Mon Sep 17 00:00:00 2001 From: Chanyoung Oh Date: Thu, 7 Jan 2021 13:16:19 +0000 Subject: [PATCH 3/4] Improve url regular expression - Improve regex to process non-whitespace characters only - Remove /s flag --- classes/template/TemplateHandler.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/template/TemplateHandler.class.php b/classes/template/TemplateHandler.class.php index 2e2935412..9161dd8b5 100644 --- a/classes/template/TemplateHandler.class.php +++ b/classes/template/TemplateHandler.class.php @@ -449,7 +449,7 @@ class TemplateHandler foreach ($url_list as &$url) { // replace if url is not starting with the pattern $url = preg_replace_callback( - '/^(?!(?:https?|file):\/\/|[\/\{])([^"]+)/is', + '/^(?!(?:https?|file):\/\/|[\/\{])(\S+)/i', array($this, '_replaceRelativePath'), trim($url) ); From af6d60e258bc4480778224039aaa6b105bb6cf08 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Thu, 7 Jan 2021 22:56:50 +0900 Subject: [PATCH 4/4] Add unit tests --- tests/unit/classes/TemplateHandlerTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/unit/classes/TemplateHandlerTest.php b/tests/unit/classes/TemplateHandlerTest.php index 8a4d07d81..95daf9746 100644 --- a/tests/unit/classes/TemplateHandlerTest.php +++ b/tests/unit/classes/TemplateHandlerTest.php @@ -275,6 +275,11 @@ class TemplateHandlerTest extends \Codeception\TestCase\Test array( 'asdf src="../img/img.gif" asdf', '?>asdf src="../img/img.gif" asdf' + ), + // srcset (PR #1544) + array( + 'this is a test image.', + '?>this is a test image.' ), // Rhymix improvements (PR #604) array(