|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright 2011 Johannes M. Schmitt <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace TwigJs\Compiler; |
|
20
|
|
|
|
|
21
|
|
|
use TwigJs\JsCompiler; |
|
22
|
|
|
use TwigJs\TypeCompilerInterface; |
|
23
|
|
|
|
|
24
|
|
|
class SpacelessCompiler implements TypeCompilerInterface |
|
25
|
|
|
{ |
|
26
|
|
|
private $count = 0; |
|
27
|
|
|
|
|
28
|
|
|
public function getType() |
|
29
|
|
|
{ |
|
30
|
|
|
return 'Twig_Node_Spaceless'; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) |
|
34
|
|
|
{ |
|
35
|
|
|
if (!$node instanceof \Twig_Node_Spaceless) { |
|
36
|
|
|
throw new \RuntimeException( |
|
37
|
|
|
sprintf( |
|
38
|
|
|
'$node must be an instanceof of \Twig_Node_Spaceless, but got "%s".', |
|
39
|
|
|
get_class($node) |
|
40
|
|
|
) |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$count = $this->count++; |
|
45
|
|
|
$sbName = 'slSb'.($count > 0 ? $count : ''); |
|
46
|
|
|
|
|
47
|
|
|
$compiler |
|
48
|
|
|
->addDebugInfo($node) |
|
49
|
|
|
->write("var $sbName = sb;\n") |
|
50
|
|
|
->write("sb = new twig.StringBuffer;") |
|
51
|
|
|
->subcompile($node->getNode('body')) |
|
52
|
|
|
->write("$sbName.append(twig.spaceless(sb.toString()));\n") |
|
53
|
|
|
->write("sb = $sbName;\n") |
|
54
|
|
|
; |
|
55
|
|
|
$this->count = $count; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|