| 1 |
define([ |
| 2 |
"dojo/_base/declare", |
| 3 |
"dojo/Deferred", |
| 4 |
"dojo/dom-construct", |
| 5 |
"dojo/html", |
| 6 |
"dojo/_base/kernel", |
| 7 |
"dojo/_base/lang", |
| 8 |
"dojo/ready", |
| 9 |
"dojo/_base/sniff", |
| 10 |
"dojo/_base/url", |
| 11 |
"dojo/_base/xhr", |
| 12 |
"dojo/when", |
| 13 |
"dojo/_base/window" |
| 14 |
], function(declare, Deferred, domConstruct, htmlUtil, kernel, lang, ready, has, _Url, xhrUtil, when, windowUtil){ |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
var html = kernel.getObject("dojox.html", true); |
| 26 |
|
| 27 |
if(has("ie")){ |
| 28 |
var alphaImageLoader = /(AlphaImageLoader\([^)]*?src=(['"]))(?![a-z]+:|\/)([^\r\n;}]+?)(\2[^)]*\)\s*[;}]?)/g; |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
var cssPaths = /(?:(?:@import\s*(['"])(?![a-z]+:|\/)([^\r\n;{]+?)\1)|url\(\s*(['"]?)(?![a-z]+:|\/)([^\r\n;]+?)\3\s*\))([a-z, \s]*[;}]?)/g; |
| 46 |
|
| 47 |
var adjustCssPaths = html._adjustCssPaths = function(cssUrl, cssText){ |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
if(!cssText || !cssUrl){ return; } |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
if(alphaImageLoader){ |
| 82 |
cssText = cssText.replace(alphaImageLoader, function(ignore, pre, delim, url, post){ |
| 83 |
return pre + (new _Url(cssUrl, './'+url).toString()) + post; |
| 84 |
}); |
| 85 |
} |
| 86 |
|
| 87 |
return cssText.replace(cssPaths, function(ignore, delimStr, strUrl, delimUrl, urlUrl, media){ |
| 88 |
if(strUrl){ |
| 89 |
return '@import "' + (new _Url(cssUrl, './'+strUrl).toString()) + '"' + media; |
| 90 |
}else{ |
| 91 |
return 'url(' + (new _Url(cssUrl, './'+urlUrl).toString()) + ')' + media; |
| 92 |
} |
| 93 |
}); |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
var htmlAttrPaths = /(<[a-z][a-z0-9]*\s[^>]*)(?:(href|src)=(['"]?)([^>]*?)\3|style=(['"]?)([^>]*?)\5)([^>]*>)/gi; |
| 100 |
|
| 101 |
var adjustHtmlPaths = html._adjustHtmlPaths = function(htmlUrl, cont){ |
| 102 |
var url = htmlUrl || "./"; |
| 103 |
|
| 104 |
return cont.replace(htmlAttrPaths, |
| 105 |
function(tag, start, name, delim, relUrl, delim2, cssText, end){ |
| 106 |
return start + (name ? |
| 107 |
(name + '=' + delim + (new _Url(url, relUrl).toString()) + delim) |
| 108 |
: ('style=' + delim2 + adjustCssPaths(url, cssText) + delim2) |
| 109 |
) + end; |
| 110 |
} |
| 111 |
); |
| 112 |
}; |
| 113 |
|
| 114 |
var snarfStyles = html._snarfStyles = function (cssUrl, cont, styles){ |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
styles.attributes = []; |
| 119 |
|
| 120 |
cont = cont.replace(/<[!][-][-](.|\s)*?[-][-]>/g, |
| 121 |
function(comment){ |
| 122 |
return comment.replace(/<(\/?)style\b/ig,"<$1Style").replace(/<(\/?)link\b/ig,"<$1Link").replace(/@import "/ig,"@ import \""); |
| 123 |
} |
| 124 |
); |
| 125 |
return cont.replace(/(?:<style([^>]*)>([\s\S]*?)<\/style>|<link\s+(?=[^>]*rel=['"]?stylesheet)([^>]*?href=(['"])([^>]*?)\4[^>\/]*)\/?>)/gi, |
| 126 |
function(ignore, styleAttr, cssText, linkAttr, delim, href){ |
| 127 |
|
| 128 |
var i, attr = (styleAttr||linkAttr||"").replace(/^\s*([\s\S]*?)\s*$/i, "$1"); |
| 129 |
if(cssText){ |
| 130 |
i = styles.push(cssUrl ? adjustCssPaths(cssUrl, cssText) : cssText); |
| 131 |
}else{ |
| 132 |
i = styles.push('@import "' + href + '";'); |
| 133 |
attr = attr.replace(/\s*(?:rel|href)=(['"])?[^\s]*\1\s*/gi, ""); |
| 134 |
} |
| 135 |
if(attr){ |
| 136 |
attr = attr.split(/\s+/); |
| 137 |
var atObj = {}, tmp; |
| 138 |
for(var j = 0, e = attr.length; j < e; j++){ |
| 139 |
tmp = attr[j].split('='); |
| 140 |
atObj[tmp[0]] = tmp[1].replace(/^\s*['"]?([\s\S]*?)['"]?\s*$/, "$1"); |
| 141 |
} |
| 142 |
styles.attributes[i - 1] = atObj; |
| 143 |
} |
| 144 |
return ""; |
| 145 |
} |
| 146 |
); |
| 147 |
}; |
| 148 |
|
| 149 |
var snarfScripts = html._snarfScripts = function(cont, byRef){ |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
byRef.code = ""; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
cont = cont.replace(/<[!][-][-](.|\s)*?[-][-]>/g, |
| 160 |
function(comment){ |
| 161 |
return comment.replace(/<(\/?)script\b/ig,"<$1Script"); |
| 162 |
} |
| 163 |
); |
| 164 |
|
| 165 |
function download(src){ |
| 166 |
if(byRef.downloadRemote){ |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
src = src.replace(/&([a-z0-9#]+);/g, function(m, name) { |
| 171 |
switch(name) { |
| 172 |
case "amp" : return "&"; |
| 173 |
case "gt" : return ">"; |
| 174 |
case "lt" : return "<"; |
| 175 |
default: |
| 176 |
return name.charAt(0)=="#" ? String.fromCharCode(name.substring(1)) : "&"+name+";"; |
| 177 |
} |
| 178 |
}); |
| 179 |
xhrUtil.get({ |
| 180 |
url: src, |
| 181 |
sync: true, |
| 182 |
load: function(code){ |
| 183 |
if(byRef.code !=="") { |
| 184 |
code = "\n" + code; |
| 185 |
} |
| 186 |
byRef.code += code+";"; |
| 187 |
}, |
| 188 |
error: byRef.errBack |
| 189 |
}); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
return cont.replace(/<script\s*(?![^>]*type=['"]?(?:dojo\/|text\/html\b))[^>]*?(?:src=(['"]?)([^>]*?)\1[^>]*)?>([\s\S]*?)<\/script>/gi, |
| 195 |
function(ignore, delim, src, code){ |
| 196 |
if(src){ |
| 197 |
download(src); |
| 198 |
}else{ |
| 199 |
if(byRef.code !=="") { |
| 200 |
code = "\n" + code; |
| 201 |
} |
| 202 |
byRef.code += code+";"; |
| 203 |
} |
| 204 |
return ""; |
| 205 |
} |
| 206 |
); |
| 207 |
}; |
| 208 |
|
| 209 |
var evalInGlobal = html.evalInGlobal = function(code, appendNode){ |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
appendNode = appendNode || windowUtil.doc.body; |
| 214 |
var n = appendNode.ownerDocument.createElement('script'); |
| 215 |
n.type = "text/javascript"; |
| 216 |
appendNode.appendChild(n); |
| 217 |
n.text = code; |
| 218 |
}; |
| 219 |
|
| 220 |
html._ContentSetter = declare( htmlUtil._ContentSetter, { |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
adjustPaths: false, |
| 225 |
referencePath: ".", |
| 226 |
renderStyles: false, |
| 227 |
|
| 228 |
executeScripts: false, |
| 229 |
scriptHasHooks: false, |
| 230 |
scriptHookReplacement: null, |
| 231 |
|
| 232 |
_renderStyles: function(styles){ |
| 233 |
|
| 234 |
this._styleNodes = []; |
| 235 |
var st, att, cssText, doc = this.node.ownerDocument; |
| 236 |
var head = doc.getElementsByTagName('head')[0]; |
| 237 |
|
| 238 |
for(var i = 0, e = styles.length; i < e; i++){ |
| 239 |
cssText = styles[i]; att = styles.attributes[i]; |
| 240 |
st = doc.createElement('style'); |
| 241 |
st.setAttribute("type", "text/css"); |
| 242 |
|
| 243 |
for(var x in att){ |
| 244 |
st.setAttribute(x, att[x]); |
| 245 |
} |
| 246 |
|
| 247 |
this._styleNodes.push(st); |
| 248 |
head.appendChild(st); |
| 249 |
|
| 250 |
if(st.styleSheet){ |
| 251 |
st.styleSheet.cssText = cssText; |
| 252 |
}else{ |
| 253 |
st.appendChild(doc.createTextNode(cssText)); |
| 254 |
} |
| 255 |
} |
| 256 |
}, |
| 257 |
|
| 258 |
empty: function() { |
| 259 |
this.inherited("empty", arguments); |
| 260 |
|
| 261 |
|
| 262 |
this._styles = []; |
| 263 |
}, |
| 264 |
|
| 265 |
onBegin: function() { |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
this.inherited("onBegin", arguments); |
| 273 |
|
| 274 |
var cont = this.content, |
| 275 |
node = this.node; |
| 276 |
|
| 277 |
var styles = this._styles; |
| 278 |
this._code = null; |
| 279 |
|
| 280 |
if(lang.isString(cont)){ |
| 281 |
if(this.adjustPaths && this.referencePath){ |
| 282 |
cont = adjustHtmlPaths(this.referencePath, cont); |
| 283 |
} |
| 284 |
|
| 285 |
if(this.renderStyles || this.cleanContent){ |
| 286 |
cont = snarfStyles(this.referencePath, cont, styles); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
if(this.executeScripts){ |
| 292 |
var _t = this; |
| 293 |
var byRef = { |
| 294 |
downloadRemote: true, |
| 295 |
errBack:function(e){ |
| 296 |
_t._onError.call(_t, 'Exec', 'Error downloading remote script in "'+_t.id+'"', e); |
| 297 |
} |
| 298 |
}; |
| 299 |
cont = snarfScripts(cont, byRef); |
| 300 |
this._code = byRef.code; |
| 301 |
} |
| 302 |
} |
| 303 |
this.content = cont; |
| 304 |
}, |
| 305 |
|
| 306 |
onEnd: function() { |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
var code = this._code, |
| 313 |
styles = this._styles; |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
if(this._styleNodes && this._styleNodes.length){ |
| 320 |
while(this._styleNodes.length){ |
| 321 |
domConstruct.destroy(this._styleNodes.pop()); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
if(this.renderStyles && styles && styles.length){ |
| 326 |
this._renderStyles(styles); |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
var d = new Deferred(); |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
var superClassOnEndMethod = this.getInherited(arguments), |
| 335 |
args = arguments, |
| 336 |
callSuperclass = lang.hitch(this, function(){ |
| 337 |
superClassOnEndMethod.apply(this, args); |
| 338 |
|
| 339 |
|
| 340 |
when(this.parseDeferred, function(){ d.resolve(); }); |
| 341 |
}); |
| 342 |
|
| 343 |
if(this.executeScripts && code){ |
| 344 |
|
| 345 |
if(this.cleanContent){ |
| 346 |
|
| 347 |
|
| 348 |
code = code.replace(/(<!--|(?:\/\/)?-->|<!\[CDATA\[|\]\]>)/g, ''); |
| 349 |
} |
| 350 |
if(this.scriptHasHooks){ |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
code = code.replace(/_container_(?!\s*=[^=])/g, this.scriptHookReplacement); |
| 355 |
} |
| 356 |
try{ |
| 357 |
evalInGlobal(code, this.node); |
| 358 |
}catch(e){ |
| 359 |
this._onError('Exec', 'Error eval script in '+this.id+', '+e.message, e); |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
ready(callSuperclass); |
| 366 |
}else{ |
| 367 |
|
| 368 |
|
| 369 |
callSuperclass(); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
return d.promise; |
| 374 |
}, |
| 375 |
|
| 376 |
tearDown: function() { |
| 377 |
this.inherited(arguments); |
| 378 |
delete this._styles; |
| 379 |
|
| 380 |
|
| 381 |
if(this._styleNodes && this._styleNodes.length){ |
| 382 |
while(this._styleNodes.length){ |
| 383 |
domConstruct.destroy(this._styleNodes.pop()); |
| 384 |
} |
| 385 |
} |
| 386 |
delete this._styleNodes; |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
lang.mixin(this, html._ContentSetter.prototype); |
| 392 |
} |
| 393 |
|
| 394 |
}); |
| 395 |
|
| 396 |
html.set = function( node, cont, params){ |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
if(!params){ |
| 417 |
|
| 418 |
return htmlUtil._setNodeContent(node, cont, true); |
| 419 |
}else{ |
| 420 |
|
| 421 |
var op = new html._ContentSetter(lang.mixin( |
| 422 |
params, |
| 423 |
{ content: cont, node: node } |
| 424 |
)); |
| 425 |
return op.set(); |
| 426 |
} |
| 427 |
}; |
| 428 |
|
| 429 |
return html; |
| 430 |
}); |