Convert class and style builder into a method of Template class

This commit is contained in:
Kijin Sung 2023-10-17 22:44:27 +09:00
parent 82b9107ca0
commit 55cafc5c33
6 changed files with 61 additions and 24 deletions

View file

@ -471,4 +471,34 @@ class Template
array_pop(self::$_loopvars); array_pop(self::$_loopvars);
} }
} }
/**
* Attribute builder for v2.
*
* @param string $attribute
* @param array $definition
* @return string
*/
protected function _v2_buildAttribute(string $attribute, array $definition = []): string
{
$delimiters = [
'class' => ' ',
'style' => '; ',
];
$values = [];
foreach ($definition as $key => $val)
{
if (is_int($key) && !empty($val))
{
$values[] = $val;
}
elseif ($val)
{
$values[] = $key;
}
}
return sprintf(' %s="%s"', $attribute, escape(implode($delimiters[$attribute], $values), false));
}
} }

View file

@ -805,20 +805,9 @@ class TemplateParser_v2
// Convert Blade-style @class and @style conditions. // Convert Blade-style @class and @style conditions.
$regexp = '#\s*(?<!@)@(class|style)\x20?(' . $parentheses . ')#'; $regexp = '#\s*(?<!@)@(class|style)\x20?(' . $parentheses . ')#';
$content = preg_replace_callback($regexp, function($match) { $content = preg_replace_callback($regexp, function($match) {
$defs = self::_convertVariableScope($match[2]); $attribute = trim($match[1]);
$delimiter = $match[1] === 'class' ? ' ' : '; '; $definitions = self::_convertVariableScope(substr($match[2], 1, strlen($match[2]) - 2));
$tpl = '<?php (function(array $__defs) { '; return sprintf("<?php echo \$this->_v2_buildAttribute('%s', %s); ?>", $attribute, $definitions);
$tpl .= '$__values = []; ';
$tpl .= 'foreach ($__defs as $__key => $__val): ';
$tpl .= 'if (is_numeric($__key)): $__values[] = $__val; ';
$tpl .= 'elseif ($__val): $__values[] = $__key; ';
$tpl .= 'endif; endforeach; ';
$tpl .= 'if ($__values): ';
$tpl .= 'echo \' ' . $match[1] . '="\'; ';
$tpl .= 'echo htmlspecialchars(implode(\'' . $delimiter . '\', $__values), \ENT_QUOTES, \'UTF-8\', false); ';
$tpl .= 'echo \'"\'; ';
$tpl .= 'endif; })(' . $defs . '); ?>';
return self::_escapeVars($tpl);
}, $content); }, $content);
return $content; return $content;

View file

@ -47,8 +47,17 @@
<?php endif; ?> <?php endif; ?>
<div class="barContainer" data-bar="<?php echo $this->config->context === 'JS' ? (json_encode($__Context->bar ?? '', \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES | \JSON_HEX_TAG | \JSON_HEX_QUOT)) : htmlspecialchars(json_encode($__Context->bar ?? '', \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES | \JSON_HEX_TAG | \JSON_HEX_QUOT), \ENT_QUOTES, 'UTF-8', false); ?>"> <div class="barContainer" data-bar="<?php echo $this->config->context === 'JS' ? (json_encode($__Context->bar ?? '', \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES | \JSON_HEX_TAG | \JSON_HEX_QUOT)) : htmlspecialchars(json_encode($__Context->bar ?? '', \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES | \JSON_HEX_TAG | \JSON_HEX_QUOT), \ENT_QUOTES, 'UTF-8', false); ?>">
<span<?php (function(array $__defs) { $__values = []; foreach ($__defs as $__key => $__val): if (is_numeric($__key)): $__values[] = $__val; elseif ($__val): $__values[] = $__key; endif; endforeach; if ($__values): echo ' class="'; echo htmlspecialchars(implode(' ', $__values), \ENT_QUOTES, 'UTF-8', false); echo '"'; endif; })((['a-1', 'font-normal' => $__Context->foo, 'text-blue' => false, 'bg-white' => true])); ?>></span> <span<?php echo $this->_v2_buildAttribute('class', [
<span<?php (function(array $__defs) { $__values = []; foreach ($__defs as $__key => $__val): if (is_numeric($__key)): $__values[] = $__val; elseif ($__val): $__values[] = $__key; endif; endforeach; if ($__values): echo ' style="'; echo htmlspecialchars(implode('; ', $__values), \ENT_QUOTES, 'UTF-8', false); echo '"'; endif; })((['border-radius: 0.25rem', 'margin: 1rem' => Context::get('bar'), 'padding: 2rem' => false])); ?>></span> 'a-1',
'font-normal' => $__Context->foo,
'text-blue' => false,
'bg-gray-200' => true
]); ?>></span>
<span<?php echo $this->_v2_buildAttribute('style', [
'border-radius: 0.25rem',
'margin: 1rem' => Context::get('bar'),
'padding: 2rem' => false,
]); ?>></span>
</div> </div>
<script type="text/javascript"<?php $this->config->context = "JS"; ?>> <script type="text/javascript"<?php $this->config->context = "JS"; ?>>

View file

