]>
Commit | Line | Data |
---|---|---|
2f01fe57 AD |
1 | /* |
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved. | |
3 | Available via Academic Free License >= 2.1 OR the modified BSD license. | |
4 | see: http://dojotoolkit.org/license for details | |
5 | */ | |
6 | ||
7 | ||
8 | if(!dojo._hasResource["dojo.html"]){ | |
9 | dojo._hasResource["dojo.html"]=true; | |
10 | dojo.provide("dojo.html"); | |
11 | dojo.require("dojo.parser"); | |
12 | (function(){ | |
13 | var _1=0,d=dojo; | |
14 | dojo.html._secureForInnerHtml=function(_2){ | |
15 | return _2.replace(/(?:\s*<!DOCTYPE\s[^>]+>|<title[^>]*>[\s\S]*?<\/title>)/ig,""); | |
16 | }; | |
17 | dojo.html._emptyNode=dojo.empty; | |
18 | dojo.html._setNodeContent=function(_3,_4){ | |
19 | d.empty(_3); | |
20 | if(_4){ | |
21 | if(typeof _4=="string"){ | |
22 | _4=d._toDom(_4,_3.ownerDocument); | |
23 | } | |
24 | if(!_4.nodeType&&d.isArrayLike(_4)){ | |
25 | for(var _5=_4.length,i=0;i<_4.length;i=_5==_4.length?i+1:0){ | |
26 | d.place(_4[i],_3,"last"); | |
27 | } | |
28 | }else{ | |
29 | d.place(_4,_3,"last"); | |
30 | } | |
31 | } | |
32 | return _3; | |
33 | }; | |
34 | dojo.declare("dojo.html._ContentSetter",null,{node:"",content:"",id:"",cleanContent:false,extractContent:false,parseContent:false,constructor:function(_6,_7){ | |
35 | dojo.mixin(this,_6||{}); | |
36 | _7=this.node=dojo.byId(this.node||_7); | |
37 | if(!this.id){ | |
38 | this.id=["Setter",(_7)?_7.id||_7.tagName:"",_1++].join("_"); | |
39 | } | |
40 | },set:function(_8,_9){ | |
41 | if(undefined!==_8){ | |
42 | this.content=_8; | |
43 | } | |
44 | if(_9){ | |
45 | this._mixin(_9); | |
46 | } | |
47 | this.onBegin(); | |
48 | this.setContent(); | |
49 | this.onEnd(); | |
50 | return this.node; | |
51 | },setContent:function(){ | |
52 | var _a=this.node; | |
53 | if(!_a){ | |
54 | throw new Error(this.declaredClass+": setContent given no node"); | |
55 | } | |
56 | try{ | |
57 | _a=dojo.html._setNodeContent(_a,this.content); | |
58 | } | |
59 | catch(e){ | |
60 | var _b=this.onContentError(e); | |
61 | try{ | |
62 | _a.innerHTML=_b; | |
63 | } | |
64 | catch(e){ | |
65 | console.error("Fatal "+this.declaredClass+".setContent could not change content due to "+e.message,e); | |
66 | } | |
67 | } | |
68 | this.node=_a; | |
69 | },empty:function(){ | |
70 | if(this.parseResults&&this.parseResults.length){ | |
71 | dojo.forEach(this.parseResults,function(w){ | |
72 | if(w.destroy){ | |
73 | w.destroy(); | |
74 | } | |
75 | }); | |
76 | delete this.parseResults; | |
77 | } | |
78 | dojo.html._emptyNode(this.node); | |
79 | },onBegin:function(){ | |
80 | var _c=this.content; | |
81 | if(dojo.isString(_c)){ | |
82 | if(this.cleanContent){ | |
83 | _c=dojo.html._secureForInnerHtml(_c); | |
84 | } | |
85 | if(this.extractContent){ | |
86 | var _d=_c.match(/<body[^>]*>\s*([\s\S]+)\s*<\/body>/im); | |
87 | if(_d){ | |
88 | _c=_d[1]; | |
89 | } | |
90 | } | |
91 | } | |
92 | this.empty(); | |
93 | this.content=_c; | |
94 | return this.node; | |
95 | },onEnd:function(){ | |
96 | if(this.parseContent){ | |
97 | this._parse(); | |
98 | } | |
99 | return this.node; | |
100 | },tearDown:function(){ | |
101 | delete this.parseResults; | |
102 | delete this.node; | |
103 | delete this.content; | |
104 | },onContentError:function(_e){ | |
105 | return "Error occured setting content: "+_e; | |
106 | },_mixin:function(_f){ | |
107 | var _10={},key; | |
108 | for(key in _f){ | |
109 | if(key in _10){ | |
110 | continue; | |
111 | } | |
112 | this[key]=_f[key]; | |
113 | } | |
114 | },_parse:function(){ | |
115 | var _11=this.node; | |
116 | try{ | |
117 | this.parseResults=dojo.parser.parse({rootNode:_11,dir:this.dir,lang:this.lang}); | |
118 | } | |
119 | catch(e){ | |
120 | this._onError("Content",e,"Error parsing in _ContentSetter#"+this.id); | |
121 | } | |
122 | },_onError:function(_12,err,_13){ | |
123 | var _14=this["on"+_12+"Error"].call(this,err); | |
124 | if(_13){ | |
125 | console.error(_13,err); | |
126 | }else{ | |
127 | if(_14){ | |
128 | dojo.html._setNodeContent(this.node,_14,true); | |
129 | } | |
130 | } | |
131 | }}); | |
132 | dojo.html.set=function(_15,_16,_17){ | |
133 | if(undefined==_16){ | |
134 | console.warn("dojo.html.set: no cont argument provided, using empty string"); | |
135 | _16=""; | |
136 | } | |
137 | if(!_17){ | |
138 | return dojo.html._setNodeContent(_15,_16,true); | |
139 | }else{ | |
140 | var op=new dojo.html._ContentSetter(dojo.mixin(_17,{content:_16,node:_15})); | |
141 | return op.set(); | |
142 | } | |
143 | }; | |
144 | })(); | |
145 | } |