Browse code

tinyMCE '2' is now 2.1.2 - Seems tinyMCE '2' was actually 3.0.9 (my fault for not checking), so '2' is now 2.1.2, and '3' is now 3.4.8... eFiction 3.4.3 used tinyMCE 2.1.0, and 3.5 upgraded that to 3.0.9.

Clarissa Walker authored on 2026/07/28 07:20:33
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,378 @@
1
+/**
2
+ * $Id$
3
+ *
4
+ * @author Moxiecode
5
+ * @copyright Copyright � 2004-2007, Moxiecode Systems AB, All rights reserved.
6
+ *
7
+ * The contents of this file will be wrapped in a class later on.
8
+ */
9
+
10
+/**#@+
11
+ * @member TinyMCE_Engine
12
+ * @method
13
+ */
14
+tinyMCE.add(TinyMCE_Engine, {
15
+	/**
16
+	 * Parses a URL in to its diffrent components.
17
+	 *
18
+	 * @param {string} url_str URL string to parse into a URL object.
19
+	 * @return URL object based on input string.
20
+	 * @type TinyMCE_URL_Item
21
+	 */
22
+	parseURL : function(url_str) {
23
+		var urlParts = [], i, pos, lastPos, chr;
24
+
25
+		if (url_str) {
26
+			// Parse protocol part
27
+			pos = url_str.indexOf('://');
28
+			if (pos != -1) {
29
+				urlParts.protocol = url_str.substring(0, pos);
30
+				lastPos = pos + 3;
31
+			}
32
+
33
+			// Find port or path start
34
+			for (i=lastPos; i<url_str.length; i++) {
35
+				chr = url_str.charAt(i);
36
+
37
+				if (chr == ':')
38
+					break;
39
+
40
+				if (chr == '/')
41
+					break;
42
+			}
43
+			pos = i;
44
+
45
+			// Get host
46
+			urlParts.host = url_str.substring(lastPos, pos);
47
+
48
+			// Get port
49
+			urlParts.port = "";
50
+			lastPos = pos;
51
+			if (url_str.charAt(pos) == ':') {
52
+				pos = url_str.indexOf('/', lastPos);
53
+				urlParts.port = url_str.substring(lastPos+1, pos);
54
+			}
55
+
56
+			// Get path
57
+			lastPos = pos;
58
+			pos = url_str.indexOf('?', lastPos);
59
+
60
+			if (pos == -1)
61
+				pos = url_str.indexOf('#', lastPos);
62
+
63
+			if (pos == -1)
64
+				pos = url_str.length;
65
+
66
+			urlParts.path = url_str.substring(lastPos, pos);
67
+
68
+			// Get query
69
+			lastPos = pos;
70
+			if (url_str.charAt(pos) == '?') {
71
+				pos = url_str.indexOf('#');
72
+				pos = (pos == -1) ? url_str.length : pos;
73
+				urlParts.query = url_str.substring(lastPos+1, pos);
74
+			}
75
+
76
+			// Get anchor
77
+			lastPos = pos;
78
+			if (url_str.charAt(pos) == '#') {
79
+				pos = url_str.length;
80
+				urlParts.anchor = url_str.substring(lastPos+1, pos);
81
+			}
82
+		}
83
+
84
+		return urlParts;
85
+	},
86
+
87
+	/**
88
+	 * Serializes the specified URL object into a string.
89
+	 *
90
+	 * @param {TinyMCE_URL_Item} up URL object to serialize.
91
+	 * @return Serialized URL object.
92
+	 * @type string
93
+	 */
94
+	serializeURL : function(up) {
95
+		var o = "";
96
+
97
+		if (up.protocol)
98
+			o += up.protocol + "://";
99
+
100
+		if (up.host)
101
+			o += up.host;
102
+
103
+		if (up.port)
104
+			o += ":" + up.port;
105
+
106
+		if (up.path)
107
+			o += up.path;
108
+
109
+		if (up.query)
110
+			o += "?" + up.query;
111
+
112
+		if (up.anchor)
113
+			o += "#" + up.anchor;
114
+
115
+		return o;
116
+	},
117
+
118
+	/**
119
+	 * Converts an absolute path to relative path.
120
+	 *
121
+	 * @param {string} base_url URL to make as a base path, URLs will be converted relative from this point.
122
+	 * @param {string} url_to_relative URL to convert into a relative URL.
123
+	 * @return Relative URL based in input.
124
+	 * @type string
125
+	 */
126
+	convertAbsoluteURLToRelativeURL : function(base_url, url_to_relative) {
127
+		var baseURL = this.parseURL(base_url), targetURL = this.parseURL(url_to_relative);
128
+		var i, strTok1, strTok2, breakPoint = 0, outPath = "", forceSlash = false;
129
+		var fileName, pos;
130
+
131
+		if (targetURL.path == '')
132
+			targetURL.path = "/";
133
+		else
134
+			forceSlash = true;
135
+
136
+		// Crop away last path part
137
+		base_url = baseURL.path.substring(0, baseURL.path.lastIndexOf('/'));
138
+		strTok1 = base_url.split('/');
139
+		strTok2 = targetURL.path.split('/');
140
+
141
+		if (strTok1.length >= strTok2.length) {
142
+			for (i=0; i<strTok1.length; i++) {
143
+				if (i >= strTok2.length || strTok1[i] != strTok2[i]) {
144
+					breakPoint = i + 1;
145
+					break;
146
+				}
147
+			}
148
+		}
149
+
150
+		if (strTok1.length < strTok2.length) {
151
+			for (i=0; i<strTok2.length; i++) {
152
+				if (i >= strTok1.length || strTok1[i] != strTok2[i]) {
153
+					breakPoint = i + 1;
154
+					break;
155
+				}
156
+			}
157
+		}
158
+
159
+		if (breakPoint == 1)
160
+			return targetURL.path;
161
+
162
+		for (i=0; i<(strTok1.length-(breakPoint-1)); i++)
163
+			outPath += "../";
164
+
165
+		for (i=breakPoint-1; i<strTok2.length; i++) {
166
+			if (i != (breakPoint-1))
167
+				outPath += "/" + strTok2[i];
168
+			else
169
+				outPath += strTok2[i];
170
+		}
171
+
172
+		targetURL.protocol = null;
173
+		targetURL.host = null;
174
+		targetURL.port = null;
175
+		targetURL.path = outPath == '' && forceSlash ? "/" : outPath;
176
+
177
+		// Remove document prefix from local anchors
178
+		fileName = baseURL.path;
179
+
180
+		if ((pos = fileName.lastIndexOf('/')) != -1)
181
+			fileName = fileName.substring(pos + 1);
182
+
183
+		// Is local anchor
184
+		if (fileName == targetURL.path && targetURL.anchor !== '')
185
+			targetURL.path = "";
186
+
187
+		// If empty and not local anchor force filename or slash
188
+		if (targetURL.path == '' && !targetURL.anchor)
189
+			targetURL.path = fileName !== '' ? fileName : "/";
190
+
191
+		return this.serializeURL(targetURL);
192
+	},
193
+
194
+	/**
195
+	 * Converts an relative path to absolute path.
196
+	 *
197
+	 * @param {string} base_url URL to make as a base path, URLs will be converted absolute from this point.
198
+	 * @param {string} relative_url URL to convert into a absolute URL.
199
+	 * @return Absolute URL based in input.
200
+	 * @type string
201
+	 */
202
+	convertRelativeToAbsoluteURL : function(base_url, relative_url) {
203
+		var baseURL = this.parseURL(base_url), baseURLParts, relURLParts, newRelURLParts, numBack, relURL = this.parseURL(relative_url), i;
204
+		var len, absPath, start, end, newBaseURLParts;
205
+
206
+		if (relative_url == '' || relative_url.indexOf('://') != -1 || /^(mailto:|javascript:|#|\/)/.test(relative_url))
207
+			return relative_url;
208
+
209
+		// Split parts
210
+		baseURLParts = baseURL.path.split('/');
211
+		relURLParts = relURL.path.split('/');
212
+
213
+		// Remove empty chunks
214
+		newBaseURLParts = [];
215
+		for (i=baseURLParts.length-1; i>=0; i--) {
216
+			if (baseURLParts[i].length == 0)
217
+				continue;
218
+
219
+			newBaseURLParts[newBaseURLParts.length] = baseURLParts[i];
220
+		}
221
+		baseURLParts = newBaseURLParts.reverse();
222
+
223
+		// Merge relURLParts chunks
224
+		newRelURLParts = [];
225
+		numBack = 0;
226
+		for (i=relURLParts.length-1; i>=0; i--) {
227
+			if (relURLParts[i].length == 0 || relURLParts[i] == ".")
228
+				continue;
229
+
230
+			if (relURLParts[i] == '..') {
231
+				numBack++;
232
+				continue;
233
+			}
234
+
235
+			if (numBack > 0) {
236
+				numBack--;
237
+				continue;
238
+			}
239
+
240
+			newRelURLParts[newRelURLParts.length] = relURLParts[i];
241
+		}
242
+
243
+		relURLParts = newRelURLParts.reverse();
244
+
245
+		// Remove end from absolute path
246
+		len = baseURLParts.length-numBack;
247
+		absPath = (len <= 0 ? "" : "/") + baseURLParts.slice(0, len).join('/') + "/" + relURLParts.join('/');
248
+		start = "";
249
+		end = "";
250
+
251
+		// Build output URL
252
+		relURL.protocol = baseURL.protocol;
253
+		relURL.host = baseURL.host;
254
+		relURL.port = baseURL.port;
255
+
256
+		// Re-add trailing slash if it's removed
257
+		if (relURL.path.charAt(relURL.path.length-1) == "/")
258
+			absPath += "/";
259
+
260
+		relURL.path = absPath;
261
+
262
+		return this.serializeURL(relURL);
263
+	},
264
+
265
+	/**
266
+	 * Converts the specified URL based in TinyMCE configuration settings.
267
+	 *
268
+	 * @param {string} url URL to convert based on config.
269
+	 * @param {HTMLElement} node HTML element that holds the URL.
270
+	 * @param {boolean} on_save Is this convertion the final output URL.
271
+	 * @return Converted URL string.
272
+	 * @type string
273
+	 */
274
+	convertURL : function(url, node, on_save) {
275
+		var dl = document.location, start, portPart, urlParts, baseUrlParts, tmpUrlParts, curl;
276
+		var prot = dl.protocol, host = dl.hostname, port = dl.port;
277
+
278
+		// Pass through file protocol
279
+		if (prot == "file:")
280
+			return url;
281
+
282
+		// Something is wrong, remove weirdness
283
+		url = tinyMCE.regexpReplace(url, '(http|https):///', '/');
284
+
285
+		// Mailto link or anchor (Pass through)
286
+		if (url.indexOf('mailto:') != -1 || url.indexOf('javascript:') != -1 || /^[ \t\r\n\+]*[#\?]/.test(url))
287
+			return url;
288
+
289
+		// Fix relative/Mozilla
290
+		if (!tinyMCE.isIE && !on_save && url.indexOf("://") == -1 && url.charAt(0) != '/')
291
+			return tinyMCE.settings.base_href + url;
292
+
293
+		// Handle relative URLs
294
+		if (on_save && tinyMCE.getParam('relative_urls')) {
295
+			curl = tinyMCE.convertRelativeToAbsoluteURL(tinyMCE.settings.base_href, url);
296
+			if (curl.charAt(0) == '/')
297
+				curl = tinyMCE.settings.document_base_prefix + curl;
298
+
299
+			urlParts = tinyMCE.parseURL(curl);
300
+			tmpUrlParts = tinyMCE.parseURL(tinyMCE.settings.document_base_url);
301
+
302
+			// Force relative
303
+			if (urlParts.host == tmpUrlParts.host && (urlParts.port == tmpUrlParts.port))
304
+				return tinyMCE.convertAbsoluteURLToRelativeURL(tinyMCE.settings.document_base_url, curl);
305
+		}
306
+
307
+		// Handle absolute URLs
308
+		if (!tinyMCE.getParam('relative_urls')) {
309
+			urlParts = tinyMCE.parseURL(url);
310
+			baseUrlParts = tinyMCE.parseURL(tinyMCE.settings.base_href);
311
+
312
+			// Force absolute URLs from relative URLs
313
+			url = tinyMCE.convertRelativeToAbsoluteURL(tinyMCE.settings.base_href, url);
314
+
315
+			// If anchor and path is the same page
316
+			if (urlParts.anchor && urlParts.path == baseUrlParts.path)
317
+				return "#" + urlParts.anchor;
318
+		}
319
+
320
+		// Remove current domain
321
+		if (tinyMCE.getParam('remove_script_host')) {
322
+			start = "";
323
+			portPart = "";
324
+
325
+			if (port !== '')
326
+				portPart = ":" + port;
327
+
328
+			start = prot + "//" + host + portPart + "/";
329
+
330
+			if (url.indexOf(start) == 0)
331
+				url = url.substring(start.length-1);
332
+		}
333
+
334
+		return url;
335
+	},
336
+
337
+	/**
338
+	 * Converts all img and a element URLs to absolute URLs. This will use the mce_src or mce_href attribute values
339
+	 * if they are provided. This function is used when the editor is initialized.
340
+	 *
341
+	 * @param {HTMLElement} body HTML element to convert all URLs in.
342
+	 */
343
+	convertAllRelativeURLs : function(body) {
344
+		var i, elms, src, href, mhref, msrc;
345
+
346
+		// Convert all image URL:s to absolute URL
347
+		elms = body.getElementsByTagName("img");
348
+		for (i=0; i<elms.length; i++) {
349
+			src = tinyMCE.getAttrib(elms[i], 'src');
350
+
351
+			msrc = tinyMCE.getAttrib(elms[i], 'mce_src');
352
+			if (msrc !== '')
353
+				src = msrc;
354
+
355
+			if (src !== '') {
356
+				src = tinyMCE.convertRelativeToAbsoluteURL(tinyMCE.settings.base_href, src);
357
+				elms[i].setAttribute("src", src);
358
+			}
359
+		}
360
+
361
+		// Convert all link URL:s to absolute URL
362
+		elms = body.getElementsByTagName("a");
363
+		for (i=0; i<elms.length; i++) {
364
+			href = tinyMCE.getAttrib(elms[i], 'href');
365
+
366
+			mhref = tinyMCE.getAttrib(elms[i], 'mce_href');
367
+			if (mhref !== '')
368
+				href = mhref;
369
+
370
+			if (href && href !== '') {
371
+				href = tinyMCE.convertRelativeToAbsoluteURL(tinyMCE.settings.base_href, href);
372
+				elms[i].setAttribute("href", href);
373
+			}
374
+		}
375
+	}
376
+
377
+	/**#@-*/
378
+});