2 * editor_plugin_src.js
\r
4 * Copyright 2009, Moxiecode Systems AB
\r
5 * Released under LGPL License.
\r
7 * License: http://tinymce.moxiecode.com/license
\r
8 * Contributing: http://tinymce.moxiecode.com/contributing
\r
12 var each = tinymce.each,
\r
15 paste_auto_cleanup_on_paste : true,
\r
16 paste_block_drop : false,
\r
17 paste_retain_style_properties : "none",
\r
18 paste_strip_class_attributes : "mso",
\r
19 paste_remove_spans : false,
\r
20 paste_remove_styles : false,
\r
21 paste_remove_styles_if_webkit : true,
\r
22 paste_convert_middot_lists : true,
\r
23 paste_convert_headers_to_strong : false,
\r
24 paste_dialog_width : "450",
\r
25 paste_dialog_height : "400",
\r
26 paste_text_use_dialog : false,
\r
27 paste_text_sticky : false,
\r
28 paste_text_notifyalways : false,
\r
29 paste_text_linebreaktype : "p",
\r
30 paste_text_replacements : [
\r
32 [/[\x93\x94\u201c\u201d]/g, '"'],
\r
33 [/[\x60\x91\x92\u2018\u2019]/g, "'"]
\r
37 function getParam(ed, name) {
\r
38 return ed.getParam(name, defs[name]);
\r
41 tinymce.create('tinymce.plugins.PastePlugin', {
\r
42 init : function(ed, url) {
\r
48 // Setup plugin events
\r
49 t.onPreProcess = new tinymce.util.Dispatcher(t);
\r
50 t.onPostProcess = new tinymce.util.Dispatcher(t);
\r
52 // Register default handlers
\r
53 t.onPreProcess.add(t._preProcess);
\r
54 t.onPostProcess.add(t._postProcess);
\r
56 // Register optional preprocess handler
\r
57 t.onPreProcess.add(function(pl, o) {
\r
58 ed.execCallback('paste_preprocess', pl, o);
\r
61 // Register optional postprocess
\r
62 t.onPostProcess.add(function(pl, o) {
\r
63 ed.execCallback('paste_postprocess', pl, o);
\r
66 // Initialize plain text flag
\r
67 ed.pasteAsPlainText = false;
\r
69 // This function executes the process handlers and inserts the contents
\r
70 // force_rich overrides plain text mode set by user, important for pasting with execCommand
\r
71 function process(o, force_rich) {
\r
74 // Execute pre process handlers
\r
75 t.onPreProcess.dispatch(t, o);
\r
77 // Create DOM structure
\r
78 o.node = dom.create('div', 0, o.content);
\r
80 // Execute post process handlers
\r
81 t.onPostProcess.dispatch(t, o);
\r
83 // Serialize content
\r
84 o.content = ed.serializer.serialize(o.node, {getInner : 1});
\r
86 // Plain text option active?
\r
87 if ((!force_rich) && (ed.pasteAsPlainText)) {
\r
88 t._insertPlainText(ed, dom, o.content);
\r
90 if (!getParam(ed, "paste_text_sticky")) {
\r
91 ed.pasteAsPlainText = false;
\r
92 ed.controlManager.setActive("pastetext", false);
\r
94 } else if (/<(p|h[1-6]|ul|ol)/.test(o.content)) {
\r
95 // Handle insertion of contents containing block elements separately
\r
96 t._insertBlockContent(ed, dom, o.content);
\r
98 t._insert(o.content);
\r
102 // Add command for external usage
\r
103 ed.addCommand('mceInsertClipboardContent', function(u, o) {
\r
107 if (!getParam(ed, "paste_text_use_dialog")) {
\r
108 ed.addCommand('mcePasteText', function(u, v) {
\r
109 var cookie = tinymce.util.Cookie;
\r
111 ed.pasteAsPlainText = !ed.pasteAsPlainText;
\r
112 ed.controlManager.setActive('pastetext', ed.pasteAsPlainText);
\r
114 if ((ed.pasteAsPlainText) && (!cookie.get("tinymcePasteText"))) {
\r
115 if (getParam(ed, "paste_text_sticky")) {
\r
116 ed.windowManager.alert(ed.translate('paste.plaintext_mode_sticky'));
\r
118 ed.windowManager.alert(ed.translate('paste.plaintext_mode_sticky'));
\r
121 if (!getParam(ed, "paste_text_notifyalways")) {
\r
122 cookie.set("tinymcePasteText", "1", new Date(new Date().getFullYear() + 1, 12, 31))
\r
128 ed.addButton('pastetext', {title: 'paste.paste_text_desc', cmd: 'mcePasteText'});
\r
129 ed.addButton('selectall', {title: 'paste.selectall_desc', cmd: 'selectall'});
\r
131 // This function grabs the contents from the clipboard by adding a
\r
132 // hidden div and placing the caret inside it and after the browser paste
\r
133 // is done it grabs that contents and processes that
\r
134 function grabContent(e) {
\r
135 var n, or, rng, sel = ed.selection, dom = ed.dom, body = ed.getBody(), posY;
\r
137 // Check if browser supports direct plaintext access
\r
138 if (ed.pasteAsPlainText && (e.clipboardData || dom.doc.dataTransfer)) {
\r
139 e.preventDefault();
\r
140 process({content : (e.clipboardData || dom.doc.dataTransfer).getData('Text')}, true);
\r
144 if (dom.get('_mcePaste'))
\r
147 // Create container to paste into
\r
148 n = dom.add(body, 'div', {id : '_mcePaste', 'class' : 'mcePaste'}, '\uFEFF<br _mce_bogus="1">');
\r
150 // If contentEditable mode we need to find out the position of the closest element
\r
151 if (body != ed.getDoc().body)
\r
152 posY = dom.getPos(ed.selection.getStart(), body).y;
\r
154 posY = body.scrollTop;
\r
156 // Styles needs to be applied after the element is added to the document since WebKit will otherwise remove all styles
\r
158 position : 'absolute',
\r
163 overflow : 'hidden'
\r
166 if (tinymce.isIE) {
\r
167 // Select the container
\r
168 rng = dom.doc.body.createTextRange();
\r
169 rng.moveToElementText(n);
\r
170 rng.execCommand('Paste');
\r
172 // Remove container
\r
175 // Check if the contents was changed, if it wasn't then clipboard extraction failed probably due
\r
176 // to IE security settings so we pass the junk though better than nothing right
\r
177 if (n.innerHTML === '\uFEFF') {
\r
178 ed.execCommand('mcePasteWord');
\r
179 e.preventDefault();
\r
183 // Process contents
\r
184 process({content : n.innerHTML});
\r
186 // Block the real paste event
\r
187 return tinymce.dom.Event.cancel(e);
\r
189 function block(e) {
\r
190 e.preventDefault();
\r
193 // Block mousedown and click to prevent selection change
\r
194 dom.bind(ed.getDoc(), 'mousedown', block);
\r
195 dom.bind(ed.getDoc(), 'keydown', block);
\r
197 or = ed.selection.getRng();
\r
199 // Move caret into hidden div
\r
201 rng = ed.getDoc().createRange();
\r
202 rng.setStart(n, 0);
\r
206 // Wait a while and grab the pasted contents
\r
207 window.setTimeout(function() {
\r
208 var h = '', nl = dom.select('div.mcePaste');
\r
210 // WebKit will split the div into multiple ones so this will loop through then all and join them to get the whole HTML string
\r
211 each(nl, function(n) {
\r
212 var child = n.firstChild;
\r
214 // WebKit inserts a DIV container with lots of odd styles
\r
215 if (child && child.nodeName == 'DIV' && child.style.marginTop && child.style.backgroundColor) {
\r
216 dom.remove(child, 1);
\r
219 // WebKit duplicates the divs so we need to remove them
\r
220 each(dom.select('div.mcePaste', n), function(n) {
\r
224 // Remove apply style spans
\r
225 each(dom.select('span.Apple-style-span', n), function(n) {
\r
229 // Remove bogus br elements
\r
230 each(dom.select('br[_mce_bogus]', n), function(n) {
\r
237 // Remove the nodes
\r
238 each(nl, function(n) {
\r
242 // Restore the old selection
\r
246 process({content : h});
\r
248 // Unblock events ones we got the contents
\r
249 dom.unbind(ed.getDoc(), 'mousedown', block);
\r
250 dom.unbind(ed.getDoc(), 'keydown', block);
\r
255 // Check if we should use the new auto process method
\r
256 if (getParam(ed, "paste_auto_cleanup_on_paste")) {
\r
257 // Is it's Opera or older FF use key handler
\r
258 if (tinymce.isOpera || /Firefox\/2/.test(navigator.userAgent)) {
\r
259 ed.onKeyDown.add(function(ed, e) {
\r
260 if (((tinymce.isMac ? e.metaKey : e.ctrlKey) && e.keyCode == 86) || (e.shiftKey && e.keyCode == 45))
\r
264 // Grab contents on paste event on Gecko and WebKit
\r
265 ed.onPaste.addToTop(function(ed, e) {
\r
266 return grabContent(e);
\r
271 // Block all drag/drop events
\r
272 if (getParam(ed, "paste_block_drop")) {
\r
273 ed.onInit.add(function() {
\r
274 ed.dom.bind(ed.getBody(), ['dragend', 'dragover', 'draggesture', 'dragdrop', 'drop', 'drag'], function(e) {
\r
275 e.preventDefault();
\r
276 e.stopPropagation();
\r
283 // Add legacy support
\r
284 t._legacySupport();
\r
287 getInfo : function() {
\r
289 longname : 'Paste text/word',
\r
290 author : 'Moxiecode Systems AB',
\r
291 authorurl : 'http://tinymce.moxiecode.com',
\r
292 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/paste',
\r
293 version : tinymce.majorVersion + "." + tinymce.minorVersion
\r
297 _preProcess : function(pl, o) {
\r
298 //console.log('Before preprocess:' + o.content);
\r
300 var ed = this.editor,
\r
302 grep = tinymce.grep,
\r
303 explode = tinymce.explode,
\r
304 trim = tinymce.trim,
\r
307 function process(items) {
\r
308 each(items, function(v) {
\r
309 // Remove or replace
\r
310 if (v.constructor == RegExp)
\r
311 h = h.replace(v, '');
\r
313 h = h.replace(v[0], v[1]);
\r
317 // Detect Word content and process it more aggressive
\r
318 if (/class="?Mso|style="[^"]*\bmso-|w:WordDocument/i.test(h) || o.wordContent) {
\r
319 o.wordContent = true; // Mark the pasted contents as word specific content
\r
320 //console.log('Word contents detected.');
\r
322 // Process away some basic content
\r
324 /^\s*( )+/gi, // entities at the start of contents
\r
325 /( |<br[^>]*>)+\s*$/gi // entities at the end of contents
\r
328 if (getParam(ed, "paste_convert_headers_to_strong")) {
\r
329 h = h.replace(/<p [^>]*class="?MsoHeading"?[^>]*>(.*?)<\/p>/gi, "<p><strong>$1</strong></p>");
\r
332 if (getParam(ed, "paste_convert_middot_lists")) {
\r
334 [/<!--\[if !supportLists\]-->/gi, '$&__MCE_ITEM__'], // Convert supportLists to a list item marker
\r
335 [/(<span[^>]+(?:mso-list:|:\s*symbol)[^>]+>)/gi, '$1__MCE_ITEM__'] // Convert mso-list and symbol spans to item markers
\r
340 // Word comments like conditional comments etc
\r
341 /<!--[\s\S]+?-->/gi,
\r
343 // Remove comments, scripts (e.g., msoShowComment), XML tag, VML content, MS Office namespaced tags, and a few other tags
\r
344 /<(!|script[^>]*>.*?<\/script(?=[>\s])|\/?(\?xml(:\w+)?|img|meta|link|style|\w:\w+)(?=[\s\/>]))[^>]*>/gi,
\r
346 // Convert <s> into <strike> for line-though
\r
347 [/<(\/?)s>/gi, "<$1strike>"],
\r
349 // Replace nsbp entites to char since it's easier to handle
\r
350 [/ /gi, "\u00a0"]
\r
353 // Remove bad attributes, with or without quotes, ensuring that attribute text is really inside a tag.
\r
354 // If JavaScript had a RegExp look-behind, we could have integrated this with the last process() array and got rid of the loop. But alas, it does not, so we cannot.
\r
357 h = h.replace(/(<[a-z][^>]*\s)(?:id|name|language|type|on\w+|\w+:\w+)=(?:"[^"]*"|\w+)\s?/gi, "$1");
\r
358 } while (len != h.length);
\r
360 // Remove all spans if no styles is to be retained
\r
361 if (getParam(ed, "paste_retain_style_properties").replace(/^none$/i, "").length == 0) {
\r
362 h = h.replace(/<\/?span[^>]*>/gi, "");
\r
364 // We're keeping styles, so at least clean them up.
\r
365 // CSS Reference: http://msdn.microsoft.com/en-us/library/aa155477.aspx
\r
368 // Convert <span style="mso-spacerun:yes">___</span> to string of alternating breaking/non-breaking spaces of same length
\r
369 [/<span\s+style\s*=\s*"\s*mso-spacerun\s*:\s*yes\s*;?\s*"\s*>([\s\u00a0]*)<\/span>/gi,
\r
370 function(str, spaces) {
\r
371 return (spaces.length > 0)? spaces.replace(/./, " ").slice(Math.floor(spaces.length/2)).split("").join("\u00a0") : "";
\r
375 // Examine all styles: delete junk, transform some, and keep the rest
\r
376 [/(<[a-z][^>]*)\sstyle="([^"]*)"/gi,
\r
377 function(str, tag, style) {
\r
380 s = explode(trim(style).replace(/"/gi, "'"), ";");
\r
382 // Examine each style definition within the tag's style attribute
\r
383 each(s, function(v) {
\r
385 parts = explode(v, ":");
\r
387 function ensureUnits(v) {
\r
388 return v + ((v !== "0") && (/\d$/.test(v)))? "px" : "";
\r
391 if (parts.length == 2) {
\r
392 name = parts[0].toLowerCase();
\r
393 value = parts[1].toLowerCase();
\r
395 // Translate certain MS Office styles into their CSS equivalents
\r
397 case "mso-padding-alt":
\r
398 case "mso-padding-top-alt":
\r
399 case "mso-padding-right-alt":
\r
400 case "mso-padding-bottom-alt":
\r
401 case "mso-padding-left-alt":
\r
402 case "mso-margin-alt":
\r
403 case "mso-margin-top-alt":
\r
404 case "mso-margin-right-alt":
\r
405 case "mso-margin-bottom-alt":
\r
406 case "mso-margin-left-alt":
\r
407 case "mso-table-layout-alt":
\r
410 case "mso-vertical-align-alt":
\r
411 n[i++] = name.replace(/^mso-|-alt$/g, "") + ":" + ensureUnits(value);
\r
414 case "horiz-align":
\r
415 n[i++] = "text-align:" + value;
\r
419 n[i++] = "vertical-align:" + value;
\r
423 case "mso-foreground":
\r
424 n[i++] = "color:" + value;
\r
427 case "mso-background":
\r
428 case "mso-highlight":
\r
429 n[i++] = "background:" + value;
\r
432 case "mso-default-height":
\r
433 n[i++] = "min-height:" + ensureUnits(value);
\r
436 case "mso-default-width":
\r
437 n[i++] = "min-width:" + ensureUnits(value);
\r
440 case "mso-padding-between-alt":
\r
441 n[i++] = "border-collapse:separate;border-spacing:" + ensureUnits(value);
\r
444 case "text-line-through":
\r
445 if ((value == "single") || (value == "double")) {
\r
446 n[i++] = "text-decoration:line-through";
\r
450 case "mso-zero-height":
\r
451 if (value == "yes") {
\r
452 n[i++] = "display:none";
\r
457 // Eliminate all MS Office style definitions that have no CSS equivalent by examining the first characters in the name
\r
458 if (/^(mso|column|font-emph|lang|layout|line-break|list-image|nav|panose|punct|row|ruby|sep|size|src|tab-|table-border|text-(?!align|decor|indent|trans)|top-bar|version|vnd|word-break)/.test(name)) {
\r
462 // If it reached this point, it must be a valid CSS style
\r
463 n[i++] = name + ":" + parts[1]; // Lower-case name, but keep value case
\r
467 // If style attribute contained any valid styles the re-write it; otherwise delete style attribute.
\r
469 return tag + ' style="' + n.join(';') + '"';
\r
479 // Replace headers with <strong>
\r
480 if (getParam(ed, "paste_convert_headers_to_strong")) {
\r
482 [/<h[1-6][^>]*>/gi, "<p><strong>"],
\r
483 [/<\/h[1-6][^>]*>/gi, "</strong></p>"]
\r
487 // Class attribute options are: leave all as-is ("none"), remove all ("all"), or remove only those starting with mso ("mso").
\r
488 // Note:- paste_strip_class_attributes: "none", verify_css_classes: true is also a good variation.
\r
489 stripClass = getParam(ed, "paste_strip_class_attributes");
\r
491 if (stripClass !== "none") {
\r
492 function removeClasses(match, g1) {
\r
493 if (stripClass === "all")
\r
496 var cls = grep(explode(g1.replace(/^(["'])(.*)\1$/, "$2"), " "),
\r
498 return (/^(?!mso)/i.test(v));
\r
502 return cls.length ? ' class="' + cls.join(" ") + '"' : '';
\r
505 h = h.replace(/ class="([^"]+)"/gi, removeClasses);
\r
506 h = h.replace(/ class=(\w+)/gi, removeClasses);
\r
509 // Remove spans option
\r
510 if (getParam(ed, "paste_remove_spans")) {
\r
511 h = h.replace(/<\/?span[^>]*>/gi, "");
\r
514 //console.log('After preprocess:' + h);
\r
520 * Various post process items.
\r
522 _postProcess : function(pl, o) {
\r
523 var t = this, ed = t.editor, dom = ed.dom, styleProps;
\r
525 if (o.wordContent) {
\r
526 // Remove named anchors or TOC links
\r
527 each(dom.select('a', o.node), function(a) {
\r
528 if (!a.href || a.href.indexOf('#_Toc') != -1)
\r
532 if (getParam(ed, "paste_convert_middot_lists")) {
\r
533 t._convertLists(pl, o);
\r
537 styleProps = getParam(ed, "paste_retain_style_properties"); // retained properties
\r
539 // Process only if a string was specified and not equal to "all" or "*"
\r
540 if ((tinymce.is(styleProps, "string")) && (styleProps !== "all") && (styleProps !== "*")) {
\r
541 styleProps = tinymce.explode(styleProps.replace(/^none$/i, ""));
\r
543 // Retains some style properties
\r
544 each(dom.select('*', o.node), function(el) {
\r
545 var newStyle = {}, npc = 0, i, sp, sv;
\r
547 // Store a subset of the existing styles
\r
549 for (i = 0; i < styleProps.length; i++) {
\r
550 sp = styleProps[i];
\r
551 sv = dom.getStyle(el, sp);
\r
560 // Remove all of the existing styles
\r
561 dom.setAttrib(el, 'style', '');
\r
563 if (styleProps && npc > 0)
\r
564 dom.setStyles(el, newStyle); // Add back the stored subset of styles
\r
565 else // Remove empty span tags that do not have class attributes
\r
566 if (el.nodeName == 'SPAN' && !el.className)
\r
567 dom.remove(el, true);
\r
572 // Remove all style information or only specifically on WebKit to avoid the style bug on that browser
\r
573 if (getParam(ed, "paste_remove_styles") || (getParam(ed, "paste_remove_styles_if_webkit") && tinymce.isWebKit)) {
\r
574 each(dom.select('*[style]', o.node), function(el) {
\r
575 el.removeAttribute('style');
\r
576 el.removeAttribute('_mce_style');
\r
579 if (tinymce.isWebKit) {
\r
580 // We need to compress the styles on WebKit since if you paste <img border="0" /> it will become <img border="0" style="... lots of junk ..." />
\r
581 // Removing the mce_style that contains the real value will force the Serializer engine to compress the styles
\r
582 each(dom.select('*', o.node), function(el) {
\r
583 el.removeAttribute('_mce_style');
\r
590 * Converts the most common bullet and number formats in Office into a real semantic UL/LI list.
\r
592 _convertLists : function(pl, o) {
\r
593 var dom = pl.editor.dom, listElm, li, lastMargin = -1, margin, levels = [], lastType, html;
\r
595 // Convert middot lists into real semantic lists
\r
596 each(dom.select('p', o.node), function(p) {
\r
597 var sib, val = '', type, html, idx, parents;
\r
599 // Get text node value at beginning of paragraph
\r
600 for (sib = p.firstChild; sib && sib.nodeType == 3; sib = sib.nextSibling)
\r
601 val += sib.nodeValue;
\r
603 val = p.innerHTML.replace(/<\/?\w+[^>]*>/gi, '').replace(/ /g, '\u00a0');
\r
605 // Detect unordered lists look for bullets
\r
606 if (/^(__MCE_ITEM__)+[\u2022\u00b7\u00a7\u00d8o]\s*\u00a0*/.test(val))
\r
609 // Detect ordered lists 1., a. or ixv.
\r
610 if (/^__MCE_ITEM__\s*\w+\.\s*\u00a0{2,}/.test(val))
\r
613 // Check if node value matches the list pattern: o
\r
615 margin = parseFloat(p.style.marginLeft || 0);
\r
617 if (margin > lastMargin)
\r
618 levels.push(margin);
\r
620 if (!listElm || type != lastType) {
\r
621 listElm = dom.create(type);
\r
622 dom.insertAfter(listElm, p);
\r
624 // Nested list element
\r
625 if (margin > lastMargin) {
\r
626 listElm = li.appendChild(dom.create(type));
\r
627 } else if (margin < lastMargin) {
\r
628 // Find parent level based on margin value
\r
629 idx = tinymce.inArray(levels, margin);
\r
630 parents = dom.getParents(listElm.parentNode, type);
\r
631 listElm = parents[parents.length - 1 - idx] || listElm;
\r
635 // Remove middot or number spans if they exists
\r
636 each(dom.select('span', p), function(span) {
\r
637 var html = span.innerHTML.replace(/<\/?\w+[^>]*>/gi, '');
\r
639 // Remove span with the middot or the number
\r
640 if (type == 'ul' && /^[\u2022\u00b7\u00a7\u00d8o]/.test(html))
\r
642 else if (/^[\s\S]*\w+\.( |\u00a0)*\s*/.test(html))
\r
646 html = p.innerHTML;
\r
648 // Remove middot/list items
\r
650 html = p.innerHTML.replace(/__MCE_ITEM__/g, '').replace(/^[\u2022\u00b7\u00a7\u00d8o]\s*( |\u00a0)+\s*/, '');
\r
652 html = p.innerHTML.replace(/__MCE_ITEM__/g, '').replace(/^\s*\w+\.( |\u00a0)+\s*/, '');
\r
654 // Create li and add paragraph data into the new li
\r
655 li = listElm.appendChild(dom.create('li', 0, html));
\r
658 lastMargin = margin;
\r
661 listElm = lastMargin = 0; // End list element
\r
664 // Remove any left over makers
\r
665 html = o.node.innerHTML;
\r
666 if (html.indexOf('__MCE_ITEM__') != -1)
\r
667 o.node.innerHTML = html.replace(/__MCE_ITEM__/g, '');
\r
671 * This method will split the current block parent and insert the contents inside the split position.
\r
672 * This logic can be improved so text nodes at the start/end remain in the start/end block elements
\r
674 _insertBlockContent : function(ed, dom, content) {
\r
675 var parentBlock, marker, sel = ed.selection, last, elm, vp, y, elmHeight, markerId = 'mce_marker';
\r
677 function select(n) {
\r
680 if (tinymce.isIE) {
\r
681 r = ed.getDoc().body.createTextRange();
\r
682 r.moveToElementText(n);
\r
687 sel.collapse(false);
\r
691 // Insert a marker for the caret position
\r
692 this._insert('<span id="' + markerId + '"></span>', 1);
\r
693 marker = dom.get(markerId);
\r
694 parentBlock = dom.getParent(marker, 'p,h1,h2,h3,h4,h5,h6,ul,ol,th,td');
\r
696 // If it's a parent block but not a table cell
\r
697 if (parentBlock && !/TD|TH/.test(parentBlock.nodeName)) {
\r
698 // Split parent block
\r
699 marker = dom.split(parentBlock, marker);
\r
701 // Insert nodes before the marker
\r
702 each(dom.create('div', 0, content).childNodes, function(n) {
\r
703 last = marker.parentNode.insertBefore(n.cloneNode(true), marker);
\r
706 // Move caret after marker
\r
709 dom.setOuterHTML(marker, content);
\r
710 sel.select(ed.getBody(), 1);
\r
714 // Remove marker if it's left
\r
715 while (elm = dom.get(markerId))
\r
718 // Get element, position and height
\r
719 elm = sel.getStart();
\r
720 vp = dom.getViewPort(ed.getWin());
\r
721 y = ed.dom.getPos(elm).y;
\r
722 elmHeight = elm.clientHeight;
\r
724 // Is element within viewport if not then scroll it into view
\r
725 if (y < vp.y || y + elmHeight > vp.y + vp.h)
\r
726 ed.getDoc().body.scrollTop = y < vp.y ? y : y - vp.h + 25;
\r
730 * Inserts the specified contents at the caret position.
\r
732 _insert : function(h, skip_undo) {
\r
733 var ed = this.editor, r = ed.selection.getRng();
\r
735 // First delete the contents seems to work better on WebKit when the selection spans multiple list items or multiple table cells.
\r
736 if (!ed.selection.isCollapsed() && r.startContainer != r.endContainer)
\r
737 ed.getDoc().execCommand('Delete', false, null);
\r
739 // It's better to use the insertHTML method on Gecko since it will combine paragraphs correctly before inserting the contents
\r
740 ed.execCommand(tinymce.isGecko ? 'insertHTML' : 'mceInsertContent', false, h, {skip_undo : skip_undo});
\r
744 * Instead of the old plain text method which tried to re-create a paste operation, the
\r
745 * new approach adds a plain text mode toggle switch that changes the behavior of paste.
\r
746 * This function is passed the same input that the regular paste plugin produces.
\r
747 * It performs additional scrubbing and produces (and inserts) the plain text.
\r
748 * This approach leverages all of the great existing functionality in the paste
\r
749 * plugin, and requires minimal changes to add the new functionality.
\r
750 * Speednet - June 2009
\r
752 _insertPlainText : function(ed, dom, h) {
\r
753 var i, len, pos, rpos, node, breakElms, before, after,
\r
756 sel = ed.selection,
\r
758 inArray = tinymce.inArray,
\r
759 linebr = getParam(ed, "paste_text_linebreaktype"),
\r
760 rl = getParam(ed, "paste_text_replacements");
\r
762 function process(items) {
\r
763 each(items, function(v) {
\r
764 if (v.constructor == RegExp)
\r
765 h = h.replace(v, "");
\r
767 h = h.replace(v[0], v[1]);
\r
771 if ((typeof(h) === "string") && (h.length > 0)) {
\r
773 entities = ("34,quot,38,amp,39,apos,60,lt,62,gt," + ed.serializer.settings.entities).split(",");
\r
775 // If HTML content with line-breaking tags, then remove all cr/lf chars because only tags will break a line
\r
776 if (/<(?:p|br|h[1-6]|ul|ol|dl|table|t[rdh]|div|blockquote|fieldset|pre|address|center)[^>]*>/i.test(h)) {
\r
781 // Otherwise just get rid of carriage returns (only need linefeeds)
\r
788 [/<\/(?:p|h[1-6]|ul|ol|dl|table|div|blockquote|fieldset|pre|address|center)>/gi, "\n\n"], // Block tags get a blank line after them
\r
789 [/<br[^>]*>|<\/tr>/gi, "\n"], // Single linebreak for <br /> tags and table rows
\r
790 [/<\/t[dh]>\s*<t[dh][^>]*>/gi, "\t"], // Table cells get tabs betweem them
\r
791 /<[a-z!\/?][^>]*>/gi, // Delete all remaining tags
\r
792 [/ /gi, " "], // Convert non-break spaces to regular spaces (remember, *plain text*)
\r
795 /&(#\d+|[a-z0-9]{1,10});/gi,
\r
797 // Replace with actual character
\r
799 if (s.charAt(0) === "#") {
\r
800 return String.fromCharCode(s.slice(1));
\r
803 return ((e = inArray(entities, s)) > 0)? String.fromCharCode(entities[e-1]) : " ";
\r
807 [/(?:(?!\n)\s)*(\n+)(?:(?!\n)\s)*/gi, "$1"], // Cool little RegExp deletes whitespace around linebreak chars.
\r
808 [/\n{3,}/g, "\n\n"], // Max. 2 consecutive linebreaks
\r
809 /^\s+|\s+$/g // Trim the front & back
\r
814 // Delete any highlighted text before pasting
\r
815 if (!sel.isCollapsed()) {
\r
816 d.execCommand("Delete", false, null);
\r
819 // Perform default or custom replacements
\r
820 if (is(rl, "array") || (is(rl, "array"))) {
\r
823 else if (is(rl, "string")) {
\r
824 process(new RegExp(rl, "gi"));
\r
827 // Treat paragraphs as specified in the config
\r
828 if (linebr == "none") {
\r
833 else if (linebr == "br") {
\r
841 [/\n\n/g, "</p><p>"],
\r
846 // This next piece of code handles the situation where we're pasting more than one paragraph of plain
\r
847 // text, and we are pasting the content into the middle of a block node in the editor. The block
\r
848 // node gets split at the selection point into "Para A" and "Para B" (for the purposes of explaining).
\r
849 // The first paragraph of the pasted text is appended to "Para A", and the last paragraph of the
\r
850 // pasted text is prepended to "Para B". Any other paragraphs of pasted text are placed between
\r
851 // "Para A" and "Para B". This code solves a host of problems with the original plain text plugin and
\r
852 // now handles styles correctly. (Pasting plain text into a styled paragraph is supposed to make the
\r
853 // plain text take the same style as the existing paragraph.)
\r
854 if ((pos = h.indexOf("</p><p>")) != -1) {
\r
855 rpos = h.lastIndexOf("</p><p>");
\r
856 node = sel.getNode();
\r
857 breakElms = []; // Get list of elements to break
\r
860 if (node.nodeType == 1) {
\r
861 // Don't break tables and break at body
\r
862 if (node.nodeName == "TD" || node.nodeName == "BODY") {
\r
866 breakElms[breakElms.length] = node;
\r
868 } while (node = node.parentNode);
\r
870 // Are we in the middle of a block node?
\r
871 if (breakElms.length > 0) {
\r
872 before = h.substring(0, pos);
\r
875 for (i=0, len=breakElms.length; i<len; i++) {
\r
876 before += "</" + breakElms[i].nodeName.toLowerCase() + ">";
\r
877 after += "<" + breakElms[breakElms.length-i-1].nodeName.toLowerCase() + ">";
\r
881 h = before + after + h.substring(pos+7);
\r
884 h = before + h.substring(pos+4, rpos+4) + after + h.substring(rpos+7);
\r
889 // Insert content at the caret, plus add a marker for repositioning the caret
\r
890 ed.execCommand("mceInsertRawHTML", false, h + '<span id="_plain_text_marker"> </span>');
\r
892 // Reposition the caret to the marker, which was placed immediately after the inserted content.
\r
893 // Needs to be done asynchronously (in window.setTimeout) or else it doesn't work in all browsers.
\r
894 // The second part of the code scrolls the content up if the caret is positioned off-screen.
\r
895 // This is only necessary for WebKit browsers, but it doesn't hurt to use for all.
\r
896 window.setTimeout(function() {
\r
897 var marker = dom.get('_plain_text_marker'),
\r
898 elm, vp, y, elmHeight;
\r
900 sel.select(marker, false);
\r
901 d.execCommand("Delete", false, null);
\r
904 // Get element, position and height
\r
905 elm = sel.getStart();
\r
906 vp = dom.getViewPort(w);
\r
907 y = dom.getPos(elm).y;
\r
908 elmHeight = elm.clientHeight;
\r
910 // Is element within viewport if not then scroll it into view
\r
911 if ((y < vp.y) || (y + elmHeight > vp.y + vp.h)) {
\r
912 d.body.scrollTop = y < vp.y ? y : y - vp.h + 25;
\r
919 * This method will open the old style paste dialogs. Some users might want the old behavior but still use the new cleanup engine.
\r
921 _legacySupport : function() {
\r
922 var t = this, ed = t.editor;
\r
924 // Register command(s) for backwards compatibility
\r
925 ed.addCommand("mcePasteWord", function() {
\r
926 ed.windowManager.open({
\r
927 file: t.url + "/pasteword.htm",
\r
928 width: parseInt(getParam(ed, "paste_dialog_width")),
\r
929 height: parseInt(getParam(ed, "paste_dialog_height")),
\r
934 if (getParam(ed, "paste_text_use_dialog")) {
\r
935 ed.addCommand("mcePasteText", function() {
\r
936 ed.windowManager.open({
\r
937 file : t.url + "/pastetext.htm",
\r
938 width: parseInt(getParam(ed, "paste_dialog_width")),
\r
939 height: parseInt(getParam(ed, "paste_dialog_height")),
\r
945 // Register button for backwards compatibility
\r
946 ed.addButton("pasteword", {title : "paste.paste_word_desc", cmd : "mcePasteWord"});
\r
951 tinymce.PluginManager.add("paste", tinymce.plugins.PastePlugin);
\r