fix #1510 이미지 상대경로 처리 보완

This commit is contained in:
bnu 2015-06-04 17:51:40 +09:00
parent 0130ab6493
commit a4f9897423
2 changed files with 21 additions and 5 deletions

View file

@ -236,7 +236,7 @@ class TemplateHandler
$buff = preg_replace('@<!--//.*?-->@s', '', $buff);
// replace value of src in img/input/script tag
$buff = preg_replace_callback('/(<(?:img|input|script).+src=")(?!(?:https?|file):\/\/|[\/\{])([^"]+)("[^>]+>)/is', array($this, '_replacePath'), $buff);
$buff = preg_replace_callback('/<(?:img|input|script)(?:[^<>]*?)(?(?=cond=")(?:cond="[^"]+"[^<>]*)+|)[^<>]* src="(?!(?:https?|file):\/\/|[\/\{])([^"]+)"/is', array($this, '_replacePath'), $buff);
// replace loop and cond template syntax
$buff = $this->_parseInline($buff);
@ -415,18 +415,18 @@ class TemplateHandler
private function _replacePath($match)
{
//return origin conde when src value started '${'.
if(preg_match('@^\${@', $match[2]))
if(preg_match('@^\${@', $match[1]))
{
return $match[0];
}
//return origin code when src value include variable.
if(preg_match('@^[\'|"]\s*\.\s*\$@', $match[2]))
if(preg_match('@^[\'|"]\s*\.\s*\$@', $match[1]))
{
return $match[0];
}
$src = preg_replace('@^(\./)+@', '', trim($match[2]));
$src = preg_replace('@^(\./)+@', '', trim($match[1]));
$src = $this->web_path . $src;
$src = str_replace('/./', '/', $src);
@ -439,7 +439,7 @@ class TemplateHandler
$src = $tmp;
}
return $match[1] . $src . $match[3];
return substr($match[0], 0, -strlen($match[1]) - 6) . "src=\"{$src}\"";
}
/**

View file

@ -259,10 +259,26 @@ class TemplateHandlerTest extends \Codeception\TestCase\Test
'<input foo="bar" /> <img cond="$foo->bar" alt="alt" src="../common/mobile.gif" />',
'?><input foo="bar" /> <?php if($__Context->foo->bar){ ?><img alt="alt" src="/xe/tests/unit/classes/common/mobile.gif" /><?php } ?>'
),
array(
'<input foo="bar" />' . "\n" . '<input foo="bar" /> <img cond="$foo->bar" alt="alt" src="../common/mobile.gif" />',
'?><input foo="bar" />' . PHP_EOL . '<input foo="bar" /> <?php if($__Context->foo->bar){ ?><img alt="alt" src="/xe/tests/unit/classes/common/mobile.gif" /><?php } ?>'
),
array(
'asf <img src="{$foo->bar}" />',
'?>asf <img src="<?php echo $__Context->foo->bar ?>" />'
),
array(
'<img alt="" '.PHP_EOL.' src="../myxe/xe/img.png" />',
'?><img alt="" '.PHP_EOL.' src="/xe/tests/unit/classes/myxe/xe/img.png" />'
),
array(
'<input>asdf src="../img/img.gif" asdf</input> <img alt="src" src="../myxe/xe/img.png" /> <input>asdf src="../img/img.gif" asdf</input>',
'?><input>asdf src="../img/img.gif" asdf</input> <img alt="src" src="/xe/tests/unit/classes/myxe/xe/img.png" /> <input>asdf src="../img/img.gif" asdf</input>'
),
array(
'<input>asdf src="../img/img.gif" asdf</input>',
'?><input>asdf src="../img/img.gif" asdf</input>'
),
);
}