4 * Copyright, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://www.tinymce.com/license
8 * Contributing: http://www.tinymce.com/contributing
12 * This class contains various fixes for browsers. These issues can not be feature
13 * detected since we have no direct control over the clipboard. However we might be able
14 * to remove some of these fixes once the browsers gets updated/fixed.
16 * @class tinymce.pasteplugin.Quirks
19 define("tinymce/pasteplugin/Quirks", [
22 ], function(Env, Tools) {
25 return function(editor) {
26 var explorerBlocksRegExp;
28 function addPreProcessFilter(filterFunc) {
29 editor.on('PastePreProcess', function(e) {
30 e.content = filterFunc(e.content);
34 function process(content, items) {
35 Tools.each(items, function(v) {
36 if (v.constructor == RegExp) {
37 content = content.replace(v, '');
39 content = content.replace(v[0], v[1]);
47 * Removes WebKit fragment comments and converted-space spans.
50 * <!--StartFragment-->a<span class="Apple-converted-space"> </span>b<!--EndFragment-->
55 function removeWebKitFragments(html) {
56 html = process(html, [
57 /^[\s\S]*<!--StartFragment-->|<!--EndFragment-->[\s\S]*$/g, // WebKit fragment
58 [/<span class="Apple-converted-space">\u00a0<\/span>/g, '\u00a0'], // WebKit
59 /<br>$/ // Traling BR elements
66 * Removes BR elements after block elements. IE9 has a nasty bug where it puts a BR element after each
67 * block element when pasting from word. This removes those elements.
70 * <p>a</p><br><p>b</p>
75 function removeExplorerBrElementsAfterBlocks(html) {
76 // Produce block regexp based on the block elements in schema
77 if (!explorerBlocksRegExp) {
78 var blockElements = [];
80 Tools.each(editor.schema.getBlockElements(), function(block, blockName) {
81 blockElements.push(blockName);
84 explorerBlocksRegExp = new RegExp(
85 '(?:<br> [\\s\\r\\n]+|<br>)*(<\\/?(' + blockElements.join('|') + ')[^>]*>)(?:<br> [\\s\\r\\n]+|<br>)*',
90 // Remove BR:s from: <BLOCK>X</BLOCK><BR>
91 html = process(html, [
92 [explorerBlocksRegExp, '$1']
95 // IE9 also adds an extra BR element for each soft-linefeed and it also adds a BR for each word wrap break
96 html = process(html, [
97 [/<br><br>/g, '<BR><BR>'], // Replace multiple BR elements with uppercase BR to keep them intact
98 [/<br>/g, ' '], // Replace single br elements with space since they are word wrap BR:s
99 [/<BR><BR>/g, '<br>'] // Replace back the double brs but into a single BR
105 // Sniff browsers and apply fixes since we can't feature detect
107 addPreProcessFilter(removeWebKitFragments);
111 addPreProcessFilter(removeExplorerBrElementsAfterBlocks);