diff --git a/src/Builder.php b/src/Builder.php index b9417c7..6e73749 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -81,7 +81,7 @@ private function executeTssRule($rule, $template, $config) { $rule->touch(); $pseudoMatcher = $config->createPseudoMatcher($rule->pseudo); - $hook = new Hook\PropertyHook($rule->properties, $this->baseDir, $rule->baseDir, $pseudoMatcher, $config->getValueParser(), $config->getFunctionSet()); + $hook = new Hook\PropertyHook($rule->properties, $this->baseDir, $config->getLine(), $rule->file, $rule->line, $pseudoMatcher, $config->getValueParser(), $config->getFunctionSet()); $config->loadProperties($hook); $template->addHook($rule->query, $hook); } @@ -107,7 +107,7 @@ private function getRules($template, $config) { //Try to load the cached rules, if not set in the cache (or expired) parse the supplied sheet $rules = $this->cache->load($key, filemtime($this->tss)); - if (!$rules) return $this->cache->write($key, (new Parser\Sheet(file_get_contents($this->tss), $this->baseDir, $config->getCssToXpath(), $config->getValueParser()))->parse()); + if (!$rules) return $this->cache->write($key, (new Parser\Sheet(file_get_contents($this->tss), $this->tss, $config->getCssToXpath(), $config->getValueParser()))->parse()); else return $rules; } else return (new Parser\Sheet($this->tss, $this->baseDir, $config->getCssToXpath(), $config->getValueParser()))->parse(); diff --git a/src/Config.php b/src/Config.php index 931baac..bd06141 100644 --- a/src/Config.php +++ b/src/Config.php @@ -12,6 +12,7 @@ class Config { private $headers; private $formatter; private $baseDir; + private $line = 0; private $elementData; private $xPath; private $valueParser; @@ -38,6 +39,10 @@ public function &getBaseDir() { return $this->baseDir; } + public function &getLine() { + return $this->line; + } + public function registerFormatter($formatter) { $this->formatter->register($formatter); } diff --git a/src/Exception.php b/src/Exception.php new file mode 100644 index 0000000..fd1bb1e --- /dev/null +++ b/src/Exception.php @@ -0,0 +1,14 @@ +getMessage() . ' on Line ' . $line . ' of ' . ($file === null ? 'tss' : $file); + + parent::__construct($message, 0, $runException->getPrevious()); + } +} diff --git a/src/FunctionSet.php b/src/FunctionSet.php index d3623b3..58cc066 100644 --- a/src/FunctionSet.php +++ b/src/FunctionSet.php @@ -16,16 +16,21 @@ public function __construct(Hook\ElementData $elementData) { } public function __call($name, $args) { - if (!($this->functions[$name] instanceof TSSFunction\Data)) { - $tokens = $args[0]; - $parser = new \Transphporm\Parser\Value($this); - $args[0] = $parser->parseTokens($tokens, $this->elementData->getData($this->element)); + try { + if (!($this->functions[$name] instanceof TSSFunction\Data)) { + $tokens = $args[0]; + $parser = new \Transphporm\Parser\Value($this); + $args[0] = $parser->parseTokens($tokens, $this->elementData->getData($this->element)); + } + else if ($args[0] instanceof Parser\Tokens) { + $args[0] = iterator_to_array($args[0]); + } + if (isset($this->functions[$name])) { + return $this->functions[$name]->run($args[0], $this->element); + } } - else if ($args[0] instanceof Parser\Tokens) { - $args[0] = iterator_to_array($args[0]); - } - if (isset($this->functions[$name])) { - return $this->functions[$name]->run($args[0], $this->element); + catch (\Exception $e) { + throw new RunException(Exception::TSS_FUNCTION, $name, $e); } return true; } diff --git a/src/Hook/Formatter.php b/src/Hook/Formatter.php index a83810a..bddbb83 100644 --- a/src/Hook/Formatter.php +++ b/src/Hook/Formatter.php @@ -16,24 +16,34 @@ public function register($formatter) { public function format($value, $rules) { if (!isset($rules['format'])) return $value; $tokens = $rules['format']; - + $functionName = $tokens->from(\Transphporm\Parser\Tokenizer::NAME, true)->read(); $options = []; - foreach (new \Transphporm\Parser\TokenFilterIterator($tokens->from(\Transphporm\Parser\Tokenizer::NAME), [\Transphporm\Parser\Tokenizer::WHITESPACE]) as $token) { + foreach (new \Transphporm\Parser\TokenFilterIterator($tokens->from(\Transphporm\Parser\Tokenizer::NAME), + [\Transphporm\Parser\Tokenizer::WHITESPACE]) as $token) { $options[] = $token['value']; } - return $this->processFormat($options, $functionName, $value); + + try { + return $this->processFormat($options, $functionName, $value); + } + catch (\Exception $e) { + throw new \Transphporm\RunException(\Transphporm\Exception::FORMATTER, $functionName, $e); + } } private function processFormat($format, $functionName, $value) { + $functionExists = false; foreach ($value as &$val) { foreach ($this->formatters as $formatter) { if (is_callable([$formatter, $functionName])) { $val = call_user_func_array([$formatter, $functionName], array_merge([$val], $format)); + $functionExists = true; } } } + if (!$functionExists) throw new \Exception("Formatter '$functionName' does not exist"); return $value; } } diff --git a/src/Hook/PropertyHook.php b/src/Hook/PropertyHook.php index f75e502..75e2aa7 100644 --- a/src/Hook/PropertyHook.php +++ b/src/Hook/PropertyHook.php @@ -8,17 +8,21 @@ /** Hooks into the template system, gets assigned as `ul li` or similar and `run()` is called with any elements that match */ class PropertyHook implements \Transphporm\Hook { private $rules; - private $origBaseDir; - private $newBaseDir; + private $baseDir; + private $configLine; + private $file; + private $line; private $valueParser; private $pseudoMatcher; private $properties = []; private $functionSet; - public function __construct(array $rules, &$origBaseDir, $newBaseDir, PseudoMatcher $pseudoMatcher, \Transphporm\Parser\Value $valueParser, \Transphporm\FunctionSet $functionSet) { + public function __construct(array $rules, &$baseDir, &$configLine, $file, $line, PseudoMatcher $pseudoMatcher, \Transphporm\Parser\Value $valueParser, \Transphporm\FunctionSet $functionSet) { $this->rules = $rules; - $this->origBaseDir = $origBaseDir; - $this->newBaseDir = $newBaseDir; + $this->baseDir = &$baseDir; + $this->configLine = &$configLine; + $this->file = $file; + $this->line = $line; $this->valueParser = $valueParser; $this->pseudoMatcher = $pseudoMatcher; $this->functionSet = $functionSet; @@ -26,17 +30,23 @@ public function __construct(array $rules, &$origBaseDir, $newBaseDir, PseudoMatc public function run(\DomElement $element) { $this->functionSet->setElement($element); - $this->origBaseDir = $this->newBaseDir; - //Don't run if there's a pseudo element like nth-child() and this element doesn't match it - if (!$this->pseudoMatcher->matches($element)) return; + if ($this->file !== null) $this->baseDir = dirname(realpath($this->file)) . DIRECTORY_SEPARATOR; + $this->configLine = $this->line; + try { + //Don't run if there's a pseudo element like nth-child() and this element doesn't match it + if (!$this->pseudoMatcher->matches($element)) return; - // TODO: Have all rule values parsed before running them so that things like `content-append` are not expecting tokens - // problem with this is that anything in data changed by run properties is not shown - // TODO: Allow `update-frequency` to be parsed before it is accessed in rule (might need to switch location of rule check) + // TODO: Have all rule values parsed before running them so that things like `content-append` are not expecting tokens + // problem with this is that anything in data changed by run properties is not shown + // TODO: Allow `update-frequency` to be parsed before it is accessed in rule (might need to switch location of rule check) - foreach ($this->rules as $name => $value) { - $result = $this->callProperty($name, $element, $this->getArgs($value)); - if ($result === false) break; + foreach ($this->rules as $name => $value) { + $result = $this->callProperty($name, $element, $this->getArgs($value)); + if ($result === false) break; + } + } + catch (\Transphporm\RunException $e) { + throw new \Transphporm\Exception($e, $this->file, $this->line); } } @@ -49,6 +59,14 @@ public function registerProperty($name, \Transphporm\Property $property) { } private function callProperty($name, $element, $value) { - if (isset($this->properties[$name])) return $this->properties[$name]->run($value, $element, $this->rules, $this->pseudoMatcher, $this->properties); + if (isset($this->properties[$name])) { + try { + return $this->properties[$name]->run($value, $element, $this->rules, $this->pseudoMatcher, $this->properties); + } + catch (\Exception $e) { + if ($e instanceof \Transphporm\RunException) throw $e; + throw new \Transphporm\RunException(\Transphporm\Exception::PROPERTY, $name, $e); + } + } } } diff --git a/src/Hook/PseudoMatcher.php b/src/Hook/PseudoMatcher.php index 7c9b3a5..fd81df0 100644 --- a/src/Hook/PseudoMatcher.php +++ b/src/Hook/PseudoMatcher.php @@ -23,11 +23,15 @@ public function registerFunction(\Transphporm\Pseudo $pseudo) { public function matches($element) { $matches = true; - foreach ($this->pseudo as $tokens) { foreach ($this->functions as $function) { - $parts = $this->getFuncParts($tokens); - $matches = $matches && $function->match($parts['name'], $parts['args'], $element); + try { + $parts = $this->getFuncParts($tokens); + $matches = $matches && $function->match($parts['name'], $parts['args'], $element); + } + catch (\Exception $e) { + throw new \Transphporm\RunException(\Transphporm\Exception::PSEUDO, $parts['name'], $e); + } } } return $matches; diff --git a/src/Module/Basics.php b/src/Module/Basics.php index 62e91f2..d4b770d 100644 --- a/src/Module/Basics.php +++ b/src/Module/Basics.php @@ -14,7 +14,7 @@ public function load(\Transphporm\Config $config) { $headers = &$config->getHeaders(); $config->registerProperty('content', new \Transphporm\Property\Content($headers, $config->getFormatter())); - $config->registerProperty('repeat', new \Transphporm\Property\Repeat($data, $config->getElementData(), $config->getBaseDir())); + $config->registerProperty('repeat', new \Transphporm\Property\Repeat($data, $config->getElementData(), $config->getBaseDir(), $config->getLine())); $config->registerProperty('display', new \Transphporm\Property\Display); $config->registerProperty('bind', new \Transphporm\Property\Bind($config->getElementData())); } diff --git a/src/Parser/Sheet.php b/src/Parser/Sheet.php index d6230c0..f2120ab 100644 --- a/src/Parser/Sheet.php +++ b/src/Parser/Sheet.php @@ -8,34 +8,39 @@ /** Parses a .tss file into individual rules, each rule has a query e,g, `ul li` and a set of rules e.g. `display: none; bind: iteration(id);` */ class Sheet { private $tss; - private $baseDir; + private $file; private $valueParser; private $xPath; private $tokenizer; - public function __construct($tss, $baseDir, CssToXpath $xPath, Value $valueParser) { + public function __construct($tss, $file, CssToXpath $xPath, Value $valueParser) { $this->tss = $this->stripComments($tss, '//', "\n"); $this->tss = $this->stripComments($this->tss, '/*', '*/'); $this->tokenizer = new Tokenizer($this->tss); $this->tss = $this->tokenizer->getTokens(); - $this->baseDir = $baseDir; + $this->file = $file; $this->xPath = $xPath; $this->valueParser = $valueParser; } public function parse($indexStart = 0) { $rules = []; + $line = 1; foreach (new TokenFilterIterator($this->tss, [Tokenizer::WHITESPACE]) as $token) { if ($processing = $this->processingInstructions($token, count($rules)+$indexStart)) { $this->tss->skip($processing['skip']+1); $rules = array_merge($rules, $processing['rules']); continue; } + else if ($token['type'] === Tokenizer::NEW_LINE) { + $line++; + continue; + } $selector = $this->tss->from($token['type'], true)->to(Tokenizer::OPEN_BRACE); $this->tss->skip(count($selector)); if (count($selector) === 0) break; - $newRules = $this->cssToRules($selector, count($rules)+$indexStart, $this->getProperties($this->tss->current()['value'])); + $newRules = $this->cssToRules($selector, count($rules)+$indexStart, $this->getProperties($this->tss->current()['value']), $line); $rules = $this->writeRule($rules, $newRules); } usort($rules, [$this, 'sortRules']); @@ -47,12 +52,12 @@ private function checkError($rules) { if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); } - private function CssToRules($selector, $index, $properties) { + private function CssToRules($selector, $index, $properties, $line) { $parts = $selector->trim()->splitOnToken(Tokenizer::ARG); $rules = []; foreach ($parts as $part) { $part = $part->trim(); - $rules[$this->tokenizer->serialize($part)] = new \Transphporm\Rule($this->xPath->getXpath($part), $this->xPath->getPseudo($part), $this->xPath->getDepth($part), $index++, $this->baseDir); + $rules[$this->tokenizer->serialize($part)] = new \Transphporm\Rule($this->xPath->getXpath($part), $this->xPath->getPseudo($part), $this->xPath->getDepth($part), $index++, $this->file, $line); $rules[$this->tokenizer->serialize($part)]->properties = $properties; } return $rules; @@ -81,8 +86,9 @@ private function processingInstructions($token, $indexStart) { } private function import($args, $indexStart) { - $fileName = $args[0]; - $sheet = new Sheet(file_get_contents($this->baseDir . $fileName), dirname(realpath($this->baseDir . $fileName)) . DIRECTORY_SEPARATOR, $this->xPath, $this->valueParser); + if ($this->file !== null) $fileName = dirname(realpath($this->file)) . DIRECTORY_SEPARATOR . $args[0]; + else $fileName = $args[0]; + $sheet = new Sheet(file_get_contents($fileName), $fileName, $this->xPath, $this->valueParser); return $sheet->parse($indexStart); } diff --git a/src/Parser/Tokenizer.php b/src/Parser/Tokenizer.php index f5876dc..eb6fa56 100644 --- a/src/Parser/Tokenizer.php +++ b/src/Parser/Tokenizer.php @@ -16,6 +16,7 @@ class Tokenizer { const CONCAT = 'CONCAT'; const ARG = 'ARG'; const WHITESPACE = 'WHITESPACE'; + const NEW_LINE = 'NEW_LINE'; const DOT = 'DOT'; const NUMERIC = 'NUMERIC'; const EQUALS = 'EQUALS'; @@ -49,7 +50,7 @@ class Tokenizer { '>' => self::GREATER_THAN, '@' => self::AT_SIGN, ' ' => self::WHITESPACE, - "\n" => self::WHITESPACE, + "\n" => self::NEW_LINE, "\r" => self::WHITESPACE, "\t" => self::WHITESPACE ]; @@ -75,7 +76,7 @@ public function getTokens($returnObj = true) { private function doSimpleTokens(&$tokens, $char) { if (in_array($char, [Tokenizer::ARG, Tokenizer::CONCAT, Tokenizer::DOT, Tokenizer::NOT, - Tokenizer::EQUALS, Tokenizer::COLON, Tokenizer::SEMI_COLON, Tokenizer::WHITESPACE, + Tokenizer::EQUALS, Tokenizer::COLON, Tokenizer::SEMI_COLON, Tokenizer::WHITESPACE, Tokenizer::NEW_LINE, Tokenizer::NUM_SIGN, Tokenizer::GREATER_THAN, Tokenizer::AT_SIGN])) { $tokens[] = ['type' => $char]; } @@ -170,7 +171,7 @@ public function serialize($tokens) { private function serializeValue($token) { if (isset($token['value'])) { if ($token['value'] instanceof Tokens) return $this->serialize($token['value']); - else return $token['value']; - } + else return $token['value']; + } } } diff --git a/src/Parser/Value.php b/src/Parser/Value.php index 259ffa4..bf3250f 100644 --- a/src/Parser/Value.php +++ b/src/Parser/Value.php @@ -51,7 +51,7 @@ public function parseTokens($tokens, $data = null) { if (count($tokens) <= 0) return [$data]; - foreach (new TokenFilterIterator($tokens, [Tokenizer::WHITESPACE]) as $token) { + foreach (new TokenFilterIterator($tokens, [Tokenizer::WHITESPACE, Tokenizer::NEW_LINE]) as $token) { $this->{$this->tokenFuncs[$token['type']]}($token); } diff --git a/src/Property/Repeat.php b/src/Property/Repeat.php index ab72997..52068b0 100644 --- a/src/Property/Repeat.php +++ b/src/Property/Repeat.php @@ -9,11 +9,13 @@ class Repeat implements \Transphporm\Property { private $functionSet; private $elementData; private $baseDir; + private $line; - public function __construct(\Transphporm\FunctionSet $functionSet, \Transphporm\Hook\ElementData $elementData, &$baseDir) { + public function __construct(\Transphporm\FunctionSet $functionSet, \Transphporm\Hook\ElementData $elementData, &$baseDir, &$line) { $this->functionSet = $functionSet; $this->elementData = $elementData; $this->baseDir = &$baseDir; + $this->line = &$line; } public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) { @@ -60,7 +62,7 @@ private function getMax($values) { } private function createHook($newRules, $pseudoMatcher, $properties) { - $hook = new \Transphporm\Hook\PropertyHook($newRules, $this->baseDir, $this->baseDir, $pseudoMatcher, new \Transphporm\Parser\Value($this->functionSet), $this->functionSet); + $hook = new \Transphporm\Hook\PropertyHook($newRules, $this->baseDir, $this->line, $this->baseDir, $this->line, $pseudoMatcher, new \Transphporm\Parser\Value($this->functionSet), $this->functionSet); foreach ($properties as $name => $property) $hook->registerProperty($name, $property); return $hook; } diff --git a/src/Pseudo/Nth.php b/src/Pseudo/Nth.php index 3b7299d..be30185 100644 --- a/src/Pseudo/Nth.php +++ b/src/Pseudo/Nth.php @@ -10,14 +10,14 @@ class Nth implements \Transphporm\Pseudo { private $count = 0; public function match($name, $args, \DomElement $element) { - + if ($name !== 'nth-child') return true; $this->count++; $criteria = $args[0]; - if (is_callable([$this, $criteria])) return $this->$criteria($this->count); + else if (!is_numeric($criteria)) throw new \Exception("Argument passed to 'nth-child' must be 'odd', 'even', or of type int"); else return $this->count == $criteria; } diff --git a/src/Rule.php b/src/Rule.php index 959eb10..647bd1a 100644 --- a/src/Rule.php +++ b/src/Rule.php @@ -10,7 +10,7 @@ class Rule { private $pseudo; private $depth; private $index; - private $baseDir; + private $file; private $properties = []; private $lastRun = 0; @@ -20,12 +20,13 @@ class Rule { const D = 86400; - public function __construct($query, $pseudo, $depth, $index, $baseDir, array $properties = []) { + public function __construct($query, $pseudo, $depth, $index, $file, $line, array $properties = []) { $this->query = $query; $this->pseudo = $pseudo; $this->depth = $depth; $this->index = $index; - $this->baseDir = $baseDir; + $this->file = $file; + $this->line = $line; $this->properties = $properties; } diff --git a/src/RunException.php b/src/RunException.php new file mode 100644 index 0000000..5a51ae8 --- /dev/null +++ b/src/RunException.php @@ -0,0 +1,9 @@ +Test + '; + + $tss = 'div:before {content: "BEFORE";}'; + + $template = new \Transphporm\Builder($template, $tss); + + $this->assertEquals($this->stripTabs('
BEFORETest
'), $this->stripTabs($template->output()->body)); + } + + public function testAfter() { + $template = ' +
Test
+ '; + + $tss = 'div:after {content: "AFTER";}'; + + $template = new \Transphporm\Builder($template, $tss); + + $this->assertEquals($this->stripTabs('
TestAFTER
'), $this->stripTabs($template->output()->body)); + } + + public function testOverrideAfter() { + $xml = '
+ Test +
'; + + + $includeFile = __DIR__ . DIRECTORY_SEPARATOR . 'include.xml'; + $includeFile = str_replace('\\', '/', $includeFile); + + $tss = "div:after {content: 'foo' } +div:after {content: 'bar' } + "; + $template = new \Transphporm\Builder($xml, $tss); + + $this->assertEquals('
Testbar
', $this->stripTabs($template->output()->body)); + + } + + public function testOverrideBefore() { + $xml = '
+ Test +
'; + + + $includeFile = __DIR__ . DIRECTORY_SEPARATOR . 'include.xml'; + + $tss = "div:before {content: 'foo' } + div:before {content: 'bar';} + "; + $template = new \Transphporm\Builder($xml, $tss); + + $this->assertEquals('
barTest
', $this->stripTabs($template->output()->body)); + + } +} diff --git a/tests/DateFormatTest.php b/tests/DateFormatTest.php index 0f0791c..df794cb 100644 --- a/tests/DateFormatTest.php +++ b/tests/DateFormatTest.php @@ -41,47 +41,47 @@ public function testTomorrow() { } public function testSecondsAgo() { - $this->assertEquals('28 seconds ago', $this->relative('-28 seconds')); + $this->assertEquals('28 seconds ago', $this->relative('-28 seconds')); } public function testSecondsin() { - $this->assertEquals('in 33 seconds', $this->relative('+33 seconds')); + $this->assertEquals('in 33 seconds', $this->relative('+33 seconds')); } public function testMinutesAgo() { - $this->assertEquals('13 minutes ago', $this->relative('-13 minutes')); + $this->assertEquals('13 minutes ago', $this->relative('-13 minutes')); } public function testMinutesin() { - $this->assertEquals('in 40 minutes', $this->relative('+40 minutes')); + $this->assertEquals('in 40 minutes', $this->relative('+40 minutes')); } public function testHoursAgo() { - $this->assertEquals('22 hours ago', $this->relative('-22 hours')); + $this->assertEquals('22 hours ago', $this->relative('-22 hours')); } public function testHoursin() { - $this->assertEquals('in 3 hours', $this->relative('+3 hours')); + $this->assertEquals('in 3 hours', $this->relative('+3 hours')); } public function testDaysAgo() { - $this->assertEquals('6 days ago', $this->relative('-6 days')); + $this->assertEquals('6 days ago', $this->relative('-6 days')); } public function testDaysin() { - $this->assertEquals('in 3 days', $this->relative('+3 days')); + $this->assertEquals('in 3 days', $this->relative('+3 days')); } public function testWeeksAgo() { - $this->assertEquals('3 weeks ago', $this->relative('-3 weeks')); + $this->assertEquals('3 weeks ago', $this->relative('-3 weeks')); } public function testWeeksin() { - $this->assertEquals('in 2 weeks', $this->relative('+2 weeks')); + $this->assertEquals('in 2 weeks', $this->relative('+2 weeks')); } public function testMonthsAgo() { - $this->assertEquals('5 months ago', $this->relative('-5 months')); + $this->assertEquals('5 months ago', $this->relative('-5 months')); } public function testMonthsin() { @@ -103,12 +103,11 @@ public function testDayBeforeYesterdayNotSet() { public function testDayBeforeYesterdaySet() { $locale = json_decode(file_get_contents('src/Formatter/Locale/enGB.json'), true); $locale['offset_strings']['day_before_yesterday'] = 'custom day before yesterday string'; - $formatter = new \Transphporm\Formatter\Date($locale); + $formatter = new \Transphporm\Formatter\Date($locale); $this->assertEquals('custom day before yesterday string', $this->relative('-2 days', $formatter)); } - public function testDayAfterTomorrowNotSet() { $this->assertEquals('in 2 days', $this->relative('+2 days')); } @@ -116,8 +115,8 @@ public function testDayAfterTomorrowNotSet() { public function testDayAfterTomorrowSet() { $locale = json_decode(file_get_contents('src/Formatter/Locale/enGB.json'), true); $locale['offset_strings']['day_after_tomorrow'] = 'custom day after tomorrow string'; - $formatter = new \Transphporm\Formatter\Date($locale); + $formatter = new \Transphporm\Formatter\Date($locale); $this->assertEquals('custom day after tomorrow string', $this->relative('+2 days', $formatter)); } -} \ No newline at end of file +} diff --git a/tests/ErrorTest.php b/tests/ErrorTest.php new file mode 100644 index 0000000..243205e --- /dev/null +++ b/tests/ErrorTest.php @@ -0,0 +1,68 @@ +expectException("Exception"); + $this->expectExceptionMessage("No TSS rules parsed"); + $template = new \Transphporm\Builder("
", "NONEXISTANT_FILE"); + $template->output(); + } + + public function testFunctionException() { + $this->expectException("Transphporm\\Exception"); + $this->expectExceptionMessage("TSS Error: Problem carrying out function 'data' on Line 2 of tss"); + + $xml = '
'; + $tss = ' + div { content: data(getTest()); } + '; + $template = new \Transphporm\Builder($xml, $tss); + $template->output(); + } + + public function testPseudoException() { + $this->expectException("Transphporm\\Exception"); + $this->expectExceptionMessage("TSS Error: Problem carrying out pseudo 'nth-child' on Line 2 of tss"); + + $xml = '
'; + $tss = ' + div:nth-child(h) { content: "test"; } + '; + $template = new \Transphporm\Builder($xml, $tss); + $template->output(); + } + + public function testFormatterException() { + $this->expectException("Transphporm\\Exception"); + $this->expectExceptionMessage("TSS Error: Problem carrying out formatter 'test' on Line 2 of tss"); + + $xml = '
'; + $tss = ' + div { content: "test"; format: test; } + '; + $template = new \Transphporm\Builder($xml, $tss); + $template->output(); + } + + public function testPropertyException() { + $this->expectException("Transphporm\\Exception"); + $this->expectExceptionMessage("TSS Error: Problem carrying out property 'repeat' on Line 2 of tss"); + + $xml = '
'; + $tss = ' + div { repeat: "test"; } + '; + $template = new \Transphporm\Builder($xml, $tss); + $template->output(); + } + + public function testParseErrorFromFile() { + $this->expectException("Transphporm\\Exception"); + $this->expectExceptionMessage("TSS Error: Problem carrying out function 'data' on Line 3 of " . __DIR__ . DIRECTORY_SEPARATOR . "parseErrorTss.tss"); + + $xml = '
'; + $tss = __DIR__ . DIRECTORY_SEPARATOR . 'parseErrorTss.tss'; + $template = new \Transphporm\Builder($xml, $tss); + $template->output(); + } +} diff --git a/tests/GeneralFormatterTest.php b/tests/GeneralFormatterTest.php new file mode 100644 index 0000000..54dbf14 --- /dev/null +++ b/tests/GeneralFormatterTest.php @@ -0,0 +1,194 @@ +Test + ';; + $tss = 'div {content: "1.234567"; format: decimal 2;}'; + + $template = new \Transphporm\Builder($template, $tss); + + $this->assertEquals('
1.23
', $this->stripTabs($template->output()->body)); + + + } + + public function testFormatCurrency() { + $template = ' +
Test
+ '; + + $tss = 'div {content: "1.234567"; format: currency;}'; + + $template = new \Transphporm\Builder($template, $tss); + + $this->assertEquals('
£1.23
', $this->stripTabs($template->output()->body)); + + + } + + public function testFormatCurrencyAfter() { + $template = ' +
Test
+ '; + + $tss = 'div {content: "1.234567"; format: currency;}'; + + $template = new \Transphporm\Builder($template, $tss); + $locale = json_decode(file_get_contents('src/Formatter/Locale/enGB.json'), true); + $locale['currency_position'] = 'after'; + $template->loadModule(new \Transphporm\Module\Format($locale)); + + $this->assertEquals('
1.23£
', $this->stripTabs($template->output()->body)); + } + + /* + * String Formatter Tests + */ + + public function testFormatStringUpper() { + $template = ' +
Test
+ '; + + $tss = 'div {content: "test"; format: uppercase}'; + + $template = new \Transphporm\Builder($template, $tss); + $template->loadModule(new \Transphporm\Module\Format); + $this->assertEquals('
TEST
', $this->stripTabs($template->output()->body)); + + + } + + + public function testFormatStringLower() { + $template = ' +
Test
+ '; + + $tss = 'div {content: "test"; format: lowercase}'; + + $template = new \Transphporm\Builder($template, $tss); + $template->loadModule(new \Transphporm\Module\Format); + $this->assertEquals('
test
', $this->stripTabs($template->output()->body)); + + + } + + public function testCustomFormatter() { + $template = ' +
Test
+ '; + + $tss = 'div {content: "test"; format: reverse}'; + + $template = new \Transphporm\Builder($template, $tss); + require_once 'tests/ReverseFormatter.php'; + $template->loadModule(new ReverseFormatterModule); + + $this->assertEquals('
tset
', $this->stripTabs($template->output()->body)); + + } + + public function testFormatStringTitle() { + $template = ' +
test
+ '; + + $tss = 'div {content: "a test title"; format: titlecase}'; + + $template = new \Transphporm\Builder($template, $tss); + + $this->assertEquals('
A Test Title
', $this->stripTabs($template->output()->body)); + + + } + + public function testHTMLFormat() { + $template = ' +
test
+ '; + + $tss = 'div {content: "foobar"; format: html}'; + + $template = new \Transphporm\Builder($template, $tss); + + $this->assertEquals($this->stripTabs('
foobar
'), $this->stripTabs($template->output()->body)); + } + + /* + * Date Formatter Tests + */ + + public function testFormatDate() { + + $template = ' +
test
+ '; + + $tss = 'div {content: "2015-10-05"; format: date}'; + + $template = new \Transphporm\Builder($template, $tss); + + + $this->assertEquals($this->stripTabs('
05/10/2015
'), $this->stripTabs($template->output()->body)); + } + + public function testFormatDateCustom() { + + $template = ' +
test
+ '; + + $tss = 'div {content: "2015-10-05"; format: date "jS M"}'; + + $template = new \Transphporm\Builder($template, $tss); + + + $this->assertEquals($this->stripTabs('
5th Oct
'), $this->stripTabs($template->output()->body)); + } + + /* + * Nl2br Formatter Tests + */ + + public function testNl2brBasic() { + $xml = " +
+ "; + + $tss = " + div { content: 'Test Line 1 \n Test Line 2'; format: nl2br; } + "; + + $transphporm = new Builder($xml, $tss); + + $this->assertEquals($this->stripTabs('
Test Line 1
Test Line 2
'), $this->stripTabs($transphporm->output()->body)); + } + + public function testNl2brBasicFromData() { + $xml = " +
+ "; + + $tss = " + div { content: data(); format: nl2br; } + "; + + $data = "Test Line 1 \n Test Line 2"; + + $transphporm = new Builder($xml, $tss); + + $this->assertEquals($this->stripTabs('
Test Line 1
Test Line 2
'), $this->stripTabs($transphporm->output($data)->body)); + } +} diff --git a/tests/ImportTest.php b/tests/ImportTest.php new file mode 100644 index 0000000..6e78712 --- /dev/null +++ b/tests/ImportTest.php @@ -0,0 +1,124 @@ +Test + '; + + $file = __DIR__ . DIRECTORY_SEPARATOR . 'import.tss'; + $file = str_replace('\\', '/', $file); + $tss = " + @import '$file'; + "; + + $template = new \Transphporm\Builder($template, $tss); + + $this->assertEquals('
foo
', $template->output()->body); + } + + public function testImportDynamic() { + $template = ' +
Test
+ '; + + + $tss = " + @import data(filename); + "; + + $data = [ + 'filename' => __DIR__ . DIRECTORY_SEPARATOR . 'import.tss' + ]; + + $template = new \Transphporm\Builder($template, $tss); + + $this->assertEquals('
foo
', $template->output($data)->body); + } + + public function testImportMiddleOfFile() { + $template = ' + foo +
Test
+

foo

+ '; + + $file = __DIR__ . DIRECTORY_SEPARATOR . 'import.tss'; + $file = str_replace('\\', '/', $file); + $tss = " + span {content: 'test1';} + @import '$file'; + h1 {content: 'h1';} + "; + + $template = new \Transphporm\Builder($template, $tss); + + $this->assertEquals($this->stripTabs('test1
foo

h1

'), $this->stripTabs($template->output()->body)); + } + + public function testMultiImport() { + $template = ' + test +
Test
+

foo

+ '; + + $file = __DIR__ . DIRECTORY_SEPARATOR . 'import.tss'; + $file2 = __DIR__ . DIRECTORY_SEPARATOR . 'import2.tss'; + $file = str_replace('\\', '/', $file); + $file2 = str_replace('\\', '/', $file2); + $tss = " + span {content: 'test1';} + @import '$file'; + h1 {content: 'h1';} + @import '$file2'; + "; + + $template = new \Transphporm\Builder($template, $tss); + + $this->assertEquals($this->stripTabs('bar
foo

h1

'), $this->stripTabs($template->output()->body)); + + } + + + public function testMultiImportTogether() { + $template = ' + test +
Test
+

foo

+ '; + + $file = __DIR__ . DIRECTORY_SEPARATOR . 'import.tss'; + $file2 = __DIR__ . DIRECTORY_SEPARATOR . 'import2.tss'; + $file = str_replace('\\', '/', $file); + $file2 = str_replace('\\', '/', $file2); + $tss = " + span {content: 'test1';} + @import '$file'; + @import '$file2'; + h1 {content: 'h1';} + + "; + + $template = new \Transphporm\Builder($template, $tss); + + $this->assertEquals($this->stripTabs('bar
foo

h1

'), $this->stripTabs($template->output()->body)); + + } + + public function testImportInImportedFile() { + $template = new \Transphporm\Builder("tests/test.xml", "tests/other/otherImport.tss"); + + $this->assertEquals('test', $this->stripTabs($template->output()->body)); + } + + public function testBaseDirChangeWithImport() { + $template = new \Transphporm\Builder("tests/test.xml", "tests/other/importTemplateFromImport.tss"); + + $this->assertEquals('

foo

', $this->stripTabs($template->output()->body)); + } +} diff --git a/tests/PseudoTest.php b/tests/PseudoTest.php new file mode 100644 index 0000000..528cd0a --- /dev/null +++ b/tests/PseudoTest.php @@ -0,0 +1,498 @@ +list = []; + + $one = new stdclass; + $one->name = 'One'; + $one->id = '1'; + $data->list[] = $one; + + $two = new stdclass; + $two->name = 'Two'; + $two->id = '2'; + $data->list[] = $two; + + $three = new stdclass; + $three->name = 'Three'; + $three->id = '3'; + $data->list[] = $three; + + + $template = ' + + '; + + $css = 'ul li {repeat: data(list);} + ul li h2 {content: iteration(id)} + ul li span {content: iteration(name); } + ul li span:iteration[id="2"] {display: none;} + '; + + + $template = new \Transphporm\Builder($template, $css); + + + $this->assertEquals($this->stripTabs(''), $this->stripTabs($template->output($data)->body)); + + } + + + public function testIterationPseudoNotEquals() { + $data = new stdclass; + $data->list = []; + + $one = new stdclass; + $one->name = 'One'; + $one->id = '1'; + $data->list[] = $one; + + $two = new stdclass; + $two->name = 'Two'; + $two->id = '2'; + $data->list[] = $two; + + $three = new stdclass; + $three->name = 'Three'; + $three->id = '3'; + $data->list[] = $three; + + + $template = ' + + '; + + $css = 'ul li {repeat: data(list);} + ul li h2 {content: iteration(id)} + ul li span {content: iteration(name); } + ul li span:iteration[id!="2"] {display: none;} + '; + + + $template = new \Transphporm\Builder($template, $css); + + + $this->assertEquals($this->stripTabs(''), $this->stripTabs($template->output($data)->body)); + + } + + public function testMultiPseudo() { + $data = new stdclass; + $data->list = []; + + $one = new stdclass; + $one->name = 'One'; + $one->id = '1'; + $data->list[] = $one; + + $two = new stdclass; + $two->name = 'Two'; + $two->id = '2'; + $data->list[] = $two; + + $three = new stdclass; + $three->name = 'Three'; + $three->id = '3'; + $data->list[] = $three; + + + $template = ' + + '; + + $css = 'ul li {repeat: data(list);} + ul li h2 {content: iteration(id)} + ul li span {content: iteration(name); } + ul li span:iteration[id="2"]:before {content: "BEFORE";} + '; + + + $template = new \Transphporm\Builder($template, $css); + + + $this->assertEquals($this->stripTabs(''), $this->stripTabs($template->output($data)->body)); + + } + + public function testFunctionCallAsConditonal() { + + $xml = '
'; + + $obj = new Foo(); + + $tss = 'div:data[returnTrue()=true] {content: "test" }'; + + $template = new \Transphporm\Builder($xml, $tss); + + $this->assertEquals($this->stripTabs($template->output($obj)->body), $this->stripTabs('
test
')); + } + + public function testFunctionCallAsConditonal2() { + + $xml = '
'; + + $obj = new Foo(); + + $tss = 'div:data[returnFalse()=false] {content: "test" }'; + + $template = new \Transphporm\Builder($xml, $tss); + + $this->assertEquals($this->stripTabs($template->output($obj)->body), $this->stripTabs('
test
')); + } + + public function testFunctionCallAsConditonal3() { + + $xml = '
'; + + $obj = new Foo(); + + $tss = 'div:data[returnOne()=1] {content: "test" }'; + + $template = new \Transphporm\Builder($xml, $tss); + + $this->assertEquals($this->stripTabs($template->output($obj)->body), $this->stripTabs('
test
')); + } + + public function testFunctionCallAsConditonal4() { + + $xml = '
'; + + $obj = new Foo(); + + $tss = 'div:[data(returnOne())=1] {content: "test" }'; + + $template = new \Transphporm\Builder($xml, $tss); + + $this->assertEquals($this->stripTabs($template->output($obj)->body), $this->stripTabs('
test
')); + } + + public function testFilterDataArray() { + $data = [ + 'anArray' => [ + 'one' => 'foo', + 'two' => 'bar' + ] + ]; + + $xml = ' +
+ +
+
+ +
+
+
+ '; + + $tss = 'div:data[anArray[attr("class")]] {content: "set"; }'; + + $template = new \Transphporm\Builder($xml, $tss); + + $this->assertEquals($this->stripTabs(' +
set
+
set
+
+ '), $this->stripTabs($template->output($data)->body)); + + } + + public function testFilterDataArrayWithEquals() { + $data = [ + 'anArray' => [ + 'one' => 'foo', + 'two' => 'bar' + ] + ]; + + $xml = ' +
+ +
+
+ +
+
+
+ '; + + $tss = 'div:data[anArray[attr(class)]="foo"] {content: data(anArray[attr(class)]) }'; + + $template = new \Transphporm\Builder($xml, $tss); + + $this->assertEquals($this->stripTabs($template->output($data)->body), $this->stripTabs(' +
foo
+
+
+
+ ')); + + } + + public function testEmptyFunctionCallAsConditonal() { + + $xml = '
'; + + $data = "test"; + + $tss = 'div:[data()="test"] {content: "test" }'; + + $template = new \Transphporm\Builder($xml, $tss); + + $this->assertEquals($this->stripTabs($template->output($data)->body), $this->stripTabs('
test
')); + } + + public function testAttrPseudoSelectorAlternateSyntax() { + $xml = '
'; + + $tss = 'div:[data()=true] {content: "test" }'; + + $template = new \Transphporm\Builder($xml, $tss); + + $this->assertEquals($this->stripTabs($template->output(true)->body), $this->stripTabs('
test
')); + } + + /* + * Nth-child Pseudo tests + */ + + public function testNthChild() { + $template = ' + + '; + + $tss = 'ul li:nth-child(2) {content: "REPLACED"}'; + + $template = new \Transphporm\Builder($template, $tss); + + $this->assertEquals($this->stripTabs(''), $this->stripTabs($template->output()->body)); + + + } + + public function testNthChild2() { + $template = ' + + '; + + $tss = 'ul li:nth-child(3) {content: "REPLACED"}'; + + $template = new \Transphporm\Builder($template, $tss); + + $this->assertEquals($this->stripTabs(''), $this->stripTabs($template->output()->body)); + + } + + public function testNthChild3() { + $template = ' + + '; + + $tss = 'ul li:nth-child(1) {content: "REPLACED"}'; + + $template = new \Transphporm\Builder($template, $tss); + + $this->assertEquals($this->stripTabs(''), $this->stripTabs($template->output()->body)); + + } + + public function testNthChild4() { + $xml = ' +

Test

+

Test

+

Test

+ '; + + $tss = '.name:nth-child(2) { display: none; } + + .name:nth-child(3) { display: none; }'; + + $template = new \Transphporm\Builder($xml, $tss); + + $this->assertEquals($template->output()->body, $this->stripTabs('

Test

')); + } + + public function testNthChildOdd() { + $template = ' + + + '; + + $tss = 'ul li:nth-child(odd) {content: "REPLACED"}'; + + $template = new \Transphporm\Builder($template, $tss); + + $this->assertEquals($this->stripTabs(''), $this->stripTabs($template->output()->body)); + + } + + public function testNthChildEven() { + $template = ' + + '; + + $tss = 'ul li:nth-child(even) {content: "REPLACED"}'; + + $template = new \Transphporm\Builder($template, $tss); + + $this->assertEquals($this->stripTabs(''), $this->stripTabs($template->output()->body)); + + } + + /* + * Not Pseudo Tests + */ + + public function testNot() { + $xml = ' +
+ +
+
+ +
+
+
+ '; + + $tss = 'div:not(.two) {content: "foo"; }'; + + + $template = new \Transphporm\Builder($xml, $tss); + + $this->assertEquals($this->stripTabs($template->output()->body), $this->stripTabs(' +
foo
+
+
foo
+ ')); + + } + + public function testNotWithMoreDepth() { + $xml = ' +
+

+
+
+

+
+
+

+
+ '; + + $tss = 'p:not("div.two p") {content: "foo"; }'; + + + $template = new \Transphporm\Builder($xml, $tss); + + $this->assertEquals($this->stripTabs($template->output()->body), $this->stripTabs(' +

foo

+

+

foo

+ ')); + + } +} diff --git a/tests/TransphpormTest.php b/tests/TransphpormTest.php index 2429ee0..7c87b87 100644 --- a/tests/TransphpormTest.php +++ b/tests/TransphpormTest.php @@ -402,309 +402,6 @@ public function testDisplayNone() { '), $this->stripTabs($template->output()->body)); } - public function testBefore() { - $template = ' -
Test
- '; - - $tss = 'div:before {content: "BEFORE";}'; - - $template = new \Transphporm\Builder($template, $tss); - - $this->assertEquals($this->stripTabs('
BEFORETest
'), $this->stripTabs($template->output()->body)); - } - - public function testAfter() { - $template = ' -
Test
- '; - - $tss = 'div:after {content: "AFTER";}'; - - $template = new \Transphporm\Builder($template, $tss); - - $this->assertEquals($this->stripTabs('
TestAFTER
'), $this->stripTabs($template->output()->body)); - } - - - public function testIterationPseudo() { - $data = new stdclass; - $data->list = []; - - $one = new stdclass; - $one->name = 'One'; - $one->id = '1'; - $data->list[] = $one; - - $two = new stdclass; - $two->name = 'Two'; - $two->id = '2'; - $data->list[] = $two; - - $three = new stdclass; - $three->name = 'Three'; - $three->id = '3'; - $data->list[] = $three; - - - $template = ' - - '; - - $css = 'ul li {repeat: data(list);} - ul li h2 {content: iteration(id)} - ul li span {content: iteration(name); } - ul li span:iteration[id="2"] {display: none;} - '; - - - $template = new \Transphporm\Builder($template, $css); - - - $this->assertEquals($this->stripTabs(''), $this->stripTabs($template->output($data)->body)); - - } - - - public function testIterationPseudoNotEquals() { - $data = new stdclass; - $data->list = []; - - $one = new stdclass; - $one->name = 'One'; - $one->id = '1'; - $data->list[] = $one; - - $two = new stdclass; - $two->name = 'Two'; - $two->id = '2'; - $data->list[] = $two; - - $three = new stdclass; - $three->name = 'Three'; - $three->id = '3'; - $data->list[] = $three; - - - $template = ' - - '; - - $css = 'ul li {repeat: data(list);} - ul li h2 {content: iteration(id)} - ul li span {content: iteration(name); } - ul li span:iteration[id!="2"] {display: none;} - '; - - - $template = new \Transphporm\Builder($template, $css); - - - $this->assertEquals($this->stripTabs(''), $this->stripTabs($template->output($data)->body)); - - } - public function testMultiPseudo() { - $data = new stdclass; - $data->list = []; - - $one = new stdclass; - $one->name = 'One'; - $one->id = '1'; - $data->list[] = $one; - - $two = new stdclass; - $two->name = 'Two'; - $two->id = '2'; - $data->list[] = $two; - - $three = new stdclass; - $three->name = 'Three'; - $three->id = '3'; - $data->list[] = $three; - - - $template = ' - - '; - - $css = 'ul li {repeat: data(list);} - ul li h2 {content: iteration(id)} - ul li span {content: iteration(name); } - ul li span:iteration[id="2"]:before {content: "BEFORE";} - '; - - - $template = new \Transphporm\Builder($template, $css); - - - $this->assertEquals($this->stripTabs(''), $this->stripTabs($template->output($data)->body)); - - } - - public function testNthChild() { - $template = ' - - '; - - $tss = 'ul li:nth-child(2) {content: "REPLACED"}'; - - $template = new \Transphporm\Builder($template, $tss); - - $this->assertEquals($this->stripTabs(''), $this->stripTabs($template->output()->body)); - - - } - - public function testNthChild2() { - $template = ' - - '; - - $tss = 'ul li:nth-child(3) {content: "REPLACED"}'; - - $template = new \Transphporm\Builder($template, $tss); - - $this->assertEquals($this->stripTabs(''), $this->stripTabs($template->output()->body)); - - } - - public function testNthChild3() { - $template = ' - - '; - - $tss = 'ul li:nth-child(1) {content: "REPLACED"}'; - - $template = new \Transphporm\Builder($template, $tss); - - $this->assertEquals($this->stripTabs(''), $this->stripTabs($template->output()->body)); - - } - - - public function testNthChildOdd() { - $template = ' - - - '; - - $tss = 'ul li:nth-child(odd) {content: "REPLACED"}'; - - $template = new \Transphporm\Builder($template, $tss); - - $this->assertEquals($this->stripTabs(''), $this->stripTabs($template->output()->body)); - - } - - public function testNthChildEven() { - $template = ' - - '; - - $tss = 'ul li:nth-child(even) {content: "REPLACED"}'; - - $template = new \Transphporm\Builder($template, $tss); - - $this->assertEquals($this->stripTabs(''), $this->stripTabs($template->output()->body)); - - } - public function testReadAttribute() { $template = '
Test
@@ -781,123 +478,6 @@ public function testCommentBeforeRule() { $this->assertEquals('
bar
', $template->output()->body); } - public function testImport() { - $template = ' -
Test
- '; - - $file = __DIR__ . DIRECTORY_SEPARATOR . 'import.tss'; - $file = str_replace('\\', '/', $file); - $tss = " - @import '$file'; - "; - - $template = new \Transphporm\Builder($template, $tss); - - $this->assertEquals('
foo
', $template->output()->body); - } - - public function testImportDynamic() { - $template = ' -
Test
- '; - - - $tss = " - @import data(filename); - "; - - $data = [ - 'filename' => __DIR__ . DIRECTORY_SEPARATOR . 'import.tss' - ]; - - $template = new \Transphporm\Builder($template, $tss); - - $this->assertEquals('
foo
', $template->output($data)->body); - } - - public function testImportMiddleOfFile() { - $template = ' - foo -
Test
-

foo

- '; - - $file = __DIR__ . DIRECTORY_SEPARATOR . 'import.tss'; - $file = str_replace('\\', '/', $file); - $tss = " - span {content: 'test1';} - @import '$file'; - h1 {content: 'h1';} - "; - - $template = new \Transphporm\Builder($template, $tss); - - $this->assertEquals($this->stripTabs('test1
foo

h1

'), $this->stripTabs($template->output()->body)); - } - - public function testMultiImport() { - $template = ' - test -
Test
-

foo

- '; - - $file = __DIR__ . DIRECTORY_SEPARATOR . 'import.tss'; - $file2 = __DIR__ . DIRECTORY_SEPARATOR . 'import2.tss'; - $file = str_replace('\\', '/', $file); - $file2 = str_replace('\\', '/', $file2); - $tss = " - span {content: 'test1';} - @import '$file'; - h1 {content: 'h1';} - @import '$file2'; - "; - - $template = new \Transphporm\Builder($template, $tss); - - $this->assertEquals($this->stripTabs('bar
foo

h1

'), $this->stripTabs($template->output()->body)); - - } - - - public function testMultiImportTogether() { - $template = ' - test -
Test
-

foo

- '; - - $file = __DIR__ . DIRECTORY_SEPARATOR . 'import.tss'; - $file2 = __DIR__ . DIRECTORY_SEPARATOR . 'import2.tss'; - $file = str_replace('\\', '/', $file); - $file2 = str_replace('\\', '/', $file2); - $tss = " - span {content: 'test1';} - @import '$file'; - @import '$file2'; - h1 {content: 'h1';} - - "; - - $template = new \Transphporm\Builder($template, $tss); - - $this->assertEquals($this->stripTabs('bar
foo

h1

'), $this->stripTabs($template->output()->body)); - - } - - public function testImportInImportedFile() { - $template = new \Transphporm\Builder("tests/test.xml", "tests/other/otherImport.tss"); - - $this->assertEquals('test', $this->stripTabs($template->output()->body)); - } - - public function testBaseDirChangeWithImport() { - $template = new \Transphporm\Builder("tests/test.xml", "tests/other/templateFromImport.tss"); - - $this->assertEquals('

foo

', $this->stripTabs($template->output()->body)); - } - public function testContentTemplate() { $template = '
Test
@@ -941,151 +521,48 @@ public function testNestedFunction() { $template = '
-
'; - - $data = ['one' => 'VALUE-OF-ONE', 'two' => 'VALUE-OF-TWO']; - - $tss = 'input:attr(value) {content: data(attr(name));}'; - - $template = new \Transphporm\Builder($template, $tss); - - $this->assertEquals($this->stripTabs('
- - -
'), $this->stripTabs($template->output($data)->body)); - } - - - public function testBind() { - //Binds a specific pice of data (which may be an array to an element) - //All calls to data() for child elements will use this as the root data object - - $template = '
- - -
'; - - //This time, the form data isn't the root data() object - - $data = ['formdata' => ['one' => 'VALUE-OF-ONE', 'two' => 'VALUE-OF-TWO']]; - - //First bind the form data to the form element, then populate the inputs using it. - //For inputs, as they're children of the form element, will use this data - $tss = ' - form {bind: data(formdata);} - input:attr(value) {content: data(attr(name));}'; - - $template = new \Transphporm\Builder($template, $tss); - - $this->assertEquals($this->stripTabs('
- - -
'), $this->stripTabs($template->output($data)->body)); - - } - - public function testFormatNumber() { - $template = ' -
Test
- ';; - $tss = 'div {content: "1.234567"; format: decimal 2;}'; - - $template = new \Transphporm\Builder($template, $tss); - - $this->assertEquals('
1.23
', $this->stripTabs($template->output()->body)); - - - } - - public function testFormatCurrency() { - $template = ' -
Test
- '; - - $tss = 'div {content: "1.234567"; format: currency;}'; - - $template = new \Transphporm\Builder($template, $tss); - - $this->assertEquals('
£1.23
', $this->stripTabs($template->output()->body)); - - - } - - - public function testFormatCurrencyAfter() { - $template = ' -
Test
- '; - - $tss = 'div {content: "1.234567"; format: currency;}'; - - $template = new \Transphporm\Builder($template, $tss); - $locale = json_decode(file_get_contents('src/Formatter/Locale/enGB.json'), true); - $locale['currency_position'] = 'after'; - $template->loadModule(new \Transphporm\Module\Format($locale)); - - $this->assertEquals('
1.23£
', $this->stripTabs($template->output()->body)); - } - - public function testFormatStringUpper() { - $template = ' -
Test
- '; - - $tss = 'div {content: "test"; format: uppercase}'; - - $template = new \Transphporm\Builder($template, $tss); - $template->loadModule(new \Transphporm\Module\Format); - $this->assertEquals('
TEST
', $this->stripTabs($template->output()->body)); - - - } - - - public function testFormatStringLower() { - $template = ' -
Test
- '; + '; - $tss = 'div {content: "test"; format: lowercase}'; + $data = ['one' => 'VALUE-OF-ONE', 'two' => 'VALUE-OF-TWO']; - $template = new \Transphporm\Builder($template, $tss); - $template->loadModule(new \Transphporm\Module\Format); - $this->assertEquals('
test
', $this->stripTabs($template->output()->body)); + $tss = 'input:attr(value) {content: data(attr(name));}'; + $template = new \Transphporm\Builder($template, $tss); + $this->assertEquals($this->stripTabs('
+ + +
'), $this->stripTabs($template->output($data)->body)); } - public function testCustomFormatter() { - $template = ' -
Test
- '; - - $tss = 'div {content: "test"; format: reverse}'; - - $template = new \Transphporm\Builder($template, $tss); - require_once 'tests/ReverseFormatter.php'; - $template->loadModule(new ReverseFormatterModule); + public function testBind() { + //Binds a specific pice of data (which may be an array to an element) + //All calls to data() for child elements will use this as the root data object - $this->assertEquals('
tset
', $this->stripTabs($template->output()->body)); + $template = '
+ + +
'; - } + //This time, the form data isn't the root data() object - public function testFormatStringTitle() { - $template = ' -
test
- '; + $data = ['formdata' => ['one' => 'VALUE-OF-ONE', 'two' => 'VALUE-OF-TWO']]; - $tss = 'div {content: "a test title"; format: titlecase}'; + //First bind the form data to the form element, then populate the inputs using it. + //For inputs, as they're children of the form element, will use this data + $tss = ' + form {bind: data(formdata);} + input:attr(value) {content: data(attr(name));}'; $template = new \Transphporm\Builder($template, $tss); - $this->assertEquals('
A Test Title
', $this->stripTabs($template->output()->body)); - + $this->assertEquals($this->stripTabs('
+ + +
'), $this->stripTabs($template->output($data)->body)); } - public function testSemicolonInString() { $template = ' @@ -1198,47 +675,6 @@ public function testTemplateWithXmlns() { $this->assertEquals($this->stripTabs('yy'), $this->stripTabs($template->output()->body)); } - public function testFormatDate() { - - $template = ' -
test
- '; - - $tss = 'div {content: "2015-10-05"; format: date}'; - - $template = new \Transphporm\Builder($template, $tss); - - - $this->assertEquals($this->stripTabs('
05/10/2015
'), $this->stripTabs($template->output()->body)); - } - - public function testFormatDateCustom() { - - $template = ' -
test
- '; - - $tss = 'div {content: "2015-10-05"; format: date "jS M"}'; - - $template = new \Transphporm\Builder($template, $tss); - - - $this->assertEquals($this->stripTabs('
5th Oct
'), $this->stripTabs($template->output()->body)); - } - - public function testHTMLFormat() { - $template = ' -
test
- '; - - $tss = 'div {content: "foobar"; format: html}'; - - $template = new \Transphporm\Builder($template, $tss); - - $this->assertEquals($this->stripTabs('
foobar
'), $this->stripTabs($template->output()->body)); - } - - public function testIncludeTemplatePartial() { $template = '
test
@@ -1475,41 +911,6 @@ public function testSelectAttributeFromData2() { ')); } - public function testOverrideAfter() { - $xml = '
- Test -
'; - - - $includeFile = __DIR__ . DIRECTORY_SEPARATOR . 'include.xml'; - $includeFile = str_replace('\\', '/', $includeFile); - - $tss = "div:after {content: 'foo' } -div:after {content: 'bar' } - "; - $template = new \Transphporm\Builder($xml, $tss); - - $this->assertEquals('
Testbar
', $this->stripTabs($template->output()->body)); - - } - - - public function testOverrideBefore() { - $xml = '
- Test -
'; - - - $includeFile = __DIR__ . DIRECTORY_SEPARATOR . 'include.xml'; - - $tss = "div:before {content: 'foo' } - div:before {content: 'bar';} - "; - $template = new \Transphporm\Builder($xml, $tss); - - $this->assertEquals('
barTest
', $this->stripTabs($template->output()->body)); - - } public function testAttrDisplayNone() { $xml = '
test @@ -1569,70 +970,6 @@ public function testAttrRead() { ')); } - public function testFilterDataArray() { - $data = [ - 'anArray' => [ - 'one' => 'foo', - 'two' => 'bar' - ] - ]; - - $xml = ' -
- -
-
- -
-
-
- '; - - $tss = 'div:data[anArray[attr("class")]] {content: "set"; }'; - - $template = new \Transphporm\Builder($xml, $tss); - - $this->assertEquals($this->stripTabs(' -
set
-
set
-
- '), $this->stripTabs($template->output($data)->body)); - - } - - public function testFilterDataArrayWithEquals() { - $data = [ - 'anArray' => [ - 'one' => 'foo', - 'two' => 'bar' - ] - ]; - - $xml = ' -
- -
-
- -
-
-
- '; - - $tss = 'div:data[anArray[attr(class)]="foo"] {content: data(anArray[attr(class)]) }'; - - $template = new \Transphporm\Builder($xml, $tss); - - $this->assertEquals($this->stripTabs($template->output($data)->body), $this->stripTabs(' -
foo
-
-
-
- ')); - - } - - public function testMultiRule() { $xml = '
@@ -1658,57 +995,6 @@ public function testMultiRule() { } - public function testNot() { - $xml = ' -
- -
-
- -
-
-
- '; - - $tss = 'div:not(.two) {content: "foo"; }'; - - - $template = new \Transphporm\Builder($xml, $tss); - - $this->assertEquals($this->stripTabs($template->output()->body), $this->stripTabs(' -
foo
-
-
foo
- ')); - - } - - public function testNotWithMoreDepth() { - $xml = ' -
-

-
-
-

-
-
-

-
- '; - - $tss = 'p:not("div.two p") {content: "foo"; }'; - - - $template = new \Transphporm\Builder($xml, $tss); - - $this->assertEquals($this->stripTabs($template->output()->body), $this->stripTabs(' -

foo

-

-

foo

- ')); - - } - public function testKeyChild() { $xml = '