/*
	Copyright (c) 2004-2009, The Dojo Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/


if(!dojo._hasResource["dojox.dtl.html"]){
dojo._hasResource["dojox.dtl.html"]=true;
dojo.provide("dojox.dtl.html");
dojo.require("dojox.dtl._base");
dojo.require("dojox.dtl.Context");
(function(){
var dd=dojox.dtl;
dd.BOOLS={checked:1,disabled:1,readonly:1};
dd.TOKEN_CHANGE=-11;
dd.TOKEN_ATTR=-12;
dd.TOKEN_CUSTOM=-13;
dd.TOKEN_NODE=1;
var _2=dd.text;
var _3=dd.html={_attributes:{},_uppers:{},_re4:/^function anonymous\(\)\s*{\s*(.*)\s*}$/,_reTrim:/(?:^\s*(\{%)?\s*|\s*(%\})?\s*$)/g,_reSplit:/\s*%\}\s*\{%\s*/g,getTemplate:function(_4){
if(typeof this._commentable=="undefined"){
this._commentable=false;
var _5=document.createElement("div");
_5.innerHTML="<!--Test comment handling, and long comments, using comments whenever possible.-->";
if(_5.childNodes.length&&_5.childNodes[0].nodeType==8&&_5.childNodes[0].data=="comment"){
this._commentable=true;
}
}
if(!this._commentable){
_4=_4.replace(/<!--({({|%).*?(%|})})-->/g,"$1");
}
if(dojo.isIE){
_4=_4.replace(/\b(checked|disabled|readonly|style)="/g,"t$1=\"");
}
_4=_4.replace(/\bstyle="/g,"tstyle=\"");
var _6;
var _7=[[true,"select","option"],[dojo.isSafari,"tr","th"],[dojo.isSafari,"tr","td"],[dojo.isSafari,"thead","tr","th"],[dojo.isSafari,"tbody","tr","td"],[dojo.isSafari,"table","tbody","tr","td"],[dojo.isSafari,"table","tr","td"]];
var _8=[];
for(var i=0,_a;_a=_7[i];i++){
if(!_a[0]){
continue;
}
if(_4.indexOf("<"+_a[1])!=-1){
var _b=new RegExp("<"+_a[1]+"[\\s\\S]*?>([\\s\\S]+?)</"+_a[1]+">","ig");
tagLoop:
while(_6=_b.exec(_4)){
var _c=false;
var _d=dojox.string.tokenize(_6[1],new RegExp("(<"+_a[2]+"[\\s\\S]*?>[\\s\\S]*?</"+_a[2]+">)","ig"),function(_e){
_c=true;
return {data:_e};
});
if(_c){
var _f=[];
for(var j=0;j<_d.length;j++){
var _11=_d[j];
if(dojo.isObject(_11)){
_f.push(_11.data);
}else{
var _12=_11.replace(this._reTrim,"");
if(_12==_11){
continue tagLoop;
}
_11=_12.split(this._reSplit);
for(var k=0,kl=_11.length;k<kl;k++){
var _15="";
for(var p=2,pl=_a.length;p<pl;p++){
_15+="<"+_a[p];
if(p==2){
_15+=" dtlinstruction=\"{% "+_11[k].replace("\"","\\\"")+" %}\"";
}
_15+=">";
}
_15+="DTL";
for(var p=_a.length-1;p>1;p--){
_15+="</"+_a[p]+">";
}
_f.push("ÿ"+_8.length);
_8.push(_15);
}
}
}
_4=_4.replace(_6[1],_f.join(""));
}
}
}
}
for(var i=_8.length;i--;){
_4=_4.replace("ÿ"+i,_8[i]);
}
var re=/\b([a-zA-Z_:][a-zA-Z0-9_\-\.:]*)=['"]/g;
while(_6=re.exec(_4)){
var _19=_6[1].toLowerCase();
if(_19=="dtlinstruction"){
continue;
}
if(_19!=_6[1]){
this._uppers[_19]=_6[1];
}
this._attributes[_19]=true;
}
var _5=document.createElement("div");
_5.innerHTML=_4;
var _1a={nodes:[]};
while(_5.childNodes.length){
_1a.nodes.push(_5.removeChild(_5.childNodes[0]));
}
return _1a;
},tokenize:function(_1b){
var _1c=[];
for(var i=0,_1e;_1e=_1b[i++];){
if(_1e.nodeType!=1){
this.__tokenize(_1e,_1c);
}else{
this._tokenize(_1e,_1c);
}
}
return _1c;
},_swallowed:[],_tokenize:function(_1f,_20){
var _21=false;
var _22=this._swallowed;
var i,j,tag,_26;
if(!_20.first){
_21=_20.first=true;
var _27=dd.register.getAttributeTags();
for(i=0;tag=_27[i];i++){
try{
(tag[2])({swallowNode:function(){
throw 1;
}},new dd.Token(dd.TOKEN_ATTR,""));
}
catch(e){
_22.push(tag);
}
}
}
for(i=0;tag=_22[i];i++){
var _28=_1f.getAttribute(tag[0]);
if(_28){
var _22=false;
var _29=(tag[2])({swallowNode:function(){
_22=true;
return _1f;
}},new dd.Token(dd.TOKEN_ATTR,_28));
if(_22){
if(_1f.parentNode&&_1f.parentNode.removeChild){
_1f.parentNode.removeChild(_1f);
}
_20.push([dd.TOKEN_CUSTOM,_29]);
return;
}
}
}
var _2a=[];
if(dojo.isIE&&_1f.tagName=="SCRIPT"){
_2a.push({nodeType:3,data:_1f.text});
_1f.text="";
}else{
for(i=0;_26=_1f.childNodes[i];i++){
_2a.push(_26);
}
}
_20.push([dd.TOKEN_NODE,_1f]);
var _2b=false;
if(_2a.length){
_20.push([dd.TOKEN_CHANGE,_1f]);
_2b=true;
}
for(var key in this._attributes){
var _2d=false;
var _2e="";
if(key=="class"){
_2e=_1f.className||_2e;
}else{
if(key=="for"){
_2e=_1f.htmlFor||_2e;
}else{
if(key=="value"&&_1f.value==_1f.innerHTML){
continue;
}else{
if(_1f.getAttribute){
_2e=_1f.getAttribute(key,2)||_2e;
if(key=="href"||key=="src"){
if(dojo.isIE){
var _2f=location.href.lastIndexOf(location.hash);
var _30=location.href.substring(0,_2f).split("/");
_30.pop();
_30=_30.join("/")+"/";
if(_2e.indexOf(_30)==0){
_2e=_2e.replace(_30,"");
}
_2e=decodeURIComponent(_2e);
}
if(_2e.indexOf("{%")!=-1||_2e.indexOf("{{")!=-1){
_2d=key;
}
}else{
if(key=="tstyle"){
_2d=key;
key="style";
}else{
if(dd.BOOLS[key.slice(1)]){
_2d=key;
key=key.slice(1);
}else{
if(this._uppers[key]){
_2d=this._uppers[key];
_1f.setAttribute(key,_2e);
}
}
}
}
}
}
}
}
if(_2d){
_1f.setAttribute(_2d,"");
_1f.removeAttribute(_2d);
}
if(typeof _2e=="function"){
_2e=_2e.toString().replace(this._re4,"$1");
}
if(!_2b){
_20.push([dd.TOKEN_CHANGE,_1f]);
_2b=true;
}
_20.push([dd.TOKEN_ATTR,_1f,key,_2e]);
}
for(i=0,_26;_26=_2a[i];i++){
if(_26.nodeType==1){
var _31=_26.getAttribute("dtlinstruction");
if(_31){
_26.parentNode.removeChild(_26);
_26={nodeType:8,data:_31};
}
}
this.__tokenize(_26,_20);
}
if(!_21&&_1f.parentNode&&_1f.parentNode.tagName){
if(_2b){
_20.push([dd.TOKEN_CHANGE,_1f,true]);
}
_20.push([dd.TOKEN_CHANGE,_1f.parentNode]);
_1f.parentNode.removeChild(_1f);
}else{
_20.push([dd.TOKEN_CHANGE,_1f,true,true]);
}
},__tokenize:function(_32,_33){
var _34=_32.data;
switch(_32.nodeType){
case 1:
this._tokenize(_32,_33);
return;
case 3:
if(_34.match(/[^\s\n]/)&&(_34.indexOf("{{")!=-1||_34.indexOf("{%")!=-1)){
var _35=_2.tokenize(_34);
for(var j=0,_37;_37=_35[j];j++){
if(typeof _37=="string"){
_33.push([dd.TOKEN_TEXT,_37]);
}else{
_33.push(_37);
}
}
}else{
_33.push([_32.nodeType,_32]);
}
if(_32.parentNode){
_32.parentNode.removeChild(_32);
}
return;
case 8:
if(_34.indexOf("{%")==0){
var _37=dojo.trim(_34.slice(2,-2));
if(_37.substr(0,5)=="load "){
var _38=dojo.trim(_37).split(/\s+/g);
for(var i=1,_3a;_3a=_38[i];i++){
dojo["require"](_3a);
}
}
_33.push([dd.TOKEN_BLOCK,_37]);
}
if(_34.indexOf("{{")==0){
_33.push([dd.TOKEN_VAR,dojo.trim(_34.slice(2,-2))]);
}
if(_32.parentNode){
_32.parentNode.removeChild(_32);
}
return;
}
}};
dd.HtmlTemplate=dojo.extend(function(obj){
if(!obj.nodes){
var _3c=dojo.byId(obj);
if(_3c&&_3c.nodeType==1){
dojo.forEach(["class","src","href","name","value"],function(_3d){
_3._attributes[_3d]=true;
});
obj={nodes:[_3c]};
}else{
if(typeof obj=="object"){
obj=_2.getTemplateString(obj);
}
obj=_3.getTemplate(obj);
}
}
var _3e=_3.tokenize(obj.nodes);
if(dd.tests){
this.tokens=_3e.slice(0);
}
var _3f=new dd._HtmlParser(_3e);
this.nodelist=_3f.parse();
},{_count:0,_re:/\bdojo:([a-zA-Z0-9_]+)\b/g,setClass:function(str){
this.getRootNode().className=str;
},getRootNode:function(){
return this.buffer.rootNode;
},getBuffer:function(){
return new dd.HtmlBuffer();
},render:function(_41,_42){
_42=this.buffer=_42||this.getBuffer();
this.rootNode=null;
var _43=this.nodelist.render(_41||new dd.Context({}),_42);
for(var i=0,_45;_45=_42._cache[i];i++){
if(_45._cache){
_45._cache.length=0;
}
}
return _43;
},unrender:function(_46,_47){
return this.nodelist.unrender(_46,_47);
}});
dd.HtmlBuffer=dojo.extend(function(_48){
this._parent=_48;
this._cache=[];
},{concat:function(_49){
var _4a=this._parent;
if(_4a&&_49.parentNode&&_49.parentNode===_4a&&!_4a._dirty){
return this;
}
if(_49.nodeType==1&&!this.rootNode){
this.rootNode=_49||true;
return this;
}
if(!_4a){
if(_49.nodeType==3&&dojo.trim(_49.data)){
throw new Error("Text should not exist outside of the root node in template");
}
return this;
}
if(this._closed){
if(_49.nodeType==3&&!dojo.trim(_49.data)){
return this;
}else{
throw new Error("Content should not exist outside of the root node in template");
}
}
if(_4a._dirty){
if(_49._drawn&&_49.parentNode==_4a){
var _4b=_4a._cache;
if(_4b){
for(var i=0,_4d;_4d=_4b[i];i++){
this.onAddNode&&this.onAddNode(_4d);
_4a.insertBefore(_4d,_49);
this.onAddNodeComplete&&this.onAddNodeComplete(_4d);
}
_4b.length=0;
}
}
_4a._dirty=false;
}
if(!_4a._cache){
_4a._cache=[];
this._cache.push(_4a);
}
_4a._dirty=true;
_4a._cache.push(_49);
return this;
},remove:function(obj){
if(typeof obj=="string"){
if(this._parent){
this._parent.removeAttribute(obj);
}
}else{
if(obj.nodeType==1&&!this.getRootNode()&&!this._removed){
this._removed=true;
return this;
}
if(obj.parentNode){
this.onRemoveNode&&this.onRemoveNode(obj);
if(obj.parentNode){
obj.parentNode.removeChild(obj);
}
}
}
return this;
},setAttribute:function(key,_50){
var old=dojo.attr(this._parent,key);
if(this.onChangeAttribute&&old!=_50){
this.onChangeAttribute(this._parent,key,old,_50);
}
dojo.attr(this._parent,key,_50);
return this;
},addEvent:function(_52,_53,fn,_55){
if(!_52.getThis()){
throw new Error("You must use Context.setObject(instance)");
}
this.onAddEvent&&this.onAddEvent(this.getParent(),_53,fn);
var _56=fn;
if(dojo.isArray(_55)){
_56=function(e){
this[fn].apply(this,[e].concat(_55));
};
}
return dojo.connect(this.getParent(),_53,_52.getThis(),_56);
},setParent:function(_58,up,_5a){
if(!this._parent){
this._parent=this._first=_58;
}
if(up&&_5a&&_58===this._first){
this._closed=true;
}
if(up){
var _5b=this._parent;
var _5c="";
var ie=dojo.isIE&&_5b.tagName=="SCRIPT";
if(ie){
_5b.text="";
}
if(_5b._dirty){
var _5e=_5b._cache;
var _5f=(_5b.tagName=="SELECT"&&!_5b.options.length);
for(var i=0,_61;_61=_5e[i];i++){
if(_61!==_5b){
this.onAddNode&&this.onAddNode(_61);
if(ie){
_5c+=_61.data;
}else{
_5b.appendChild(_61);
if(_5f&&_61.defaultSelected&&i){
_5f=i;
}
}
this.onAddNodeComplete&&this.onAddNodeComplete(_61);
}
}
if(_5f){
_5b.options.selectedIndex=(typeof _5f=="number")?_5f:0;
}
_5e.length=0;
_5b._dirty=false;
}
if(ie){
_5b.text=_5c;
}
}
this._parent=_58;
this.onSetParent&&this.onSetParent(_58,up,_5a);
return this;
},getParent:function(){
return this._parent;
},getRootNode:function(){
return this.rootNode;
}});
dd._HtmlNode=dojo.extend(function(_62){
this.contents=_62;
},{render:function(_63,_64){
this._rendered=true;
return _64.concat(this.contents);
},unrender:function(_65,_66){
if(!this._rendered){
return _66;
}
this._rendered=false;
return _66.remove(this.contents);
},clone:function(_67){
return new this.constructor(this.contents);
}});
dd._HtmlNodeList=dojo.extend(function(_68){
this.contents=_68||[];
},{push:function(_69){
this.contents.push(_69);
},unshift:function(_6a){
this.contents.unshift(_6a);
},render:function(_6b,_6c,_6d){
_6c=_6c||dd.HtmlTemplate.prototype.getBuffer();
if(_6d){
var _6e=_6c.getParent();
}
for(var i=0;i<this.contents.length;i++){
_6c=this.contents[i].render(_6b,_6c);
if(!_6c){
throw new Error("Template node render functions must return their buffer");
}
}
if(_6e){
_6c.setParent(_6e);
}
return _6c;
},dummyRender:function(_70,_71,_72){
var div=document.createElement("div");
var _74=_71.getParent();
var old=_74._clone;
_74._clone=div;
var _76=this.clone(_71,div);
if(old){
_74._clone=old;
}else{
_74._clone=null;
}
_71=dd.HtmlTemplate.prototype.getBuffer();
_76.unshift(new dd.ChangeNode(div));
_76.unshift(new dd._HtmlNode(div));
_76.push(new dd.ChangeNode(div,true));
_76.render(_70,_71);
if(_72){
return _71.getRootNode();
}
var _77=div.innerHTML;
return (dojo.isIE)?_77.replace(/\s*_(dirty|clone)="[^"]*"/g,""):_77;
},unrender:function(_78,_79,_7a){
if(_7a){
var _7b=_79.getParent();
}
for(var i=0;i<this.contents.length;i++){
_79=this.contents[i].unrender(_78,_79);
if(!_79){
throw new Error("Template node render functions must return their buffer");
}
}
if(_7b){
_79.setParent(_7b);
}
return _79;
},clone:function(_7d){
var _7e=_7d.getParent();
var _7f=this.contents;
var _80=new dd._HtmlNodeList();
var _81=[];
for(var i=0;i<_7f.length;i++){
var _83=_7f[i].clone(_7d);
if(_83 instanceof dd.ChangeNode||_83 instanceof dd._HtmlNode){
var _84=_83.contents._clone;
if(_84){
_83.contents=_84;
}else{
if(_7e!=_83.contents&&_83 instanceof dd._HtmlNode){
var _85=_83.contents;
_83.contents=_83.contents.cloneNode(false);
_7d.onClone&&_7d.onClone(_85,_83.contents);
_81.push(_85);
_85._clone=_83.contents;
}
}
}
_80.push(_83);
}
for(var i=0,_83;_83=_81[i];i++){
_83._clone=null;
}
return _80;
},rtrim:function(){
while(1){
var i=this.contents.length-1;
if(this.contents[i] instanceof dd._HtmlTextNode&&this.contents[i].isEmpty()){
this.contents.pop();
}else{
break;
}
}
return this;
}});
dd._HtmlVarNode=dojo.extend(function(str){
this.contents=new dd._Filter(str);
},{render:function(_88,_89){
var str=this.contents.resolve(_88);
var _8b="text";
if(str){
if(str.render&&str.getRootNode){
_8b="injection";
}else{
if(str.safe){
if(str.nodeType){
_8b="node";
}else{
if(str.toString){
str=str.toString();
_8b="html";
}
}
}
}
}
if(this._type&&_8b!=this._type){
this.unrender(_88,_89);
}
this._type=_8b;
switch(_8b){
case "text":
this._rendered=true;
this._txt=this._txt||document.createTextNode(str);
if(this._txt.data!=str){
var old=this._txt.data;
this._txt.data=str;
_89.onChangeData&&_89.onChangeData(this._txt,old,this._txt.data);
}
return _89.concat(this._txt);
case "injection":
var _8d=str.getRootNode();
if(this._rendered&&_8d!=this._root){
_89=this.unrender(_88,_89);
}
this._root=_8d;
var _8e=this._injected=new dd._HtmlNodeList();
_8e.push(new dd.ChangeNode(_89.getParent()));
_8e.push(new dd._HtmlNode(_8d));
_8e.push(str);
_8e.push(new dd.ChangeNode(_89.getParent()));
this._rendered=true;
return _8e.render(_88,_89);
case "node":
this._rendered=true;
if(this._node&&this._node!=str&&this._node.parentNode&&this._node.parentNode===_89.getParent()){
this._node.parentNode.removeChild(this._node);
}
this._node=str;
return _89.concat(str);
case "html":
if(this._rendered&&this._src!=str){
_89=this.unrender(_88,_89);
}
this._src=str;
if(!this._rendered){
this._rendered=true;
this._html=this._html||[];
var div=(this._div=this._div||document.createElement("div"));
div.innerHTML=str;
var _90=div.childNodes;
while(_90.length){
var _91=div.removeChild(_90[0]);
this._html.push(_91);
_89=_89.concat(_91);
}
}
return _89;
default:
return _89;
}
},unrender:function(_92,_93){
if(!this._rendered){
return _93;
}
this._rendered=false;
switch(this._type){
case "text":
return _93.remove(this._txt);
case "injection":
return this._injection.unrender(_92,_93);
case "node":
if(this._node.parentNode===_93.getParent()){
return _93.remove(this._node);
}
return _93;
case "html":
for(var i=0,l=this._html.length;i<l;i++){
_93=_93.remove(this._html[i]);
}
return _93;
default:
return _93;
}
},clone:function(){
return new this.constructor(this.contents.getExpression());
}});
dd.ChangeNode=dojo.extend(function(_96,up,_98){
this.contents=_96;
this.up=up;
this.root=_98;
},{render:function(_99,_9a){
return _9a.setParent(this.contents,this.up,this.root);
},unrender:function(_9b,_9c){
if(!_9c.getParent()){
return _9c;
}
return _9c.setParent(this.contents);
},clone:function(){
return new this.constructor(this.contents,this.up,this.root);
}});
dd.AttributeNode=dojo.extend(function(key,_9e){
this.key=key;
this.value=_9e;
this.contents=_9e;
if(this._pool[_9e]){
this.nodelist=this._pool[_9e];
}else{
if(!(this.nodelist=dd.quickFilter(_9e))){
this.nodelist=(new dd.Template(_9e,true)).nodelist;
}
this._pool[_9e]=this.nodelist;
}
this.contents="";
},{_pool:{},render:function(_9f,_a0){
var key=this.key;
var _a2=this.nodelist.dummyRender(_9f);
if(dd.BOOLS[key]){
_a2=!(_a2=="false"||_a2=="undefined"||!_a2);
}
if(_a2!==this.contents){
this.contents=_a2;
return _a0.setAttribute(key,_a2);
}
return _a0;
},unrender:function(_a3,_a4){
return _a4.remove(this.key);
},clone:function(_a5){
return new this.constructor(this.key,this.value);
}});
dd._HtmlTextNode=dojo.extend(function(str){
this.contents=document.createTextNode(str);
this.upcoming=str;
},{set:function(_a7){
this.upcoming=_a7;
return this;
},render:function(_a8,_a9){
if(this.contents.data!=this.upcoming){
var old=this.contents.data;
this.contents.data=this.upcoming;
_a9.onChangeData&&_a9.onChangeData(this.contents,old,this.upcoming);
}
return _a9.concat(this.contents);
},unrender:function(_ab,_ac){
return _ac.remove(this.contents);
},isEmpty:function(){
return !dojo.trim(this.contents.data);
},clone:function(){
return new this.constructor(this.contents.data);
}});
dd._HtmlParser=dojo.extend(function(_ad){
this.contents=_ad;
},{i:0,parse:function(_ae){
var _af={};
var _b0=this.contents;
if(!_ae){
_ae=[];
}
for(var i=0;i<_ae.length;i++){
_af[_ae[i]]=true;
}
var _b2=new dd._HtmlNodeList();
while(this.i<_b0.length){
var _b3=_b0[this.i++];
var _b4=_b3[0];
var _b5=_b3[1];
if(_b4==dd.TOKEN_CUSTOM){
_b2.push(_b5);
}else{
if(_b4==dd.TOKEN_CHANGE){
var _b6=new dd.ChangeNode(_b5,_b3[2],_b3[3]);
_b5[_b6.attr]=_b6;
_b2.push(_b6);
}else{
if(_b4==dd.TOKEN_ATTR){
var fn=_2.getTag("attr:"+_b3[2],true);
if(fn&&_b3[3]){
_b2.push(fn(null,new dd.Token(_b4,_b3[2]+" "+_b3[3])));
}else{
if(dojo.isString(_b3[3])&&(_b3[3].indexOf("{%")!=-1||_b3[3].indexOf("{{")!=-1)){
_b2.push(new dd.AttributeNode(_b3[2],_b3[3]));
}
}
}else{
if(_b4==dd.TOKEN_NODE){
var fn=_2.getTag("node:"+_b5.tagName.toLowerCase(),true);
if(fn){
_b2.push(fn(null,new dd.Token(_b4,_b5),_b5.tagName.toLowerCase()));
}
_b2.push(new dd._HtmlNode(_b5));
}else{
if(_b4==dd.TOKEN_VAR){
_b2.push(new dd._HtmlVarNode(_b5));
}else{
if(_b4==dd.TOKEN_TEXT){
_b2.push(new dd._HtmlTextNode(_b5.data||_b5));
}else{
if(_b4==dd.TOKEN_BLOCK){
if(_af[_b5]){
--this.i;
return _b2;
}
var cmd=_b5.split(/\s+/g);
if(cmd.length){
cmd=cmd[0];
var fn=_2.getTag(cmd);
if(typeof fn!="function"){
throw new Error("Function not found for "+cmd);
}
var tpl=fn(this,new dd.Token(_b4,_b5));
if(tpl){
_b2.push(tpl);
}
}
}
}
}
}
}
}
}
}
if(_ae.length){
throw new Error("Could not find closing tag(s): "+_ae.toString());
}
return _b2;
},next_token:function(){
var _ba=this.contents[this.i++];
return new dd.Token(_ba[0],_ba[1]);
},delete_first_token:function(){
this.i++;
},skip_past:function(_bb){
return dd._Parser.prototype.skip_past.call(this,_bb);
},create_variable_node:function(_bc){
return new dd._HtmlVarNode(_bc);
},create_text_node:function(_bd){
return new dd._HtmlTextNode(_bd||"");
},getTemplate:function(loc){
return new dd.HtmlTemplate(_3.getTemplate(loc));
}});
})();
}
