- fix issue outlined by Emmanuel Engelhart <emmanuel@engelhart.org> on wikitech-l
[mediawiki.git] / PHPTAL-NP-0.7.0 / libs / PHPTAL / Attribute / TAL / Attributes.php
blob42420f88d05a1f14bc8fb1d66a96f4cbba0d6dc6
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 //
4 // Copyright (c) 2003 Laurent Bedubourg
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // Authors: Laurent Bedubourg <laurent.bedubourg@free.fr>
21 //
23 /**
24 * tal:attributes attribute handler.
26 * This template attribute defines xhtml entity attributes.
28 * Assuming link variable :
30 * $link->href = "http://www.google.fr";
31 * $link->title = "google search engine";
32 * $link->text = "google";
34 * The template code :
36 * <a href="http://www.example.com"
37 * title="sample title"
38 * class="cssLink"
40 * tal:attributes="href link/href; title link/title"
41 * tal:content="href link/text"
43 * >sample text</a>
45 * Will produce :
47 * <a class="cssLink"
48 * href="http://www.google.com"
49 * title="google search engine"
50 * >google</a>
52 * As shown above, non overwritten attributes are keep in result xhtml.
54 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
56 class PHPTAL_Attribute_TAL_Attributes extends PHPTAL_Attribute
58 var $_overwritten = array();
60 function activate(&$g, &$tag)
62 global $_phptal_xhtml_boolean_attributes;
64 $g->doPrintString('<', $tag->name());
66 $atts = $tag->attributes();
68 $exp = new PHPTAL_Expression($g, $tag, $this->expression);
69 $exp->setPolicy(_PHPTAL_ES_RECEIVER_IS_TEMP);
70 $err = $exp->prepare();
71 if (PEAR::isError($err)) { return $err; }
73 if ($exp->countSubs() > 0) {
74 foreach ($exp->subs() as $sub) {
75 $err = $this->_attributeExpression($g, $tag, $sub);
76 if (PEAR::isError($err)) { return $err; }
78 } else {
79 $err = $this->_attributeExpression($g, $tag, $exp);
80 if (PEAR::isError($err)) { return $err; }
83 // echo non overwritten xhtml attributes
85 foreach ($atts as $key=>$value) {
86 $test_key = strtolower($key);
87 if (!in_array($test_key, $this->_overwritten)) {
88 // boolean attributes
89 if ($tag->_parser->_outputMode() == PHPTAL_XHTML
90 && in_array($test_key, $_phptal_xhtml_boolean_attributes)) {
91 $g->doPrintString(' '.$key);
92 } else {
93 $g->doPrintString(' '.$key .'="');
94 $g->doPrintString($value);
95 $g->doPrintString('"');
100 if ($tag->_isXHTMLEmptyElement()) {
101 $g->doPrintString(' />');
102 return;
105 // continue tag show
106 if ($tag->hasContent()) {
107 $g->doPrintString('>');
108 } else {
109 $g->doPrintString('/>');
112 $err = $tag->generateContent($g);
113 if (PEAR::isError($err)) { return $err; }
115 if ($tag->hasContent()) {
116 $g->doPrintString('</', $tag->name(), '>');
120 function _printAttribute(&$g, &$tag, &$realName, &$varName, $default=false)
122 global $_phptal_xhtml_boolean_attributes;
123 // watch boolean attributes on XHTML mode
124 if ($tag->_parser->_outputMode() == PHPTAL_XHTML
125 && in_array($realName, $_phptal_xhtml_boolean_attributes)) {
126 $g->doIf('!PEAR::isError($'.$varName.') && $'.$varName);
127 $g->doPrintString(' '.$realName);
128 $g->endBlock();
129 } elseif ($default) {
130 $g->doIf('!PEAR::isError($'.$varName.') && false !== $'.$varName.' && null !== $'.$varName);
131 $g->doPrintString(' '. $realName . '="');
132 $g->doPrintVar($varName);
133 $g->doPrintString('"');
134 $g->endBlock();
135 } else {
136 $g->doPrintString(' '. $realName . '="');
137 $g->doPrintVar($varName);
138 $g->doPrintString('"');
142 function _attributeExpression(&$g, &$tag, &$sub)
144 $atts = $tag->attributes();
145 $default = false;
146 $name = strtolower($sub->getReceiver());
148 // default in tal:attributes use default tag attributes
149 // if (preg_match('/\|\s*?\bdefault\b/sm', $sub->_src)) {
150 if (preg_match('/\bdefault\b/sm', $sub->_src)) {
151 // ensure the attribute as a default value set in source
152 // template
153 $attrs = $tag->attributes();
154 if (!array_key_exists($name, $atts)){
155 $g->doAffectResult('$__default__', 'false');
156 } else {
157 // store the default value in __default__ variable
158 $g->doAffectResult('$__default__',
159 '\''. str_replace('\'', '\\\'', $atts[$name]) . '\'' );
161 $default = true;
164 $real = str_replace('__phptales_dd__', ':', $name);
165 $this->_overwritten[] = $real;
166 $err = $sub->generate();
167 if (PEAR::isError($err)) { return $err; }
169 $this->_printAttribute($g, $tag, $real, $name, $default);