@ -52,7 +52,7 @@
<p>The full class name is Rhymix\Framework\Push, Rhymix\Framework\Push really.</p> <p>The full class name is Rhymix\Framework\Push, Rhymix\Framework\Push really.</p>
<div class="barContainer" data-bar="[&quot;Rhy&quot;,&quot;miX&quot;,&quot;is&quot;,&quot;da&quot;,&quot;BEST!&quot;]"> <div class="barContainer" data-bar="[&quot;Rhy&quot;,&quot;miX&quot;,&quot;is&quot;,&quot;da&quot;,&quot;BEST!&quot;]">
<span class="a-1 font-normal bg-white"></span> <span class="a-1 font-normal bg-gray-200"></span>
<span style="border-radius: 0.25rem; margin: 1rem"></span> <span style="border-radius: 0.25rem; margin: 1rem"></span>
</div> </div>

View file

@ -47,8 +47,17 @@
@enddesktop @enddesktop
<div class="barContainer" data-bar="{$bar|json}"> <div class="barContainer" data-bar="{$bar|json}">
<span @class(['a-1', 'font-normal' => $foo, 'text-blue' => false, 'bg-white' => true])></span> <span @class([
<span @style(['border-radius: 0.25rem', 'margin: 1rem' => Context::get('bar'), 'padding: 2rem' => false])></span> 'a-1',
'font-normal' => $foo,
'text-blue' => false,
'bg-gray-200' => true
])></span>
<span @style([
'border-radius: 0.25rem',
'margin: 1rem' => Context::get('bar'),
'padding: 2rem' => false,
])></span>
</div> </div>
<script type="text/javascript"> <script type="text/javascript">

View file

@ -868,13 +868,13 @@ class TemplateParserV2Test extends \Codeception\Test\Unit
// @class // @class
$source = "<span @class(['a-1', 'font-normal' => \$foo, 'text-blue' => false, 'bg-white' => true])></span>"; $source = "<span @class(['a-1', 'font-normal' => \$foo, 'text-blue' => false, 'bg-white' => true])></span>";
$this->assertStringContainsString("implode(' ', \$__values)", $this->_parse($source)); $this->assertStringContainsString("\$this->_v2_buildAttribute(", $this->_parse($source));
$this->assertStringContainsString("\$__Context->foo", $this->_parse($source)); $this->assertStringContainsString("\$__Context->foo", $this->_parse($source));
// @style // @style
$source = "<span @style(['border-radius: 0.25rem', 'margin: 1rem' => Context::get('bar')])></span>"; $source = "<span @style(['border-radius: 0.25rem', 'margin: 1rem' => Context::get('bar')])></span>";
$this->assertStringContainsString("implode('; ', \$__values)", $this->_parse($source)); $this->assertStringContainsString("\$this->_v2_buildAttribute(", $this->_parse($source));
$this->assertStringContainsString("if (is_numeric(\$__key)):", $this->_parse($source)); $this->assertStringContainsString("Context::get('bar')]);", $this->_parse($source));
} }
public function testMiscDirectives() public function testMiscDirectives()
@ -1014,7 +1014,7 @@ class TemplateParserV2Test extends \Codeception\Test\Unit
// Get compiled code // Get compiled code
$compiled_output = $tmpl->compileDirect('./tests/_data/template', 'v2example.html'); $compiled_output = $tmpl->compileDirect('./tests/_data/template', 'v2example.html');
$tmpvar = preg_match('/\$__tmp_([0-9a-f]{14})/', $compiled_output, $m) ? $m[1] : ''; $tmpvar = preg_match('/\$__tmp_([0-9a-f]{14})/', $compiled_output, $m) ? $m[1] : '';
//Rhymix\Framework\Storage::write(\RX_BASEDIR . 'tests/_data/template/v2example.compiled.html', $compiled_output); Rhymix\Framework\Storage::write(\RX_BASEDIR . 'tests/_data/template/v2example.compiled.html', $compiled_output);
$expected = file_get_contents(\RX_BASEDIR . 'tests/_data/template/v2example.compiled.html'); $expected = file_get_contents(\RX_BASEDIR . 'tests/_data/template/v2example.compiled.html');
$expected = preg_replace('/RANDOM_LOOP_ID/', $tmpvar, $expected); $expected = preg_replace('/RANDOM_LOOP_ID/', $tmpvar, $expected);
$this->assertEquals( $this->assertEquals(
@ -1024,7 +1024,7 @@ class TemplateParserV2Test extends \Codeception\Test\Unit
// Get final output // Get final output
$executed_output = $tmpl->compile(); $executed_output = $tmpl->compile();
//Rhymix\Framework\Storage::write(\RX_BASEDIR . 'tests/_data/template/v2example.executed.html', $executed_output); Rhymix\Framework\Storage::write(\RX_BASEDIR . 'tests/_data/template/v2example.executed.html', $executed_output);
$expected = file_get_contents(\RX_BASEDIR . 'tests/_data/template/v2example.executed.html'); $expected = file_get_contents(\RX_BASEDIR . 'tests/_data/template/v2example.executed.html');
$expected = preg_replace('/RANDOM_LOOP_ID/', $tmpvar, $expected); $expected = preg_replace('/RANDOM_LOOP_ID/', $tmpvar, $expected);
$this->assertEquals( $this->assertEquals(