You are here

function Smarty_Compiler::_parse_attrs in Quiz 6.5

Same name and namespace in other branches
  1. 6.6 includes/moodle/lib/smarty/Smarty_Compiler.class.php \Smarty_Compiler::_parse_attrs()

Parse attribute string

Parameters

string $tag_args:

Return value

array

9 calls to Smarty_Compiler::_parse_attrs()
Smarty_Compiler::_compile_block_tag in includes/moodle/lib/smarty/Smarty_Compiler.class.php
compile block function tag
Smarty_Compiler::_compile_capture_tag in includes/moodle/lib/smarty/Smarty_Compiler.class.php
Compile {capture} .. {/capture} tags
Smarty_Compiler::_compile_custom_tag in includes/moodle/lib/smarty/Smarty_Compiler.class.php
compile custom function tag
Smarty_Compiler::_compile_foreach_start in includes/moodle/lib/smarty/Smarty_Compiler.class.php
Compile {foreach ...} tag.
Smarty_Compiler::_compile_include_php_tag in includes/moodle/lib/smarty/Smarty_Compiler.class.php
Compile {include ...} tag

... See full list

File

includes/moodle/lib/smarty/Smarty_Compiler.class.php, line 1497

Class

Smarty_Compiler
Template compiling class @package Smarty

Code

function _parse_attrs($tag_args) {

  /* Tokenize tag attributes. */
  preg_match_all('~(?:' . $this->_obj_call_regexp . '|' . $this->_qstr_regexp . ' | (?>[^"\'=\\s]+)
                         )+ |
                         [=]
                        ~x', $tag_args, $match);
  $tokens = $match[0];
  $attrs = array();

  /* Parse state:
     0 - expecting attribute name
     1 - expecting '='
     2 - expecting attribute value (not '=') */
  $state = 0;
  foreach ($tokens as $token) {
    switch ($state) {
      case 0:

        /* If the token is a valid identifier, we set attribute name
           and go to state 1. */
        if (preg_match('~^\\w+$~', $token)) {
          $attr_name = $token;
          $state = 1;
        }
        else {
          $this
            ->_syntax_error("invalid attribute name: '{$token}'", E_USER_ERROR, __FILE__, __LINE__);
        }
        break;
      case 1:

        /* If the token is '=', then we go to state 2. */
        if ($token == '=') {
          $state = 2;
        }
        else {
          $this
            ->_syntax_error("expecting '=' after attribute name '{$last_token}'", E_USER_ERROR, __FILE__, __LINE__);
        }
        break;
      case 2:

        /* If token is not '=', we set the attribute value and go to
           state 0. */
        if ($token != '=') {

          /* We booleanize the token if it's a non-quoted possible
             boolean value. */
          if (preg_match('~^(on|yes|true)$~', $token)) {
            $token = 'true';
          }
          else {
            if (preg_match('~^(off|no|false)$~', $token)) {
              $token = 'false';
            }
            else {
              if ($token == 'null') {
                $token = 'null';
              }
              else {
                if (preg_match('~^' . $this->_num_const_regexp . '|0[xX][0-9a-fA-F]+$~', $token)) {

                  /* treat integer literally */
                }
                else {
                  if (!preg_match('~^' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '(?:' . $this->_mod_regexp . ')*$~', $token)) {

                    /* treat as a string, double-quote it escaping quotes */
                    $token = '"' . addslashes($token) . '"';
                  }
                }
              }
            }
          }
          $attrs[$attr_name] = $token;
          $state = 0;
        }
        else {
          $this
            ->_syntax_error("'=' cannot be an attribute value", E_USER_ERROR, __FILE__, __LINE__);
        }
        break;
    }
    $last_token = $token;
  }
  if ($state != 0) {
    if ($state == 1) {
      $this
        ->_syntax_error("expecting '=' after attribute name '{$last_token}'", E_USER_ERROR, __FILE__, __LINE__);
    }
    else {
      $this
        ->_syntax_error("missing attribute value", E_USER_ERROR, __FILE__, __LINE__);
    }
  }
  $this
    ->_parse_vars_props($attrs);
  return $attrs;
}