/**
* Quirks.js
*
* Copyright, Moxiecode Systems AB
* Released under LGPL License.
*
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
*/
/**
* This class contains various fixes for browsers. These issues can not be feature
* detected since we have no direct control over the clipboard. However we might be able
* to remove some of these fixes once the browsers gets updated/fixed.
*
* @class tinymce.pasteplugin.Quirks
* @private
*/
define("tinymce/pasteplugin/Quirks", [
"tinymce/Env",
"tinymce/util/Tools",
"tinymce/pasteplugin/WordFilter",
"tinymce/pasteplugin/Utils"
], function(Env, Tools, WordFilter, Utils) {
"use strict";
return function(editor) {
function addPreProcessFilter(filterFunc) {
editor.on('BeforePastePreProcess', function(e) {
e.content = filterFunc(e.content);
});
}
/**
* Removes WebKit fragment comments and converted-space spans.
*
* This:
* a b
*
* Becomes:
* a b
*/
function removeWebKitFragments(html) {
html = Utils.filter(html, [
/^[\s\S]*|[\s\S]*$/g, // WebKit fragment
[/\u00a0<\/span>/g, '\u00a0'], // WebKit
/ a b a b
$/ // Traling BR elements
]);
return html;
}
/**
* Removes BR elements after block elements. IE9 has a nasty bug where it puts a BR element after each
* block element when pasting from word. This removes those elements.
*
* This:
*
[\\s\\r\\n]+|
)*(<\\/?(' + blockElements.join('|') + ')[^>]*>)(?:
[\\s\\r\\n]+|
)*',
'g'
);
// Remove BR:s from:
html = Utils.filter(html, [
[explorerBlocksRegExp, '$1']
]);
// IE9 also adds an extra BR element for each soft-linefeed and it also adds a BR for each word wrap break
html = Utils.filter(html, [
[/
/g, '
'], // Replace multiple BR elements with uppercase BR to keep them intact
[/
/g, ' '], // Replace single br elements with space since they are word wrap BR:s
[/
/g, '
'] // Replace back the double brs but into a single BR
]);
return html;
}
/**
* WebKit has a nasty bug where the all runtime styles gets added to style attributes when copy/pasting contents.
* This fix solves that by simply removing the whole style attribute.
*
* Todo: This can be made smarter. Keeping styles that override existing ones etc.
*
* @param {String} content Content that needs to be processed.
* @return {String} Processed contents.
*/
function removeWebKitStyles(content) {
if (editor.settings.paste_remove_styles || editor.settings.paste_remove_styles_if_webkit !== false) {
content = content.replace(/ style=\"[^\"]+\"/g, '');
}
return content;
}
// Sniff browsers and apply fixes since we can't feature detect
if (Env.webkit) {
addPreProcessFilter(removeWebKitStyles);
addPreProcessFilter(removeWebKitFragments);
}
if (Env.ie) {
addPreProcessFilter(removeExplorerBrElementsAfterBlocks);
}
};
});