Skip to content

Commit

Permalink
Merge pull request #199 from thisispiers/master
Browse files Browse the repository at this point in the history
add “constant()” TSSFunction
  • Loading branch information
TRPB committed Nov 23, 2018
2 parents 1c2473e + babee5b commit 0220407
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Module/Functions.php
Expand Up @@ -21,6 +21,7 @@ public function load(\Transphporm\Config $config) {
$templateFunction = new \Transphporm\TSSFunction\Template($config->getElementData(), $config->getCssToXpath(), $baseDir);
$functionSet->addFunction('template', $templateFunction);
$functionSet->addFunction('json', new \Transphporm\TSSFunction\Json($baseDir));
$functionSet->addFunction('constant', new \Transphporm\TSSFunction\Constant());

// Register HTML formatter here because it uses the template function
$config->registerFormatter(new \Transphporm\Formatter\HTMLFormatter($templateFunction));
Expand Down
17 changes: 17 additions & 0 deletions src/TSSFunction/Constant.php
@@ -0,0 +1,17 @@
<?php
/* @description Transformation Style Sheets - Revolutionising PHP templating *
* @author Tom Butler tom@r.je *
* @copyright 2017 Tom Butler <tom@r.je> | https://r.je/ *
* @license http://www.opensource.org/licenses/bsd-license.php BSD License *
* @version 1.2 */
namespace Transphporm\TSSFunction;
/* Handles constant() function in the TSS stlyesheet */
class Constant implements \Transphporm\TSSFunction {
public function run(array $args, \DomElement $element) {
$const_name = strtoupper(trim($args[0]));
if (!defined($const_name)) {
throw new \Exception($const_name . ' is not a defined constant');
}
return constant($const_name);
}
}
28 changes: 28 additions & 0 deletions tests/TransphpormTest.php
Expand Up @@ -494,6 +494,34 @@ public function testWriteAttributeParamFromData() {
$this->assertEquals('<div class="classname">Test</div>', $template->output("class")->body);
}

public function testReadConstant() {
$template = '
<div>Test</div>
';

$tss = 'div {content: constant(MY_CONSTANT); }';

define('MY_CONSTANT', 'constant value');
$template = new \Transphporm\Builder($template, $tss);

$this->assertEquals('<div class="MY_CONSTANT">constant value</div>', $template->output()->body);
}



public function testReadConstantFromAttribute() {
$template = '
<input name="foo" data-value="MY_ATTR_CONSTANT" />
';

$tss = 'input:attr(value) {content: constant(attr(data-value)); }';

define('MY_ATTR_CONSTANT', 'attr constant value');
$template = new \Transphporm\Builder($template, $tss);

$this->assertEquals('<input name="foo" data-value="MY_ATTR_CONSTANT" value="attr constant value">', $template->output()->body);
}

public function testComments() {
$template = '
<div>Test</div>
Expand Down

0 comments on commit 0220407

Please sign in to comment.