source: [view]
if(typeof this._commentable == "undefined"){
// Check to see if the browser can handle comments
this._commentable = false;
var div = document.createElement("div");
div.innerHTML = "";
if(div.childNodes.length && div.childNodes[0].nodeType == 8 && div.childNodes[0].data == "comment"){
this._commentable = true;
}
}
if(!this._commentable){
// Strip comments
text = text.replace(//g, "$1");
}
if(dojo.isIE){
text = text.replace(/\b(checked|disabled|readonly|style)="/g, 't$1="');
}
text = text.replace(/\bstyle="/g, 'tstyle="');
var match;
var table = dojo.isWebKit;
var pairs = [ // Format: [enable, parent, allowed children (first for nesting), nestings]
[true, "select", "option"],
[table, "tr", "td|th"],
[table, "thead", "tr", "th"],
[table, "tbody", "tr", "td"],
[table, "table", "tbody|thead|tr", "tr", "td"]
];
var replacements = [];
// Some tags can't contain text. So we wrap the text in tags that they can have.
for(var i = 0, pair; pair = pairs[i]; i++){
if(!pair[0]){
continue;
}
if(text.indexOf("<" + pair[1]) != -1){
var selectRe = new RegExp("<" + pair[1] + "(?:.|\n)*?>((?:.|\n)+?)" + pair[1] + ">", "ig");
tagLoop: while(match = selectRe.exec(text)){
// Do it like this to make sure we don't double-wrap
var inners = pair[2].split("|");
var innerRe = [];
for(var j = 0, inner; inner = inners[j]; j++){
innerRe.push("<" + inner + "(?:.|\n)*?>(?:.|\n)*?" + inner + ">");
}
var tags = [];
var tokens = dojox.string.tokenize(match[1], new RegExp("(" + innerRe.join("|") + ")", "ig"), function(data){
var tag = /<(\w+)/.exec(data)[1];
if(!tags[tag]){
tags[tag] = true;
tags.push(tag);
}
return {data: data};
});
if(tags.length){
var tag = (tags.length == 1) ? tags[0] : pair[2].split("|")[0];
var replace = [];
for(var j = 0, jl = tokens.length; j < jl; j++) {
var token = tokens[j];
if(dojo.isObject(token)){
replace.push(token.data);
}else{
var stripped = token.replace(this._reTrim, "");
if(!stripped){ continue; }
token = stripped.split(this._reSplit);
for(var k = 0, kl = token.length; k < kl; k++){
var replacement = "";
for(var p = 2, pl = pair.length; p < pl; p++){
if(p == 2){
replacement += "<" + tag + ' dtlinstruction="{% ' + token[k].replace('"', '\\"') + ' %}">';
}else if(tag == pair[p]) {
continue;
}else{
replacement += "<" + pair[p] + ">";
}
}
replacement += "DTL";
for(var p = pair.length - 1; p > 1; p--){
if(p == 2){
replacement += "" + tag + ">";
}else if(tag == pair[p]) {
continue;
}else{
replacement += "" + pair[p] + ">";
}
}
replace.push("\xFF" + replacements.length);
replacements.push(replacement);
}
}
}
text = text.replace(match[1], replace.join(""));
}
}
}
}
for(var i=replacements.length; i--;){
text = text.replace("\xFF" + i, replacements[i]);
}
var re = /\b([a-zA-Z_:][a-zA-Z0-9_\-\.:]*)=['"]/g;
while(match = re.exec(text)){
var lower = match[1].toLowerCase();
if(lower == "dtlinstruction"){ continue; }
if(lower != match[1]){
this._uppers[lower] = match[1];
}
this._attributes[lower] = true;
}
var div = document.createElement("div");
div.innerHTML = text;
var output = {nodes: []};
while(div.childNodes.length){
output.nodes.push(div.removeChild(div.childNodes[0]))
}
return output;