| 1 | define([ |
| 2 | "dojo/cache", // dojo.cache |
| 3 | "dojo/_base/declare", // declare |
| 4 | "dojo/dom-construct", // domConstruct.destroy, domConstruct.toDom |
| 5 | "dojo/_base/lang", // lang.getObject |
| 6 | "dojo/on", |
| 7 | "dojo/sniff", // has("ie") |
| 8 | "dojo/string", // string.substitute string.trim |
| 9 | "./_AttachMixin" |
| 10 | ], function(cache, declare, domConstruct, lang, on, has, string, _AttachMixin){ |
| 11 | |
| 12 | // module: |
| 13 | // dijit/_TemplatedMixin |
| 14 | |
| 15 | var _TemplatedMixin = declare("dijit._TemplatedMixin", _AttachMixin, { |
| 16 | // summary: |
| 17 | // Mixin for widgets that are instantiated from a template |
| 18 | |
| 19 | // templateString: [protected] String |
| 20 | // A string that represents the widget template. |
| 21 | // Use in conjunction with dojo.cache() to load from a file. |
| 22 | templateString: null, |
| 23 | |
| 24 | // templatePath: [protected deprecated] String |
| 25 | // Path to template (HTML file) for this widget relative to dojo.baseUrl. |
| 26 | // Deprecated: use templateString with require([... "dojo/text!..."], ...) instead |
| 27 | templatePath: null, |
| 28 | |
| 29 | // skipNodeCache: [protected] Boolean |
| 30 | // If using a cached widget template nodes poses issues for a |
| 31 | // particular widget class, it can set this property to ensure |
| 32 | // that its template is always re-built from a string |
| 33 | _skipNodeCache: false, |
| 34 | |
| 35 | /*===== |
| 36 | // _rendered: Boolean |
| 37 | // Not normally use, but this flag can be set by the app if the server has already rendered the template, |
| 38 | // i.e. already inlining the template for the widget into the main page. Reduces _TemplatedMixin to |
| 39 | // just function like _AttachMixin. |
| 40 | _rendered: false, |
| 41 | =====*/ |
| 42 | |
| 43 | // Set _AttachMixin.searchContainerNode to true for back-compat for widgets that have data-dojo-attach-point's |
| 44 | // and events inside this.containerNode. Remove for 2.0. |
| 45 | searchContainerNode: true, |
| 46 | |
| 47 | _stringRepl: function(tmpl){ |
| 48 | // summary: |
| 49 | // Does substitution of ${foo} type properties in template string |
| 50 | // tags: |
| 51 | // private |
| 52 | var className = this.declaredClass, _this = this; |
| 53 | // Cache contains a string because we need to do property replacement |
| 54 | // do the property replacement |
| 55 | return string.substitute(tmpl, this, function(value, key){ |
| 56 | if(key.charAt(0) == '!'){ value = lang.getObject(key.substr(1), false, _this); } |
| 57 | if(typeof value == "undefined"){ throw new Error(className+" template:"+key); } // a debugging aide |
| 58 | if(value == null){ return ""; } |
| 59 | |
| 60 | // Substitution keys beginning with ! will skip the transform step, |
| 61 | // in case a user wishes to insert unescaped markup, e.g. ${!foo} |
| 62 | return key.charAt(0) == "!" ? value : this._escapeValue("" + value); |
| 63 | }, this); |
| 64 | }, |
| 65 | |
| 66 | _escapeValue: function(/*String*/ val){ |
| 67 | // summary: |
| 68 | // Escape a value to be inserted into the template, either into an attribute value |
| 69 | // (ex: foo="${bar}") or as inner text of an element (ex: <span>${foo}</span>) |
| 70 | |
| 71 | // Safer substitution, see heading "Attribute values" in |
| 72 | // http://www.w3.org/TR/REC-html40/appendix/notes.html#h-B.3.2 |
| 73 | // and also https://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet#RULE_.231_-_HTML_Escape_Before_Inserting_Untrusted_Data_into_HTML_Element_Content |
| 74 | return val.replace(/["'<>&]/g, function(val){ |
| 75 | return { |
| 76 | "&": "&", |
| 77 | "<": "<", |
| 78 | ">": ">", |
| 79 | "\"": """, |
| 80 | "'": "'" |
| 81 | }[val]; |
| 82 | }); |
| 83 | }, |
| 84 | |
| 85 | buildRendering: function(){ |
| 86 | // summary: |
| 87 | // Construct the UI for this widget from a template, setting this.domNode. |
| 88 | // tags: |
| 89 | // protected |
| 90 | |
| 91 | if(!this._rendered){ |
| 92 | if(!this.templateString){ |
| 93 | this.templateString = cache(this.templatePath, {sanitize: true}); |
| 94 | } |
| 95 | |
| 96 | // Lookup cached version of template, and download to cache if it |
| 97 | // isn't there already. Returns either a DomNode or a string, depending on |
| 98 | // whether or not the template contains ${foo} replacement parameters. |
| 99 | var cached = _TemplatedMixin.getCachedTemplate(this.templateString, this._skipNodeCache, this.ownerDocument); |
| 100 | |
| 101 | var node; |
| 102 | if(lang.isString(cached)){ |
| 103 | node = domConstruct.toDom(this._stringRepl(cached), this.ownerDocument); |
| 104 | if(node.nodeType != 1){ |
| 105 | // Flag common problems such as templates with multiple top level nodes (nodeType == 11) |
| 106 | throw new Error("Invalid template: " + cached); |
| 107 | } |
| 108 | }else{ |
| 109 | // if it's a node, all we have to do is clone it |
| 110 | node = cached.cloneNode(true); |
| 111 | } |
| 112 | |
| 113 | this.domNode = node; |
| 114 | } |
| 115 | |
| 116 | // Call down to _WidgetBase.buildRendering() to get base classes assigned |
| 117 | // TODO: change the baseClass assignment to _setBaseClassAttr |
| 118 | this.inherited(arguments); |
| 119 | |
| 120 | if(!this._rendered){ |
| 121 | this._fillContent(this.srcNodeRef); |
| 122 | } |
| 123 | |
| 124 | this._rendered = true; |
| 125 | }, |
| 126 | |
| 127 | _fillContent: function(/*DomNode*/ source){ |
| 128 | // summary: |
| 129 | // Relocate source contents to templated container node. |
| 130 | // this.containerNode must be able to receive children, or exceptions will be thrown. |
| 131 | // tags: |
| 132 | // protected |
| 133 | var dest = this.containerNode; |
| 134 | if(source && dest){ |
| 135 | while(source.hasChildNodes()){ |
| 136 | dest.appendChild(source.firstChild); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | }); |
| 142 | |
| 143 | // key is templateString; object is either string or DOM tree |
| 144 | _TemplatedMixin._templateCache = {}; |
| 145 | |
| 146 | _TemplatedMixin.getCachedTemplate = function(templateString, alwaysUseString, doc){ |
| 147 | // summary: |
| 148 | // Static method to get a template based on the templatePath or |
| 149 | // templateString key |
| 150 | // templateString: String |
| 151 | // The template |
| 152 | // alwaysUseString: Boolean |
| 153 | // Don't cache the DOM tree for this template, even if it doesn't have any variables |
| 154 | // doc: Document? |
| 155 | // The target document. Defaults to document global if unspecified. |
| 156 | // returns: Mixed |
| 157 | // Either string (if there are ${} variables that need to be replaced) or just |
| 158 | // a DOM tree (if the node can be cloned directly) |
| 159 | |
| 160 | // is it already cached? |
| 161 | var tmplts = _TemplatedMixin._templateCache; |
| 162 | var key = templateString; |
| 163 | var cached = tmplts[key]; |
| 164 | if(cached){ |
| 165 | try{ |
| 166 | // if the cached value is an innerHTML string (no ownerDocument) or a DOM tree created within the |
| 167 | // current document, then use the current cached value |
| 168 | if(!cached.ownerDocument || cached.ownerDocument == (doc || document)){ |
| 169 | // string or node of the same document |
| 170 | return cached; |
| 171 | } |
| 172 | }catch(e){ /* squelch */ } // IE can throw an exception if cached.ownerDocument was reloaded |
| 173 | domConstruct.destroy(cached); |
| 174 | } |
| 175 | |
| 176 | templateString = string.trim(templateString); |
| 177 | |
| 178 | if(alwaysUseString || templateString.match(/\$\{([^\}]+)\}/g)){ |
| 179 | // there are variables in the template so all we can do is cache the string |
| 180 | return (tmplts[key] = templateString); //String |
| 181 | }else{ |
| 182 | // there are no variables in the template so we can cache the DOM tree |
| 183 | var node = domConstruct.toDom(templateString, doc); |
| 184 | if(node.nodeType != 1){ |
| 185 | throw new Error("Invalid template: " + templateString); |
| 186 | } |
| 187 | return (tmplts[key] = node); //Node |
| 188 | } |
| 189 | }; |
| 190 | |
| 191 | if(has("ie")){ |
| 192 | on(window, "unload", function(){ |
| 193 | var cache = _TemplatedMixin._templateCache; |
| 194 | for(var key in cache){ |
| 195 | var value = cache[key]; |
| 196 | if(typeof value == "object"){ // value is either a string or a DOM node template |
| 197 | domConstruct.destroy(value); |
| 198 | } |
| 199 | delete cache[key]; |
| 200 | } |
| 201 | }); |
| 202 | } |
| 203 | |
| 204 | return _TemplatedMixin; |
| 205 | }); |