Debian Codegraph @

dijit/_editor/html.js

raw symbols SCIP
1 define([
2 "dojo/_base/array",
3 "dojo/_base/lang", // lang.setObject
4 "dojo/sniff" // has("ie")
5 ], function(array, lang, has){
6
7 // module:
8 // dijit/_editor/html
9
10 var exports = {
11 // summary:
12 // HTML serialization utility functions used by editor
13 };
14 lang.setObject("dijit._editor.html", exports);
15
16 var escape = exports.escapeXml = function(/*String*/ str, /*Boolean?*/ noSingleQuotes){
17 // summary:
18 // Adds escape sequences for special characters in XML: `&<>"'`.
19 // Optionally skips escapes for single quotes.
20 str = str.replace(/&/gm, "&amp;").replace(/</gm, "&lt;").replace(/>/gm, "&gt;").replace(/"/gm, "&quot;");
21 if(!noSingleQuotes){
22 str = str.replace(/'/gm, "&#39;");
23 }
24 return str; // string
25 };
26
27 exports.getNodeHtml = function(/*DomNode*/ node){
28 // summary:
29 // Return string representing HTML for node and it's children
30 var output = [];
31 exports.getNodeHtmlHelper(node, output);
32 return output.join("");
33 };
34
35 exports.getNodeHtmlHelper = function(/*DomNode*/ node, /*String[]*/ output){
36 // summary:
37 // Pushes array of strings into output[] which represent HTML for node and it's children
38 switch(node.nodeType){
39 case 1: // element node
40 var lName = node.nodeName.toLowerCase();
41 if(!lName || lName.charAt(0) == "/"){
42 // IE does some strange things with malformed HTML input, like
43 // treating a close tag </span> without an open tag <span>, as
44 // a new tag with tagName of /span. Corrupts output HTML, remove
45 // them. Other browsers don't prefix tags that way, so will
46 // never show up.
47 return "";
48 }
49 output.push('<', lName);
50
51 // store the list of attributes and sort it to have the
52 // attributes appear in the dictionary order
53 var attrarray = [], attrhash = {};
54 var attr;
55 if(has("dom-attributes-explicit") || has("dom-attributes-specified-flag")){
56 // IE8+ and all other browsers.
57 var i = 0;
58 while((attr = node.attributes[i++])){
59 // ignore all attributes starting with _dj which are
60 // internal temporary attributes used by the editor
61 var n = attr.name;
62 if(n.substr(0, 3) !== '_dj' &&
63 (!has("dom-attributes-specified-flag") || attr.specified) && !(n in attrhash)){ // workaround repeated attributes bug in IE8 (LinkDialog test)
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 // IE6-7 code path
79 var clone = /^input$|^img$/i.test(node.nodeName) ? node : node.cloneNode(false);
80 var s = clone.outerHTML;
81 // Split up and manage the attrs via regexp
82 // similar to prettyPrint attr logic.
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 // This somehow gets lost on IE for IMG tags and the like
109 // and we have to find it in outerHTML, known IE oddity.
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 // This somehow gets lost on IE for IMG tags and the like
119 // and we have to find it in outerHTML, known IE oddity.
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 // These should all be singly closed
155 output.push(' />');
156 break;
157 case 'script':
158 // Browsers handle script tags differently in how you get content,
159 // but innerHTML always seems to work, so insert its content that way
160 // Yes, it's bad to allow script tags in the editor code, but some people
161 // seem to want to do it, so we need to at least return them right.
162 // other plugins/filters can strip them.
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: // cdata
174 case 3: // text
175 // FIXME:
176 output.push(escape(node.nodeValue, true));
177 break;
178 case 8: // comment
179 // FIXME:
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(/*DomNode*/ node){
188 // summary:
189 // Returns the html content of a DomNode's children
190 var output = [];
191 exports.getChildrenHtmlHelper(node, output);
192 return output.join("");
193 };
194
195 exports.getChildrenHtmlHelper = function(/*DomNode*/ dom, /*String[]*/ output){
196 // summary:
197 // Pushes the html content of a DomNode's children into out[]
198
199 if(!dom){
200 return;
201 }
202 var nodes = dom["childNodes"] || dom;
203
204 // IE issue.
205 // If we have an actual node we can check parent relationships on for IE,
206 // We should check, as IE sometimes builds invalid DOMS. If no parent, we can't check
207 // And should just process it and hope for the best.
208 var checkParent = !has("ie") || nodes !== dom;
209
210 var node, i = 0;
211 while((node = nodes[i++])){
212 // IE is broken. DOMs are supposed to be a tree. But in the case of malformed HTML, IE generates a graph
213 // meaning one node ends up with multiple references (multiple parents). This is totally wrong and invalid, but
214 // such is what it is. We have to keep track and check for this because otherwise the source output HTML will have dups.
215 // No other browser generates a graph. Leave it to IE to break a fundamental DOM rule. So, we check the parent if we can
216 // If we can't, nothing more we can do other than walk it.
217 if(!checkParent || node.parentNode == dom){
218 exports.getNodeHtmlHelper(node, output);
219 }
220 }
221 };
222
223 return exports;
224 });