123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- /*
- Copyright (c) 2004-2012, 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["dijit._editor.html"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
- dojo._hasResource["dijit._editor.html"] = true;
- dojo.provide("dijit._editor.html");
- var exports = dojo.getObject("_editor", true, dijit);
- var escape = exports.escapeXml=function(/*String*/str, /*Boolean?*/noSingleQuotes){
- // summary:
- // Adds escape sequences for special characters in XML: &<>"'
- // Optionally skips escapes for single quotes
- str = str.replace(/&/gm, "&").replace(/</gm, "<").replace(/>/gm, ">").replace(/"/gm, """);
- if(!noSingleQuotes){
- str = str.replace(/'/gm, "'");
- }
- return str; // string
- };
- exports.getNodeHtml = function(/*DomNode*/ node){
- // summary:
- // Return string representing HTML for node and it's children
- var output = [];
- exports.getNodeHtmlHelper(node, output);
- return output.join("");
- };
- exports.getNodeHtmlHelper = function(/*DomNode*/ node, /*String[]*/ output){
- // summary:
- // Pushes array of strings into output[] which represent HTML for node and it's children
- switch(node.nodeType){
- case 1: //element node
- var lName = node.nodeName.toLowerCase();
- if(!lName || lName.charAt(0) == "/"){
- // IE does some strange things with malformed HTML input, like
- // treating a close tag </span> without an open tag <span>, as
- // a new tag with tagName of /span. Corrupts output HTML, remove
- // them. Other browsers don't prefix tags that way, so will
- // never show up.
- return "";
- }
- output.push('<', lName);
- //store the list of attributes and sort it to have the
- //attributes appear in the dictionary order
- var attrarray = [];
- var attr;
- if(dojo.isIE < 9){
- var clone = /^input$|^img$/i.test(node.nodeName) ? node : node.cloneNode(false);
- var s = clone.outerHTML;
- s = s.substr(0, s.indexOf('>'))
- .replace(/(['"])[^"']*\1/g, ''); //to make the following regexp safe
- var reg = /(\b\w+)\s?=/g;
- var m, key;
- while((m = reg.exec(s))){
- key = m[1];
- if(key.substr(0,3) != '_dj'){
- if(key == 'src' || key == 'href'){
- if(node.getAttribute('_djrealurl')){
- attrarray.push([key,node.getAttribute('_djrealurl')]);
- continue;
- }
- }
- var val, match;
- switch(key){
- case 'style':
- val = node.style.cssText.toLowerCase();
- break;
- case 'class':
- val = node.className;
- break;
- case 'width':
- if(lName === "img"){
- // This somehow gets lost on IE for IMG tags and the like
- // and we have to find it in outerHTML, known IE oddity.
- match=/width=(\S+)/i.exec(s);
- if(match){
- val = match[1];
- }
- break;
- }
- case 'height':
- if(lName === "img"){
- // This somehow gets lost on IE for IMG tags and the like
- // and we have to find it in outerHTML, known IE oddity.
- match=/height=(\S+)/i.exec(s);
- if(match){
- val = match[1];
- }
- break;
- }
- default:
- val = node.getAttribute(key);
- }
- if(val != null){
- attrarray.push([key, val.toString()]);
- }
- }
- }
- }else{
- var i = 0;
- while((attr = node.attributes[i++])){
- //ignore all attributes starting with _dj which are
- //internal temporary attributes used by the editor
- var n = attr.name;
- if(n.substr(0,3) != '_dj' /*&&
- (attr.specified == undefined || attr.specified)*/){
- var v = attr.value;
- if(n == 'src' || n == 'href'){
- if(node.getAttribute('_djrealurl')){
- v = node.getAttribute('_djrealurl');
- }
- }
- attrarray.push([n,v]);
- }
- }
- }
- attrarray.sort(function(a,b){
- return a[0] < b[0] ? -1 : (a[0] == b[0] ? 0 : 1);
- });
- var j = 0;
- while((attr = attrarray[j++])){
- output.push(' ', attr[0], '="',
- (dojo.isString(attr[1]) ? escape(attr[1], true) : attr[1]), '"');
- }
- switch(lName){
- case 'br':
- case 'hr':
- case 'img':
- case 'input':
- case 'base':
- case 'meta':
- case 'area':
- case 'basefont':
- // These should all be singly closed
- output.push(' />');
- break;
- case 'script':
- // Browsers handle script tags differently in how you get content,
- // but innerHTML always seems to work, so insert its content that way
- // Yes, it's bad to allow script tags in the editor code, but some people
- // seem to want to do it, so we need to at least return them right.
- // other plugins/filters can strip them.
- output.push('>', node.innerHTML, '</', lName, '>');
- break;
- default:
- output.push('>');
- if(node.hasChildNodes()){
- exports.getChildrenHtmlHelper(node, output);
- }
- output.push('</', lName, '>');
- }
- break;
- case 4: // cdata
- case 3: // text
- // FIXME:
- output.push(escape(node.nodeValue, true));
- break;
- case 8: //comment
- // FIXME:
- output.push('<!--', escape(node.nodeValue, true), '-->');
- break;
- default:
- output.push("<!-- Element not recognized - Type: ", node.nodeType, " Name: ", node.nodeName, "-->");
- }
- };
- exports.getChildrenHtml = function(/*DomNode*/ node){
- // summary:
- // Returns the html content of a DomNode's children
- var output = [];
- exports.getChildrenHtmlHelper(node, output);
- return output.join("");
- };
- exports.getChildrenHtmlHelper = function(/*DomNode*/ dom, /*String[]*/ output){
- // summary:
- // Pushes the html content of a DomNode's children into out[]
- if(!dom){ return; }
- var nodes = dom["childNodes"] || dom;
- //IE issue.
- //If we have an actual node we can check parent relationships on for IE,
- //We should check, as IE sometimes builds invalid DOMS. If no parent, we can't check
- //And should just process it and hope for the best.
- var checkParent = !dojo.isIE || nodes !== dom;
- var node, i = 0;
- while((node = nodes[i++])){
- //IE is broken. DOMs are supposed to be a tree. But in the case of malformed HTML, IE generates a graph
- //meaning one node ends up with multiple references (multiple parents). This is totally wrong and invalid, but
- //such is what it is. We have to keep track and check for this because otherwise the source output HTML will have dups.
- //No other browser generates a graph. Leave it to IE to break a fundamental DOM rule. So, we check the parent if we can
- //If we can't, nothing more we can do other than walk it.
- if(!checkParent || node.parentNode == dom){
- exports.getNodeHtmlHelper(node, output);
- }
- }
- };
- }
|