PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/kotal/vendor/phptal/PHPTAL/Dom/Defs.php

https://bitbucket.org/chrispiechowicz/zepto
PHP | 224 lines | 109 code | 18 blank | 97 comment | 9 complexity | ca515ed7fe0d58864296be3d2fa14345 MD5 | raw file
Possible License(s): LGPL-2.1, MIT, BSD-3-Clause
  1. <?php
  2. /**
  3. * PHPTAL templating engine
  4. *
  5. * PHP Version 5
  6. *
  7. * @category HTML
  8. * @package PHPTAL
  9. * @author Laurent Bedubourg <lbedubourg@motion-twin.com>
  10. * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk>
  11. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  12. * @version SVN: $Id$
  13. * @link http://phptal.org/
  14. */
  15. /**
  16. * PHPTAL constants.
  17. *
  18. * This is a pseudo singleton class, a user may decide to provide
  19. * his own singleton instance which will then be used by PHPTAL.
  20. *
  21. * This behaviour is mainly useful to remove builtin namespaces
  22. * and provide custom ones.
  23. *
  24. * @package PHPTAL
  25. * @subpackage Dom
  26. * @author Laurent Bedubourg <lbedubourg@motion-twin.com>
  27. */
  28. class PHPTAL_Dom_Defs
  29. {
  30. /**
  31. * this is a singleton
  32. */
  33. public static function getInstance()
  34. {
  35. if (!self::$_instance) {
  36. self::$_instance = new PHPTAL_Dom_Defs();
  37. }
  38. return self::$_instance;
  39. }
  40. protected function __construct()
  41. {
  42. $this->registerNamespace(new PHPTAL_Namespace_TAL());
  43. $this->registerNamespace(new PHPTAL_Namespace_METAL());
  44. $this->registerNamespace(new PHPTAL_Namespace_I18N());
  45. $this->registerNamespace(new PHPTAL_Namespace_PHPTAL());
  46. }
  47. /**
  48. * true if it's empty in XHTML (e.g. <img/>)
  49. * it will assume elements with no namespace may be XHTML too.
  50. *
  51. * @param string $tagName local name of the tag
  52. *
  53. * @return bool
  54. */
  55. public function isEmptyTagNS($namespace_uri, $local_name)
  56. {
  57. return ($namespace_uri === 'http://www.w3.org/1999/xhtml' || $namespace_uri === '')
  58. && in_array(strtolower($local_name), self::$XHTML_EMPTY_TAGS);
  59. }
  60. /**
  61. * gives namespace URI for given registered (built-in) prefix
  62. */
  63. public function prefixToNamespaceURI($prefix)
  64. {
  65. return isset($this->prefix_to_uri[$prefix]) ? $this->prefix_to_uri[$prefix] : false;
  66. }
  67. /**
  68. * gives typical prefix for given (built-in) namespace
  69. */
  70. public function namespaceURIToPrefix($uri)
  71. {
  72. return array_search($uri, $this->prefix_to_uri, true);
  73. }
  74. /**
  75. * array prefix => uri for prefixes that don't have to be declared in PHPTAL
  76. * @return array
  77. */
  78. public function getPredefinedPrefixes()
  79. {
  80. return $this->prefix_to_uri;
  81. }
  82. /**
  83. * Returns true if the attribute is an xhtml boolean attribute.
  84. *
  85. * @param string $att local name
  86. *
  87. * @return bool
  88. */
  89. public function isBooleanAttribute($att)
  90. {
  91. return in_array($att, self::$XHTML_BOOLEAN_ATTRIBUTES);
  92. }
  93. /**
  94. * true if elements content is parsed as CDATA in text/html
  95. * and also accepts /* * / as comments.
  96. */
  97. public function isCDATAElementInHTML($namespace_uri, $local_name)
  98. {
  99. return ($local_name === 'script' || $local_name === 'style')
  100. && ($namespace_uri === 'http://www.w3.org/1999/xhtml' || $namespace_uri === '');
  101. }
  102. /**
  103. * Returns true if the attribute is a valid phptal attribute
  104. *
  105. * Examples of valid attributes: tal:content, metal:use-slot
  106. * Examples of invalid attributes: tal:unknown, metal:content
  107. *
  108. * @return bool
  109. */
  110. public function isValidAttributeNS($namespace_uri, $local_name)
  111. {
  112. if (!$this->isHandledNamespace($namespace_uri)) return false;
  113. $attrs = $this->namespaces_by_uri[$namespace_uri]->getAttributes();
  114. return isset($attrs[$local_name]);
  115. }
  116. /**
  117. * is URI registered (built-in) namespace
  118. */
  119. public function isHandledNamespace($namespace_uri)
  120. {
  121. return isset($this->namespaces_by_uri[$namespace_uri]);
  122. }
  123. /**
  124. * Returns true if the attribute is a phptal handled xml namespace
  125. * declaration.
  126. *
  127. * Examples of handled xmlns: xmlns:tal, xmlns:metal
  128. *
  129. * @return bool
  130. */
  131. public function isHandledXmlNs($qname, $value)
  132. {
  133. return substr(strtolower($qname), 0, 6) == 'xmlns:' && $this->isHandledNamespace($value);
  134. }
  135. /**
  136. * return objects that holds information about given TAL attribute
  137. */
  138. public function getNamespaceAttribute($namespace_uri, $local_name)
  139. {
  140. $attrs = $this->namespaces_by_uri[$namespace_uri]->getAttributes();
  141. return $attrs[$local_name];
  142. }
  143. /**
  144. * Register a PHPTAL_Namespace and its attribute into PHPTAL.
  145. */
  146. public function registerNamespace(PHPTAL_Namespace $ns)
  147. {
  148. $this->namespaces_by_uri[$ns->getNamespaceURI()] = $ns;
  149. $this->prefix_to_uri[$ns->getPrefix()] = $ns->getNamespaceURI();
  150. $prefix = strtolower($ns->getPrefix());
  151. foreach ($ns->getAttributes() as $name => $attribute) {
  152. $key = $prefix.':'.strtolower($name);
  153. $this->_dictionary[$key] = $attribute;
  154. }
  155. }
  156. private static $_instance = null;
  157. private $_dictionary = array();
  158. /**
  159. * list of PHPTAL_Namespace objects
  160. */
  161. private $namespaces_by_uri = array();
  162. private $prefix_to_uri = array(
  163. 'xml'=>'http://www.w3.org/XML/1998/namespace',
  164. 'xmlns'=>'http://www.w3.org/2000/xmlns/',
  165. );
  166. /**
  167. * This array contains XHTML tags that must be echoed in a &lt;tag/&gt; form
  168. * instead of the &lt;tag&gt;&lt;/tag&gt; form.
  169. *
  170. * In fact, some browsers does not support the later form so PHPTAL
  171. * ensure these tags are correctly echoed.
  172. */
  173. private static $XHTML_EMPTY_TAGS = array(
  174. 'area',
  175. 'base',
  176. 'basefont',
  177. 'br',
  178. 'col',
  179. 'frame',
  180. 'hr',
  181. 'img',
  182. 'input',
  183. 'isindex',
  184. 'link',
  185. 'meta',
  186. 'param',
  187. );
  188. /**
  189. * This array contains XHTML boolean attributes, their value is self
  190. * contained (ie: they are present or not).
  191. */
  192. private static $XHTML_BOOLEAN_ATTRIBUTES = array(
  193. 'checked',
  194. 'compact',
  195. 'declare',
  196. 'defer',
  197. 'disabled',
  198. 'ismap',
  199. 'multiple',
  200. 'noresize',
  201. 'noshade',
  202. 'nowrap',
  203. 'readonly',
  204. 'selected',
  205. );
  206. }