(function(){'use strict';var Style,tools=CKEDITOR.tools,plug={};CKEDITOR.plugins.pastetools.filters.common=plug;plug.rules=function(html,editor,filter){var availableFonts=getMatchingFonts(editor);return{elements:{'^':function(element){removeSuperfluousStyles(element);normalizeAttributesName(element);},'span':function(element){if(element.hasClass('Apple-converted-space')){return new CKEDITOR.htmlParser.text(' ');}},'table':function(element){element.filterChildren(filter);var parent=element.parent,root=parent&&parent.parent,parentChildren,i;if(parent.name&&parent.name==='div'&&parent.attributes.align&&tools.object.keys(parent.attributes).length===1&&parent.children.length===1){element.attributes.align=parent.attributes.align;parentChildren=parent.children.splice(0);element.remove();for(i=parentChildren.length-1;i>=0;i--){root.add(parentChildren[i],parent.getIndex());}
parent.remove();}
Style.convertStyleToPx(element);},'tr':function(element){element.attributes={};},'td':function(element){var ascendant=element.getAscendant('table'),ascendantStyle=tools.parseCssText(ascendant.attributes.style,true);var background=ascendantStyle.background;if(background){Style.setStyle(element,'background',background,true);}
var backgroundColor=ascendantStyle['background-color'];if(backgroundColor){Style.setStyle(element,'background-color',backgroundColor,true);}
var styles=tools.parseCssText(element.attributes.style,true),borderStyles=styles.border?CKEDITOR.tools.style.border.fromCssRule(styles.border):{},borders=tools.style.border.splitCssValues(styles,borderStyles),tmpStyles=CKEDITOR.tools.clone(styles);for(var key in tmpStyles){if(key.indexOf('border')==0){delete tmpStyles[key];}}
element.attributes.style=CKEDITOR.tools.writeCssText(tmpStyles);if(styles.background){var bg=CKEDITOR.tools.style.parse.background(styles.background);if(bg.color){Style.setStyle(element,'background-color',bg.color,true);Style.setStyle(element,'background','');}}
for(var border in borders){var borderStyle=styles[border]?CKEDITOR.tools.style.border.fromCssRule(styles[border]):borders[border];if(borderStyle.style==='none'){Style.setStyle(element,border,'none');}else{Style.setStyle(element,border,borderStyle.toString());}}
Style.mapCommonStyles(element);Style.convertStyleToPx(element);Style.createStyleStack(element,filter,editor,/margin|text\-align|padding|list\-style\-type|width|height|border|white\-space|vertical\-align|background/i);},'font':function(element){if(element.attributes.face&&availableFonts){element.attributes.face=replaceWithMatchingFont(element.attributes.face,availableFonts);}}}};};plug.styles={setStyle:function(element,key,value,dontOverwrite){var styles=tools.parseCssText(element.attributes.style);if(dontOverwrite&&styles[key]){return;}
if(value===''){delete styles[key];}else{styles[key]=value;}
element.attributes.style=CKEDITOR.tools.writeCssText(styles);},convertStyleToPx:function(element){var style=element.attributes.style;if(!style){return;}
element.attributes.style=style.replace(/\d+(\.\d+)?pt/g,function(match){return CKEDITOR.tools.convertToPx(match)+'px';});},mapStyles:function(element,attributeStyleMap){for(var attribute in attributeStyleMap){if(element.attributes[attribute]){if(typeof attributeStyleMap[attribute]==='function'){attributeStyleMap[attribute](element.attributes[attribute]);}else{Style.setStyle(element,attributeStyleMap[attribute],element.attributes[attribute]);}
delete element.attributes[attribute];}}},mapCommonStyles:function(element){return Style.mapStyles(element,{vAlign:function(value){Style.setStyle(element,'vertical-align',value);},width:function(value){Style.setStyle(element,'width',fixValue(value));},height:function(value){Style.setStyle(element,'height',fixValue(value));}});},normalizedStyles:function(element,editor){var resetStyles=['background-color:transparent','border-image:none','color:windowtext','direction:ltr','mso-','visibility:visible','div:border:none'],textStyles=['font-family','font','font-size','color','background-color','line-height','text-decoration'],matchStyle=function(){var keys=[];for(var i=0;i<arguments.length;i++){if(arguments[i]){keys.push(arguments[i]);}}
return tools.indexOf(resetStyles,keys.join(':'))!==-1;},removeFontStyles=CKEDITOR.plugins.pastetools.getConfigValue(editor,'removeFontStyles')===true;var styles=tools.parseCssText(element.attributes.style);if(element.name=='cke:li'){if(styles['TEXT-INDENT']&&styles.MARGIN){element.attributes['cke-indentation']=plug.lists.getElementIndentation(element);styles.MARGIN=styles.MARGIN.replace(/(([\w\.]+ ){3,3})[\d\.]+(\w+$)/,'$10$3');}else{delete styles['TEXT-INDENT'];}
delete styles['text-indent'];}
var keys=tools.object.keys(styles);for(var i=0;i<keys.length;i++){var styleName=keys[i].toLowerCase(),styleValue=styles[keys[i]],indexOf=CKEDITOR.tools.indexOf,toBeRemoved=removeFontStyles&&indexOf(textStyles,styleName.toLowerCase())!==-1;if(toBeRemoved||matchStyle(null,styleName,styleValue)||matchStyle(null,styleName.replace(/\-.*$/,'-'))||matchStyle(null,styleName)||matchStyle(element.name,styleName,styleValue)||matchStyle(element.name,styleName.replace(/\-.*$/,'-'))||matchStyle(element.name,styleName)||matchStyle(styleValue)){delete styles[keys[i]];}}
var keepZeroMargins=CKEDITOR.plugins.pastetools.getConfigValue(editor,'keepZeroMargins');parseShorthandMargins(styles);normalizeMargins();return CKEDITOR.tools.writeCssText(styles);function normalizeMargins(){var keys=['top','right','bottom','left'];CKEDITOR.tools.array.forEach(keys,function(key){key='margin-'+key;if(!(key in styles)){return;}
var value=CKEDITOR.tools.convertToPx(styles[key]);if(value||keepZeroMargins){styles[key]=value?value+'px':0;}else{delete styles[key];}});}},createStyleStack:function(element,filter,editor,skipStyles){var children=[],i;element.filterChildren(filter);for(i=element.children.length-1;i>=0;i--){children.unshift(element.children[i]);element.children[i].remove();}
Style.sortStyles(element);var styles=tools.parseCssText(Style.normalizedStyles(element,editor)),innermostElement=element,styleTopmost=element.name==='span';for(var style in styles){if(style.match(skipStyles||/margin((?!-)|-left|-top|-bottom|-right)|text-indent|text-align|width|border|padding/i)){continue;}
if(styleTopmost){styleTopmost=false;continue;}
var newElement=new CKEDITOR.htmlParser.element('span');newElement.attributes.style=style+':'+styles[style];innermostElement.add(newElement);innermostElement=newElement;delete styles[style];}
if(!CKEDITOR.tools.isEmpty(styles)){element.attributes.style=CKEDITOR.tools.writeCssText(styles);}else{delete element.attributes.style;}
for(i=0;i<children.length;i++){innermostElement.add(children[i]);}},sortStyles:function(element){var orderedStyles=['border','border-bottom','font-size','background'],style=tools.parseCssText(element.attributes.style),keys=tools.object.keys(style),sortedKeys=[],nonSortedKeys=[];for(var i=0;i<keys.length;i++){if(tools.indexOf(orderedStyles,keys[i].toLowerCase())!==-1){sortedKeys.push(keys[i]);}else{nonSortedKeys.push(keys[i]);}}
sortedKeys.sort(function(a,b){var aIndex=tools.indexOf(orderedStyles,a.toLowerCase());var bIndex=tools.indexOf(orderedStyles,b.toLowerCase());return aIndex-bIndex;});keys=[].concat(sortedKeys,nonSortedKeys);var sortedStyles={};for(i=0;i<keys.length;i++){sortedStyles[keys[i]]=style[keys[i]];}
element.attributes.style=CKEDITOR.tools.writeCssText(sortedStyles);},pushStylesLower:function(element,exceptions,wrapText){if(!element.attributes.style||element.children.length===0){return false;}
exceptions=exceptions||{};var retainedStyles={'list-style-type':true,'width':true,'height':true,'border':true,'border-':true};var styles=tools.parseCssText(element.attributes.style);for(var style in styles){if(style.toLowerCase()in retainedStyles||retainedStyles[style.toLowerCase().replace(/\-.*$/,'-')]||style.toLowerCase()in exceptions){continue;}
var pushed=false;for(var i=0;i<element.children.length;i++){var child=element.children[i];if(child.type===CKEDITOR.NODE_TEXT&&wrapText){var wrapper=new CKEDITOR.htmlParser.element('span');wrapper.setHtml(child.value);child.replaceWith(wrapper);child=wrapper;}
if(child.type!==CKEDITOR.NODE_ELEMENT){continue;}
pushed=true;Style.setStyle(child,style,styles[style]);}
if(pushed){delete styles[style];}}
element.attributes.style=CKEDITOR.tools.writeCssText(styles);return true;},inliner:{filtered:['break-before','break-after','break-inside','page-break','page-break-before','page-break-after','page-break-inside'],parse:function(styles){var parseCssText=CKEDITOR.tools.parseCssText,filterStyles=Style.inliner.filter,sheet=styles.is?styles.$.sheet:createIsolatedStylesheet(styles);function createIsolatedStylesheet(styles){var style=new CKEDITOR.dom.element('style'),iframe=new CKEDITOR.dom.element('iframe');iframe.hide();CKEDITOR.document.getBody().append(iframe);iframe.$.contentDocument.documentElement.appendChild(style.$);style.$.textContent=styles;iframe.remove();return style.$.sheet;}
function getStyles(cssText){var startIndex=cssText.indexOf('{'),endIndex=cssText.indexOf('}');return parseCssText(cssText.substring(startIndex+1,endIndex),true);}
var parsedStyles=[],rules,i;if(sheet){rules=sheet.cssRules;for(i=0;i<rules.length;i++){if(rules[i].type===window.CSSRule.STYLE_RULE){parsedStyles.push({selector:rules[i].selectorText,styles:filterStyles(getStyles(rules[i].cssText))});}}}
return parsedStyles;},filter:function(stylesObj){var toRemove=Style.inliner.filtered,indexOf=tools.array.indexOf,newObj={},style;for(style in stylesObj){if(indexOf(toRemove,style)===-1){newObj[style]=stylesObj[style];}}
return newObj;},sort:function(stylesArray){function getCompareFunction(styles){var order=CKEDITOR.tools.array.map(styles,function(item){return item.selector;});return function(style1,style2){var value1=isClassSelector(style1.selector)?1:0,value2=isClassSelector(style2.selector)?1:0,result=value2-value1;return result!==0?result:order.indexOf(style2.selector)-order.indexOf(style1.selector);};}
function isClassSelector(selector){return(''+selector).indexOf('.')!==-1;}
return stylesArray.sort(getCompareFunction(stylesArray));},inline:function(html){var parseStyles=Style.inliner.parse,sortStyles=Style.inliner.sort,document=createTempDocument(html),stylesTags=document.find('style'),stylesArray=sortStyles(parseStyleTags(stylesTags));function createTempDocument(html){var parser=new DOMParser(),document=parser.parseFromString(html,'text/html');return new CKEDITOR.dom.document(document);}
function parseStyleTags(stylesTags){var styles=[],i;for(i=0;i<stylesTags.count();i++){styles=styles.concat(parseStyles(stylesTags.getItem(i)));}
return styles;}
function applyStyle(document,selector,style){var elements=document.find(selector),element,oldStyle,newStyle,i;parseShorthandMargins(style);for(i=0;i<elements.count();i++){element=elements.getItem(i);oldStyle=CKEDITOR.tools.parseCssText(element.getAttribute('style'));parseShorthandMargins(oldStyle);newStyle=CKEDITOR.tools.extend({},oldStyle,style);element.setAttribute('style',CKEDITOR.tools.writeCssText(newStyle));}}
CKEDITOR.tools.array.forEach(stylesArray,function(style){applyStyle(document,style.selector,style.styles);});return document;}}};Style=plug.styles;plug.lists={getElementIndentation:function(element){var style=tools.parseCssText(element.attributes.style);if(style.margin||style.MARGIN){style.margin=style.margin||style.MARGIN;var fakeElement={styles:{margin:style.margin}};CKEDITOR.filter.transformationsTools.splitMarginShorthand(fakeElement);style['margin-left']=fakeElement.styles['margin-left'];}
return parseInt(tools.convertToPx(style['margin-left']||'0px'),10);}};plug.elements={replaceWithChildren:function(element){for(var i=element.children.length-1;i>=0;i--){element.children[i].insertAfter(element);}}};plug.createAttributeStack=createAttributeStack;plug.parseShorthandMargins=parseShorthandMargins;function fixValue(value){var endsWithPercent=/%$/;return endsWithPercent.test(value)?value:value+'px';}
function createAttributeStack(element,filter){var i,children=[];element.filterChildren(filter);for(i=element.children.length-1;i>=0;i--){children.unshift(element.children[i]);element.children[i].remove();}
var attributes=element.attributes,innermostElement=element,topmost=true;for(var attribute in attributes){if(topmost){topmost=false;continue;}
var newElement=new CKEDITOR.htmlParser.element(element.name);newElement.attributes[attribute]=attributes[attribute];innermostElement.add(newElement);innermostElement=newElement;delete attributes[attribute];}
for(i=0;i<children.length;i++){innermostElement.add(children[i]);}}
function parseShorthandMargins(style){var marginCase=style.margin?'margin':style.MARGIN?'MARGIN':false,key,margin;if(marginCase){margin=CKEDITOR.tools.style.parse.margin(style[marginCase]);for(key in margin){style['margin-'+key]=margin[key];}
delete style[marginCase];}}
function removeSuperfluousStyles(element){var resetStyles=['background-color:transparent','background:transparent','background-color:none','background:none','background-position:initial initial','background-repeat:initial initial','caret-color','font-family:-webkit-standard','font-variant-caps','letter-spacing:normal','orphans','widows','text-transform:none','word-spacing:0px','-webkit-text-size-adjust:auto','-webkit-text-stroke-width:0px','text-indent:0px','margin-bottom:0in'];var styles=CKEDITOR.tools.parseCssText(element.attributes.style),styleName,styleString;for(styleName in styles){styleString=styleName+':'+styles[styleName];if(CKEDITOR.tools.array.some(resetStyles,function(val){return styleString.substring(0,val.length).toLowerCase()===val;})){delete styles[styleName];continue;}}
styles=CKEDITOR.tools.writeCssText(styles);if(styles!==''){element.attributes.style=styles;}else{delete element.attributes.style;}}
function getMatchingFonts(editor){var fontNames=editor.config.font_names,validNames=[];if(!fontNames||!fontNames.length){return false;}
validNames=CKEDITOR.tools.array.map(fontNames.split(';'),function(value){if(value.indexOf('/')===-1){return value;}
return value.split('/')[1];});return validNames.length?validNames:false;}
function replaceWithMatchingFont(fontValue,availableFonts){var fontParts=fontValue.split(','),matchingFont=CKEDITOR.tools.array.find(availableFonts,function(font){for(var i=0;i<fontParts.length;i++){if(font.indexOf(CKEDITOR.tools.trim(fontParts[i]))===-1){return false;}}
return true;});return matchingFont||fontValue;}
function normalizeAttributesName(element){if(element.attributes.bgcolor){var styles=CKEDITOR.tools.parseCssText(element.attributes.style);if(!styles['background-color']){styles['background-color']=element.attributes.bgcolor;element.attributes.style=CKEDITOR.tools.writeCssText(styles);}}}})();