summaryrefslogtreecommitdiff
path: root/lib/phptal/PHPTAL/Php/Attribute/TAL/Attributes.php
blob: 158d079d18f2ba4733ddb24bd66054b30d5fa2bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/**
 * PHPTAL templating engine
 *
 * PHP Version 5
 *
 * @category HTML
 * @package  PHPTAL
 * @author   Laurent Bedubourg <lbedubourg@motion-twin.com>
 * @author   Kornel Lesiński <kornel@aardvarkmedia.co.uk>
 * @license  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
 * @version  SVN: $Id$
 * @link     http://phptal.org/
 */


/**
 * TAL Specifications 1.4
 *
 *       argument             ::= attribute_statement [';' attribute_statement]*
 *       attribute_statement  ::= attribute_name expression
 *       attribute_name       ::= [namespace ':'] Name
 *       namespace            ::= Name
 *
 * examples:
 *
 *      <a href="/sample/link.html"
 *         tal:attributes="href here/sub/absolute_url">
 *      <textarea rows="80" cols="20"
 *         tal:attributes="rows request/rows;cols request/cols">
 *
 * IN PHPTAL: attributes will not work on structured replace.
 *
 * @package PHPTAL
 * @subpackage Php.attribute.tal
 * @author Laurent Bedubourg <lbedubourg@motion-twin.com>
 */
