Пример #1
0
 public function escape($s)
 {
     switch ($this->compiler->getContentType()) {
         case Compiler::CONTENT_XHTML:
         case Compiler::CONTENT_HTML:
             $context = $this->compiler->getContext();
             switch ($context[0]) {
                 case Compiler::CONTEXT_SINGLE_QUOTED:
                 case Compiler::CONTEXT_DOUBLE_QUOTED:
                     if ($context[1] === Compiler::CONTENT_JS) {
                         $s = "Nette\\Templating\\Helpers::escapeJs({$s})";
                     } elseif ($context[1] === Compiler::CONTENT_CSS) {
                         $s = "Nette\\Templating\\Helpers::escapeCss({$s})";
                     }
                     $quote = $context[0] === Compiler::CONTEXT_DOUBLE_QUOTED ? '' : ', ENT_QUOTES';
                     return "htmlSpecialChars({$s}{$quote})";
                 case Compiler::CONTEXT_COMMENT:
                     return "Nette\\Templating\\Helpers::escapeHtmlComment({$s})";
                 case Compiler::CONTENT_JS:
                 case Compiler::CONTENT_CSS:
                     return 'Nette\\Templating\\Helpers::escape' . ucfirst($context[0]) . "({$s})";
                 default:
                     return "Nette\\Templating\\Helpers::escapeHtml({$s}, ENT_NOQUOTES)";
             }
         case Compiler::CONTENT_XML:
         case Compiler::CONTENT_JS:
         case Compiler::CONTENT_CSS:
         case Compiler::CONTENT_ICAL:
             return 'Nette\\Templating\\Helpers::escape' . ucfirst($this->compiler->getContentType()) . "({$s})";
         case Compiler::CONTENT_TEXT:
             return $s;
         default:
             return "\$template->escape({$s})";
     }
 }
Пример #2
0
 /**
  * Escapes expression in tokens.
  * @return MacroTokens
  */
 public function escapeFilter(MacroTokens $tokens)
 {
     $tokens = clone $tokens;
     switch ($this->compiler->getContentType()) {
         case Compiler::CONTENT_XHTML:
         case Compiler::CONTENT_HTML:
             $context = $this->compiler->getContext();
             switch ($context[0]) {
                 case Compiler::CONTEXT_SINGLE_QUOTED_ATTR:
                 case Compiler::CONTEXT_DOUBLE_QUOTED_ATTR:
                 case Compiler::CONTEXT_UNQUOTED_ATTR:
                     if ($context[1] === Compiler::CONTENT_JS) {
                         $tokens->prepend('Nette\\Templating\\Helpers::escapeJs(')->append(')');
                     } elseif ($context[1] === Compiler::CONTENT_CSS) {
                         $tokens->prepend('Nette\\Templating\\Helpers::escapeCss(')->append(')');
                     }
                     $tokens->prepend('htmlSpecialChars(')->append($context[0] === Compiler::CONTEXT_SINGLE_QUOTED_ATTR ? ', ENT_QUOTES)' : ')');
                     if ($context[0] === Compiler::CONTEXT_UNQUOTED_ATTR) {
                         $tokens->prepend("'\"' . ")->append(" . '\"'");
                     }
                     return $tokens;
                 case Compiler::CONTEXT_COMMENT:
                     return $tokens->prepend('Nette\\Templating\\Helpers::escapeHtmlComment(')->append(')');
                 case Compiler::CONTENT_JS:
                 case Compiler::CONTENT_CSS:
                     return $tokens->prepend('Nette\\Templating\\Helpers::escape' . ucfirst($context[0]) . '(')->append(')');
                 default:
                     return $tokens->prepend('Nette\\Templating\\Helpers::escapeHtml(')->append(', ENT_NOQUOTES)');
             }
         case Compiler::CONTENT_XML:
             $context = $this->compiler->getContext();
             switch ($context[0]) {
                 case Compiler::CONTEXT_COMMENT:
                     return $tokens->prepend('Nette\\Templating\\Helpers::escapeHtmlComment(')->append(')');
                 default:
                     $tokens->prepend('Nette\\Templating\\Helpers::escapeXml(')->append(')');
                     if ($context[0] === Compiler::CONTEXT_UNQUOTED_ATTR) {
                         $tokens->prepend("'\"' . ")->append(" . '\"'");
                     }
                     return $tokens;
             }
         case Compiler::CONTENT_JS:
         case Compiler::CONTENT_CSS:
         case Compiler::CONTENT_ICAL:
             return $tokens->prepend('Nette\\Templating\\Helpers::escape' . ucfirst($this->compiler->getContentType()) . '(')->append(')');
         case Compiler::CONTENT_TEXT:
             return $tokens;
         default:
             return $tokens->prepend('$template->escape(')->append(')');
     }
 }