odoo.define('web_editor.transcoder', function (require) { 'use strict'; var widget = require('web_editor.widget'); var cache = {}; var rulesCache = []; var getMatchedCSSRules = function (a) { if(cache[a.tagName + "." +a.className]) { return cache[a.tagName + "." +a.className]; } if (!rulesCache.length) { var sheets = document.styleSheets; for(var i = sheets.length-1; i >= 0 ; i--) { var rules; if (sheets[i].rules) { rules = sheets[i].rules; } else { //try...catch because Firefox not able to enumerate document.styleSheets[].cssRules[] for cross-domain stylesheets. try { rules = sheets[i].cssRules; } catch(e) { console.warn("Can't read the css rules of: " + sheets[i].href, e); continue; } rules = sheets[i].cssRules; } if (rules) { for(var r = rules.length-1; r >= 0; r--) { var selectorText = rules[r].selectorText; if (selectorText && rules[r].cssText && selectorText.indexOf(".") !== -1 && selectorText.indexOf(".note-") === -1 && selectorText.indexOf(":hover") === -1 && selectorText.indexOf(":before") === -1 && selectorText.indexOf(":after") === -1 && selectorText.indexOf(":active") === -1 && selectorText.indexOf(":link") === -1 && selectorText.indexOf("::") === -1 && selectorText.indexOf("\"") === -1 && selectorText.indexOf("'") === -1) { var st = selectorText.split(/\s*,\s*/); for (var k=0; k").attr("src", src).attr("data-class", $font.attr("class")).attr("style", style); $font.replaceWith($img); } else { $font.remove(); } }); }; // convert image into font awsome var img_to_font = function ($editable) { $("img[src*='/web_editor/font_to_img/']", $editable).each(function () { var $img = $(this); var $font = $("").attr("class", $img.data("class")).attr("style", $img.attr("style")).css("height", ""); $img.replaceWith($font); }); }; // convert class into inline style to send by mail var class_to_style = function ($editable) { if (!rulesCache.length) { getMatchedCSSRules($editable[0]); } var selector = _.map(rulesCache, function (a) { return a.selector;}).join(","); $editable.find(selector).each(function () { var $target = $(this); var css = getMatchedCSSRules(this); var style = $target.attr("style") || ""; _.each(css, function (v,k) { if (style.indexOf(k) === -1) { style = k+":"+v+";"+style; } }); $target.attr("style", style); }); }; // convert style into inline class from mail var style_to_class = function ($editable) { getMatchedCSSRules($editable[0]); var classes = []; $editable.find("[class]").each(function () { classes = classes.concat(this.className.split(/\s+/)); }); classes = _.compact(classes); var maybe_selector = _.filter(rulesCache, function (a) { return !!_.find(classes, function (b) { return a.selector.indexOf("."+b) !== -1; } ); }); var selector = _.map(maybe_selector, function (a) { return a.selector;}).join(","); var $c = $('').appendTo("body"); $editable.find(selector).each(function () { var $target = $(this); var css = getMatchedCSSRules(this); var style = ""; _.each(css, function (v,k) { if (style.indexOf(k) === -1) { style = k+":"+v+";"+style; } }); css = $c.attr("style", style).attr("style").split(/\s*;\s*/); style = $target.attr("style") || ""; _.each(css, function (v) { style = style.replace(v, ''); }); $target.attr("style", style.replace(/;+(\s;)*/g, ';').replace(/^;/g, '')); }); $c.remove(); }; return { 'font_to_img': font_to_img, 'img_to_font': img_to_font, 'class_to_style': class_to_style, 'style_to_class': style_to_class, }; });