| 1 |
define([ |
| 2 |
"dojo/_base/array", |
| 3 |
"dojo/_base/lang", |
| 4 |
"dojo/sniff" |
| 5 |
], function(array, lang, has){ |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
var exports = { |
| 11 |
|
| 12 |
|
| 13 |
}; |
| 14 |
lang.setObject("dijit._editor.html", exports); |
| 15 |
|
| 16 |
var escape = exports.escapeXml = function( str, noSingleQuotes){ |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
str = str.replace(/&/gm, "&").replace(/</gm, "<").replace(/>/gm, ">").replace(/"/gm, """); |
| 21 |
if(!noSingleQuotes){ |
| 22 |
str = str.replace(/'/gm, "'"); |
| 23 |
} |
| 24 |
return str; |
| 25 |
}; |
| 26 |
|
| 27 |
exports.getNodeHtml = function( node){ |
| 28 |
|
| 29 |
|
| 30 |
var output = []; |
| 31 |
exports.getNodeHtmlHelper(node, output); |
| 32 |
return output.join(""); |
| 33 |
}; |
| 34 |
|
| 35 |
exports.getNodeHtmlHelper = function( node, output){ |
| 36 |
|
| 37 |
|
| 38 |
switch(node.nodeType){ |
| 39 |
case 1: |
| 40 |
var lName = node.nodeName.toLowerCase(); |
| 41 |
if(!lName || lName.charAt(0) == "/"){ |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
return ""; |
| 48 |
} |
| 49 |
output.push('<', lName); |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
var attrarray = [], attrhash = {}; |
| 54 |
var attr; |
| 55 |
if(has("dom-attributes-explicit") || has("dom-attributes-specified-flag")){ |
| 56 |
|
| 57 |
var i = 0; |
| 58 |
while((attr = node.attributes[i++])){ |
| 59 |
|
| 60 |
|
| 61 |
var n = attr.name; |
| 62 |
if(n.substr(0, 3) !== '_dj' && |
| 63 |
(!has("dom-attributes-specified-flag") || attr.specified) && !(n in attrhash)){ |
| 64 |
var v = attr.value; |
| 65 |
if(n == 'src' || n == 'href'){ |
| 66 |
if(node.getAttribute('_djrealurl')){ |
| 67 |
v = node.getAttribute('_djrealurl'); |
| 68 |
} |
| 69 |
} |
| 70 |
if(has("ie") === 8 && n === "style"){ |
| 71 |
v = v.replace("HEIGHT:", "height:").replace("WIDTH:", "width:"); |
| 72 |
} |
| 73 |
attrarray.push([n, v]); |
| 74 |
attrhash[n] = v; |
| 75 |
} |
| 76 |
} |
| 77 |
}else{ |
| 78 |
|
| 79 |
var clone = /^input$|^img$/i.test(node.nodeName) ? node : node.cloneNode(false); |
| 80 |
var s = clone.outerHTML; |
| 81 |
|
| 82 |
|
| 83 |
var rgxp_attrsMatch = /[\w-]+=("[^"]*"|'[^']*'|\S*)/gi |
| 84 |
var attrSplit = s.match(rgxp_attrsMatch); |
| 85 |
s = s.substr(0, s.indexOf('>')); |
| 86 |
array.forEach(attrSplit, function(attr){ |
| 87 |
if(attr){ |
| 88 |
var idx = attr.indexOf("="); |
| 89 |
if(idx > 0){ |
| 90 |
var key = attr.substring(0, idx); |
| 91 |
if(key.substr(0, 3) != '_dj'){ |
| 92 |
if(key == 'src' || key == 'href'){ |
| 93 |
if(node.getAttribute('_djrealurl')){ |
| 94 |
attrarray.push([key, node.getAttribute('_djrealurl')]); |
| 95 |
return; |
| 96 |
} |
| 97 |
} |
| 98 |
var val, match; |
| 99 |
switch(key){ |
| 100 |
case 'style': |
| 101 |
val = node.style.cssText.toLowerCase(); |
| 102 |
break; |
| 103 |
case 'class': |
| 104 |
val = node.className; |
| 105 |
break; |
| 106 |
case 'width': |
| 107 |
if(lName === "img"){ |
| 108 |
|
| 109 |
|
| 110 |
match = /width=(\S+)/i.exec(s); |
| 111 |
if(match){ |
| 112 |
val = match[1]; |
| 113 |
} |
| 114 |
break; |
| 115 |
} |
| 116 |
case 'height': |
| 117 |
if(lName === "img"){ |
| 118 |
|
| 119 |
|
| 120 |
match = /height=(\S+)/i.exec(s); |
| 121 |
if(match){ |
| 122 |
val = match[1]; |
| 123 |
} |
| 124 |
break; |
| 125 |
} |
| 126 |
default: |
| 127 |
val = node.getAttribute(key); |
| 128 |
} |
| 129 |
if(val != null){ |
| 130 |
attrarray.push([key, val.toString()]); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
}, this); |
| 136 |
} |
| 137 |
attrarray.sort(function(a, b){ |
| 138 |
return a[0] < b[0] ? -1 : (a[0] == b[0] ? 0 : 1); |
| 139 |
}); |
| 140 |
var j = 0; |
| 141 |
while((attr = attrarray[j++])){ |
| 142 |
output.push(' ', attr[0], '="', |
| 143 |
(typeof attr[1] === "string" ? escape(attr[1], true) : attr[1]), '"'); |
| 144 |
} |
| 145 |
switch(lName){ |
| 146 |
case 'br': |
| 147 |
case 'hr': |
| 148 |
case 'img': |
| 149 |
case 'input': |
| 150 |
case 'base': |
| 151 |
case 'meta': |
| 152 |
case 'area': |
| 153 |
case 'basefont': |
| 154 |
|
| 155 |
output.push(' />'); |
| 156 |
break; |
| 157 |
case 'script': |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
output.push('>', node.innerHTML, '</', lName, '>'); |
| 164 |
break; |
| 165 |
default: |
| 166 |
output.push('>'); |
| 167 |
if(node.hasChildNodes()){ |
| 168 |
exports.getChildrenHtmlHelper(node, output); |
| 169 |
} |
| 170 |
output.push('</', lName, '>'); |
| 171 |
} |
| 172 |
break; |
| 173 |
case 4: |
| 174 |
case 3: |
| 175 |
|
| 176 |
output.push(escape(node.nodeValue, true)); |
| 177 |
break; |
| 178 |
case 8: |
| 179 |
|
| 180 |
output.push('<!--', escape(node.nodeValue, true), '-->'); |
| 181 |
break; |
| 182 |
default: |
| 183 |
output.push("<!-- Element not recognized - Type: ", node.nodeType, " Name: ", node.nodeName, "-->"); |
| 184 |
} |
| 185 |
}; |
| 186 |
|
| 187 |
exports.getChildrenHtml = function( node){ |
| 188 |
|
| 189 |
|
| 190 |
var output = []; |
| 191 |
exports.getChildrenHtmlHelper(node, output); |
| 192 |
return output.join(""); |
| 193 |
}; |
| 194 |
|
| 195 |
exports.getChildrenHtmlHelper = function( dom, output){ |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
if(!dom){ |
| 200 |
return; |
| 201 |
} |
| 202 |
var nodes = dom["childNodes"] || dom; |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
var checkParent = !has("ie") || nodes !== dom; |
| 209 |
|
| 210 |
var node, i = 0; |
| 211 |
while((node = nodes[i++])){ |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
if(!checkParent || node.parentNode == dom){ |
| 218 |
exports.getNodeHtmlHelper(node, output); |
| 219 |
} |
| 220 |
} |
| 221 |
}; |
| 222 |
|
| 223 |
return exports; |
| 224 |
}); |