DoneJS StealJS jQuery ++ FuncUnit DocumentJS
3.0.0
2.3.27

 

  • Github
  • Twitter
  • Chat
  • Forum
  • Guides
  • Core
    • can-component
    • can-compute
    • can-connect
    • can-define
    • can-define/list/list
    • can-define/map/map
    • can-route
    • can-route-pushstate
    • can-set
    • can-stache
      • Pages
        • Magic Tag Types
        • Scope and Context
        • Expressions
        • Template Acquisition
        • Helpers
        • Live Binding
      • Methods
        • from
        • registerConverter
        • registerHelper
        • registerPartial
        • registerSimpleHelper
        • safeString
      • Tags
        • {{expression}}
        • {{{expression}}}
        • {{#expression}}
        • {{/expression}}
        • {{^expression}}
        • {{>key}}
        • {{!expression}}
        • {{else}}
      • Expressions
        • Bracket Expression
        • Call Expression
        • Hash Expression
        • Helper Expression
        • KeyLookup Expression
        • Literal Expression
      • Key Operators
        • @at
        • ~compute
        • ./current
        • ../parent
        • %special
        • this
        • *variable
        • key
      • Helpers
        • {{#if expression}}
        • {{#unless expression}}
        • {{#each expression}}
        • {{#with expression}}
        • {{log}}
        • {{#is expressions}}
        • {{#switch expression}}
        • {{#case expression}}
        • {{#default}}
        • {{joinBase expressions}}
      • Types
        • getterSetter
        • helper
        • helperOptions
        • renderer
        • sectionRenderer
        • simpleHelper
    • can-stache/helpers/route
    • can-stache-bindings
  • Ecosystem
  • Infrastructure
  • Legacy
  • Bitovi
    • Bitovi.com
    • Blog
    • Consulting
    • Training
    • Open Source
  • Chat
  • Forum
  • Star
  • Follow @canjs
  • CanJS
  • /
  • Core
  • /
  • can-stache
  • /
  • safeString
  • / On this page
    • safeString

      function

      Indicate that a string does not need to be escaped to be safely inserted into the page.

      • source

      stache.safeString(str)

      By default, stache tries to prevent some common forms of cross site scripting attacks by escaping content passed to tags like {{expression}} and the result of helpers. However, one will often need to create helpers that return HTML content that shouldn't be escaped.

      stache.safeString can be used to indicate that a returned string from a helper is safe:

      stache.registerHelper("myHelper", function(){
        return stache.safeString("<blink>Hello There!</blink>");
      })
      

      Parameters

      1. str {String}:

        A string you don't want to become escaped.

      Returns

      {String}:

      A string flagged by stache as safe, which will not become escaped, even if you use {{expression}}.

      Use

      If you write a helper that generates its own HTML, you will usually want to return a stache.safeString. In this case, you will want to manually escape parameters with string.esc.

      var string = require("can-util/js/string/string");
      
      stache.registerHelper('link', function(text, url) {
        text = string.esc(text);
        url  = string.esc(url);
      
        var result = '<a href="' + url + '">' + text + '</a>';
        return stache.safeString(result);
      });
      

      Rendering:

      <div>{{link "Google", "http://google.com"}}</div>
      

      Results in:

      <div><a href="http://google.com">Google</a></div>
      

      As an anchor tag whereas if we would have just returned the result rather than a stache.safeString our template would have rendered a div with the escaped anchor tag.

      CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 3.0.0.