Fixed some bugs in TemplateHandler class

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@9274 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ngleader 2011-09-22 08:36:51 +00:00
parent 18a01f4e35
commit a9660e3fe3
2 changed files with 41 additions and 17 deletions

View file

@ -389,15 +389,18 @@
if($cmd == 'import') $cmd = 'load';
}
$isRemoteFile = !!preg_match('@^https?://@i', $attr['target']);
if(!$isRemoteFile && !preg_match('@^\.?/@',$attr['target'])) $attr['target'] = './'.$attr['target'];
$metafile = '';
$pathinfo = pathinfo($attr['target']);
$relativeDir = $this->_getRelativeDir($pathinfo['dirname']);
if(!$isRemoteFile) $attr['target'] = $relativeDir.'/'.$pathinfo['basename'];
$isRemoteFile = !!preg_match('@^https?://@i', $attr['target']);
if(!$isRemoteFile) {
if(!preg_match('@^\.?/@',$attr['target'])) $attr['target'] = './'.$attr['target'];
$relativeDir = $this->_getRelativeDir($pathinfo['dirname']);
$attr['target'] = $relativeDir.'/'.$pathinfo['basename'];
}
switch($pathinfo['extension'])
{
@ -411,18 +414,19 @@
}
break;
case 'js':
if($cmd == 'unload') {
$result = "Context::unloadFile('{$attr['target']}','{$attr['targetie']}');";
if($cmd == 'load') {
$metafile = $attr['target'];
$result = "\$__tmp=array('{$attr['target']}','{$attr['type']}','{$attr['targetie']}','{$attr['index']}','{$attr['usecdn']}','{$attr['cdnprefix']}','{$attr['cdnversion']}');Context::loadFile(\$__tmp);unset(\$__tmp);";
} else {
$result = "\$__tmp=array('{$attr['target']}','{$attr['media']}','{$attr['targetie']}','{$attr['index']}','{$attr['usecdn']}','{$attr['cdnprefix']}','{$attr['cdnversion']}');Context::loadFile(\$__tmp);unset(\$__tmp);";
$result = "Context::unloadFile('{$attr['target']}','{$attr['targetie']}');";
}
break;
case 'css':
if($cmd == 'unload') {
$result = "Context::unloadFile('{$attr['target']}','{$attr['targetie']}','{$attr['media']}');";
} else {
if($cmd == 'load') {
$metafile = $attr['target'];
$result = "\$__tmp=array('{$attr['target']}','{$attr['type']}','{$attr['targetie']}','{$attr['index']}','{$attr['usecdn']}','{$attr['cdnprefix']}','{$attr['cdnversion']}');Context::loadFile(\$__tmp);unset(\$__tmp);";
$result = "\$__tmp=array('{$attr['target']}','{$attr['media']}','{$attr['targetie']}','{$attr['index']}','{$attr['usecdn']}','{$attr['cdnprefix']}','{$attr['cdnversion']}');Context::loadFile(\$__tmp);unset(\$__tmp);";
} else {
$result = "Context::unloadFile('{$attr['target']}','{$attr['targetie']}','{$attr['media']}');";
}
break;
}

View file

@ -103,11 +103,31 @@ class TemplateHandlerTest extends PHPUnit_Framework_TestCase
'<dummy /><include target="../sample.html" /><div>This is another dummy</div>',
'<dummy /><?php echo TemplateHandler::getInstance()->compile(\'tests/classes\',\'sample.html\') ?><div>This is another dummy</div>'
),
// <load target="../../../modules/page/lang/lang.xml">
array(
'<dummy /><load target="../../../modules/page/lang/lang.xml" /><dummy />',
'<dummy /><?php Context::loadLang(\'modules/page/lang\'); ?><dummy />'
),
// <load target="style.css">
array(
'<dummy /><load target="css/style.css" /><dummy />',
'<dummy /><!--#Meta:tests/classes/template/css/style.css--><?php $__tmp=array(\'tests/classes/template/css/style.css\',\'\',\'\',\'\',\'\',\'\',\'\');Context::loadFile($__tmp);unset($__tmp); ?><dummy />'
),
// <unload target="style.css">
array(
'<dummy /><unload target="css/style.css" /><dummy />',
'<dummy /><?php Context::unloadFile(\'tests/classes/template/css/style.css\',\'\',\'\'); ?><dummy />'
),
// <!--%import("../script.js",type="body")-->
array(
'<dummy /><!--%import("../script.js",type="body")--><dummy />',
'<dummy /><!--#Meta:tests/classes/script.js--><?php $__tmp=array(\'tests/classes/script.js\',\'body\',\'\',\'\',\'\',\'\',\'\');Context::loadFile($__tmp);unset($__tmp); ?><dummy />'
),
// <!--%unload("../script.js",type="body")-->
array(
'<dummy /><!--%unload("../script.js",type="body")--><dummy />',
'<dummy /><?php Context::unloadFile(\'tests/classes/script.js\',\'\'); ?><dummy />'
),
// comment
array(
'<dummy_before /><!--// this is a comment--><dummy_after />',
@ -118,12 +138,12 @@ class TemplateHandlerTest extends PHPUnit_Framework_TestCase
'<meta charset="utf-8" cond="$foo">',
'<?php if($__Context->foo){ ?><meta charset="utf-8"><?php } ?>'
),
// relative path
// relative path1
array(
'<img src="http://naver.com/naver.gif"><input type="image" src="../local.gif" />',
'<img src="http://naver.com/naver.gif"><input type="image" src="/xe/tests/classes/local.gif" />'
),
// relative path
// relative path2
array(
'<img src="http://naver.com/naver.gif"><input type="image" src="../../dir/local.gif" />',
'<img src="http://naver.com/naver.gif"><input type="image" src="/xe/tests/dir/local.gif" />'
@ -136,7 +156,7 @@ class TemplateHandlerTest extends PHPUnit_Framework_TestCase
// issue 103
array(
'<load target="http://aaa.com/aaa.js" />',
'<?php $__tmp=array(\'http://aaa.com/aaa.js\',\'\',\'\',\'\',\'\',\'\',\'\');Context::loadFile($__tmp);unset($__tmp); ?>'
'<!--#Meta:http://aaa.com/aaa.js--><?php $__tmp=array(\'http://aaa.com/aaa.js\',\'\',\'\',\'\',\'\',\'\',\'\');Context::loadFile($__tmp);unset($__tmp); ?>'
),
// issue 135
array(
@ -156,7 +176,7 @@ class TemplateHandlerTest extends PHPUnit_Framework_TestCase
*/
public function testParse($tpl, $expected)
{
$tmpl = new TemplateHandler();
$tmpl = TemplateHandler::getInstance();
$tmpl->init(dirname(__FILE__), 'sample.html');
$result = $tmpl->parse($tpl, $expected);