Merge pull request #1544 from shydah/pr/template-srcset

템플릿 내 srcset의 상대경로를 절대경로로 변환하도록 개선
This commit is contained in:
Kijin Sung 2021-01-07 23:04:59 +09:00 committed by GitHub
commit e2e32902b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 4 deletions

View file

@ -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);
@ -381,7 +384,7 @@ class TemplateHandler
}
/**
* preg_replace_callback hanlder
* preg_replace_callback handler
*
* replace image path
* @param array $match
@ -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 handler
*
* 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):\/\/|[\/\{])(\S+)/i',
array($this, '_replaceRelativePath'),
trim($url)
);
}
$srcset = implode(", ", $url_list);
return substr($match[0], 0, -strlen($match[1]) - 9) . "srcset=\"{$srcset}\"";
}
/**
@ -536,7 +577,7 @@ class TemplateHandler
}
/**
* preg_replace_callback hanlder
* preg_replace_callback handler
* replace php code.
* @param array $m
* @return string changed result

View file

@ -275,6 +275,11 @@ class TemplateHandlerTest extends \Codeception\TestCase\Test
array(
'<input>asdf src="../img/img.gif" asdf</input>',
'?><input>asdf src="../img/img.gif" asdf</input>'
),
// srcset (PR #1544)
array(
'<img src="./img/sticker_banner_960w.png" alt="this is a test image." srcset="https://abc.com/static/img/test@2x.png 2x, http://abc.com/static/test@2.5x.png 2.5x,../img/test@3x.png 3x, ../img/test_960w.png 960w, {$mid}/image.png 480w">',
'?><img src="/rhymix/tests/unit/classes/template/img/sticker_banner_960w.png" alt="this is a test image." srcset="https://abc.com/static/img/test@2x.png 2x, http://abc.com/static/test@2.5x.png 2.5x, /rhymix/tests/unit/classes/img/test@3x.png 3x, /rhymix/tests/unit/classes/img/test_960w.png 960w, <?php echo $__Context->mid ?>/image.png 480w">'
),
// Rhymix improvements (PR #604)
array(