Fix #2203 'this' in lang directive will refer to current module

This commit is contained in:
Kijin Sung 2023-10-31 21:59:33 +09:00
parent 3233619eac
commit e72336c90c
7 changed files with 69 additions and 6 deletions

View file

@ -18,6 +18,7 @@ class Template
*/
public $config;
public $source_type;
public $source_name;
public $parent;
public $vars;
@ -184,7 +185,11 @@ class Template
$this->config->version = 2;
$this->config->autoescape = true;
}
$this->source_type = preg_match('!^((?:m\.)?[a-z]+)/!', $this->relative_dirname, $match) ? $match[1] : null;
if (preg_match('!^(addons|common|(?:m\.)?layouts|modules|plugins|themes|widgets|widgetstyles)/(\w+)!', $this->relative_dirname, $match))
{
$this->source_type = $match[1];
$this->source_name = $match[2];
}
$this->path = $this->absolute_dirname;
$this->web_path = \RX_BASEURL . $this->relative_dirname;
$this->setCachePath();
@ -928,4 +933,26 @@ class Template
}
return count($args) ? in_array((string)$validator_id, $args, true) : true;
}
/**
* Lang shortcut for v2.
*
* @param ...$args
* @return string
*/
protected function _v2_lang(...$args): string
{
if (!isset($GLOBALS['lang']) || !$GLOBALS['lang'] instanceof Lang)
{
$GLOBALS['lang'] = Lang::getInstance(\Context::getLangType());
$GLOBALS['lang']->loadDirectory(\RX_BASEDIR . 'common/lang', 'common');
}
if (isset($args[0]) && !strncmp($args[0], 'this.', 5))
{
$args[0] = $this->source_name . '.' . substr($args[0], 5);
}
return $GLOBALS['lang']->get(...$args);
}
}

View file

@ -720,7 +720,7 @@ class TemplateParser_v2
'json_encode(%s, self::$_json_options) : ' .
'htmlspecialchars(json_encode(%s, self::$_json_options), \ENT_QUOTES, \'UTF-8\', false); ?>', $args, $args);
case 'lang':
return sprintf('<?php echo $this->config->context === \'JS\' ? escape_js(lang(%s)) : lang(%s); ?>', $args, $args);
return sprintf('<?php echo $this->config->context === \'JS\' ? escape_js($this->_v2_lang(%s)) : $this->_v2_lang(%s); ?>', $args, $args);
case 'dump':
return sprintf('<?php ob_start(); var_dump(%s); \$__dump = ob_get_clean(); echo rtrim(\$__dump); ?>', $args);
case 'dd':

View file

@ -84,7 +84,7 @@
<!-- ETC -->
@desktop
<div id="rhymix_waiting" class="wfsr" data-message="@lang('msg_call_server')"></div>
<div id="rhymix_waiting" class="wfsr" data-message="@lang('common.msg_call_server')"></div>
@enddesktop
<div id="rhymix_alert"></div>
<div id="rhymix_debug_panel"></div>

View file

@ -0,0 +1,3 @@
<p>comment.foobarfoobar</p>
<p>document.foobarbazbaz</p>

View file

@ -0,0 +1,3 @@
<p>comment.foobarfoobar</p>
<p>member.foobarbazbaz</p>

View file

@ -0,0 +1,4 @@
@version(2)
<p>@lang('comment.foobarfoobar')</p>
<p>@lang('this.foobarbazbaz')</p>

View file

@ -936,17 +936,17 @@ class TemplateParserV2Test extends \Codeception\Test\Unit
// Lang code with variable as name
$source = '@lang($var->name)';
$target = '<?php echo $this->config->context === \'JS\' ? escape_js(lang($__Context->var->name)) : lang($__Context->var->name); ?>';
$target = '<?php echo $this->config->context === \'JS\' ? escape_js($this->_v2_lang($__Context->var->name)) : $this->_v2_lang($__Context->var->name); ?>';
$this->assertEquals($target, $this->_parse($source));
// Lang code with literal name and variable
$source = "@lang('board.cmd_list_items', \$var)";
$target = "<?php echo \$this->config->context === 'JS' ? escape_js(lang('board.cmd_list_items', \$__Context->var)) : lang('board.cmd_list_items', \$__Context->var); ?>";
$target = "<?php echo \$this->config->context === 'JS' ? escape_js(\$this->_v2_lang('board.cmd_list_items', \$__Context->var)) : \$this->_v2_lang('board.cmd_list_items', \$__Context->var); ?>";
$this->assertEquals($target, $this->_parse($source));
// Lang code with class alias
$source = "@use('Rhymix\Framework\Lang', 'Lang')\n" . '<p>@lang(Lang::getLang())</p>';
$target = "\n" . '<p><?php echo $this->config->context === \'JS\' ? escape_js(lang(Rhymix\Framework\Lang::getLang())) : lang(Rhymix\Framework\Lang::getLang()); ?></p>';
$target = "\n" . '<p><?php echo $this->config->context === \'JS\' ? escape_js($this->_v2_lang(Rhymix\Framework\Lang::getLang())) : $this->_v2_lang(Rhymix\Framework\Lang::getLang()); ?></p>';
$this->assertEquals($target, $this->_parse($source));
// Dump one variable
@ -1149,6 +1149,32 @@ class TemplateParserV2Test extends \Codeception\Test\Unit
$list = \Context::getCssFile();
$this->assertStringContainsString('/rhymix/tests/_data/template/css/style.scss', array_first($list)['file']);
// Lang
$tmpl = new \Rhymix\Framework\Template('./tests/_data/template', 'v2lang.html');
$tmpl->source_type = 'modules';
$tmpl->source_name = 'document';
$tmpl->disableCache();
$executed_output = $tmpl->compile();
//Rhymix\Framework\Storage::write(\RX_BASEDIR . 'tests/_data/template/v2lang.executed1.html', $executed_output);
$expected = file_get_contents(\RX_BASEDIR . 'tests/_data/template/v2lang.executed1.html');
$this->assertEquals(
$this->_normalizeWhitespace($expected),
$this->_normalizeWhitespace($executed_output)
);
$tmpl->source_type = 'modules';
$tmpl->source_name = 'member';
$tmpl->disableCache();
$executed_output = $tmpl->compile();
//Rhymix\Framework\Storage::write(\RX_BASEDIR . 'tests/_data/template/v2lang.executed2.html', $executed_output);
$expected = file_get_contents(\RX_BASEDIR . 'tests/_data/template/v2lang.executed2.html');
$this->assertEquals(
$this->_normalizeWhitespace($expected),
$this->_normalizeWhitespace($executed_output)
);
// Loop variable
$tmpl = new \Rhymix\Framework\Template('./tests/_data/template', 'v2loops.html');
$tmpl->disableCache();