odoo.define('web_editor.transcoder', function (require) { 'use strict'; var widget = require('web_editor.widget'); var rulesCache = []; /** * Returns the css rules which applies on an element, tweaked so that they are * browser/mail client ok. * * @param {DOMElement} a * @returns {Object} css property name -> css property value */ function getMatchedCSSRules(a) { var i, r, k; if (!rulesCache.length) { var sheets = document.styleSheets; for (i = sheets.length-1 ; i >= 0 ; i--) { var rules; // try...catch because browser may not able to enumerate rules for cross-domain sheets try { rules = sheets[i].rules || sheets[i].cssRules; } catch (e) { console.warn("Can't read the css rules of: " + sheets[i].href, e); continue; } if (rules) { for (r = rules.length-1; r >= 0; r--) { var selectorText = rules[r].selectorText; if (selectorText && rules[r].cssText && selectorText !== '*' && 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 (k = 0 ; k < st.length ; k++) { rulesCache.push({ 'selector': st[k], 'style': rules[r].style }); } } } } } rulesCache.reverse(); } var css = []; var style; a.matches = a.matches || a.webkitMatchesSelector || a.mozMatchesSelector || a.msMatchesSelector || a.oMatchesSelector; for (r = 0; r < rulesCache.length; r++) { if (a.matches(rulesCache[r].selector)) { style = rulesCache[r].style; if (style.parentRule) { var style_obj = {}; var len; for (k = 0, len = style.length ; k < len ; k++) { if (style[k].indexOf('animation') !== -1) { continue; } style_obj[style[k]] = style[style[k].replace(/-(.)/g, function (a, b) { return b.toUpperCase(); })]; if (new RegExp(style[k] + '\s*:[^:;]+!important' ).test(style.cssText)) { style_obj[style[k]] += ' !important'; } } rulesCache[r].style = style = style_obj; } css.push([rulesCache[r].selector, style]); } } function specificity(selector) { // http://www.w3.org/TR/css3-selectors/#specificity var a = 0; selector = selector.replace(/#[a-z0-9_-]+/gi, function () { a++; return ''; }); var b = 0; selector = selector.replace(/(\.[a-z0-9_-]+)|(\[.*?\])/gi, function () { b++; return ''; }); var c = 0; selector = selector.replace(/(^|\s+|:+)[a-z0-9_-]+/gi, function (a) { if (a.indexOf(':not(')===-1) c++; return ''; }); return a*100 + b*10 + c; } css.sort(function (a, b) { return specificity(a[0]) - specificity(b[0]); }); style = {}; _.each(css, function (v,k) { _.each(v[1], function (v,k) { if (v && _.isString(v) && k.indexOf('-webkit') === -1 && (!style[k] || style[k].indexOf('important') === -1 || v.indexOf('important') !== -1)) { style[k] = v; } }); }); _.each(style, function (v,k) { if (v.indexOf('important') !== -1) { style[k] = v.slice(0, v.length-11); } }); if (style.display === 'block') { delete style.display; } // The css generates all the attributes separately and not in simplified form. // In order to have a better compatibility (outlook for example) we simplify the css tags. // e.g. border-left-style: none; border-bottom-s .... will be simplified in border-style = none _.each([['margin'], ['padding'], ['border', 'style']], function (attr) { var p = attr[0]; var e = attr[1] ? '-' + attr[1] : ''; if (style[p+'-top'+e] || style[p+'-right'+e] || style[p+'-bottom'+e] || style[p+'-left'+e]) { if (style[p+'-top'+e] === style[p+'-right'+e] && style[p+'-top'+e] === style[p+'-bottom'+e] && style[p+'-top'+e] === style[p+'-left'+e]) { // keep => property: [top/right/bottom/left value]; style[p+e] = style[p+'-top'+e]; } else { // keep => property: [top value] [right value] [bottom value] [left value]; style[p+e] = (style[p+'-top'+e] || 0) + ' ' + (style[p+'-right'+e] || 0) + ' ' + (style[p+'-bottom'+e] || 0) + ' ' + (style[p+'-left'+e] || 0); if (style[p+e].indexOf('inherit') !== -1 || style[p+e].indexOf('initial') !== -1) { // keep => property-top: [top value]; property-right: [right value]; property-bottom: [bottom value]; property-left: [left value]; delete style[p+e]; return; } } delete style[p+'-top'+e]; delete style[p+'-right'+e]; delete style[p+'-bottom'+e]; delete style[p+'-left'+e]; } }); if (style['border-bottom-left-radius']) { style['border-radius'] = style['border-bottom-left-radius']; delete style['border-bottom-left-radius']; delete style['border-bottom-right-radius']; delete style['border-top-left-radius']; delete style['border-top-right-radius']; } // if the border styling is initial we remove it to simplify the css tags for compatibility. // Also, since we do not send a css style tag, the initial value of the border is useless. _.each(_.keys(style), function (k) { if (k.indexOf('border') !== -1 && style[k] === 'initial') { delete style[k]; } }); // text-decoration rule is decomposed in -line, -color and -style. This is // however not supported by many browser/mail clients and the editor does // not allow to change -color and -style rule anyway if (style['text-decoration-line']) { style['text-decoration'] = style['text-decoration-line']; delete style['text-decoration-line']; delete style['text-decoration-color']; delete style['text-decoration-style']; } // text-align inheritance does not seem to get past