2 * Fix most common html formatting misbehaviors of browsers implementation when inserting
3 * content via copy & paste contentEditable
5 * @author Christopher Blum
7 wysihtml5.quirks.cleanPastedHTML = (function() {
9 var styleToRegex = function (styleStr) {
10 var trimmedStr = wysihtml5.lang.string(styleStr).trim(),
11 escapedStr = trimmedStr.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
13 return new RegExp("^((?!^" + escapedStr + "$).)*$", "i");
16 var extendRulesWithStyleExceptions = function (rules, exceptStyles) {
17 var newRules = wysihtml5.lang.object(rules).clone(true),
20 for (tag in newRules.tags) {
22 if (newRules.tags.hasOwnProperty(tag)) {
23 if (newRules.tags[tag].keep_styles) {
24 for (style in newRules.tags[tag].keep_styles) {
25 if (newRules.tags[tag].keep_styles.hasOwnProperty(style)) {
26 if (exceptStyles[style]) {
27 newRules.tags[tag].keep_styles[style] = styleToRegex(exceptStyles[style]);
38 var pickRuleset = function(ruleset, html) {
39 var pickedSet, defaultSet;
45 for (var i = 0, max = ruleset.length; i < max; i++) {
46 if (!ruleset[i].condition) {
47 defaultSet = ruleset[i].set;
49 if (ruleset[i].condition && ruleset[i].condition.test(html)) {
50 return ruleset[i].set;
57 return function(html, options) {
59 'color': wysihtml5.dom.getStyle("color").from(options.referenceNode),
60 'fontSize': wysihtml5.dom.getStyle("font-size").from(options.referenceNode)
62 rules = extendRulesWithStyleExceptions(pickRuleset(options.rules, html) || {}, exceptStyles),
65 newHtml = wysihtml5.dom.parse(html, {
67 "cleanUp": true, // <span> elements, empty or without attributes, should be removed/replaced with their content
68 "context": options.referenceNode.ownerDocument,
69 "uneditableClass": options.uneditableClass,
70 "clearInternals" : true, // don't paste temprorary selection and other markings