class PHPTAL_Php_Attribute_TAL_Attributes
extends PHPTAL_Php_Attribute
implements PHPTAL_Php_TalesChainReader
{
    /** before creates several variables that need to be freed in after */
    private $vars_to_recycle = array();

    /**
     * value for default keyword
     */
    private $_default_escaped;

    public function before(PHPTAL_Php_CodeWriter $codewriter)
    {
        // split attributes using ; delimiter
        $attrs = $codewriter->splitExpression($this->expression);
        foreach ($attrs as $exp) {
            list($qname, $expression) = $this->parseSetExpression($exp);
            if ($expression) {
                $this->prepareAttribute($codewriter, $qname, $expression);
            }
        }
    }

    private function prepareAttribute(PHPTAL_Php_CodeWriter $codewriter, $qname, $expression)
    {
        $tales_code = $this->extractEchoType($expression);
        $code = $codewriter->evaluateExpression($tales_code);

        // XHTML boolean attribute does not appear when empty or false
        if (PHPTAL_Dom_Defs::getInstance()->isBooleanAttribute($qname)) {

            // I don't want to mix code for boolean with chained executor
            // so compile it again to simple expression
            if (is_array($code)) {
                $code = PHPTAL_Php_TalesInternal::compileToPHPExpression($tales_code);
            }
            return $this->prepareBooleanAttribute($codewriter, $qname, $code);
        }

        // if $code is an array then the attribute value is decided by a
        // tales chained expression
        if (is_array($code)) {
            return $this->prepareChainedAttribute($codewriter, $qname, $code);
        }

        // i18n needs to read replaced value of the attribute, which is not possible if attribute is completely replaced with conditional code
        if ($this->phpelement->hasAttributeNS('http://xml.zope.org/namespaces/i18n', 'attributes')) {
            $this->prepareAttributeUnconditional($codewriter, $qname, $code);
        } else {
            $this->prepareAttributeConditional($codewriter, $qname, $code);
        }
    }

    /**
     * attribute will be output regardless of its evaluated value. NULL behaves just like "".
     */
    private function prepareAttributeUnconditional(PHPTAL_Php_CodeWriter $codewriter, $qname, $code)
    {
        // regular attribute which value is the evaluation of $code
        $attkey = $this->getVarName($qname, $codewriter);
        if ($this->_echoType == PHPTAL_Php_Attribute::ECHO_STRUCTURE) {
            $value = $codewriter->stringifyCode($code);
        } else {
            $value = $codewriter->escapeCode($code);
        }
        $codewriter->doSetVar($attkey, $value);
        $this->phpelement->getOrCreateAttributeNode($qname)->overwriteValueWithVariable($attkey);
    }

    /**
     * If evaluated value of attribute is NULL, it will not be output at all.
     */
    private function prepareAttributeConditional(PHPTAL_Php_CodeWriter $codewriter, $qname, $code)
    {
        // regular attribute which value is the evaluation of $code
        $attkey = $this->getVarName($qname, $codewriter);

        $codewriter->doIf("null !== ($attkey = ($code))");

        if ($this->_echoType !== PHPTAL_Php_Attribute::ECHO_STRUCTURE)
            $codewriter->doSetVar($attkey, $codewriter->str(" $qname=\"").".".$codewriter->escapeCode($attkey).".'\"'");
        else
            $codewriter->doSetVar($attkey, $codewriter->str(" $qname=\"").".".$codewriter->stringifyCode($attkey).".'\"'");

        $codewriter->doElse();
        $codewriter->doSetVar($attkey, "''");
        $codewriter->doEnd('if');

        $this->phpelement->getOrCreateAttributeNode($qname)->overwriteFullWithVariable($attkey);
    }

    private function prepareChainedAttribute(PHPTAL_Php_CodeWriter $codewriter, $qname, $chain)
    {
        $this->_default_escaped = false;
        $this->_attribute = $qname;
        if ($default_attr = $this->phpelement->getAttributeNode($qname)) {
            $this->_default_escaped = $default_attr->getValueEscaped();
        }
        $this->_attkey = $this->getVarName($qname, $codewriter);
        $executor = new PHPTAL_Php_TalesChainExecutor($codewriter, $chain, $this);
        $this->phpelement->getOrCreateAttributeNode($qname)->overwriteFullWithVariable($this->_attkey);
    }

    private function prepareBooleanAttribute(PHPTAL_Php_CodeWriter $codewriter, $qname, $code)
    {
        $attkey = $this->getVarName($qname, $codewriter);

        if ($codewriter->getOutputMode() === PHPTAL::HTML5) {
            $value  = "' $qname'";
        } else {
            $value  = "' $qname=\"$qname\"'";
        }
        $codewriter->doIf($code);
        $codewriter->doSetVar($attkey, $value);
        $codewriter->doElse();
        $codewriter->doSetVar($attkey, '\'\'');
        $codewriter->doEnd('if');
        $this->phpelement->getOrCreateAttributeNode($qname)->overwriteFullWithVariable($attkey);
    }

    private function getVarName($qname, PHPTAL_Php_CodeWriter $codewriter)
    {
        $var = $codewriter->createTempVariable();
        $this->vars_to_recycle[] = $var;
        return $var;
    }


    public function after(PHPTAL_Php_CodeWriter $codewriter)
    {
        foreach ($this->vars_to_recycle as $var) $codewriter->recycleTempVariable($var);
    }

    public function talesChainNothingKeyword(PHPTAL_Php_TalesChainExecutor $executor)
    {
        $codewriter = $executor->getCodeWriter();
        $executor->doElse();
        $codewriter->doSetVar(
            $this->_attkey,
            "''"
        );
        $executor->breakChain();
    }

    public function talesChainDefaultKeyword(PHPTAL_Php_TalesChainExecutor $executor)
    {
        $codewriter = $executor->getCodeWriter();
        $executor->doElse();
        $attr_str = ($this->_default_escaped !== false)
            ? ' '.$this->_attribute.'='.$codewriter->quoteAttributeValue($this->_default_escaped)  // default value
            : '';                                 // do not print attribute
        $codewriter->doSetVar($this->_attkey, $codewriter->str($attr_str));
        $executor->breakChain();
    }

    public function talesChainPart(PHPTAL_Php_TalesChainExecutor $executor, $exp, $islast)
    {
        $codewriter = $executor->getCodeWriter();

        if (!$islast) {
            $condition = "!phptal_isempty($this->_attkey = ($exp))";
        } else {
            $condition = "null !== ($this->_attkey = ($exp))";
        }
        $executor->doIf($condition);

        if ($this->_echoType == PHPTAL_Php_Attribute::ECHO_STRUCTURE)
            $value = $codewriter->stringifyCode($this->_attkey);
        else
            $value = $codewriter->escapeCode($this->_attkey);

        $codewriter->doSetVar($this->_attkey, $codewriter->str(" {$this->_attribute}=\"").".$value.'\"'");
    }
}