diff --git a/common/framework/parsers/template/TemplateParser_v2.php b/common/framework/parsers/template/TemplateParser_v2.php index ec5a6c873..b1d6c0c46 100644 --- a/common/framework/parsers/template/TemplateParser_v2.php +++ b/common/framework/parsers/template/TemplateParser_v2.php @@ -236,7 +236,7 @@ class TemplateParser_v2 // Convert all src and srcset attributes. $regexp = '#(<(?:img|audio|video|script|input|source|link)\s[^>]*)(src|srcset|poster)="([^"]+)"#'; - return preg_replace_callback($regexp, function($match) use ($basepath) { + $content = preg_replace_callback($regexp, function($match) use ($basepath) { if ($match[2] !== 'srcset') { $src = trim($match[3]); @@ -253,6 +253,21 @@ class TemplateParser_v2 return $match[1] . sprintf('srcset="%s"', implode(', ', $result)); } }, $content); + + // Convert relative paths in CSS url() function. + $regexp = ['#\b(style=")([^"]+)(")#', '#()#s']; + $content = preg_replace_callback($regexp, function($match) use ($basepath) { + $regexp = '#\b(url\([\'"]?)([^\'"\(\)]+)([\'"]?\))#'; + $match[2] = preg_replace_callback($regexp, function($match) use ($basepath) { + if ($this->template->isRelativePath($match[2] = trim($match[2]))) + { + $match[2] = $this->template->convertPath($match[2], $basepath); + } + return $match[1] . $match[2] . $match[3]; + }, $match[2]); + return $match[1] . $match[2] . $match[3]; + }, $content); + return $content; } /** diff --git a/tests/unit/framework/parsers/TemplateParserV2Test.php b/tests/unit/framework/parsers/TemplateParserV2Test.php index 15c010ad3..e6c7b0d9c 100644 --- a/tests/unit/framework/parsers/TemplateParserV2Test.php +++ b/tests/unit/framework/parsers/TemplateParserV2Test.php @@ -600,6 +600,41 @@ class TemplateParserV2Test extends \Codeception\Test\Unit $source = ''; $target = ''; $this->assertEquals($target, $this->_parse($source)); + + // url() conversion in style sttribute + $source = '
'; + $target = '
'; + $this->assertEquals($target, $this->_parse($source)); + + $source = '
'; + $target = '
'; + $this->assertEquals($target, $this->_parse($source)); + + $source = '
'; + $target = '
'; + $this->assertEquals($target, $this->_parse($source)); + + $source = '
'; + $target = '
'; + $this->assertEquals($target, $this->_parse($source)); + + $source = '
'; + $target = '
'; + $this->assertEquals($target, $this->_parse($source)); + + // url() conversion in '; + $target = ''; + $this->assertEquals($target, $this->_parse($source)); + + // No url() conversion in other tags or attributes + $source = ' .foo { list-style-image: url(img/foo.jpg); } '; + $target = ' .foo { list-style-image: url(img/foo.jpg); } '; + $this->assertEquals($target, $this->_parse($source)); + + $source = '

url(img/foo.jpg); }

'; + $target = '

url(img/foo.jpg); }

'; + $this->assertEquals($target, $this->_parse($source)); } public function testBlockConditions()