engine/public/javascripts/admin/aloha/plugins/com.gentics.aloha.plugins.P.../wordpastehandler.js

7 lines
5.9 KiB
JavaScript

/*
* Aloha Editor
* Author & Copyright (c) 2010 Gentics Software GmbH
* aloha-sales@gentics.com
* Licensed unter the terms of http://www.aloha-editor.com/license.html
*/
GENTICS.Aloha.PastePlugin.WordPasteHandler=new GENTICS.Aloha.PastePlugin.PasteHandler();GENTICS.Aloha.PastePlugin.WordPasteHandler.handlePaste=function(jqPasteDiv){if(this.detectWordContent(jqPasteDiv)){this.transformWordContent(jqPasteDiv)}};GENTICS.Aloha.PastePlugin.WordPasteHandler.detectWordContent=function(jqPasteDiv){var wordDetected=false;jqPasteDiv.find("*").each(function(){var style=jQuery(this).attr("style");if(style){if(style.toLowerCase().indexOf("mso")>=0){wordDetected=true;return false}}var clazz=jQuery(this).attr("class");if(clazz){if(clazz.toLowerCase().indexOf("mso")>=0){wordDetected=true;return false}}});return wordDetected};GENTICS.Aloha.PastePlugin.WordPasteHandler.isOrderedList=function(listSpan){if(listSpan.css("fontFamily")=="Wingdings"||listSpan.css("fontFamily")=="Symbol"){return false}return listSpan.text().match(/^([0-9]{1,3}\.)|([0-9]{1,3}\)|([a-zA-Z]{1,5}\.)|([a-zA-Z]{1,5}\)))$/)?true:false};GENTICS.Aloha.PastePlugin.WordPasteHandler.transformListsFromWord=function(jqPasteDiv){var that=this;var listElementClass="aloha-list-element";var bulletClass="aloha-list-bullet";var detectionFilter="p.MsoListParagraphCxSpFirst,p.MsoListParagraph,p span";var paragraphs=jqPasteDiv.find(detectionFilter);paragraphs.each(function(){var jqElem=jQuery(this);if(jqElem.hasClass("MsoListParagraphCxSpFirst")||jqElem.hasClass("MsoListParagraph")){jqElem.addClass(listElementClass)}else{if(jqElem.css("font-family").indexOf("Symbol")>=0){jqElem.closest("p").addClass(listElementClass)}else{if(jqElem.css("mso-list")!=""){jqElem.closest("p").addClass(listElementClass)}}}});detectionFilter="p span span span";var spans=jqPasteDiv.find(detectionFilter);spans.each(function(){var jqElem=jQuery(this);var innerText=jqElem.text().trim().replace(/&nbsp;/g,"");if(innerText.length==0){var outerText=jqElem.parent().parent().text().trim().replace(/&nbsp;/g,"");if(outerText.match(/^([0-9]{1,3}\.)|([0-9]{1,3}\)|([a-zA-Z]{1,5}\.)|([a-zA-Z]{1,5}\)))$/)){jqElem.closest("p").addClass(listElementClass);jqElem.parent().parent().addClass(bulletClass)}}});detectionFilter="p."+listElementClass;var negateDetectionFilter=":not("+detectionFilter+")";paragraphs=jqPasteDiv.find(detectionFilter);if(paragraphs.length>0){paragraphs.each(function(){var jqElem=jQuery(this);jqElem.removeClass(listElementClass);jqElem.find("font").each(function(){jQuery(this).contents().unwrap()});var nestLevel=0;var margin=parseFloat(jqElem.css("marginLeft"));var lists=[];var following=jqElem.nextUntil(negateDetectionFilter);var firstSpan=jQuery(jqElem.find("span."+bulletClass));if(firstSpan.length==0){firstSpan=jQuery(jqElem.children("span:first"))}var ordered=that.isOrderedList(firstSpan);firstSpan.remove();var jqList=jQuery(ordered?"<ol></ol>":"<ul></ul>");lists.push(jqList);var jqNewLi=jQuery("<li></li>");jqList.append(jqNewLi);jqElem.contents().appendTo(jqNewLi);jqElem.replaceWith(jqList);following.each(function(){var jqElem=jQuery(this);jqElem.find("font").each(function(){jQuery(this).contents().unwrap()});var newMargin=parseFloat(jqElem.css("marginLeft"));firstSpan=jQuery(jqElem.find("span."+bulletClass));if(firstSpan.length==0){firstSpan=jQuery(jqElem.children("span:first"))}ordered=that.isOrderedList(firstSpan);firstSpan.remove();if(newMargin>margin){var jqNewList=jQuery(ordered?"<ol></ol>":"<ul></ul>");jqList.children(":last").append(jqNewList);jqList=jqNewList;lists.push(jqList);nestLevel++;margin=newMargin}else{if(newMargin<margin&&nestLevel>0){lists.pop();nestLevel--;jqList=lists[nestLevel];margin=newMargin}}jqNewLi=jQuery("<li></li>");jqList.append(jqNewLi);jqElem.contents().appendTo(jqNewLi);jqElem.remove()})})}};GENTICS.Aloha.PastePlugin.WordPasteHandler.transformTitles=function(jqPasteDiv){jqPasteDiv.find("p.MsoTitle").each(function(){GENTICS.Aloha.Markup.transformDomObject(jQuery(this),"h1")});jqPasteDiv.find("p.MsoSubtitle").each(function(){GENTICS.Aloha.Markup.transformDomObject(jQuery(this),"h2")})};GENTICS.Aloha.PastePlugin.WordPasteHandler.transformTables=function(jqPasteDiv){jqPasteDiv.find("table").each(function(){jQuery(this).removeAttr("border").removeAttr("cellspacing").removeAttr("cellpadding")});jqPasteDiv.find("td").each(function(){jQuery(this).removeAttr("width").removeAttr("height").removeAttr("valign")})};GENTICS.Aloha.PastePlugin.WordPasteHandler.transformFormattings=function(jqPasteDiv){jqPasteDiv.find("strong,em,s,u").each(function(){if(this.nodeName.toLowerCase()=="strong"){GENTICS.Aloha.Markup.transformDomObject(jQuery(this),"b")}else{if(this.nodeName.toLowerCase()=="em"){GENTICS.Aloha.Markup.transformDomObject(jQuery(this),"i")}else{if(this.nodeName.toLowerCase()=="s"){GENTICS.Aloha.Markup.transformDomObject(jQuery(this),"del")}else{if(this.nodeName.toLowerCase()=="u"){jQuery(this).contents().unwrap()}}}}})};GENTICS.Aloha.PastePlugin.WordPasteHandler.removeComments=function(jqPasteDiv){jqPasteDiv.contents().each(function(){if(this.nodeType==8){jQuery(this).remove()}})};GENTICS.Aloha.PastePlugin.WordPasteHandler.unwrapTags=function(jqPasteDiv){jqPasteDiv.find("span,font,div").each(function(){jQuery(this).contents().unwrap()})};GENTICS.Aloha.PastePlugin.WordPasteHandler.removeStyles=function(jqPasteDiv){jqPasteDiv.find("*").each(function(){jQuery(this).removeAttr("style").removeClass()})};GENTICS.Aloha.PastePlugin.WordPasteHandler.removeNamespacedElements=function(jqPasteDiv){jqPasteDiv.find("*").each(function(){var nsPrefix=this.prefix?this.prefix:(this.scopeName?this.scopeName:undefined);if(nsPrefix&&nsPrefix!="HTML"){jQuery(this).remove()}})};GENTICS.Aloha.PastePlugin.WordPasteHandler.transformWordContent=function(jqPasteDiv){this.transformListsFromWord(jqPasteDiv);this.transformTables(jqPasteDiv);this.transformTitles(jqPasteDiv);this.removeComments(jqPasteDiv);this.unwrapTags(jqPasteDiv);this.removeStyles(jqPasteDiv);this.removeNamespacedElements(jqPasteDiv);this.transformFormattings(jqPasteDiv)};