src/pyams_skin/resources/js/ext/tinymce/dev/plugins/autolink/plugin.js
changeset 69 a361355b55c7
equal deleted inserted replaced
68:fd8fb93e1b6a 69:a361355b55c7
       
     1 /**
       
     2  * plugin.js
       
     3  *
       
     4  * Copyright 2011, Moxiecode Systems AB
       
     5  * Released under LGPL License.
       
     6  *
       
     7  * License: http://www.tinymce.com/license
       
     8  * Contributing: http://www.tinymce.com/contributing
       
     9  */
       
    10 
       
    11 /*global tinymce:true */
       
    12 
       
    13 tinymce.PluginManager.add('autolink', function(editor) {
       
    14 	var AutoUrlDetectState;
       
    15 
       
    16 	editor.on("keydown", function(e) {
       
    17 		if (e.keyCode == 13) {
       
    18 			return handleEnter(editor);
       
    19 		}
       
    20 	});
       
    21 
       
    22 	// Internet Explorer has built-in automatic linking for most cases
       
    23 	if (tinymce.Env.ie) {
       
    24 		editor.on("focus", function() {
       
    25 			if (!AutoUrlDetectState) {
       
    26 				AutoUrlDetectState = true;
       
    27 
       
    28 				try {
       
    29 					editor.execCommand('AutoUrlDetect', false, true);
       
    30 				} catch (ex) {
       
    31 					// Ignore
       
    32 				}
       
    33 			}
       
    34 		});
       
    35 
       
    36 		return;
       
    37 	}
       
    38 
       
    39 	editor.on("keypress", function(e) {
       
    40 		if (e.keyCode == 41) {
       
    41 			return handleEclipse(editor);
       
    42 		}
       
    43 	});
       
    44 
       
    45 	editor.on("keyup", function(e) {
       
    46 		if (e.keyCode == 32) {
       
    47 			return handleSpacebar(editor);
       
    48 		}
       
    49 	});
       
    50 
       
    51 	function handleEclipse(editor) {
       
    52 		parseCurrentLine(editor, -1, '(', true);
       
    53 	}
       
    54 
       
    55 	function handleSpacebar(editor) {
       
    56 		parseCurrentLine(editor, 0, '', true);
       
    57 	}
       
    58 
       
    59 	function handleEnter(editor) {
       
    60 		parseCurrentLine(editor, -1, '', false);
       
    61 	}
       
    62 
       
    63 	function parseCurrentLine(editor, end_offset, delimiter) {
       
    64 		var rng, end, start, endContainer, bookmark, text, matches, prev, len, rngText;
       
    65 
       
    66 		function scopeIndex(container, index) {
       
    67 			if (index < 0) {
       
    68 				index = 0;
       
    69 			}
       
    70 
       
    71 			if (container.nodeType == 3) {
       
    72 				var len = container.data.length;
       
    73 
       
    74 				if (index > len) {
       
    75 					index = len;
       
    76 				}
       
    77 			}
       
    78 
       
    79 			return index;
       
    80 		}
       
    81 
       
    82 		function setStart(container, offset) {
       
    83 			if (container.nodeType != 1 || container.hasChildNodes()) {
       
    84 				rng.setStart(container, scopeIndex(container, offset));
       
    85 			} else {
       
    86 				rng.setStartBefore(container);
       
    87 			}
       
    88 		}
       
    89 
       
    90 		function setEnd(container, offset) {
       
    91 			if (container.nodeType != 1 || container.hasChildNodes()) {
       
    92 				rng.setEnd(container, scopeIndex(container, offset));
       
    93 			} else {
       
    94 				rng.setEndAfter(container);
       
    95 			}
       
    96 		}
       
    97 
       
    98 		// We need at least five characters to form a URL,
       
    99 		// hence, at minimum, five characters from the beginning of the line.
       
   100 		rng = editor.selection.getRng(true).cloneRange();
       
   101 		if (rng.startOffset < 5) {
       
   102 			// During testing, the caret is placed inbetween two text nodes.
       
   103 			// The previous text node contains the URL.
       
   104 			prev = rng.endContainer.previousSibling;
       
   105 			if (!prev) {
       
   106 				if (!rng.endContainer.firstChild || !rng.endContainer.firstChild.nextSibling) {
       
   107 					return;
       
   108 				}
       
   109 
       
   110 				prev = rng.endContainer.firstChild.nextSibling;
       
   111 			}
       
   112 
       
   113 			len = prev.length;
       
   114 			setStart(prev, len);
       
   115 			setEnd(prev, len);
       
   116 
       
   117 			if (rng.endOffset < 5) {
       
   118 				return;
       
   119 			}
       
   120 
       
   121 			end = rng.endOffset;
       
   122 			endContainer = prev;
       
   123 		} else {
       
   124 			endContainer = rng.endContainer;
       
   125 
       
   126 			// Get a text node
       
   127 			if (endContainer.nodeType != 3 && endContainer.firstChild) {
       
   128 				while (endContainer.nodeType != 3 && endContainer.firstChild) {
       
   129 					endContainer = endContainer.firstChild;
       
   130 				}
       
   131 
       
   132 				// Move range to text node
       
   133 				if (endContainer.nodeType == 3) {
       
   134 					setStart(endContainer, 0);
       
   135 					setEnd(endContainer, endContainer.nodeValue.length);
       
   136 				}
       
   137 			}
       
   138 
       
   139 			if (rng.endOffset == 1) {
       
   140 				end = 2;
       
   141 			} else {
       
   142 				end = rng.endOffset - 1 - end_offset;
       
   143 			}
       
   144 		}
       
   145 
       
   146 		start = end;
       
   147 
       
   148 		do {
       
   149 			// Move the selection one character backwards.
       
   150 			setStart(endContainer, end >= 2 ? end - 2 : 0);
       
   151 			setEnd(endContainer, end >= 1 ? end - 1 : 0);
       
   152 			end -= 1;
       
   153 			rngText = rng.toString();
       
   154 
       
   155 			// Loop until one of the following is found: a blank space, &nbsp;, delimiter, (end-2) >= 0
       
   156 		} while (rngText != ' ' && rngText !== '' && rngText.charCodeAt(0) != 160 && (end - 2) >= 0 && rngText != delimiter);
       
   157 
       
   158 		if (rng.toString() == delimiter || rng.toString().charCodeAt(0) == 160) {
       
   159 			setStart(endContainer, end);
       
   160 			setEnd(endContainer, start);
       
   161 			end += 1;
       
   162 		} else if (rng.startOffset === 0) {
       
   163 			setStart(endContainer, 0);
       
   164 			setEnd(endContainer, start);
       
   165 		} else {
       
   166 			setStart(endContainer, end);
       
   167 			setEnd(endContainer, start);
       
   168 		}
       
   169 
       
   170 		// Exclude last . from word like "www.site.com."
       
   171 		text = rng.toString();
       
   172 		if (text.charAt(text.length - 1) == '.') {
       
   173 			setEnd(endContainer, start - 1);
       
   174 		}
       
   175 
       
   176 		text = rng.toString();
       
   177 		matches = text.match(/^(https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.|(?:mailto:)?[A-Z0-9._%+\-]+@)(.+)$/i);
       
   178 
       
   179 		if (matches) {
       
   180 			if (matches[1] == 'www.') {
       
   181 				matches[1] = 'http://www.';
       
   182 			} else if (/@$/.test(matches[1]) && !/^mailto:/.test(matches[1])) {
       
   183 				matches[1] = 'mailto:' + matches[1];
       
   184 			}
       
   185 
       
   186 			bookmark = editor.selection.getBookmark();
       
   187 
       
   188 			editor.selection.setRng(rng);
       
   189 			editor.execCommand('createlink', false, matches[1] + matches[2]);
       
   190 			editor.selection.moveToBookmark(bookmark);
       
   191 			editor.nodeChanged();
       
   192 		}
       
   193 	}
       
   194 });