Permalink
Browse files

Add quoting to JstProcessor, add test for JstProcessor

  • Loading branch information...
1 parent 80cc387 commit eb4759ba7585cb9d16435aeae9968173e13d54ca @CHH committed Jan 19, 2013
Showing with 92 additions and 0 deletions.
  1. +7 −0 lib/Pipe/JstProcessor.php
  2. +85 −0 tests/Pipe/Test/JstProcessorTest.php
@@ -15,8 +15,15 @@ function render($context = null, $vars = array())
{
$namespace = static::$defaultNamespace;
$name = json_encode($context->logicalPath);
+
+ # Indent with four spaces
$value = preg_replace('/$(.)/m', '\\1 ', $this->getData());
+ if (@$this->options['quote']) {
+ # Escape quotes, and quote data
+ $value = sprintf('"%s"', str_replace('"', '\\"', $value));
+ }
+
return <<<JST
(function() {
$namespace || ($namespace = {});
@@ -0,0 +1,85 @@
+<?php
+
+namespace Pipe\Test;
+
+use Pipe\JstProcessor;
+use Pipe\Context;
+use Pipe\Environment;
+
+class JstProcessorTest extends \PHPUnit_Framework_TestCase
+{
+ protected $env;
+ protected $ctx;
+
+ function setup()
+ {
+ $this->env = new Environment;
+ $this->ctx = new Context($this->env);
+ }
+
+ function test()
+ {
+ $this->ctx->logicalPath = 'foo';
+
+ $jst = new JstProcessor(function() {
+ return <<<JS
+function() { return "foo"; }
+JS;
+ });
+
+ $this->assertEquals(<<<EXPECTED
+(function() {
+ this.JST || (this.JST = {});
+
+ this.JST["foo"] = function() { return "foo"; };
+}).call(this);
+EXPECTED
+ , $jst->render($this->ctx));
+ }
+
+ function testCustomNamespace()
+ {
+ $this->ctx->logicalPath = 'foo';
+
+ $defaultNamespace = JstProcessor::$defaultNamespace;
+ JstProcessor::$defaultNamespace = "this.Pipe";
+
+ $jst = new JstProcessor(function() {
+ return <<<JS
+function() { return "foo"; }
+JS;
+ });
+
+ $this->assertEquals(<<<EXPECTED
+(function() {
+ this.Pipe || (this.Pipe = {});
+
+ this.Pipe["foo"] = function() { return "foo"; };
+}).call(this);
+EXPECTED
+ , $jst->render($this->ctx));
+
+ JstProcessor::$defaultNamespace = $defaultNamespace;
+ }
+
+ function testQuoting()
+ {
+ $this->ctx->logicalPath = 'foo';
+
+ $jst = new JstProcessor(function() {
+ return <<<JS
+{{#foo}}<span>foo</span>{{/foo}} "foo"
+JS;
+ }, array('quote' => true));
+
+ $this->assertEquals(<<<EXPECTED
+(function() {
+ this.JST || (this.JST = {});
+
+ this.JST["foo"] = "{{#foo}}<span>foo</span>{{/foo}} \\"foo\\"";
+}).call(this);
+EXPECTED
+ , $jst->render($this->ctx));
+ }
+}
+

0 comments on commit eb4759b

Please sign in to comment.