X-Git-Url: http://git.roojs.org/?a=blobdiff_plain;f=Roo%2FHtmlEditorCore.js;h=eee4f0510ebe13a027ce581ca569b3b7b726b364;hb=825a74c132eae7b97e43866ab3c00ff122fb239c;hp=017fda02aa46dfe63466072a816b7fa03d9556cf;hpb=3132b71b39ea6571756da5e45b7cfca9befb93a9;p=roojs1 diff --git a/Roo/HtmlEditorCore.js b/Roo/HtmlEditorCore.js index 017fda02aa..eee4f0510e 100644 --- a/Roo/HtmlEditorCore.js +++ b/Roo/HtmlEditorCore.js @@ -72,6 +72,7 @@ Roo.HtmlEditorCore = function(config){ * @param {Roo.HtmlEditorCore} this */ editorevent: true + }); // at this point this.owner is set, so we can start working out the whitelisted / blacklisted elements @@ -132,7 +133,7 @@ Roo.extend(Roo.HtmlEditorCore, Roo.Component, { black: false, white: false, - + bodyCls : '', /** * Protected method that will not generally be called directly. It @@ -142,7 +143,6 @@ Roo.extend(Roo.HtmlEditorCore, Roo.Component, { getDocMarkup : function(){ // body styles.. var st = ''; - Roo.log(this.stylesheets); // inherit styels from page...?? if (this.stylesheets === false) { @@ -160,23 +160,27 @@ Roo.extend(Roo.HtmlEditorCore, Roo.Component, { st = ''; - } else { - Roo.each(this.stylesheets, function(s) { - st += '' - }); - + } else { + st = ''; } st += ''; + var cls = 'roo-htmleditor-body'; + + if(this.bodyCls.length){ + cls += ' ' + this.bodyCls; + } return '' + st + //' + - ' '; + ' '; }, // private @@ -244,8 +248,6 @@ Roo.extend(Roo.HtmlEditorCore, Roo.Component, { }; Roo.TaskMgr.start(task); - - }, // private @@ -517,7 +519,8 @@ Roo.extend(Roo.HtmlEditorCore, Roo.Component, { this.execCmd('FontSize', v ); }, - onEditorEvent : function(e){ + onEditorEvent : function(e) + { this.owner.fireEvent('editorevent', this, e); // this.updateToolbar(); this.syncValue(); //we can not sync so often.. sync cleans, so this breaks stuff @@ -591,8 +594,6 @@ Roo.extend(Roo.HtmlEditorCore, Roo.Component, { insertAtCursor : function(text) { - - if(!this.activated){ return; } @@ -943,13 +944,16 @@ Roo.extend(Roo.HtmlEditorCore, Roo.Component, { var nodeIsBefore = ss == 1; var nodeIsAfter = ee == -1; - if (nodeIsBefore && nodeIsAfter) + if (nodeIsBefore && nodeIsAfter) { return 0; // outer - if (!nodeIsBefore && nodeIsAfter) + } + if (!nodeIsBefore && nodeIsAfter) { return 1; //right trailed. + } - if (nodeIsBefore && !nodeIsAfter) + if (nodeIsBefore && !nodeIsAfter) { return 2; // left trailed. + } // fully contined. return 3; }, @@ -1051,7 +1055,7 @@ Roo.extend(Roo.HtmlEditorCore, Roo.Component, { if (v.match(/^\./) || v.match(/^\//)) { return; } - if (v.match(/^(http|https):\/\//) || v.match(/^mailto:/)) { + if (v.match(/^(http|https):\/\//) || v.match(/^mailto:/) || v.match(/^ftp:/)) { return; } if (v.match(/^#/)) { @@ -1062,15 +1066,15 @@ Roo.extend(Roo.HtmlEditorCore, Roo.Component, { } + var cwhite = this.cwhite; + var cblack = this.cblack; + function cleanStyle(n,v) { if (v.match(/expression/)) { //XSS?? should we even bother.. node.removeAttribute(n); return; } - var cwhite = typeof(ed.cwhite) != 'undefined' && ed.cwhite !== false ? ed.cwhite : Roo.HtmlEditorCore.cwhite; - var cblack = typeof(ed.cblack) != 'undefined' && ed.cwhite !== false ? ed.cblack : Roo.HtmlEditorCore.cblack; - var parts = v.split(/;/); var clean = []; @@ -1083,7 +1087,7 @@ Roo.extend(Roo.HtmlEditorCore, Roo.Component, { var l = p.split(':').shift().replace(/\s+/g,''); l = l.replace(/^\s+/g,'').replace(/\s+$/g,''); - if ( cblack.indexOf(l) > -1) { + if ( cwhite.length && cblack.indexOf(l) > -1) { // Roo.log('(REMOVE CSS)' + node.tagName +'.' + n + ':'+l + '=' + v); //node.removeAttribute(n); return true; @@ -1140,7 +1144,7 @@ Roo.extend(Roo.HtmlEditorCore, Roo.Component, { node.className = ''; } - if (a.value.match(/body/)) { + if (a.value.match(/^body$/)) { node.className = ''; } continue; @@ -1156,27 +1160,29 @@ Roo.extend(Roo.HtmlEditorCore, Roo.Component, { }, + /** * Clean up MS wordisms... */ cleanWord : function(node) { - var _t = this; - var cleanWordChildren = function() - { - if (!node.childNodes.length) { - return; - } - for (var i = node.childNodes.length-1; i > -1 ; i--) { - _t.cleanWord(node.childNodes[i]); - } - } - - if (!node) { this.cleanWord(this.doc.body); return; } + + if( + node.nodeName == 'SPAN' && + !node.hasAttributes() && + node.childNodes.length == 1 && + node.firstChild.nodeName == "#text" + ) { + var text = ' ' + node.innerHTML + ' '; + var textNode = document.createTextNode(text); + node.parentNode.insertBefore(textNode, node); + node.parentNode.removeChild(node); + } + if (node.nodeName == "#text") { // clean up silly Windows -- stuff? return; @@ -1200,7 +1206,7 @@ Roo.extend(Roo.HtmlEditorCore, Roo.Component, { node.parentNode.insertBefore(cn, node); } node.parentNode.removeChild(node); - cleanWordChildren(); + this.iterateChildren(node, this.cleanWord); return; } // clean styles @@ -1244,11 +1250,87 @@ Roo.extend(Roo.HtmlEditorCore, Roo.Component, { node.removeAttribute('style'); } } + this.iterateChildren(node, this.cleanWord); + + + + }, + /** + * iterateChildren of a Node, calling fn each time, using this as the scole.. + * @param {DomNode} node node to iterate children of. + * @param {Function} fn method of this class to call on each item. + */ + iterateChildren : function(node, fn) + { + if (!node.childNodes.length) { + return; + } + for (var i = node.childNodes.length-1; i > -1 ; i--) { + fn.call(this, node.childNodes[i]) + } + }, + + + /** + * cleanTableWidths. + * + * Quite often pasting from word etc.. results in tables with column and widths. + * This does not work well on fluid HTML layouts - like emails. - so this code should hunt an destroy them.. + * + */ + cleanTableWidths : function(node) + { + + + if (!node) { + this.cleanTableWidths(this.doc.body); + return; + } + + // ignore list... + if (node.nodeName == "#text" || node.nodeName == "#comment") { + return; + } + Roo.log(node.tagName); + if (!node.tagName.toLowerCase().match(/^(table|td|tr)$/)) { + this.iterateChildren(node, this.cleanTableWidths); + return; + } + if (node.hasAttribute('width')) { + node.removeAttribute('width'); + } + + + if (node.hasAttribute("style")) { + // pretty basic... + + var styles = node.getAttribute("style").split(";"); + var nstyle = []; + Roo.each(styles, function(s) { + if (!s.match(/:/)) { + return; + } + var kv = s.split(":"); + if (kv[0].match(/^\s*(width|min-width)\s*$/)) { + return; + } + // what ever is left... we allow. + nstyle.push(s); + }); + node.setAttribute("style", nstyle.length ? nstyle.join(';') : ''); + if (!nstyle.length) { + node.removeAttribute('style'); + } + } - cleanWordChildren(); + this.iterateChildren(node, this.cleanTableWidths); }, + + + + domToHTML : function(currentElement, depth, nopadtext) { depth = depth || 0; @@ -1265,7 +1347,8 @@ Roo.extend(Roo.HtmlEditorCore, Roo.Component, { var tagName = Roo.util.Format.htmlEncode(currentElement.tagName); if (nodeName == '#text') { - return currentElement.nodeValue; + + return nopadtext ? currentElement.nodeValue : currentElement.nodeValue.trim(); } @@ -1317,6 +1400,7 @@ Roo.extend(Roo.HtmlEditorCore, Roo.Component, { // text if (currentElementChild.nodeName == '#text') { var toadd = Roo.util.Format.htmlEncode(currentElementChild.nodeValue); + toadd = nopadtext ? toadd : toadd.trim(); if (!nopad && toadd.length > 80) { innerHTML += "\n" + (new Array( depth + 1 )).join( " " ); } @@ -1443,6 +1527,56 @@ Roo.extend(Roo.HtmlEditorCore, Roo.Component, { this.cblack.push(tag); }, this); + }, + + setStylesheets : function(stylesheets) + { + if(typeof(stylesheets) == 'string'){ + Roo.get(this.iframe.contentDocument.head).createChild({ + tag : 'link', + rel : 'stylesheet', + type : 'text/css', + href : stylesheets + }); + + return; + } + var _this = this; + + Roo.each(stylesheets, function(s) { + if(!s.length){ + return; + } + + Roo.get(_this.iframe.contentDocument.head).createChild({ + tag : 'link', + rel : 'stylesheet', + type : 'text/css', + href : s + }); + }); + + + }, + + removeStylesheets : function() + { + var _this = this; + + Roo.each(Roo.get(_this.iframe.contentDocument.head).select('link[rel=stylesheet]', true).elements, function(s){ + s.remove(); + }); + }, + + setStyle : function(style) + { + Roo.get(this.iframe.contentDocument.head).createChild({ + tag : 'style', + type : 'text/css', + html : style + }); + + return; } // hide stuff that is not compatible