2012-11-08 104 views
-3

當我從word文檔複製文本時,當我將該文本粘貼到網頁上的cleditor時,也會複製應用的格式,這是我們的rails應用程序。如何從文檔中複製的文本中刪除格式

+0

這是一個真正的問題,從Word和Outlook粘貼帶來了不必要的每噸HTML與它 –

回答

2

cleditor google groups seems to have the answer:

下面是一個擴展我使用,其中包括Microsoft Word中的 濾波updateTextArea的if/endif垃圾和規範的 標籤XHTML/HTML5友好的人的組合: BR | b |刪除|插件| I |李|醇| p | UL

(function($) { 
$.cleditor.defaultOptions.docType = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>"; 
$.cleditor.defaultOptions.docCSSFile = AkmeWindowUtil.CONTEXT_PATH+"/embed/cleditor/jquery.cleditor.iframe.css"; 

$.cleditor.defaultOptions.updateTextArea = function(html) { //if (document.forms[0].debugArea) document.forms[0].debugArea.value = html; 
    // Normalize to xhtml/html5 common standards and only keep allowed tags. 
    var ary = html.split("<"); 
    var end = -1; 
    for (var i=0; i<ary.length; i++) { 
    if (ary[i].lastIndexOf("!--[if ", 7) === 0) { // handle Microsoft <!--[if ... <![endif]--> 
    ary[i] = ""; 
    var found = false; 
    for (i++; i<ary.length; i++) { 
    if (ary[i].lastIndexOf("![endif]-->", 11) === 0) {found = true;} 
    ary[i] = ""; 
    if (found) break; 
    } 
    if (i>=ary.length) break; 
    } 
    end = ary[i].indexOf(">"); 
    if (end == -1) continue; 
    ary[i] = ary[i].substring(0,end).toLowerCase()+ary[i].substring(end); 
    var search = ["strong>","em>","strike>","u>","br>"]; 
    var replace = ["b>","i>","del>","ins>","br/>"]; 
    for (var j=0; j<search.length; j++) { 
    var pos = ary[i].lastIndexOf(search[j], search[j].length+1); 
    if (pos == 0 || (pos == 1 && ary[i].charAt(0) == '/')) { 
    ary[i] = (pos == 1 ? "/" : "")+ replace[j] +ary[i].substring(search[j].length+pos); 
    } 
    } 
    var spellcheckerRE = /^\/?span[^\/>]*\/?>/m; 
    var cleanupRE = /^(\/?)(br|b|del|ins|i|li|ol|p|ul)[^a-zA-Z\/>]*[^\/>]*(\/?)>/m; 
    if (spellcheckerRE.test(ary[i])) { 
    ary[i] = ary[i].replace(spellcheckerRE, ""); 
    } else if (cleanupRE.test(ary[i])) { 
    ary[i] = ary[i].replace(cleanupRE, "<$1$2$3>"); 
    ary[i] = ary[i].replace(/^<p>/, ""); 
    ary[i] = ary[i].replace(/^<\/?p\/?>/, "<br/>"); 
    } else { 
    ary[i] = end+1 < ary[i].length ? ary[i].substring(end+1) : ""; 
    } 
    ary[i] = ary[i].replace(/&nbsp;/gm, ""); 
    ary[i] = ary[i].replace(/\n\n/gm, "\n"); 
    } 
    html = ary.join(""); 
    // Trim leading whitespace. 
    var trimRE = /^(\s+|&nbsp;|<br\/?>|<p>(&nbsp;)*<\/p>)+/m; 
    if (trimRE.test(html)) { 
    html = html.replace(trimRE, ""); 
    } 
    // Test if there is any actual non-whitespace text content. 
    var body = document.getElementsByTagName("body")[0]; 
    var div = document.createElement("div"); 
    div.style.display = "none"; 
    body.appendChild(div); 
    div.innerHTML = html; 
    var text = div.innerText || div.textContent; 
    body.removeChild(div); 
    var trimRE = /\S/m; 
    if (!trimRE.test(text)) html = ""; 
    return html; 
}; 
})(jQuery); 
+0

我人所以建議ckeditor,我們在我們的項目中使用它,在這些項目中,祕書可以毫無問題地粘貼單詞。 – nurettin

+0

thnx dude.i wl試一試 – Sontya