annotate media/js/tiny_mce/tiny_mce_src.js @ 234:f53eb0aae7a9

Fix #89; avatar caching was broken.
author Brian Neal <bgneal@gmail.com>
date Sun, 01 Aug 2010 22:14:01 +0000
parents 237710206167
children 6ed2932901fa
rev   line source
bgneal@183 1 (function(win) {
bgneal@183 2 var whiteSpaceRe = /^\s*|\s*$/g,
bgneal@183 3 undefined;
bgneal@183 4
bgneal@183 5 var tinymce = {
bgneal@183 6 majorVersion : '3',
bgneal@183 7
bgneal@217 8 minorVersion : '3.6',
bgneal@217 9
bgneal@217 10 releaseDate : '2010-05-20',
bgneal@183 11
bgneal@183 12 _init : function() {
bgneal@183 13 var t = this, d = document, na = navigator, ua = na.userAgent, i, nl, n, base, p, v;
bgneal@183 14
bgneal@183 15 t.isOpera = win.opera && opera.buildNumber;
bgneal@183 16
bgneal@183 17 t.isWebKit = /WebKit/.test(ua);
bgneal@183 18
bgneal@183 19 t.isIE = !t.isWebKit && !t.isOpera && (/MSIE/gi).test(ua) && (/Explorer/gi).test(na.appName);
bgneal@183 20
bgneal@183 21 t.isIE6 = t.isIE && /MSIE [56]/.test(ua);
bgneal@183 22
bgneal@183 23 t.isGecko = !t.isWebKit && /Gecko/.test(ua);
bgneal@183 24
bgneal@183 25 t.isMac = ua.indexOf('Mac') != -1;
bgneal@183 26
bgneal@183 27 t.isAir = /adobeair/i.test(ua);
bgneal@183 28
bgneal@217 29 t.isIDevice = /(iPad|iPhone)/.test(ua);
bgneal@217 30
bgneal@183 31 // TinyMCE .NET webcontrol might be setting the values for TinyMCE
bgneal@183 32 if (win.tinyMCEPreInit) {
bgneal@183 33 t.suffix = tinyMCEPreInit.suffix;
bgneal@183 34 t.baseURL = tinyMCEPreInit.base;
bgneal@183 35 t.query = tinyMCEPreInit.query;
bgneal@45 36 return;
bgneal@183 37 }
bgneal@183 38
bgneal@183 39 // Get suffix and base
bgneal@183 40 t.suffix = '';
bgneal@183 41
bgneal@183 42 // If base element found, add that infront of baseURL
bgneal@183 43 nl = d.getElementsByTagName('base');
bgneal@183 44 for (i=0; i<nl.length; i++) {
bgneal@183 45 if (v = nl[i].href) {
bgneal@183 46 // Host only value like http://site.com or http://site.com:8008
bgneal@183 47 if (/^https?:\/\/[^\/]+$/.test(v))
bgneal@183 48 v += '/';
bgneal@183 49
bgneal@183 50 base = v ? v.match(/.*\//)[0] : ''; // Get only directory
bgneal@183 51 }
bgneal@183 52 }
bgneal@183 53
bgneal@183 54 function getBase(n) {
bgneal@183 55 if (n.src && /tiny_mce(|_gzip|_jquery|_prototype)(_dev|_src)?.js/.test(n.src)) {
bgneal@183 56 if (/_(src|dev)\.js/g.test(n.src))
bgneal@183 57 t.suffix = '_src';
bgneal@183 58
bgneal@183 59 if ((p = n.src.indexOf('?')) != -1)
bgneal@183 60 t.query = n.src.substring(p + 1);
bgneal@183 61
bgneal@183 62 t.baseURL = n.src.substring(0, n.src.lastIndexOf('/'));
bgneal@183 63
bgneal@183 64 // If path to script is relative and a base href was found add that one infront
bgneal@183 65 // the src property will always be an absolute one on non IE browsers and IE 8
bgneal@183 66 // so this logic will basically only be executed on older IE versions
bgneal@183 67 if (base && t.baseURL.indexOf('://') == -1 && t.baseURL.indexOf('/') !== 0)
bgneal@183 68 t.baseURL = base + t.baseURL;
bgneal@183 69
bgneal@183 70 return t.baseURL;
bgneal@183 71 }
bgneal@183 72
bgneal@183 73 return null;
bgneal@183 74 };
bgneal@183 75
bgneal@183 76 // Check document
bgneal@183 77 nl = d.getElementsByTagName('script');
bgneal@45 78 for (i=0; i<nl.length; i++) {
bgneal@45 79 if (getBase(nl[i]))
bgneal@45 80 return;
bgneal@45 81 }
bgneal@183 82
bgneal@183 83 // Check head
bgneal@183 84 n = d.getElementsByTagName('head')[0];
bgneal@183 85 if (n) {
bgneal@183 86 nl = n.getElementsByTagName('script');
bgneal@183 87 for (i=0; i<nl.length; i++) {
bgneal@183 88 if (getBase(nl[i]))
bgneal@183 89 return;
bgneal@183 90 }
bgneal@183 91 }
bgneal@183 92
bgneal@183 93 return;
bgneal@183 94 },
bgneal@183 95
bgneal@183 96 is : function(o, t) {
bgneal@183 97 if (!t)
bgneal@183 98 return o !== undefined;
bgneal@183 99
bgneal@183 100 if (t == 'array' && (o.hasOwnProperty && o instanceof Array))
bgneal@183 101 return true;
bgneal@183 102
bgneal@183 103 return typeof(o) == t;
bgneal@183 104 },
bgneal@183 105
bgneal@183 106 each : function(o, cb, s) {
bgneal@183 107 var n, l;
bgneal@183 108
bgneal@183 109 if (!o)
bgneal@183 110 return 0;
bgneal@183 111
bgneal@183 112 s = s || o;
bgneal@183 113
bgneal@183 114 if (o.length !== undefined) {
bgneal@183 115 // Indexed arrays, needed for Safari
bgneal@183 116 for (n=0, l = o.length; n < l; n++) {
bgneal@45 117 if (cb.call(s, o[n], n, o) === false)
bgneal@45 118 return 0;
bgneal@45 119 }
bgneal@45 120 } else {
bgneal@183 121 // Hashtables
bgneal@183 122 for (n in o) {
bgneal@183 123 if (o.hasOwnProperty(n)) {
bgneal@183 124 if (cb.call(s, o[n], n, o) === false)
bgneal@183 125 return 0;
bgneal@183 126 }
bgneal@183 127 }
bgneal@183 128 }
bgneal@183 129
bgneal@183 130 return 1;
bgneal@183 131 },
bgneal@183 132
bgneal@183 133
bgneal@183 134 map : function(a, f) {
bgneal@183 135 var o = [];
bgneal@183 136
bgneal@183 137 tinymce.each(a, function(v) {
bgneal@183 138 o.push(f(v));
bgneal@183 139 });
bgneal@183 140
bgneal@183 141 return o;
bgneal@183 142 },
bgneal@183 143
bgneal@183 144 grep : function(a, f) {
bgneal@183 145 var o = [];
bgneal@183 146
bgneal@183 147 tinymce.each(a, function(v) {
bgneal@183 148 if (!f || f(v))
bgneal@183 149 o.push(v);
bgneal@183 150 });
bgneal@183 151
bgneal@183 152 return o;
bgneal@183 153 },
bgneal@183 154
bgneal@183 155 inArray : function(a, v) {
bgneal@183 156 var i, l;
bgneal@183 157
bgneal@183 158 if (a) {
bgneal@183 159 for (i = 0, l = a.length; i < l; i++) {
bgneal@183 160 if (a[i] === v)
bgneal@183 161 return i;
bgneal@183 162 }
bgneal@183 163 }
bgneal@183 164
bgneal@183 165 return -1;
bgneal@183 166 },
bgneal@183 167
bgneal@183 168 extend : function(o, e) {
bgneal@183 169 var i, l, a = arguments;
bgneal@183 170
bgneal@183 171 for (i = 1, l = a.length; i < l; i++) {
bgneal@183 172 e = a[i];
bgneal@183 173
bgneal@183 174 tinymce.each(e, function(v, n) {
bgneal@183 175 if (v !== undefined)
bgneal@183 176 o[n] = v;
bgneal@183 177 });
bgneal@183 178 }
bgneal@183 179
bgneal@183 180 return o;
bgneal@183 181 },
bgneal@183 182
bgneal@183 183
bgneal@183 184 trim : function(s) {
bgneal@183 185 return (s ? '' + s : '').replace(whiteSpaceRe, '');
bgneal@183 186 },
bgneal@183 187
bgneal@183 188 create : function(s, p) {
bgneal@183 189 var t = this, sp, ns, cn, scn, c, de = 0;
bgneal@183 190
bgneal@183 191 // Parse : <prefix> <class>:<super class>
bgneal@183 192 s = /^((static) )?([\w.]+)(:([\w.]+))?/.exec(s);
bgneal@183 193 cn = s[3].match(/(^|\.)(\w+)$/i)[2]; // Class name
bgneal@183 194
bgneal@183 195 // Create namespace for new class
bgneal@183 196 ns = t.createNS(s[3].replace(/\.\w+$/, ''));
bgneal@183 197
bgneal@183 198 // Class already exists
bgneal@183 199 if (ns[cn])
bgneal@183 200 return;
bgneal@183 201
bgneal@183 202 // Make pure static class
bgneal@183 203 if (s[2] == 'static') {
bgneal@183 204 ns[cn] = p;
bgneal@183 205
bgneal@183 206 if (this.onCreate)
bgneal@183 207 this.onCreate(s[2], s[3], ns[cn]);
bgneal@183 208
bgneal@183 209 return;
bgneal@183 210 }
bgneal@183 211
bgneal@183 212 // Create default constructor
bgneal@183 213 if (!p[cn]) {
bgneal@183 214 p[cn] = function() {};
bgneal@183 215 de = 1;
bgneal@183 216 }
bgneal@183 217
bgneal@183 218 // Add constructor and methods
bgneal@183 219 ns[cn] = p[cn];
bgneal@183 220 t.extend(ns[cn].prototype, p);
bgneal@183 221
bgneal@183 222 // Extend
bgneal@183 223 if (s[5]) {
bgneal@183 224 sp = t.resolve(s[5]).prototype;
bgneal@183 225 scn = s[5].match(/\.(\w+)$/i)[1]; // Class name
bgneal@183 226
bgneal@183 227 // Extend constructor
bgneal@183 228 c = ns[cn];
bgneal@183 229 if (de) {
bgneal@183 230 // Add passthrough constructor
bgneal@183 231 ns[cn] = function() {
bgneal@183 232 return sp[scn].apply(this, arguments);
bgneal@45 233 };
bgneal@45 234 } else {
bgneal@183 235 // Add inherit constructor
bgneal@183 236 ns[cn] = function() {
bgneal@183 237 this.parent = sp[scn];
bgneal@183 238 return c.apply(this, arguments);
bgneal@183 239 };
bgneal@183 240 }
bgneal@183 241 ns[cn].prototype[cn] = ns[cn];
bgneal@183 242
bgneal@183 243 // Add super methods
bgneal@183 244 t.each(sp, function(f, n) {
bgneal@183 245 ns[cn].prototype[n] = sp[n];
bgneal@183 246 });
bgneal@183 247
bgneal@183 248 // Add overridden methods
bgneal@183 249 t.each(p, function(f, n) {
bgneal@183 250 // Extend methods if needed
bgneal@183 251 if (sp[n]) {
bgneal@183 252 ns[cn].prototype[n] = function() {
bgneal@183 253 this.parent = sp[n];
bgneal@183 254 return f.apply(this, arguments);
bgneal@183 255 };
bgneal@183 256 } else {
bgneal@183 257 if (n != cn)
bgneal@183 258 ns[cn].prototype[n] = f;
bgneal@183 259 }
bgneal@183 260 });
bgneal@183 261 }
bgneal@183 262
bgneal@183 263 // Add static methods
bgneal@183 264 t.each(p['static'], function(f, n) {
bgneal@183 265 ns[cn][n] = f;
bgneal@183 266 });
bgneal@183 267
bgneal@183 268 if (this.onCreate)
bgneal@183 269 this.onCreate(s[2], s[3], ns[cn].prototype);
bgneal@183 270 },
bgneal@183 271
bgneal@183 272 walk : function(o, f, n, s) {
bgneal@183 273 s = s || this;
bgneal@183 274
bgneal@183 275 if (o) {
bgneal@183 276 if (n)
bgneal@183 277 o = o[n];
bgneal@183 278
bgneal@183 279 tinymce.each(o, function(o, i) {
bgneal@183 280 if (f.call(s, o, i, n) === false)
bgneal@183 281 return false;
bgneal@183 282
bgneal@183 283 tinymce.walk(o, f, n, s);
bgneal@183 284 });
bgneal@183 285 }
bgneal@183 286 },
bgneal@183 287
bgneal@183 288 createNS : function(n, o) {
bgneal@183 289 var i, v;
bgneal@183 290
bgneal@183 291 o = o || win;
bgneal@183 292
bgneal@183 293 n = n.split('.');
bgneal@183 294 for (i=0; i<n.length; i++) {
bgneal@183 295 v = n[i];
bgneal@183 296
bgneal@183 297 if (!o[v])
bgneal@183 298 o[v] = {};
bgneal@183 299
bgneal@183 300 o = o[v];
bgneal@183 301 }
bgneal@183 302
bgneal@183 303 return o;
bgneal@183 304 },
bgneal@183 305
bgneal@183 306 resolve : function(n, o) {
bgneal@183 307 var i, l;
bgneal@183 308
bgneal@183 309 o = o || win;
bgneal@183 310
bgneal@183 311 n = n.split('.');
bgneal@183 312 for (i = 0, l = n.length; i < l; i++) {
bgneal@183 313 o = o[n[i]];
bgneal@183 314
bgneal@183 315 if (!o)
bgneal@183 316 break;
bgneal@183 317 }
bgneal@183 318
bgneal@183 319 return o;
bgneal@183 320 },
bgneal@183 321
bgneal@183 322 addUnload : function(f, s) {
bgneal@183 323 var t = this;
bgneal@183 324
bgneal@183 325 f = {func : f, scope : s || this};
bgneal@183 326
bgneal@183 327 if (!t.unloads) {
bgneal@183 328 function unload() {
bgneal@183 329 var li = t.unloads, o, n;
bgneal@183 330
bgneal@183 331 if (li) {
bgneal@183 332 // Call unload handlers
bgneal@183 333 for (n in li) {
bgneal@183 334 o = li[n];
bgneal@183 335
bgneal@183 336 if (o && o.func)
bgneal@183 337 o.func.call(o.scope, 1); // Send in one arg to distinct unload and user destroy
bgneal@183 338 }
bgneal@183 339
bgneal@183 340 // Detach unload function
bgneal@183 341 if (win.detachEvent) {
bgneal@183 342 win.detachEvent('onbeforeunload', fakeUnload);
bgneal@183 343 win.detachEvent('onunload', unload);
bgneal@183 344 } else if (win.removeEventListener)
bgneal@183 345 win.removeEventListener('unload', unload, false);
bgneal@183 346
bgneal@183 347 // Destroy references
bgneal@183 348 t.unloads = o = li = w = unload = 0;
bgneal@183 349
bgneal@183 350 // Run garbarge collector on IE
bgneal@183 351 if (win.CollectGarbage)
bgneal@183 352 CollectGarbage();
bgneal@183 353 }
bgneal@183 354 };
bgneal@183 355
bgneal@183 356 function fakeUnload() {
bgneal@183 357 var d = document;
bgneal@183 358
bgneal@183 359 // Is there things still loading, then do some magic
bgneal@183 360 if (d.readyState == 'interactive') {
bgneal@183 361 function stop() {
bgneal@183 362 // Prevent memory leak
bgneal@183 363 d.detachEvent('onstop', stop);
bgneal@183 364
bgneal@183 365 // Call unload handler
bgneal@183 366 if (unload)
bgneal@183 367 unload();
bgneal@183 368
bgneal@183 369 d = 0;
bgneal@183 370 };
bgneal@183 371
bgneal@183 372 // Fire unload when the currently loading page is stopped
bgneal@183 373 if (d)
bgneal@183 374 d.attachEvent('onstop', stop);
bgneal@183 375
bgneal@183 376 // Remove onstop listener after a while to prevent the unload function
bgneal@183 377 // to execute if the user presses cancel in an onbeforeunload
bgneal@183 378 // confirm dialog and then presses the browser stop button
bgneal@183 379 win.setTimeout(function() {
bgneal@183 380 if (d)
bgneal@183 381 d.detachEvent('onstop', stop);
bgneal@183 382 }, 0);
bgneal@183 383 }
bgneal@183 384 };
bgneal@183 385
bgneal@183 386 // Attach unload handler
bgneal@183 387 if (win.attachEvent) {
bgneal@183 388 win.attachEvent('onunload', unload);
bgneal@183 389 win.attachEvent('onbeforeunload', fakeUnload);
bgneal@183 390 } else if (win.addEventListener)
bgneal@183 391 win.addEventListener('unload', unload, false);
bgneal@183 392
bgneal@183 393 // Setup initial unload handler array
bgneal@183 394 t.unloads = [f];
bgneal@183 395 } else
bgneal@183 396 t.unloads.push(f);
bgneal@183 397
bgneal@183 398 return f;
bgneal@183 399 },
bgneal@183 400
bgneal@183 401 removeUnload : function(f) {
bgneal@183 402 var u = this.unloads, r = null;
bgneal@183 403
bgneal@183 404 tinymce.each(u, function(o, i) {
bgneal@183 405 if (o && o.func == f) {
bgneal@183 406 u.splice(i, 1);
bgneal@183 407 r = f;
bgneal@183 408 return false;
bgneal@183 409 }
bgneal@183 410 });
bgneal@183 411
bgneal@183 412 return r;
bgneal@183 413 },
bgneal@183 414
bgneal@183 415 explode : function(s, d) {
bgneal@183 416 return s ? tinymce.map(s.split(d || ','), tinymce.trim) : s;
bgneal@183 417 },
bgneal@183 418
bgneal@183 419 _addVer : function(u) {
bgneal@183 420 var v;
bgneal@183 421
bgneal@183 422 if (!this.query)
bgneal@183 423 return u;
bgneal@183 424
bgneal@183 425 v = (u.indexOf('?') == -1 ? '?' : '&') + this.query;
bgneal@183 426
bgneal@183 427 if (u.indexOf('#') == -1)
bgneal@183 428 return u + v;
bgneal@183 429
bgneal@183 430 return u.replace('#', v + '#');
bgneal@45 431 }
bgneal@45 432
bgneal@183 433 };
bgneal@183 434
bgneal@183 435 // Initialize the API
bgneal@183 436 tinymce._init();
bgneal@183 437
bgneal@183 438 // Expose tinymce namespace to the global namespace (window)
bgneal@183 439 win.tinymce = win.tinyMCE = tinymce;
bgneal@183 440 })(window);
bgneal@183 441
bgneal@183 442
bgneal@45 443 tinymce.create('tinymce.util.Dispatcher', {
bgneal@45 444 scope : null,
bgneal@45 445 listeners : null,
bgneal@45 446
bgneal@45 447 Dispatcher : function(s) {
bgneal@45 448 this.scope = s || this;
bgneal@45 449 this.listeners = [];
bgneal@45 450 },
bgneal@45 451
bgneal@45 452 add : function(cb, s) {
bgneal@45 453 this.listeners.push({cb : cb, scope : s || this.scope});
bgneal@45 454
bgneal@45 455 return cb;
bgneal@45 456 },
bgneal@45 457
bgneal@45 458 addToTop : function(cb, s) {
bgneal@45 459 this.listeners.unshift({cb : cb, scope : s || this.scope});
bgneal@45 460
bgneal@45 461 return cb;
bgneal@45 462 },
bgneal@45 463
bgneal@45 464 remove : function(cb) {
bgneal@45 465 var l = this.listeners, o = null;
bgneal@45 466
bgneal@45 467 tinymce.each(l, function(c, i) {
bgneal@45 468 if (cb == c.cb) {
bgneal@45 469 o = cb;
bgneal@45 470 l.splice(i, 1);
bgneal@45 471 return false;
bgneal@45 472 }
bgneal@45 473 });
bgneal@45 474
bgneal@45 475 return o;
bgneal@45 476 },
bgneal@45 477
bgneal@45 478 dispatch : function() {
bgneal@45 479 var s, a = arguments, i, li = this.listeners, c;
bgneal@45 480
bgneal@45 481 // Needs to be a real loop since the listener count might change while looping
bgneal@45 482 // And this is also more efficient
bgneal@45 483 for (i = 0; i<li.length; i++) {
bgneal@45 484 c = li[i];
bgneal@45 485 s = c.cb.apply(c.scope, a);
bgneal@45 486
bgneal@45 487 if (s === false)
bgneal@45 488 break;
bgneal@45 489 }
bgneal@45 490
bgneal@45 491 return s;
bgneal@45 492 }
bgneal@45 493
bgneal@45 494 });
bgneal@183 495
bgneal@45 496 (function() {
bgneal@45 497 var each = tinymce.each;
bgneal@45 498
bgneal@45 499 tinymce.create('tinymce.util.URI', {
bgneal@45 500 URI : function(u, s) {
bgneal@45 501 var t = this, o, a, b;
bgneal@45 502
bgneal@183 503 // Trim whitespace
bgneal@183 504 u = tinymce.trim(u);
bgneal@183 505
bgneal@45 506 // Default settings
bgneal@45 507 s = t.settings = s || {};
bgneal@45 508
bgneal@45 509 // Strange app protocol or local anchor
bgneal@183 510 if (/^(mailto|tel|news|javascript|about|data):/i.test(u) || /^\s*#/.test(u)) {
bgneal@45 511 t.source = u;
bgneal@45 512 return;
bgneal@45 513 }
bgneal@45 514
bgneal@45 515 // Absolute path with no host, fake host and protocol
bgneal@45 516 if (u.indexOf('/') === 0 && u.indexOf('//') !== 0)
bgneal@45 517 u = (s.base_uri ? s.base_uri.protocol || 'http' : 'http') + '://mce_host' + u;
bgneal@45 518
bgneal@183 519 // Relative path http:// or protocol relative //path
bgneal@183 520 if (!/^\w*:?\/\//.test(u))
bgneal@45 521 u = (s.base_uri.protocol || 'http') + '://mce_host' + t.toAbsPath(s.base_uri.path, u);
bgneal@45 522
bgneal@45 523 // Parse URL (Credits goes to Steave, http://blog.stevenlevithan.com/archives/parseuri)
bgneal@45 524 u = u.replace(/@@/g, '(mce_at)'); // Zope 3 workaround, they use @@something
bgneal@45 525 u = /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/.exec(u);
bgneal@45 526 each(["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"], function(v, i) {
bgneal@45 527 var s = u[i];
bgneal@45 528
bgneal@45 529 // Zope 3 workaround, they use @@something
bgneal@45 530 if (s)
bgneal@45 531 s = s.replace(/\(mce_at\)/g, '@@');
bgneal@45 532
bgneal@45 533 t[v] = s;
bgneal@45 534 });
bgneal@45 535
bgneal@45 536 if (b = s.base_uri) {
bgneal@45 537 if (!t.protocol)
bgneal@45 538 t.protocol = b.protocol;
bgneal@45 539
bgneal@45 540 if (!t.userInfo)
bgneal@45 541 t.userInfo = b.userInfo;
bgneal@45 542
bgneal@45 543 if (!t.port && t.host == 'mce_host')
bgneal@45 544 t.port = b.port;
bgneal@45 545
bgneal@45 546 if (!t.host || t.host == 'mce_host')
bgneal@45 547 t.host = b.host;
bgneal@45 548
bgneal@45 549 t.source = '';
bgneal@45 550 }
bgneal@45 551
bgneal@45 552 //t.path = t.path || '/';
bgneal@45 553 },
bgneal@45 554
bgneal@45 555 setPath : function(p) {
bgneal@45 556 var t = this;
bgneal@45 557
bgneal@45 558 p = /^(.*?)\/?(\w+)?$/.exec(p);
bgneal@45 559
bgneal@45 560 // Update path parts
bgneal@45 561 t.path = p[0];
bgneal@45 562 t.directory = p[1];
bgneal@45 563 t.file = p[2];
bgneal@45 564
bgneal@45 565 // Rebuild source
bgneal@45 566 t.source = '';
bgneal@45 567 t.getURI();
bgneal@45 568 },
bgneal@45 569
bgneal@45 570 toRelative : function(u) {
bgneal@45 571 var t = this, o;
bgneal@45 572
bgneal@45 573 if (u === "./")
bgneal@45 574 return u;
bgneal@45 575
bgneal@45 576 u = new tinymce.util.URI(u, {base_uri : t});
bgneal@45 577
bgneal@45 578 // Not on same domain/port or protocol
bgneal@45 579 if ((u.host != 'mce_host' && t.host != u.host && u.host) || t.port != u.port || t.protocol != u.protocol)
bgneal@45 580 return u.getURI();
bgneal@45 581
bgneal@45 582 o = t.toRelPath(t.path, u.path);
bgneal@45 583
bgneal@45 584 // Add query
bgneal@45 585 if (u.query)
bgneal@45 586 o += '?' + u.query;
bgneal@45 587
bgneal@45 588 // Add anchor
bgneal@45 589 if (u.anchor)
bgneal@45 590 o += '#' + u.anchor;
bgneal@45 591
bgneal@45 592 return o;
bgneal@45 593 },
bgneal@45 594
bgneal@45 595 toAbsolute : function(u, nh) {
bgneal@45 596 var u = new tinymce.util.URI(u, {base_uri : this});
bgneal@45 597
bgneal@183 598 return u.getURI(this.host == u.host && this.protocol == u.protocol ? nh : 0);
bgneal@45 599 },
bgneal@45 600
bgneal@45 601 toRelPath : function(base, path) {
bgneal@45 602 var items, bp = 0, out = '', i, l;
bgneal@45 603
bgneal@45 604 // Split the paths
bgneal@45 605 base = base.substring(0, base.lastIndexOf('/'));
bgneal@45 606 base = base.split('/');
bgneal@45 607 items = path.split('/');
bgneal@45 608
bgneal@45 609 if (base.length >= items.length) {
bgneal@45 610 for (i = 0, l = base.length; i < l; i++) {
bgneal@45 611 if (i >= items.length || base[i] != items[i]) {
bgneal@45 612 bp = i + 1;
bgneal@45 613 break;
bgneal@45 614 }
bgneal@45 615 }
bgneal@45 616 }
bgneal@45 617
bgneal@45 618 if (base.length < items.length) {
bgneal@45 619 for (i = 0, l = items.length; i < l; i++) {
bgneal@45 620 if (i >= base.length || base[i] != items[i]) {
bgneal@45 621 bp = i + 1;
bgneal@45 622 break;
bgneal@45 623 }
bgneal@45 624 }
bgneal@45 625 }
bgneal@45 626
bgneal@45 627 if (bp == 1)
bgneal@45 628 return path;
bgneal@45 629
bgneal@45 630 for (i = 0, l = base.length - (bp - 1); i < l; i++)
bgneal@45 631 out += "../";
bgneal@45 632
bgneal@45 633 for (i = bp - 1, l = items.length; i < l; i++) {
bgneal@45 634 if (i != bp - 1)
bgneal@45 635 out += "/" + items[i];
bgneal@45 636 else
bgneal@45 637 out += items[i];
bgneal@45 638 }
bgneal@45 639
bgneal@45 640 return out;
bgneal@45 641 },
bgneal@45 642
bgneal@45 643 toAbsPath : function(base, path) {
bgneal@183 644 var i, nb = 0, o = [], tr, outPath;
bgneal@45 645
bgneal@45 646 // Split paths
bgneal@45 647 tr = /\/$/.test(path) ? '/' : '';
bgneal@45 648 base = base.split('/');
bgneal@45 649 path = path.split('/');
bgneal@45 650
bgneal@45 651 // Remove empty chunks
bgneal@45 652 each(base, function(k) {
bgneal@45 653 if (k)
bgneal@45 654 o.push(k);
bgneal@45 655 });
bgneal@45 656
bgneal@45 657 base = o;
bgneal@45 658
bgneal@45 659 // Merge relURLParts chunks
bgneal@45 660 for (i = path.length - 1, o = []; i >= 0; i--) {
bgneal@45 661 // Ignore empty or .
bgneal@45 662 if (path[i].length == 0 || path[i] == ".")
bgneal@45 663 continue;
bgneal@45 664
bgneal@45 665 // Is parent
bgneal@45 666 if (path[i] == '..') {
bgneal@45 667 nb++;
bgneal@45 668 continue;
bgneal@45 669 }
bgneal@45 670
bgneal@45 671 // Move up
bgneal@45 672 if (nb > 0) {
bgneal@45 673 nb--;
bgneal@45 674 continue;
bgneal@45 675 }
bgneal@45 676
bgneal@45 677 o.push(path[i]);
bgneal@45 678 }
bgneal@45 679
bgneal@45 680 i = base.length - nb;
bgneal@45 681
bgneal@45 682 // If /a/b/c or /
bgneal@45 683 if (i <= 0)
bgneal@183 684 outPath = o.reverse().join('/');
bgneal@183 685 else
bgneal@183 686 outPath = base.slice(0, i).join('/') + '/' + o.reverse().join('/');
bgneal@183 687
bgneal@183 688 // Add front / if it's needed
bgneal@183 689 if (outPath.indexOf('/') !== 0)
bgneal@183 690 outPath = '/' + outPath;
bgneal@183 691
bgneal@183 692 // Add traling / if it's needed
bgneal@183 693 if (tr && outPath.lastIndexOf('/') !== outPath.length - 1)
bgneal@183 694 outPath += tr;
bgneal@183 695
bgneal@183 696 return outPath;
bgneal@45 697 },
bgneal@45 698
bgneal@45 699 getURI : function(nh) {
bgneal@45 700 var s, t = this;
bgneal@45 701
bgneal@45 702 // Rebuild source
bgneal@45 703 if (!t.source || nh) {
bgneal@45 704 s = '';
bgneal@45 705
bgneal@45 706 if (!nh) {
bgneal@45 707 if (t.protocol)
bgneal@45 708 s += t.protocol + '://';
bgneal@45 709
bgneal@45 710 if (t.userInfo)
bgneal@45 711 s += t.userInfo + '@';
bgneal@45 712
bgneal@45 713 if (t.host)
bgneal@45 714 s += t.host;
bgneal@45 715
bgneal@45 716 if (t.port)
bgneal@45 717 s += ':' + t.port;
bgneal@45 718 }
bgneal@45 719
bgneal@45 720 if (t.path)
bgneal@45 721 s += t.path;
bgneal@45 722
bgneal@45 723 if (t.query)
bgneal@45 724 s += '?' + t.query;
bgneal@45 725
bgneal@45 726 if (t.anchor)
bgneal@45 727 s += '#' + t.anchor;
bgneal@45 728
bgneal@45 729 t.source = s;
bgneal@45 730 }
bgneal@45 731
bgneal@45 732 return t.source;
bgneal@45 733 }
bgneal@183 734 });
bgneal@45 735 })();
bgneal@183 736
bgneal@45 737 (function() {
bgneal@45 738 var each = tinymce.each;
bgneal@45 739
bgneal@45 740 tinymce.create('static tinymce.util.Cookie', {
bgneal@45 741 getHash : function(n) {
bgneal@45 742 var v = this.get(n), h;
bgneal@45 743
bgneal@45 744 if (v) {
bgneal@45 745 each(v.split('&'), function(v) {
bgneal@45 746 v = v.split('=');
bgneal@45 747 h = h || {};
bgneal@45 748 h[unescape(v[0])] = unescape(v[1]);
bgneal@45 749 });
bgneal@45 750 }
bgneal@45 751
bgneal@45 752 return h;
bgneal@45 753 },
bgneal@45 754
bgneal@45 755 setHash : function(n, v, e, p, d, s) {
bgneal@45 756 var o = '';
bgneal@45 757
bgneal@45 758 each(v, function(v, k) {
bgneal@45 759 o += (!o ? '' : '&') + escape(k) + '=' + escape(v);
bgneal@45 760 });
bgneal@45 761
bgneal@45 762 this.set(n, o, e, p, d, s);
bgneal@45 763 },
bgneal@45 764
bgneal@45 765 get : function(n) {
bgneal@45 766 var c = document.cookie, e, p = n + "=", b;
bgneal@45 767
bgneal@45 768 // Strict mode
bgneal@45 769 if (!c)
bgneal@45 770 return;
bgneal@45 771
bgneal@45 772 b = c.indexOf("; " + p);
bgneal@45 773
bgneal@45 774 if (b == -1) {
bgneal@45 775 b = c.indexOf(p);
bgneal@45 776
bgneal@45 777 if (b != 0)
bgneal@45 778 return null;
bgneal@45 779 } else
bgneal@45 780 b += 2;
bgneal@45 781
bgneal@45 782 e = c.indexOf(";", b);
bgneal@45 783
bgneal@45 784 if (e == -1)
bgneal@45 785 e = c.length;
bgneal@45 786
bgneal@45 787 return unescape(c.substring(b + p.length, e));
bgneal@45 788 },
bgneal@45 789
bgneal@45 790 set : function(n, v, e, p, d, s) {
bgneal@45 791 document.cookie = n + "=" + escape(v) +
bgneal@45 792 ((e) ? "; expires=" + e.toGMTString() : "") +
bgneal@45 793 ((p) ? "; path=" + escape(p) : "") +
bgneal@45 794 ((d) ? "; domain=" + d : "") +
bgneal@45 795 ((s) ? "; secure" : "");
bgneal@45 796 },
bgneal@45 797
bgneal@45 798 remove : function(n, p) {
bgneal@45 799 var d = new Date();
bgneal@45 800
bgneal@45 801 d.setTime(d.getTime() - 1000);
bgneal@45 802
bgneal@45 803 this.set(n, '', d, p, d);
bgneal@45 804 }
bgneal@183 805 });
bgneal@45 806 })();
bgneal@183 807
bgneal@45 808 tinymce.create('static tinymce.util.JSON', {
bgneal@45 809 serialize : function(o) {
bgneal@45 810 var i, v, s = tinymce.util.JSON.serialize, t;
bgneal@45 811
bgneal@45 812 if (o == null)
bgneal@45 813 return 'null';
bgneal@45 814
bgneal@45 815 t = typeof o;
bgneal@45 816
bgneal@45 817 if (t == 'string') {
bgneal@45 818 v = '\bb\tt\nn\ff\rr\""\'\'\\\\';
bgneal@45 819
bgneal@45 820 return '"' + o.replace(/([\u0080-\uFFFF\x00-\x1f\"])/g, function(a, b) {
bgneal@45 821 i = v.indexOf(b);
bgneal@45 822
bgneal@45 823 if (i + 1)
bgneal@45 824 return '\\' + v.charAt(i + 1);
bgneal@45 825
bgneal@45 826 a = b.charCodeAt().toString(16);
bgneal@45 827
bgneal@45 828 return '\\u' + '0000'.substring(a.length) + a;
bgneal@45 829 }) + '"';
bgneal@45 830 }
bgneal@45 831
bgneal@45 832 if (t == 'object') {
bgneal@45 833 if (o.hasOwnProperty && o instanceof Array) {
bgneal@45 834 for (i=0, v = '['; i<o.length; i++)
bgneal@45 835 v += (i > 0 ? ',' : '') + s(o[i]);
bgneal@45 836
bgneal@45 837 return v + ']';
bgneal@45 838 }
bgneal@45 839
bgneal@45 840 v = '{';
bgneal@45 841
bgneal@45 842 for (i in o)
bgneal@45 843 v += typeof o[i] != 'function' ? (v.length > 1 ? ',"' : '"') + i + '":' + s(o[i]) : '';
bgneal@45 844
bgneal@45 845 return v + '}';
bgneal@45 846 }
bgneal@45 847
bgneal@45 848 return '' + o;
bgneal@45 849 },
bgneal@45 850
bgneal@45 851 parse : function(s) {
bgneal@45 852 try {
bgneal@45 853 return eval('(' + s + ')');
bgneal@45 854 } catch (ex) {
bgneal@45 855 // Ignore
bgneal@45 856 }
bgneal@45 857 }
bgneal@45 858
bgneal@45 859 });
bgneal@183 860
bgneal@45 861 tinymce.create('static tinymce.util.XHR', {
bgneal@45 862 send : function(o) {
bgneal@45 863 var x, t, w = window, c = 0;
bgneal@45 864
bgneal@45 865 // Default settings
bgneal@45 866 o.scope = o.scope || this;
bgneal@45 867 o.success_scope = o.success_scope || o.scope;
bgneal@45 868 o.error_scope = o.error_scope || o.scope;
bgneal@45 869 o.async = o.async === false ? false : true;
bgneal@45 870 o.data = o.data || '';
bgneal@45 871
bgneal@45 872 function get(s) {
bgneal@45 873 x = 0;
bgneal@45 874
bgneal@45 875 try {
bgneal@45 876 x = new ActiveXObject(s);
bgneal@45 877 } catch (ex) {
bgneal@45 878 }
bgneal@45 879
bgneal@45 880 return x;
bgneal@45 881 };
bgneal@45 882
bgneal@45 883 x = w.XMLHttpRequest ? new XMLHttpRequest() : get('Microsoft.XMLHTTP') || get('Msxml2.XMLHTTP');
bgneal@45 884
bgneal@45 885 if (x) {
bgneal@45 886 if (x.overrideMimeType)
bgneal@45 887 x.overrideMimeType(o.content_type);
bgneal@45 888
bgneal@45 889 x.open(o.type || (o.data ? 'POST' : 'GET'), o.url, o.async);
bgneal@45 890
bgneal@45 891 if (o.content_type)
bgneal@45 892 x.setRequestHeader('Content-Type', o.content_type);
bgneal@45 893
bgneal@183 894 x.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
bgneal@183 895
bgneal@45 896 x.send(o.data);
bgneal@45 897
bgneal@45 898 function ready() {
bgneal@45 899 if (!o.async || x.readyState == 4 || c++ > 10000) {
bgneal@45 900 if (o.success && c < 10000 && x.status == 200)
bgneal@45 901 o.success.call(o.success_scope, '' + x.responseText, x, o);
bgneal@45 902 else if (o.error)
bgneal@45 903 o.error.call(o.error_scope, c > 10000 ? 'TIMED_OUT' : 'GENERAL', x, o);
bgneal@45 904
bgneal@45 905 x = null;
bgneal@45 906 } else
bgneal@45 907 w.setTimeout(ready, 10);
bgneal@45 908 };
bgneal@45 909
bgneal@45 910 // Syncronous request
bgneal@45 911 if (!o.async)
bgneal@45 912 return ready();
bgneal@45 913
bgneal@45 914 // Wait for response, onReadyStateChange can not be used since it leaks memory in IE
bgneal@45 915 t = w.setTimeout(ready, 10);
bgneal@45 916 }
bgneal@183 917 }
bgneal@45 918 });
bgneal@183 919
bgneal@45 920 (function() {
bgneal@45 921 var extend = tinymce.extend, JSON = tinymce.util.JSON, XHR = tinymce.util.XHR;
bgneal@45 922
bgneal@45 923 tinymce.create('tinymce.util.JSONRequest', {
bgneal@45 924 JSONRequest : function(s) {
bgneal@45 925 this.settings = extend({
bgneal@45 926 }, s);
bgneal@45 927 this.count = 0;
bgneal@45 928 },
bgneal@45 929
bgneal@45 930 send : function(o) {
bgneal@45 931 var ecb = o.error, scb = o.success;
bgneal@45 932
bgneal@45 933 o = extend(this.settings, o);
bgneal@45 934
bgneal@45 935 o.success = function(c, x) {
bgneal@45 936 c = JSON.parse(c);
bgneal@45 937
bgneal@45 938 if (typeof(c) == 'undefined') {
bgneal@45 939 c = {
bgneal@45 940 error : 'JSON Parse error.'
bgneal@45 941 };
bgneal@45 942 }
bgneal@45 943
bgneal@45 944 if (c.error)
bgneal@45 945 ecb.call(o.error_scope || o.scope, c.error, x);
bgneal@45 946 else
bgneal@45 947 scb.call(o.success_scope || o.scope, c.result);
bgneal@45 948 };
bgneal@45 949
bgneal@45 950 o.error = function(ty, x) {
bgneal@45 951 ecb.call(o.error_scope || o.scope, ty, x);
bgneal@45 952 };
bgneal@45 953
bgneal@45 954 o.data = JSON.serialize({
bgneal@45 955 id : o.id || 'c' + (this.count++),
bgneal@45 956 method : o.method,
bgneal@45 957 params : o.params
bgneal@45 958 });
bgneal@45 959
bgneal@45 960 // JSON content type for Ruby on rails. Bug: #1883287
bgneal@45 961 o.content_type = 'application/json';
bgneal@45 962
bgneal@45 963 XHR.send(o);
bgneal@45 964 },
bgneal@45 965
bgneal@45 966 'static' : {
bgneal@45 967 sendRPC : function(o) {
bgneal@45 968 return new tinymce.util.JSONRequest().send(o);
bgneal@45 969 }
bgneal@45 970 }
bgneal@183 971 });
bgneal@183 972 }());
bgneal@183 973 (function(tinymce) {
bgneal@45 974 // Shorten names
bgneal@183 975 var each = tinymce.each,
bgneal@183 976 is = tinymce.is,
bgneal@183 977 isWebKit = tinymce.isWebKit,
bgneal@183 978 isIE = tinymce.isIE,
bgneal@183 979 blockRe = /^(H[1-6R]|P|DIV|ADDRESS|PRE|FORM|T(ABLE|BODY|HEAD|FOOT|H|R|D)|LI|OL|UL|CAPTION|BLOCKQUOTE|CENTER|DL|DT|DD|DIR|FIELDSET|NOSCRIPT|MENU|ISINDEX|SAMP)$/,
bgneal@183 980 boolAttrs = makeMap('checked,compact,declare,defer,disabled,ismap,multiple,nohref,noresize,noshade,nowrap,readonly,selected'),
bgneal@183 981 mceAttribs = makeMap('src,href,style,coords,shape'),
bgneal@183 982 encodedChars = {'&' : '&amp;', '"' : '&quot;', '<' : '&lt;', '>' : '&gt;'},
bgneal@183 983 encodeCharsRe = /[<>&\"]/g,
bgneal@183 984 simpleSelectorRe = /^([a-z0-9],?)+$/i,
bgneal@183 985 tagRegExp = /<(\w+)((?:\s+\w+(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)(\s*\/?)>/g,
bgneal@183 986 attrRegExp = /(\w+)(?:\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|([^>\s]+)))?/g;
bgneal@183 987
bgneal@183 988 function makeMap(str) {
bgneal@183 989 var map = {}, i;
bgneal@183 990
bgneal@183 991 str = str.split(',');
bgneal@183 992 for (i = str.length; i >= 0; i--)
bgneal@183 993 map[str[i]] = 1;
bgneal@183 994
bgneal@183 995 return map;
bgneal@183 996 };
bgneal@45 997
bgneal@45 998 tinymce.create('tinymce.dom.DOMUtils', {
bgneal@45 999 doc : null,
bgneal@45 1000 root : null,
bgneal@45 1001 files : null,
bgneal@45 1002 pixelStyles : /^(top|left|bottom|right|width|height|borderWidth)$/,
bgneal@45 1003 props : {
bgneal@45 1004 "for" : "htmlFor",
bgneal@45 1005 "class" : "className",
bgneal@45 1006 className : "className",
bgneal@45 1007 checked : "checked",
bgneal@45 1008 disabled : "disabled",
bgneal@45 1009 maxlength : "maxLength",
bgneal@45 1010 readonly : "readOnly",
bgneal@45 1011 selected : "selected",
bgneal@45 1012 value : "value",
bgneal@45 1013 id : "id",
bgneal@45 1014 name : "name",
bgneal@45 1015 type : "type"
bgneal@45 1016 },
bgneal@45 1017
bgneal@45 1018 DOMUtils : function(d, s) {
bgneal@183 1019 var t = this, globalStyle;
bgneal@45 1020
bgneal@45 1021 t.doc = d;
bgneal@45 1022 t.win = window;
bgneal@45 1023 t.files = {};
bgneal@45 1024 t.cssFlicker = false;
bgneal@45 1025 t.counter = 0;
bgneal@45 1026 t.boxModel = !tinymce.isIE || d.compatMode == "CSS1Compat";
bgneal@45 1027 t.stdMode = d.documentMode === 8;
bgneal@45 1028
bgneal@183 1029 t.settings = s = tinymce.extend({
bgneal@45 1030 keep_values : false,
bgneal@45 1031 hex_colors : 1,
bgneal@45 1032 process_html : 1
bgneal@45 1033 }, s);
bgneal@45 1034
bgneal@45 1035 // Fix IE6SP2 flicker and check it failed for pre SP2
bgneal@45 1036 if (tinymce.isIE6) {
bgneal@45 1037 try {
bgneal@45 1038 d.execCommand('BackgroundImageCache', false, true);
bgneal@45 1039 } catch (e) {
bgneal@45 1040 t.cssFlicker = true;
bgneal@45 1041 }
bgneal@45 1042 }
bgneal@45 1043
bgneal@183 1044 // Build styles list
bgneal@183 1045 if (s.valid_styles) {
bgneal@183 1046 t._styles = {};
bgneal@183 1047
bgneal@183 1048 // Convert styles into a rule list
bgneal@183 1049 each(s.valid_styles, function(value, key) {
bgneal@183 1050 t._styles[key] = tinymce.explode(value);
bgneal@183 1051 });
bgneal@183 1052 }
bgneal@183 1053
bgneal@45 1054 tinymce.addUnload(t.destroy, t);
bgneal@45 1055 },
bgneal@45 1056
bgneal@45 1057 getRoot : function() {
bgneal@45 1058 var t = this, s = t.settings;
bgneal@45 1059
bgneal@45 1060 return (s && t.get(s.root_element)) || t.doc.body;
bgneal@45 1061 },
bgneal@45 1062
bgneal@45 1063 getViewPort : function(w) {
bgneal@45 1064 var d, b;
bgneal@45 1065
bgneal@45 1066 w = !w ? this.win : w;
bgneal@45 1067 d = w.document;
bgneal@45 1068 b = this.boxModel ? d.documentElement : d.body;
bgneal@45 1069
bgneal@45 1070 // Returns viewport size excluding scrollbars
bgneal@45 1071 return {
bgneal@45 1072 x : w.pageXOffset || b.scrollLeft,
bgneal@45 1073 y : w.pageYOffset || b.scrollTop,
bgneal@45 1074 w : w.innerWidth || b.clientWidth,
bgneal@45 1075 h : w.innerHeight || b.clientHeight
bgneal@45 1076 };
bgneal@45 1077 },
bgneal@45 1078
bgneal@45 1079 getRect : function(e) {
bgneal@45 1080 var p, t = this, sr;
bgneal@45 1081
bgneal@45 1082 e = t.get(e);
bgneal@45 1083 p = t.getPos(e);
bgneal@45 1084 sr = t.getSize(e);
bgneal@45 1085
bgneal@45 1086 return {
bgneal@45 1087 x : p.x,
bgneal@45 1088 y : p.y,
bgneal@45 1089 w : sr.w,
bgneal@45 1090 h : sr.h
bgneal@45 1091 };
bgneal@45 1092 },
bgneal@45 1093
bgneal@45 1094 getSize : function(e) {
bgneal@45 1095 var t = this, w, h;
bgneal@45 1096
bgneal@45 1097 e = t.get(e);
bgneal@45 1098 w = t.getStyle(e, 'width');
bgneal@45 1099 h = t.getStyle(e, 'height');
bgneal@45 1100
bgneal@45 1101 // Non pixel value, then force offset/clientWidth
bgneal@45 1102 if (w.indexOf('px') === -1)
bgneal@45 1103 w = 0;
bgneal@45 1104
bgneal@45 1105 // Non pixel value, then force offset/clientWidth
bgneal@45 1106 if (h.indexOf('px') === -1)
bgneal@45 1107 h = 0;
bgneal@45 1108
bgneal@45 1109 return {
bgneal@45 1110 w : parseInt(w) || e.offsetWidth || e.clientWidth,
bgneal@45 1111 h : parseInt(h) || e.offsetHeight || e.clientHeight
bgneal@45 1112 };
bgneal@45 1113 },
bgneal@45 1114
bgneal@45 1115 getParent : function(n, f, r) {
bgneal@45 1116 return this.getParents(n, f, r, false);
bgneal@45 1117 },
bgneal@45 1118
bgneal@45 1119 getParents : function(n, f, r, c) {
bgneal@45 1120 var t = this, na, se = t.settings, o = [];
bgneal@45 1121
bgneal@45 1122 n = t.get(n);
bgneal@45 1123 c = c === undefined;
bgneal@45 1124
bgneal@45 1125 if (se.strict_root)
bgneal@45 1126 r = r || t.getRoot();
bgneal@45 1127
bgneal@45 1128 // Wrap node name as func
bgneal@45 1129 if (is(f, 'string')) {
bgneal@45 1130 na = f;
bgneal@45 1131
bgneal@45 1132 if (f === '*') {
bgneal@45 1133 f = function(n) {return n.nodeType == 1;};
bgneal@45 1134 } else {
bgneal@45 1135 f = function(n) {
bgneal@45 1136 return t.is(n, na);
bgneal@45 1137 };
bgneal@45 1138 }
bgneal@45 1139 }
bgneal@45 1140
bgneal@45 1141 while (n) {
bgneal@183 1142 if (n == r || !n.nodeType || n.nodeType === 9)
bgneal@45 1143 break;
bgneal@45 1144
bgneal@45 1145 if (!f || f(n)) {
bgneal@45 1146 if (c)
bgneal@45 1147 o.push(n);
bgneal@45 1148 else
bgneal@45 1149 return n;
bgneal@45 1150 }
bgneal@45 1151
bgneal@45 1152 n = n.parentNode;
bgneal@45 1153 }
bgneal@45 1154
bgneal@45 1155 return c ? o : null;
bgneal@45 1156 },
bgneal@45 1157
bgneal@45 1158 get : function(e) {
bgneal@45 1159 var n;
bgneal@45 1160
bgneal@45 1161 if (e && this.doc && typeof(e) == 'string') {
bgneal@45 1162 n = e;
bgneal@45 1163 e = this.doc.getElementById(e);
bgneal@45 1164
bgneal@45 1165 // IE and Opera returns meta elements when they match the specified input ID, but getElementsByName seems to do the trick
bgneal@45 1166 if (e && e.id !== n)
bgneal@45 1167 return this.doc.getElementsByName(n)[1];
bgneal@45 1168 }
bgneal@45 1169
bgneal@45 1170 return e;
bgneal@45 1171 },
bgneal@45 1172
bgneal@183 1173 getNext : function(node, selector) {
bgneal@183 1174 return this._findSib(node, selector, 'nextSibling');
bgneal@183 1175 },
bgneal@183 1176
bgneal@183 1177 getPrev : function(node, selector) {
bgneal@183 1178 return this._findSib(node, selector, 'previousSibling');
bgneal@183 1179 },
bgneal@183 1180
bgneal@45 1181
bgneal@45 1182 select : function(pa, s) {
bgneal@45 1183 var t = this;
bgneal@45 1184
bgneal@45 1185 return tinymce.dom.Sizzle(pa, t.get(s) || t.get(t.settings.root_element) || t.doc, []);
bgneal@45 1186 },
bgneal@45 1187
bgneal@183 1188 is : function(n, selector) {
bgneal@183 1189 var i;
bgneal@183 1190
bgneal@183 1191 // If it isn't an array then try to do some simple selectors instead of Sizzle for to boost performance
bgneal@183 1192 if (n.length === undefined) {
bgneal@183 1193 // Simple all selector
bgneal@183 1194 if (selector === '*')
bgneal@183 1195 return n.nodeType == 1;
bgneal@183 1196
bgneal@183 1197 // Simple selector just elements
bgneal@183 1198 if (simpleSelectorRe.test(selector)) {
bgneal@183 1199 selector = selector.toLowerCase().split(/,/);
bgneal@183 1200 n = n.nodeName.toLowerCase();
bgneal@183 1201
bgneal@183 1202 for (i = selector.length - 1; i >= 0; i--) {
bgneal@183 1203 if (selector[i] == n)
bgneal@183 1204 return true;
bgneal@183 1205 }
bgneal@183 1206
bgneal@183 1207 return false;
bgneal@183 1208 }
bgneal@183 1209 }
bgneal@183 1210
bgneal@183 1211 return tinymce.dom.Sizzle.matches(selector, n.nodeType ? [n] : n).length > 0;
bgneal@183 1212 },
bgneal@183 1213
bgneal@45 1214
bgneal@45 1215 add : function(p, n, a, h, c) {
bgneal@45 1216 var t = this;
bgneal@45 1217
bgneal@45 1218 return this.run(p, function(p) {
bgneal@45 1219 var e, k;
bgneal@45 1220
bgneal@45 1221 e = is(n, 'string') ? t.doc.createElement(n) : n;
bgneal@45 1222 t.setAttribs(e, a);
bgneal@45 1223
bgneal@45 1224 if (h) {
bgneal@45 1225 if (h.nodeType)
bgneal@45 1226 e.appendChild(h);
bgneal@45 1227 else
bgneal@45 1228 t.setHTML(e, h);
bgneal@45 1229 }
bgneal@45 1230
bgneal@45 1231 return !c ? p.appendChild(e) : e;
bgneal@45 1232 });
bgneal@45 1233 },
bgneal@45 1234
bgneal@45 1235 create : function(n, a, h) {
bgneal@45 1236 return this.add(this.doc.createElement(n), n, a, h, 1);
bgneal@45 1237 },
bgneal@45 1238
bgneal@45 1239 createHTML : function(n, a, h) {
bgneal@45 1240 var o = '', t = this, k;
bgneal@45 1241
bgneal@45 1242 o += '<' + n;
bgneal@45 1243
bgneal@45 1244 for (k in a) {
bgneal@45 1245 if (a.hasOwnProperty(k))
bgneal@45 1246 o += ' ' + k + '="' + t.encode(a[k]) + '"';
bgneal@45 1247 }
bgneal@45 1248
bgneal@45 1249 if (tinymce.is(h))
bgneal@45 1250 return o + '>' + h + '</' + n + '>';
bgneal@45 1251
bgneal@45 1252 return o + ' />';
bgneal@45 1253 },
bgneal@45 1254
bgneal@183 1255 remove : function(node, keep_children) {
bgneal@183 1256 return this.run(node, function(node) {
bgneal@183 1257 var parent, child;
bgneal@183 1258
bgneal@183 1259 parent = node.parentNode;
bgneal@183 1260
bgneal@183 1261 if (!parent)
bgneal@45 1262 return null;
bgneal@45 1263
bgneal@183 1264 if (keep_children) {
bgneal@183 1265 while (child = node.firstChild) {
bgneal@183 1266 // IE 8 will crash if you don't remove completely empty text nodes
bgneal@217 1267 if (!tinymce.isIE || child.nodeType !== 3 || child.nodeValue)
bgneal@183 1268 parent.insertBefore(child, node);
bgneal@183 1269 else
bgneal@183 1270 node.removeChild(child);
bgneal@183 1271 }
bgneal@183 1272 }
bgneal@183 1273
bgneal@183 1274 return parent.removeChild(node);
bgneal@183 1275 });
bgneal@183 1276 },
bgneal@45 1277
bgneal@45 1278 setStyle : function(n, na, v) {
bgneal@45 1279 var t = this;
bgneal@45 1280
bgneal@45 1281 return t.run(n, function(e) {
bgneal@45 1282 var s, i;
bgneal@45 1283
bgneal@45 1284 s = e.style;
bgneal@45 1285
bgneal@45 1286 // Camelcase it, if needed
bgneal@45 1287 na = na.replace(/-(\D)/g, function(a, b){
bgneal@45 1288 return b.toUpperCase();
bgneal@45 1289 });
bgneal@45 1290
bgneal@45 1291 // Default px suffix on these
bgneal@45 1292 if (t.pixelStyles.test(na) && (tinymce.is(v, 'number') || /^[\-0-9\.]+$/.test(v)))
bgneal@45 1293 v += 'px';
bgneal@45 1294
bgneal@45 1295 switch (na) {
bgneal@45 1296 case 'opacity':
bgneal@45 1297 // IE specific opacity
bgneal@45 1298 if (isIE) {
bgneal@45 1299 s.filter = v === '' ? '' : "alpha(opacity=" + (v * 100) + ")";
bgneal@45 1300
bgneal@45 1301 if (!n.currentStyle || !n.currentStyle.hasLayout)
bgneal@45 1302 s.display = 'inline-block';
bgneal@45 1303 }
bgneal@45 1304
bgneal@45 1305 // Fix for older browsers
bgneal@45 1306 s[na] = s['-moz-opacity'] = s['-khtml-opacity'] = v || '';
bgneal@45 1307 break;
bgneal@45 1308
bgneal@45 1309 case 'float':
bgneal@45 1310 isIE ? s.styleFloat = v : s.cssFloat = v;
bgneal@45 1311 break;
bgneal@45 1312
bgneal@45 1313 default:
bgneal@45 1314 s[na] = v || '';
bgneal@45 1315 }
bgneal@45 1316
bgneal@45 1317 // Force update of the style data
bgneal@45 1318 if (t.settings.update_styles)
bgneal@183 1319 t.setAttrib(e, '_mce_style');
bgneal@45 1320 });
bgneal@45 1321 },
bgneal@45 1322
bgneal@45 1323 getStyle : function(n, na, c) {
bgneal@45 1324 n = this.get(n);
bgneal@45 1325
bgneal@45 1326 if (!n)
bgneal@45 1327 return false;
bgneal@45 1328
bgneal@45 1329 // Gecko
bgneal@45 1330 if (this.doc.defaultView && c) {
bgneal@45 1331 // Remove camelcase
bgneal@45 1332 na = na.replace(/[A-Z]/g, function(a){
bgneal@45 1333 return '-' + a;
bgneal@45 1334 });
bgneal@45 1335
bgneal@45 1336 try {
bgneal@45 1337 return this.doc.defaultView.getComputedStyle(n, null).getPropertyValue(na);
bgneal@45 1338 } catch (ex) {
bgneal@45 1339 // Old safari might fail
bgneal@45 1340 return null;
bgneal@45 1341 }
bgneal@45 1342 }
bgneal@45 1343
bgneal@45 1344 // Camelcase it, if needed
bgneal@45 1345 na = na.replace(/-(\D)/g, function(a, b){
bgneal@45 1346 return b.toUpperCase();
bgneal@45 1347 });
bgneal@45 1348
bgneal@45 1349 if (na == 'float')
bgneal@45 1350 na = isIE ? 'styleFloat' : 'cssFloat';
bgneal@45 1351
bgneal@45 1352 // IE & Opera
bgneal@45 1353 if (n.currentStyle && c)
bgneal@45 1354 return n.currentStyle[na];
bgneal@45 1355
bgneal@45 1356 return n.style[na];
bgneal@45 1357 },
bgneal@45 1358
bgneal@45 1359 setStyles : function(e, o) {
bgneal@45 1360 var t = this, s = t.settings, ol;
bgneal@45 1361
bgneal@45 1362 ol = s.update_styles;
bgneal@45 1363 s.update_styles = 0;
bgneal@45 1364
bgneal@45 1365 each(o, function(v, n) {
bgneal@45 1366 t.setStyle(e, n, v);
bgneal@45 1367 });
bgneal@45 1368
bgneal@45 1369 // Update style info
bgneal@45 1370 s.update_styles = ol;
bgneal@45 1371 if (s.update_styles)
bgneal@45 1372 t.setAttrib(e, s.cssText);
bgneal@45 1373 },
bgneal@45 1374
bgneal@45 1375 setAttrib : function(e, n, v) {
bgneal@45 1376 var t = this;
bgneal@45 1377
bgneal@45 1378 // Whats the point
bgneal@45 1379 if (!e || !n)
bgneal@45 1380 return;
bgneal@45 1381
bgneal@45 1382 // Strict XML mode
bgneal@45 1383 if (t.settings.strict)
bgneal@45 1384 n = n.toLowerCase();
bgneal@45 1385
bgneal@45 1386 return this.run(e, function(e) {
bgneal@45 1387 var s = t.settings;
bgneal@45 1388
bgneal@45 1389 switch (n) {
bgneal@45 1390 case "style":
bgneal@45 1391 if (!is(v, 'string')) {
bgneal@45 1392 each(v, function(v, n) {
bgneal@45 1393 t.setStyle(e, n, v);
bgneal@45 1394 });
bgneal@45 1395
bgneal@45 1396 return;
bgneal@45 1397 }
bgneal@45 1398
bgneal@45 1399 // No mce_style for elements with these since they might get resized by the user
bgneal@45 1400 if (s.keep_values) {
bgneal@45 1401 if (v && !t._isRes(v))
bgneal@183 1402 e.setAttribute('_mce_style', v, 2);
bgneal@45 1403 else
bgneal@183 1404 e.removeAttribute('_mce_style', 2);
bgneal@45 1405 }
bgneal@45 1406
bgneal@45 1407 e.style.cssText = v;
bgneal@45 1408 break;
bgneal@45 1409
bgneal@45 1410 case "class":
bgneal@45 1411 e.className = v || ''; // Fix IE null bug
bgneal@45 1412 break;
bgneal@45 1413
bgneal@45 1414 case "src":
bgneal@45 1415 case "href":
bgneal@45 1416 if (s.keep_values) {
bgneal@45 1417 if (s.url_converter)
bgneal@45 1418 v = s.url_converter.call(s.url_converter_scope || t, v, n, e);
bgneal@45 1419
bgneal@183 1420 t.setAttrib(e, '_mce_' + n, v, 2);
bgneal@45 1421 }
bgneal@45 1422
bgneal@45 1423 break;
bgneal@45 1424
bgneal@45 1425 case "shape":
bgneal@183 1426 e.setAttribute('_mce_style', v);
bgneal@45 1427 break;
bgneal@45 1428 }
bgneal@45 1429
bgneal@45 1430 if (is(v) && v !== null && v.length !== 0)
bgneal@45 1431 e.setAttribute(n, '' + v, 2);
bgneal@45 1432 else
bgneal@45 1433 e.removeAttribute(n, 2);
bgneal@45 1434 });
bgneal@45 1435 },
bgneal@45 1436
bgneal@45 1437 setAttribs : function(e, o) {
bgneal@45 1438 var t = this;
bgneal@45 1439
bgneal@45 1440 return this.run(e, function(e) {
bgneal@45 1441 each(o, function(v, n) {
bgneal@45 1442 t.setAttrib(e, n, v);
bgneal@45 1443 });
bgneal@45 1444 });
bgneal@45 1445 },
bgneal@45 1446
bgneal@45 1447 getAttrib : function(e, n, dv) {
bgneal@45 1448 var v, t = this;
bgneal@45 1449
bgneal@45 1450 e = t.get(e);
bgneal@45 1451
bgneal@45 1452 if (!e || e.nodeType !== 1)
bgneal@45 1453 return false;
bgneal@45 1454
bgneal@45 1455 if (!is(dv))
bgneal@45 1456 dv = '';
bgneal@45 1457
bgneal@45 1458 // Try the mce variant for these
bgneal@45 1459 if (/^(src|href|style|coords|shape)$/.test(n)) {
bgneal@183 1460 v = e.getAttribute("_mce_" + n);
bgneal@45 1461
bgneal@45 1462 if (v)
bgneal@45 1463 return v;
bgneal@45 1464 }
bgneal@45 1465
bgneal@45 1466 if (isIE && t.props[n]) {
bgneal@45 1467 v = e[t.props[n]];
bgneal@45 1468 v = v && v.nodeValue ? v.nodeValue : v;
bgneal@45 1469 }
bgneal@45 1470
bgneal@45 1471 if (!v)
bgneal@45 1472 v = e.getAttribute(n, 2);
bgneal@45 1473
bgneal@183 1474 // Check boolean attribs
bgneal@183 1475 if (/^(checked|compact|declare|defer|disabled|ismap|multiple|nohref|noshade|nowrap|readonly|selected)$/.test(n)) {
bgneal@183 1476 if (e[t.props[n]] === true && v === '')
bgneal@183 1477 return n;
bgneal@183 1478
bgneal@183 1479 return v ? n : '';
bgneal@183 1480 }
bgneal@183 1481
bgneal@183 1482 // Inner input elements will override attributes on form elements
bgneal@183 1483 if (e.nodeName === "FORM" && e.getAttributeNode(n))
bgneal@183 1484 return e.getAttributeNode(n).nodeValue;
bgneal@183 1485
bgneal@45 1486 if (n === 'style') {
bgneal@45 1487 v = v || e.style.cssText;
bgneal@45 1488
bgneal@45 1489 if (v) {
bgneal@183 1490 v = t.serializeStyle(t.parseStyle(v), e.nodeName);
bgneal@45 1491
bgneal@45 1492 if (t.settings.keep_values && !t._isRes(v))
bgneal@183 1493 e.setAttribute('_mce_style', v);
bgneal@45 1494 }
bgneal@45 1495 }
bgneal@45 1496
bgneal@45 1497 // Remove Apple and WebKit stuff
bgneal@45 1498 if (isWebKit && n === "class" && v)
bgneal@45 1499 v = v.replace(/(apple|webkit)\-[a-z\-]+/gi, '');
bgneal@45 1500
bgneal@45 1501 // Handle IE issues
bgneal@45 1502 if (isIE) {
bgneal@45 1503 switch (n) {
bgneal@45 1504 case 'rowspan':
bgneal@45 1505 case 'colspan':
bgneal@45 1506 // IE returns 1 as default value
bgneal@45 1507 if (v === 1)
bgneal@45 1508 v = '';
bgneal@45 1509
bgneal@45 1510 break;
bgneal@45 1511
bgneal@45 1512 case 'size':
bgneal@45 1513 // IE returns +0 as default value for size
bgneal@45 1514 if (v === '+0' || v === 20 || v === 0)
bgneal@45 1515 v = '';
bgneal@45 1516
bgneal@45 1517 break;
bgneal@45 1518
bgneal@45 1519 case 'width':
bgneal@45 1520 case 'height':
bgneal@45 1521 case 'vspace':
bgneal@45 1522 case 'checked':
bgneal@45 1523 case 'disabled':
bgneal@45 1524 case 'readonly':
bgneal@45 1525 if (v === 0)
bgneal@45 1526 v = '';
bgneal@45 1527
bgneal@45 1528 break;
bgneal@45 1529
bgneal@45 1530 case 'hspace':
bgneal@45 1531 // IE returns -1 as default value
bgneal@45 1532 if (v === -1)
bgneal@45 1533 v = '';
bgneal@45 1534
bgneal@45 1535 break;
bgneal@45 1536
bgneal@45 1537 case 'maxlength':
bgneal@45 1538 case 'tabindex':
bgneal@45 1539 // IE returns default value
bgneal@45 1540 if (v === 32768 || v === 2147483647 || v === '32768')
bgneal@45 1541 v = '';
bgneal@45 1542
bgneal@45 1543 break;
bgneal@45 1544
bgneal@45 1545 case 'multiple':
bgneal@45 1546 case 'compact':
bgneal@45 1547 case 'noshade':
bgneal@45 1548 case 'nowrap':
bgneal@45 1549 if (v === 65535)
bgneal@45 1550 return n;
bgneal@45 1551
bgneal@45 1552 return dv;
bgneal@45 1553
bgneal@45 1554 case 'shape':
bgneal@45 1555 v = v.toLowerCase();
bgneal@45 1556 break;
bgneal@45 1557
bgneal@45 1558 default:
bgneal@45 1559 // IE has odd anonymous function for event attributes
bgneal@45 1560 if (n.indexOf('on') === 0 && v)
bgneal@183 1561 v = ('' + v).replace(/^function\s+\w+\(\)\s+\{\s+(.*)\s+\}$/, '$1');
bgneal@45 1562 }
bgneal@45 1563 }
bgneal@45 1564
bgneal@45 1565 return (v !== undefined && v !== null && v !== '') ? '' + v : dv;
bgneal@45 1566 },
bgneal@45 1567
bgneal@183 1568 getPos : function(n, ro) {
bgneal@45 1569 var t = this, x = 0, y = 0, e, d = t.doc, r;
bgneal@45 1570
bgneal@45 1571 n = t.get(n);
bgneal@183 1572 ro = ro || d.body;
bgneal@183 1573
bgneal@183 1574 if (n) {
bgneal@183 1575 // Use getBoundingClientRect on IE, Opera has it but it's not perfect
bgneal@183 1576 if (isIE && !t.stdMode) {
bgneal@183 1577 n = n.getBoundingClientRect();
bgneal@183 1578 e = t.boxModel ? d.documentElement : d.body;
bgneal@183 1579 x = t.getStyle(t.select('html')[0], 'borderWidth'); // Remove border
bgneal@183 1580 x = (x == 'medium' || t.boxModel && !t.isIE6) && 2 || x;
bgneal@183 1581 n.top += t.win.self != t.win.top ? 2 : 0; // IE adds some strange extra cord if used in a frameset
bgneal@183 1582
bgneal@183 1583 return {x : n.left + e.scrollLeft - x, y : n.top + e.scrollTop - x};
bgneal@183 1584 }
bgneal@183 1585
bgneal@183 1586 r = n;
bgneal@183 1587 while (r && r != ro && r.nodeType) {
bgneal@183 1588 x += r.offsetLeft || 0;
bgneal@183 1589 y += r.offsetTop || 0;
bgneal@183 1590 r = r.offsetParent;
bgneal@183 1591 }
bgneal@183 1592
bgneal@183 1593 r = n.parentNode;
bgneal@183 1594 while (r && r != ro && r.nodeType) {
bgneal@45 1595 x -= r.scrollLeft || 0;
bgneal@45 1596 y -= r.scrollTop || 0;
bgneal@183 1597 r = r.parentNode;
bgneal@183 1598 }
bgneal@45 1599 }
bgneal@45 1600
bgneal@45 1601 return {x : x, y : y};
bgneal@45 1602 },
bgneal@45 1603
bgneal@45 1604 parseStyle : function(st) {
bgneal@45 1605 var t = this, s = t.settings, o = {};
bgneal@45 1606
bgneal@45 1607 if (!st)
bgneal@45 1608 return o;
bgneal@45 1609
bgneal@45 1610 function compress(p, s, ot) {
bgneal@45 1611 var t, r, b, l;
bgneal@45 1612
bgneal@45 1613 // Get values and check it it needs compressing
bgneal@45 1614 t = o[p + '-top' + s];
bgneal@45 1615 if (!t)
bgneal@45 1616 return;
bgneal@45 1617
bgneal@45 1618 r = o[p + '-right' + s];
bgneal@45 1619 if (t != r)
bgneal@45 1620 return;
bgneal@45 1621
bgneal@45 1622 b = o[p + '-bottom' + s];
bgneal@45 1623 if (r != b)
bgneal@45 1624 return;
bgneal@45 1625
bgneal@45 1626 l = o[p + '-left' + s];
bgneal@45 1627 if (b != l)
bgneal@45 1628 return;
bgneal@45 1629
bgneal@45 1630 // Compress
bgneal@45 1631 o[ot] = l;
bgneal@45 1632 delete o[p + '-top' + s];
bgneal@45 1633 delete o[p + '-right' + s];
bgneal@45 1634 delete o[p + '-bottom' + s];
bgneal@45 1635 delete o[p + '-left' + s];
bgneal@45 1636 };
bgneal@45 1637
bgneal@45 1638 function compress2(ta, a, b, c) {
bgneal@45 1639 var t;
bgneal@45 1640
bgneal@45 1641 t = o[a];
bgneal@45 1642 if (!t)
bgneal@45 1643 return;
bgneal@45 1644
bgneal@45 1645 t = o[b];
bgneal@45 1646 if (!t)
bgneal@45 1647 return;
bgneal@45 1648
bgneal@45 1649 t = o[c];
bgneal@45 1650 if (!t)
bgneal@45 1651 return;
bgneal@45 1652
bgneal@45 1653 // Compress
bgneal@45 1654 o[ta] = o[a] + ' ' + o[b] + ' ' + o[c];
bgneal@45 1655 delete o[a];
bgneal@45 1656 delete o[b];
bgneal@45 1657 delete o[c];
bgneal@45 1658 };
bgneal@45 1659
bgneal@45 1660 st = st.replace(/&(#?[a-z0-9]+);/g, '&$1_MCE_SEMI_'); // Protect entities
bgneal@45 1661
bgneal@45 1662 each(st.split(';'), function(v) {
bgneal@45 1663 var sv, ur = [];
bgneal@45 1664
bgneal@45 1665 if (v) {
bgneal@45 1666 v = v.replace(/_MCE_SEMI_/g, ';'); // Restore entities
bgneal@45 1667 v = v.replace(/url\([^\)]+\)/g, function(v) {ur.push(v);return 'url(' + ur.length + ')';});
bgneal@45 1668 v = v.split(':');
bgneal@45 1669 sv = tinymce.trim(v[1]);
bgneal@45 1670 sv = sv.replace(/url\(([^\)]+)\)/g, function(a, b) {return ur[parseInt(b) - 1];});
bgneal@45 1671
bgneal@45 1672 sv = sv.replace(/rgb\([^\)]+\)/g, function(v) {
bgneal@45 1673 return t.toHex(v);
bgneal@45 1674 });
bgneal@45 1675
bgneal@45 1676 if (s.url_converter) {
bgneal@45 1677 sv = sv.replace(/url\([\'\"]?([^\)\'\"]+)[\'\"]?\)/g, function(x, c) {
bgneal@45 1678 return 'url(' + s.url_converter.call(s.url_converter_scope || t, t.decode(c), 'style', null) + ')';
bgneal@45 1679 });
bgneal@45 1680 }
bgneal@45 1681
bgneal@45 1682 o[tinymce.trim(v[0]).toLowerCase()] = sv;
bgneal@45 1683 }
bgneal@45 1684 });
bgneal@45 1685
bgneal@45 1686 compress("border", "", "border");
bgneal@45 1687 compress("border", "-width", "border-width");
bgneal@45 1688 compress("border", "-color", "border-color");
bgneal@45 1689 compress("border", "-style", "border-style");
bgneal@45 1690 compress("padding", "", "padding");
bgneal@45 1691 compress("margin", "", "margin");
bgneal@45 1692 compress2('border', 'border-width', 'border-style', 'border-color');
bgneal@45 1693
bgneal@45 1694 if (isIE) {
bgneal@45 1695 // Remove pointless border
bgneal@45 1696 if (o.border == 'medium none')
bgneal@45 1697 o.border = '';
bgneal@45 1698 }
bgneal@45 1699
bgneal@45 1700 return o;
bgneal@45 1701 },
bgneal@45 1702
bgneal@183 1703 serializeStyle : function(o, name) {
bgneal@183 1704 var t = this, s = '';
bgneal@183 1705
bgneal@183 1706 function add(v, k) {
bgneal@45 1707 if (k && v) {
bgneal@183 1708 // Remove browser specific styles like -moz- or -webkit-
bgneal@183 1709 if (k.indexOf('-') === 0)
bgneal@45 1710 return;
bgneal@45 1711
bgneal@45 1712 switch (k) {
bgneal@183 1713 case 'font-weight':
bgneal@183 1714 // Opera will output bold as 700
bgneal@183 1715 if (v == 700)
bgneal@183 1716 v = 'bold';
bgneal@183 1717
bgneal@183 1718 break;
bgneal@183 1719
bgneal@45 1720 case 'color':
bgneal@45 1721 case 'background-color':
bgneal@45 1722 v = v.toLowerCase();
bgneal@45 1723 break;
bgneal@45 1724 }
bgneal@45 1725
bgneal@45 1726 s += (s ? ' ' : '') + k + ': ' + v + ';';
bgneal@45 1727 }
bgneal@183 1728 };
bgneal@183 1729
bgneal@183 1730 // Validate style output
bgneal@183 1731 if (name && t._styles) {
bgneal@183 1732 each(t._styles['*'], function(name) {
bgneal@183 1733 add(o[name], name);
bgneal@183 1734 });
bgneal@183 1735
bgneal@183 1736 each(t._styles[name.toLowerCase()], function(name) {
bgneal@183 1737 add(o[name], name);
bgneal@183 1738 });
bgneal@183 1739 } else
bgneal@183 1740 each(o, add);
bgneal@45 1741
bgneal@45 1742 return s;
bgneal@45 1743 },
bgneal@45 1744
bgneal@45 1745 loadCSS : function(u) {
bgneal@183 1746 var t = this, d = t.doc, head;
bgneal@45 1747
bgneal@45 1748 if (!u)
bgneal@45 1749 u = '';
bgneal@45 1750
bgneal@183 1751 head = t.select('head')[0];
bgneal@183 1752
bgneal@45 1753 each(u.split(','), function(u) {
bgneal@183 1754 var link;
bgneal@183 1755
bgneal@45 1756 if (t.files[u])
bgneal@45 1757 return;
bgneal@45 1758
bgneal@45 1759 t.files[u] = true;
bgneal@183 1760 link = t.create('link', {rel : 'stylesheet', href : tinymce._addVer(u)});
bgneal@183 1761
bgneal@183 1762 // IE 8 has a bug where dynamically loading stylesheets would produce a 1 item remaining bug
bgneal@183 1763 // This fix seems to resolve that issue by realcing the document ones a stylesheet finishes loading
bgneal@183 1764 // It's ugly but it seems to work fine.
bgneal@183 1765 if (isIE && d.documentMode) {
bgneal@183 1766 link.onload = function() {
bgneal@183 1767 d.recalc();
bgneal@183 1768 link.onload = null;
bgneal@183 1769 };
bgneal@183 1770 }
bgneal@183 1771
bgneal@183 1772 head.appendChild(link);
bgneal@183 1773 });
bgneal@183 1774 },
bgneal@45 1775
bgneal@45 1776 addClass : function(e, c) {
bgneal@45 1777 return this.run(e, function(e) {
bgneal@45 1778 var o;
bgneal@45 1779
bgneal@45 1780 if (!c)
bgneal@45 1781 return 0;
bgneal@45 1782
bgneal@45 1783 if (this.hasClass(e, c))
bgneal@45 1784 return e.className;
bgneal@45 1785
bgneal@45 1786 o = this.removeClass(e, c);
bgneal@45 1787
bgneal@45 1788 return e.className = (o != '' ? (o + ' ') : '') + c;
bgneal@45 1789 });
bgneal@45 1790 },
bgneal@45 1791
bgneal@45 1792 removeClass : function(e, c) {
bgneal@45 1793 var t = this, re;
bgneal@45 1794
bgneal@45 1795 return t.run(e, function(e) {
bgneal@45 1796 var v;
bgneal@45 1797
bgneal@45 1798 if (t.hasClass(e, c)) {
bgneal@45 1799 if (!re)
bgneal@45 1800 re = new RegExp("(^|\\s+)" + c + "(\\s+|$)", "g");
bgneal@45 1801
bgneal@45 1802 v = e.className.replace(re, ' ');
bgneal@183 1803 v = tinymce.trim(v != ' ' ? v : '');
bgneal@183 1804
bgneal@183 1805 e.className = v;
bgneal@183 1806
bgneal@183 1807 // Empty class attr
bgneal@183 1808 if (!v) {
bgneal@183 1809 e.removeAttribute('class');
bgneal@183 1810 e.removeAttribute('className');
bgneal@183 1811 }
bgneal@183 1812
bgneal@183 1813 return v;
bgneal@45 1814 }
bgneal@45 1815
bgneal@45 1816 return e.className;
bgneal@45 1817 });
bgneal@45 1818 },
bgneal@45 1819
bgneal@45 1820 hasClass : function(n, c) {
bgneal@45 1821 n = this.get(n);
bgneal@45 1822
bgneal@45 1823 if (!n || !c)
bgneal@45 1824 return false;
bgneal@45 1825
bgneal@45 1826 return (' ' + n.className + ' ').indexOf(' ' + c + ' ') !== -1;
bgneal@45 1827 },
bgneal@45 1828
bgneal@45 1829 show : function(e) {
bgneal@45 1830 return this.setStyle(e, 'display', 'block');
bgneal@45 1831 },
bgneal@45 1832
bgneal@45 1833 hide : function(e) {
bgneal@45 1834 return this.setStyle(e, 'display', 'none');
bgneal@45 1835 },
bgneal@45 1836
bgneal@45 1837 isHidden : function(e) {
bgneal@45 1838 e = this.get(e);
bgneal@45 1839
bgneal@45 1840 return !e || e.style.display == 'none' || this.getStyle(e, 'display') == 'none';
bgneal@45 1841 },
bgneal@45 1842
bgneal@45 1843 uniqueId : function(p) {
bgneal@45 1844 return (!p ? 'mce_' : p) + (this.counter++);
bgneal@45 1845 },
bgneal@45 1846
bgneal@45 1847 setHTML : function(e, h) {
bgneal@45 1848 var t = this;
bgneal@45 1849
bgneal@45 1850 return this.run(e, function(e) {
bgneal@45 1851 var x, i, nl, n, p, x;
bgneal@45 1852
bgneal@45 1853 h = t.processHTML(h);
bgneal@45 1854
bgneal@45 1855 if (isIE) {
bgneal@45 1856 function set() {
bgneal@183 1857 // Remove all child nodes
bgneal@183 1858 while (e.firstChild)
bgneal@183 1859 e.firstChild.removeNode();
bgneal@183 1860
bgneal@45 1861 try {
bgneal@45 1862 // IE will remove comments from the beginning
bgneal@45 1863 // unless you padd the contents with something
bgneal@45 1864 e.innerHTML = '<br />' + h;
bgneal@45 1865 e.removeChild(e.firstChild);
bgneal@45 1866 } catch (ex) {
bgneal@45 1867 // IE sometimes produces an unknown runtime error on innerHTML if it's an block element within a block element for example a div inside a p
bgneal@45 1868 // This seems to fix this problem
bgneal@45 1869
bgneal@45 1870 // Create new div with HTML contents and a BR infront to keep comments
bgneal@45 1871 x = t.create('div');
bgneal@45 1872 x.innerHTML = '<br />' + h;
bgneal@45 1873
bgneal@45 1874 // Add all children from div to target
bgneal@45 1875 each (x.childNodes, function(n, i) {
bgneal@45 1876 // Skip br element
bgneal@45 1877 if (i)
bgneal@45 1878 e.appendChild(n);
bgneal@45 1879 });
bgneal@45 1880 }
bgneal@45 1881 };
bgneal@45 1882
bgneal@45 1883 // IE has a serious bug when it comes to paragraphs it can produce an invalid
bgneal@45 1884 // DOM tree if contents like this <p><ul><li>Item 1</li></ul></p> is inserted
bgneal@45 1885 // It seems to be that IE doesn't like a root block element placed inside another root block element
bgneal@45 1886 if (t.settings.fix_ie_paragraphs)
bgneal@183 1887 h = h.replace(/<p><\/p>|<p([^>]+)><\/p>|<p[^\/+]\/>/gi, '<p$1 _mce_keep="true">&nbsp;</p>');
bgneal@45 1888
bgneal@45 1889 set();
bgneal@45 1890
bgneal@45 1891 if (t.settings.fix_ie_paragraphs) {
bgneal@45 1892 // Check for odd paragraphs this is a sign of a broken DOM
bgneal@45 1893 nl = e.getElementsByTagName("p");
bgneal@45 1894 for (i = nl.length - 1, x = 0; i >= 0; i--) {
bgneal@45 1895 n = nl[i];
bgneal@45 1896
bgneal@45 1897 if (!n.hasChildNodes()) {
bgneal@183 1898 if (!n._mce_keep) {
bgneal@45 1899 x = 1; // Is broken
bgneal@45 1900 break;
bgneal@45 1901 }
bgneal@45 1902
bgneal@183 1903 n.removeAttribute('_mce_keep');
bgneal@45 1904 }
bgneal@45 1905 }
bgneal@45 1906 }
bgneal@45 1907
bgneal@45 1908 // Time to fix the madness IE left us
bgneal@45 1909 if (x) {
bgneal@45 1910 // So if we replace the p elements with divs and mark them and then replace them back to paragraphs
bgneal@45 1911 // after we use innerHTML we can fix the DOM tree
bgneal@183 1912 h = h.replace(/<p ([^>]+)>|<p>/ig, '<div $1 _mce_tmp="1">');
bgneal@217 1913 h = h.replace(/<\/p>/gi, '</div>');
bgneal@45 1914
bgneal@45 1915 // Set the new HTML with DIVs
bgneal@45 1916 set();
bgneal@45 1917
bgneal@183 1918 // Replace all DIV elements with the _mce_tmp attibute back to paragraphs
bgneal@45 1919 // This is needed since IE has a annoying bug see above for details
bgneal@45 1920 // This is a slow process but it has to be done. :(
bgneal@45 1921 if (t.settings.fix_ie_paragraphs) {
bgneal@45 1922 nl = e.getElementsByTagName("DIV");
bgneal@45 1923 for (i = nl.length - 1; i >= 0; i--) {
bgneal@45 1924 n = nl[i];
bgneal@45 1925
bgneal@45 1926 // Is it a temp div
bgneal@183 1927 if (n._mce_tmp) {
bgneal@45 1928 // Create new paragraph
bgneal@45 1929 p = t.doc.createElement('p');
bgneal@45 1930
bgneal@45 1931 // Copy all attributes
bgneal@45 1932 n.cloneNode(false).outerHTML.replace(/([a-z0-9\-_]+)=/gi, function(a, b) {
bgneal@45 1933 var v;
bgneal@45 1934
bgneal@183 1935 if (b !== '_mce_tmp') {
bgneal@45 1936 v = n.getAttribute(b);
bgneal@45 1937
bgneal@45 1938 if (!v && b === 'class')
bgneal@45 1939 v = n.className;
bgneal@45 1940
bgneal@45 1941 p.setAttribute(b, v);
bgneal@45 1942 }
bgneal@45 1943 });
bgneal@45 1944
bgneal@45 1945 // Append all children to new paragraph
bgneal@45 1946 for (x = 0; x<n.childNodes.length; x++)
bgneal@45 1947 p.appendChild(n.childNodes[x].cloneNode(true));
bgneal@45 1948
bgneal@45 1949 // Replace div with new paragraph
bgneal@45 1950 n.swapNode(p);
bgneal@45 1951 }
bgneal@45 1952 }
bgneal@45 1953 }
bgneal@45 1954 }
bgneal@45 1955 } else
bgneal@45 1956 e.innerHTML = h;
bgneal@45 1957
bgneal@45 1958 return h;
bgneal@45 1959 });
bgneal@45 1960 },
bgneal@45 1961
bgneal@45 1962 processHTML : function(h) {
bgneal@183 1963 var t = this, s = t.settings, codeBlocks = [];
bgneal@45 1964
bgneal@45 1965 if (!s.process_html)
bgneal@45 1966 return h;
bgneal@45 1967
bgneal@183 1968 if (isIE) {
bgneal@45 1969 h = h.replace(/&apos;/g, '&#39;'); // IE can't handle apos
bgneal@45 1970 h = h.replace(/\s+(disabled|checked|readonly|selected)\s*=\s*[\"\']?(false|0)[\"\']?/gi, ''); // IE doesn't handle default values correct
bgneal@45 1971 }
bgneal@45 1972
bgneal@45 1973 // Fix some issues
bgneal@45 1974 h = h.replace(/<a( )([^>]+)\/>|<a\/>/gi, '<a$1$2></a>'); // Force open
bgneal@45 1975
bgneal@183 1976 // Store away src and href in _mce_src and mce_href since browsers mess them up
bgneal@45 1977 if (s.keep_values) {
bgneal@45 1978 // Wrap scripts and styles in comments for serialization purposes
bgneal@183 1979 if (/<script|noscript|style/i.test(h)) {
bgneal@45 1980 function trim(s) {
bgneal@45 1981 // Remove prefix and suffix code for element
bgneal@45 1982 s = s.replace(/(<!--\[CDATA\[|\]\]-->)/g, '\n');
bgneal@45 1983 s = s.replace(/^[\r\n]*|[\r\n]*$/g, '');
bgneal@45 1984 s = s.replace(/^\s*(\/\/\s*<!--|\/\/\s*<!\[CDATA\[|<!--|<!\[CDATA\[)[\r\n]*/g, '');
bgneal@45 1985 s = s.replace(/\s*(\/\/\s*\]\]>|\/\/\s*-->|\]\]>|-->|\]\]-->)\s*$/g, '');
bgneal@45 1986
bgneal@45 1987 return s;
bgneal@45 1988 };
bgneal@45 1989
bgneal@183 1990 // Wrap the script contents in CDATA and keep them from executing
bgneal@183 1991 h = h.replace(/<script([^>]+|)>([\s\S]*?)<\/script>/gi, function(v, attribs, text) {
bgneal@45 1992 // Force type attribute
bgneal@183 1993 if (!attribs)
bgneal@183 1994 attribs = ' type="text/javascript"';
bgneal@183 1995
bgneal@183 1996 // Convert the src attribute of the scripts
bgneal@183 1997 attribs = attribs.replace(/src=\"([^\"]+)\"?/i, function(a, url) {
bgneal@183 1998 if (s.url_converter)
bgneal@183 1999 url = t.encode(s.url_converter.call(s.url_converter_scope || t, t.decode(url), 'src', 'script'));
bgneal@183 2000
bgneal@183 2001 return '_mce_src="' + url + '"';
bgneal@183 2002 });
bgneal@183 2003
bgneal@183 2004 // Wrap text contents
bgneal@183 2005 if (tinymce.trim(text)) {
bgneal@183 2006 codeBlocks.push(trim(text));
bgneal@183 2007 text = '<!--\nMCE_SCRIPT:' + (codeBlocks.length - 1) + '\n// -->';
bgneal@183 2008 }
bgneal@183 2009
bgneal@183 2010 return '<mce:script' + attribs + '>' + text + '</mce:script>';
bgneal@45 2011 });
bgneal@45 2012
bgneal@183 2013 // Wrap style elements
bgneal@183 2014 h = h.replace(/<style([^>]+|)>([\s\S]*?)<\/style>/gi, function(v, attribs, text) {
bgneal@183 2015 // Wrap text contents
bgneal@183 2016 if (text) {
bgneal@183 2017 codeBlocks.push(trim(text));
bgneal@183 2018 text = '<!--\nMCE_SCRIPT:' + (codeBlocks.length - 1) + '\n-->';
bgneal@183 2019 }
bgneal@183 2020
bgneal@183 2021 return '<mce:style' + attribs + '>' + text + '</mce:style><style ' + attribs + ' _mce_bogus="1">' + text + '</style>';
bgneal@45 2022 });
bgneal@183 2023
bgneal@183 2024 // Wrap noscript elements
bgneal@183 2025 h = h.replace(/<noscript([^>]+|)>([\s\S]*?)<\/noscript>/g, function(v, attribs, text) {
bgneal@183 2026 return '<mce:noscript' + attribs + '><!--' + t.encode(text).replace(/--/g, '&#45;&#45;') + '--></mce:noscript>';
bgneal@183 2027 });
bgneal@45 2028 }
bgneal@45 2029
bgneal@45 2030 h = h.replace(/<!\[CDATA\[([\s\S]+)\]\]>/g, '<!--[CDATA[$1]]-->');
bgneal@45 2031
bgneal@183 2032 // This function processes the attributes in the HTML string to force boolean
bgneal@183 2033 // attributes to the attr="attr" format and convert style, src and href to _mce_ versions
bgneal@183 2034 function processTags(html) {
bgneal@183 2035 return html.replace(tagRegExp, function(match, elm_name, attrs, end) {
bgneal@183 2036 return '<' + elm_name + attrs.replace(attrRegExp, function(match, name, value, val2, val3) {
bgneal@183 2037 var mceValue;
bgneal@183 2038
bgneal@183 2039 name = name.toLowerCase();
bgneal@183 2040 value = value || val2 || val3 || "";
bgneal@183 2041
bgneal@183 2042 // Treat boolean attributes
bgneal@183 2043 if (boolAttrs[name]) {
bgneal@183 2044 // false or 0 is treated as a missing attribute
bgneal@183 2045 if (value === 'false' || value === '0')
bgneal@183 2046 return;
bgneal@183 2047
bgneal@183 2048 return name + '="' + name + '"';
bgneal@45 2049 }
bgneal@45 2050
bgneal@183 2051 // Is attribute one that needs special treatment
bgneal@183 2052 if (mceAttribs[name] && attrs.indexOf('_mce_' + name) == -1) {
bgneal@183 2053 mceValue = t.decode(value);
bgneal@183 2054
bgneal@183 2055 // Convert URLs to relative/absolute ones
bgneal@183 2056 if (s.url_converter && (name == "src" || name == "href"))
bgneal@183 2057 mceValue = s.url_converter.call(s.url_converter_scope || t, mceValue, name, elm_name);
bgneal@183 2058
bgneal@183 2059 // Process styles lowercases them and compresses them
bgneal@183 2060 if (name == 'style')
bgneal@183 2061 mceValue = t.serializeStyle(t.parseStyle(mceValue), name);
bgneal@183 2062
bgneal@183 2063 return name + '="' + value + '"' + ' _mce_' + name + '="' + t.encode(mceValue) + '"';
bgneal@45 2064 }
bgneal@183 2065
bgneal@183 2066 return match;
bgneal@183 2067 }) + end + '>';
bgneal@183 2068 });
bgneal@183 2069 };
bgneal@183 2070
bgneal@183 2071 h = processTags(h);
bgneal@183 2072
bgneal@183 2073 // Restore script blocks
bgneal@183 2074 h = h.replace(/MCE_SCRIPT:([0-9]+)/g, function(val, idx) {
bgneal@183 2075 return codeBlocks[idx];
bgneal@45 2076 });
bgneal@45 2077 }
bgneal@45 2078
bgneal@45 2079 return h;
bgneal@45 2080 },
bgneal@45 2081
bgneal@45 2082 getOuterHTML : function(e) {
bgneal@45 2083 var d;
bgneal@45 2084
bgneal@45 2085 e = this.get(e);
bgneal@45 2086
bgneal@45 2087 if (!e)
bgneal@45 2088 return null;
bgneal@45 2089
bgneal@45 2090 if (e.outerHTML !== undefined)
bgneal@45 2091 return e.outerHTML;
bgneal@45 2092
bgneal@45 2093 d = (e.ownerDocument || this.doc).createElement("body");
bgneal@45 2094 d.appendChild(e.cloneNode(true));
bgneal@45 2095
bgneal@45 2096 return d.innerHTML;
bgneal@45 2097 },
bgneal@45 2098
bgneal@45 2099 setOuterHTML : function(e, h, d) {
bgneal@45 2100 var t = this;
bgneal@45 2101
bgneal@183 2102 function setHTML(e, h, d) {
bgneal@183 2103 var n, tp;
bgneal@183 2104
bgneal@183 2105 tp = d.createElement("body");
bgneal@183 2106 tp.innerHTML = h;
bgneal@183 2107
bgneal@183 2108 n = tp.lastChild;
bgneal@183 2109 while (n) {
bgneal@183 2110 t.insertAfter(n.cloneNode(true), e);
bgneal@183 2111 n = n.previousSibling;
bgneal@183 2112 }
bgneal@183 2113
bgneal@183 2114 t.remove(e);
bgneal@183 2115 };
bgneal@183 2116
bgneal@45 2117 return this.run(e, function(e) {
bgneal@45 2118 e = t.get(e);
bgneal@183 2119
bgneal@183 2120 // Only set HTML on elements
bgneal@183 2121 if (e.nodeType == 1) {
bgneal@183 2122 d = d || e.ownerDocument || t.doc;
bgneal@183 2123
bgneal@183 2124 if (isIE) {
bgneal@183 2125 try {
bgneal@183 2126 // Try outerHTML for IE it sometimes produces an unknown runtime error
bgneal@183 2127 if (isIE && e.nodeType == 1)
bgneal@183 2128 e.outerHTML = h;
bgneal@183 2129 else
bgneal@183 2130 setHTML(e, h, d);
bgneal@183 2131 } catch (ex) {
bgneal@183 2132 // Fix for unknown runtime error
bgneal@183 2133 setHTML(e, h, d);
bgneal@183 2134 }
bgneal@183 2135 } else
bgneal@183 2136 setHTML(e, h, d);
bgneal@45 2137 }
bgneal@45 2138 });
bgneal@45 2139 },
bgneal@45 2140
bgneal@45 2141 decode : function(s) {
bgneal@45 2142 var e, n, v;
bgneal@45 2143
bgneal@45 2144 // Look for entities to decode
bgneal@183 2145 if (/&[\w#]+;/.test(s)) {
bgneal@45 2146 // Decode the entities using a div element not super efficient but less code
bgneal@45 2147 e = this.doc.createElement("div");
bgneal@45 2148 e.innerHTML = s;
bgneal@45 2149 n = e.firstChild;
bgneal@45 2150 v = '';
bgneal@45 2151
bgneal@45 2152 if (n) {
bgneal@45 2153 do {
bgneal@45 2154 v += n.nodeValue;
bgneal@183 2155 } while (n = n.nextSibling);
bgneal@45 2156 }
bgneal@45 2157
bgneal@45 2158 return v || s;
bgneal@45 2159 }
bgneal@45 2160
bgneal@45 2161 return s;
bgneal@45 2162 },
bgneal@45 2163
bgneal@183 2164 encode : function(str) {
bgneal@183 2165 return ('' + str).replace(encodeCharsRe, function(chr) {
bgneal@183 2166 return encodedChars[chr];
bgneal@183 2167 });
bgneal@183 2168 },
bgneal@183 2169
bgneal@183 2170 insertAfter : function(node, reference_node) {
bgneal@183 2171 reference_node = this.get(reference_node);
bgneal@183 2172
bgneal@183 2173 return this.run(node, function(node) {
bgneal@183 2174 var parent, nextSibling;
bgneal@183 2175
bgneal@183 2176 parent = reference_node.parentNode;
bgneal@183 2177 nextSibling = reference_node.nextSibling;
bgneal@183 2178
bgneal@183 2179 if (nextSibling)
bgneal@183 2180 parent.insertBefore(node, nextSibling);
bgneal@45 2181 else
bgneal@183 2182 parent.appendChild(node);
bgneal@183 2183
bgneal@183 2184 return node;
bgneal@183 2185 });
bgneal@183 2186 },
bgneal@45 2187
bgneal@45 2188 isBlock : function(n) {
bgneal@45 2189 if (n.nodeType && n.nodeType !== 1)
bgneal@45 2190 return false;
bgneal@45 2191
bgneal@45 2192 n = n.nodeName || n;
bgneal@45 2193
bgneal@183 2194 return blockRe.test(n);
bgneal@183 2195 },
bgneal@45 2196
bgneal@45 2197 replace : function(n, o, k) {
bgneal@45 2198 var t = this;
bgneal@45 2199
bgneal@45 2200 if (is(o, 'array'))
bgneal@45 2201 n = n.cloneNode(true);
bgneal@45 2202
bgneal@45 2203 return t.run(o, function(o) {
bgneal@45 2204 if (k) {
bgneal@183 2205 each(tinymce.grep(o.childNodes), function(c) {
bgneal@183 2206 n.appendChild(c);
bgneal@45 2207 });
bgneal@45 2208 }
bgneal@45 2209
bgneal@45 2210 return o.parentNode.replaceChild(n, o);
bgneal@45 2211 });
bgneal@45 2212 },
bgneal@45 2213
bgneal@183 2214 rename : function(elm, name) {
bgneal@183 2215 var t = this, newElm;
bgneal@183 2216
bgneal@183 2217 if (elm.nodeName != name.toUpperCase()) {
bgneal@183 2218 // Rename block element
bgneal@183 2219 newElm = t.create(name);
bgneal@183 2220
bgneal@183 2221 // Copy attribs to new block
bgneal@183 2222 each(t.getAttribs(elm), function(attr_node) {
bgneal@183 2223 t.setAttrib(newElm, attr_node.nodeName, t.getAttrib(elm, attr_node.nodeName));
bgneal@183 2224 });
bgneal@183 2225
bgneal@183 2226 // Replace block
bgneal@183 2227 t.replace(newElm, elm, 1);
bgneal@183 2228 }
bgneal@183 2229
bgneal@183 2230 return newElm || elm;
bgneal@183 2231 },
bgneal@45 2232
bgneal@45 2233 findCommonAncestor : function(a, b) {
bgneal@45 2234 var ps = a, pe;
bgneal@45 2235
bgneal@45 2236 while (ps) {
bgneal@45 2237 pe = b;
bgneal@45 2238
bgneal@45 2239 while (pe && ps != pe)
bgneal@45 2240 pe = pe.parentNode;
bgneal@45 2241
bgneal@45 2242 if (ps == pe)
bgneal@45 2243 break;
bgneal@45 2244
bgneal@45 2245 ps = ps.parentNode;
bgneal@45 2246 }
bgneal@45 2247
bgneal@45 2248 if (!ps && a.ownerDocument)
bgneal@45 2249 return a.ownerDocument.documentElement;
bgneal@45 2250
bgneal@45 2251 return ps;
bgneal@45 2252 },
bgneal@45 2253
bgneal@45 2254 toHex : function(s) {
bgneal@45 2255 var c = /^\s*rgb\s*?\(\s*?([0-9]+)\s*?,\s*?([0-9]+)\s*?,\s*?([0-9]+)\s*?\)\s*$/i.exec(s);
bgneal@45 2256
bgneal@45 2257 function hex(s) {
bgneal@45 2258 s = parseInt(s).toString(16);
bgneal@45 2259
bgneal@45 2260 return s.length > 1 ? s : '0' + s; // 0 -> 00
bgneal@45 2261 };
bgneal@45 2262
bgneal@45 2263 if (c) {
bgneal@45 2264 s = '#' + hex(c[1]) + hex(c[2]) + hex(c[3]);
bgneal@45 2265
bgneal@45 2266 return s;
bgneal@45 2267 }
bgneal@45 2268
bgneal@45 2269 return s;
bgneal@45 2270 },
bgneal@45 2271
bgneal@45 2272 getClasses : function() {
bgneal@45 2273 var t = this, cl = [], i, lo = {}, f = t.settings.class_filter, ov;
bgneal@45 2274
bgneal@45 2275 if (t.classes)
bgneal@45 2276 return t.classes;
bgneal@45 2277
bgneal@45 2278 function addClasses(s) {
bgneal@45 2279 // IE style imports
bgneal@45 2280 each(s.imports, function(r) {
bgneal@45 2281 addClasses(r);
bgneal@45 2282 });
bgneal@45 2283
bgneal@45 2284 each(s.cssRules || s.rules, function(r) {
bgneal@45 2285 // Real type or fake it on IE
bgneal@45 2286 switch (r.type || 1) {
bgneal@45 2287 // Rule
bgneal@45 2288 case 1:
bgneal@45 2289 if (r.selectorText) {
bgneal@45 2290 each(r.selectorText.split(','), function(v) {
bgneal@45 2291 v = v.replace(/^\s*|\s*$|^\s\./g, "");
bgneal@45 2292
bgneal@45 2293 // Is internal or it doesn't contain a class
bgneal@45 2294 if (/\.mce/.test(v) || !/\.[\w\-]+$/.test(v))
bgneal@45 2295 return;
bgneal@45 2296
bgneal@45 2297 // Remove everything but class name
bgneal@45 2298 ov = v;
bgneal@45 2299 v = v.replace(/.*\.([a-z0-9_\-]+).*/i, '$1');
bgneal@45 2300
bgneal@45 2301 // Filter classes
bgneal@45 2302 if (f && !(v = f(v, ov)))
bgneal@45 2303 return;
bgneal@45 2304
bgneal@45 2305 if (!lo[v]) {
bgneal@45 2306 cl.push({'class' : v});
bgneal@45 2307 lo[v] = 1;
bgneal@45 2308 }
bgneal@45 2309 });
bgneal@45 2310 }
bgneal@45 2311 break;
bgneal@45 2312
bgneal@45 2313 // Import
bgneal@45 2314 case 3:
bgneal@45 2315 addClasses(r.styleSheet);
bgneal@45 2316 break;
bgneal@45 2317 }
bgneal@45 2318 });
bgneal@45 2319 };
bgneal@45 2320
bgneal@45 2321 try {
bgneal@45 2322 each(t.doc.styleSheets, addClasses);
bgneal@45 2323 } catch (ex) {
bgneal@45 2324 // Ignore
bgneal@45 2325 }
bgneal@45 2326
bgneal@45 2327 if (cl.length > 0)
bgneal@45 2328 t.classes = cl;
bgneal@45 2329
bgneal@45 2330 return cl;
bgneal@45 2331 },
bgneal@45 2332
bgneal@45 2333 run : function(e, f, s) {
bgneal@45 2334 var t = this, o;
bgneal@45 2335
bgneal@45 2336 if (t.doc && typeof(e) === 'string')
bgneal@45 2337 e = t.get(e);
bgneal@45 2338
bgneal@45 2339 if (!e)
bgneal@45 2340 return false;
bgneal@45 2341
bgneal@45 2342 s = s || this;
bgneal@45 2343 if (!e.nodeType && (e.length || e.length === 0)) {
bgneal@45 2344 o = [];
bgneal@45 2345
bgneal@45 2346 each(e, function(e, i) {
bgneal@45 2347 if (e) {
bgneal@45 2348 if (typeof(e) == 'string')
bgneal@45 2349 e = t.doc.getElementById(e);
bgneal@45 2350
bgneal@45 2351 o.push(f.call(s, e, i));
bgneal@45 2352 }
bgneal@45 2353 });
bgneal@45 2354
bgneal@45 2355 return o;
bgneal@45 2356 }
bgneal@45 2357
bgneal@45 2358 return f.call(s, e);
bgneal@45 2359 },
bgneal@45 2360
bgneal@45 2361 getAttribs : function(n) {
bgneal@45 2362 var o;
bgneal@45 2363
bgneal@45 2364 n = this.get(n);
bgneal@45 2365
bgneal@45 2366 if (!n)
bgneal@45 2367 return [];
bgneal@45 2368
bgneal@45 2369 if (isIE) {
bgneal@45 2370 o = [];
bgneal@45 2371
bgneal@45 2372 // Object will throw exception in IE
bgneal@45 2373 if (n.nodeName == 'OBJECT')
bgneal@45 2374 return n.attributes;
bgneal@45 2375
bgneal@183 2376 // IE doesn't keep the selected attribute if you clone option elements
bgneal@183 2377 if (n.nodeName === 'OPTION' && this.getAttrib(n, 'selected'))
bgneal@183 2378 o.push({specified : 1, nodeName : 'selected'});
bgneal@183 2379
bgneal@45 2380 // It's crazy that this is faster in IE but it's because it returns all attributes all the time
bgneal@183 2381 n.cloneNode(false).outerHTML.replace(/<\/?[\w:\-]+ ?|=[\"][^\"]+\"|=\'[^\']+\'|=[\w\-]+|>/gi, '').replace(/[\w:\-]+/gi, function(a) {
bgneal@183 2382 o.push({specified : 1, nodeName : a});
bgneal@45 2383 });
bgneal@45 2384
bgneal@45 2385 return o;
bgneal@45 2386 }
bgneal@45 2387
bgneal@45 2388 return n.attributes;
bgneal@45 2389 },
bgneal@45 2390
bgneal@45 2391 destroy : function(s) {
bgneal@45 2392 var t = this;
bgneal@45 2393
bgneal@183 2394 if (t.events)
bgneal@183 2395 t.events.destroy();
bgneal@183 2396
bgneal@183 2397 t.win = t.doc = t.root = t.events = null;
bgneal@45 2398
bgneal@45 2399 // Manual destroy then remove unload handler
bgneal@45 2400 if (!s)
bgneal@45 2401 tinymce.removeUnload(t.destroy);
bgneal@45 2402 },
bgneal@45 2403
bgneal@45 2404 createRng : function() {
bgneal@45 2405 var d = this.doc;
bgneal@45 2406
bgneal@45 2407 return d.createRange ? d.createRange() : new tinymce.dom.Range(this);
bgneal@45 2408 },
bgneal@45 2409
bgneal@183 2410 nodeIndex : function(node, normalized) {
bgneal@183 2411 var idx = 0, lastNodeType, lastNode, nodeType;
bgneal@183 2412
bgneal@183 2413 if (node) {
bgneal@183 2414 for (lastNodeType = node.nodeType, node = node.previousSibling, lastNode = node; node; node = node.previousSibling) {
bgneal@183 2415 nodeType = node.nodeType;
bgneal@183 2416
bgneal@217 2417 // Normalize text nodes
bgneal@217 2418 if (normalized && nodeType == 3) {
bgneal@217 2419 if (nodeType == lastNodeType || !node.nodeValue.length)
bgneal@217 2420 continue;
bgneal@217 2421 }
bgneal@217 2422
bgneal@217 2423 idx++;
bgneal@183 2424 lastNodeType = nodeType;
bgneal@183 2425 }
bgneal@183 2426 }
bgneal@183 2427
bgneal@183 2428 return idx;
bgneal@183 2429 },
bgneal@183 2430
bgneal@45 2431 split : function(pe, e, re) {
bgneal@45 2432 var t = this, r = t.createRng(), bef, aft, pa;
bgneal@45 2433
bgneal@183 2434 // W3C valid browsers tend to leave empty nodes to the left/right side of the contents, this makes sense
bgneal@183 2435 // but we don't want that in our code since it serves no purpose for the end user
bgneal@45 2436 // For example if this is chopped:
bgneal@45 2437 // <p>text 1<span><b>CHOP</b></span>text 2</p>
bgneal@45 2438 // would produce:
bgneal@45 2439 // <p>text 1<span></span></p><b>CHOP</b><p><span></span>text 2</p>
bgneal@45 2440 // this function will then trim of empty edges and produce:
bgneal@45 2441 // <p>text 1</p><b>CHOP</b><p>text 2</p>
bgneal@183 2442 function trim(node) {
bgneal@183 2443 var i, children = node.childNodes;
bgneal@183 2444
bgneal@183 2445 if (node.nodeType == 1 && node.getAttribute('_mce_type') == 'bookmark')
bgneal@183 2446 return;
bgneal@183 2447
bgneal@183 2448 for (i = children.length - 1; i >= 0; i--)
bgneal@183 2449 trim(children[i]);
bgneal@183 2450
bgneal@183 2451 if (node.nodeType != 9) {
bgneal@183 2452 // Keep non whitespace text nodes
bgneal@183 2453 if (node.nodeType == 3 && node.nodeValue.length > 0)
bgneal@183 2454 return;
bgneal@183 2455
bgneal@183 2456 if (node.nodeType == 1) {
bgneal@183 2457 // If the only child is a bookmark then move it up
bgneal@183 2458 children = node.childNodes;
bgneal@183 2459 if (children.length == 1 && children[0] && children[0].nodeType == 1 && children[0].getAttribute('_mce_type') == 'bookmark')
bgneal@183 2460 node.parentNode.insertBefore(children[0], node);
bgneal@183 2461
bgneal@183 2462 // Keep non empty elements or img, hr etc
bgneal@183 2463 if (children.length || /^(br|hr|input|img)$/i.test(node.nodeName))
bgneal@183 2464 return;
bgneal@183 2465 }
bgneal@183 2466
bgneal@183 2467 t.remove(node);
bgneal@183 2468 }
bgneal@183 2469
bgneal@183 2470 return node;
bgneal@45 2471 };
bgneal@45 2472
bgneal@45 2473 if (pe && e) {
bgneal@45 2474 // Get before chunk
bgneal@183 2475 r.setStart(pe.parentNode, t.nodeIndex(pe));
bgneal@183 2476 r.setEnd(e.parentNode, t.nodeIndex(e));
bgneal@45 2477 bef = r.extractContents();
bgneal@45 2478
bgneal@45 2479 // Get after chunk
bgneal@45 2480 r = t.createRng();
bgneal@183 2481 r.setStart(e.parentNode, t.nodeIndex(e) + 1);
bgneal@183 2482 r.setEnd(pe.parentNode, t.nodeIndex(pe) + 1);
bgneal@45 2483 aft = r.extractContents();
bgneal@45 2484
bgneal@183 2485 // Insert before chunk
bgneal@45 2486 pa = pe.parentNode;
bgneal@183 2487 pa.insertBefore(trim(bef), pe);
bgneal@183 2488
bgneal@183 2489 // Insert middle chunk
bgneal@45 2490 if (re)
bgneal@45 2491 pa.replaceChild(re, e);
bgneal@45 2492 else
bgneal@45 2493 pa.insertBefore(e, pe);
bgneal@45 2494
bgneal@183 2495 // Insert after chunk
bgneal@183 2496 pa.insertBefore(trim(aft), pe);
bgneal@45 2497 t.remove(pe);
bgneal@45 2498
bgneal@45 2499 return re || e;
bgneal@45 2500 }
bgneal@45 2501 },
bgneal@45 2502
bgneal@183 2503 bind : function(target, name, func, scope) {
bgneal@183 2504 var t = this;
bgneal@183 2505
bgneal@183 2506 if (!t.events)
bgneal@183 2507 t.events = new tinymce.dom.EventUtils();
bgneal@183 2508
bgneal@183 2509 return t.events.add(target, name, func, scope || this);
bgneal@183 2510 },
bgneal@183 2511
bgneal@183 2512 unbind : function(target, name, func) {
bgneal@183 2513 var t = this;
bgneal@183 2514
bgneal@183 2515 if (!t.events)
bgneal@183 2516 t.events = new tinymce.dom.EventUtils();
bgneal@183 2517
bgneal@183 2518 return t.events.remove(target, name, func);
bgneal@183 2519 },
bgneal@183 2520
bgneal@183 2521
bgneal@183 2522 _findSib : function(node, selector, name) {
bgneal@183 2523 var t = this, f = selector;
bgneal@183 2524
bgneal@183 2525 if (node) {
bgneal@183 2526 // If expression make a function of it using is
bgneal@183 2527 if (is(f, 'string')) {
bgneal@183 2528 f = function(node) {
bgneal@183 2529 return t.is(node, selector);
bgneal@183 2530 };
bgneal@183 2531 }
bgneal@183 2532
bgneal@183 2533 // Loop all siblings
bgneal@183 2534 for (node = node[name]; node; node = node[name]) {
bgneal@183 2535 if (f(node))
bgneal@183 2536 return node;
bgneal@183 2537 }
bgneal@183 2538 }
bgneal@183 2539
bgneal@183 2540 return null;
bgneal@183 2541 },
bgneal@183 2542
bgneal@45 2543 _isRes : function(c) {
bgneal@45 2544 // Is live resizble element
bgneal@45 2545 return /^(top|left|bottom|right|width|height)/i.test(c) || /;\s*(top|left|bottom|right|width|height)/i.test(c);
bgneal@45 2546 }
bgneal@45 2547
bgneal@45 2548 /*
bgneal@45 2549 walk : function(n, f, s) {
bgneal@45 2550 var d = this.doc, w;
bgneal@45 2551
bgneal@45 2552 if (d.createTreeWalker) {
bgneal@45 2553 w = d.createTreeWalker(n, NodeFilter.SHOW_TEXT, null, false);
bgneal@45 2554
bgneal@45 2555 while ((n = w.nextNode()) != null)
bgneal@45 2556 f.call(s || this, n);
bgneal@45 2557 } else
bgneal@45 2558 tinymce.walk(n, f, 'childNodes', s);
bgneal@45 2559 }
bgneal@45 2560 */
bgneal@45 2561
bgneal@45 2562 /*
bgneal@45 2563 toRGB : function(s) {
bgneal@45 2564 var c = /^\s*?#([0-9A-F]{2})([0-9A-F]{1,2})([0-9A-F]{2})?\s*?$/.exec(s);
bgneal@45 2565
bgneal@45 2566 if (c) {
bgneal@45 2567 // #FFF -> #FFFFFF
bgneal@45 2568 if (!is(c[3]))
bgneal@45 2569 c[3] = c[2] = c[1];
bgneal@45 2570
bgneal@45 2571 return "rgb(" + parseInt(c[1], 16) + "," + parseInt(c[2], 16) + "," + parseInt(c[3], 16) + ")";
bgneal@45 2572 }
bgneal@45 2573
bgneal@45 2574 return s;
bgneal@45 2575 }
bgneal@45 2576 */
bgneal@183 2577 });
bgneal@183 2578
bgneal@45 2579 tinymce.DOM = new tinymce.dom.DOMUtils(document, {process_html : 0});
bgneal@45 2580 })(tinymce);
bgneal@183 2581
bgneal@45 2582 (function(ns) {
bgneal@45 2583 // Range constructor
bgneal@45 2584 function Range(dom) {
bgneal@183 2585 var t = this,
bgneal@183 2586 doc = dom.doc,
bgneal@183 2587 EXTRACT = 0,
bgneal@183 2588 CLONE = 1,
bgneal@183 2589 DELETE = 2,
bgneal@183 2590 TRUE = true,
bgneal@183 2591 FALSE = false,
bgneal@183 2592 START_OFFSET = 'startOffset',
bgneal@183 2593 START_CONTAINER = 'startContainer',
bgneal@183 2594 END_CONTAINER = 'endContainer',
bgneal@183 2595 END_OFFSET = 'endOffset',
bgneal@183 2596 extend = tinymce.extend,
bgneal@183 2597 nodeIndex = dom.nodeIndex;
bgneal@183 2598
bgneal@183 2599 extend(t, {
bgneal@45 2600 // Inital states
bgneal@183 2601 startContainer : doc,
bgneal@45 2602 startOffset : 0,
bgneal@183 2603 endContainer : doc,
bgneal@45 2604 endOffset : 0,
bgneal@183 2605 collapsed : TRUE,
bgneal@183 2606 commonAncestorContainer : doc,
bgneal@45 2607
bgneal@45 2608 // Range constants
bgneal@45 2609 START_TO_START : 0,
bgneal@45 2610 START_TO_END : 1,
bgneal@45 2611 END_TO_END : 2,
bgneal@183 2612 END_TO_START : 3,
bgneal@183 2613
bgneal@183 2614 // Public methods
bgneal@183 2615 setStart : setStart,
bgneal@183 2616 setEnd : setEnd,
bgneal@183 2617 setStartBefore : setStartBefore,
bgneal@183 2618 setStartAfter : setStartAfter,
bgneal@183 2619 setEndBefore : setEndBefore,
bgneal@183 2620 setEndAfter : setEndAfter,
bgneal@183 2621 collapse : collapse,
bgneal@183 2622 selectNode : selectNode,
bgneal@183 2623 selectNodeContents : selectNodeContents,
bgneal@183 2624 compareBoundaryPoints : compareBoundaryPoints,
bgneal@183 2625 deleteContents : deleteContents,
bgneal@183 2626 extractContents : extractContents,
bgneal@183 2627 cloneContents : cloneContents,
bgneal@183 2628 insertNode : insertNode,
bgneal@183 2629 surroundContents : surroundContents,
bgneal@183 2630 cloneRange : cloneRange
bgneal@45 2631 });
bgneal@183 2632
bgneal@183 2633 function setStart(n, o) {
bgneal@183 2634 _setEndPoint(TRUE, n, o);
bgneal@183 2635 };
bgneal@183 2636
bgneal@183 2637 function setEnd(n, o) {
bgneal@183 2638 _setEndPoint(FALSE, n, o);
bgneal@183 2639 };
bgneal@183 2640
bgneal@183 2641 function setStartBefore(n) {
bgneal@183 2642 setStart(n.parentNode, nodeIndex(n));
bgneal@183 2643 };
bgneal@183 2644
bgneal@183 2645 function setStartAfter(n) {
bgneal@183 2646 setStart(n.parentNode, nodeIndex(n) + 1);
bgneal@183 2647 };
bgneal@183 2648
bgneal@183 2649 function setEndBefore(n) {
bgneal@183 2650 setEnd(n.parentNode, nodeIndex(n));
bgneal@183 2651 };
bgneal@183 2652
bgneal@183 2653 function setEndAfter(n) {
bgneal@183 2654 setEnd(n.parentNode, nodeIndex(n) + 1);
bgneal@183 2655 };
bgneal@183 2656
bgneal@183 2657 function collapse(ts) {
bgneal@45 2658 if (ts) {
bgneal@183 2659 t[END_CONTAINER] = t[START_CONTAINER];
bgneal@183 2660 t[END_OFFSET] = t[START_OFFSET];
bgneal@45 2661 } else {
bgneal@183 2662 t[START_CONTAINER] = t[END_CONTAINER];
bgneal@183 2663 t[START_OFFSET] = t[END_OFFSET];
bgneal@183 2664 }
bgneal@183 2665
bgneal@183 2666 t.collapsed = TRUE;
bgneal@183 2667 };
bgneal@183 2668
bgneal@183 2669 function selectNode(n) {
bgneal@183 2670 setStartBefore(n);
bgneal@183 2671 setEndAfter(n);
bgneal@183 2672 };
bgneal@183 2673
bgneal@183 2674 function selectNodeContents(n) {
bgneal@183 2675 setStart(n, 0);
bgneal@183 2676 setEnd(n, n.nodeType === 1 ? n.childNodes.length : n.nodeValue.length);
bgneal@183 2677 };
bgneal@183 2678
bgneal@183 2679 function compareBoundaryPoints(h, r) {
bgneal@183 2680 var sc = t[START_CONTAINER], so = t[START_OFFSET], ec = t[END_CONTAINER], eo = t[END_OFFSET];
bgneal@45 2681
bgneal@45 2682 // Check START_TO_START
bgneal@45 2683 if (h === 0)
bgneal@183 2684 return _compareBoundaryPoints(sc, so, sc, so);
bgneal@45 2685
bgneal@45 2686 // Check START_TO_END
bgneal@45 2687 if (h === 1)
bgneal@183 2688 return _compareBoundaryPoints(sc, so, ec, eo);
bgneal@45 2689
bgneal@45 2690 // Check END_TO_END
bgneal@45 2691 if (h === 2)
bgneal@183 2692 return _compareBoundaryPoints(ec, eo, ec, eo);
bgneal@45 2693
bgneal@45 2694 // Check END_TO_START
bgneal@45 2695 if (h === 3)
bgneal@183 2696 return _compareBoundaryPoints(ec, eo, sc, so);
bgneal@183 2697 };
bgneal@183 2698
bgneal@183 2699 function deleteContents() {
bgneal@183 2700 _traverse(DELETE);
bgneal@183 2701 };
bgneal@183 2702
bgneal@183 2703 function extractContents() {
bgneal@183 2704 return _traverse(EXTRACT);
bgneal@183 2705 };
bgneal@183 2706
bgneal@183 2707 function cloneContents() {
bgneal@183 2708 return _traverse(CLONE);
bgneal@183 2709 };
bgneal@183 2710
bgneal@183 2711 function insertNode(n) {
bgneal@183 2712 var startContainer = this[START_CONTAINER],
bgneal@183 2713 startOffset = this[START_OFFSET], nn, o;
bgneal@45 2714
bgneal@45 2715 // Node is TEXT_NODE or CDATA
bgneal@183 2716 if ((startContainer.nodeType === 3 || startContainer.nodeType === 4) && startContainer.nodeValue) {
bgneal@183 2717 if (!startOffset) {
bgneal@183 2718 // At the start of text
bgneal@183 2719 startContainer.parentNode.insertBefore(n, startContainer);
bgneal@183 2720 } else if (startOffset >= startContainer.nodeValue.length) {
bgneal@183 2721 // At the end of text
bgneal@183 2722 dom.insertAfter(n, startContainer);
bgneal@183 2723 } else {
bgneal@183 2724 // Middle, need to split
bgneal@183 2725 nn = startContainer.splitText(startOffset);
bgneal@183 2726 startContainer.parentNode.insertBefore(n, nn);
bgneal@183 2727 }
bgneal@45 2728 } else {
bgneal@45 2729 // Insert element node
bgneal@183 2730 if (startContainer.childNodes.length > 0)
bgneal@183 2731 o = startContainer.childNodes[startOffset];
bgneal@183 2732
bgneal@183 2733 if (o)
bgneal@183 2734 startContainer.insertBefore(n, o);
bgneal@183 2735 else
bgneal@183 2736 startContainer.appendChild(n);
bgneal@183 2737 }
bgneal@183 2738 };
bgneal@183 2739
bgneal@183 2740 function surroundContents(n) {
bgneal@183 2741 var f = t.extractContents();
bgneal@45 2742
bgneal@45 2743 t.insertNode(n);
bgneal@45 2744 n.appendChild(f);
bgneal@45 2745 t.selectNode(n);
bgneal@183 2746 };
bgneal@183 2747
bgneal@183 2748 function cloneRange() {
bgneal@183 2749 return extend(new Range(dom), {
bgneal@183 2750 startContainer : t[START_CONTAINER],
bgneal@183 2751 startOffset : t[START_OFFSET],
bgneal@183 2752 endContainer : t[END_CONTAINER],
bgneal@183 2753 endOffset : t[END_OFFSET],
bgneal@45 2754 collapsed : t.collapsed,
bgneal@45 2755 commonAncestorContainer : t.commonAncestorContainer
bgneal@45 2756 });
bgneal@183 2757 };
bgneal@183 2758
bgneal@183 2759 // Private methods
bgneal@183 2760
bgneal@183 2761 function _getSelectedNode(container, offset) {
bgneal@183 2762 var child;
bgneal@183 2763
bgneal@183 2764 if (container.nodeType == 3 /* TEXT_NODE */)
bgneal@183 2765 return container;
bgneal@183 2766
bgneal@183 2767 if (offset < 0)
bgneal@183 2768 return container;
bgneal@183 2769
bgneal@183 2770 child = container.firstChild;
bgneal@183 2771 while (child && offset > 0) {
bgneal@183 2772 --offset;
bgneal@183 2773 child = child.nextSibling;
bgneal@183 2774 }
bgneal@183 2775
bgneal@183 2776 if (child)
bgneal@183 2777 return child;
bgneal@183 2778
bgneal@183 2779 return container;
bgneal@183 2780 };
bgneal@183 2781
bgneal@183 2782 function _isCollapsed() {
bgneal@183 2783 return (t[START_CONTAINER] == t[END_CONTAINER] && t[START_OFFSET] == t[END_OFFSET]);
bgneal@183 2784 };
bgneal@183 2785
bgneal@183 2786 function _compareBoundaryPoints(containerA, offsetA, containerB, offsetB) {
bgneal@45 2787 var c, offsetC, n, cmnRoot, childA, childB;
bgneal@45 2788
bgneal@183 2789 // In the first case the boundary-points have the same container. A is before B
bgneal@183 2790 // if its offset is less than the offset of B, A is equal to B if its offset is
bgneal@183 2791 // equal to the offset of B, and A is after B if its offset is greater than the
bgneal@45 2792 // offset of B.
bgneal@45 2793 if (containerA == containerB) {
bgneal@183 2794 if (offsetA == offsetB)
bgneal@45 2795 return 0; // equal
bgneal@183 2796
bgneal@183 2797 if (offsetA < offsetB)
bgneal@45 2798 return -1; // before
bgneal@183 2799
bgneal@183 2800 return 1; // after
bgneal@183 2801 }
bgneal@183 2802
bgneal@183 2803 // In the second case a child node C of the container of A is an ancestor
bgneal@183 2804 // container of B. In this case, A is before B if the offset of A is less than or
bgneal@45 2805 // equal to the index of the child node C and A is after B otherwise.
bgneal@45 2806 c = containerB;
bgneal@183 2807 while (c && c.parentNode != containerA)
bgneal@45 2808 c = c.parentNode;
bgneal@183 2809
bgneal@45 2810 if (c) {
bgneal@45 2811 offsetC = 0;
bgneal@45 2812 n = containerA.firstChild;
bgneal@45 2813
bgneal@45 2814 while (n != c && offsetC < offsetA) {
bgneal@45 2815 offsetC++;
bgneal@45 2816 n = n.nextSibling;
bgneal@45 2817 }
bgneal@45 2818
bgneal@183 2819 if (offsetA <= offsetC)
bgneal@45 2820 return -1; // before
bgneal@183 2821
bgneal@183 2822 return 1; // after
bgneal@183 2823 }
bgneal@183 2824
bgneal@183 2825 // In the third case a child node C of the container of B is an ancestor container
bgneal@183 2826 // of A. In this case, A is before B if the index of the child node C is less than
bgneal@45 2827 // the offset of B and A is after B otherwise.
bgneal@45 2828 c = containerA;
bgneal@45 2829 while (c && c.parentNode != containerB) {
bgneal@45 2830 c = c.parentNode;
bgneal@45 2831 }
bgneal@45 2832
bgneal@45 2833 if (c) {
bgneal@45 2834 offsetC = 0;
bgneal@45 2835 n = containerB.firstChild;
bgneal@45 2836
bgneal@45 2837 while (n != c && offsetC < offsetB) {
bgneal@45 2838 offsetC++;
bgneal@45 2839 n = n.nextSibling;
bgneal@45 2840 }
bgneal@45 2841
bgneal@183 2842 if (offsetC < offsetB)
bgneal@45 2843 return -1; // before
bgneal@183 2844
bgneal@183 2845 return 1; // after
bgneal@183 2846 }
bgneal@183 2847
bgneal@183 2848 // In the fourth case, none of three other cases hold: the containers of A and B
bgneal@183 2849 // are siblings or descendants of sibling nodes. In this case, A is before B if
bgneal@45 2850 // the container of A is before the container of B in a pre-order traversal of the
bgneal@45 2851 // Ranges' context tree and A is after B otherwise.
bgneal@183 2852 cmnRoot = dom.findCommonAncestor(containerA, containerB);
bgneal@45 2853 childA = containerA;
bgneal@45 2854
bgneal@183 2855 while (childA && childA.parentNode != cmnRoot)
bgneal@183 2856 childA = childA.parentNode;
bgneal@183 2857
bgneal@183 2858 if (!childA)
bgneal@45 2859 childA = cmnRoot;
bgneal@45 2860
bgneal@45 2861 childB = containerB;
bgneal@183 2862 while (childB && childB.parentNode != cmnRoot)
bgneal@45 2863 childB = childB.parentNode;
bgneal@183 2864
bgneal@183 2865 if (!childB)
bgneal@45 2866 childB = cmnRoot;
bgneal@183 2867
bgneal@183 2868 if (childA == childB)
bgneal@45 2869 return 0; // equal
bgneal@45 2870
bgneal@45 2871 n = cmnRoot.firstChild;
bgneal@45 2872 while (n) {
bgneal@183 2873 if (n == childA)
bgneal@45 2874 return -1; // before
bgneal@183 2875
bgneal@183 2876 if (n == childB)
bgneal@45 2877 return 1; // after
bgneal@45 2878
bgneal@45 2879 n = n.nextSibling;
bgneal@45 2880 }
bgneal@183 2881 };
bgneal@183 2882
bgneal@183 2883 function _setEndPoint(st, n, o) {
bgneal@183 2884 var ec, sc;
bgneal@45 2885
bgneal@45 2886 if (st) {
bgneal@183 2887 t[START_CONTAINER] = n;
bgneal@183 2888 t[START_OFFSET] = o;
bgneal@45 2889 } else {
bgneal@183 2890 t[END_CONTAINER] = n;
bgneal@183 2891 t[END_OFFSET] = o;
bgneal@183 2892 }
bgneal@183 2893
bgneal@183 2894 // If one boundary-point of a Range is set to have a root container
bgneal@183 2895 // other than the current one for the Range, the Range is collapsed to
bgneal@45 2896 // the new position. This enforces the restriction that both boundary-
bgneal@45 2897 // points of a Range must have the same root container.
bgneal@183 2898 ec = t[END_CONTAINER];
bgneal@45 2899 while (ec.parentNode)
bgneal@45 2900 ec = ec.parentNode;
bgneal@45 2901
bgneal@183 2902 sc = t[START_CONTAINER];
bgneal@45 2903 while (sc.parentNode)
bgneal@45 2904 sc = sc.parentNode;
bgneal@45 2905
bgneal@183 2906 if (sc == ec) {
bgneal@183 2907 // The start position of a Range is guaranteed to never be after the
bgneal@183 2908 // end position. To enforce this restriction, if the start is set to
bgneal@183 2909 // be at a position after the end, the Range is collapsed to that
bgneal@183 2910 // position.
bgneal@183 2911 if (_compareBoundaryPoints(t[START_CONTAINER], t[START_OFFSET], t[END_CONTAINER], t[END_OFFSET]) > 0)
bgneal@183 2912 t.collapse(st);
bgneal@183 2913 } else
bgneal@45 2914 t.collapse(st);
bgneal@183 2915
bgneal@183 2916 t.collapsed = _isCollapsed();
bgneal@183 2917 t.commonAncestorContainer = dom.findCommonAncestor(t[START_CONTAINER], t[END_CONTAINER]);
bgneal@183 2918 };
bgneal@183 2919
bgneal@183 2920 function _traverse(how) {
bgneal@183 2921 var c, endContainerDepth = 0, startContainerDepth = 0, p, depthDiff, startNode, endNode, sp, ep;
bgneal@183 2922
bgneal@183 2923 if (t[START_CONTAINER] == t[END_CONTAINER])
bgneal@183 2924 return _traverseSameContainer(how);
bgneal@183 2925
bgneal@183 2926 for (c = t[END_CONTAINER], p = c.parentNode; p; c = p, p = p.parentNode) {
bgneal@183 2927 if (p == t[START_CONTAINER])
bgneal@183 2928 return _traverseCommonStartContainer(c, how);
bgneal@45 2929
bgneal@45 2930 ++endContainerDepth;
bgneal@45 2931 }
bgneal@45 2932
bgneal@183 2933 for (c = t[START_CONTAINER], p = c.parentNode; p; c = p, p = p.parentNode) {
bgneal@183 2934 if (p == t[END_CONTAINER])
bgneal@183 2935 return _traverseCommonEndContainer(c, how);
bgneal@45 2936
bgneal@45 2937 ++startContainerDepth;
bgneal@45 2938 }
bgneal@45 2939
bgneal@45 2940 depthDiff = startContainerDepth - endContainerDepth;
bgneal@45 2941
bgneal@183 2942 startNode = t[START_CONTAINER];
bgneal@45 2943 while (depthDiff > 0) {
bgneal@45 2944 startNode = startNode.parentNode;
bgneal@45 2945 depthDiff--;
bgneal@45 2946 }
bgneal@45 2947
bgneal@183 2948 endNode = t[END_CONTAINER];
bgneal@45 2949 while (depthDiff < 0) {
bgneal@45 2950 endNode = endNode.parentNode;
bgneal@45 2951 depthDiff++;
bgneal@45 2952 }
bgneal@45 2953
bgneal@45 2954 // ascend the ancestor hierarchy until we have a common parent.
bgneal@45 2955 for (sp = startNode.parentNode, ep = endNode.parentNode; sp != ep; sp = sp.parentNode, ep = ep.parentNode) {
bgneal@45 2956 startNode = sp;
bgneal@45 2957 endNode = ep;
bgneal@45 2958 }
bgneal@45 2959
bgneal@183 2960 return _traverseCommonAncestors(startNode, endNode, how);
bgneal@183 2961 };
bgneal@183 2962
bgneal@183 2963 function _traverseSameContainer(how) {
bgneal@183 2964 var frag, s, sub, n, cnt, sibling, xferNode;
bgneal@45 2965
bgneal@45 2966 if (how != DELETE)
bgneal@183 2967 frag = doc.createDocumentFragment();
bgneal@45 2968
bgneal@45 2969 // If selection is empty, just return the fragment
bgneal@183 2970 if (t[START_OFFSET] == t[END_OFFSET])
bgneal@45 2971 return frag;
bgneal@45 2972
bgneal@45 2973 // Text node needs special case handling
bgneal@183 2974 if (t[START_CONTAINER].nodeType == 3 /* TEXT_NODE */) {
bgneal@45 2975 // get the substring
bgneal@183 2976 s = t[START_CONTAINER].nodeValue;
bgneal@183 2977 sub = s.substring(t[START_OFFSET], t[END_OFFSET]);
bgneal@45 2978
bgneal@45 2979 // set the original text node to its new value
bgneal@45 2980 if (how != CLONE) {
bgneal@183 2981 t[START_CONTAINER].deleteData(t[START_OFFSET], t[END_OFFSET] - t[START_OFFSET]);
bgneal@45 2982
bgneal@45 2983 // Nothing is partially selected, so collapse to start point
bgneal@183 2984 t.collapse(TRUE);
bgneal@45 2985 }
bgneal@45 2986
bgneal@45 2987 if (how == DELETE)
bgneal@183 2988 return;
bgneal@183 2989
bgneal@183 2990 frag.appendChild(doc.createTextNode(sub));
bgneal@45 2991 return frag;
bgneal@45 2992 }
bgneal@45 2993
bgneal@45 2994 // Copy nodes between the start/end offsets.
bgneal@183 2995 n = _getSelectedNode(t[START_CONTAINER], t[START_OFFSET]);
bgneal@183 2996 cnt = t[END_OFFSET] - t[START_OFFSET];
bgneal@45 2997
bgneal@45 2998 while (cnt > 0) {
bgneal@45 2999 sibling = n.nextSibling;
bgneal@183 3000 xferNode = _traverseFullySelected(n, how);
bgneal@45 3001
bgneal@45 3002 if (frag)
bgneal@45 3003 frag.appendChild( xferNode );
bgneal@45 3004
bgneal@45 3005 --cnt;
bgneal@45 3006 n = sibling;
bgneal@45 3007 }
bgneal@45 3008
bgneal@45 3009 // Nothing is partially selected, so collapse to start point
bgneal@45 3010 if (how != CLONE)
bgneal@183 3011 t.collapse(TRUE);
bgneal@45 3012
bgneal@45 3013 return frag;
bgneal@183 3014 };
bgneal@183 3015
bgneal@183 3016 function _traverseCommonStartContainer(endAncestor, how) {
bgneal@183 3017 var frag, n, endIdx, cnt, sibling, xferNode;
bgneal@45 3018
bgneal@45 3019 if (how != DELETE)
bgneal@183 3020 frag = doc.createDocumentFragment();
bgneal@183 3021
bgneal@183 3022 n = _traverseRightBoundary(endAncestor, how);
bgneal@45 3023
bgneal@45 3024 if (frag)
bgneal@45 3025 frag.appendChild(n);
bgneal@45 3026
bgneal@183 3027 endIdx = nodeIndex(endAncestor);
bgneal@183 3028 cnt = endIdx - t[START_OFFSET];
bgneal@45 3029
bgneal@45 3030 if (cnt <= 0) {
bgneal@183 3031 // Collapse to just before the endAncestor, which
bgneal@45 3032 // is partially selected.
bgneal@45 3033 if (how != CLONE) {
bgneal@45 3034 t.setEndBefore(endAncestor);
bgneal@183 3035 t.collapse(FALSE);
bgneal@45 3036 }
bgneal@45 3037
bgneal@45 3038 return frag;
bgneal@45 3039 }
bgneal@45 3040
bgneal@45 3041 n = endAncestor.previousSibling;
bgneal@45 3042 while (cnt > 0) {
bgneal@45 3043 sibling = n.previousSibling;
bgneal@183 3044 xferNode = _traverseFullySelected(n, how);
bgneal@45 3045
bgneal@45 3046 if (frag)
bgneal@45 3047 frag.insertBefore(xferNode, frag.firstChild);
bgneal@45 3048
bgneal@45 3049 --cnt;
bgneal@45 3050 n = sibling;
bgneal@45 3051 }
bgneal@45 3052
bgneal@183 3053 // Collapse to just before the endAncestor, which
bgneal@45 3054 // is partially selected.
bgneal@45 3055 if (how != CLONE) {
bgneal@45 3056 t.setEndBefore(endAncestor);
bgneal@183 3057 t.collapse(FALSE);
bgneal@45 3058 }
bgneal@45 3059
bgneal@45 3060 return frag;
bgneal@183 3061 };
bgneal@183 3062
bgneal@183 3063 function _traverseCommonEndContainer(startAncestor, how) {
bgneal@183 3064 var frag, startIdx, n, cnt, sibling, xferNode;
bgneal@45 3065
bgneal@45 3066 if (how != DELETE)
bgneal@183 3067 frag = doc.createDocumentFragment();
bgneal@183 3068
bgneal@183 3069 n = _traverseLeftBoundary(startAncestor, how);
bgneal@45 3070 if (frag)
bgneal@45 3071 frag.appendChild(n);
bgneal@45 3072
bgneal@183 3073 startIdx = nodeIndex(startAncestor);
bgneal@45 3074 ++startIdx; // Because we already traversed it....
bgneal@45 3075
bgneal@183 3076 cnt = t[END_OFFSET] - startIdx;
bgneal@45 3077 n = startAncestor.nextSibling;
bgneal@45 3078 while (cnt > 0) {
bgneal@45 3079 sibling = n.nextSibling;
bgneal@183 3080 xferNode = _traverseFullySelected(n, how);
bgneal@45 3081
bgneal@45 3082 if (frag)
bgneal@45 3083 frag.appendChild(xferNode);
bgneal@45 3084
bgneal@45 3085 --cnt;
bgneal@45 3086 n = sibling;
bgneal@45 3087 }
bgneal@45 3088
bgneal@45 3089 if (how != CLONE) {
bgneal@45 3090 t.setStartAfter(startAncestor);
bgneal@183 3091 t.collapse(TRUE);
bgneal@45 3092 }
bgneal@45 3093
bgneal@45 3094 return frag;
bgneal@183 3095 };
bgneal@183 3096
bgneal@183 3097 function _traverseCommonAncestors(startAncestor, endAncestor, how) {
bgneal@183 3098 var n, frag, commonParent, startOffset, endOffset, cnt, sibling, nextSibling;
bgneal@45 3099
bgneal@45 3100 if (how != DELETE)
bgneal@183 3101 frag = doc.createDocumentFragment();
bgneal@183 3102
bgneal@183 3103 n = _traverseLeftBoundary(startAncestor, how);
bgneal@45 3104 if (frag)
bgneal@45 3105 frag.appendChild(n);
bgneal@45 3106
bgneal@45 3107 commonParent = startAncestor.parentNode;
bgneal@183 3108 startOffset = nodeIndex(startAncestor);
bgneal@183 3109 endOffset = nodeIndex(endAncestor);
bgneal@45 3110 ++startOffset;
bgneal@45 3111
bgneal@45 3112 cnt = endOffset - startOffset;
bgneal@45 3113 sibling = startAncestor.nextSibling;
bgneal@45 3114
bgneal@45 3115 while (cnt > 0) {
bgneal@45 3116 nextSibling = sibling.nextSibling;
bgneal@183 3117 n = _traverseFullySelected(sibling, how);
bgneal@45 3118
bgneal@45 3119 if (frag)
bgneal@45 3120 frag.appendChild(n);
bgneal@45 3121
bgneal@45 3122 sibling = nextSibling;
bgneal@45 3123 --cnt;
bgneal@45 3124 }
bgneal@45 3125
bgneal@183 3126 n = _traverseRightBoundary(endAncestor, how);
bgneal@45 3127
bgneal@45 3128 if (frag)
bgneal@45 3129 frag.appendChild(n);
bgneal@45 3130
bgneal@45 3131 if (how != CLONE) {
bgneal@45 3132 t.setStartAfter(startAncestor);
bgneal@183 3133 t.collapse(TRUE);
bgneal@45 3134 }
bgneal@45 3135
bgneal@45 3136 return frag;
bgneal@183 3137 };
bgneal@183 3138
bgneal@183 3139 function _traverseRightBoundary(root, how) {
bgneal@183 3140 var next = _getSelectedNode(t[END_CONTAINER], t[END_OFFSET] - 1), parent, clonedParent, prevSibling, clonedChild, clonedGrandParent, isFullySelected = next != t[END_CONTAINER];
bgneal@45 3141
bgneal@45 3142 if (next == root)
bgneal@183 3143 return _traverseNode(next, isFullySelected, FALSE, how);
bgneal@45 3144
bgneal@45 3145 parent = next.parentNode;
bgneal@183 3146 clonedParent = _traverseNode(parent, FALSE, FALSE, how);
bgneal@183 3147
bgneal@183 3148 while (parent) {
bgneal@183 3149 while (next) {
bgneal@45 3150 prevSibling = next.previousSibling;
bgneal@183 3151 clonedChild = _traverseNode(next, isFullySelected, FALSE, how);
bgneal@45 3152
bgneal@45 3153 if (how != DELETE)
bgneal@45 3154 clonedParent.insertBefore(clonedChild, clonedParent.firstChild);
bgneal@45 3155
bgneal@183 3156 isFullySelected = TRUE;
bgneal@45 3157 next = prevSibling;
bgneal@45 3158 }
bgneal@45 3159
bgneal@45 3160 if (parent == root)
bgneal@45 3161 return clonedParent;
bgneal@45 3162
bgneal@45 3163 next = parent.previousSibling;
bgneal@45 3164 parent = parent.parentNode;
bgneal@45 3165
bgneal@183 3166 clonedGrandParent = _traverseNode(parent, FALSE, FALSE, how);
bgneal@45 3167
bgneal@45 3168 if (how != DELETE)
bgneal@45 3169 clonedGrandParent.appendChild(clonedParent);
bgneal@45 3170
bgneal@45 3171 clonedParent = clonedGrandParent;
bgneal@45 3172 }
bgneal@183 3173 };
bgneal@183 3174
bgneal@183 3175 function _traverseLeftBoundary(root, how) {
bgneal@183 3176 var next = _getSelectedNode(t[START_CONTAINER], t[START_OFFSET]), isFullySelected = next != t[START_CONTAINER], parent, clonedParent, nextSibling, clonedChild, clonedGrandParent;
bgneal@45 3177
bgneal@45 3178 if (next == root)
bgneal@183 3179 return _traverseNode(next, isFullySelected, TRUE, how);
bgneal@45 3180
bgneal@45 3181 parent = next.parentNode;
bgneal@183 3182 clonedParent = _traverseNode(parent, FALSE, TRUE, how);
bgneal@183 3183
bgneal@183 3184 while (parent) {
bgneal@183 3185 while (next) {
bgneal@45 3186 nextSibling = next.nextSibling;
bgneal@183 3187 clonedChild = _traverseNode(next, isFullySelected, TRUE, how);
bgneal@45 3188
bgneal@45 3189 if (how != DELETE)
bgneal@45 3190 clonedParent.appendChild(clonedChild);
bgneal@45 3191
bgneal@183 3192 isFullySelected = TRUE;
bgneal@45 3193 next = nextSibling;
bgneal@45 3194 }
bgneal@45 3195
bgneal@45 3196 if (parent == root)
bgneal@45 3197 return clonedParent;
bgneal@45 3198
bgneal@45 3199 next = parent.nextSibling;
bgneal@45 3200 parent = parent.parentNode;
bgneal@45 3201
bgneal@183 3202 clonedGrandParent = _traverseNode(parent, FALSE, TRUE, how);
bgneal@45 3203
bgneal@45 3204 if (how != DELETE)
bgneal@45 3205 clonedGrandParent.appendChild(clonedParent);
bgneal@45 3206
bgneal@45 3207 clonedParent = clonedGrandParent;
bgneal@45 3208 }
bgneal@183 3209 };
bgneal@183 3210
bgneal@183 3211 function _traverseNode(n, isFullySelected, isLeft, how) {
bgneal@183 3212 var txtValue, newNodeValue, oldNodeValue, offset, newNode;
bgneal@45 3213
bgneal@45 3214 if (isFullySelected)
bgneal@183 3215 return _traverseFullySelected(n, how);
bgneal@45 3216
bgneal@45 3217 if (n.nodeType == 3 /* TEXT_NODE */) {
bgneal@45 3218 txtValue = n.nodeValue;
bgneal@45 3219
bgneal@45 3220 if (isLeft) {
bgneal@183 3221 offset = t[START_OFFSET];
bgneal@45 3222 newNodeValue = txtValue.substring(offset);
bgneal@45 3223 oldNodeValue = txtValue.substring(0, offset);
bgneal@45 3224 } else {
bgneal@183 3225 offset = t[END_OFFSET];
bgneal@45 3226 newNodeValue = txtValue.substring(0, offset);
bgneal@45 3227 oldNodeValue = txtValue.substring(offset);
bgneal@45 3228 }
bgneal@45 3229
bgneal@45 3230 if (how != CLONE)
bgneal@45 3231 n.nodeValue = oldNodeValue;
bgneal@45 3232
bgneal@45 3233 if (how == DELETE)
bgneal@183 3234 return;
bgneal@183 3235
bgneal@183 3236 newNode = n.cloneNode(FALSE);
bgneal@45 3237 newNode.nodeValue = newNodeValue;
bgneal@45 3238
bgneal@45 3239 return newNode;
bgneal@45 3240 }
bgneal@45 3241
bgneal@45 3242 if (how == DELETE)
bgneal@183 3243 return;
bgneal@183 3244
bgneal@183 3245 return n.cloneNode(FALSE);
bgneal@183 3246 };
bgneal@183 3247
bgneal@183 3248 function _traverseFullySelected(n, how) {
bgneal@45 3249 if (how != DELETE)
bgneal@183 3250 return how == CLONE ? n.cloneNode(TRUE) : n;
bgneal@45 3251
bgneal@45 3252 n.parentNode.removeChild(n);
bgneal@183 3253 };
bgneal@183 3254 };
bgneal@45 3255
bgneal@45 3256 ns.Range = Range;
bgneal@45 3257 })(tinymce.dom);
bgneal@183 3258
bgneal@45 3259 (function() {
bgneal@45 3260 function Selection(selection) {
bgneal@183 3261 var t = this, invisibleChar = '\uFEFF', range, lastIERng, dom = selection.dom, TRUE = true, FALSE = false;
bgneal@183 3262
bgneal@183 3263 // Returns a W3C DOM compatible range object by using the IE Range API
bgneal@45 3264 function getRange() {
bgneal@217 3265 var ieRange = selection.getRng(), domRange = dom.createRng(), element, collapsed;
bgneal@183 3266
bgneal@183 3267 // If selection is outside the current document just return an empty range
bgneal@183 3268 element = ieRange.item ? ieRange.item(0) : ieRange.parentElement();
bgneal@183 3269 if (element.ownerDocument != dom.doc)
bgneal@45 3270 return domRange;
bgneal@183 3271
bgneal@183 3272 // Handle control selection or text selection of a image
bgneal@183 3273 if (ieRange.item || !element.hasChildNodes()) {
bgneal@183 3274 domRange.setStart(element.parentNode, dom.nodeIndex(element));
bgneal@183 3275 domRange.setEnd(domRange.startContainer, domRange.startOffset + 1);
bgneal@183 3276
bgneal@183 3277 return domRange;
bgneal@183 3278 }
bgneal@183 3279
bgneal@183 3280 collapsed = selection.isCollapsed();
bgneal@183 3281
bgneal@217 3282 function findEndPoint(start) {
bgneal@217 3283 var marker, container, offset, nodes, startIndex = 0, endIndex, index, parent, checkRng, position;
bgneal@217 3284
bgneal@217 3285 // Setup temp range and collapse it
bgneal@217 3286 checkRng = ieRange.duplicate();
bgneal@217 3287 checkRng.collapse(start);
bgneal@217 3288
bgneal@217 3289 // Create marker and insert it at the end of the endpoints parent
bgneal@217 3290 marker = dom.create('a');
bgneal@217 3291 parent = checkRng.parentElement();
bgneal@217 3292 parent.appendChild(marker);
bgneal@217 3293 checkRng.moveToElementText(marker);
bgneal@217 3294 position = ieRange.compareEndPoints(start ? 'StartToStart' : 'EndToEnd', checkRng);
bgneal@217 3295 if (position > 0) {
bgneal@217 3296 // The position is after the end of the parent element.
bgneal@217 3297 // This is the case where IE puts the caret to the left edge of a table.
bgneal@217 3298 domRange[start ? 'setStartAfter' : 'setEndAfter'](parent);
bgneal@183 3299 dom.remove(marker);
bgneal@217 3300 return;
bgneal@217 3301 }
bgneal@217 3302
bgneal@217 3303 // Setup node list and endIndex
bgneal@217 3304 nodes = tinymce.grep(parent.childNodes);
bgneal@217 3305 endIndex = nodes.length - 1;
bgneal@217 3306 // Perform a binary search for the position
bgneal@217 3307 while (startIndex <= endIndex) {
bgneal@217 3308 index = Math.floor((startIndex + endIndex) / 2);
bgneal@217 3309
bgneal@217 3310 // Insert marker and check it's position relative to the selection
bgneal@217 3311 parent.insertBefore(marker, nodes[index]);
bgneal@217 3312 checkRng.moveToElementText(marker);
bgneal@217 3313 position = ieRange.compareEndPoints(start ? 'StartToStart' : 'EndToEnd', checkRng);
bgneal@217 3314 if (position > 0) {
bgneal@217 3315 // Marker is to the right
bgneal@217 3316 startIndex = index + 1;
bgneal@217 3317 } else if (position < 0) {
bgneal@217 3318 // Marker is to the left
bgneal@217 3319 endIndex = index - 1;
bgneal@217 3320 } else {
bgneal@217 3321 // Maker is where we are
bgneal@217 3322 found = true;
bgneal@217 3323 break;
bgneal@217 3324 }
bgneal@217 3325 }
bgneal@217 3326
bgneal@217 3327 // Setup container
bgneal@217 3328 container = position > 0 || index == 0 ? marker.nextSibling : marker.previousSibling;
bgneal@217 3329
bgneal@217 3330 // Handle element selection
bgneal@217 3331 if (container.nodeType == 1) {
bgneal@217 3332 dom.remove(marker);
bgneal@217 3333
bgneal@217 3334 // Find offset and container
bgneal@217 3335 offset = dom.nodeIndex(container);
bgneal@217 3336 container = container.parentNode;
bgneal@217 3337
bgneal@217 3338 // Move the offset if we are setting the end or the position is after an element
bgneal@217 3339 if (!start || index > 0)
bgneal@217 3340 offset++;
bgneal@183 3341 } else {
bgneal@217 3342 // Calculate offset within text node
bgneal@217 3343 if (position > 0 || index == 0) {
bgneal@217 3344 checkRng.setEndPoint(start ? 'StartToStart' : 'EndToEnd', ieRange);
bgneal@217 3345 offset = checkRng.text.length;
bgneal@183 3346 } else {
bgneal@217 3347 checkRng.setEndPoint(start ? 'StartToStart' : 'EndToEnd', ieRange);
bgneal@217 3348 offset = container.nodeValue.length - checkRng.text.length;
bgneal@183 3349 }
bgneal@183 3350
bgneal@183 3351 dom.remove(marker);
bgneal@183 3352 }
bgneal@183 3353
bgneal@217 3354 domRange[start ? 'setStart' : 'setEnd'](container, offset);
bgneal@45 3355 };
bgneal@45 3356
bgneal@217 3357 // Find start point
bgneal@217 3358 findEndPoint(true);
bgneal@217 3359
bgneal@217 3360 // Find end point if needed
bgneal@183 3361 if (!collapsed)
bgneal@217 3362 findEndPoint();
bgneal@183 3363
bgneal@183 3364 return domRange;
bgneal@183 3365 };
bgneal@183 3366
bgneal@183 3367 this.addRange = function(rng) {
bgneal@183 3368 var ieRng, ieRng2, doc = selection.dom.doc, body = doc.body, startPos, endPos, sc, so, ec, eo, marker, lastIndex, skipStart, skipEnd;
bgneal@183 3369
bgneal@183 3370 this.destroy();
bgneal@183 3371
bgneal@183 3372 // Setup some shorter versions
bgneal@183 3373 sc = rng.startContainer;
bgneal@183 3374 so = rng.startOffset;
bgneal@183 3375 ec = rng.endContainer;
bgneal@183 3376 eo = rng.endOffset;
bgneal@183 3377 ieRng = body.createTextRange();
bgneal@183 3378
bgneal@183 3379 // If document selection move caret to first node in document
bgneal@183 3380 if (sc == doc || ec == doc) {
bgneal@183 3381 ieRng = body.createTextRange();
bgneal@183 3382 ieRng.collapse();
bgneal@183 3383 ieRng.select();
bgneal@183 3384 return;
bgneal@183 3385 }
bgneal@183 3386
bgneal@183 3387 // If child index resolve it
bgneal@183 3388 if (sc.nodeType == 1 && sc.hasChildNodes()) {
bgneal@183 3389 lastIndex = sc.childNodes.length - 1;
bgneal@183 3390
bgneal@183 3391 // Index is higher that the child count then we need to jump over the start container
bgneal@183 3392 if (so > lastIndex) {
bgneal@183 3393 skipStart = 1;
bgneal@183 3394 sc = sc.childNodes[lastIndex];
bgneal@183 3395 } else
bgneal@183 3396 sc = sc.childNodes[so];
bgneal@183 3397
bgneal@183 3398 // Child was text node then move offset to start of it
bgneal@183 3399 if (sc.nodeType == 3)
bgneal@183 3400 so = 0;
bgneal@183 3401 }
bgneal@183 3402
bgneal@183 3403 // If child index resolve it
bgneal@183 3404 if (ec.nodeType == 1 && ec.hasChildNodes()) {
bgneal@183 3405 lastIndex = ec.childNodes.length - 1;
bgneal@183 3406
bgneal@183 3407 if (eo == 0) {
bgneal@183 3408 skipEnd = 1;
bgneal@183 3409 ec = ec.childNodes[0];
bgneal@183 3410 } else {
bgneal@183 3411 ec = ec.childNodes[Math.min(lastIndex, eo - 1)];
bgneal@183 3412
bgneal@183 3413 // Child was text node then move offset to end of text node
bgneal@183 3414 if (ec.nodeType == 3)
bgneal@183 3415 eo = ec.nodeValue.length;
bgneal@183 3416 }
bgneal@183 3417 }
bgneal@183 3418
bgneal@183 3419 // Single element selection
bgneal@183 3420 if (sc == ec && sc.nodeType == 1) {
bgneal@183 3421 // Make control selection for some elements
bgneal@183 3422 if (/^(IMG|TABLE)$/.test(sc.nodeName) && so != eo) {
bgneal@183 3423 ieRng = body.createControlRange();
bgneal@183 3424 ieRng.addElement(sc);
bgneal@183 3425 } else {
bgneal@183 3426 ieRng = body.createTextRange();
bgneal@183 3427
bgneal@183 3428 // Padd empty elements with invisible character
bgneal@183 3429 if (!sc.hasChildNodes() && sc.canHaveHTML)
bgneal@183 3430 sc.innerHTML = invisibleChar;
bgneal@183 3431
bgneal@183 3432 // Select element contents
bgneal@183 3433 ieRng.moveToElementText(sc);
bgneal@183 3434
bgneal@183 3435 // If it's only containing a padding remove it so the caret remains
bgneal@183 3436 if (sc.innerHTML == invisibleChar) {
bgneal@183 3437 ieRng.collapse(TRUE);
bgneal@183 3438 sc.removeChild(sc.firstChild);
bgneal@183 3439 }
bgneal@183 3440 }
bgneal@183 3441
bgneal@183 3442 if (so == eo)
bgneal@183 3443 ieRng.collapse(eo <= rng.endContainer.childNodes.length - 1);
bgneal@183 3444
bgneal@183 3445 ieRng.select();
bgneal@183 3446 ieRng.scrollIntoView();
bgneal@183 3447 return;
bgneal@183 3448 }
bgneal@183 3449
bgneal@183 3450 // Create range and marker
bgneal@183 3451 ieRng = body.createTextRange();
bgneal@183 3452 marker = doc.createElement('span');
bgneal@183 3453 marker.innerHTML = ' ';
bgneal@183 3454
bgneal@183 3455 // Set start of range to startContainer/startOffset
bgneal@183 3456 if (sc.nodeType == 3) {
bgneal@183 3457 // Insert marker after/before startContainer
bgneal@183 3458 if (skipStart)
bgneal@183 3459 dom.insertAfter(marker, sc);
bgneal@183 3460 else
bgneal@183 3461 sc.parentNode.insertBefore(marker, sc);
bgneal@183 3462
bgneal@183 3463 // Select marker the caret to offset position
bgneal@183 3464 ieRng.moveToElementText(marker);
bgneal@183 3465 marker.parentNode.removeChild(marker);
bgneal@183 3466 ieRng.move('character', so);
bgneal@183 3467 } else {
bgneal@183 3468 ieRng.moveToElementText(sc);
bgneal@183 3469
bgneal@183 3470 if (skipStart)
bgneal@183 3471 ieRng.collapse(FALSE);
bgneal@183 3472 }
bgneal@183 3473
bgneal@183 3474 // If same text container then we can do a more simple move
bgneal@183 3475 if (sc == ec && sc.nodeType == 3) {
bgneal@217 3476 try {
bgneal@217 3477 ieRng.moveEnd('character', eo - so);
bgneal@217 3478 ieRng.select();
bgneal@217 3479 ieRng.scrollIntoView();
bgneal@217 3480 } catch (ex) {
bgneal@217 3481 // Some times a Runtime error of the 800a025e type gets thrown
bgneal@217 3482 // especially when the caret is placed before a table.
bgneal@217 3483 // This is a somewhat strange location for the caret.
bgneal@217 3484 // TODO: Find a better solution for this would possible require a rewrite of the setRng method
bgneal@217 3485 }
bgneal@217 3486
bgneal@183 3487 return;
bgneal@183 3488 }
bgneal@183 3489
bgneal@183 3490 // Set end of range to endContainer/endOffset
bgneal@183 3491 ieRng2 = body.createTextRange();
bgneal@183 3492 if (ec.nodeType == 3) {
bgneal@183 3493 // Insert marker after/before startContainer
bgneal@183 3494 ec.parentNode.insertBefore(marker, ec);
bgneal@183 3495
bgneal@183 3496 // Move selection to end marker and move caret to end offset
bgneal@183 3497 ieRng2.moveToElementText(marker);
bgneal@183 3498 marker.parentNode.removeChild(marker);
bgneal@183 3499 ieRng2.move('character', eo);
bgneal@183 3500 ieRng.setEndPoint('EndToStart', ieRng2);
bgneal@183 3501 } else {
bgneal@183 3502 ieRng2.moveToElementText(ec);
bgneal@183 3503 ieRng2.collapse(!!skipEnd);
bgneal@183 3504 ieRng.setEndPoint('EndToEnd', ieRng2);
bgneal@183 3505 }
bgneal@183 3506
bgneal@183 3507 ieRng.select();
bgneal@183 3508 ieRng.scrollIntoView();
bgneal@183 3509 };
bgneal@183 3510
bgneal@183 3511 this.getRangeAt = function() {
bgneal@183 3512 // Setup new range if the cache is empty
bgneal@217 3513 if (!range || !tinymce.dom.RangeUtils.compareRanges(lastIERng, selection.getRng())) {
bgneal@183 3514 range = getRange();
bgneal@183 3515
bgneal@183 3516 // Store away text range for next call
bgneal@183 3517 lastIERng = selection.getRng();
bgneal@183 3518 }
bgneal@183 3519
bgneal@217 3520 // IE will say that the range is equal then produce an invalid argument exception
bgneal@217 3521 // if you perform specific operations in a keyup event. For example Ctrl+Del.
bgneal@217 3522 // This hack will invalidate the range cache if the exception occurs
bgneal@217 3523 try {
bgneal@217 3524 range.startContainer.nextSibling;
bgneal@217 3525 } catch (ex) {
bgneal@217 3526 range = getRange();
bgneal@217 3527 lastIERng = null;
bgneal@217 3528 }
bgneal@217 3529
bgneal@183 3530 // Return cached range
bgneal@183 3531 return range;
bgneal@183 3532 };
bgneal@183 3533
bgneal@183 3534 this.destroy = function() {
bgneal@183 3535 // Destroy cached range and last IE range to avoid memory leaks
bgneal@183 3536 lastIERng = range = null;
bgneal@183 3537 };
bgneal@183 3538
bgneal@183 3539 // IE has an issue where you can't select/move the caret by clicking outside the body if the document is in standards mode
bgneal@183 3540 if (selection.dom.boxModel) {
bgneal@183 3541 (function() {
bgneal@183 3542 var doc = dom.doc, body = doc.body, started, startRng;
bgneal@183 3543
bgneal@183 3544 // Make HTML element unselectable since we are going to handle selection by hand
bgneal@183 3545 doc.documentElement.unselectable = TRUE;
bgneal@183 3546
bgneal@183 3547 // Return range from point or null if it failed
bgneal@183 3548 function rngFromPoint(x, y) {
bgneal@183 3549 var rng = body.createTextRange();
bgneal@183 3550
bgneal@183 3551 try {
bgneal@183 3552 rng.moveToPoint(x, y);
bgneal@183 3553 } catch (ex) {
bgneal@183 3554 // IE sometimes throws and exception, so lets just ignore it
bgneal@183 3555 rng = null;
bgneal@183 3556 }
bgneal@183 3557
bgneal@183 3558 return rng;
bgneal@183 3559 };
bgneal@183 3560
bgneal@183 3561 // Fires while the selection is changing
bgneal@183 3562 function selectionChange(e) {
bgneal@183 3563 var pointRng;
bgneal@183 3564
bgneal@183 3565 // Check if the button is down or not
bgneal@183 3566 if (e.button) {
bgneal@183 3567 // Create range from mouse position
bgneal@183 3568 pointRng = rngFromPoint(e.x, e.y);
bgneal@183 3569
bgneal@183 3570 if (pointRng) {
bgneal@183 3571 // Check if pointRange is before/after selection then change the endPoint
bgneal@183 3572 if (pointRng.compareEndPoints('StartToStart', startRng) > 0)
bgneal@183 3573 pointRng.setEndPoint('StartToStart', startRng);
bgneal@183 3574 else
bgneal@183 3575 pointRng.setEndPoint('EndToEnd', startRng);
bgneal@183 3576
bgneal@183 3577 pointRng.select();
bgneal@183 3578 }
bgneal@45 3579 } else
bgneal@183 3580 endSelection();
bgneal@183 3581 }
bgneal@183 3582
bgneal@183 3583 // Removes listeners
bgneal@183 3584 function endSelection() {
bgneal@183 3585 dom.unbind(doc, 'mouseup', endSelection);
bgneal@183 3586 dom.unbind(doc, 'mousemove', selectionChange);
bgneal@183 3587 started = 0;
bgneal@183 3588 };
bgneal@183 3589
bgneal@183 3590 // Detect when user selects outside BODY
bgneal@183 3591 dom.bind(doc, 'mousedown', function(e) {
bgneal@183 3592 if (e.target.nodeName === 'HTML') {
bgneal@183 3593 if (started)
bgneal@183 3594 endSelection();
bgneal@183 3595
bgneal@183 3596 started = 1;
bgneal@183 3597
bgneal@183 3598 // Setup start position
bgneal@183 3599 startRng = rngFromPoint(e.x, e.y);
bgneal@183 3600 if (startRng) {
bgneal@183 3601 // Listen for selection change events
bgneal@183 3602 dom.bind(doc, 'mouseup', endSelection);
bgneal@183 3603 dom.bind(doc, 'mousemove', selectionChange);
bgneal@183 3604
bgneal@183 3605 startRng.select();
bgneal@183 3606 }
bgneal@183 3607 }
bgneal@183 3608 });
bgneal@183 3609 })();
bgneal@183 3610 }
bgneal@45 3611 };
bgneal@45 3612
bgneal@45 3613 // Expose the selection object
bgneal@45 3614 tinymce.dom.TridentSelection = Selection;
bgneal@45 3615 })();
bgneal@45 3616
bgneal@183 3617
bgneal@45 3618 /*
bgneal@45 3619 * Sizzle CSS Selector Engine - v1.0
bgneal@45 3620 * Copyright 2009, The Dojo Foundation
bgneal@45 3621 * Released under the MIT, BSD, and GPL Licenses.
bgneal@45 3622 * More information: http://sizzlejs.com/
bgneal@45 3623 */
bgneal@45 3624 (function(){
bgneal@45 3625
bgneal@45 3626 var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]*\]|['"][^'"]*['"]|[^[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?/g,
bgneal@45 3627 done = 0,
bgneal@45 3628 toString = Object.prototype.toString,
bgneal@183 3629 hasDuplicate = false;
bgneal@45 3630
bgneal@45 3631 var Sizzle = function(selector, context, results, seed) {
bgneal@45 3632 results = results || [];
bgneal@45 3633 var origContext = context = context || document;
bgneal@45 3634
bgneal@45 3635 if ( context.nodeType !== 1 && context.nodeType !== 9 ) {
bgneal@45 3636 return [];
bgneal@45 3637 }
bgneal@45 3638
bgneal@45 3639 if ( !selector || typeof selector !== "string" ) {
bgneal@45 3640 return results;
bgneal@45 3641 }
bgneal@45 3642
bgneal@45 3643 var parts = [], m, set, checkSet, check, mode, extra, prune = true, contextXML = isXML(context);
bgneal@45 3644
bgneal@45 3645 // Reset the position of the chunker regexp (start from head)
bgneal@45 3646 chunker.lastIndex = 0;
bgneal@45 3647
bgneal@45 3648 while ( (m = chunker.exec(selector)) !== null ) {
bgneal@45 3649 parts.push( m[1] );
bgneal@45 3650
bgneal@45 3651 if ( m[2] ) {
bgneal@45 3652 extra = RegExp.rightContext;
bgneal@45 3653 break;
bgneal@45 3654 }
bgneal@45 3655 }
bgneal@45 3656
bgneal@45 3657 if ( parts.length > 1 && origPOS.exec( selector ) ) {
bgneal@45 3658 if ( parts.length === 2 && Expr.relative[ parts[0] ] ) {
bgneal@45 3659 set = posProcess( parts[0] + parts[1], context );
bgneal@45 3660 } else {
bgneal@45 3661 set = Expr.relative[ parts[0] ] ?
bgneal@45 3662 [ context ] :
bgneal@45 3663 Sizzle( parts.shift(), context );
bgneal@45 3664
bgneal@45 3665 while ( parts.length ) {
bgneal@45 3666 selector = parts.shift();
bgneal@45 3667
bgneal@45 3668 if ( Expr.relative[ selector ] )
bgneal@45 3669 selector += parts.shift();
bgneal@45 3670
bgneal@45 3671 set = posProcess( selector, set );
bgneal@45 3672 }
bgneal@45 3673 }
bgneal@45 3674 } else {
bgneal@45 3675 // Take a shortcut and set the context if the root selector is an ID
bgneal@45 3676 // (but not if it'll be faster if the inner selector is an ID)
bgneal@45 3677 if ( !seed && parts.length > 1 && context.nodeType === 9 && !contextXML &&
bgneal@45 3678 Expr.match.ID.test(parts[0]) && !Expr.match.ID.test(parts[parts.length - 1]) ) {
bgneal@45 3679 var ret = Sizzle.find( parts.shift(), context, contextXML );
bgneal@45 3680 context = ret.expr ? Sizzle.filter( ret.expr, ret.set )[0] : ret.set[0];
bgneal@45 3681 }
bgneal@45 3682
bgneal@45 3683 if ( context ) {
bgneal@45 3684 var ret = seed ?
bgneal@45 3685 { expr: parts.pop(), set: makeArray(seed) } :
bgneal@45 3686 Sizzle.find( parts.pop(), parts.length === 1 && (parts[0] === "~" || parts[0] === "+") && context.parentNode ? context.parentNode : context, contextXML );
bgneal@45 3687 set = ret.expr ? Sizzle.filter( ret.expr, ret.set ) : ret.set;
bgneal@45 3688
bgneal@45 3689 if ( parts.length > 0 ) {
bgneal@45 3690 checkSet = makeArray(set);
bgneal@45 3691 } else {
bgneal@45 3692 prune = false;
bgneal@45 3693 }
bgneal@45 3694
bgneal@45 3695 while ( parts.length ) {
bgneal@45 3696 var cur = parts.pop(), pop = cur;
bgneal@45 3697
bgneal@45 3698 if ( !Expr.relative[ cur ] ) {
bgneal@45 3699 cur = "";
bgneal@45 3700 } else {
bgneal@45 3701 pop = parts.pop();
bgneal@45 3702 }
bgneal@45 3703
bgneal@45 3704 if ( pop == null ) {
bgneal@45 3705 pop = context;
bgneal@45 3706 }
bgneal@45 3707
bgneal@45 3708 Expr.relative[ cur ]( checkSet, pop, contextXML );
bgneal@45 3709 }
bgneal@45 3710 } else {
bgneal@45 3711 checkSet = parts = [];
bgneal@45 3712 }
bgneal@45 3713 }
bgneal@45 3714
bgneal@45 3715 if ( !checkSet ) {
bgneal@45 3716 checkSet = set;
bgneal@45 3717 }
bgneal@45 3718
bgneal@45 3719 if ( !checkSet ) {
bgneal@45 3720 throw "Syntax error, unrecognized expression: " + (cur || selector);
bgneal@45 3721 }
bgneal@45 3722
bgneal@45 3723 if ( toString.call(checkSet) === "[object Array]" ) {
bgneal@45 3724 if ( !prune ) {
bgneal@183 3725 results.push.apply( results, checkSet );
bgneal@45 3726 } else if ( context && context.nodeType === 1 ) {
bgneal@45 3727 for ( var i = 0; checkSet[i] != null; i++ ) {
bgneal@45 3728 if ( checkSet[i] && (checkSet[i] === true || checkSet[i].nodeType === 1 && contains(context, checkSet[i])) ) {
bgneal@183 3729 results.push( set[i] );
bgneal@45 3730 }
bgneal@45 3731 }
bgneal@45 3732 } else {
bgneal@45 3733 for ( var i = 0; checkSet[i] != null; i++ ) {
bgneal@45 3734 if ( checkSet[i] && checkSet[i].nodeType === 1 ) {
bgneal@183 3735 results.push( set[i] );
bgneal@45 3736 }
bgneal@45 3737 }
bgneal@45 3738 }
bgneal@45 3739 } else {
bgneal@45 3740 makeArray( checkSet, results );
bgneal@45 3741 }
bgneal@45 3742
bgneal@45 3743 if ( extra ) {
bgneal@45 3744 Sizzle( extra, origContext, results, seed );
bgneal@45 3745 Sizzle.uniqueSort( results );
bgneal@45 3746 }
bgneal@45 3747
bgneal@45 3748 return results;
bgneal@45 3749 };
bgneal@45 3750
bgneal@45 3751 Sizzle.uniqueSort = function(results){
bgneal@45 3752 if ( sortOrder ) {
bgneal@45 3753 hasDuplicate = false;
bgneal@183 3754 results.sort(sortOrder);
bgneal@45 3755
bgneal@45 3756 if ( hasDuplicate ) {
bgneal@45 3757 for ( var i = 1; i < results.length; i++ ) {
bgneal@45 3758 if ( results[i] === results[i-1] ) {
bgneal@183 3759 results.splice(i--, 1);
bgneal@45 3760 }
bgneal@45 3761 }
bgneal@45 3762 }
bgneal@45 3763 }
bgneal@45 3764 };
bgneal@45 3765
bgneal@45 3766 Sizzle.matches = function(expr, set){
bgneal@45 3767 return Sizzle(expr, null, null, set);
bgneal@45 3768 };
bgneal@45 3769
bgneal@45 3770 Sizzle.find = function(expr, context, isXML){
bgneal@45 3771 var set, match;
bgneal@45 3772
bgneal@45 3773 if ( !expr ) {
bgneal@45 3774 return [];
bgneal@45 3775 }
bgneal@45 3776
bgneal@45 3777 for ( var i = 0, l = Expr.order.length; i < l; i++ ) {
bgneal@45 3778 var type = Expr.order[i], match;
bgneal@45 3779
bgneal@45 3780 if ( (match = Expr.match[ type ].exec( expr )) ) {
bgneal@45 3781 var left = RegExp.leftContext;
bgneal@45 3782
bgneal@45 3783 if ( left.substr( left.length - 1 ) !== "\\" ) {
bgneal@45 3784 match[1] = (match[1] || "").replace(/\\/g, "");
bgneal@45 3785 set = Expr.find[ type ]( match, context, isXML );
bgneal@45 3786 if ( set != null ) {
bgneal@45 3787 expr = expr.replace( Expr.match[ type ], "" );
bgneal@45 3788 break;
bgneal@45 3789 }
bgneal@45 3790 }
bgneal@45 3791 }
bgneal@45 3792 }
bgneal@45 3793
bgneal@45 3794 if ( !set ) {
bgneal@45 3795 set = context.getElementsByTagName("*");
bgneal@45 3796 }
bgneal@45 3797
bgneal@45 3798 return {set: set, expr: expr};
bgneal@45 3799 };
bgneal@45 3800
bgneal@45 3801 Sizzle.filter = function(expr, set, inplace, not){
bgneal@45 3802 var old = expr, result = [], curLoop = set, match, anyFound,
bgneal@45 3803 isXMLFilter = set && set[0] && isXML(set[0]);
bgneal@45 3804
bgneal@45 3805 while ( expr && set.length ) {
bgneal@45 3806 for ( var type in Expr.filter ) {
bgneal@45 3807 if ( (match = Expr.match[ type ].exec( expr )) != null ) {
bgneal@45 3808 var filter = Expr.filter[ type ], found, item;
bgneal@45 3809 anyFound = false;
bgneal@45 3810
bgneal@45 3811 if ( curLoop == result ) {
bgneal@45 3812 result = [];
bgneal@45 3813 }
bgneal@45 3814
bgneal@45 3815 if ( Expr.preFilter[ type ] ) {
bgneal@45 3816 match = Expr.preFilter[ type ]( match, curLoop, inplace, result, not, isXMLFilter );
bgneal@45 3817
bgneal@45 3818 if ( !match ) {
bgneal@45 3819 anyFound = found = true;
bgneal@45 3820 } else if ( match === true ) {
bgneal@45 3821 continue;
bgneal@45 3822 }
bgneal@45 3823 }
bgneal@45 3824
bgneal@45 3825 if ( match ) {
bgneal@45 3826 for ( var i = 0; (item = curLoop[i]) != null; i++ ) {
bgneal@45 3827 if ( item ) {
bgneal@45 3828 found = filter( item, match, i, curLoop );
bgneal@45 3829 var pass = not ^ !!found;
bgneal@45 3830
bgneal@45 3831 if ( inplace && found != null ) {
bgneal@45 3832 if ( pass ) {
bgneal@45 3833 anyFound = true;
bgneal@45 3834 } else {
bgneal@45 3835 curLoop[i] = false;
bgneal@45 3836 }
bgneal@45 3837 } else if ( pass ) {
bgneal@45 3838 result.push( item );
bgneal@45 3839 anyFound = true;
bgneal@45 3840 }
bgneal@45 3841 }
bgneal@45 3842 }
bgneal@45 3843 }
bgneal@45 3844
bgneal@45 3845 if ( found !== undefined ) {
bgneal@45 3846 if ( !inplace ) {
bgneal@45 3847 curLoop = result;
bgneal@45 3848 }
bgneal@45 3849
bgneal@45 3850 expr = expr.replace( Expr.match[ type ], "" );
bgneal@45 3851
bgneal@45 3852 if ( !anyFound ) {
bgneal@45 3853 return [];
bgneal@45 3854 }
bgneal@45 3855
bgneal@45 3856 break;
bgneal@45 3857 }
bgneal@45 3858 }
bgneal@45 3859 }
bgneal@45 3860
bgneal@45 3861 // Improper expression
bgneal@45 3862 if ( expr == old ) {
bgneal@45 3863 if ( anyFound == null ) {
bgneal@45 3864 throw "Syntax error, unrecognized expression: " + expr;
bgneal@45 3865 } else {
bgneal@45 3866 break;
bgneal@45 3867 }
bgneal@45 3868 }
bgneal@45 3869
bgneal@45 3870 old = expr;
bgneal@45 3871 }
bgneal@45 3872
bgneal@45 3873 return curLoop;
bgneal@45 3874 };
bgneal@45 3875
bgneal@45 3876 var Expr = Sizzle.selectors = {
bgneal@45 3877 order: [ "ID", "NAME", "TAG" ],
bgneal@45 3878 match: {
bgneal@45 3879 ID: /#((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,
bgneal@45 3880 CLASS: /\.((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,
bgneal@45 3881 NAME: /\[name=['"]*((?:[\w\u00c0-\uFFFF_-]|\\.)+)['"]*\]/,
bgneal@45 3882 ATTR: /\[\s*((?:[\w\u00c0-\uFFFF_-]|\\.)+)\s*(?:(\S?=)\s*(['"]*)(.*?)\3|)\s*\]/,
bgneal@45 3883 TAG: /^((?:[\w\u00c0-\uFFFF\*_-]|\\.)+)/,
bgneal@45 3884 CHILD: /:(only|nth|last|first)-child(?:\((even|odd|[\dn+-]*)\))?/,
bgneal@45 3885 POS: /:(nth|eq|gt|lt|first|last|even|odd)(?:\((\d*)\))?(?=[^-]|$)/,
bgneal@45 3886 PSEUDO: /:((?:[\w\u00c0-\uFFFF_-]|\\.)+)(?:\((['"]*)((?:\([^\)]+\)|[^\2\(\)]*)+)\2\))?/
bgneal@45 3887 },
bgneal@45 3888 attrMap: {
bgneal@45 3889 "class": "className",
bgneal@45 3890 "for": "htmlFor"
bgneal@45 3891 },
bgneal@45 3892 attrHandle: {
bgneal@45 3893 href: function(elem){
bgneal@45 3894 return elem.getAttribute("href");
bgneal@45 3895 }
bgneal@45 3896 },
bgneal@45 3897 relative: {
bgneal@45 3898 "+": function(checkSet, part, isXML){
bgneal@45 3899 var isPartStr = typeof part === "string",
bgneal@45 3900 isTag = isPartStr && !/\W/.test(part),
bgneal@45 3901 isPartStrNotTag = isPartStr && !isTag;
bgneal@45 3902
bgneal@45 3903 if ( isTag && !isXML ) {
bgneal@45 3904 part = part.toUpperCase();
bgneal@45 3905 }
bgneal@45 3906
bgneal@45 3907 for ( var i = 0, l = checkSet.length, elem; i < l; i++ ) {
bgneal@45 3908 if ( (elem = checkSet[i]) ) {
bgneal@45 3909 while ( (elem = elem.previousSibling) && elem.nodeType !== 1 ) {}
bgneal@45 3910
bgneal@45 3911 checkSet[i] = isPartStrNotTag || elem && elem.nodeName === part ?
bgneal@45 3912 elem || false :
bgneal@45 3913 elem === part;
bgneal@45 3914 }
bgneal@45 3915 }
bgneal@45 3916
bgneal@45 3917 if ( isPartStrNotTag ) {
bgneal@45 3918 Sizzle.filter( part, checkSet, true );
bgneal@45 3919 }
bgneal@45 3920 },
bgneal@45 3921 ">": function(checkSet, part, isXML){
bgneal@45 3922 var isPartStr = typeof part === "string";
bgneal@45 3923
bgneal@45 3924 if ( isPartStr && !/\W/.test(part) ) {
bgneal@45 3925 part = isXML ? part : part.toUpperCase();
bgneal@45 3926
bgneal@45 3927 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
bgneal@45 3928 var elem = checkSet[i];
bgneal@45 3929 if ( elem ) {
bgneal@45 3930 var parent = elem.parentNode;
bgneal@45 3931 checkSet[i] = parent.nodeName === part ? parent : false;
bgneal@45 3932 }
bgneal@45 3933 }
bgneal@45 3934 } else {
bgneal@45 3935 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
bgneal@45 3936 var elem = checkSet[i];
bgneal@45 3937 if ( elem ) {
bgneal@45 3938 checkSet[i] = isPartStr ?
bgneal@45 3939 elem.parentNode :
bgneal@45 3940 elem.parentNode === part;
bgneal@45 3941 }
bgneal@45 3942 }
bgneal@45 3943
bgneal@45 3944 if ( isPartStr ) {
bgneal@45 3945 Sizzle.filter( part, checkSet, true );
bgneal@45 3946 }
bgneal@45 3947 }
bgneal@45 3948 },
bgneal@45 3949 "": function(checkSet, part, isXML){
bgneal@45 3950 var doneName = done++, checkFn = dirCheck;
bgneal@45 3951
bgneal@45 3952 if ( !part.match(/\W/) ) {
bgneal@45 3953 var nodeCheck = part = isXML ? part : part.toUpperCase();
bgneal@45 3954 checkFn = dirNodeCheck;
bgneal@45 3955 }
bgneal@45 3956
bgneal@45 3957 checkFn("parentNode", part, doneName, checkSet, nodeCheck, isXML);
bgneal@45 3958 },
bgneal@45 3959 "~": function(checkSet, part, isXML){
bgneal@45 3960 var doneName = done++, checkFn = dirCheck;
bgneal@45 3961
bgneal@45 3962 if ( typeof part === "string" && !part.match(/\W/) ) {
bgneal@45 3963 var nodeCheck = part = isXML ? part : part.toUpperCase();
bgneal@45 3964 checkFn = dirNodeCheck;
bgneal@45 3965 }
bgneal@45 3966
bgneal@45 3967 checkFn("previousSibling", part, doneName, checkSet, nodeCheck, isXML);
bgneal@45 3968 }
bgneal@45 3969 },
bgneal@45 3970 find: {
bgneal@45 3971 ID: function(match, context, isXML){
bgneal@45 3972 if ( typeof context.getElementById !== "undefined" && !isXML ) {
bgneal@45 3973 var m = context.getElementById(match[1]);
bgneal@45 3974 return m ? [m] : [];
bgneal@45 3975 }
bgneal@45 3976 },
bgneal@45 3977 NAME: function(match, context, isXML){
bgneal@45 3978 if ( typeof context.getElementsByName !== "undefined" ) {
bgneal@45 3979 var ret = [], results = context.getElementsByName(match[1]);
bgneal@45 3980
bgneal@45 3981 for ( var i = 0, l = results.length; i < l; i++ ) {
bgneal@45 3982 if ( results[i].getAttribute("name") === match[1] ) {
bgneal@45 3983 ret.push( results[i] );
bgneal@45 3984 }
bgneal@45 3985 }
bgneal@45 3986
bgneal@45 3987 return ret.length === 0 ? null : ret;
bgneal@45 3988 }
bgneal@45 3989 },
bgneal@45 3990 TAG: function(match, context){
bgneal@45 3991 return context.getElementsByTagName(match[1]);
bgneal@45 3992 }
bgneal@45 3993 },
bgneal@45 3994 preFilter: {
bgneal@45 3995 CLASS: function(match, curLoop, inplace, result, not, isXML){
bgneal@45 3996 match = " " + match[1].replace(/\\/g, "") + " ";
bgneal@45 3997
bgneal@45 3998 if ( isXML ) {
bgneal@45 3999 return match;
bgneal@45 4000 }
bgneal@45 4001
bgneal@45 4002 for ( var i = 0, elem; (elem = curLoop[i]) != null; i++ ) {
bgneal@45 4003 if ( elem ) {
bgneal@45 4004 if ( not ^ (elem.className && (" " + elem.className + " ").indexOf(match) >= 0) ) {
bgneal@45 4005 if ( !inplace )
bgneal@45 4006 result.push( elem );
bgneal@45 4007 } else if ( inplace ) {
bgneal@45 4008 curLoop[i] = false;
bgneal@45 4009 }
bgneal@45 4010 }
bgneal@45 4011 }
bgneal@45 4012
bgneal@45 4013 return false;
bgneal@45 4014 },
bgneal@45 4015 ID: function(match){
bgneal@45 4016 return match[1].replace(/\\/g, "");
bgneal@45 4017 },
bgneal@45 4018 TAG: function(match, curLoop){
bgneal@45 4019 for ( var i = 0; curLoop[i] === false; i++ ){}
bgneal@45 4020 return curLoop[i] && isXML(curLoop[i]) ? match[1] : match[1].toUpperCase();
bgneal@45 4021 },
bgneal@45 4022 CHILD: function(match){
bgneal@45 4023 if ( match[1] == "nth" ) {
bgneal@45 4024 // parse equations like 'even', 'odd', '5', '2n', '3n+2', '4n-1', '-n+6'
bgneal@45 4025 var test = /(-?)(\d*)n((?:\+|-)?\d*)/.exec(
bgneal@45 4026 match[2] == "even" && "2n" || match[2] == "odd" && "2n+1" ||
bgneal@45 4027 !/\D/.test( match[2] ) && "0n+" + match[2] || match[2]);
bgneal@45 4028
bgneal@45 4029 // calculate the numbers (first)n+(last) including if they are negative
bgneal@45 4030 match[2] = (test[1] + (test[2] || 1)) - 0;
bgneal@45 4031 match[3] = test[3] - 0;
bgneal@45 4032 }
bgneal@45 4033
bgneal@45 4034 // TODO: Move to normal caching system
bgneal@45 4035 match[0] = done++;
bgneal@45 4036
bgneal@45 4037 return match;
bgneal@45 4038 },
bgneal@45 4039 ATTR: function(match, curLoop, inplace, result, not, isXML){
bgneal@45 4040 var name = match[1].replace(/\\/g, "");
bgneal@45 4041
bgneal@45 4042 if ( !isXML && Expr.attrMap[name] ) {
bgneal@45 4043 match[1] = Expr.attrMap[name];
bgneal@45 4044 }
bgneal@45 4045
bgneal@45 4046 if ( match[2] === "~=" ) {
bgneal@45 4047 match[4] = " " + match[4] + " ";
bgneal@45 4048 }
bgneal@45 4049
bgneal@45 4050 return match;
bgneal@45 4051 },
bgneal@45 4052 PSEUDO: function(match, curLoop, inplace, result, not){
bgneal@45 4053 if ( match[1] === "not" ) {
bgneal@45 4054 // If we're dealing with a complex expression, or a simple one
bgneal@45 4055 if ( match[3].match(chunker).length > 1 || /^\w/.test(match[3]) ) {
bgneal@45 4056 match[3] = Sizzle(match[3], null, null, curLoop);
bgneal@45 4057 } else {
bgneal@45 4058 var ret = Sizzle.filter(match[3], curLoop, inplace, true ^ not);
bgneal@45 4059 if ( !inplace ) {
bgneal@45 4060 result.push.apply( result, ret );
bgneal@45 4061 }
bgneal@45 4062 return false;
bgneal@45 4063 }
bgneal@45 4064 } else if ( Expr.match.POS.test( match[0] ) || Expr.match.CHILD.test( match[0] ) ) {
bgneal@45 4065 return true;
bgneal@45 4066 }
bgneal@45 4067
bgneal@45 4068 return match;
bgneal@45 4069 },
bgneal@45 4070 POS: function(match){
bgneal@45 4071 match.unshift( true );
bgneal@45 4072 return match;
bgneal@45 4073 }
bgneal@45 4074 },
bgneal@45 4075 filters: {
bgneal@45 4076 enabled: function(elem){
bgneal@45 4077 return elem.disabled === false && elem.type !== "hidden";
bgneal@45 4078 },
bgneal@45 4079 disabled: function(elem){
bgneal@45 4080 return elem.disabled === true;
bgneal@45 4081 },
bgneal@45 4082 checked: function(elem){
bgneal@45 4083 return elem.checked === true;
bgneal@45 4084 },
bgneal@45 4085 selected: function(elem){
bgneal@45 4086 // Accessing this property makes selected-by-default
bgneal@45 4087 // options in Safari work properly
bgneal@45 4088 elem.parentNode.selectedIndex;
bgneal@45 4089 return elem.selected === true;
bgneal@45 4090 },
bgneal@45 4091 parent: function(elem){
bgneal@45 4092 return !!elem.firstChild;
bgneal@45 4093 },
bgneal@45 4094 empty: function(elem){
bgneal@45 4095 return !elem.firstChild;
bgneal@45 4096 },
bgneal@45 4097 has: function(elem, i, match){
bgneal@45 4098 return !!Sizzle( match[3], elem ).length;
bgneal@45 4099 },
bgneal@45 4100 header: function(elem){
bgneal@45 4101 return /h\d/i.test( elem.nodeName );
bgneal@45 4102 },
bgneal@45 4103 text: function(elem){
bgneal@45 4104 return "text" === elem.type;
bgneal@45 4105 },
bgneal@45 4106 radio: function(elem){
bgneal@45 4107 return "radio" === elem.type;
bgneal@45 4108 },
bgneal@45 4109 checkbox: function(elem){
bgneal@45 4110 return "checkbox" === elem.type;
bgneal@45 4111 },
bgneal@45 4112 file: function(elem){
bgneal@45 4113 return "file" === elem.type;
bgneal@45 4114 },
bgneal@45 4115 password: function(elem){
bgneal@45 4116 return "password" === elem.type;
bgneal@45 4117 },
bgneal@45 4118 submit: function(elem){
bgneal@45 4119 return "submit" === elem.type;
bgneal@45 4120 },
bgneal@45 4121 image: function(elem){
bgneal@45 4122 return "image" === elem.type;
bgneal@45 4123 },
bgneal@45 4124 reset: function(elem){
bgneal@45 4125 return "reset" === elem.type;
bgneal@45 4126 },
bgneal@45 4127 button: function(elem){
bgneal@45 4128 return "button" === elem.type || elem.nodeName.toUpperCase() === "BUTTON";
bgneal@45 4129 },
bgneal@45 4130 input: function(elem){
bgneal@45 4131 return /input|select|textarea|button/i.test(elem.nodeName);
bgneal@45 4132 }
bgneal@45 4133 },
bgneal@45 4134 setFilters: {
bgneal@45 4135 first: function(elem, i){
bgneal@45 4136 return i === 0;
bgneal@45 4137 },
bgneal@45 4138 last: function(elem, i, match, array){
bgneal@45 4139 return i === array.length - 1;
bgneal@45 4140 },
bgneal@45 4141 even: function(elem, i){
bgneal@45 4142 return i % 2 === 0;
bgneal@45 4143 },
bgneal@45 4144 odd: function(elem, i){
bgneal@45 4145 return i % 2 === 1;
bgneal@45 4146 },
bgneal@45 4147 lt: function(elem, i, match){
bgneal@45 4148 return i < match[3] - 0;
bgneal@45 4149 },
bgneal@45 4150 gt: function(elem, i, match){
bgneal@45 4151 return i > match[3] - 0;
bgneal@45 4152 },
bgneal@45 4153 nth: function(elem, i, match){
bgneal@45 4154 return match[3] - 0 == i;
bgneal@45 4155 },
bgneal@45 4156 eq: function(elem, i, match){
bgneal@45 4157 return match[3] - 0 == i;
bgneal@45 4158 }
bgneal@45 4159 },
bgneal@45 4160 filter: {
bgneal@45 4161 PSEUDO: function(elem, match, i, array){
bgneal@45 4162 var name = match[1], filter = Expr.filters[ name ];
bgneal@45 4163
bgneal@45 4164 if ( filter ) {
bgneal@45 4165 return filter( elem, i, match, array );
bgneal@45 4166 } else if ( name === "contains" ) {
bgneal@45 4167 return (elem.textContent || elem.innerText || "").indexOf(match[3]) >= 0;
bgneal@45 4168 } else if ( name === "not" ) {
bgneal@45 4169 var not = match[3];
bgneal@45 4170
bgneal@45 4171 for ( var i = 0, l = not.length; i < l; i++ ) {
bgneal@45 4172 if ( not[i] === elem ) {
bgneal@45 4173 return false;
bgneal@45 4174 }
bgneal@45 4175 }
bgneal@45 4176
bgneal@45 4177 return true;
bgneal@45 4178 }
bgneal@45 4179 },
bgneal@45 4180 CHILD: function(elem, match){
bgneal@45 4181 var type = match[1], node = elem;
bgneal@45 4182 switch (type) {
bgneal@45 4183 case 'only':
bgneal@45 4184 case 'first':
bgneal@45 4185 while (node = node.previousSibling) {
bgneal@45 4186 if ( node.nodeType === 1 ) return false;
bgneal@45 4187 }
bgneal@45 4188 if ( type == 'first') return true;
bgneal@45 4189 node = elem;
bgneal@45 4190 case 'last':
bgneal@45 4191 while (node = node.nextSibling) {
bgneal@45 4192 if ( node.nodeType === 1 ) return false;
bgneal@45 4193 }
bgneal@45 4194 return true;
bgneal@45 4195 case 'nth':
bgneal@45 4196 var first = match[2], last = match[3];
bgneal@45 4197
bgneal@45 4198 if ( first == 1 && last == 0 ) {
bgneal@45 4199 return true;
bgneal@45 4200 }
bgneal@45 4201
bgneal@45 4202 var doneName = match[0],
bgneal@45 4203 parent = elem.parentNode;
bgneal@45 4204
bgneal@45 4205 if ( parent && (parent.sizcache !== doneName || !elem.nodeIndex) ) {
bgneal@45 4206 var count = 0;
bgneal@45 4207 for ( node = parent.firstChild; node; node = node.nextSibling ) {
bgneal@45 4208 if ( node.nodeType === 1 ) {
bgneal@45 4209 node.nodeIndex = ++count;
bgneal@45 4210 }
bgneal@45 4211 }
bgneal@45 4212 parent.sizcache = doneName;
bgneal@45 4213 }
bgneal@45 4214
bgneal@45 4215 var diff = elem.nodeIndex - last;
bgneal@45 4216 if ( first == 0 ) {
bgneal@45 4217 return diff == 0;
bgneal@45 4218 } else {
bgneal@45 4219 return ( diff % first == 0 && diff / first >= 0 );
bgneal@45 4220 }
bgneal@45 4221 }
bgneal@45 4222 },
bgneal@45 4223 ID: function(elem, match){
bgneal@45 4224 return elem.nodeType === 1 && elem.getAttribute("id") === match;
bgneal@45 4225 },
bgneal@45 4226 TAG: function(elem, match){
bgneal@45 4227 return (match === "*" && elem.nodeType === 1) || elem.nodeName === match;
bgneal@45 4228 },
bgneal@45 4229 CLASS: function(elem, match){
bgneal@45 4230 return (" " + (elem.className || elem.getAttribute("class")) + " ")
bgneal@45 4231 .indexOf( match ) > -1;
bgneal@45 4232 },
bgneal@45 4233 ATTR: function(elem, match){
bgneal@45 4234 var name = match[1],
bgneal@45 4235 result = Expr.attrHandle[ name ] ?
bgneal@45 4236 Expr.attrHandle[ name ]( elem ) :
bgneal@45 4237 elem[ name ] != null ?
bgneal@45 4238 elem[ name ] :
bgneal@45 4239 elem.getAttribute( name ),
bgneal@45 4240 value = result + "",
bgneal@45 4241 type = match[2],
bgneal@45 4242 check = match[4];
bgneal@45 4243
bgneal@45 4244 return result == null ?
bgneal@45 4245 type === "!=" :
bgneal@45 4246 type === "=" ?
bgneal@45 4247 value === check :
bgneal@45 4248 type === "*=" ?
bgneal@45 4249 value.indexOf(check) >= 0 :
bgneal@45 4250 type === "~=" ?
bgneal@45 4251 (" " + value + " ").indexOf(check) >= 0 :
bgneal@45 4252 !check ?
bgneal@45 4253 value && result !== false :
bgneal@45 4254 type === "!=" ?
bgneal@45 4255 value != check :
bgneal@45 4256 type === "^=" ?
bgneal@45 4257 value.indexOf(check) === 0 :
bgneal@45 4258 type === "$=" ?
bgneal@45 4259 value.substr(value.length - check.length) === check :
bgneal@45 4260 type === "|=" ?
bgneal@45 4261 value === check || value.substr(0, check.length + 1) === check + "-" :
bgneal@45 4262 false;
bgneal@45 4263 },
bgneal@45 4264 POS: function(elem, match, i, array){
bgneal@45 4265 var name = match[2], filter = Expr.setFilters[ name ];
bgneal@45 4266
bgneal@45 4267 if ( filter ) {
bgneal@45 4268 return filter( elem, i, match, array );
bgneal@45 4269 }
bgneal@45 4270 }
bgneal@45 4271 }
bgneal@45 4272 };
bgneal@45 4273
bgneal@45 4274 var origPOS = Expr.match.POS;
bgneal@45 4275
bgneal@45 4276 for ( var type in Expr.match ) {
bgneal@45 4277 Expr.match[ type ] = new RegExp( Expr.match[ type ].source + /(?![^\[]*\])(?![^\(]*\))/.source );
bgneal@45 4278 }
bgneal@45 4279
bgneal@45 4280 var makeArray = function(array, results) {
bgneal@45 4281 array = Array.prototype.slice.call( array );
bgneal@45 4282
bgneal@45 4283 if ( results ) {
bgneal@183 4284 results.push.apply( results, array );
bgneal@45 4285 return results;
bgneal@45 4286 }
bgneal@45 4287
bgneal@45 4288 return array;
bgneal@45 4289 };
bgneal@45 4290
bgneal@45 4291 // Perform a simple check to determine if the browser is capable of
bgneal@45 4292 // converting a NodeList to an array using builtin methods.
bgneal@45 4293 try {
bgneal@45 4294 Array.prototype.slice.call( document.documentElement.childNodes );
bgneal@45 4295
bgneal@45 4296 // Provide a fallback method if it does not work
bgneal@45 4297 } catch(e){
bgneal@45 4298 makeArray = function(array, results) {
bgneal@45 4299 var ret = results || [];
bgneal@45 4300
bgneal@45 4301 if ( toString.call(array) === "[object Array]" ) {
bgneal@45 4302 Array.prototype.push.apply( ret, array );
bgneal@45 4303 } else {
bgneal@45 4304 if ( typeof array.length === "number" ) {
bgneal@45 4305 for ( var i = 0, l = array.length; i < l; i++ ) {
bgneal@45 4306 ret.push( array[i] );
bgneal@45 4307 }
bgneal@45 4308 } else {
bgneal@45 4309 for ( var i = 0; array[i]; i++ ) {
bgneal@45 4310 ret.push( array[i] );
bgneal@45 4311 }
bgneal@45 4312 }
bgneal@45 4313 }
bgneal@45 4314
bgneal@45 4315 return ret;
bgneal@45 4316 };
bgneal@45 4317 }
bgneal@45 4318
bgneal@45 4319 var sortOrder;
bgneal@45 4320
bgneal@45 4321 if ( document.documentElement.compareDocumentPosition ) {
bgneal@45 4322 sortOrder = function( a, b ) {
bgneal@45 4323 var ret = a.compareDocumentPosition(b) & 4 ? -1 : a === b ? 0 : 1;
bgneal@45 4324 if ( ret === 0 ) {
bgneal@45 4325 hasDuplicate = true;
bgneal@45 4326 }
bgneal@45 4327 return ret;
bgneal@45 4328 };
bgneal@45 4329 } else if ( "sourceIndex" in document.documentElement ) {
bgneal@45 4330 sortOrder = function( a, b ) {
bgneal@45 4331 var ret = a.sourceIndex - b.sourceIndex;
bgneal@45 4332 if ( ret === 0 ) {
bgneal@45 4333 hasDuplicate = true;
bgneal@45 4334 }
bgneal@45 4335 return ret;
bgneal@45 4336 };
bgneal@45 4337 } else if ( document.createRange ) {
bgneal@45 4338 sortOrder = function( a, b ) {
bgneal@45 4339 var aRange = a.ownerDocument.createRange(), bRange = b.ownerDocument.createRange();
bgneal@183 4340 aRange.setStart(a, 0);
bgneal@183 4341 aRange.setEnd(a, 0);
bgneal@183 4342 bRange.setStart(b, 0);
bgneal@183 4343 bRange.setEnd(b, 0);
bgneal@45 4344 var ret = aRange.compareBoundaryPoints(Range.START_TO_END, bRange);
bgneal@45 4345 if ( ret === 0 ) {
bgneal@45 4346 hasDuplicate = true;
bgneal@45 4347 }
bgneal@45 4348 return ret;
bgneal@45 4349 };
bgneal@45 4350 }
bgneal@45 4351
bgneal@45 4352 // Check to see if the browser returns elements by name when
bgneal@45 4353 // querying by getElementById (and provide a workaround)
bgneal@45 4354 (function(){
bgneal@45 4355 // We're going to inject a fake input element with a specified name
bgneal@183 4356 var form = document.createElement("div"),
bgneal@45 4357 id = "script" + (new Date).getTime();
bgneal@183 4358 form.innerHTML = "<a name='" + id + "'/>";
bgneal@45 4359
bgneal@45 4360 // Inject it into the root element, check its status, and remove it quickly
bgneal@45 4361 var root = document.documentElement;
bgneal@45 4362 root.insertBefore( form, root.firstChild );
bgneal@45 4363
bgneal@45 4364 // The workaround has to do additional checks after a getElementById
bgneal@45 4365 // Which slows things down for other browsers (hence the branching)
bgneal@45 4366 if ( !!document.getElementById( id ) ) {
bgneal@45 4367 Expr.find.ID = function(match, context, isXML){
bgneal@45 4368 if ( typeof context.getElementById !== "undefined" && !isXML ) {
bgneal@45 4369 var m = context.getElementById(match[1]);
bgneal@45 4370 return m ? m.id === match[1] || typeof m.getAttributeNode !== "undefined" && m.getAttributeNode("id").nodeValue === match[1] ? [m] : undefined : [];
bgneal@45 4371 }
bgneal@45 4372 };
bgneal@45 4373
bgneal@45 4374 Expr.filter.ID = function(elem, match){
bgneal@45 4375 var node = typeof elem.getAttributeNode !== "undefined" && elem.getAttributeNode("id");
bgneal@45 4376 return elem.nodeType === 1 && node && node.nodeValue === match;
bgneal@45 4377 };
bgneal@45 4378 }
bgneal@45 4379
bgneal@45 4380 root.removeChild( form );
bgneal@45 4381 })();
bgneal@45 4382
bgneal@45 4383 (function(){
bgneal@45 4384 // Check to see if the browser returns only elements
bgneal@45 4385 // when doing getElementsByTagName("*")
bgneal@45 4386
bgneal@45 4387 // Create a fake element
bgneal@45 4388 var div = document.createElement("div");
bgneal@45 4389 div.appendChild( document.createComment("") );
bgneal@45 4390
bgneal@45 4391 // Make sure no comments are found
bgneal@45 4392 if ( div.getElementsByTagName("*").length > 0 ) {
bgneal@45 4393 Expr.find.TAG = function(match, context){
bgneal@45 4394 var results = context.getElementsByTagName(match[1]);
bgneal@45 4395
bgneal@45 4396 // Filter out possible comments
bgneal@45 4397 if ( match[1] === "*" ) {
bgneal@45 4398 var tmp = [];
bgneal@45 4399
bgneal@45 4400 for ( var i = 0; results[i]; i++ ) {
bgneal@45 4401 if ( results[i].nodeType === 1 ) {
bgneal@45 4402 tmp.push( results[i] );
bgneal@45 4403 }
bgneal@45 4404 }
bgneal@45 4405
bgneal@45 4406 results = tmp;
bgneal@45 4407 }
bgneal@45 4408
bgneal@45 4409 return results;
bgneal@45 4410 };
bgneal@45 4411 }
bgneal@45 4412
bgneal@45 4413 // Check to see if an attribute returns normalized href attributes
bgneal@45 4414 div.innerHTML = "<a href='#'></a>";
bgneal@45 4415 if ( div.firstChild && typeof div.firstChild.getAttribute !== "undefined" &&
bgneal@45 4416 div.firstChild.getAttribute("href") !== "#" ) {
bgneal@45 4417 Expr.attrHandle.href = function(elem){
bgneal@45 4418 return elem.getAttribute("href", 2);
bgneal@45 4419 };
bgneal@45 4420 }
bgneal@45 4421 })();
bgneal@45 4422
bgneal@45 4423 if ( document.querySelectorAll ) (function(){
bgneal@45 4424 var oldSizzle = Sizzle, div = document.createElement("div");
bgneal@45 4425 div.innerHTML = "<p class='TEST'></p>";
bgneal@45 4426
bgneal@45 4427 // Safari can't handle uppercase or unicode characters when
bgneal@45 4428 // in quirks mode.
bgneal@45 4429 if ( div.querySelectorAll && div.querySelectorAll(".TEST").length === 0 ) {
bgneal@45 4430 return;
bgneal@45 4431 }
bgneal@45 4432
bgneal@45 4433 Sizzle = function(query, context, extra, seed){
bgneal@45 4434 context = context || document;
bgneal@45 4435
bgneal@45 4436 // Only use querySelectorAll on non-XML documents
bgneal@45 4437 // (ID selectors don't work in non-HTML documents)
bgneal@45 4438 if ( !seed && context.nodeType === 9 && !isXML(context) ) {
bgneal@45 4439 try {
bgneal@45 4440 return makeArray( context.querySelectorAll(query), extra );
bgneal@45 4441 } catch(e){}
bgneal@45 4442 }
bgneal@45 4443
bgneal@45 4444 return oldSizzle(query, context, extra, seed);
bgneal@45 4445 };
bgneal@45 4446
bgneal@45 4447 for ( var prop in oldSizzle ) {
bgneal@45 4448 Sizzle[ prop ] = oldSizzle[ prop ];
bgneal@45 4449 }
bgneal@45 4450 })();
bgneal@45 4451
bgneal@45 4452 if ( document.getElementsByClassName && document.documentElement.getElementsByClassName ) (function(){
bgneal@45 4453 var div = document.createElement("div");
bgneal@45 4454 div.innerHTML = "<div class='test e'></div><div class='test'></div>";
bgneal@45 4455
bgneal@45 4456 // Opera can't find a second classname (in 9.6)
bgneal@45 4457 if ( div.getElementsByClassName("e").length === 0 )
bgneal@45 4458 return;
bgneal@45 4459
bgneal@45 4460 // Safari caches class attributes, doesn't catch changes (in 3.2)
bgneal@45 4461 div.lastChild.className = "e";
bgneal@45 4462
bgneal@45 4463 if ( div.getElementsByClassName("e").length === 1 )
bgneal@45 4464 return;
bgneal@45 4465
bgneal@45 4466 Expr.order.splice(1, 0, "CLASS");
bgneal@45 4467 Expr.find.CLASS = function(match, context, isXML) {
bgneal@45 4468 if ( typeof context.getElementsByClassName !== "undefined" && !isXML ) {
bgneal@45 4469 return context.getElementsByClassName(match[1]);
bgneal@45 4470 }
bgneal@45 4471 };
bgneal@45 4472 })();
bgneal@45 4473
bgneal@45 4474 function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
bgneal@45 4475 var sibDir = dir == "previousSibling" && !isXML;
bgneal@45 4476 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
bgneal@45 4477 var elem = checkSet[i];
bgneal@45 4478 if ( elem ) {
bgneal@45 4479 if ( sibDir && elem.nodeType === 1 ){
bgneal@45 4480 elem.sizcache = doneName;
bgneal@45 4481 elem.sizset = i;
bgneal@45 4482 }
bgneal@45 4483 elem = elem[dir];
bgneal@45 4484 var match = false;
bgneal@45 4485
bgneal@45 4486 while ( elem ) {
bgneal@45 4487 if ( elem.sizcache === doneName ) {
bgneal@45 4488 match = checkSet[elem.sizset];
bgneal@45 4489 break;
bgneal@45 4490 }
bgneal@45 4491
bgneal@45 4492 if ( elem.nodeType === 1 && !isXML ){
bgneal@45 4493 elem.sizcache = doneName;
bgneal@45 4494 elem.sizset = i;
bgneal@45 4495 }
bgneal@45 4496
bgneal@45 4497 if ( elem.nodeName === cur ) {
bgneal@45 4498 match = elem;
bgneal@45 4499 break;
bgneal@45 4500 }
bgneal@45 4501
bgneal@45 4502 elem = elem[dir];
bgneal@45 4503 }
bgneal@45 4504
bgneal@45 4505 checkSet[i] = match;
bgneal@45 4506 }
bgneal@45 4507 }
bgneal@45 4508 }
bgneal@45 4509
bgneal@45 4510 function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
bgneal@45 4511 var sibDir = dir == "previousSibling" && !isXML;
bgneal@45 4512 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
bgneal@45 4513 var elem = checkSet[i];
bgneal@45 4514 if ( elem ) {
bgneal@45 4515 if ( sibDir && elem.nodeType === 1 ) {
bgneal@45 4516 elem.sizcache = doneName;
bgneal@45 4517 elem.sizset = i;
bgneal@45 4518 }
bgneal@45 4519 elem = elem[dir];
bgneal@45 4520 var match = false;
bgneal@45 4521
bgneal@45 4522 while ( elem ) {
bgneal@45 4523 if ( elem.sizcache === doneName ) {
bgneal@45 4524 match = checkSet[elem.sizset];
bgneal@45 4525 break;
bgneal@45 4526 }
bgneal@45 4527
bgneal@45 4528 if ( elem.nodeType === 1 ) {
bgneal@45 4529 if ( !isXML ) {
bgneal@45 4530 elem.sizcache = doneName;
bgneal@45 4531 elem.sizset = i;
bgneal@45 4532 }
bgneal@45 4533 if ( typeof cur !== "string" ) {
bgneal@45 4534 if ( elem === cur ) {
bgneal@45 4535 match = true;
bgneal@45 4536 break;
bgneal@45 4537 }
bgneal@45 4538
bgneal@45 4539 } else if ( Sizzle.filter( cur, [elem] ).length > 0 ) {
bgneal@45 4540 match = elem;
bgneal@45 4541 break;
bgneal@45 4542 }
bgneal@45 4543 }
bgneal@45 4544
bgneal@45 4545 elem = elem[dir];
bgneal@45 4546 }
bgneal@45 4547
bgneal@45 4548 checkSet[i] = match;
bgneal@45 4549 }
bgneal@45 4550 }
bgneal@45 4551 }
bgneal@45 4552
bgneal@45 4553 var contains = document.compareDocumentPosition ? function(a, b){
bgneal@45 4554 return a.compareDocumentPosition(b) & 16;
bgneal@45 4555 } : function(a, b){
bgneal@45 4556 return a !== b && (a.contains ? a.contains(b) : true);
bgneal@45 4557 };
bgneal@45 4558
bgneal@45 4559 var isXML = function(elem){
bgneal@45 4560 return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" ||
bgneal@45 4561 !!elem.ownerDocument && elem.ownerDocument.documentElement.nodeName !== "HTML";
bgneal@45 4562 };
bgneal@45 4563
bgneal@45 4564 var posProcess = function(selector, context){
bgneal@45 4565 var tmpSet = [], later = "", match,
bgneal@45 4566 root = context.nodeType ? [context] : context;
bgneal@45 4567
bgneal@45 4568 // Position selectors must be done after the filter
bgneal@45 4569 // And so must :not(positional) so we move all PSEUDOs to the end
bgneal@45 4570 while ( (match = Expr.match.PSEUDO.exec( selector )) ) {
bgneal@45 4571 later += match[0];
bgneal@45 4572 selector = selector.replace( Expr.match.PSEUDO, "" );
bgneal@45 4573 }
bgneal@45 4574
bgneal@45 4575 selector = Expr.relative[selector] ? selector + "*" : selector;
bgneal@45 4576
bgneal@45 4577 for ( var i = 0, l = root.length; i < l; i++ ) {
bgneal@45 4578 Sizzle( selector, root[i], tmpSet );
bgneal@45 4579 }
bgneal@45 4580
bgneal@45 4581 return Sizzle.filter( later, tmpSet );
bgneal@45 4582 };
bgneal@45 4583
bgneal@45 4584 // EXPOSE
bgneal@45 4585
bgneal@45 4586 window.tinymce.dom.Sizzle = Sizzle;
bgneal@45 4587
bgneal@45 4588 })();
bgneal@183 4589
bgneal@183 4590
bgneal@45 4591 (function(tinymce) {
bgneal@45 4592 // Shorten names
bgneal@45 4593 var each = tinymce.each, DOM = tinymce.DOM, isIE = tinymce.isIE, isWebKit = tinymce.isWebKit, Event;
bgneal@45 4594
bgneal@183 4595 tinymce.create('tinymce.dom.EventUtils', {
bgneal@183 4596 EventUtils : function() {
bgneal@183 4597 this.inits = [];
bgneal@183 4598 this.events = [];
bgneal@183 4599 },
bgneal@45 4600
bgneal@45 4601 add : function(o, n, f, s) {
bgneal@45 4602 var cb, t = this, el = t.events, r;
bgneal@45 4603
bgneal@183 4604 if (n instanceof Array) {
bgneal@183 4605 r = [];
bgneal@183 4606
bgneal@183 4607 each(n, function(n) {
bgneal@183 4608 r.push(t.add(o, n, f, s));
bgneal@183 4609 });
bgneal@183 4610
bgneal@183 4611 return r;
bgneal@183 4612 }
bgneal@183 4613
bgneal@45 4614 // Handle array
bgneal@45 4615 if (o && o.hasOwnProperty && o instanceof Array) {
bgneal@45 4616 r = [];
bgneal@45 4617
bgneal@45 4618 each(o, function(o) {
bgneal@45 4619 o = DOM.get(o);
bgneal@45 4620 r.push(t.add(o, n, f, s));
bgneal@45 4621 });
bgneal@45 4622
bgneal@45 4623 return r;
bgneal@45 4624 }
bgneal@45 4625
bgneal@45 4626 o = DOM.get(o);
bgneal@45 4627
bgneal@45 4628 if (!o)
bgneal@45 4629 return;
bgneal@45 4630
bgneal@45 4631 // Setup event callback
bgneal@45 4632 cb = function(e) {
bgneal@183 4633 // Is all events disabled
bgneal@183 4634 if (t.disabled)
bgneal@183 4635 return;
bgneal@183 4636
bgneal@45 4637 e = e || window.event;
bgneal@45 4638
bgneal@183 4639 // Patch in target, preventDefault and stopPropagation in IE it's W3C valid
bgneal@183 4640 if (e && isIE) {
bgneal@183 4641 if (!e.target)
bgneal@183 4642 e.target = e.srcElement;
bgneal@183 4643
bgneal@183 4644 // Patch in preventDefault, stopPropagation methods for W3C compatibility
bgneal@183 4645 tinymce.extend(e, t._stoppers);
bgneal@183 4646 }
bgneal@45 4647
bgneal@45 4648 if (!s)
bgneal@45 4649 return f(e);
bgneal@45 4650
bgneal@45 4651 return f.call(s, e);
bgneal@45 4652 };
bgneal@45 4653
bgneal@45 4654 if (n == 'unload') {
bgneal@45 4655 tinymce.unloads.unshift({func : cb});
bgneal@45 4656 return cb;
bgneal@45 4657 }
bgneal@45 4658
bgneal@45 4659 if (n == 'init') {
bgneal@45 4660 if (t.domLoaded)
bgneal@45 4661 cb();
bgneal@45 4662 else
bgneal@45 4663 t.inits.push(cb);
bgneal@45 4664
bgneal@45 4665 return cb;
bgneal@45 4666 }
bgneal@45 4667
bgneal@45 4668 // Store away listener reference
bgneal@45 4669 el.push({
bgneal@45 4670 obj : o,
bgneal@45 4671 name : n,
bgneal@45 4672 func : f,
bgneal@45 4673 cfunc : cb,
bgneal@45 4674 scope : s
bgneal@45 4675 });
bgneal@45 4676
bgneal@45 4677 t._add(o, n, cb);
bgneal@45 4678
bgneal@45 4679 return f;
bgneal@45 4680 },
bgneal@45 4681
bgneal@45 4682 remove : function(o, n, f) {
bgneal@45 4683 var t = this, a = t.events, s = false, r;
bgneal@45 4684
bgneal@45 4685 // Handle array
bgneal@45 4686 if (o && o.hasOwnProperty && o instanceof Array) {
bgneal@45 4687 r = [];
bgneal@45 4688
bgneal@45 4689 each(o, function(o) {
bgneal@45 4690 o = DOM.get(o);
bgneal@45 4691 r.push(t.remove(o, n, f));
bgneal@45 4692 });
bgneal@45 4693
bgneal@45 4694 return r;
bgneal@45 4695 }
bgneal@45 4696
bgneal@45 4697 o = DOM.get(o);
bgneal@45 4698
bgneal@45 4699 each(a, function(e, i) {
bgneal@45 4700 if (e.obj == o && e.name == n && (!f || (e.func == f || e.cfunc == f))) {
bgneal@45 4701 a.splice(i, 1);
bgneal@45 4702 t._remove(o, n, e.cfunc);
bgneal@45 4703 s = true;
bgneal@45 4704 return false;
bgneal@45 4705 }
bgneal@45 4706 });
bgneal@45 4707
bgneal@45 4708 return s;
bgneal@45 4709 },
bgneal@45 4710
bgneal@45 4711 clear : function(o) {
bgneal@45 4712 var t = this, a = t.events, i, e;
bgneal@45 4713
bgneal@45 4714 if (o) {
bgneal@45 4715 o = DOM.get(o);
bgneal@45 4716
bgneal@45 4717 for (i = a.length - 1; i >= 0; i--) {
bgneal@45 4718 e = a[i];
bgneal@45 4719
bgneal@45 4720 if (e.obj === o) {
bgneal@45 4721 t._remove(e.obj, e.name, e.cfunc);
bgneal@45 4722 e.obj = e.cfunc = null;
bgneal@45 4723 a.splice(i, 1);
bgneal@45 4724 }
bgneal@45 4725 }
bgneal@45 4726 }
bgneal@45 4727 },
bgneal@45 4728
bgneal@45 4729 cancel : function(e) {
bgneal@45 4730 if (!e)
bgneal@45 4731 return false;
bgneal@45 4732
bgneal@45 4733 this.stop(e);
bgneal@183 4734
bgneal@45 4735 return this.prevent(e);
bgneal@45 4736 },
bgneal@45 4737
bgneal@45 4738 stop : function(e) {
bgneal@45 4739 if (e.stopPropagation)
bgneal@45 4740 e.stopPropagation();
bgneal@45 4741 else
bgneal@45 4742 e.cancelBubble = true;
bgneal@45 4743
bgneal@45 4744 return false;
bgneal@45 4745 },
bgneal@45 4746
bgneal@45 4747 prevent : function(e) {
bgneal@45 4748 if (e.preventDefault)
bgneal@45 4749 e.preventDefault();
bgneal@45 4750 else
bgneal@45 4751 e.returnValue = false;
bgneal@45 4752
bgneal@45 4753 return false;
bgneal@45 4754 },
bgneal@45 4755
bgneal@183 4756 destroy : function() {
bgneal@183 4757 var t = this;
bgneal@45 4758
bgneal@45 4759 each(t.events, function(e, i) {
bgneal@45 4760 t._remove(e.obj, e.name, e.cfunc);
bgneal@45 4761 e.obj = e.cfunc = null;
bgneal@45 4762 });
bgneal@45 4763
bgneal@45 4764 t.events = [];
bgneal@45 4765 t = null;
bgneal@45 4766 },
bgneal@45 4767
bgneal@45 4768 _add : function(o, n, f) {
bgneal@45 4769 if (o.attachEvent)
bgneal@45 4770 o.attachEvent('on' + n, f);
bgneal@45 4771 else if (o.addEventListener)
bgneal@45 4772 o.addEventListener(n, f, false);
bgneal@45 4773 else
bgneal@45 4774 o['on' + n] = f;
bgneal@45 4775 },
bgneal@45 4776
bgneal@45 4777 _remove : function(o, n, f) {
bgneal@45 4778 if (o) {
bgneal@45 4779 try {
bgneal@45 4780 if (o.detachEvent)
bgneal@45 4781 o.detachEvent('on' + n, f);
bgneal@45 4782 else if (o.removeEventListener)
bgneal@45 4783 o.removeEventListener(n, f, false);
bgneal@45 4784 else
bgneal@45 4785 o['on' + n] = null;
bgneal@45 4786 } catch (ex) {
bgneal@45 4787 // Might fail with permission denined on IE so we just ignore that
bgneal@45 4788 }
bgneal@45 4789 }
bgneal@45 4790 },
bgneal@45 4791
bgneal@183 4792 _pageInit : function(win) {
bgneal@183 4793 var t = this;
bgneal@183 4794
bgneal@183 4795 // Keep it from running more than once
bgneal@183 4796 if (t.domLoaded)
bgneal@45 4797 return;
bgneal@45 4798
bgneal@183 4799 t.domLoaded = true;
bgneal@183 4800
bgneal@183 4801 each(t.inits, function(c) {
bgneal@45 4802 c();
bgneal@45 4803 });
bgneal@45 4804
bgneal@183 4805 t.inits = [];
bgneal@183 4806 },
bgneal@183 4807
bgneal@183 4808 _wait : function(win) {
bgneal@183 4809 var t = this, doc = win.document;
bgneal@45 4810
bgneal@45 4811 // No need since the document is already loaded
bgneal@183 4812 if (win.tinyMCE_GZ && tinyMCE_GZ.loaded) {
bgneal@183 4813 t.domLoaded = 1;
bgneal@45 4814 return;
bgneal@45 4815 }
bgneal@45 4816
bgneal@183 4817 // Use IE method
bgneal@183 4818 if (doc.attachEvent) {
bgneal@183 4819 doc.attachEvent("onreadystatechange", function() {
bgneal@183 4820 if (doc.readyState === "complete") {
bgneal@183 4821 doc.detachEvent("onreadystatechange", arguments.callee);
bgneal@183 4822 t._pageInit(win);
bgneal@183 4823 }
bgneal@183 4824 });
bgneal@183 4825
bgneal@183 4826 if (doc.documentElement.doScroll && win == win.top) {
bgneal@183 4827 (function() {
bgneal@183 4828 if (t.domLoaded)
bgneal@183 4829 return;
bgneal@183 4830
bgneal@183 4831 try {
bgneal@183 4832 // If IE is used, use the trick by Diego Perini
bgneal@183 4833 // http://javascript.nwbox.com/IEContentLoaded/
bgneal@183 4834 doc.documentElement.doScroll("left");
bgneal@183 4835 } catch (ex) {
bgneal@183 4836 setTimeout(arguments.callee, 0);
bgneal@183 4837 return;
bgneal@183 4838 }
bgneal@183 4839
bgneal@183 4840 t._pageInit(win);
bgneal@183 4841 })();
bgneal@183 4842 }
bgneal@183 4843 } else if (doc.addEventListener) {
bgneal@183 4844 t._add(win, 'DOMContentLoaded', function() {
bgneal@183 4845 t._pageInit(win);
bgneal@183 4846 });
bgneal@183 4847 }
bgneal@183 4848
bgneal@183 4849 t._add(win, 'load', function() {
bgneal@183 4850 t._pageInit(win);
bgneal@183 4851 });
bgneal@183 4852 },
bgneal@183 4853
bgneal@183 4854 _stoppers : {
bgneal@183 4855 preventDefault : function() {
bgneal@183 4856 this.returnValue = false;
bgneal@183 4857 },
bgneal@183 4858
bgneal@183 4859 stopPropagation : function() {
bgneal@183 4860 this.cancelBubble = true;
bgneal@45 4861 }
bgneal@45 4862 }
bgneal@183 4863 });
bgneal@183 4864
bgneal@183 4865 Event = tinymce.dom.Event = new tinymce.dom.EventUtils();
bgneal@45 4866
bgneal@45 4867 // Dispatch DOM content loaded event for IE and Safari
bgneal@183 4868 Event._wait(window);
bgneal@183 4869
bgneal@183 4870 tinymce.addUnload(function() {
bgneal@183 4871 Event.destroy();
bgneal@183 4872 });
bgneal@45 4873 })(tinymce);
bgneal@183 4874
bgneal@45 4875 (function(tinymce) {
bgneal@183 4876 tinymce.dom.Element = function(id, settings) {
bgneal@183 4877 var t = this, dom, el;
bgneal@183 4878
bgneal@183 4879 t.settings = settings = settings || {};
bgneal@183 4880 t.id = id;
bgneal@183 4881 t.dom = dom = settings.dom || tinymce.DOM;
bgneal@183 4882
bgneal@183 4883 // Only IE leaks DOM references, this is a lot faster
bgneal@183 4884 if (!tinymce.isIE)
bgneal@183 4885 el = dom.get(t.id);
bgneal@183 4886
bgneal@183 4887 tinymce.each(
bgneal@183 4888 ('getPos,getRect,getParent,add,setStyle,getStyle,setStyles,' +
bgneal@183 4889 'setAttrib,setAttribs,getAttrib,addClass,removeClass,' +
bgneal@183 4890 'hasClass,getOuterHTML,setOuterHTML,remove,show,hide,' +
bgneal@183 4891 'isHidden,setHTML,get').split(/,/)
bgneal@183 4892 , function(k) {
bgneal@45 4893 t[k] = function() {
bgneal@45 4894 var a = [id], i;
bgneal@45 4895
bgneal@45 4896 for (i = 0; i < arguments.length; i++)
bgneal@45 4897 a.push(arguments[i]);
bgneal@45 4898
bgneal@45 4899 a = dom[k].apply(dom, a);
bgneal@45 4900 t.update(k);
bgneal@45 4901
bgneal@45 4902 return a;
bgneal@45 4903 };
bgneal@45 4904 });
bgneal@183 4905
bgneal@183 4906 tinymce.extend(t, {
bgneal@183 4907 on : function(n, f, s) {
bgneal@183 4908 return tinymce.dom.Event.add(t.id, n, f, s);
bgneal@183 4909 },
bgneal@183 4910
bgneal@183 4911 getXY : function() {
bgneal@183 4912 return {
bgneal@183 4913 x : parseInt(t.getStyle('left')),
bgneal@183 4914 y : parseInt(t.getStyle('top'))
bgneal@183 4915 };
bgneal@183 4916 },
bgneal@183 4917
bgneal@183 4918 getSize : function() {
bgneal@183 4919 var n = dom.get(t.id);
bgneal@183 4920
bgneal@183 4921 return {
bgneal@183 4922 w : parseInt(t.getStyle('width') || n.clientWidth),
bgneal@183 4923 h : parseInt(t.getStyle('height') || n.clientHeight)
bgneal@183 4924 };
bgneal@183 4925 },
bgneal@183 4926
bgneal@183 4927 moveTo : function(x, y) {
bgneal@183 4928 t.setStyles({left : x, top : y});
bgneal@183 4929 },
bgneal@183 4930
bgneal@183 4931 moveBy : function(x, y) {
bgneal@183 4932 var p = t.getXY();
bgneal@183 4933
bgneal@183 4934 t.moveTo(p.x + x, p.y + y);
bgneal@183 4935 },
bgneal@183 4936
bgneal@183 4937 resizeTo : function(w, h) {
bgneal@183 4938 t.setStyles({width : w, height : h});
bgneal@183 4939 },
bgneal@183 4940
bgneal@183 4941 resizeBy : function(w, h) {
bgneal@183 4942 var s = t.getSize();
bgneal@183 4943
bgneal@183 4944 t.resizeTo(s.w + w, s.h + h);
bgneal@183 4945 },
bgneal@183 4946
bgneal@183 4947 update : function(k) {
bgneal@183 4948 var b;
bgneal@183 4949
bgneal@183 4950 if (tinymce.isIE6 && settings.blocker) {
bgneal@183 4951 k = k || '';
bgneal@183 4952
bgneal@183 4953 // Ignore getters
bgneal@183 4954 if (k.indexOf('get') === 0 || k.indexOf('has') === 0 || k.indexOf('is') === 0)
bgneal@183 4955 return;
bgneal@183 4956
bgneal@183 4957 // Remove blocker on remove
bgneal@183 4958 if (k == 'remove') {
bgneal@183 4959 dom.remove(t.blocker);
bgneal@183 4960 return;
bgneal@183 4961 }
bgneal@183 4962
bgneal@183 4963 if (!t.blocker) {
bgneal@183 4964 t.blocker = dom.uniqueId();
bgneal@183 4965 b = dom.add(settings.container || dom.getRoot(), 'iframe', {id : t.blocker, style : 'position:absolute;', frameBorder : 0, src : 'javascript:""'});
bgneal@183 4966 dom.setStyle(b, 'opacity', 0);
bgneal@183 4967 } else
bgneal@183 4968 b = dom.get(t.blocker);
bgneal@183 4969
bgneal@183 4970 dom.setStyles(b, {
bgneal@183 4971 left : t.getStyle('left', 1),
bgneal@183 4972 top : t.getStyle('top', 1),
bgneal@183 4973 width : t.getStyle('width', 1),
bgneal@183 4974 height : t.getStyle('height', 1),
bgneal@183 4975 display : t.getStyle('display', 1),
bgneal@183 4976 zIndex : parseInt(t.getStyle('zIndex', 1) || 0) - 1
bgneal@183 4977 });
bgneal@183 4978 }
bgneal@183 4979 }
bgneal@183 4980 });
bgneal@183 4981 };
bgneal@45 4982 })(tinymce);
bgneal@183 4983
bgneal@45 4984 (function(tinymce) {
bgneal@45 4985 function trimNl(s) {
bgneal@45 4986 return s.replace(/[\n\r]+/g, '');
bgneal@45 4987 };
bgneal@45 4988
bgneal@45 4989 // Shorten names
bgneal@45 4990 var is = tinymce.is, isIE = tinymce.isIE, each = tinymce.each;
bgneal@45 4991
bgneal@45 4992 tinymce.create('tinymce.dom.Selection', {
bgneal@45 4993 Selection : function(dom, win, serializer) {
bgneal@45 4994 var t = this;
bgneal@45 4995
bgneal@45 4996 t.dom = dom;
bgneal@45 4997 t.win = win;
bgneal@45 4998 t.serializer = serializer;
bgneal@45 4999
bgneal@45 5000 // Add events
bgneal@45 5001 each([
bgneal@45 5002 'onBeforeSetContent',
bgneal@45 5003 'onBeforeGetContent',
bgneal@45 5004 'onSetContent',
bgneal@45 5005 'onGetContent'
bgneal@45 5006 ], function(e) {
bgneal@45 5007 t[e] = new tinymce.util.Dispatcher(t);
bgneal@45 5008 });
bgneal@45 5009
bgneal@45 5010 // No W3C Range support
bgneal@45 5011 if (!t.win.getSelection)
bgneal@45 5012 t.tridentSel = new tinymce.dom.TridentSelection(t);
bgneal@45 5013
bgneal@45 5014 // Prevent leaks
bgneal@45 5015 tinymce.addUnload(t.destroy, t);
bgneal@45 5016 },
bgneal@45 5017
bgneal@45 5018 getContent : function(s) {
bgneal@45 5019 var t = this, r = t.getRng(), e = t.dom.create("body"), se = t.getSel(), wb, wa, n;
bgneal@45 5020
bgneal@45 5021 s = s || {};
bgneal@45 5022 wb = wa = '';
bgneal@45 5023 s.get = true;
bgneal@45 5024 s.format = s.format || 'html';
bgneal@45 5025 t.onBeforeGetContent.dispatch(t, s);
bgneal@45 5026
bgneal@45 5027 if (s.format == 'text')
bgneal@45 5028 return t.isCollapsed() ? '' : (r.text || (se.toString ? se.toString() : ''));
bgneal@45 5029
bgneal@45 5030 if (r.cloneContents) {
bgneal@45 5031 n = r.cloneContents();
bgneal@45 5032
bgneal@45 5033 if (n)
bgneal@45 5034 e.appendChild(n);
bgneal@45 5035 } else if (is(r.item) || is(r.htmlText))
bgneal@45 5036 e.innerHTML = r.item ? r.item(0).outerHTML : r.htmlText;
bgneal@45 5037 else
bgneal@45 5038 e.innerHTML = r.toString();
bgneal@45 5039
bgneal@45 5040 // Keep whitespace before and after
bgneal@45 5041 if (/^\s/.test(e.innerHTML))
bgneal@45 5042 wb = ' ';
bgneal@45 5043
bgneal@45 5044 if (/\s+$/.test(e.innerHTML))
bgneal@45 5045 wa = ' ';
bgneal@45 5046
bgneal@45 5047 s.getInner = true;
bgneal@45 5048
bgneal@45 5049 s.content = t.isCollapsed() ? '' : wb + t.serializer.serialize(e, s) + wa;
bgneal@45 5050 t.onGetContent.dispatch(t, s);
bgneal@45 5051
bgneal@45 5052 return s.content;
bgneal@45 5053 },
bgneal@45 5054
bgneal@45 5055 setContent : function(h, s) {
bgneal@45 5056 var t = this, r = t.getRng(), c, d = t.win.document;
bgneal@45 5057
bgneal@45 5058 s = s || {format : 'html'};
bgneal@45 5059 s.set = true;
bgneal@45 5060 h = s.content = t.dom.processHTML(h);
bgneal@45 5061
bgneal@45 5062 // Dispatch before set content event
bgneal@45 5063 t.onBeforeSetContent.dispatch(t, s);
bgneal@45 5064 h = s.content;
bgneal@45 5065
bgneal@45 5066 if (r.insertNode) {
bgneal@45 5067 // Make caret marker since insertNode places the caret in the beginning of text after insert
bgneal@45 5068 h += '<span id="__caret">_</span>';
bgneal@45 5069
bgneal@45 5070 // Delete and insert new node
bgneal@217 5071
bgneal@217 5072 if (r.startContainer == d && r.endContainer == d) {
bgneal@183 5073 // WebKit will fail if the body is empty since the range is then invalid and it can't insert contents
bgneal@183 5074 d.body.innerHTML = h;
bgneal@183 5075 } else {
bgneal@183 5076 r.deleteContents();
bgneal@217 5077 if (d.body.childNodes.length == 0) {
bgneal@217 5078 d.body.innerHTML = h;
bgneal@217 5079 } else {
bgneal@217 5080 r.insertNode(r.createContextualFragment(h));
bgneal@217 5081 }
bgneal@183 5082 }
bgneal@45 5083
bgneal@45 5084 // Move to caret marker
bgneal@45 5085 c = t.dom.get('__caret');
bgneal@45 5086 // Make sure we wrap it compleatly, Opera fails with a simple select call
bgneal@45 5087 r = d.createRange();
bgneal@45 5088 r.setStartBefore(c);
bgneal@183 5089 r.setEndBefore(c);
bgneal@45 5090 t.setRng(r);
bgneal@45 5091
bgneal@45 5092 // Remove the caret position
bgneal@45 5093 t.dom.remove('__caret');
bgneal@45 5094 } else {
bgneal@45 5095 if (r.item) {
bgneal@45 5096 // Delete content and get caret text selection
bgneal@45 5097 d.execCommand('Delete', false, null);
bgneal@45 5098 r = t.getRng();
bgneal@45 5099 }
bgneal@45 5100
bgneal@45 5101 r.pasteHTML(h);
bgneal@45 5102 }
bgneal@45 5103
bgneal@45 5104 // Dispatch set content event
bgneal@45 5105 t.onSetContent.dispatch(t, s);
bgneal@45 5106 },
bgneal@45 5107
bgneal@45 5108 getStart : function() {
bgneal@45 5109 var t = this, r = t.getRng(), e;
bgneal@45 5110
bgneal@183 5111 if (r.duplicate || r.item) {
bgneal@45 5112 if (r.item)
bgneal@45 5113 return r.item(0);
bgneal@45 5114
bgneal@45 5115 r = r.duplicate();
bgneal@45 5116 r.collapse(1);
bgneal@45 5117 e = r.parentElement();
bgneal@45 5118
bgneal@45 5119 if (e && e.nodeName == 'BODY')
bgneal@183 5120 return e.firstChild || e;
bgneal@45 5121
bgneal@45 5122 return e;
bgneal@45 5123 } else {
bgneal@45 5124 e = r.startContainer;
bgneal@45 5125
bgneal@183 5126 if (e.nodeType == 1 && e.hasChildNodes())
bgneal@183 5127 e = e.childNodes[Math.min(e.childNodes.length - 1, r.startOffset)];
bgneal@183 5128
bgneal@183 5129 if (e && e.nodeType == 3)
bgneal@183 5130 return e.parentNode;
bgneal@183 5131
bgneal@183 5132 return e;
bgneal@45 5133 }
bgneal@45 5134 },
bgneal@45 5135
bgneal@45 5136 getEnd : function() {
bgneal@183 5137 var t = this, r = t.getRng(), e, eo;
bgneal@183 5138
bgneal@183 5139 if (r.duplicate || r.item) {
bgneal@45 5140 if (r.item)
bgneal@45 5141 return r.item(0);
bgneal@45 5142
bgneal@45 5143 r = r.duplicate();
bgneal@45 5144 r.collapse(0);
bgneal@45 5145 e = r.parentElement();
bgneal@45 5146
bgneal@45 5147 if (e && e.nodeName == 'BODY')
bgneal@183 5148 return e.lastChild || e;
bgneal@45 5149
bgneal@45 5150 return e;
bgneal@45 5151 } else {
bgneal@45 5152 e = r.endContainer;
bgneal@183 5153 eo = r.endOffset;
bgneal@183 5154
bgneal@183 5155 if (e.nodeType == 1 && e.hasChildNodes())
bgneal@183 5156 e = e.childNodes[eo > 0 ? eo - 1 : eo];
bgneal@183 5157
bgneal@183 5158 if (e && e.nodeType == 3)
bgneal@183 5159 return e.parentNode;
bgneal@183 5160
bgneal@183 5161 return e;
bgneal@183 5162 }
bgneal@183 5163 },
bgneal@183 5164
bgneal@183 5165 getBookmark : function(type, normalized) {
bgneal@183 5166 var t = this, dom = t.dom, rng, rng2, id, collapsed, name, element, index, chr = '\uFEFF', styles;
bgneal@183 5167
bgneal@183 5168 function findIndex(name, element) {
bgneal@183 5169 var index = 0;
bgneal@183 5170
bgneal@183 5171 each(dom.select(name), function(node, i) {
bgneal@183 5172 if (node == element)
bgneal@183 5173 index = i;
bgneal@183 5174 });
bgneal@183 5175
bgneal@183 5176 return index;
bgneal@183 5177 };
bgneal@183 5178
bgneal@183 5179 if (type == 2) {
bgneal@183 5180 function getLocation() {
bgneal@183 5181 var rng = t.getRng(true), root = dom.getRoot(), bookmark = {};
bgneal@183 5182
bgneal@183 5183 function getPoint(rng, start) {
bgneal@183 5184 var container = rng[start ? 'startContainer' : 'endContainer'],
bgneal@183 5185 offset = rng[start ? 'startOffset' : 'endOffset'], point = [], node, childNodes, after = 0;
bgneal@183 5186
bgneal@183 5187 if (container.nodeType == 3) {
bgneal@183 5188 if (normalized) {
bgneal@183 5189 for (node = container.previousSibling; node && node.nodeType == 3; node = node.previousSibling)
bgneal@183 5190 offset += node.nodeValue.length;
bgneal@183 5191 }
bgneal@183 5192
bgneal@183 5193 point.push(offset);
bgneal@183 5194 } else {
bgneal@183 5195 childNodes = container.childNodes;
bgneal@217 5196
bgneal@217 5197 if (offset >= childNodes.length && childNodes.length) {
bgneal@183 5198 after = 1;
bgneal@217 5199 offset = Math.max(0, childNodes.length - 1);
bgneal@183 5200 }
bgneal@183 5201
bgneal@183 5202 point.push(t.dom.nodeIndex(childNodes[offset], normalized) + after);
bgneal@183 5203 }
bgneal@183 5204
bgneal@183 5205 for (; container && container != root; container = container.parentNode)
bgneal@183 5206 point.push(t.dom.nodeIndex(container, normalized));
bgneal@183 5207
bgneal@183 5208 return point;
bgneal@45 5209 };
bgneal@183 5210
bgneal@183 5211 bookmark.start = getPoint(rng, true);
bgneal@183 5212
bgneal@183 5213 if (!t.isCollapsed())
bgneal@183 5214 bookmark.end = getPoint(rng);
bgneal@183 5215
bgneal@183 5216 return bookmark;
bgneal@183 5217 };
bgneal@183 5218
bgneal@183 5219 return getLocation();
bgneal@183 5220 }
bgneal@183 5221
bgneal@183 5222 // Handle simple range
bgneal@183 5223 if (type)
bgneal@183 5224 return {rng : t.getRng()};
bgneal@183 5225
bgneal@183 5226 rng = t.getRng();
bgneal@183 5227 id = dom.uniqueId();
bgneal@183 5228 collapsed = tinyMCE.activeEditor.selection.isCollapsed();
bgneal@183 5229 styles = 'overflow:hidden;line-height:0px';
bgneal@183 5230
bgneal@183 5231 // Explorer method
bgneal@183 5232 if (rng.duplicate || rng.item) {
bgneal@45 5233 // Text selection
bgneal@183 5234 if (!rng.item) {
bgneal@183 5235 rng2 = rng.duplicate();
bgneal@183 5236
bgneal@183 5237 // Insert start marker
bgneal@183 5238 rng.collapse();
bgneal@183 5239 rng.pasteHTML('<span _mce_type="bookmark" id="' + id + '_start" style="' + styles + '">' + chr + '</span>');
bgneal@183 5240
bgneal@183 5241 // Insert end marker
bgneal@183 5242 if (!collapsed) {
bgneal@183 5243 rng2.collapse(false);
bgneal@183 5244 rng2.pasteHTML('<span _mce_type="bookmark" id="' + id + '_end" style="' + styles + '">' + chr + '</span>');
bgneal@183 5245 }
bgneal@183 5246 } else {
bgneal@183 5247 // Control selection
bgneal@183 5248 element = rng.item(0);
bgneal@183 5249 name = element.nodeName;
bgneal@183 5250
bgneal@183 5251 return {name : name, index : findIndex(name, element)};
bgneal@183 5252 }
bgneal@183 5253 } else {
bgneal@183 5254 element = t.getNode();
bgneal@183 5255 name = element.nodeName;
bgneal@183 5256 if (name == 'IMG')
bgneal@183 5257 return {name : name, index : findIndex(name, element)};
bgneal@183 5258
bgneal@183 5259 // W3C method
bgneal@183 5260 rng2 = rng.cloneRange();
bgneal@183 5261
bgneal@183 5262 // Insert end marker
bgneal@183 5263 if (!collapsed) {
bgneal@183 5264 rng2.collapse(false);
bgneal@183 5265 rng2.insertNode(dom.create('span', {_mce_type : "bookmark", id : id + '_end', style : styles}, chr));
bgneal@183 5266 }
bgneal@183 5267
bgneal@183 5268 rng.collapse(true);
bgneal@183 5269 rng.insertNode(dom.create('span', {_mce_type : "bookmark", id : id + '_start', style : styles}, chr));
bgneal@183 5270 }
bgneal@183 5271
bgneal@183 5272 t.moveToBookmark({id : id, keep : 1});
bgneal@183 5273
bgneal@183 5274 return {id : id};
bgneal@183 5275 },
bgneal@183 5276
bgneal@183 5277 moveToBookmark : function(bookmark) {
bgneal@217 5278 var t = this, dom = t.dom, marker1, marker2, rng, root, startContainer, endContainer, startOffset, endOffset;
bgneal@183 5279
bgneal@183 5280 // Clear selection cache
bgneal@183 5281 if (t.tridentSel)
bgneal@183 5282 t.tridentSel.destroy();
bgneal@183 5283
bgneal@183 5284 if (bookmark) {
bgneal@183 5285 if (bookmark.start) {
bgneal@183 5286 rng = dom.createRng();
bgneal@183 5287 root = dom.getRoot();
bgneal@183 5288
bgneal@183 5289 function setEndPoint(start) {
bgneal@183 5290 var point = bookmark[start ? 'start' : 'end'], i, node, offset;
bgneal@183 5291
bgneal@183 5292 if (point) {
bgneal@183 5293 // Find container node
bgneal@183 5294 for (node = root, i = point.length - 1; i >= 1; i--)
bgneal@183 5295 node = node.childNodes[point[i]];
bgneal@183 5296
bgneal@183 5297 // Set offset within container node
bgneal@183 5298 if (start)
bgneal@183 5299 rng.setStart(node, point[0]);
bgneal@183 5300 else
bgneal@183 5301 rng.setEnd(node, point[0]);
bgneal@183 5302 }
bgneal@183 5303 };
bgneal@183 5304
bgneal@183 5305 setEndPoint(true);
bgneal@183 5306 setEndPoint();
bgneal@183 5307
bgneal@183 5308 t.setRng(rng);
bgneal@183 5309 } else if (bookmark.id) {
bgneal@183 5310 function restoreEndPoint(suffix) {
bgneal@183 5311 var marker = dom.get(bookmark.id + '_' + suffix), node, idx, next, prev, keep = bookmark.keep;
bgneal@183 5312
bgneal@183 5313 if (marker) {
bgneal@183 5314 node = marker.parentNode;
bgneal@183 5315
bgneal@183 5316 if (suffix == 'start') {
bgneal@183 5317 if (!keep) {
bgneal@183 5318 idx = dom.nodeIndex(marker);
bgneal@183 5319 } else {
bgneal@183 5320 node = marker;
bgneal@183 5321 idx = 1;
bgneal@183 5322 }
bgneal@183 5323
bgneal@217 5324 startContainer = endContainer = node;
bgneal@217 5325 startOffset = endOffset = idx;
bgneal@183 5326 } else {
bgneal@183 5327 if (!keep) {
bgneal@183 5328 idx = dom.nodeIndex(marker);
bgneal@183 5329 } else {
bgneal@183 5330 node = marker;
bgneal@183 5331 idx = 1;
bgneal@183 5332 }
bgneal@183 5333
bgneal@217 5334 endContainer = node;
bgneal@217 5335 endOffset = idx;
bgneal@183 5336 }
bgneal@183 5337
bgneal@183 5338 if (!keep) {
bgneal@183 5339 prev = marker.previousSibling;
bgneal@183 5340 next = marker.nextSibling;
bgneal@183 5341
bgneal@183 5342 // Remove all marker text nodes
bgneal@183 5343 each(tinymce.grep(marker.childNodes), function(node) {
bgneal@183 5344 if (node.nodeType == 3)
bgneal@183 5345 node.nodeValue = node.nodeValue.replace(/\uFEFF/g, '');
bgneal@183 5346 });
bgneal@183 5347
bgneal@183 5348 // Remove marker but keep children if for example contents where inserted into the marker
bgneal@183 5349 // Also remove duplicated instances of the marker for example by a split operation or by WebKit auto split on paste feature
bgneal@183 5350 while (marker = dom.get(bookmark.id + '_' + suffix))
bgneal@183 5351 dom.remove(marker, 1);
bgneal@183 5352
bgneal@183 5353 // If siblings are text nodes then merge them
bgneal@183 5354 if (prev && next && prev.nodeType == next.nodeType && prev.nodeType == 3) {
bgneal@183 5355 idx = prev.nodeValue.length;
bgneal@183 5356 prev.appendData(next.nodeValue);
bgneal@183 5357 dom.remove(next);
bgneal@183 5358
bgneal@183 5359 if (suffix == 'start') {
bgneal@217 5360 startContainer = endContainer = prev;
bgneal@217 5361 startOffset = endOffset = idx;
bgneal@217 5362 } else {
bgneal@217 5363 endContainer = prev;
bgneal@217 5364 endOffset = idx;
bgneal@217 5365 }
bgneal@183 5366 }
bgneal@183 5367 }
bgneal@183 5368 }
bgneal@183 5369 };
bgneal@183 5370
bgneal@183 5371 // Restore start/end points
bgneal@183 5372 restoreEndPoint('start');
bgneal@183 5373 restoreEndPoint('end');
bgneal@183 5374
bgneal@217 5375 rng = dom.createRng();
bgneal@217 5376 rng.setStart(startContainer, startOffset);
bgneal@217 5377 rng.setEnd(endContainer, endOffset);
bgneal@183 5378 t.setRng(rng);
bgneal@183 5379 } else if (bookmark.name) {
bgneal@183 5380 t.select(dom.select(bookmark.name)[bookmark.index]);
bgneal@183 5381 } else if (bookmark.rng)
bgneal@183 5382 t.setRng(bookmark.rng);
bgneal@183 5383 }
bgneal@183 5384 },
bgneal@183 5385
bgneal@183 5386 select : function(node, content) {
bgneal@183 5387 var t = this, dom = t.dom, rng = dom.createRng(), idx;
bgneal@183 5388
bgneal@183 5389 idx = dom.nodeIndex(node);
bgneal@183 5390 rng.setStart(node.parentNode, idx);
bgneal@183 5391 rng.setEnd(node.parentNode, idx + 1);
bgneal@183 5392
bgneal@183 5393 // Find first/last text node or BR element
bgneal@183 5394 if (content) {
bgneal@183 5395 function setPoint(node, start) {
bgneal@183 5396 var walker = new tinymce.dom.TreeWalker(node, node);
bgneal@183 5397
bgneal@183 5398 do {
bgneal@183 5399 // Text node
bgneal@183 5400 if (node.nodeType == 3 && tinymce.trim(node.nodeValue).length != 0) {
bgneal@183 5401 if (start)
bgneal@183 5402 rng.setStart(node, 0);
bgneal@183 5403 else
bgneal@183 5404 rng.setEnd(node, node.nodeValue.length);
bgneal@183 5405
bgneal@183 5406 return;
bgneal@183 5407 }
bgneal@183 5408
bgneal@183 5409 // BR element
bgneal@183 5410 if (node.nodeName == 'BR') {
bgneal@183 5411 if (start)
bgneal@183 5412 rng.setStartBefore(node);
bgneal@183 5413 else
bgneal@183 5414 rng.setEndBefore(node);
bgneal@183 5415
bgneal@183 5416 return;
bgneal@183 5417 }
bgneal@183 5418 } while (node = (start ? walker.next() : walker.prev()));
bgneal@45 5419 };
bgneal@183 5420
bgneal@183 5421 setPoint(node, 1);
bgneal@183 5422 setPoint(node);
bgneal@183 5423 }
bgneal@183 5424
bgneal@183 5425 t.setRng(rng);
bgneal@183 5426
bgneal@183 5427 return node;
bgneal@45 5428 },
bgneal@45 5429
bgneal@45 5430 isCollapsed : function() {
bgneal@45 5431 var t = this, r = t.getRng(), s = t.getSel();
bgneal@45 5432
bgneal@45 5433 if (!r || r.item)
bgneal@45 5434 return false;
bgneal@45 5435
bgneal@183 5436 if (r.compareEndPoints)
bgneal@183 5437 return r.compareEndPoints('StartToEnd', r) === 0;
bgneal@183 5438
bgneal@183 5439 return !s || r.collapsed;
bgneal@45 5440 },
bgneal@45 5441
bgneal@45 5442 collapse : function(b) {
bgneal@45 5443 var t = this, r = t.getRng(), n;
bgneal@45 5444
bgneal@45 5445 // Control range on IE
bgneal@45 5446 if (r.item) {
bgneal@45 5447 n = r.item(0);
bgneal@45 5448 r = this.win.document.body.createTextRange();
bgneal@45 5449 r.moveToElementText(n);
bgneal@45 5450 }
bgneal@45 5451
bgneal@45 5452 r.collapse(!!b);
bgneal@45 5453 t.setRng(r);
bgneal@45 5454 },
bgneal@45 5455
bgneal@45 5456 getSel : function() {
bgneal@45 5457 var t = this, w = this.win;
bgneal@45 5458
bgneal@45 5459 return w.getSelection ? w.getSelection() : w.document.selection;
bgneal@45 5460 },
bgneal@45 5461
bgneal@45 5462 getRng : function(w3c) {
bgneal@45 5463 var t = this, s, r;
bgneal@45 5464
bgneal@45 5465 // Found tridentSel object then we need to use that one
bgneal@45 5466 if (w3c && t.tridentSel)
bgneal@45 5467 return t.tridentSel.getRangeAt(0);
bgneal@45 5468
bgneal@45 5469 try {
bgneal@45 5470 if (s = t.getSel())
bgneal@45 5471 r = s.rangeCount > 0 ? s.getRangeAt(0) : (s.createRange ? s.createRange() : t.win.document.createRange());
bgneal@45 5472 } catch (ex) {
bgneal@45 5473 // IE throws unspecified error here if TinyMCE is placed in a frame/iframe
bgneal@45 5474 }
bgneal@45 5475
bgneal@45 5476 // No range found then create an empty one
bgneal@45 5477 // This can occur when the editor is placed in a hidden container element on Gecko
bgneal@45 5478 // Or on IE when there was an exception
bgneal@45 5479 if (!r)
bgneal@183 5480 r = t.win.document.createRange ? t.win.document.createRange() : t.win.document.body.createTextRange();
bgneal@45 5481
bgneal@217 5482 if (t.selectedRange && t.explicitRange) {
bgneal@217 5483 if (r.compareBoundaryPoints(r.START_TO_START, t.selectedRange) === 0 && r.compareBoundaryPoints(r.END_TO_END, t.selectedRange) === 0) {
bgneal@217 5484 // Safari, Opera and Chrome only ever select text which causes the range to change.
bgneal@217 5485 // This lets us use the originally set range if the selection hasn't been changed by the user.
bgneal@217 5486 r = t.explicitRange;
bgneal@217 5487 } else {
bgneal@217 5488 t.selectedRange = null;
bgneal@217 5489 t.explicitRange = null;
bgneal@217 5490 }
bgneal@217 5491 }
bgneal@45 5492 return r;
bgneal@45 5493 },
bgneal@45 5494
bgneal@45 5495 setRng : function(r) {
bgneal@45 5496 var s, t = this;
bgneal@217 5497
bgneal@45 5498 if (!t.tridentSel) {
bgneal@45 5499 s = t.getSel();
bgneal@45 5500
bgneal@45 5501 if (s) {
bgneal@217 5502 t.explicitRange = r;
bgneal@45 5503 s.removeAllRanges();
bgneal@45 5504 s.addRange(r);
bgneal@217 5505 t.selectedRange = s.getRangeAt(0);
bgneal@45 5506 }
bgneal@45 5507 } else {
bgneal@45 5508 // Is W3C Range
bgneal@45 5509 if (r.cloneRange) {
bgneal@45 5510 t.tridentSel.addRange(r);
bgneal@45 5511 return;
bgneal@45 5512 }
bgneal@45 5513
bgneal@45 5514 // Is IE specific range
bgneal@45 5515 try {
bgneal@45 5516 r.select();
bgneal@45 5517 } catch (ex) {
bgneal@45 5518 // Needed for some odd IE bug #1843306
bgneal@45 5519 }
bgneal@45 5520 }
bgneal@45 5521 },
bgneal@45 5522
bgneal@45 5523 setNode : function(n) {
bgneal@45 5524 var t = this;
bgneal@45 5525
bgneal@45 5526 t.setContent(t.dom.getOuterHTML(n));
bgneal@45 5527
bgneal@45 5528 return n;
bgneal@45 5529 },
bgneal@45 5530
bgneal@45 5531 getNode : function() {
bgneal@183 5532 var t = this, rng = t.getRng(), sel = t.getSel(), elm;
bgneal@183 5533
bgneal@183 5534 if (rng.setStart) {
bgneal@45 5535 // Range maybe lost after the editor is made visible again
bgneal@183 5536 if (!rng)
bgneal@45 5537 return t.dom.getRoot();
bgneal@45 5538
bgneal@183 5539 elm = rng.commonAncestorContainer;
bgneal@45 5540
bgneal@45 5541 // Handle selection a image or other control like element such as anchors
bgneal@183 5542 if (!rng.collapsed) {
bgneal@183 5543 if (rng.startContainer == rng.endContainer) {
bgneal@183 5544 if (rng.startOffset - rng.endOffset < 2) {
bgneal@183 5545 if (rng.startContainer.hasChildNodes())
bgneal@183 5546 elm = rng.startContainer.childNodes[rng.startOffset];
bgneal@183 5547 }
bgneal@183 5548 }
bgneal@183 5549
bgneal@45 5550 // If the anchor node is a element instead of a text node then return this element
bgneal@183 5551 if (tinymce.isWebKit && sel.anchorNode && sel.anchorNode.nodeType == 1)
bgneal@183 5552 return sel.anchorNode.childNodes[sel.anchorOffset];
bgneal@183 5553 }
bgneal@183 5554
bgneal@183 5555 if (elm && elm.nodeType == 3)
bgneal@183 5556 return elm.parentNode;
bgneal@183 5557
bgneal@183 5558 return elm;
bgneal@183 5559 }
bgneal@183 5560
bgneal@183 5561 return rng.item ? rng.item(0) : rng.parentElement();
bgneal@45 5562 },
bgneal@45 5563
bgneal@45 5564 getSelectedBlocks : function(st, en) {
bgneal@45 5565 var t = this, dom = t.dom, sb, eb, n, bl = [];
bgneal@45 5566
bgneal@45 5567 sb = dom.getParent(st || t.getStart(), dom.isBlock);
bgneal@45 5568 eb = dom.getParent(en || t.getEnd(), dom.isBlock);
bgneal@45 5569
bgneal@45 5570 if (sb)
bgneal@45 5571 bl.push(sb);
bgneal@45 5572
bgneal@45 5573 if (sb && eb && sb != eb) {
bgneal@45 5574 n = sb;
bgneal@45 5575
bgneal@45 5576 while ((n = n.nextSibling) && n != eb) {
bgneal@45 5577 if (dom.isBlock(n))
bgneal@45 5578 bl.push(n);
bgneal@45 5579 }
bgneal@45 5580 }
bgneal@45 5581
bgneal@45 5582 if (eb && sb != eb)
bgneal@45 5583 bl.push(eb);
bgneal@45 5584
bgneal@45 5585 return bl;
bgneal@45 5586 },
bgneal@45 5587
bgneal@45 5588 destroy : function(s) {
bgneal@45 5589 var t = this;
bgneal@45 5590
bgneal@45 5591 t.win = null;
bgneal@45 5592
bgneal@183 5593 if (t.tridentSel)
bgneal@183 5594 t.tridentSel.destroy();
bgneal@183 5595
bgneal@45 5596 // Manual destroy then remove unload handler
bgneal@45 5597 if (!s)
bgneal@45 5598 tinymce.removeUnload(t.destroy);
bgneal@45 5599 }
bgneal@183 5600 });
bgneal@45 5601 })(tinymce);
bgneal@183 5602
bgneal@45 5603 (function(tinymce) {
bgneal@45 5604 tinymce.create('tinymce.dom.XMLWriter', {
bgneal@45 5605 node : null,
bgneal@45 5606
bgneal@45 5607 XMLWriter : function(s) {
bgneal@45 5608 // Get XML document
bgneal@45 5609 function getXML() {
bgneal@45 5610 var i = document.implementation;
bgneal@45 5611
bgneal@45 5612 if (!i || !i.createDocument) {
bgneal@45 5613 // Try IE objects
bgneal@45 5614 try {return new ActiveXObject('MSXML2.DOMDocument');} catch (ex) {}
bgneal@45 5615 try {return new ActiveXObject('Microsoft.XmlDom');} catch (ex) {}
bgneal@45 5616 } else
bgneal@45 5617 return i.createDocument('', '', null);
bgneal@45 5618 };
bgneal@45 5619
bgneal@45 5620 this.doc = getXML();
bgneal@45 5621
bgneal@45 5622 // Since Opera and WebKit doesn't escape > into &gt; we need to do it our self to normalize the output for all browsers
bgneal@45 5623 this.valid = tinymce.isOpera || tinymce.isWebKit;
bgneal@45 5624
bgneal@45 5625 this.reset();
bgneal@45 5626 },
bgneal@45 5627
bgneal@45 5628 reset : function() {
bgneal@45 5629 var t = this, d = t.doc;
bgneal@45 5630
bgneal@45 5631 if (d.firstChild)
bgneal@45 5632 d.removeChild(d.firstChild);
bgneal@45 5633
bgneal@45 5634 t.node = d.appendChild(d.createElement("html"));
bgneal@45 5635 },
bgneal@45 5636
bgneal@45 5637 writeStartElement : function(n) {
bgneal@45 5638 var t = this;
bgneal@45 5639
bgneal@45 5640 t.node = t.node.appendChild(t.doc.createElement(n));
bgneal@45 5641 },
bgneal@45 5642
bgneal@45 5643 writeAttribute : function(n, v) {
bgneal@45 5644 if (this.valid)
bgneal@45 5645 v = v.replace(/>/g, '%MCGT%');
bgneal@45 5646
bgneal@45 5647 this.node.setAttribute(n, v);
bgneal@45 5648 },
bgneal@45 5649
bgneal@45 5650 writeEndElement : function() {
bgneal@45 5651 this.node = this.node.parentNode;
bgneal@45 5652 },
bgneal@45 5653
bgneal@45 5654 writeFullEndElement : function() {
bgneal@45 5655 var t = this, n = t.node;
bgneal@45 5656
bgneal@45 5657 n.appendChild(t.doc.createTextNode(""));
bgneal@45 5658 t.node = n.parentNode;
bgneal@45 5659 },
bgneal@45 5660
bgneal@45 5661 writeText : function(v) {
bgneal@45 5662 if (this.valid)
bgneal@45 5663 v = v.replace(/>/g, '%MCGT%');
bgneal@45 5664
bgneal@45 5665 this.node.appendChild(this.doc.createTextNode(v));
bgneal@45 5666 },
bgneal@45 5667
bgneal@45 5668 writeCDATA : function(v) {
bgneal@183 5669 this.node.appendChild(this.doc.createCDATASection(v));
bgneal@45 5670 },
bgneal@45 5671
bgneal@45 5672 writeComment : function(v) {
bgneal@45 5673 // Fix for bug #2035694
bgneal@45 5674 if (tinymce.isIE)
bgneal@45 5675 v = v.replace(/^\-|\-$/g, ' ');
bgneal@45 5676
bgneal@45 5677 this.node.appendChild(this.doc.createComment(v.replace(/\-\-/g, ' ')));
bgneal@45 5678 },
bgneal@45 5679
bgneal@45 5680 getContent : function() {
bgneal@45 5681 var h;
bgneal@45 5682
bgneal@45 5683 h = this.doc.xml || new XMLSerializer().serializeToString(this.doc);
bgneal@45 5684 h = h.replace(/<\?[^?]+\?>|<html>|<\/html>|<html\/>|<!DOCTYPE[^>]+>/g, '');
bgneal@45 5685 h = h.replace(/ ?\/>/g, ' />');
bgneal@45 5686
bgneal@45 5687 if (this.valid)
bgneal@45 5688 h = h.replace(/\%MCGT%/g, '&gt;');
bgneal@45 5689
bgneal@45 5690 return h;
bgneal@45 5691 }
bgneal@183 5692 });
bgneal@45 5693 })(tinymce);
bgneal@183 5694
bgneal@45 5695 (function(tinymce) {
bgneal@45 5696 tinymce.create('tinymce.dom.StringWriter', {
bgneal@45 5697 str : null,
bgneal@45 5698 tags : null,
bgneal@45 5699 count : 0,
bgneal@45 5700 settings : null,
bgneal@45 5701 indent : null,
bgneal@45 5702
bgneal@45 5703 StringWriter : function(s) {
bgneal@45 5704 this.settings = tinymce.extend({
bgneal@45 5705 indent_char : ' ',
bgneal@183 5706 indentation : 0
bgneal@45 5707 }, s);
bgneal@45 5708
bgneal@45 5709 this.reset();
bgneal@45 5710 },
bgneal@45 5711
bgneal@45 5712 reset : function() {
bgneal@45 5713 this.indent = '';
bgneal@45 5714 this.str = "";
bgneal@45 5715 this.tags = [];
bgneal@45 5716 this.count = 0;
bgneal@45 5717 },
bgneal@45 5718
bgneal@45 5719 writeStartElement : function(n) {
bgneal@45 5720 this._writeAttributesEnd();
bgneal@45 5721 this.writeRaw('<' + n);
bgneal@45 5722 this.tags.push(n);
bgneal@45 5723 this.inAttr = true;
bgneal@45 5724 this.count++;
bgneal@45 5725 this.elementCount = this.count;
bgneal@45 5726 },
bgneal@45 5727
bgneal@45 5728 writeAttribute : function(n, v) {
bgneal@45 5729 var t = this;
bgneal@45 5730
bgneal@45 5731 t.writeRaw(" " + t.encode(n) + '="' + t.encode(v) + '"');
bgneal@45 5732 },
bgneal@45 5733
bgneal@45 5734 writeEndElement : function() {
bgneal@45 5735 var n;
bgneal@45 5736
bgneal@45 5737 if (this.tags.length > 0) {
bgneal@45 5738 n = this.tags.pop();
bgneal@45 5739
bgneal@45 5740 if (this._writeAttributesEnd(1))
bgneal@45 5741 this.writeRaw('</' + n + '>');
bgneal@45 5742
bgneal@45 5743 if (this.settings.indentation > 0)
bgneal@45 5744 this.writeRaw('\n');
bgneal@45 5745 }
bgneal@45 5746 },
bgneal@45 5747
bgneal@45 5748 writeFullEndElement : function() {
bgneal@45 5749 if (this.tags.length > 0) {
bgneal@45 5750 this._writeAttributesEnd();
bgneal@45 5751 this.writeRaw('</' + this.tags.pop() + '>');
bgneal@45 5752
bgneal@45 5753 if (this.settings.indentation > 0)
bgneal@45 5754 this.writeRaw('\n');
bgneal@45 5755 }
bgneal@45 5756 },
bgneal@45 5757
bgneal@45 5758 writeText : function(v) {
bgneal@45 5759 this._writeAttributesEnd();
bgneal@45 5760 this.writeRaw(this.encode(v));
bgneal@45 5761 this.count++;
bgneal@45 5762 },
bgneal@45 5763
bgneal@45 5764 writeCDATA : function(v) {
bgneal@45 5765 this._writeAttributesEnd();
bgneal@45 5766 this.writeRaw('<![CDATA[' + v + ']]>');
bgneal@45 5767 this.count++;
bgneal@45 5768 },
bgneal@45 5769
bgneal@45 5770 writeComment : function(v) {
bgneal@45 5771 this._writeAttributesEnd();
bgneal@45 5772 this.writeRaw('<!-- ' + v + '-->');
bgneal@45 5773 this.count++;
bgneal@45 5774 },
bgneal@45 5775
bgneal@45 5776 writeRaw : function(v) {
bgneal@45 5777 this.str += v;
bgneal@45 5778 },
bgneal@45 5779
bgneal@45 5780 encode : function(s) {
bgneal@45 5781 return s.replace(/[<>&"]/g, function(v) {
bgneal@45 5782 switch (v) {
bgneal@45 5783 case '<':
bgneal@45 5784 return '&lt;';
bgneal@45 5785
bgneal@45 5786 case '>':
bgneal@45 5787 return '&gt;';
bgneal@45 5788
bgneal@45 5789 case '&':
bgneal@45 5790 return '&amp;';
bgneal@45 5791
bgneal@45 5792 case '"':
bgneal@45 5793 return '&quot;';
bgneal@45 5794 }
bgneal@45 5795
bgneal@45 5796 return v;
bgneal@45 5797 });
bgneal@45 5798 },
bgneal@45 5799
bgneal@45 5800 getContent : function() {
bgneal@45 5801 return this.str;
bgneal@45 5802 },
bgneal@45 5803
bgneal@45 5804 _writeAttributesEnd : function(s) {
bgneal@45 5805 if (!this.inAttr)
bgneal@45 5806 return;
bgneal@45 5807
bgneal@45 5808 this.inAttr = false;
bgneal@45 5809
bgneal@45 5810 if (s && this.elementCount == this.count) {
bgneal@45 5811 this.writeRaw(' />');
bgneal@45 5812 return false;
bgneal@45 5813 }
bgneal@45 5814
bgneal@45 5815 this.writeRaw('>');
bgneal@45 5816
bgneal@45 5817 return true;
bgneal@45 5818 }
bgneal@183 5819 });
bgneal@45 5820 })(tinymce);
bgneal@183 5821
bgneal@45 5822 (function(tinymce) {
bgneal@45 5823 // Shorten names
bgneal@45 5824 var extend = tinymce.extend, each = tinymce.each, Dispatcher = tinymce.util.Dispatcher, isIE = tinymce.isIE, isGecko = tinymce.isGecko;
bgneal@45 5825
bgneal@45 5826 function wildcardToRE(s) {
bgneal@45 5827 return s.replace(/([?+*])/g, '.$1');
bgneal@45 5828 };
bgneal@45 5829
bgneal@45 5830 tinymce.create('tinymce.dom.Serializer', {
bgneal@45 5831 Serializer : function(s) {
bgneal@45 5832 var t = this;
bgneal@45 5833
bgneal@45 5834 t.key = 0;
bgneal@45 5835 t.onPreProcess = new Dispatcher(t);
bgneal@45 5836 t.onPostProcess = new Dispatcher(t);
bgneal@45 5837
bgneal@45 5838 try {
bgneal@45 5839 t.writer = new tinymce.dom.XMLWriter();
bgneal@45 5840 } catch (ex) {
bgneal@45 5841 // IE might throw exception if ActiveX is disabled so we then switch to the slightly slower StringWriter
bgneal@45 5842 t.writer = new tinymce.dom.StringWriter();
bgneal@45 5843 }
bgneal@45 5844
bgneal@45 5845 // Default settings
bgneal@45 5846 t.settings = s = extend({
bgneal@45 5847 dom : tinymce.DOM,
bgneal@45 5848 valid_nodes : 0,
bgneal@45 5849 node_filter : 0,
bgneal@45 5850 attr_filter : 0,
bgneal@183 5851 invalid_attrs : /^(_mce_|_moz_|sizset|sizcache)/,
bgneal@183 5852 closed : /^(br|hr|input|meta|img|link|param|area)$/,
bgneal@45 5853 entity_encoding : 'named',
bgneal@45 5854 entities : '160,nbsp,161,iexcl,162,cent,163,pound,164,curren,165,yen,166,brvbar,167,sect,168,uml,169,copy,170,ordf,171,laquo,172,not,173,shy,174,reg,175,macr,176,deg,177,plusmn,178,sup2,179,sup3,180,acute,181,micro,182,para,183,middot,184,cedil,185,sup1,186,ordm,187,raquo,188,frac14,189,frac12,190,frac34,191,iquest,192,Agrave,193,Aacute,194,Acirc,195,Atilde,196,Auml,197,Aring,198,AElig,199,Ccedil,200,Egrave,201,Eacute,202,Ecirc,203,Euml,204,Igrave,205,Iacute,206,Icirc,207,Iuml,208,ETH,209,Ntilde,210,Ograve,211,Oacute,212,Ocirc,213,Otilde,214,Ouml,215,times,216,Oslash,217,Ugrave,218,Uacute,219,Ucirc,220,Uuml,221,Yacute,222,THORN,223,szlig,224,agrave,225,aacute,226,acirc,227,atilde,228,auml,229,aring,230,aelig,231,ccedil,232,egrave,233,eacute,234,ecirc,235,euml,236,igrave,237,iacute,238,icirc,239,iuml,240,eth,241,ntilde,242,ograve,243,oacute,244,ocirc,245,otilde,246,ouml,247,divide,248,oslash,249,ugrave,250,uacute,251,ucirc,252,uuml,253,yacute,254,thorn,255,yuml,402,fnof,913,Alpha,914,Beta,915,Gamma,916,Delta,917,Epsilon,918,Zeta,919,Eta,920,Theta,921,Iota,922,Kappa,923,Lambda,924,Mu,925,Nu,926,Xi,927,Omicron,928,Pi,929,Rho,931,Sigma,932,Tau,933,Upsilon,934,Phi,935,Chi,936,Psi,937,Omega,945,alpha,946,beta,947,gamma,948,delta,949,epsilon,950,zeta,951,eta,952,theta,953,iota,954,kappa,955,lambda,956,mu,957,nu,958,xi,959,omicron,960,pi,961,rho,962,sigmaf,963,sigma,964,tau,965,upsilon,966,phi,967,chi,968,psi,969,omega,977,thetasym,978,upsih,982,piv,8226,bull,8230,hellip,8242,prime,8243,Prime,8254,oline,8260,frasl,8472,weierp,8465,image,8476,real,8482,trade,8501,alefsym,8592,larr,8593,uarr,8594,rarr,8595,darr,8596,harr,8629,crarr,8656,lArr,8657,uArr,8658,rArr,8659,dArr,8660,hArr,8704,forall,8706,part,8707,exist,8709,empty,8711,nabla,8712,isin,8713,notin,8715,ni,8719,prod,8721,sum,8722,minus,8727,lowast,8730,radic,8733,prop,8734,infin,8736,ang,8743,and,8744,or,8745,cap,8746,cup,8747,int,8756,there4,8764,sim,8773,cong,8776,asymp,8800,ne,8801,equiv,8804,le,8805,ge,8834,sub,8835,sup,8836,nsub,8838,sube,8839,supe,8853,oplus,8855,otimes,8869,perp,8901,sdot,8968,lceil,8969,rceil,8970,lfloor,8971,rfloor,9001,lang,9002,rang,9674,loz,9824,spades,9827,clubs,9829,hearts,9830,diams,338,OElig,339,oelig,352,Scaron,353,scaron,376,Yuml,710,circ,732,tilde,8194,ensp,8195,emsp,8201,thinsp,8204,zwnj,8205,zwj,8206,lrm,8207,rlm,8211,ndash,8212,mdash,8216,lsquo,8217,rsquo,8218,sbquo,8220,ldquo,8221,rdquo,8222,bdquo,8224,dagger,8225,Dagger,8240,permil,8249,lsaquo,8250,rsaquo,8364,euro',
bgneal@45 5855 valid_elements : '*[*]',
bgneal@45 5856 extended_valid_elements : 0,
bgneal@45 5857 invalid_elements : 0,
bgneal@45 5858 fix_table_elements : 1,
bgneal@45 5859 fix_list_elements : true,
bgneal@45 5860 fix_content_duplication : true,
bgneal@45 5861 convert_fonts_to_spans : false,
bgneal@45 5862 font_size_classes : 0,
bgneal@45 5863 apply_source_formatting : 0,
bgneal@45 5864 indent_mode : 'simple',
bgneal@45 5865 indent_char : '\t',
bgneal@45 5866 indent_levels : 1,
bgneal@45 5867 remove_linebreaks : 1,
bgneal@45 5868 remove_redundant_brs : 1,
bgneal@45 5869 element_format : 'xhtml'
bgneal@45 5870 }, s);
bgneal@45 5871
bgneal@45 5872 t.dom = s.dom;
bgneal@183 5873 t.schema = s.schema;
bgneal@183 5874
bgneal@183 5875 // Use raw entities if no entities are defined
bgneal@183 5876 if (s.entity_encoding == 'named' && !s.entities)
bgneal@183 5877 s.entity_encoding = 'raw';
bgneal@45 5878
bgneal@45 5879 if (s.remove_redundant_brs) {
bgneal@45 5880 t.onPostProcess.add(function(se, o) {
bgneal@183 5881 // Remove single BR at end of block elements since they get rendered
bgneal@183 5882 o.content = o.content.replace(/(<br \/>\s*)+<\/(p|h[1-6]|div|li)>/gi, function(a, b, c) {
bgneal@183 5883 // Check if it's a single element
bgneal@183 5884 if (/^<br \/>\s*<\//.test(a))
bgneal@183 5885 return '</' + c + '>';
bgneal@183 5886
bgneal@183 5887 return a;
bgneal@183 5888 });
bgneal@45 5889 });
bgneal@45 5890 }
bgneal@45 5891
bgneal@45 5892 // Remove XHTML element endings i.e. produce crap :) XHTML is better
bgneal@45 5893 if (s.element_format == 'html') {
bgneal@45 5894 t.onPostProcess.add(function(se, o) {
bgneal@45 5895 o.content = o.content.replace(/<([^>]+) \/>/g, '<$1>');
bgneal@45 5896 });
bgneal@45 5897 }
bgneal@45 5898
bgneal@45 5899 if (s.fix_list_elements) {
bgneal@45 5900 t.onPreProcess.add(function(se, o) {
bgneal@45 5901 var nl, x, a = ['ol', 'ul'], i, n, p, r = /^(OL|UL)$/, np;
bgneal@45 5902
bgneal@45 5903 function prevNode(e, n) {
bgneal@45 5904 var a = n.split(','), i;
bgneal@45 5905
bgneal@45 5906 while ((e = e.previousSibling) != null) {
bgneal@45 5907 for (i=0; i<a.length; i++) {
bgneal@45 5908 if (e.nodeName == a[i])
bgneal@45 5909 return e;
bgneal@45 5910 }
bgneal@45 5911 }
bgneal@45 5912
bgneal@45 5913 return null;
bgneal@45 5914 };
bgneal@45 5915
bgneal@45 5916 for (x=0; x<a.length; x++) {
bgneal@45 5917 nl = t.dom.select(a[x], o.node);
bgneal@45 5918
bgneal@45 5919 for (i=0; i<nl.length; i++) {
bgneal@45 5920 n = nl[i];
bgneal@45 5921 p = n.parentNode;
bgneal@45 5922
bgneal@45 5923 if (r.test(p.nodeName)) {
bgneal@45 5924 np = prevNode(n, 'LI');
bgneal@45 5925
bgneal@45 5926 if (!np) {
bgneal@45 5927 np = t.dom.create('li');
bgneal@45 5928 np.innerHTML = '&nbsp;';
bgneal@45 5929 np.appendChild(n);
bgneal@45 5930 p.insertBefore(np, p.firstChild);
bgneal@45 5931 } else
bgneal@45 5932 np.appendChild(n);
bgneal@45 5933 }
bgneal@45 5934 }
bgneal@45 5935 }
bgneal@45 5936 });
bgneal@45 5937 }
bgneal@45 5938
bgneal@45 5939 if (s.fix_table_elements) {
bgneal@45 5940 t.onPreProcess.add(function(se, o) {
bgneal@183 5941 // Since Opera will crash if you attach the node to a dynamic document we need to brrowser sniff a specific build
bgneal@183 5942 // so Opera users with an older version will have to live with less compaible output not much we can do here
bgneal@183 5943 if (!tinymce.isOpera || opera.buildNumber() >= 1767) {
bgneal@183 5944 each(t.dom.select('p table', o.node).reverse(), function(n) {
bgneal@183 5945 var parent = t.dom.getParent(n.parentNode, 'table,p');
bgneal@183 5946
bgneal@183 5947 if (parent.nodeName != 'TABLE') {
bgneal@183 5948 try {
bgneal@183 5949 t.dom.split(parent, n);
bgneal@183 5950 } catch (ex) {
bgneal@183 5951 // IE can sometimes fire an unknown runtime error so we just ignore it
bgneal@183 5952 }
bgneal@183 5953 }
bgneal@183 5954 });
bgneal@183 5955 }
bgneal@45 5956 });
bgneal@45 5957 }
bgneal@45 5958 },
bgneal@45 5959
bgneal@45 5960 setEntities : function(s) {
bgneal@183 5961 var t = this, a, i, l = {}, v;
bgneal@45 5962
bgneal@45 5963 // No need to setup more than once
bgneal@45 5964 if (t.entityLookup)
bgneal@45 5965 return;
bgneal@45 5966
bgneal@45 5967 // Build regex and lookup array
bgneal@45 5968 a = s.split(',');
bgneal@45 5969 for (i = 0; i < a.length; i += 2) {
bgneal@45 5970 v = a[i];
bgneal@45 5971
bgneal@45 5972 // Don't add default &amp; &quot; etc.
bgneal@45 5973 if (v == 34 || v == 38 || v == 60 || v == 62)
bgneal@45 5974 continue;
bgneal@45 5975
bgneal@45 5976 l[String.fromCharCode(a[i])] = a[i + 1];
bgneal@45 5977
bgneal@45 5978 v = parseInt(a[i]).toString(16);
bgneal@183 5979 }
bgneal@183 5980
bgneal@45 5981 t.entityLookup = l;
bgneal@45 5982 },
bgneal@45 5983
bgneal@45 5984 setRules : function(s) {
bgneal@45 5985 var t = this;
bgneal@45 5986
bgneal@45 5987 t._setup();
bgneal@45 5988 t.rules = {};
bgneal@45 5989 t.wildRules = [];
bgneal@45 5990 t.validElements = {};
bgneal@45 5991
bgneal@45 5992 return t.addRules(s);
bgneal@45 5993 },
bgneal@45 5994
bgneal@45 5995 addRules : function(s) {
bgneal@45 5996 var t = this, dr;
bgneal@45 5997
bgneal@45 5998 if (!s)
bgneal@45 5999 return;
bgneal@45 6000
bgneal@45 6001 t._setup();
bgneal@45 6002
bgneal@45 6003 each(s.split(','), function(s) {
bgneal@45 6004 var p = s.split(/\[|\]/), tn = p[0].split('/'), ra, at, wat, va = [];
bgneal@45 6005
bgneal@45 6006 // Extend with default rules
bgneal@45 6007 if (dr)
bgneal@45 6008 at = tinymce.extend([], dr.attribs);
bgneal@45 6009
bgneal@45 6010 // Parse attributes
bgneal@45 6011 if (p.length > 1) {
bgneal@45 6012 each(p[1].split('|'), function(s) {
bgneal@45 6013 var ar = {}, i;
bgneal@45 6014
bgneal@45 6015 at = at || [];
bgneal@45 6016
bgneal@45 6017 // Parse attribute rule
bgneal@45 6018 s = s.replace(/::/g, '~');
bgneal@45 6019 s = /^([!\-])?([\w*.?~_\-]+|)([=:<])?(.+)?$/.exec(s);
bgneal@45 6020 s[2] = s[2].replace(/~/g, ':');
bgneal@45 6021
bgneal@45 6022 // Add required attributes
bgneal@45 6023 if (s[1] == '!') {
bgneal@45 6024 ra = ra || [];
bgneal@45 6025 ra.push(s[2]);
bgneal@45 6026 }
bgneal@45 6027
bgneal@45 6028 // Remove inherited attributes
bgneal@45 6029 if (s[1] == '-') {
bgneal@45 6030 for (i = 0; i <at.length; i++) {
bgneal@45 6031 if (at[i].name == s[2]) {
bgneal@45 6032 at.splice(i, 1);
bgneal@45 6033 return;
bgneal@45 6034 }
bgneal@45 6035 }
bgneal@45 6036 }
bgneal@45 6037
bgneal@45 6038 switch (s[3]) {
bgneal@45 6039 // Add default attrib values
bgneal@45 6040 case '=':
bgneal@45 6041 ar.defaultVal = s[4] || '';
bgneal@45 6042 break;
bgneal@45 6043
bgneal@45 6044 // Add forced attrib values
bgneal@45 6045 case ':':
bgneal@45 6046 ar.forcedVal = s[4];
bgneal@45 6047 break;
bgneal@45 6048
bgneal@45 6049 // Add validation values
bgneal@45 6050 case '<':
bgneal@45 6051 ar.validVals = s[4].split('?');
bgneal@45 6052 break;
bgneal@45 6053 }
bgneal@45 6054
bgneal@45 6055 if (/[*.?]/.test(s[2])) {
bgneal@45 6056 wat = wat || [];
bgneal@45 6057 ar.nameRE = new RegExp('^' + wildcardToRE(s[2]) + '$');
bgneal@45 6058 wat.push(ar);
bgneal@45 6059 } else {
bgneal@45 6060 ar.name = s[2];
bgneal@45 6061 at.push(ar);
bgneal@45 6062 }
bgneal@45 6063
bgneal@45 6064 va.push(s[2]);
bgneal@45 6065 });
bgneal@45 6066 }
bgneal@45 6067
bgneal@45 6068 // Handle element names
bgneal@45 6069 each(tn, function(s, i) {
bgneal@45 6070 var pr = s.charAt(0), x = 1, ru = {};
bgneal@45 6071
bgneal@45 6072 // Extend with default rule data
bgneal@45 6073 if (dr) {
bgneal@45 6074 if (dr.noEmpty)
bgneal@45 6075 ru.noEmpty = dr.noEmpty;
bgneal@45 6076
bgneal@45 6077 if (dr.fullEnd)
bgneal@45 6078 ru.fullEnd = dr.fullEnd;
bgneal@45 6079
bgneal@45 6080 if (dr.padd)
bgneal@45 6081 ru.padd = dr.padd;
bgneal@45 6082 }
bgneal@45 6083
bgneal@45 6084 // Handle prefixes
bgneal@45 6085 switch (pr) {
bgneal@45 6086 case '-':
bgneal@45 6087 ru.noEmpty = true;
bgneal@45 6088 break;
bgneal@45 6089
bgneal@45 6090 case '+':
bgneal@45 6091 ru.fullEnd = true;
bgneal@45 6092 break;
bgneal@45 6093
bgneal@45 6094 case '#':
bgneal@45 6095 ru.padd = true;
bgneal@45 6096 break;
bgneal@45 6097
bgneal@45 6098 default:
bgneal@45 6099 x = 0;
bgneal@45 6100 }
bgneal@45 6101
bgneal@45 6102 tn[i] = s = s.substring(x);
bgneal@45 6103 t.validElements[s] = 1;
bgneal@45 6104
bgneal@45 6105 // Add element name or element regex
bgneal@45 6106 if (/[*.?]/.test(tn[0])) {
bgneal@45 6107 ru.nameRE = new RegExp('^' + wildcardToRE(tn[0]) + '$');
bgneal@45 6108 t.wildRules = t.wildRules || {};
bgneal@45 6109 t.wildRules.push(ru);
bgneal@45 6110 } else {
bgneal@45 6111 ru.name = tn[0];
bgneal@45 6112
bgneal@45 6113 // Store away default rule
bgneal@45 6114 if (tn[0] == '@')
bgneal@45 6115 dr = ru;
bgneal@45 6116
bgneal@45 6117 t.rules[s] = ru;
bgneal@45 6118 }
bgneal@45 6119
bgneal@45 6120 ru.attribs = at;
bgneal@45 6121
bgneal@45 6122 if (ra)
bgneal@45 6123 ru.requiredAttribs = ra;
bgneal@45 6124
bgneal@45 6125 if (wat) {
bgneal@45 6126 // Build valid attributes regexp
bgneal@45 6127 s = '';
bgneal@45 6128 each(va, function(v) {
bgneal@45 6129 if (s)
bgneal@45 6130 s += '|';
bgneal@45 6131
bgneal@45 6132 s += '(' + wildcardToRE(v) + ')';
bgneal@45 6133 });
bgneal@45 6134 ru.validAttribsRE = new RegExp('^' + s.toLowerCase() + '$');
bgneal@45 6135 ru.wildAttribs = wat;
bgneal@45 6136 }
bgneal@45 6137 });
bgneal@45 6138 });
bgneal@45 6139
bgneal@45 6140 // Build valid elements regexp
bgneal@45 6141 s = '';
bgneal@45 6142 each(t.validElements, function(v, k) {
bgneal@45 6143 if (s)
bgneal@45 6144 s += '|';
bgneal@45 6145
bgneal@45 6146 if (k != '@')
bgneal@45 6147 s += k;
bgneal@45 6148 });
bgneal@45 6149 t.validElementsRE = new RegExp('^(' + wildcardToRE(s.toLowerCase()) + ')$');
bgneal@45 6150
bgneal@45 6151 //console.debug(t.validElementsRE.toString());
bgneal@45 6152 //console.dir(t.rules);
bgneal@45 6153 //console.dir(t.wildRules);
bgneal@45 6154 },
bgneal@45 6155
bgneal@45 6156 findRule : function(n) {
bgneal@45 6157 var t = this, rl = t.rules, i, r;
bgneal@45 6158
bgneal@45 6159 t._setup();
bgneal@45 6160
bgneal@45 6161 // Exact match
bgneal@45 6162 r = rl[n];
bgneal@45 6163 if (r)
bgneal@45 6164 return r;
bgneal@45 6165
bgneal@45 6166 // Try wildcards
bgneal@45 6167 rl = t.wildRules;
bgneal@45 6168 for (i = 0; i < rl.length; i++) {
bgneal@45 6169 if (rl[i].nameRE.test(n))
bgneal@45 6170 return rl[i];
bgneal@45 6171 }
bgneal@45 6172
bgneal@45 6173 return null;
bgneal@45 6174 },
bgneal@45 6175
bgneal@45 6176 findAttribRule : function(ru, n) {
bgneal@45 6177 var i, wa = ru.wildAttribs;
bgneal@45 6178
bgneal@45 6179 for (i = 0; i < wa.length; i++) {
bgneal@45 6180 if (wa[i].nameRE.test(n))
bgneal@45 6181 return wa[i];
bgneal@45 6182 }
bgneal@45 6183
bgneal@45 6184 return null;
bgneal@45 6185 },
bgneal@45 6186
bgneal@45 6187 serialize : function(n, o) {
bgneal@183 6188 var h, t = this, doc, oldDoc, impl, selected;
bgneal@45 6189
bgneal@45 6190 t._setup();
bgneal@45 6191 o = o || {};
bgneal@45 6192 o.format = o.format || 'html';
bgneal@45 6193 t.processObj = o;
bgneal@183 6194
bgneal@183 6195 // IE looses the selected attribute on option elements so we need to store it
bgneal@183 6196 // See: http://support.microsoft.com/kb/829907
bgneal@183 6197 if (isIE) {
bgneal@183 6198 selected = [];
bgneal@183 6199 each(n.getElementsByTagName('option'), function(n) {
bgneal@183 6200 var v = t.dom.getAttrib(n, 'selected');
bgneal@183 6201
bgneal@183 6202 selected.push(v ? v : null);
bgneal@183 6203 });
bgneal@183 6204 }
bgneal@183 6205
bgneal@45 6206 n = n.cloneNode(true);
bgneal@183 6207
bgneal@183 6208 // IE looses the selected attribute on option elements so we need to restore it
bgneal@183 6209 if (isIE) {
bgneal@183 6210 each(n.getElementsByTagName('option'), function(n, i) {
bgneal@183 6211 t.dom.setAttrib(n, 'selected', selected[i]);
bgneal@183 6212 });
bgneal@183 6213 }
bgneal@183 6214
bgneal@183 6215 // Nodes needs to be attached to something in WebKit/Opera
bgneal@183 6216 // Older builds of Opera crashes if you attach the node to an document created dynamically
bgneal@183 6217 // and since we can't feature detect a crash we need to sniff the acutal build number
bgneal@183 6218 // This fix will make DOM ranges and make Sizzle happy!
bgneal@183 6219 impl = n.ownerDocument.implementation;
bgneal@183 6220 if (impl.createHTMLDocument && (tinymce.isOpera && opera.buildNumber() >= 1767)) {
bgneal@183 6221 // Create an empty HTML document
bgneal@183 6222 doc = impl.createHTMLDocument("");
bgneal@183 6223
bgneal@183 6224 // Add the element or it's children if it's a body element to the new document
bgneal@183 6225 each(n.nodeName == 'BODY' ? n.childNodes : [n], function(node) {
bgneal@183 6226 doc.body.appendChild(doc.importNode(node, true));
bgneal@183 6227 });
bgneal@183 6228
bgneal@183 6229 // Grab first child or body element for serialization
bgneal@183 6230 if (n.nodeName != 'BODY')
bgneal@183 6231 n = doc.body.firstChild;
bgneal@183 6232 else
bgneal@183 6233 n = doc.body;
bgneal@183 6234
bgneal@183 6235 // set the new document in DOMUtils so createElement etc works
bgneal@183 6236 oldDoc = t.dom.doc;
bgneal@183 6237 t.dom.doc = doc;
bgneal@183 6238 }
bgneal@183 6239
bgneal@45 6240 t.key = '' + (parseInt(t.key) + 1);
bgneal@45 6241
bgneal@45 6242 // Pre process
bgneal@45 6243 if (!o.no_events) {
bgneal@45 6244 o.node = n;
bgneal@45 6245 t.onPreProcess.dispatch(t, o);
bgneal@45 6246 }
bgneal@45 6247
bgneal@45 6248 // Serialize HTML DOM into a string
bgneal@45 6249 t.writer.reset();
bgneal@183 6250 t._info = o;
bgneal@45 6251 t._serializeNode(n, o.getInner);
bgneal@45 6252
bgneal@45 6253 // Post process
bgneal@45 6254 o.content = t.writer.getContent();
bgneal@45 6255
bgneal@183 6256 // Restore the old document if it was changed
bgneal@183 6257 if (oldDoc)
bgneal@183 6258 t.dom.doc = oldDoc;
bgneal@183 6259
bgneal@45 6260 if (!o.no_events)
bgneal@45 6261 t.onPostProcess.dispatch(t, o);
bgneal@45 6262
bgneal@45 6263 t._postProcess(o);
bgneal@45 6264 o.node = null;
bgneal@45 6265
bgneal@45 6266 return tinymce.trim(o.content);
bgneal@45 6267 },
bgneal@45 6268
bgneal@45 6269 // Internal functions
bgneal@45 6270
bgneal@45 6271 _postProcess : function(o) {
bgneal@45 6272 var t = this, s = t.settings, h = o.content, sc = [], p;
bgneal@45 6273
bgneal@45 6274 if (o.format == 'html') {
bgneal@45 6275 // Protect some elements
bgneal@45 6276 p = t._protect({
bgneal@45 6277 content : h,
bgneal@45 6278 patterns : [
bgneal@45 6279 {pattern : /(<script[^>]*>)(.*?)(<\/script>)/g},
bgneal@183 6280 {pattern : /(<noscript[^>]*>)(.*?)(<\/noscript>)/g},
bgneal@45 6281 {pattern : /(<style[^>]*>)(.*?)(<\/style>)/g},
bgneal@45 6282 {pattern : /(<pre[^>]*>)(.*?)(<\/pre>)/g, encode : 1},
bgneal@45 6283 {pattern : /(<!--\[CDATA\[)(.*?)(\]\]-->)/g}
bgneal@45 6284 ]
bgneal@45 6285 });
bgneal@45 6286
bgneal@45 6287 h = p.content;
bgneal@45 6288
bgneal@45 6289 // Entity encode
bgneal@45 6290 if (s.entity_encoding !== 'raw')
bgneal@45 6291 h = t._encode(h);
bgneal@45 6292
bgneal@45 6293 // Use BR instead of &nbsp; padded P elements inside editor and use <p>&nbsp;</p> outside editor
bgneal@45 6294 /* if (o.set)
bgneal@45 6295 h = h.replace(/<p>\s+(&nbsp;|&#160;|\u00a0|<br \/>)\s+<\/p>/g, '<p><br /></p>');
bgneal@45 6296 else
bgneal@45 6297 h = h.replace(/<p>\s+(&nbsp;|&#160;|\u00a0|<br \/>)\s+<\/p>/g, '<p>$1</p>');*/
bgneal@45 6298
bgneal@45 6299 // Since Gecko and Safari keeps whitespace in the DOM we need to
bgneal@45 6300 // remove it inorder to match other browsers. But I think Gecko and Safari is right.
bgneal@45 6301 // This process is only done when getting contents out from the editor.
bgneal@45 6302 if (!o.set) {
bgneal@45 6303 // We need to replace paragraph whitespace with an nbsp before indentation to keep the \u00a0 char
bgneal@45 6304 h = h.replace(/<p>\s+<\/p>|<p([^>]+)>\s+<\/p>/g, s.entity_encoding == 'numeric' ? '<p$1>&#160;</p>' : '<p$1>&nbsp;</p>');
bgneal@45 6305
bgneal@45 6306 if (s.remove_linebreaks) {
bgneal@45 6307 h = h.replace(/\r?\n|\r/g, ' ');
bgneal@45 6308 h = h.replace(/(<[^>]+>)\s+/g, '$1 ');
bgneal@45 6309 h = h.replace(/\s+(<\/[^>]+>)/g, ' $1');
bgneal@45 6310 h = h.replace(/<(p|h[1-6]|blockquote|hr|div|table|tbody|tr|td|body|head|html|title|meta|style|pre|script|link|object) ([^>]+)>\s+/g, '<$1 $2>'); // Trim block start
bgneal@45 6311 h = h.replace(/<(p|h[1-6]|blockquote|hr|div|table|tbody|tr|td|body|head|html|title|meta|style|pre|script|link|object)>\s+/g, '<$1>'); // Trim block start
bgneal@45 6312 h = h.replace(/\s+<\/(p|h[1-6]|blockquote|hr|div|table|tbody|tr|td|body|head|html|title|meta|style|pre|script|link|object)>/g, '</$1>'); // Trim block end
bgneal@45 6313 }
bgneal@45 6314
bgneal@45 6315 // Simple indentation
bgneal@45 6316 if (s.apply_source_formatting && s.indent_mode == 'simple') {
bgneal@45 6317 // Add line breaks before and after block elements
bgneal@45 6318 h = h.replace(/<(\/?)(ul|hr|table|meta|link|tbody|tr|object|body|head|html|map)(|[^>]+)>\s*/g, '\n<$1$2$3>\n');
bgneal@45 6319 h = h.replace(/\s*<(p|h[1-6]|blockquote|div|title|style|pre|script|td|li|area)(|[^>]+)>/g, '\n<$1$2>');
bgneal@45 6320 h = h.replace(/<\/(p|h[1-6]|blockquote|div|title|style|pre|script|td|li)>\s*/g, '</$1>\n');
bgneal@45 6321 h = h.replace(/\n\n/g, '\n');
bgneal@45 6322 }
bgneal@45 6323 }
bgneal@45 6324
bgneal@45 6325 h = t._unprotect(h, p);
bgneal@45 6326
bgneal@45 6327 // Restore CDATA sections
bgneal@45 6328 h = h.replace(/<!--\[CDATA\[([\s\S]+)\]\]-->/g, '<![CDATA[$1]]>');
bgneal@45 6329
bgneal@45 6330 // Restore the \u00a0 character if raw mode is enabled
bgneal@45 6331 if (s.entity_encoding == 'raw')
bgneal@45 6332 h = h.replace(/<p>&nbsp;<\/p>|<p([^>]+)>&nbsp;<\/p>/g, '<p$1>\u00a0</p>');
bgneal@183 6333
bgneal@183 6334 // Restore noscript elements
bgneal@183 6335 h = h.replace(/<noscript([^>]+|)>([\s\S]*?)<\/noscript>/g, function(v, attribs, text) {
bgneal@183 6336 return '<noscript' + attribs + '>' + t.dom.decode(text.replace(/<!--|-->/g, '')) + '</noscript>';
bgneal@183 6337 });
bgneal@45 6338 }
bgneal@45 6339
bgneal@45 6340 o.content = h;
bgneal@45 6341 },
bgneal@45 6342
bgneal@183 6343 _serializeNode : function(n, inner) {
bgneal@183 6344 var t = this, s = t.settings, w = t.writer, hc, el, cn, i, l, a, at, no, v, nn, ru, ar, iv, closed, keep, type;
bgneal@45 6345
bgneal@45 6346 if (!s.node_filter || s.node_filter(n)) {
bgneal@45 6347 switch (n.nodeType) {
bgneal@45 6348 case 1: // Element
bgneal@183 6349 if (n.hasAttribute ? n.hasAttribute('_mce_bogus') : n.getAttribute('_mce_bogus'))
bgneal@45 6350 return;
bgneal@45 6351
bgneal@183 6352 iv = keep = false;
bgneal@45 6353 hc = n.hasChildNodes();
bgneal@183 6354 nn = n.getAttribute('_mce_name') || n.nodeName.toLowerCase();
bgneal@183 6355
bgneal@183 6356 // Get internal type
bgneal@183 6357 type = n.getAttribute('_mce_type');
bgneal@183 6358 if (type) {
bgneal@183 6359 if (!t._info.cleanup) {
bgneal@183 6360 iv = true;
bgneal@183 6361 return;
bgneal@183 6362 } else
bgneal@183 6363 keep = 1;
bgneal@183 6364 }
bgneal@45 6365
bgneal@45 6366 // Add correct prefix on IE
bgneal@45 6367 if (isIE) {
bgneal@45 6368 if (n.scopeName !== 'HTML' && n.scopeName !== 'html')
bgneal@45 6369 nn = n.scopeName + ':' + nn;
bgneal@45 6370 }
bgneal@45 6371
bgneal@45 6372 // Remove mce prefix on IE needed for the abbr element
bgneal@45 6373 if (nn.indexOf('mce:') === 0)
bgneal@45 6374 nn = nn.substring(4);
bgneal@45 6375
bgneal@45 6376 // Check if valid
bgneal@183 6377 if (!keep) {
bgneal@183 6378 if (!t.validElementsRE || !t.validElementsRE.test(nn) || (t.invalidElementsRE && t.invalidElementsRE.test(nn)) || inner) {
bgneal@183 6379 iv = true;
bgneal@183 6380 break;
bgneal@183 6381 }
bgneal@45 6382 }
bgneal@45 6383
bgneal@45 6384 if (isIE) {
bgneal@45 6385 // Fix IE content duplication (DOM can have multiple copies of the same node)
bgneal@45 6386 if (s.fix_content_duplication) {
bgneal@183 6387 if (n._mce_serialized == t.key)
bgneal@45 6388 return;
bgneal@45 6389
bgneal@183 6390 n._mce_serialized = t.key;
bgneal@45 6391 }
bgneal@45 6392
bgneal@45 6393 // IE sometimes adds a / infront of the node name
bgneal@45 6394 if (nn.charAt(0) == '/')
bgneal@45 6395 nn = nn.substring(1);
bgneal@45 6396 } else if (isGecko) {
bgneal@45 6397 // Ignore br elements
bgneal@45 6398 if (n.nodeName === 'BR' && n.getAttribute('type') == '_moz')
bgneal@45 6399 return;
bgneal@45 6400 }
bgneal@45 6401
bgneal@45 6402 // Check if valid child
bgneal@183 6403 if (s.validate_children) {
bgneal@183 6404 if (t.elementName && !t.schema.isValid(t.elementName, nn)) {
bgneal@183 6405 iv = true;
bgneal@183 6406 break;
bgneal@45 6407 }
bgneal@45 6408
bgneal@45 6409 t.elementName = nn;
bgneal@45 6410 }
bgneal@45 6411
bgneal@45 6412 ru = t.findRule(nn);
bgneal@183 6413
bgneal@183 6414 // No valid rule for this element could be found then skip it
bgneal@183 6415 if (!ru) {
bgneal@183 6416 iv = true;
bgneal@183 6417 break;
bgneal@183 6418 }
bgneal@183 6419
bgneal@45 6420 nn = ru.name || nn;
bgneal@183 6421 closed = s.closed.test(nn);
bgneal@45 6422
bgneal@45 6423 // Skip empty nodes or empty node name in IE
bgneal@45 6424 if ((!hc && ru.noEmpty) || (isIE && !nn)) {
bgneal@45 6425 iv = true;
bgneal@45 6426 break;
bgneal@45 6427 }
bgneal@45 6428
bgneal@45 6429 // Check required
bgneal@45 6430 if (ru.requiredAttribs) {
bgneal@45 6431 a = ru.requiredAttribs;
bgneal@45 6432
bgneal@45 6433 for (i = a.length - 1; i >= 0; i--) {
bgneal@45 6434 if (this.dom.getAttrib(n, a[i]) !== '')
bgneal@45 6435 break;
bgneal@45 6436 }
bgneal@45 6437
bgneal@45 6438 // None of the required was there
bgneal@45 6439 if (i == -1) {
bgneal@45 6440 iv = true;
bgneal@45 6441 break;
bgneal@45 6442 }
bgneal@45 6443 }
bgneal@45 6444
bgneal@45 6445 w.writeStartElement(nn);
bgneal@45 6446
bgneal@45 6447 // Add ordered attributes
bgneal@45 6448 if (ru.attribs) {
bgneal@45 6449 for (i=0, at = ru.attribs, l = at.length; i<l; i++) {
bgneal@45 6450 a = at[i];
bgneal@45 6451 v = t._getAttrib(n, a);
bgneal@45 6452
bgneal@45 6453 if (v !== null)
bgneal@45 6454 w.writeAttribute(a.name, v);
bgneal@45 6455 }
bgneal@45 6456 }
bgneal@45 6457
bgneal@45 6458 // Add wild attributes
bgneal@45 6459 if (ru.validAttribsRE) {
bgneal@45 6460 at = t.dom.getAttribs(n);
bgneal@45 6461 for (i=at.length-1; i>-1; i--) {
bgneal@45 6462 no = at[i];
bgneal@45 6463
bgneal@45 6464 if (no.specified) {
bgneal@45 6465 a = no.nodeName.toLowerCase();
bgneal@45 6466
bgneal@45 6467 if (s.invalid_attrs.test(a) || !ru.validAttribsRE.test(a))
bgneal@45 6468 continue;
bgneal@45 6469
bgneal@45 6470 ar = t.findAttribRule(ru, a);
bgneal@45 6471 v = t._getAttrib(n, ar, a);
bgneal@45 6472
bgneal@45 6473 if (v !== null)
bgneal@45 6474 w.writeAttribute(a, v);
bgneal@45 6475 }
bgneal@45 6476 }
bgneal@45 6477 }
bgneal@45 6478
bgneal@183 6479 // Keep type attribute
bgneal@183 6480 if (type && keep)
bgneal@183 6481 w.writeAttribute('_mce_type', type);
bgneal@183 6482
bgneal@183 6483 // Write text from script
bgneal@183 6484 if (nn === 'script' && tinymce.trim(n.innerHTML)) {
bgneal@183 6485 w.writeText('// '); // Padd it with a comment so it will parse on older browsers
bgneal@183 6486 w.writeCDATA(n.innerHTML.replace(/<!--|-->|<\[CDATA\[|\]\]>/g, '')); // Remove comments and cdata stuctures
bgneal@183 6487 hc = false;
bgneal@183 6488 break;
bgneal@183 6489 }
bgneal@183 6490
bgneal@45 6491 // Padd empty nodes with a &nbsp;
bgneal@45 6492 if (ru.padd) {
bgneal@45 6493 // If it has only one bogus child, padd it anyway workaround for <td><br /></td> bug
bgneal@45 6494 if (hc && (cn = n.firstChild) && cn.nodeType === 1 && n.childNodes.length === 1) {
bgneal@183 6495 if (cn.hasAttribute ? cn.hasAttribute('_mce_bogus') : cn.getAttribute('_mce_bogus'))
bgneal@45 6496 w.writeText('\u00a0');
bgneal@45 6497 } else if (!hc)
bgneal@45 6498 w.writeText('\u00a0'); // No children then padd it
bgneal@45 6499 }
bgneal@45 6500
bgneal@45 6501 break;
bgneal@45 6502
bgneal@45 6503 case 3: // Text
bgneal@45 6504 // Check if valid child
bgneal@183 6505 if (s.validate_children && t.elementName && !t.schema.isValid(t.elementName, '#text'))
bgneal@183 6506 return;
bgneal@45 6507
bgneal@45 6508 return w.writeText(n.nodeValue);
bgneal@45 6509
bgneal@45 6510 case 4: // CDATA
bgneal@45 6511 return w.writeCDATA(n.nodeValue);
bgneal@45 6512
bgneal@45 6513 case 8: // Comment
bgneal@45 6514 return w.writeComment(n.nodeValue);
bgneal@45 6515 }
bgneal@45 6516 } else if (n.nodeType == 1)
bgneal@45 6517 hc = n.hasChildNodes();
bgneal@45 6518
bgneal@183 6519 if (hc && !closed) {
bgneal@45 6520 cn = n.firstChild;
bgneal@45 6521
bgneal@45 6522 while (cn) {
bgneal@45 6523 t._serializeNode(cn);
bgneal@45 6524 t.elementName = nn;
bgneal@45 6525 cn = cn.nextSibling;
bgneal@45 6526 }
bgneal@45 6527 }
bgneal@45 6528
bgneal@45 6529 // Write element end
bgneal@45 6530 if (!iv) {
bgneal@183 6531 if (!closed)
bgneal@45 6532 w.writeFullEndElement();
bgneal@45 6533 else
bgneal@45 6534 w.writeEndElement();
bgneal@45 6535 }
bgneal@45 6536 },
bgneal@45 6537
bgneal@45 6538 _protect : function(o) {
bgneal@45 6539 var t = this;
bgneal@45 6540
bgneal@45 6541 o.items = o.items || [];
bgneal@45 6542
bgneal@45 6543 function enc(s) {
bgneal@45 6544 return s.replace(/[\r\n\\]/g, function(c) {
bgneal@45 6545 if (c === '\n')
bgneal@45 6546 return '\\n';
bgneal@45 6547 else if (c === '\\')
bgneal@45 6548 return '\\\\';
bgneal@45 6549
bgneal@45 6550 return '\\r';
bgneal@45 6551 });
bgneal@45 6552 };
bgneal@45 6553
bgneal@45 6554 function dec(s) {
bgneal@45 6555 return s.replace(/\\[\\rn]/g, function(c) {
bgneal@45 6556 if (c === '\\n')
bgneal@45 6557 return '\n';
bgneal@45 6558 else if (c === '\\\\')
bgneal@45 6559 return '\\';
bgneal@45 6560
bgneal@45 6561 return '\r';
bgneal@45 6562 });
bgneal@45 6563 };
bgneal@45 6564
bgneal@45 6565 each(o.patterns, function(p) {
bgneal@45 6566 o.content = dec(enc(o.content).replace(p.pattern, function(x, a, b, c) {
bgneal@45 6567 b = dec(b);
bgneal@45 6568
bgneal@45 6569 if (p.encode)
bgneal@45 6570 b = t._encode(b);
bgneal@45 6571
bgneal@45 6572 o.items.push(b);
bgneal@45 6573 return a + '<!--mce:' + (o.items.length - 1) + '-->' + c;
bgneal@45 6574 }));
bgneal@45 6575 });
bgneal@45 6576
bgneal@45 6577 return o;
bgneal@45 6578 },
bgneal@45 6579
bgneal@45 6580 _unprotect : function(h, o) {
bgneal@45 6581 h = h.replace(/\<!--mce:([0-9]+)--\>/g, function(a, b) {
bgneal@45 6582 return o.items[parseInt(b)];
bgneal@45 6583 });
bgneal@45 6584
bgneal@45 6585 o.items = [];
bgneal@45 6586
bgneal@45 6587 return h;
bgneal@45 6588 },
bgneal@45 6589
bgneal@45 6590 _encode : function(h) {
bgneal@45 6591 var t = this, s = t.settings, l;
bgneal@45 6592
bgneal@45 6593 // Entity encode
bgneal@45 6594 if (s.entity_encoding !== 'raw') {
bgneal@45 6595 if (s.entity_encoding.indexOf('named') != -1) {
bgneal@45 6596 t.setEntities(s.entities);
bgneal@45 6597 l = t.entityLookup;
bgneal@45 6598
bgneal@183 6599 h = h.replace(/[\u007E-\uFFFF]/g, function(a) {
bgneal@45 6600 var v;
bgneal@45 6601
bgneal@45 6602 if (v = l[a])
bgneal@45 6603 a = '&' + v + ';';
bgneal@45 6604
bgneal@45 6605 return a;
bgneal@45 6606 });
bgneal@45 6607 }
bgneal@45 6608
bgneal@45 6609 if (s.entity_encoding.indexOf('numeric') != -1) {
bgneal@45 6610 h = h.replace(/[\u007E-\uFFFF]/g, function(a) {
bgneal@45 6611 return '&#' + a.charCodeAt(0) + ';';
bgneal@45 6612 });
bgneal@45 6613 }
bgneal@45 6614 }
bgneal@45 6615
bgneal@45 6616 return h;
bgneal@45 6617 },
bgneal@45 6618
bgneal@45 6619 _setup : function() {
bgneal@45 6620 var t = this, s = this.settings;
bgneal@45 6621
bgneal@45 6622 if (t.done)
bgneal@45 6623 return;
bgneal@45 6624
bgneal@45 6625 t.done = 1;
bgneal@45 6626
bgneal@45 6627 t.setRules(s.valid_elements);
bgneal@45 6628 t.addRules(s.extended_valid_elements);
bgneal@45 6629
bgneal@45 6630 if (s.invalid_elements)
bgneal@45 6631 t.invalidElementsRE = new RegExp('^(' + wildcardToRE(s.invalid_elements.replace(/,/g, '|').toLowerCase()) + ')$');
bgneal@45 6632
bgneal@45 6633 if (s.attrib_value_filter)
bgneal@45 6634 t.attribValueFilter = s.attribValueFilter;
bgneal@45 6635 },
bgneal@45 6636
bgneal@45 6637 _getAttrib : function(n, a, na) {
bgneal@45 6638 var i, v;
bgneal@45 6639
bgneal@45 6640 na = na || a.name;
bgneal@45 6641
bgneal@45 6642 if (a.forcedVal && (v = a.forcedVal)) {
bgneal@45 6643 if (v === '{$uid}')
bgneal@45 6644 return this.dom.uniqueId();
bgneal@45 6645
bgneal@45 6646 return v;
bgneal@45 6647 }
bgneal@45 6648
bgneal@45 6649 v = this.dom.getAttrib(n, na);
bgneal@45 6650
bgneal@45 6651 switch (na) {
bgneal@45 6652 case 'rowspan':
bgneal@45 6653 case 'colspan':
bgneal@45 6654 // Whats the point? Remove usless attribute value
bgneal@45 6655 if (v == '1')
bgneal@45 6656 v = '';
bgneal@45 6657
bgneal@45 6658 break;
bgneal@45 6659 }
bgneal@45 6660
bgneal@45 6661 if (this.attribValueFilter)
bgneal@45 6662 v = this.attribValueFilter(na, v, n);
bgneal@45 6663
bgneal@45 6664 if (a.validVals) {
bgneal@45 6665 for (i = a.validVals.length - 1; i >= 0; i--) {
bgneal@45 6666 if (v == a.validVals[i])
bgneal@45 6667 break;
bgneal@45 6668 }
bgneal@45 6669
bgneal@45 6670 if (i == -1)
bgneal@45 6671 return null;
bgneal@45 6672 }
bgneal@45 6673
bgneal@45 6674 if (v === '' && typeof(a.defaultVal) != 'undefined') {
bgneal@45 6675 v = a.defaultVal;
bgneal@45 6676
bgneal@45 6677 if (v === '{$uid}')
bgneal@45 6678 return this.dom.uniqueId();
bgneal@45 6679
bgneal@45 6680 return v;
bgneal@45 6681 } else {
bgneal@45 6682 // Remove internal mceItemXX classes when content is extracted from editor
bgneal@45 6683 if (na == 'class' && this.processObj.get)
bgneal@45 6684 v = v.replace(/\s?mceItem\w+\s?/g, '');
bgneal@45 6685 }
bgneal@45 6686
bgneal@45 6687 if (v === '')
bgneal@45 6688 return null;
bgneal@45 6689
bgneal@45 6690
bgneal@45 6691 return v;
bgneal@45 6692 }
bgneal@183 6693 });
bgneal@45 6694 })(tinymce);
bgneal@183 6695
bgneal@45 6696 (function(tinymce) {
bgneal@183 6697 tinymce.dom.ScriptLoader = function(settings) {
bgneal@183 6698 var QUEUED = 0,
bgneal@183 6699 LOADING = 1,
bgneal@183 6700 LOADED = 2,
bgneal@183 6701 states = {},
bgneal@183 6702 queue = [],
bgneal@183 6703 scriptLoadedCallbacks = {},
bgneal@183 6704 queueLoadedCallbacks = [],
bgneal@183 6705 loading = 0,
bgneal@183 6706 undefined;
bgneal@183 6707
bgneal@183 6708 function loadScript(url, callback) {
bgneal@183 6709 var t = this, dom = tinymce.DOM, elm, uri, loc, id;
bgneal@183 6710
bgneal@183 6711 // Execute callback when script is loaded
bgneal@183 6712 function done() {
bgneal@183 6713 dom.remove(id);
bgneal@183 6714
bgneal@183 6715 if (elm)
bgneal@183 6716 elm.onreadystatechange = elm.onload = elm = null;
bgneal@183 6717
bgneal@183 6718 callback();
bgneal@183 6719 };
bgneal@183 6720
bgneal@183 6721 id = dom.uniqueId();
bgneal@183 6722
bgneal@183 6723 if (tinymce.isIE6) {
bgneal@183 6724 uri = new tinymce.util.URI(url);
bgneal@183 6725 loc = location;
bgneal@183 6726
bgneal@183 6727 // If script is from same domain and we
bgneal@183 6728 // use IE 6 then use XHR since it's more reliable
bgneal@183 6729 if (uri.host == loc.hostname && uri.port == loc.port && (uri.protocol + ':') == loc.protocol) {
bgneal@45 6730 tinymce.util.XHR.send({
bgneal@183 6731 url : tinymce._addVer(uri.getURI()),
bgneal@183 6732 success : function(content) {
bgneal@183 6733 // Create new temp script element
bgneal@183 6734 var script = dom.create('script', {
bgneal@183 6735 type : 'text/javascript'
bgneal@183 6736 });
bgneal@183 6737
bgneal@183 6738 // Evaluate script in global scope
bgneal@183 6739 script.text = content;
bgneal@183 6740 document.getElementsByTagName('head')[0].appendChild(script);
bgneal@183 6741 dom.remove(script);
bgneal@183 6742
bgneal@183 6743 done();
bgneal@45 6744 }
bgneal@45 6745 });
bgneal@183 6746
bgneal@183 6747 return;
bgneal@183 6748 }
bgneal@183 6749 }
bgneal@183 6750
bgneal@183 6751 // Create new script element
bgneal@183 6752 elm = dom.create('script', {
bgneal@183 6753 id : id,
bgneal@183 6754 type : 'text/javascript',
bgneal@183 6755 src : tinymce._addVer(url)
bgneal@183 6756 });
bgneal@183 6757
bgneal@183 6758 // Add onload and readystate listeners
bgneal@183 6759 elm.onload = done;
bgneal@183 6760 elm.onreadystatechange = function() {
bgneal@183 6761 var state = elm.readyState;
bgneal@183 6762
bgneal@183 6763 // Loaded state is passed on IE 6 however there
bgneal@183 6764 // are known issues with this method but we can't use
bgneal@183 6765 // XHR in a cross domain loading
bgneal@183 6766 if (state == 'complete' || state == 'loaded')
bgneal@183 6767 done();
bgneal@45 6768 };
bgneal@45 6769
bgneal@183 6770 // Most browsers support this feature so we report errors
bgneal@183 6771 // for those at least to help users track their missing plugins etc
bgneal@183 6772 // todo: Removed since it produced error if the document is unloaded by navigating away, re-add it as an option
bgneal@183 6773 /*elm.onerror = function() {
bgneal@183 6774 alert('Failed to load: ' + url);
bgneal@183 6775 };*/
bgneal@183 6776
bgneal@183 6777 // Add script to document
bgneal@183 6778 (document.getElementsByTagName('head')[0] || document.body).appendChild(elm);
bgneal@183 6779 };
bgneal@183 6780
bgneal@183 6781 this.isDone = function(url) {
bgneal@183 6782 return states[url] == LOADED;
bgneal@183 6783 };
bgneal@183 6784
bgneal@183 6785 this.markDone = function(url) {
bgneal@183 6786 states[url] = LOADED;
bgneal@183 6787 };
bgneal@183 6788
bgneal@183 6789 this.add = this.load = function(url, callback, scope) {
bgneal@183 6790 var item, state = states[url];
bgneal@183 6791
bgneal@183 6792 // Add url to load queue
bgneal@183 6793 if (state == undefined) {
bgneal@183 6794 queue.push(url);
bgneal@183 6795 states[url] = QUEUED;
bgneal@183 6796 }
bgneal@183 6797
bgneal@183 6798 if (callback) {
bgneal@183 6799 // Store away callback for later execution
bgneal@183 6800 if (!scriptLoadedCallbacks[url])
bgneal@183 6801 scriptLoadedCallbacks[url] = [];
bgneal@183 6802
bgneal@183 6803 scriptLoadedCallbacks[url].push({
bgneal@183 6804 func : callback,
bgneal@183 6805 scope : scope || this
bgneal@183 6806 });
bgneal@183 6807 }
bgneal@183 6808 };
bgneal@183 6809
bgneal@183 6810 this.loadQueue = function(callback, scope) {
bgneal@183 6811 this.loadScripts(queue, callback, scope);
bgneal@183 6812 };
bgneal@183 6813
bgneal@183 6814 this.loadScripts = function(scripts, callback, scope) {
bgneal@183 6815 var loadScripts;
bgneal@183 6816
bgneal@183 6817 function execScriptLoadedCallbacks(url) {
bgneal@183 6818 // Execute URL callback functions
bgneal@183 6819 tinymce.each(scriptLoadedCallbacks[url], function(callback) {
bgneal@183 6820 callback.func.call(callback.scope);
bgneal@183 6821 });
bgneal@183 6822
bgneal@183 6823 scriptLoadedCallbacks[url] = undefined;
bgneal@183 6824 };
bgneal@183 6825
bgneal@183 6826 queueLoadedCallbacks.push({
bgneal@183 6827 func : callback,
bgneal@183 6828 scope : scope || this
bgneal@183 6829 });
bgneal@183 6830
bgneal@183 6831 loadScripts = function() {
bgneal@183 6832 var loadingScripts = tinymce.grep(scripts);
bgneal@183 6833
bgneal@183 6834 // Current scripts has been handled
bgneal@183 6835 scripts.length = 0;
bgneal@183 6836
bgneal@183 6837 // Load scripts that needs to be loaded
bgneal@183 6838 tinymce.each(loadingScripts, function(url) {
bgneal@183 6839 // Script is already loaded then execute script callbacks directly
bgneal@183 6840 if (states[url] == LOADED) {
bgneal@183 6841 execScriptLoadedCallbacks(url);
bgneal@183 6842 return;
bgneal@183 6843 }
bgneal@183 6844
bgneal@183 6845 // Is script not loading then start loading it
bgneal@183 6846 if (states[url] != LOADING) {
bgneal@183 6847 states[url] = LOADING;
bgneal@183 6848 loading++;
bgneal@183 6849
bgneal@183 6850 loadScript(url, function() {
bgneal@183 6851 states[url] = LOADED;
bgneal@183 6852 loading--;
bgneal@183 6853
bgneal@183 6854 execScriptLoadedCallbacks(url);
bgneal@183 6855
bgneal@183 6856 // Load more scripts if they where added by the recently loaded script
bgneal@183 6857 loadScripts();
bgneal@183 6858 });
bgneal@183 6859 }
bgneal@183 6860 });
bgneal@183 6861
bgneal@183 6862 // No scripts are currently loading then execute all pending queue loaded callbacks
bgneal@183 6863 if (!loading) {
bgneal@183 6864 tinymce.each(queueLoadedCallbacks, function(callback) {
bgneal@183 6865 callback.func.call(callback.scope);
bgneal@45 6866 });
bgneal@183 6867
bgneal@183 6868 queueLoadedCallbacks.length = 0;
bgneal@183 6869 }
bgneal@45 6870 };
bgneal@45 6871
bgneal@183 6872 loadScripts();
bgneal@183 6873 };
bgneal@183 6874 };
bgneal@45 6875
bgneal@45 6876 // Global script loader
bgneal@45 6877 tinymce.ScriptLoader = new tinymce.dom.ScriptLoader();
bgneal@45 6878 })(tinymce);
bgneal@183 6879
bgneal@183 6880 tinymce.dom.TreeWalker = function(start_node, root_node) {
bgneal@183 6881 var node = start_node;
bgneal@183 6882
bgneal@183 6883 function findSibling(node, start_name, sibling_name, shallow) {
bgneal@183 6884 var sibling, parent;
bgneal@183 6885
bgneal@183 6886 if (node) {
bgneal@183 6887 // Walk into nodes if it has a start
bgneal@183 6888 if (!shallow && node[start_name])
bgneal@183 6889 return node[start_name];
bgneal@183 6890
bgneal@183 6891 // Return the sibling if it has one
bgneal@183 6892 if (node != root_node) {
bgneal@183 6893 sibling = node[sibling_name];
bgneal@183 6894 if (sibling)
bgneal@183 6895 return sibling;
bgneal@183 6896
bgneal@183 6897 // Walk up the parents to look for siblings
bgneal@183 6898 for (parent = node.parentNode; parent && parent != root_node; parent = parent.parentNode) {
bgneal@183 6899 sibling = parent[sibling_name];
bgneal@183 6900 if (sibling)
bgneal@183 6901 return sibling;
bgneal@183 6902 }
bgneal@183 6903 }
bgneal@183 6904 }
bgneal@183 6905 };
bgneal@183 6906
bgneal@183 6907 this.current = function() {
bgneal@183 6908 return node;
bgneal@183 6909 };
bgneal@183 6910
bgneal@183 6911 this.next = function(shallow) {
bgneal@183 6912 return (node = findSibling(node, 'firstChild', 'nextSibling', shallow));
bgneal@183 6913 };
bgneal@183 6914
bgneal@183 6915 this.prev = function(shallow) {
bgneal@183 6916 return (node = findSibling(node, 'lastChild', 'lastSibling', shallow));
bgneal@183 6917 };
bgneal@183 6918 };
bgneal@183 6919
bgneal@183 6920 (function() {
bgneal@183 6921 var transitional = {};
bgneal@183 6922
bgneal@183 6923 function unpack(lookup, data) {
bgneal@183 6924 var key;
bgneal@183 6925
bgneal@183 6926 function replace(value) {
bgneal@183 6927 return value.replace(/[A-Z]+/g, function(key) {
bgneal@183 6928 return replace(lookup[key]);
bgneal@183 6929 });
bgneal@183 6930 };
bgneal@183 6931
bgneal@183 6932 // Unpack lookup
bgneal@183 6933 for (key in lookup) {
bgneal@183 6934 if (lookup.hasOwnProperty(key))
bgneal@183 6935 lookup[key] = replace(lookup[key]);
bgneal@183 6936 }
bgneal@183 6937
bgneal@183 6938 // Unpack and parse data into object map
bgneal@183 6939 replace(data).replace(/#/g, '#text').replace(/(\w+)\[([^\]]+)\]/g, function(str, name, children) {
bgneal@183 6940 var i, map = {};
bgneal@183 6941
bgneal@183 6942 children = children.split(/\|/);
bgneal@183 6943
bgneal@183 6944 for (i = children.length - 1; i >= 0; i--)
bgneal@183 6945 map[children[i]] = 1;
bgneal@183 6946
bgneal@183 6947 transitional[name] = map;
bgneal@183 6948 });
bgneal@183 6949 };
bgneal@183 6950
bgneal@183 6951 // This is the XHTML 1.0 transitional elements with it's children packed to reduce it's size
bgneal@183 6952 // we will later include the attributes here and use it as a default for valid elements but it
bgneal@183 6953 // requires us to rewrite the serializer engine
bgneal@183 6954 unpack({
bgneal@183 6955 Z : '#|H|K|N|O|P',
bgneal@183 6956 Y : '#|X|form|R|Q',
bgneal@183 6957 X : 'p|T|div|U|W|isindex|fieldset|table',
bgneal@183 6958 W : 'pre|hr|blockquote|address|center|noframes',
bgneal@183 6959 U : 'ul|ol|dl|menu|dir',
bgneal@183 6960 ZC : '#|p|Y|div|U|W|table|br|span|bdo|object|applet|img|map|K|N|Q',
bgneal@183 6961 T : 'h1|h2|h3|h4|h5|h6',
bgneal@183 6962 ZB : '#|X|S|Q',
bgneal@183 6963 S : 'R|P',
bgneal@183 6964 ZA : '#|a|G|J|M|O|P',
bgneal@183 6965 R : '#|a|H|K|N|O',
bgneal@183 6966 Q : 'noscript|P',
bgneal@183 6967 P : 'ins|del|script',
bgneal@183 6968 O : 'input|select|textarea|label|button',
bgneal@183 6969 N : 'M|L',
bgneal@183 6970 M : 'em|strong|dfn|code|q|samp|kbd|var|cite|abbr|acronym',
bgneal@183 6971 L : 'sub|sup',
bgneal@183 6972 K : 'J|I',
bgneal@183 6973 J : 'tt|i|b|u|s|strike',
bgneal@183 6974 I : 'big|small|font|basefont',
bgneal@183 6975 H : 'G|F',
bgneal@183 6976 G : 'br|span|bdo',
bgneal@183 6977 F : 'object|applet|img|map|iframe'
bgneal@183 6978 }, 'script[]' +
bgneal@183 6979 'style[]' +
bgneal@183 6980 'object[#|param|X|form|a|H|K|N|O|Q]' +
bgneal@183 6981 'param[]' +
bgneal@183 6982 'p[S]' +
bgneal@183 6983 'a[Z]' +
bgneal@183 6984 'br[]' +
bgneal@183 6985 'span[S]' +
bgneal@183 6986 'bdo[S]' +
bgneal@183 6987 'applet[#|param|X|form|a|H|K|N|O|Q]' +
bgneal@183 6988 'h1[S]' +
bgneal@183 6989 'img[]' +
bgneal@183 6990 'map[X|form|Q|area]' +
bgneal@183 6991 'h2[S]' +
bgneal@183 6992 'iframe[#|X|form|a|H|K|N|O|Q]' +
bgneal@183 6993 'h3[S]' +
bgneal@183 6994 'tt[S]' +
bgneal@183 6995 'i[S]' +
bgneal@183 6996 'b[S]' +
bgneal@183 6997 'u[S]' +
bgneal@183 6998 's[S]' +
bgneal@183 6999 'strike[S]' +
bgneal@183 7000 'big[S]' +
bgneal@183 7001 'small[S]' +
bgneal@183 7002 'font[S]' +
bgneal@183 7003 'basefont[]' +
bgneal@183 7004 'em[S]' +
bgneal@183 7005 'strong[S]' +
bgneal@183 7006 'dfn[S]' +
bgneal@183 7007 'code[S]' +
bgneal@183 7008 'q[S]' +
bgneal@183 7009 'samp[S]' +
bgneal@183 7010 'kbd[S]' +
bgneal@183 7011 'var[S]' +
bgneal@183 7012 'cite[S]' +
bgneal@183 7013 'abbr[S]' +
bgneal@183 7014 'acronym[S]' +
bgneal@183 7015 'sub[S]' +
bgneal@183 7016 'sup[S]' +
bgneal@183 7017 'input[]' +
bgneal@183 7018 'select[optgroup|option]' +
bgneal@183 7019 'optgroup[option]' +
bgneal@183 7020 'option[]' +
bgneal@183 7021 'textarea[]' +
bgneal@183 7022 'label[S]' +
bgneal@183 7023 'button[#|p|T|div|U|W|table|G|object|applet|img|map|K|N|Q]' +
bgneal@183 7024 'h4[S]' +
bgneal@183 7025 'ins[#|X|form|a|H|K|N|O|Q]' +
bgneal@183 7026 'h5[S]' +
bgneal@183 7027 'del[#|X|form|a|H|K|N|O|Q]' +
bgneal@183 7028 'h6[S]' +
bgneal@183 7029 'div[#|X|form|a|H|K|N|O|Q]' +
bgneal@183 7030 'ul[li]' +
bgneal@183 7031 'li[#|X|form|a|H|K|N|O|Q]' +
bgneal@183 7032 'ol[li]' +
bgneal@183 7033 'dl[dt|dd]' +
bgneal@183 7034 'dt[S]' +
bgneal@183 7035 'dd[#|X|form|a|H|K|N|O|Q]' +
bgneal@183 7036 'menu[li]' +
bgneal@183 7037 'dir[li]' +
bgneal@183 7038 'pre[ZA]' +
bgneal@183 7039 'hr[]' +
bgneal@183 7040 'blockquote[#|X|form|a|H|K|N|O|Q]' +
bgneal@183 7041 'address[S|p]' +
bgneal@183 7042 'center[#|X|form|a|H|K|N|O|Q]' +
bgneal@183 7043 'noframes[#|X|form|a|H|K|N|O|Q]' +
bgneal@183 7044 'isindex[]' +
bgneal@183 7045 'fieldset[#|legend|X|form|a|H|K|N|O|Q]' +
bgneal@183 7046 'legend[S]' +
bgneal@183 7047 'table[caption|col|colgroup|thead|tfoot|tbody|tr]' +
bgneal@183 7048 'caption[S]' +
bgneal@183 7049 'col[]' +
bgneal@183 7050 'colgroup[col]' +
bgneal@183 7051 'thead[tr]' +
bgneal@183 7052 'tr[th|td]' +
bgneal@183 7053 'th[#|X|form|a|H|K|N|O|Q]' +
bgneal@183 7054 'form[#|X|a|H|K|N|O|Q]' +
bgneal@183 7055 'noscript[#|X|form|a|H|K|N|O|Q]' +
bgneal@183 7056 'td[#|X|form|a|H|K|N|O|Q]' +
bgneal@183 7057 'tfoot[tr]' +
bgneal@183 7058 'tbody[tr]' +
bgneal@183 7059 'area[]' +
bgneal@183 7060 'base[]' +
bgneal@183 7061 'body[#|X|form|a|H|K|N|O|Q]'
bgneal@183 7062 );
bgneal@183 7063
bgneal@183 7064 tinymce.dom.Schema = function() {
bgneal@183 7065 var t = this, elements = transitional;
bgneal@183 7066
bgneal@183 7067 t.isValid = function(name, child_name) {
bgneal@183 7068 var element = elements[name];
bgneal@183 7069
bgneal@183 7070 return !!(element && (!child_name || element[child_name]));
bgneal@183 7071 };
bgneal@183 7072 };
bgneal@183 7073 })();
bgneal@183 7074 (function(tinymce) {
bgneal@183 7075 tinymce.dom.RangeUtils = function(dom) {
bgneal@183 7076 var INVISIBLE_CHAR = '\uFEFF';
bgneal@183 7077
bgneal@183 7078 this.walk = function(rng, callback) {
bgneal@183 7079 var startContainer = rng.startContainer,
bgneal@183 7080 startOffset = rng.startOffset,
bgneal@183 7081 endContainer = rng.endContainer,
bgneal@183 7082 endOffset = rng.endOffset,
bgneal@183 7083 ancestor, startPoint,
bgneal@183 7084 endPoint, node, parent, siblings, nodes;
bgneal@183 7085
bgneal@183 7086 // Handle table cell selection the table plugin enables
bgneal@183 7087 // you to fake select table cells and perform formatting actions on them
bgneal@183 7088 nodes = dom.select('td.mceSelected,th.mceSelected');
bgneal@183 7089 if (nodes.length > 0) {
bgneal@183 7090 tinymce.each(nodes, function(node) {
bgneal@183 7091 callback([node]);
bgneal@183 7092 });
bgneal@183 7093
bgneal@183 7094 return;
bgneal@183 7095 }
bgneal@183 7096
bgneal@183 7097 function collectSiblings(node, name, end_node) {
bgneal@183 7098 var siblings = [];
bgneal@183 7099
bgneal@183 7100 for (; node && node != end_node; node = node[name])
bgneal@183 7101 siblings.push(node);
bgneal@183 7102
bgneal@183 7103 return siblings;
bgneal@183 7104 };
bgneal@183 7105
bgneal@183 7106 function findEndPoint(node, root) {
bgneal@183 7107 do {
bgneal@183 7108 if (node.parentNode == root)
bgneal@183 7109 return node;
bgneal@183 7110
bgneal@183 7111 node = node.parentNode;
bgneal@183 7112 } while(node);
bgneal@183 7113 };
bgneal@183 7114
bgneal@183 7115 function walkBoundary(start_node, end_node, next) {
bgneal@183 7116 var siblingName = next ? 'nextSibling' : 'previousSibling';
bgneal@183 7117
bgneal@183 7118 for (node = start_node, parent = node.parentNode; node && node != end_node; node = parent) {
bgneal@183 7119 parent = node.parentNode;
bgneal@183 7120 siblings = collectSiblings(node == start_node ? node : node[siblingName], siblingName);
bgneal@183 7121
bgneal@183 7122 if (siblings.length) {
bgneal@183 7123 if (!next)
bgneal@183 7124 siblings.reverse();
bgneal@183 7125
bgneal@183 7126 callback(siblings);
bgneal@183 7127 }
bgneal@183 7128 }
bgneal@183 7129 };
bgneal@183 7130
bgneal@183 7131 // If index based start position then resolve it
bgneal@183 7132 if (startContainer.nodeType == 1 && startContainer.hasChildNodes())
bgneal@183 7133 startContainer = startContainer.childNodes[startOffset];
bgneal@183 7134
bgneal@183 7135 // If index based end position then resolve it
bgneal@183 7136 if (endContainer.nodeType == 1 && endContainer.hasChildNodes())
bgneal@183 7137 endContainer = endContainer.childNodes[Math.min(startOffset == endOffset ? endOffset : endOffset - 1, endContainer.childNodes.length - 1)];
bgneal@183 7138
bgneal@183 7139 // Find common ancestor and end points
bgneal@183 7140 ancestor = dom.findCommonAncestor(startContainer, endContainer);
bgneal@183 7141
bgneal@183 7142 // Same container
bgneal@183 7143 if (startContainer == endContainer)
bgneal@183 7144 return callback([startContainer]);
bgneal@183 7145
bgneal@183 7146 // Process left side
bgneal@183 7147 for (node = startContainer; node; node = node.parentNode) {
bgneal@183 7148 if (node == endContainer)
bgneal@183 7149 return walkBoundary(startContainer, ancestor, true);
bgneal@183 7150
bgneal@183 7151 if (node == ancestor)
bgneal@183 7152 break;
bgneal@183 7153 }
bgneal@183 7154
bgneal@183 7155 // Process right side
bgneal@183 7156 for (node = endContainer; node; node = node.parentNode) {
bgneal@183 7157 if (node == startContainer)
bgneal@183 7158 return walkBoundary(endContainer, ancestor);
bgneal@183 7159
bgneal@183 7160 if (node == ancestor)
bgneal@183 7161 break;
bgneal@183 7162 }
bgneal@183 7163
bgneal@183 7164 // Find start/end point
bgneal@183 7165 startPoint = findEndPoint(startContainer, ancestor) || startContainer;
bgneal@183 7166 endPoint = findEndPoint(endContainer, ancestor) || endContainer;
bgneal@183 7167
bgneal@183 7168 // Walk left leaf
bgneal@183 7169 walkBoundary(startContainer, startPoint, true);
bgneal@183 7170
bgneal@183 7171 // Walk the middle from start to end point
bgneal@183 7172 siblings = collectSiblings(
bgneal@183 7173 startPoint == startContainer ? startPoint : startPoint.nextSibling,
bgneal@183 7174 'nextSibling',
bgneal@183 7175 endPoint == endContainer ? endPoint.nextSibling : endPoint
bgneal@183 7176 );
bgneal@183 7177
bgneal@183 7178 if (siblings.length)
bgneal@183 7179 callback(siblings);
bgneal@183 7180
bgneal@183 7181 // Walk right leaf
bgneal@183 7182 walkBoundary(endContainer, endPoint);
bgneal@183 7183 };
bgneal@183 7184
bgneal@183 7185 /* this.split = function(rng) {
bgneal@183 7186 var startContainer = rng.startContainer,
bgneal@183 7187 startOffset = rng.startOffset,
bgneal@183 7188 endContainer = rng.endContainer,
bgneal@183 7189 endOffset = rng.endOffset;
bgneal@183 7190
bgneal@183 7191 function splitText(node, offset) {
bgneal@183 7192 if (offset == node.nodeValue.length)
bgneal@183 7193 node.appendData(INVISIBLE_CHAR);
bgneal@183 7194
bgneal@183 7195 node = node.splitText(offset);
bgneal@183 7196
bgneal@183 7197 if (node.nodeValue === INVISIBLE_CHAR)
bgneal@183 7198 node.nodeValue = '';
bgneal@183 7199
bgneal@183 7200 return node;
bgneal@183 7201 };
bgneal@183 7202
bgneal@183 7203 // Handle single text node
bgneal@183 7204 if (startContainer == endContainer) {
bgneal@183 7205 if (startContainer.nodeType == 3) {
bgneal@183 7206 if (startOffset != 0)
bgneal@183 7207 startContainer = endContainer = splitText(startContainer, startOffset);
bgneal@183 7208
bgneal@183 7209 if (endOffset - startOffset != startContainer.nodeValue.length)
bgneal@183 7210 splitText(startContainer, endOffset - startOffset);
bgneal@183 7211 }
bgneal@183 7212 } else {
bgneal@183 7213 // Split startContainer text node if needed
bgneal@183 7214 if (startContainer.nodeType == 3 && startOffset != 0) {
bgneal@183 7215 startContainer = splitText(startContainer, startOffset);
bgneal@183 7216 startOffset = 0;
bgneal@183 7217 }
bgneal@183 7218
bgneal@183 7219 // Split endContainer text node if needed
bgneal@183 7220 if (endContainer.nodeType == 3 && endOffset != endContainer.nodeValue.length) {
bgneal@183 7221 endContainer = splitText(endContainer, endOffset).previousSibling;
bgneal@183 7222 endOffset = endContainer.nodeValue.length;
bgneal@183 7223 }
bgneal@183 7224 }
bgneal@183 7225
bgneal@183 7226 return {
bgneal@183 7227 startContainer : startContainer,
bgneal@183 7228 startOffset : startOffset,
bgneal@183 7229 endContainer : endContainer,
bgneal@183 7230 endOffset : endOffset
bgneal@183 7231 };
bgneal@183 7232 };
bgneal@183 7233 */
bgneal@183 7234 };
bgneal@217 7235
bgneal@217 7236 tinymce.dom.RangeUtils.compareRanges = function(rng1, rng2) {
bgneal@217 7237 if (rng1 && rng2) {
bgneal@217 7238 // Compare native IE ranges
bgneal@217 7239 if (rng1.item || rng1.duplicate) {
bgneal@217 7240 // Both are control ranges and the selected element matches
bgneal@217 7241 if (rng1.item && rng2.item && rng1.item(0) === rng2.item(0))
bgneal@217 7242 return true;
bgneal@217 7243
bgneal@217 7244 // Both are text ranges and the range matches
bgneal@217 7245 if (rng1.isEqual && rng2.isEqual && rng2.isEqual(rng1))
bgneal@217 7246 return true;
bgneal@217 7247 } else {
bgneal@217 7248 // Compare w3c ranges
bgneal@217 7249 return rng1.startContainer == rng2.startContainer && rng1.startOffset == rng2.startOffset;
bgneal@217 7250 }
bgneal@217 7251 }
bgneal@217 7252
bgneal@217 7253 return false;
bgneal@217 7254 };
bgneal@183 7255 })(tinymce);
bgneal@183 7256
bgneal@45 7257 (function(tinymce) {
bgneal@45 7258 // Shorten class names
bgneal@45 7259 var DOM = tinymce.DOM, is = tinymce.is;
bgneal@45 7260
bgneal@45 7261 tinymce.create('tinymce.ui.Control', {
bgneal@45 7262 Control : function(id, s) {
bgneal@45 7263 this.id = id;
bgneal@45 7264 this.settings = s = s || {};
bgneal@45 7265 this.rendered = false;
bgneal@45 7266 this.onRender = new tinymce.util.Dispatcher(this);
bgneal@45 7267 this.classPrefix = '';
bgneal@45 7268 this.scope = s.scope || this;
bgneal@45 7269 this.disabled = 0;
bgneal@45 7270 this.active = 0;
bgneal@45 7271 },
bgneal@45 7272
bgneal@45 7273 setDisabled : function(s) {
bgneal@45 7274 var e;
bgneal@45 7275
bgneal@45 7276 if (s != this.disabled) {
bgneal@45 7277 e = DOM.get(this.id);
bgneal@45 7278
bgneal@45 7279 // Add accessibility title for unavailable actions
bgneal@45 7280 if (e && this.settings.unavailable_prefix) {
bgneal@45 7281 if (s) {
bgneal@45 7282 this.prevTitle = e.title;
bgneal@45 7283 e.title = this.settings.unavailable_prefix + ": " + e.title;
bgneal@45 7284 } else
bgneal@45 7285 e.title = this.prevTitle;
bgneal@45 7286 }
bgneal@45 7287
bgneal@45 7288 this.setState('Disabled', s);
bgneal@45 7289 this.setState('Enabled', !s);
bgneal@45 7290 this.disabled = s;
bgneal@45 7291 }
bgneal@45 7292 },
bgneal@45 7293
bgneal@45 7294 isDisabled : function() {
bgneal@45 7295 return this.disabled;
bgneal@45 7296 },
bgneal@45 7297
bgneal@45 7298 setActive : function(s) {
bgneal@45 7299 if (s != this.active) {
bgneal@45 7300 this.setState('Active', s);
bgneal@45 7301 this.active = s;
bgneal@45 7302 }
bgneal@45 7303 },
bgneal@45 7304
bgneal@45 7305 isActive : function() {
bgneal@45 7306 return this.active;
bgneal@45 7307 },
bgneal@45 7308
bgneal@45 7309 setState : function(c, s) {
bgneal@45 7310 var n = DOM.get(this.id);
bgneal@45 7311
bgneal@45 7312 c = this.classPrefix + c;
bgneal@45 7313
bgneal@45 7314 if (s)
bgneal@45 7315 DOM.addClass(n, c);
bgneal@45 7316 else
bgneal@45 7317 DOM.removeClass(n, c);
bgneal@45 7318 },
bgneal@45 7319
bgneal@45 7320 isRendered : function() {
bgneal@45 7321 return this.rendered;
bgneal@45 7322 },
bgneal@45 7323
bgneal@45 7324 renderHTML : function() {
bgneal@45 7325 },
bgneal@45 7326
bgneal@45 7327 renderTo : function(n) {
bgneal@45 7328 DOM.setHTML(n, this.renderHTML());
bgneal@45 7329 },
bgneal@45 7330
bgneal@45 7331 postRender : function() {
bgneal@45 7332 var t = this, b;
bgneal@45 7333
bgneal@45 7334 // Set pending states
bgneal@45 7335 if (is(t.disabled)) {
bgneal@45 7336 b = t.disabled;
bgneal@45 7337 t.disabled = -1;
bgneal@45 7338 t.setDisabled(b);
bgneal@45 7339 }
bgneal@45 7340
bgneal@45 7341 if (is(t.active)) {
bgneal@45 7342 b = t.active;
bgneal@45 7343 t.active = -1;
bgneal@45 7344 t.setActive(b);
bgneal@45 7345 }
bgneal@45 7346 },
bgneal@45 7347
bgneal@45 7348 remove : function() {
bgneal@45 7349 DOM.remove(this.id);
bgneal@45 7350 this.destroy();
bgneal@45 7351 },
bgneal@45 7352
bgneal@45 7353 destroy : function() {
bgneal@45 7354 tinymce.dom.Event.clear(this.id);
bgneal@45 7355 }
bgneal@183 7356 });
bgneal@183 7357 })(tinymce);
bgneal@183 7358 tinymce.create('tinymce.ui.Container:tinymce.ui.Control', {
bgneal@45 7359 Container : function(id, s) {
bgneal@45 7360 this.parent(id, s);
bgneal@183 7361
bgneal@45 7362 this.controls = [];
bgneal@183 7363
bgneal@45 7364 this.lookup = {};
bgneal@45 7365 },
bgneal@45 7366
bgneal@45 7367 add : function(c) {
bgneal@45 7368 this.lookup[c.id] = c;
bgneal@45 7369 this.controls.push(c);
bgneal@45 7370
bgneal@45 7371 return c;
bgneal@45 7372 },
bgneal@45 7373
bgneal@45 7374 get : function(n) {
bgneal@45 7375 return this.lookup[n];
bgneal@45 7376 }
bgneal@183 7377 });
bgneal@183 7378
bgneal@45 7379
bgneal@45 7380 tinymce.create('tinymce.ui.Separator:tinymce.ui.Control', {
bgneal@45 7381 Separator : function(id, s) {
bgneal@45 7382 this.parent(id, s);
bgneal@45 7383 this.classPrefix = 'mceSeparator';
bgneal@45 7384 },
bgneal@45 7385
bgneal@45 7386 renderHTML : function() {
bgneal@45 7387 return tinymce.DOM.createHTML('span', {'class' : this.classPrefix});
bgneal@45 7388 }
bgneal@183 7389 });
bgneal@183 7390
bgneal@45 7391 (function(tinymce) {
bgneal@45 7392 var is = tinymce.is, DOM = tinymce.DOM, each = tinymce.each, walk = tinymce.walk;
bgneal@45 7393
bgneal@45 7394 tinymce.create('tinymce.ui.MenuItem:tinymce.ui.Control', {
bgneal@45 7395 MenuItem : function(id, s) {
bgneal@45 7396 this.parent(id, s);
bgneal@45 7397 this.classPrefix = 'mceMenuItem';
bgneal@45 7398 },
bgneal@45 7399
bgneal@45 7400 setSelected : function(s) {
bgneal@45 7401 this.setState('Selected', s);
bgneal@45 7402 this.selected = s;
bgneal@45 7403 },
bgneal@45 7404
bgneal@45 7405 isSelected : function() {
bgneal@45 7406 return this.selected;
bgneal@45 7407 },
bgneal@45 7408
bgneal@45 7409 postRender : function() {
bgneal@45 7410 var t = this;
bgneal@45 7411
bgneal@45 7412 t.parent();
bgneal@45 7413
bgneal@45 7414 // Set pending state
bgneal@45 7415 if (is(t.selected))
bgneal@45 7416 t.setSelected(t.selected);
bgneal@45 7417 }
bgneal@183 7418 });
bgneal@45 7419 })(tinymce);
bgneal@183 7420
bgneal@45 7421 (function(tinymce) {
bgneal@45 7422 var is = tinymce.is, DOM = tinymce.DOM, each = tinymce.each, walk = tinymce.walk;
bgneal@45 7423
bgneal@45 7424 tinymce.create('tinymce.ui.Menu:tinymce.ui.MenuItem', {
bgneal@45 7425 Menu : function(id, s) {
bgneal@45 7426 var t = this;
bgneal@45 7427
bgneal@45 7428 t.parent(id, s);
bgneal@45 7429 t.items = {};
bgneal@45 7430 t.collapsed = false;
bgneal@45 7431 t.menuCount = 0;
bgneal@45 7432 t.onAddItem = new tinymce.util.Dispatcher(this);
bgneal@45 7433 },
bgneal@45 7434
bgneal@45 7435 expand : function(d) {
bgneal@45 7436 var t = this;
bgneal@45 7437
bgneal@45 7438 if (d) {
bgneal@45 7439 walk(t, function(o) {
bgneal@45 7440 if (o.expand)
bgneal@45 7441 o.expand();
bgneal@45 7442 }, 'items', t);
bgneal@45 7443 }
bgneal@45 7444
bgneal@45 7445 t.collapsed = false;
bgneal@45 7446 },
bgneal@45 7447
bgneal@45 7448 collapse : function(d) {
bgneal@45 7449 var t = this;
bgneal@45 7450
bgneal@45 7451 if (d) {
bgneal@45 7452 walk(t, function(o) {
bgneal@45 7453 if (o.collapse)
bgneal@45 7454 o.collapse();
bgneal@45 7455 }, 'items', t);
bgneal@45 7456 }
bgneal@45 7457
bgneal@45 7458 t.collapsed = true;
bgneal@45 7459 },
bgneal@45 7460
bgneal@45 7461 isCollapsed : function() {
bgneal@45 7462 return this.collapsed;
bgneal@45 7463 },
bgneal@45 7464
bgneal@45 7465 add : function(o) {
bgneal@45 7466 if (!o.settings)
bgneal@45 7467 o = new tinymce.ui.MenuItem(o.id || DOM.uniqueId(), o);
bgneal@45 7468
bgneal@45 7469 this.onAddItem.dispatch(this, o);
bgneal@45 7470
bgneal@45 7471 return this.items[o.id] = o;
bgneal@45 7472 },
bgneal@45 7473
bgneal@45 7474 addSeparator : function() {
bgneal@45 7475 return this.add({separator : true});
bgneal@45 7476 },
bgneal@45 7477
bgneal@45 7478 addMenu : function(o) {
bgneal@45 7479 if (!o.collapse)
bgneal@45 7480 o = this.createMenu(o);
bgneal@45 7481
bgneal@45 7482 this.menuCount++;
bgneal@45 7483
bgneal@45 7484 return this.add(o);
bgneal@45 7485 },
bgneal@45 7486
bgneal@45 7487 hasMenus : function() {
bgneal@45 7488 return this.menuCount !== 0;
bgneal@45 7489 },
bgneal@45 7490
bgneal@45 7491 remove : function(o) {
bgneal@45 7492 delete this.items[o.id];
bgneal@45 7493 },
bgneal@45 7494
bgneal@45 7495 removeAll : function() {
bgneal@45 7496 var t = this;
bgneal@45 7497
bgneal@45 7498 walk(t, function(o) {
bgneal@45 7499 if (o.removeAll)
bgneal@45 7500 o.removeAll();
bgneal@45 7501 else
bgneal@45 7502 o.remove();
bgneal@45 7503
bgneal@45 7504 o.destroy();
bgneal@45 7505 }, 'items', t);
bgneal@45 7506
bgneal@45 7507 t.items = {};
bgneal@45 7508 },
bgneal@45 7509
bgneal@45 7510 createMenu : function(o) {
bgneal@45 7511 var m = new tinymce.ui.Menu(o.id || DOM.uniqueId(), o);
bgneal@45 7512
bgneal@45 7513 m.onAddItem.add(this.onAddItem.dispatch, this.onAddItem);
bgneal@45 7514
bgneal@45 7515 return m;
bgneal@45 7516 }
bgneal@183 7517 });
bgneal@183 7518 })(tinymce);
bgneal@183 7519 (function(tinymce) {
bgneal@45 7520 var is = tinymce.is, DOM = tinymce.DOM, each = tinymce.each, Event = tinymce.dom.Event, Element = tinymce.dom.Element;
bgneal@45 7521
bgneal@45 7522 tinymce.create('tinymce.ui.DropMenu:tinymce.ui.Menu', {
bgneal@45 7523 DropMenu : function(id, s) {
bgneal@45 7524 s = s || {};
bgneal@45 7525 s.container = s.container || DOM.doc.body;
bgneal@45 7526 s.offset_x = s.offset_x || 0;
bgneal@45 7527 s.offset_y = s.offset_y || 0;
bgneal@45 7528 s.vp_offset_x = s.vp_offset_x || 0;
bgneal@45 7529 s.vp_offset_y = s.vp_offset_y || 0;
bgneal@45 7530
bgneal@45 7531 if (is(s.icons) && !s.icons)
bgneal@45 7532 s['class'] += ' mceNoIcons';
bgneal@45 7533
bgneal@45 7534 this.parent(id, s);
bgneal@45 7535 this.onShowMenu = new tinymce.util.Dispatcher(this);
bgneal@45 7536 this.onHideMenu = new tinymce.util.Dispatcher(this);
bgneal@45 7537 this.classPrefix = 'mceMenu';
bgneal@45 7538 },
bgneal@45 7539
bgneal@45 7540 createMenu : function(s) {
bgneal@45 7541 var t = this, cs = t.settings, m;
bgneal@45 7542
bgneal@45 7543 s.container = s.container || cs.container;
bgneal@45 7544 s.parent = t;
bgneal@45 7545 s.constrain = s.constrain || cs.constrain;
bgneal@45 7546 s['class'] = s['class'] || cs['class'];
bgneal@45 7547 s.vp_offset_x = s.vp_offset_x || cs.vp_offset_x;
bgneal@45 7548 s.vp_offset_y = s.vp_offset_y || cs.vp_offset_y;
bgneal@45 7549 m = new tinymce.ui.DropMenu(s.id || DOM.uniqueId(), s);
bgneal@45 7550
bgneal@45 7551 m.onAddItem.add(t.onAddItem.dispatch, t.onAddItem);
bgneal@45 7552
bgneal@45 7553 return m;
bgneal@45 7554 },
bgneal@45 7555
bgneal@45 7556 update : function() {
bgneal@45 7557 var t = this, s = t.settings, tb = DOM.get('menu_' + t.id + '_tbl'), co = DOM.get('menu_' + t.id + '_co'), tw, th;
bgneal@45 7558
bgneal@45 7559 tw = s.max_width ? Math.min(tb.clientWidth, s.max_width) : tb.clientWidth;
bgneal@45 7560 th = s.max_height ? Math.min(tb.clientHeight, s.max_height) : tb.clientHeight;
bgneal@45 7561
bgneal@45 7562 if (!DOM.boxModel)
bgneal@45 7563 t.element.setStyles({width : tw + 2, height : th + 2});
bgneal@45 7564 else
bgneal@45 7565 t.element.setStyles({width : tw, height : th});
bgneal@45 7566
bgneal@45 7567 if (s.max_width)
bgneal@45 7568 DOM.setStyle(co, 'width', tw);
bgneal@45 7569
bgneal@45 7570 if (s.max_height) {
bgneal@45 7571 DOM.setStyle(co, 'height', th);
bgneal@45 7572
bgneal@45 7573 if (tb.clientHeight < s.max_height)
bgneal@45 7574 DOM.setStyle(co, 'overflow', 'hidden');
bgneal@45 7575 }
bgneal@45 7576 },
bgneal@45 7577
bgneal@45 7578 showMenu : function(x, y, px) {
bgneal@45 7579 var t = this, s = t.settings, co, vp = DOM.getViewPort(), w, h, mx, my, ot = 2, dm, tb, cp = t.classPrefix;
bgneal@45 7580
bgneal@45 7581 t.collapse(1);
bgneal@45 7582
bgneal@45 7583 if (t.isMenuVisible)
bgneal@45 7584 return;
bgneal@45 7585
bgneal@45 7586 if (!t.rendered) {
bgneal@45 7587 co = DOM.add(t.settings.container, t.renderNode());
bgneal@45 7588
bgneal@45 7589 each(t.items, function(o) {
bgneal@45 7590 o.postRender();
bgneal@45 7591 });
bgneal@45 7592
bgneal@45 7593 t.element = new Element('menu_' + t.id, {blocker : 1, container : s.container});
bgneal@45 7594 } else
bgneal@45 7595 co = DOM.get('menu_' + t.id);
bgneal@45 7596
bgneal@45 7597 // Move layer out of sight unless it's Opera since it scrolls to top of page due to an bug
bgneal@45 7598 if (!tinymce.isOpera)
bgneal@45 7599 DOM.setStyles(co, {left : -0xFFFF , top : -0xFFFF});
bgneal@45 7600
bgneal@45 7601 DOM.show(co);
bgneal@45 7602 t.update();
bgneal@45 7603
bgneal@45 7604 x += s.offset_x || 0;
bgneal@45 7605 y += s.offset_y || 0;
bgneal@45 7606 vp.w -= 4;
bgneal@45 7607 vp.h -= 4;
bgneal@45 7608
bgneal@45 7609 // Move inside viewport if not submenu
bgneal@45 7610 if (s.constrain) {
bgneal@45 7611 w = co.clientWidth - ot;
bgneal@45 7612 h = co.clientHeight - ot;
bgneal@45 7613 mx = vp.x + vp.w;
bgneal@45 7614 my = vp.y + vp.h;
bgneal@45 7615
bgneal@45 7616 if ((x + s.vp_offset_x + w) > mx)
bgneal@45 7617 x = px ? px - w : Math.max(0, (mx - s.vp_offset_x) - w);
bgneal@45 7618
bgneal@45 7619 if ((y + s.vp_offset_y + h) > my)
bgneal@45 7620 y = Math.max(0, (my - s.vp_offset_y) - h);
bgneal@45 7621 }
bgneal@45 7622
bgneal@45 7623 DOM.setStyles(co, {left : x , top : y});
bgneal@45 7624 t.element.update();
bgneal@45 7625
bgneal@45 7626 t.isMenuVisible = 1;
bgneal@45 7627 t.mouseClickFunc = Event.add(co, 'click', function(e) {
bgneal@45 7628 var m;
bgneal@45 7629
bgneal@45 7630 e = e.target;
bgneal@45 7631
bgneal@183 7632 if (e && (e = DOM.getParent(e, 'tr')) && !DOM.hasClass(e, cp + 'ItemSub')) {
bgneal@45 7633 m = t.items[e.id];
bgneal@45 7634
bgneal@45 7635 if (m.isDisabled())
bgneal@45 7636 return;
bgneal@45 7637
bgneal@45 7638 dm = t;
bgneal@45 7639
bgneal@45 7640 while (dm) {
bgneal@45 7641 if (dm.hideMenu)
bgneal@45 7642 dm.hideMenu();
bgneal@45 7643
bgneal@45 7644 dm = dm.settings.parent;
bgneal@45 7645 }
bgneal@45 7646
bgneal@45 7647 if (m.settings.onclick)
bgneal@45 7648 m.settings.onclick(e);
bgneal@45 7649
bgneal@45 7650 return Event.cancel(e); // Cancel to fix onbeforeunload problem
bgneal@45 7651 }
bgneal@45 7652 });
bgneal@45 7653
bgneal@45 7654 if (t.hasMenus()) {
bgneal@45 7655 t.mouseOverFunc = Event.add(co, 'mouseover', function(e) {
bgneal@45 7656 var m, r, mi;
bgneal@45 7657
bgneal@45 7658 e = e.target;
bgneal@183 7659 if (e && (e = DOM.getParent(e, 'tr'))) {
bgneal@45 7660 m = t.items[e.id];
bgneal@45 7661
bgneal@45 7662 if (t.lastMenu)
bgneal@45 7663 t.lastMenu.collapse(1);
bgneal@45 7664
bgneal@45 7665 if (m.isDisabled())
bgneal@45 7666 return;
bgneal@45 7667
bgneal@45 7668 if (e && DOM.hasClass(e, cp + 'ItemSub')) {
bgneal@45 7669 //p = DOM.getPos(s.container);
bgneal@45 7670 r = DOM.getRect(e);
bgneal@45 7671 m.showMenu((r.x + r.w - ot), r.y - ot, r.x);
bgneal@45 7672 t.lastMenu = m;
bgneal@45 7673 DOM.addClass(DOM.get(m.id).firstChild, cp + 'ItemActive');
bgneal@45 7674 }
bgneal@45 7675 }
bgneal@45 7676 });
bgneal@45 7677 }
bgneal@45 7678
bgneal@45 7679 t.onShowMenu.dispatch(t);
bgneal@45 7680
bgneal@45 7681 if (s.keyboard_focus) {
bgneal@45 7682 Event.add(co, 'keydown', t._keyHandler, t);
bgneal@45 7683 DOM.select('a', 'menu_' + t.id)[0].focus(); // Select first link
bgneal@45 7684 t._focusIdx = 0;
bgneal@45 7685 }
bgneal@45 7686 },
bgneal@45 7687
bgneal@45 7688 hideMenu : function(c) {
bgneal@45 7689 var t = this, co = DOM.get('menu_' + t.id), e;
bgneal@45 7690
bgneal@45 7691 if (!t.isMenuVisible)
bgneal@45 7692 return;
bgneal@45 7693
bgneal@45 7694 Event.remove(co, 'mouseover', t.mouseOverFunc);
bgneal@45 7695 Event.remove(co, 'click', t.mouseClickFunc);
bgneal@45 7696 Event.remove(co, 'keydown', t._keyHandler);
bgneal@45 7697 DOM.hide(co);
bgneal@45 7698 t.isMenuVisible = 0;
bgneal@45 7699
bgneal@45 7700 if (!c)
bgneal@45 7701 t.collapse(1);
bgneal@45 7702
bgneal@45 7703 if (t.element)
bgneal@45 7704 t.element.hide();
bgneal@45 7705
bgneal@45 7706 if (e = DOM.get(t.id))
bgneal@45 7707 DOM.removeClass(e.firstChild, t.classPrefix + 'ItemActive');
bgneal@45 7708
bgneal@45 7709 t.onHideMenu.dispatch(t);
bgneal@45 7710 },
bgneal@45 7711
bgneal@45 7712 add : function(o) {
bgneal@45 7713 var t = this, co;
bgneal@45 7714
bgneal@45 7715 o = t.parent(o);
bgneal@45 7716
bgneal@45 7717 if (t.isRendered && (co = DOM.get('menu_' + t.id)))
bgneal@45 7718 t._add(DOM.select('tbody', co)[0], o);
bgneal@45 7719
bgneal@45 7720 return o;
bgneal@45 7721 },
bgneal@45 7722
bgneal@45 7723 collapse : function(d) {
bgneal@45 7724 this.parent(d);
bgneal@45 7725 this.hideMenu(1);
bgneal@45 7726 },
bgneal@45 7727
bgneal@45 7728 remove : function(o) {
bgneal@45 7729 DOM.remove(o.id);
bgneal@45 7730 this.destroy();
bgneal@45 7731
bgneal@45 7732 return this.parent(o);
bgneal@45 7733 },
bgneal@45 7734
bgneal@45 7735 destroy : function() {
bgneal@45 7736 var t = this, co = DOM.get('menu_' + t.id);
bgneal@45 7737
bgneal@45 7738 Event.remove(co, 'mouseover', t.mouseOverFunc);
bgneal@45 7739 Event.remove(co, 'click', t.mouseClickFunc);
bgneal@45 7740
bgneal@45 7741 if (t.element)
bgneal@45 7742 t.element.remove();
bgneal@45 7743
bgneal@45 7744 DOM.remove(co);
bgneal@45 7745 },
bgneal@45 7746
bgneal@45 7747 renderNode : function() {
bgneal@45 7748 var t = this, s = t.settings, n, tb, co, w;
bgneal@45 7749
bgneal@45 7750 w = DOM.create('div', {id : 'menu_' + t.id, 'class' : s['class'], 'style' : 'position:absolute;left:0;top:0;z-index:200000'});
bgneal@45 7751 co = DOM.add(w, 'div', {id : 'menu_' + t.id + '_co', 'class' : t.classPrefix + (s['class'] ? ' ' + s['class'] : '')});
bgneal@45 7752 t.element = new Element('menu_' + t.id, {blocker : 1, container : s.container});
bgneal@45 7753
bgneal@45 7754 if (s.menu_line)
bgneal@45 7755 DOM.add(co, 'span', {'class' : t.classPrefix + 'Line'});
bgneal@45 7756
bgneal@45 7757 // n = DOM.add(co, 'div', {id : 'menu_' + t.id + '_co', 'class' : 'mceMenuContainer'});
bgneal@45 7758 n = DOM.add(co, 'table', {id : 'menu_' + t.id + '_tbl', border : 0, cellPadding : 0, cellSpacing : 0});
bgneal@45 7759 tb = DOM.add(n, 'tbody');
bgneal@45 7760
bgneal@45 7761 each(t.items, function(o) {
bgneal@45 7762 t._add(tb, o);
bgneal@45 7763 });
bgneal@45 7764
bgneal@45 7765 t.rendered = true;
bgneal@45 7766
bgneal@45 7767 return w;
bgneal@45 7768 },
bgneal@45 7769
bgneal@45 7770 // Internal functions
bgneal@45 7771
bgneal@45 7772 _keyHandler : function(e) {
bgneal@45 7773 var t = this, kc = e.keyCode;
bgneal@45 7774
bgneal@45 7775 function focus(d) {
bgneal@45 7776 var i = t._focusIdx + d, e = DOM.select('a', 'menu_' + t.id)[i];
bgneal@45 7777
bgneal@45 7778 if (e) {
bgneal@45 7779 t._focusIdx = i;
bgneal@45 7780 e.focus();
bgneal@45 7781 }
bgneal@45 7782 };
bgneal@45 7783
bgneal@45 7784 switch (kc) {
bgneal@45 7785 case 38:
bgneal@45 7786 focus(-1); // Select first link
bgneal@45 7787 return;
bgneal@45 7788
bgneal@45 7789 case 40:
bgneal@45 7790 focus(1);
bgneal@45 7791 return;
bgneal@45 7792
bgneal@45 7793 case 13:
bgneal@45 7794 return;
bgneal@45 7795
bgneal@45 7796 case 27:
bgneal@45 7797 return this.hideMenu();
bgneal@45 7798 }
bgneal@45 7799 },
bgneal@45 7800
bgneal@45 7801 _add : function(tb, o) {
bgneal@45 7802 var n, s = o.settings, a, ro, it, cp = this.classPrefix, ic;
bgneal@45 7803
bgneal@45 7804 if (s.separator) {
bgneal@45 7805 ro = DOM.add(tb, 'tr', {id : o.id, 'class' : cp + 'ItemSeparator'});
bgneal@45 7806 DOM.add(ro, 'td', {'class' : cp + 'ItemSeparator'});
bgneal@45 7807
bgneal@45 7808 if (n = ro.previousSibling)
bgneal@45 7809 DOM.addClass(n, 'mceLast');
bgneal@45 7810
bgneal@45 7811 return;
bgneal@45 7812 }
bgneal@45 7813
bgneal@45 7814 n = ro = DOM.add(tb, 'tr', {id : o.id, 'class' : cp + 'Item ' + cp + 'ItemEnabled'});
bgneal@45 7815 n = it = DOM.add(n, 'td');
bgneal@45 7816 n = a = DOM.add(n, 'a', {href : 'javascript:;', onclick : "return false;", onmousedown : 'return false;'});
bgneal@45 7817
bgneal@45 7818 DOM.addClass(it, s['class']);
bgneal@45 7819 // n = DOM.add(n, 'span', {'class' : 'item'});
bgneal@45 7820
bgneal@45 7821 ic = DOM.add(n, 'span', {'class' : 'mceIcon' + (s.icon ? ' mce_' + s.icon : '')});
bgneal@45 7822
bgneal@45 7823 if (s.icon_src)
bgneal@45 7824 DOM.add(ic, 'img', {src : s.icon_src});
bgneal@45 7825
bgneal@45 7826 n = DOM.add(n, s.element || 'span', {'class' : 'mceText', title : o.settings.title}, o.settings.title);
bgneal@45 7827
bgneal@45 7828 if (o.settings.style)
bgneal@45 7829 DOM.setAttrib(n, 'style', o.settings.style);
bgneal@45 7830
bgneal@45 7831 if (tb.childNodes.length == 1)
bgneal@45 7832 DOM.addClass(ro, 'mceFirst');
bgneal@45 7833
bgneal@45 7834 if ((n = ro.previousSibling) && DOM.hasClass(n, cp + 'ItemSeparator'))
bgneal@45 7835 DOM.addClass(ro, 'mceFirst');
bgneal@45 7836
bgneal@45 7837 if (o.collapse)
bgneal@45 7838 DOM.addClass(ro, cp + 'ItemSub');
bgneal@45 7839
bgneal@45 7840 if (n = ro.previousSibling)
bgneal@45 7841 DOM.removeClass(n, 'mceLast');
bgneal@45 7842
bgneal@45 7843 DOM.addClass(ro, 'mceLast');
bgneal@45 7844 }
bgneal@183 7845 });
bgneal@183 7846 })(tinymce);
bgneal@183 7847 (function(tinymce) {
bgneal@45 7848 var DOM = tinymce.DOM;
bgneal@45 7849
bgneal@45 7850 tinymce.create('tinymce.ui.Button:tinymce.ui.Control', {
bgneal@45 7851 Button : function(id, s) {
bgneal@45 7852 this.parent(id, s);
bgneal@45 7853 this.classPrefix = 'mceButton';
bgneal@45 7854 },
bgneal@45 7855
bgneal@45 7856 renderHTML : function() {
bgneal@45 7857 var cp = this.classPrefix, s = this.settings, h, l;
bgneal@45 7858
bgneal@45 7859 l = DOM.encode(s.label || '');
bgneal@45 7860 h = '<a id="' + this.id + '" href="javascript:;" class="' + cp + ' ' + cp + 'Enabled ' + s['class'] + (l ? ' ' + cp + 'Labeled' : '') +'" onmousedown="return false;" onclick="return false;" title="' + DOM.encode(s.title) + '">';
bgneal@45 7861
bgneal@45 7862 if (s.image)
bgneal@45 7863 h += '<img class="mceIcon" src="' + s.image + '" />' + l + '</a>';
bgneal@45 7864 else
bgneal@45 7865 h += '<span class="mceIcon ' + s['class'] + '"></span>' + (l ? '<span class="' + cp + 'Label">' + l + '</span>' : '') + '</a>';
bgneal@45 7866
bgneal@45 7867 return h;
bgneal@45 7868 },
bgneal@45 7869
bgneal@45 7870 postRender : function() {
bgneal@45 7871 var t = this, s = t.settings;
bgneal@45 7872
bgneal@45 7873 tinymce.dom.Event.add(t.id, 'click', function(e) {
bgneal@45 7874 if (!t.isDisabled())
bgneal@45 7875 return s.onclick.call(s.scope, e);
bgneal@45 7876 });
bgneal@45 7877 }
bgneal@183 7878 });
bgneal@45 7879 })(tinymce);
bgneal@183 7880
bgneal@45 7881 (function(tinymce) {
bgneal@45 7882 var DOM = tinymce.DOM, Event = tinymce.dom.Event, each = tinymce.each, Dispatcher = tinymce.util.Dispatcher;
bgneal@45 7883
bgneal@45 7884 tinymce.create('tinymce.ui.ListBox:tinymce.ui.Control', {
bgneal@45 7885 ListBox : function(id, s) {
bgneal@45 7886 var t = this;
bgneal@45 7887
bgneal@45 7888 t.parent(id, s);
bgneal@183 7889
bgneal@45 7890 t.items = [];
bgneal@183 7891
bgneal@45 7892 t.onChange = new Dispatcher(t);
bgneal@183 7893
bgneal@45 7894 t.onPostRender = new Dispatcher(t);
bgneal@183 7895
bgneal@45 7896 t.onAdd = new Dispatcher(t);
bgneal@183 7897
bgneal@45 7898 t.onRenderMenu = new tinymce.util.Dispatcher(this);
bgneal@183 7899
bgneal@45 7900 t.classPrefix = 'mceListBox';
bgneal@45 7901 },
bgneal@45 7902
bgneal@45 7903 select : function(va) {
bgneal@45 7904 var t = this, fv, f;
bgneal@45 7905
bgneal@45 7906 if (va == undefined)
bgneal@45 7907 return t.selectByIndex(-1);
bgneal@45 7908
bgneal@45 7909 // Is string or number make function selector
bgneal@45 7910 if (va && va.call)
bgneal@45 7911 f = va;
bgneal@45 7912 else {
bgneal@45 7913 f = function(v) {
bgneal@45 7914 return v == va;
bgneal@45 7915 };
bgneal@45 7916 }
bgneal@45 7917
bgneal@45 7918 // Do we need to do something?
bgneal@45 7919 if (va != t.selectedValue) {
bgneal@45 7920 // Find item
bgneal@45 7921 each(t.items, function(o, i) {
bgneal@45 7922 if (f(o.value)) {
bgneal@45 7923 fv = 1;
bgneal@45 7924 t.selectByIndex(i);
bgneal@45 7925 return false;
bgneal@45 7926 }
bgneal@45 7927 });
bgneal@45 7928
bgneal@45 7929 if (!fv)
bgneal@45 7930 t.selectByIndex(-1);
bgneal@45 7931 }
bgneal@45 7932 },
bgneal@45 7933
bgneal@45 7934 selectByIndex : function(idx) {
bgneal@45 7935 var t = this, e, o;
bgneal@45 7936
bgneal@45 7937 if (idx != t.selectedIndex) {
bgneal@45 7938 e = DOM.get(t.id + '_text');
bgneal@45 7939 o = t.items[idx];
bgneal@45 7940
bgneal@45 7941 if (o) {
bgneal@45 7942 t.selectedValue = o.value;
bgneal@45 7943 t.selectedIndex = idx;
bgneal@45 7944 DOM.setHTML(e, DOM.encode(o.title));
bgneal@45 7945 DOM.removeClass(e, 'mceTitle');
bgneal@45 7946 } else {
bgneal@45 7947 DOM.setHTML(e, DOM.encode(t.settings.title));
bgneal@45 7948 DOM.addClass(e, 'mceTitle');
bgneal@45 7949 t.selectedValue = t.selectedIndex = null;
bgneal@45 7950 }
bgneal@45 7951
bgneal@45 7952 e = 0;
bgneal@45 7953 }
bgneal@45 7954 },
bgneal@45 7955
bgneal@45 7956 add : function(n, v, o) {
bgneal@45 7957 var t = this;
bgneal@45 7958
bgneal@45 7959 o = o || {};
bgneal@45 7960 o = tinymce.extend(o, {
bgneal@45 7961 title : n,
bgneal@45 7962 value : v
bgneal@45 7963 });
bgneal@45 7964
bgneal@45 7965 t.items.push(o);
bgneal@45 7966 t.onAdd.dispatch(t, o);
bgneal@45 7967 },
bgneal@45 7968
bgneal@45 7969 getLength : function() {
bgneal@45 7970 return this.items.length;
bgneal@45 7971 },
bgneal@45 7972
bgneal@45 7973 renderHTML : function() {
bgneal@45 7974 var h = '', t = this, s = t.settings, cp = t.classPrefix;
bgneal@45 7975
bgneal@45 7976 h = '<table id="' + t.id + '" cellpadding="0" cellspacing="0" class="' + cp + ' ' + cp + 'Enabled' + (s['class'] ? (' ' + s['class']) : '') + '"><tbody><tr>';
bgneal@45 7977 h += '<td>' + DOM.createHTML('a', {id : t.id + '_text', href : 'javascript:;', 'class' : 'mceText', onclick : "return false;", onmousedown : 'return false;'}, DOM.encode(t.settings.title)) + '</td>';
bgneal@45 7978 h += '<td>' + DOM.createHTML('a', {id : t.id + '_open', tabindex : -1, href : 'javascript:;', 'class' : 'mceOpen', onclick : "return false;", onmousedown : 'return false;'}, '<span></span>') + '</td>';
bgneal@45 7979 h += '</tr></tbody></table>';
bgneal@45 7980
bgneal@45 7981 return h;
bgneal@45 7982 },
bgneal@45 7983
bgneal@45 7984 showMenu : function() {
bgneal@45 7985 var t = this, p1, p2, e = DOM.get(this.id), m;
bgneal@45 7986
bgneal@45 7987 if (t.isDisabled() || t.items.length == 0)
bgneal@45 7988 return;
bgneal@45 7989
bgneal@45 7990 if (t.menu && t.menu.isMenuVisible)
bgneal@45 7991 return t.hideMenu();
bgneal@45 7992
bgneal@45 7993 if (!t.isMenuRendered) {
bgneal@45 7994 t.renderMenu();
bgneal@45 7995 t.isMenuRendered = true;
bgneal@45 7996 }
bgneal@45 7997
bgneal@45 7998 p1 = DOM.getPos(this.settings.menu_container);
bgneal@45 7999 p2 = DOM.getPos(e);
bgneal@45 8000
bgneal@45 8001 m = t.menu;
bgneal@45 8002 m.settings.offset_x = p2.x;
bgneal@45 8003 m.settings.offset_y = p2.y;
bgneal@45 8004 m.settings.keyboard_focus = !tinymce.isOpera; // Opera is buggy when it comes to auto focus
bgneal@45 8005
bgneal@45 8006 // Select in menu
bgneal@45 8007 if (t.oldID)
bgneal@45 8008 m.items[t.oldID].setSelected(0);
bgneal@45 8009
bgneal@45 8010 each(t.items, function(o) {
bgneal@45 8011 if (o.value === t.selectedValue) {
bgneal@45 8012 m.items[o.id].setSelected(1);
bgneal@45 8013 t.oldID = o.id;
bgneal@45 8014 }
bgneal@45 8015 });
bgneal@45 8016
bgneal@45 8017 m.showMenu(0, e.clientHeight);
bgneal@45 8018
bgneal@45 8019 Event.add(DOM.doc, 'mousedown', t.hideMenu, t);
bgneal@45 8020 DOM.addClass(t.id, t.classPrefix + 'Selected');
bgneal@45 8021
bgneal@45 8022 //DOM.get(t.id + '_text').focus();
bgneal@45 8023 },
bgneal@45 8024
bgneal@45 8025 hideMenu : function(e) {
bgneal@45 8026 var t = this;
bgneal@45 8027
bgneal@183 8028 if (t.menu && t.menu.isMenuVisible) {
bgneal@183 8029 // Prevent double toogles by canceling the mouse click event to the button
bgneal@183 8030 if (e && e.type == "mousedown" && (e.target.id == t.id + '_text' || e.target.id == t.id + '_open'))
bgneal@183 8031 return;
bgneal@183 8032
bgneal@183 8033 if (!e || !DOM.getParent(e.target, '.mceMenu')) {
bgneal@183 8034 DOM.removeClass(t.id, t.classPrefix + 'Selected');
bgneal@183 8035 Event.remove(DOM.doc, 'mousedown', t.hideMenu, t);
bgneal@45 8036 t.menu.hideMenu();
bgneal@183 8037 }
bgneal@45 8038 }
bgneal@45 8039 },
bgneal@45 8040
bgneal@45 8041 renderMenu : function() {
bgneal@45 8042 var t = this, m;
bgneal@45 8043
bgneal@45 8044 m = t.settings.control_manager.createDropMenu(t.id + '_menu', {
bgneal@45 8045 menu_line : 1,
bgneal@45 8046 'class' : t.classPrefix + 'Menu mceNoIcons',
bgneal@45 8047 max_width : 150,
bgneal@45 8048 max_height : 150
bgneal@45 8049 });
bgneal@45 8050
bgneal@45 8051 m.onHideMenu.add(t.hideMenu, t);
bgneal@45 8052
bgneal@45 8053 m.add({
bgneal@45 8054 title : t.settings.title,
bgneal@45 8055 'class' : 'mceMenuItemTitle',
bgneal@45 8056 onclick : function() {
bgneal@45 8057 if (t.settings.onselect('') !== false)
bgneal@45 8058 t.select(''); // Must be runned after
bgneal@45 8059 }
bgneal@45 8060 });
bgneal@45 8061
bgneal@45 8062 each(t.items, function(o) {
bgneal@183 8063 // No value then treat it as a title
bgneal@183 8064 if (o.value === undefined) {
bgneal@183 8065 m.add({
bgneal@183 8066 title : o.title,
bgneal@183 8067 'class' : 'mceMenuItemTitle',
bgneal@183 8068 onclick : function() {
bgneal@183 8069 if (t.settings.onselect('') !== false)
bgneal@183 8070 t.select(''); // Must be runned after
bgneal@183 8071 }
bgneal@183 8072 });
bgneal@183 8073 } else {
bgneal@183 8074 o.id = DOM.uniqueId();
bgneal@183 8075 o.onclick = function() {
bgneal@183 8076 if (t.settings.onselect(o.value) !== false)
bgneal@183 8077 t.select(o.value); // Must be runned after
bgneal@183 8078 };
bgneal@183 8079
bgneal@183 8080 m.add(o);
bgneal@183 8081 }
bgneal@45 8082 });
bgneal@45 8083
bgneal@45 8084 t.onRenderMenu.dispatch(t, m);
bgneal@45 8085 t.menu = m;
bgneal@45 8086 },
bgneal@45 8087
bgneal@45 8088 postRender : function() {
bgneal@45 8089 var t = this, cp = t.classPrefix;
bgneal@45 8090
bgneal@45 8091 Event.add(t.id, 'click', t.showMenu, t);
bgneal@45 8092 Event.add(t.id + '_text', 'focus', function(e) {
bgneal@45 8093 if (!t._focused) {
bgneal@45 8094 t.keyDownHandler = Event.add(t.id + '_text', 'keydown', function(e) {
bgneal@45 8095 var idx = -1, v, kc = e.keyCode;
bgneal@45 8096
bgneal@45 8097 // Find current index
bgneal@45 8098 each(t.items, function(v, i) {
bgneal@45 8099 if (t.selectedValue == v.value)
bgneal@45 8100 idx = i;
bgneal@45 8101 });
bgneal@45 8102
bgneal@45 8103 // Move up/down
bgneal@45 8104 if (kc == 38)
bgneal@45 8105 v = t.items[idx - 1];
bgneal@45 8106 else if (kc == 40)
bgneal@45 8107 v = t.items[idx + 1];
bgneal@45 8108 else if (kc == 13) {
bgneal@45 8109 // Fake select on enter
bgneal@45 8110 v = t.selectedValue;
bgneal@45 8111 t.selectedValue = null; // Needs to be null to fake change
bgneal@45 8112 t.settings.onselect(v);
bgneal@45 8113 return Event.cancel(e);
bgneal@45 8114 }
bgneal@45 8115
bgneal@45 8116 if (v) {
bgneal@45 8117 t.hideMenu();
bgneal@45 8118 t.select(v.value);
bgneal@45 8119 }
bgneal@45 8120 });
bgneal@45 8121 }
bgneal@45 8122
bgneal@45 8123 t._focused = 1;
bgneal@45 8124 });
bgneal@45 8125 Event.add(t.id + '_text', 'blur', function() {Event.remove(t.id + '_text', 'keydown', t.keyDownHandler); t._focused = 0;});
bgneal@45 8126
bgneal@45 8127 // Old IE doesn't have hover on all elements
bgneal@45 8128 if (tinymce.isIE6 || !DOM.boxModel) {
bgneal@45 8129 Event.add(t.id, 'mouseover', function() {
bgneal@45 8130 if (!DOM.hasClass(t.id, cp + 'Disabled'))
bgneal@45 8131 DOM.addClass(t.id, cp + 'Hover');
bgneal@45 8132 });
bgneal@45 8133
bgneal@45 8134 Event.add(t.id, 'mouseout', function() {
bgneal@45 8135 if (!DOM.hasClass(t.id, cp + 'Disabled'))
bgneal@45 8136 DOM.removeClass(t.id, cp + 'Hover');
bgneal@45 8137 });
bgneal@45 8138 }
bgneal@45 8139
bgneal@45 8140 t.onPostRender.dispatch(t, DOM.get(t.id));
bgneal@45 8141 },
bgneal@45 8142
bgneal@45 8143 destroy : function() {
bgneal@45 8144 this.parent();
bgneal@45 8145
bgneal@45 8146 Event.clear(this.id + '_text');
bgneal@183 8147 Event.clear(this.id + '_open');
bgneal@45 8148 }
bgneal@183 8149 });
bgneal@183 8150 })(tinymce);
bgneal@183 8151 (function(tinymce) {
bgneal@45 8152 var DOM = tinymce.DOM, Event = tinymce.dom.Event, each = tinymce.each, Dispatcher = tinymce.util.Dispatcher;
bgneal@45 8153
bgneal@45 8154 tinymce.create('tinymce.ui.NativeListBox:tinymce.ui.ListBox', {
bgneal@45 8155 NativeListBox : function(id, s) {
bgneal@45 8156 this.parent(id, s);
bgneal@45 8157 this.classPrefix = 'mceNativeListBox';
bgneal@45 8158 },
bgneal@45 8159
bgneal@45 8160 setDisabled : function(s) {
bgneal@45 8161 DOM.get(this.id).disabled = s;
bgneal@45 8162 },
bgneal@45 8163
bgneal@45 8164 isDisabled : function() {
bgneal@45 8165 return DOM.get(this.id).disabled;
bgneal@45 8166 },
bgneal@45 8167
bgneal@45 8168 select : function(va) {
bgneal@45 8169 var t = this, fv, f;
bgneal@45 8170
bgneal@45 8171 if (va == undefined)
bgneal@45 8172 return t.selectByIndex(-1);
bgneal@45 8173
bgneal@45 8174 // Is string or number make function selector
bgneal@45 8175 if (va && va.call)
bgneal@45 8176 f = va;
bgneal@45 8177 else {
bgneal@45 8178 f = function(v) {
bgneal@45 8179 return v == va;
bgneal@45 8180 };
bgneal@45 8181 }
bgneal@45 8182
bgneal@45 8183 // Do we need to do something?
bgneal@45 8184 if (va != t.selectedValue) {
bgneal@45 8185 // Find item
bgneal@45 8186 each(t.items, function(o, i) {
bgneal@45 8187 if (f(o.value)) {
bgneal@45 8188 fv = 1;
bgneal@45 8189 t.selectByIndex(i);
bgneal@45 8190 return false;
bgneal@45 8191 }
bgneal@45 8192 });
bgneal@45 8193
bgneal@45 8194 if (!fv)
bgneal@45 8195 t.selectByIndex(-1);
bgneal@45 8196 }
bgneal@45 8197 },
bgneal@45 8198
bgneal@45 8199 selectByIndex : function(idx) {
bgneal@45 8200 DOM.get(this.id).selectedIndex = idx + 1;
bgneal@45 8201 this.selectedValue = this.items[idx] ? this.items[idx].value : null;
bgneal@45 8202 },
bgneal@45 8203
bgneal@45 8204 add : function(n, v, a) {
bgneal@45 8205 var o, t = this;
bgneal@45 8206
bgneal@45 8207 a = a || {};
bgneal@45 8208 a.value = v;
bgneal@45 8209
bgneal@45 8210 if (t.isRendered())
bgneal@45 8211 DOM.add(DOM.get(this.id), 'option', a, n);
bgneal@45 8212
bgneal@45 8213 o = {
bgneal@45 8214 title : n,
bgneal@45 8215 value : v,
bgneal@45 8216 attribs : a
bgneal@45 8217 };
bgneal@45 8218
bgneal@45 8219 t.items.push(o);
bgneal@45 8220 t.onAdd.dispatch(t, o);
bgneal@45 8221 },
bgneal@45 8222
bgneal@45 8223 getLength : function() {
bgneal@183 8224 return this.items.length;
bgneal@45 8225 },
bgneal@45 8226
bgneal@45 8227 renderHTML : function() {
bgneal@45 8228 var h, t = this;
bgneal@45 8229
bgneal@45 8230 h = DOM.createHTML('option', {value : ''}, '-- ' + t.settings.title + ' --');
bgneal@45 8231
bgneal@45 8232 each(t.items, function(it) {
bgneal@45 8233 h += DOM.createHTML('option', {value : it.value}, it.title);
bgneal@45 8234 });
bgneal@45 8235
bgneal@45 8236 h = DOM.createHTML('select', {id : t.id, 'class' : 'mceNativeListBox'}, h);
bgneal@45 8237
bgneal@45 8238 return h;
bgneal@45 8239 },
bgneal@45 8240
bgneal@45 8241 postRender : function() {
bgneal@45 8242 var t = this, ch;
bgneal@45 8243
bgneal@45 8244 t.rendered = true;
bgneal@45 8245
bgneal@45 8246 function onChange(e) {
bgneal@45 8247 var v = t.items[e.target.selectedIndex - 1];
bgneal@45 8248
bgneal@45 8249 if (v && (v = v.value)) {
bgneal@45 8250 t.onChange.dispatch(t, v);
bgneal@45 8251
bgneal@45 8252 if (t.settings.onselect)
bgneal@45 8253 t.settings.onselect(v);
bgneal@45 8254 }
bgneal@45 8255 };
bgneal@45 8256
bgneal@45 8257 Event.add(t.id, 'change', onChange);
bgneal@45 8258
bgneal@45 8259 // Accessibility keyhandler
bgneal@45 8260 Event.add(t.id, 'keydown', function(e) {
bgneal@45 8261 var bf;
bgneal@45 8262
bgneal@45 8263 Event.remove(t.id, 'change', ch);
bgneal@45 8264
bgneal@45 8265 bf = Event.add(t.id, 'blur', function() {
bgneal@45 8266 Event.add(t.id, 'change', onChange);
bgneal@45 8267 Event.remove(t.id, 'blur', bf);
bgneal@45 8268 });
bgneal@45 8269
bgneal@45 8270 if (e.keyCode == 13 || e.keyCode == 32) {
bgneal@45 8271 onChange(e);
bgneal@45 8272 return Event.cancel(e);
bgneal@45 8273 }
bgneal@45 8274 });
bgneal@45 8275
bgneal@45 8276 t.onPostRender.dispatch(t, DOM.get(t.id));
bgneal@45 8277 }
bgneal@183 8278 });
bgneal@183 8279 })(tinymce);
bgneal@183 8280 (function(tinymce) {
bgneal@45 8281 var DOM = tinymce.DOM, Event = tinymce.dom.Event, each = tinymce.each;
bgneal@45 8282
bgneal@45 8283 tinymce.create('tinymce.ui.MenuButton:tinymce.ui.Button', {
bgneal@45 8284 MenuButton : function(id, s) {
bgneal@45 8285 this.parent(id, s);
bgneal@183 8286
bgneal@45 8287 this.onRenderMenu = new tinymce.util.Dispatcher(this);
bgneal@183 8288
bgneal@45 8289 s.menu_container = s.menu_container || DOM.doc.body;
bgneal@45 8290 },
bgneal@45 8291
bgneal@45 8292 showMenu : function() {
bgneal@45 8293 var t = this, p1, p2, e = DOM.get(t.id), m;
bgneal@45 8294
bgneal@45 8295 if (t.isDisabled())
bgneal@45 8296 return;
bgneal@45 8297
bgneal@45 8298 if (!t.isMenuRendered) {
bgneal@45 8299 t.renderMenu();
bgneal@45 8300 t.isMenuRendered = true;
bgneal@45 8301 }
bgneal@45 8302
bgneal@45 8303 if (t.isMenuVisible)
bgneal@45 8304 return t.hideMenu();
bgneal@45 8305
bgneal@45 8306 p1 = DOM.getPos(t.settings.menu_container);
bgneal@45 8307 p2 = DOM.getPos(e);
bgneal@45 8308
bgneal@45 8309 m = t.menu;
bgneal@45 8310 m.settings.offset_x = p2.x;
bgneal@45 8311 m.settings.offset_y = p2.y;
bgneal@45 8312 m.settings.vp_offset_x = p2.x;
bgneal@45 8313 m.settings.vp_offset_y = p2.y;
bgneal@45 8314 m.settings.keyboard_focus = t._focused;
bgneal@45 8315 m.showMenu(0, e.clientHeight);
bgneal@45 8316
bgneal@45 8317 Event.add(DOM.doc, 'mousedown', t.hideMenu, t);
bgneal@45 8318 t.setState('Selected', 1);
bgneal@45 8319
bgneal@45 8320 t.isMenuVisible = 1;
bgneal@45 8321 },
bgneal@45 8322
bgneal@45 8323 renderMenu : function() {
bgneal@45 8324 var t = this, m;
bgneal@45 8325
bgneal@45 8326 m = t.settings.control_manager.createDropMenu(t.id + '_menu', {
bgneal@45 8327 menu_line : 1,
bgneal@45 8328 'class' : this.classPrefix + 'Menu',
bgneal@45 8329 icons : t.settings.icons
bgneal@45 8330 });
bgneal@45 8331
bgneal@45 8332 m.onHideMenu.add(t.hideMenu, t);
bgneal@45 8333
bgneal@45 8334 t.onRenderMenu.dispatch(t, m);
bgneal@45 8335 t.menu = m;
bgneal@45 8336 },
bgneal@45 8337
bgneal@45 8338 hideMenu : function(e) {
bgneal@45 8339 var t = this;
bgneal@45 8340
bgneal@45 8341 // Prevent double toogles by canceling the mouse click event to the button
bgneal@45 8342 if (e && e.type == "mousedown" && DOM.getParent(e.target, function(e) {return e.id === t.id || e.id === t.id + '_open';}))
bgneal@45 8343 return;
bgneal@45 8344
bgneal@45 8345 if (!e || !DOM.getParent(e.target, '.mceMenu')) {
bgneal@45 8346 t.setState('Selected', 0);
bgneal@45 8347 Event.remove(DOM.doc, 'mousedown', t.hideMenu, t);
bgneal@45 8348 if (t.menu)
bgneal@45 8349 t.menu.hideMenu();
bgneal@45 8350 }
bgneal@45 8351
bgneal@45 8352 t.isMenuVisible = 0;
bgneal@45 8353 },
bgneal@45 8354
bgneal@45 8355 postRender : function() {
bgneal@45 8356 var t = this, s = t.settings;
bgneal@45 8357
bgneal@45 8358 Event.add(t.id, 'click', function() {
bgneal@45 8359 if (!t.isDisabled()) {
bgneal@45 8360 if (s.onclick)
bgneal@45 8361 s.onclick(t.value);
bgneal@45 8362
bgneal@45 8363 t.showMenu();
bgneal@45 8364 }
bgneal@45 8365 });
bgneal@45 8366 }
bgneal@183 8367 });
bgneal@45 8368 })(tinymce);
bgneal@183 8369
bgneal@45 8370 (function(tinymce) {
bgneal@45 8371 var DOM = tinymce.DOM, Event = tinymce.dom.Event, each = tinymce.each;
bgneal@45 8372
bgneal@45 8373 tinymce.create('tinymce.ui.SplitButton:tinymce.ui.MenuButton', {
bgneal@45 8374 SplitButton : function(id, s) {
bgneal@45 8375 this.parent(id, s);
bgneal@45 8376 this.classPrefix = 'mceSplitButton';
bgneal@45 8377 },
bgneal@45 8378
bgneal@45 8379 renderHTML : function() {
bgneal@45 8380 var h, t = this, s = t.settings, h1;
bgneal@45 8381
bgneal@45 8382 h = '<tbody><tr>';
bgneal@45 8383
bgneal@45 8384 if (s.image)
bgneal@45 8385 h1 = DOM.createHTML('img ', {src : s.image, 'class' : 'mceAction ' + s['class']});
bgneal@45 8386 else
bgneal@45 8387 h1 = DOM.createHTML('span', {'class' : 'mceAction ' + s['class']}, '');
bgneal@45 8388
bgneal@45 8389 h += '<td>' + DOM.createHTML('a', {id : t.id + '_action', href : 'javascript:;', 'class' : 'mceAction ' + s['class'], onclick : "return false;", onmousedown : 'return false;', title : s.title}, h1) + '</td>';
bgneal@45 8390
bgneal@45 8391 h1 = DOM.createHTML('span', {'class' : 'mceOpen ' + s['class']});
bgneal@45 8392 h += '<td>' + DOM.createHTML('a', {id : t.id + '_open', href : 'javascript:;', 'class' : 'mceOpen ' + s['class'], onclick : "return false;", onmousedown : 'return false;', title : s.title}, h1) + '</td>';
bgneal@45 8393
bgneal@45 8394 h += '</tr></tbody>';
bgneal@45 8395
bgneal@45 8396 return DOM.createHTML('table', {id : t.id, 'class' : 'mceSplitButton mceSplitButtonEnabled ' + s['class'], cellpadding : '0', cellspacing : '0', onmousedown : 'return false;', title : s.title}, h);
bgneal@45 8397 },
bgneal@45 8398
bgneal@45 8399 postRender : function() {
bgneal@45 8400 var t = this, s = t.settings;
bgneal@45 8401
bgneal@45 8402 if (s.onclick) {
bgneal@45 8403 Event.add(t.id + '_action', 'click', function() {
bgneal@45 8404 if (!t.isDisabled())
bgneal@45 8405 s.onclick(t.value);
bgneal@45 8406 });
bgneal@45 8407 }
bgneal@45 8408
bgneal@45 8409 Event.add(t.id + '_open', 'click', t.showMenu, t);
bgneal@45 8410 Event.add(t.id + '_open', 'focus', function() {t._focused = 1;});
bgneal@45 8411 Event.add(t.id + '_open', 'blur', function() {t._focused = 0;});
bgneal@45 8412
bgneal@45 8413 // Old IE doesn't have hover on all elements
bgneal@45 8414 if (tinymce.isIE6 || !DOM.boxModel) {
bgneal@45 8415 Event.add(t.id, 'mouseover', function() {
bgneal@45 8416 if (!DOM.hasClass(t.id, 'mceSplitButtonDisabled'))
bgneal@45 8417 DOM.addClass(t.id, 'mceSplitButtonHover');
bgneal@45 8418 });
bgneal@45 8419
bgneal@45 8420 Event.add(t.id, 'mouseout', function() {
bgneal@45 8421 if (!DOM.hasClass(t.id, 'mceSplitButtonDisabled'))
bgneal@45 8422 DOM.removeClass(t.id, 'mceSplitButtonHover');
bgneal@45 8423 });
bgneal@45 8424 }
bgneal@45 8425 },
bgneal@45 8426
bgneal@45 8427 destroy : function() {
bgneal@45 8428 this.parent();
bgneal@45 8429
bgneal@45 8430 Event.clear(this.id + '_action');
bgneal@45 8431 Event.clear(this.id + '_open');
bgneal@45 8432 }
bgneal@183 8433 });
bgneal@45 8434 })(tinymce);
bgneal@183 8435
bgneal@45 8436 (function(tinymce) {
bgneal@45 8437 var DOM = tinymce.DOM, Event = tinymce.dom.Event, is = tinymce.is, each = tinymce.each;
bgneal@45 8438
bgneal@45 8439 tinymce.create('tinymce.ui.ColorSplitButton:tinymce.ui.SplitButton', {
bgneal@45 8440 ColorSplitButton : function(id, s) {
bgneal@45 8441 var t = this;
bgneal@45 8442
bgneal@45 8443 t.parent(id, s);
bgneal@45 8444
bgneal@45 8445 t.settings = s = tinymce.extend({
bgneal@45 8446 colors : '000000,993300,333300,003300,003366,000080,333399,333333,800000,FF6600,808000,008000,008080,0000FF,666699,808080,FF0000,FF9900,99CC00,339966,33CCCC,3366FF,800080,999999,FF00FF,FFCC00,FFFF00,00FF00,00FFFF,00CCFF,993366,C0C0C0,FF99CC,FFCC99,FFFF99,CCFFCC,CCFFFF,99CCFF,CC99FF,FFFFFF',
bgneal@45 8447 grid_width : 8,
bgneal@45 8448 default_color : '#888888'
bgneal@45 8449 }, t.settings);
bgneal@45 8450
bgneal@45 8451 t.onShowMenu = new tinymce.util.Dispatcher(t);
bgneal@183 8452
bgneal@45 8453 t.onHideMenu = new tinymce.util.Dispatcher(t);
bgneal@45 8454
bgneal@45 8455 t.value = s.default_color;
bgneal@45 8456 },
bgneal@45 8457
bgneal@45 8458 showMenu : function() {
bgneal@45 8459 var t = this, r, p, e, p2;
bgneal@45 8460
bgneal@45 8461 if (t.isDisabled())
bgneal@45 8462 return;
bgneal@45 8463
bgneal@45 8464 if (!t.isMenuRendered) {
bgneal@45 8465 t.renderMenu();
bgneal@45 8466 t.isMenuRendered = true;
bgneal@45 8467 }
bgneal@45 8468
bgneal@45 8469 if (t.isMenuVisible)
bgneal@45 8470 return t.hideMenu();
bgneal@45 8471
bgneal@45 8472 e = DOM.get(t.id);
bgneal@45 8473 DOM.show(t.id + '_menu');
bgneal@45 8474 DOM.addClass(e, 'mceSplitButtonSelected');
bgneal@45 8475 p2 = DOM.getPos(e);
bgneal@45 8476 DOM.setStyles(t.id + '_menu', {
bgneal@45 8477 left : p2.x,
bgneal@45 8478 top : p2.y + e.clientHeight,
bgneal@45 8479 zIndex : 200000
bgneal@45 8480 });
bgneal@45 8481 e = 0;
bgneal@45 8482
bgneal@45 8483 Event.add(DOM.doc, 'mousedown', t.hideMenu, t);
bgneal@183 8484 t.onShowMenu.dispatch(t);
bgneal@45 8485
bgneal@45 8486 if (t._focused) {
bgneal@45 8487 t._keyHandler = Event.add(t.id + '_menu', 'keydown', function(e) {
bgneal@45 8488 if (e.keyCode == 27)
bgneal@45 8489 t.hideMenu();
bgneal@45 8490 });
bgneal@45 8491
bgneal@45 8492 DOM.select('a', t.id + '_menu')[0].focus(); // Select first link
bgneal@45 8493 }
bgneal@45 8494
bgneal@45 8495 t.isMenuVisible = 1;
bgneal@45 8496 },
bgneal@45 8497
bgneal@45 8498 hideMenu : function(e) {
bgneal@45 8499 var t = this;
bgneal@45 8500
bgneal@45 8501 // Prevent double toogles by canceling the mouse click event to the button
bgneal@45 8502 if (e && e.type == "mousedown" && DOM.getParent(e.target, function(e) {return e.id === t.id + '_open';}))
bgneal@45 8503 return;
bgneal@45 8504
bgneal@45 8505 if (!e || !DOM.getParent(e.target, '.mceSplitButtonMenu')) {
bgneal@45 8506 DOM.removeClass(t.id, 'mceSplitButtonSelected');
bgneal@45 8507 Event.remove(DOM.doc, 'mousedown', t.hideMenu, t);
bgneal@45 8508 Event.remove(t.id + '_menu', 'keydown', t._keyHandler);
bgneal@45 8509 DOM.hide(t.id + '_menu');
bgneal@45 8510 }
bgneal@45 8511
bgneal@45 8512 t.onHideMenu.dispatch(t);
bgneal@45 8513
bgneal@45 8514 t.isMenuVisible = 0;
bgneal@45 8515 },
bgneal@45 8516
bgneal@45 8517 renderMenu : function() {
bgneal@45 8518 var t = this, m, i = 0, s = t.settings, n, tb, tr, w;
bgneal@45 8519
bgneal@45 8520 w = DOM.add(s.menu_container, 'div', {id : t.id + '_menu', 'class' : s['menu_class'] + ' ' + s['class'], style : 'position:absolute;left:0;top:-1000px;'});
bgneal@45 8521 m = DOM.add(w, 'div', {'class' : s['class'] + ' mceSplitButtonMenu'});
bgneal@45 8522 DOM.add(m, 'span', {'class' : 'mceMenuLine'});
bgneal@45 8523
bgneal@45 8524 n = DOM.add(m, 'table', {'class' : 'mceColorSplitMenu'});
bgneal@45 8525 tb = DOM.add(n, 'tbody');
bgneal@45 8526
bgneal@45 8527 // Generate color grid
bgneal@45 8528 i = 0;
bgneal@45 8529 each(is(s.colors, 'array') ? s.colors : s.colors.split(','), function(c) {
bgneal@45 8530 c = c.replace(/^#/, '');
bgneal@45 8531
bgneal@45 8532 if (!i--) {
bgneal@45 8533 tr = DOM.add(tb, 'tr');
bgneal@45 8534 i = s.grid_width - 1;
bgneal@45 8535 }
bgneal@45 8536
bgneal@45 8537 n = DOM.add(tr, 'td');
bgneal@45 8538
bgneal@45 8539 n = DOM.add(n, 'a', {
bgneal@45 8540 href : 'javascript:;',
bgneal@45 8541 style : {
bgneal@45 8542 backgroundColor : '#' + c
bgneal@45 8543 },
bgneal@183 8544 _mce_color : '#' + c
bgneal@45 8545 });
bgneal@45 8546 });
bgneal@45 8547
bgneal@45 8548 if (s.more_colors_func) {
bgneal@45 8549 n = DOM.add(tb, 'tr');
bgneal@45 8550 n = DOM.add(n, 'td', {colspan : s.grid_width, 'class' : 'mceMoreColors'});
bgneal@45 8551 n = DOM.add(n, 'a', {id : t.id + '_more', href : 'javascript:;', onclick : 'return false;', 'class' : 'mceMoreColors'}, s.more_colors_title);
bgneal@45 8552
bgneal@45 8553 Event.add(n, 'click', function(e) {
bgneal@45 8554 s.more_colors_func.call(s.more_colors_scope || this);
bgneal@45 8555 return Event.cancel(e); // Cancel to fix onbeforeunload problem
bgneal@45 8556 });
bgneal@45 8557 }
bgneal@45 8558
bgneal@45 8559 DOM.addClass(m, 'mceColorSplitMenu');
bgneal@45 8560
bgneal@45 8561 Event.add(t.id + '_menu', 'click', function(e) {
bgneal@45 8562 var c;
bgneal@45 8563
bgneal@45 8564 e = e.target;
bgneal@45 8565
bgneal@183 8566 if (e.nodeName == 'A' && (c = e.getAttribute('_mce_color')))
bgneal@45 8567 t.setColor(c);
bgneal@45 8568
bgneal@45 8569 return Event.cancel(e); // Prevent IE auto save warning
bgneal@45 8570 });
bgneal@45 8571
bgneal@45 8572 return w;
bgneal@45 8573 },
bgneal@45 8574
bgneal@45 8575 setColor : function(c) {
bgneal@45 8576 var t = this;
bgneal@45 8577
bgneal@45 8578 DOM.setStyle(t.id + '_preview', 'backgroundColor', c);
bgneal@45 8579
bgneal@45 8580 t.value = c;
bgneal@45 8581 t.hideMenu();
bgneal@45 8582 t.settings.onselect(c);
bgneal@45 8583 },
bgneal@45 8584
bgneal@45 8585 postRender : function() {
bgneal@45 8586 var t = this, id = t.id;
bgneal@45 8587
bgneal@45 8588 t.parent();
bgneal@45 8589 DOM.add(id + '_action', 'div', {id : id + '_preview', 'class' : 'mceColorPreview'});
bgneal@45 8590 DOM.setStyle(t.id + '_preview', 'backgroundColor', t.value);
bgneal@45 8591 },
bgneal@45 8592
bgneal@45 8593 destroy : function() {
bgneal@45 8594 this.parent();
bgneal@45 8595
bgneal@45 8596 Event.clear(this.id + '_menu');
bgneal@45 8597 Event.clear(this.id + '_more');
bgneal@45 8598 DOM.remove(this.id + '_menu');
bgneal@45 8599 }
bgneal@183 8600 });
bgneal@45 8601 })(tinymce);
bgneal@183 8602
bgneal@45 8603 tinymce.create('tinymce.ui.Toolbar:tinymce.ui.Container', {
bgneal@45 8604 renderHTML : function() {
bgneal@45 8605 var t = this, h = '', c, co, dom = tinymce.DOM, s = t.settings, i, pr, nx, cl;
bgneal@45 8606
bgneal@45 8607 cl = t.controls;
bgneal@45 8608 for (i=0; i<cl.length; i++) {
bgneal@45 8609 // Get current control, prev control, next control and if the control is a list box or not
bgneal@45 8610 co = cl[i];
bgneal@45 8611 pr = cl[i - 1];
bgneal@45 8612 nx = cl[i + 1];
bgneal@45 8613
bgneal@45 8614 // Add toolbar start
bgneal@45 8615 if (i === 0) {
bgneal@45 8616 c = 'mceToolbarStart';
bgneal@45 8617
bgneal@45 8618 if (co.Button)
bgneal@45 8619 c += ' mceToolbarStartButton';
bgneal@45 8620 else if (co.SplitButton)
bgneal@45 8621 c += ' mceToolbarStartSplitButton';
bgneal@45 8622 else if (co.ListBox)
bgneal@45 8623 c += ' mceToolbarStartListBox';
bgneal@45 8624
bgneal@45 8625 h += dom.createHTML('td', {'class' : c}, dom.createHTML('span', null, '<!-- IE -->'));
bgneal@45 8626 }
bgneal@45 8627
bgneal@45 8628 // Add toolbar end before list box and after the previous button
bgneal@45 8629 // This is to fix the o2k7 editor skins
bgneal@45 8630 if (pr && co.ListBox) {
bgneal@45 8631 if (pr.Button || pr.SplitButton)
bgneal@45 8632 h += dom.createHTML('td', {'class' : 'mceToolbarEnd'}, dom.createHTML('span', null, '<!-- IE -->'));
bgneal@45 8633 }
bgneal@45 8634
bgneal@45 8635 // Render control HTML
bgneal@45 8636
bgneal@45 8637 // IE 8 quick fix, needed to propertly generate a hit area for anchors
bgneal@45 8638 if (dom.stdMode)
bgneal@45 8639 h += '<td style="position: relative">' + co.renderHTML() + '</td>';
bgneal@45 8640 else
bgneal@45 8641 h += '<td>' + co.renderHTML() + '</td>';
bgneal@45 8642
bgneal@45 8643 // Add toolbar start after list box and before the next button
bgneal@45 8644 // This is to fix the o2k7 editor skins
bgneal@45 8645 if (nx && co.ListBox) {
bgneal@45 8646 if (nx.Button || nx.SplitButton)
bgneal@45 8647 h += dom.createHTML('td', {'class' : 'mceToolbarStart'}, dom.createHTML('span', null, '<!-- IE -->'));
bgneal@45 8648 }
bgneal@45 8649 }
bgneal@45 8650
bgneal@45 8651 c = 'mceToolbarEnd';
bgneal@45 8652
bgneal@45 8653 if (co.Button)
bgneal@45 8654 c += ' mceToolbarEndButton';
bgneal@45 8655 else if (co.SplitButton)
bgneal@45 8656 c += ' mceToolbarEndSplitButton';
bgneal@45 8657 else if (co.ListBox)
bgneal@45 8658 c += ' mceToolbarEndListBox';
bgneal@45 8659
bgneal@45 8660 h += dom.createHTML('td', {'class' : c}, dom.createHTML('span', null, '<!-- IE -->'));
bgneal@45 8661
bgneal@45 8662 return dom.createHTML('table', {id : t.id, 'class' : 'mceToolbar' + (s['class'] ? ' ' + s['class'] : ''), cellpadding : '0', cellspacing : '0', align : t.settings.align || ''}, '<tbody><tr>' + h + '</tr></tbody>');
bgneal@45 8663 }
bgneal@183 8664 });
bgneal@183 8665
bgneal@45 8666 (function(tinymce) {
bgneal@45 8667 var Dispatcher = tinymce.util.Dispatcher, each = tinymce.each;
bgneal@45 8668
bgneal@45 8669 tinymce.create('tinymce.AddOnManager', {
bgneal@45 8670 items : [],
bgneal@45 8671 urls : {},
bgneal@45 8672 lookup : {},
bgneal@183 8673
bgneal@45 8674 onAdd : new Dispatcher(this),
bgneal@45 8675
bgneal@45 8676 get : function(n) {
bgneal@45 8677 return this.lookup[n];
bgneal@45 8678 },
bgneal@45 8679
bgneal@45 8680 requireLangPack : function(n) {
bgneal@183 8681 var s = tinymce.settings;
bgneal@183 8682
bgneal@183 8683 if (s && s.language)
bgneal@183 8684 tinymce.ScriptLoader.add(this.urls[n] + '/langs/' + s.language + '.js');
bgneal@45 8685 },
bgneal@45 8686
bgneal@45 8687 add : function(id, o) {
bgneal@45 8688 this.items.push(o);
bgneal@45 8689 this.lookup[id] = o;
bgneal@45 8690 this.onAdd.dispatch(this, id, o);
bgneal@45 8691
bgneal@45 8692 return o;
bgneal@45 8693 },
bgneal@45 8694
bgneal@45 8695 load : function(n, u, cb, s) {
bgneal@45 8696 var t = this;
bgneal@45 8697
bgneal@45 8698 if (t.urls[n])
bgneal@45 8699 return;
bgneal@45 8700
bgneal@45 8701 if (u.indexOf('/') != 0 && u.indexOf('://') == -1)
bgneal@45 8702 u = tinymce.baseURL + '/' + u;
bgneal@45 8703
bgneal@45 8704 t.urls[n] = u.substring(0, u.lastIndexOf('/'));
bgneal@45 8705 tinymce.ScriptLoader.add(u, cb, s);
bgneal@45 8706 }
bgneal@183 8707 });
bgneal@45 8708
bgneal@45 8709 // Create plugin and theme managers
bgneal@45 8710 tinymce.PluginManager = new tinymce.AddOnManager();
bgneal@45 8711 tinymce.ThemeManager = new tinymce.AddOnManager();
bgneal@183 8712 }(tinymce));
bgneal@183 8713
bgneal@183 8714 (function(tinymce) {
bgneal@45 8715 // Shorten names
bgneal@183 8716 var each = tinymce.each, extend = tinymce.extend,
bgneal@183 8717 DOM = tinymce.DOM, Event = tinymce.dom.Event,
bgneal@183 8718 ThemeManager = tinymce.ThemeManager, PluginManager = tinymce.PluginManager,
bgneal@183 8719 explode = tinymce.explode,
bgneal@183 8720 Dispatcher = tinymce.util.Dispatcher, undefined, instanceCounter = 0;
bgneal@183 8721
bgneal@183 8722 // Setup some URLs where the editor API is located and where the document is
bgneal@183 8723 tinymce.documentBaseURL = window.location.href.replace(/[\?#].*$/, '').replace(/[\/\\][^\/]+$/, '');
bgneal@183 8724 if (!/[\/\\]$/.test(tinymce.documentBaseURL))
bgneal@183 8725 tinymce.documentBaseURL += '/';
bgneal@183 8726
bgneal@183 8727 tinymce.baseURL = new tinymce.util.URI(tinymce.documentBaseURL).toAbsolute(tinymce.baseURL);
bgneal@183 8728
bgneal@183 8729 tinymce.baseURI = new tinymce.util.URI(tinymce.baseURL);
bgneal@183 8730
bgneal@183 8731 // Add before unload listener
bgneal@183 8732 // This was required since IE was leaking memory if you added and removed beforeunload listeners
bgneal@183 8733 // with attachEvent/detatchEvent so this only adds one listener and instances can the attach to the onBeforeUnload event
bgneal@183 8734 tinymce.onBeforeUnload = new Dispatcher(tinymce);
bgneal@183 8735
bgneal@183 8736 // Must be on window or IE will leak if the editor is placed in frame or iframe
bgneal@183 8737 Event.add(window, 'beforeunload', function(e) {
bgneal@183 8738 tinymce.onBeforeUnload.dispatch(tinymce, e);
bgneal@183 8739 });
bgneal@183 8740
bgneal@183 8741 tinymce.onAddEditor = new Dispatcher(tinymce);
bgneal@183 8742
bgneal@183 8743 tinymce.onRemoveEditor = new Dispatcher(tinymce);
bgneal@183 8744
bgneal@183 8745 tinymce.EditorManager = extend(tinymce, {
bgneal@183 8746 editors : [],
bgneal@183 8747
bgneal@45 8748 i18n : {},
bgneal@183 8749
bgneal@45 8750 activeEditor : null,
bgneal@45 8751
bgneal@45 8752 init : function(s) {
bgneal@183 8753 var t = this, pl, sl = tinymce.ScriptLoader, e, el = [], ed;
bgneal@45 8754
bgneal@45 8755 function execCallback(se, n, s) {
bgneal@45 8756 var f = se[n];
bgneal@45 8757
bgneal@45 8758 if (!f)
bgneal@45 8759 return;
bgneal@45 8760
bgneal@45 8761 if (tinymce.is(f, 'string')) {
bgneal@45 8762 s = f.replace(/\.\w+$/, '');
bgneal@45 8763 s = s ? tinymce.resolve(s) : 0;
bgneal@45 8764 f = tinymce.resolve(f);
bgneal@45 8765 }
bgneal@45 8766
bgneal@45 8767 return f.apply(s || this, Array.prototype.slice.call(arguments, 2));
bgneal@45 8768 };
bgneal@45 8769
bgneal@45 8770 s = extend({
bgneal@45 8771 theme : "simple",
bgneal@183 8772 language : "en"
bgneal@45 8773 }, s);
bgneal@45 8774
bgneal@45 8775 t.settings = s;
bgneal@45 8776
bgneal@45 8777 // Legacy call
bgneal@45 8778 Event.add(document, 'init', function() {
bgneal@45 8779 var l, co;
bgneal@45 8780
bgneal@45 8781 execCallback(s, 'onpageload');
bgneal@45 8782
bgneal@45 8783 switch (s.mode) {
bgneal@45 8784 case "exact":
bgneal@45 8785 l = s.elements || '';
bgneal@45 8786
bgneal@45 8787 if(l.length > 0) {
bgneal@45 8788 each(explode(l), function(v) {
bgneal@45 8789 if (DOM.get(v)) {
bgneal@45 8790 ed = new tinymce.Editor(v, s);
bgneal@45 8791 el.push(ed);
bgneal@45 8792 ed.render(1);
bgneal@45 8793 } else {
bgneal@45 8794 each(document.forms, function(f) {
bgneal@45 8795 each(f.elements, function(e) {
bgneal@45 8796 if (e.name === v) {
bgneal@183 8797 v = 'mce_editor_' + instanceCounter++;
bgneal@45 8798 DOM.setAttrib(e, 'id', v);
bgneal@45 8799
bgneal@45 8800 ed = new tinymce.Editor(v, s);
bgneal@45 8801 el.push(ed);
bgneal@45 8802 ed.render(1);
bgneal@45 8803 }
bgneal@45 8804 });
bgneal@45 8805 });
bgneal@45 8806 }
bgneal@45 8807 });
bgneal@45 8808 }
bgneal@45 8809 break;
bgneal@45 8810
bgneal@45 8811 case "textareas":
bgneal@45 8812 case "specific_textareas":
bgneal@45 8813 function hasClass(n, c) {
bgneal@45 8814 return c.constructor === RegExp ? c.test(n.className) : DOM.hasClass(n, c);
bgneal@45 8815 };
bgneal@45 8816
bgneal@45 8817 each(DOM.select('textarea'), function(v) {
bgneal@45 8818 if (s.editor_deselector && hasClass(v, s.editor_deselector))
bgneal@45 8819 return;
bgneal@45 8820
bgneal@45 8821 if (!s.editor_selector || hasClass(v, s.editor_selector)) {
bgneal@45 8822 // Can we use the name
bgneal@45 8823 e = DOM.get(v.name);
bgneal@45 8824 if (!v.id && !e)
bgneal@45 8825 v.id = v.name;
bgneal@45 8826
bgneal@45 8827 // Generate unique name if missing or already exists
bgneal@45 8828 if (!v.id || t.get(v.id))
bgneal@45 8829 v.id = DOM.uniqueId();
bgneal@45 8830
bgneal@45 8831 ed = new tinymce.Editor(v.id, s);
bgneal@45 8832 el.push(ed);
bgneal@45 8833 ed.render(1);
bgneal@45 8834 }
bgneal@45 8835 });
bgneal@45 8836 break;
bgneal@45 8837 }
bgneal@45 8838
bgneal@45 8839 // Call onInit when all editors are initialized
bgneal@45 8840 if (s.oninit) {
bgneal@45 8841 l = co = 0;
bgneal@45 8842
bgneal@183 8843 each(el, function(ed) {
bgneal@45 8844 co++;
bgneal@45 8845
bgneal@45 8846 if (!ed.initialized) {
bgneal@45 8847 // Wait for it
bgneal@45 8848 ed.onInit.add(function() {
bgneal@45 8849 l++;
bgneal@45 8850
bgneal@45 8851 // All done
bgneal@45 8852 if (l == co)
bgneal@45 8853 execCallback(s, 'oninit');
bgneal@45 8854 });
bgneal@45 8855 } else
bgneal@45 8856 l++;
bgneal@45 8857
bgneal@45 8858 // All done
bgneal@45 8859 if (l == co)
bgneal@45 8860 execCallback(s, 'oninit');
bgneal@45 8861 });
bgneal@45 8862 }
bgneal@45 8863 });
bgneal@45 8864 },
bgneal@45 8865
bgneal@45 8866 get : function(id) {
bgneal@183 8867 if (id === undefined)
bgneal@183 8868 return this.editors;
bgneal@183 8869
bgneal@45 8870 return this.editors[id];
bgneal@45 8871 },
bgneal@45 8872
bgneal@45 8873 getInstanceById : function(id) {
bgneal@45 8874 return this.get(id);
bgneal@45 8875 },
bgneal@45 8876
bgneal@183 8877 add : function(editor) {
bgneal@183 8878 var self = this, editors = self.editors;
bgneal@183 8879
bgneal@183 8880 // Add named and index editor instance
bgneal@183 8881 editors[editor.id] = editor;
bgneal@183 8882 editors.push(editor);
bgneal@183 8883
bgneal@183 8884 self._setActive(editor);
bgneal@183 8885 self.onAddEditor.dispatch(self, editor);
bgneal@183 8886
bgneal@183 8887
bgneal@183 8888 return editor;
bgneal@183 8889 },
bgneal@183 8890
bgneal@183 8891 remove : function(editor) {
bgneal@183 8892 var t = this, i, editors = t.editors;
bgneal@45 8893
bgneal@45 8894 // Not in the collection
bgneal@183 8895 if (!editors[editor.id])
bgneal@45 8896 return null;
bgneal@45 8897
bgneal@183 8898 delete editors[editor.id];
bgneal@183 8899
bgneal@183 8900 for (i = 0; i < editors.length; i++) {
bgneal@183 8901 if (editors[i] == editor) {
bgneal@183 8902 editors.splice(i, 1);
bgneal@183 8903 break;
bgneal@183 8904 }
bgneal@183 8905 }
bgneal@45 8906
bgneal@45 8907 // Select another editor since the active one was removed
bgneal@183 8908 if (t.activeEditor == editor)
bgneal@183 8909 t._setActive(editors[0]);
bgneal@183 8910
bgneal@183 8911 editor.destroy();
bgneal@183 8912 t.onRemoveEditor.dispatch(t, editor);
bgneal@183 8913
bgneal@183 8914 return editor;
bgneal@45 8915 },
bgneal@45 8916
bgneal@45 8917 execCommand : function(c, u, v) {
bgneal@45 8918 var t = this, ed = t.get(v), w;
bgneal@45 8919
bgneal@45 8920 // Manager commands
bgneal@45 8921 switch (c) {
bgneal@45 8922 case "mceFocus":
bgneal@45 8923 ed.focus();
bgneal@45 8924 return true;
bgneal@45 8925
bgneal@45 8926 case "mceAddEditor":
bgneal@45 8927 case "mceAddControl":
bgneal@45 8928 if (!t.get(v))
bgneal@45 8929 new tinymce.Editor(v, t.settings).render();
bgneal@45 8930
bgneal@45 8931 return true;
bgneal@45 8932
bgneal@45 8933 case "mceAddFrameControl":
bgneal@45 8934 w = v.window;
bgneal@45 8935
bgneal@45 8936 // Add tinyMCE global instance and tinymce namespace to specified window
bgneal@45 8937 w.tinyMCE = tinyMCE;
bgneal@45 8938 w.tinymce = tinymce;
bgneal@45 8939
bgneal@45 8940 tinymce.DOM.doc = w.document;
bgneal@45 8941 tinymce.DOM.win = w;
bgneal@45 8942
bgneal@45 8943 ed = new tinymce.Editor(v.element_id, v);
bgneal@45 8944 ed.render();
bgneal@45 8945
bgneal@45 8946 // Fix IE memory leaks
bgneal@45 8947 if (tinymce.isIE) {
bgneal@45 8948 function clr() {
bgneal@45 8949 ed.destroy();
bgneal@45 8950 w.detachEvent('onunload', clr);
bgneal@45 8951 w = w.tinyMCE = w.tinymce = null; // IE leak
bgneal@45 8952 };
bgneal@45 8953
bgneal@45 8954 w.attachEvent('onunload', clr);
bgneal@45 8955 }
bgneal@45 8956
bgneal@45 8957 v.page_window = null;
bgneal@45 8958
bgneal@45 8959 return true;
bgneal@45 8960
bgneal@45 8961 case "mceRemoveEditor":
bgneal@45 8962 case "mceRemoveControl":
bgneal@45 8963 if (ed)
bgneal@45 8964 ed.remove();
bgneal@45 8965
bgneal@45 8966 return true;
bgneal@45 8967
bgneal@45 8968 case 'mceToggleEditor':
bgneal@45 8969 if (!ed) {
bgneal@45 8970 t.execCommand('mceAddControl', 0, v);
bgneal@45 8971 return true;
bgneal@45 8972 }
bgneal@45 8973
bgneal@45 8974 if (ed.isHidden())
bgneal@45 8975 ed.show();
bgneal@45 8976 else
bgneal@45 8977 ed.hide();
bgneal@45 8978
bgneal@45 8979 return true;
bgneal@45 8980 }
bgneal@45 8981
bgneal@45 8982 // Run command on active editor
bgneal@45 8983 if (t.activeEditor)
bgneal@45 8984 return t.activeEditor.execCommand(c, u, v);
bgneal@45 8985
bgneal@45 8986 return false;
bgneal@45 8987 },
bgneal@45 8988
bgneal@45 8989 execInstanceCommand : function(id, c, u, v) {
bgneal@45 8990 var ed = this.get(id);
bgneal@45 8991
bgneal@45 8992 if (ed)
bgneal@45 8993 return ed.execCommand(c, u, v);
bgneal@45 8994
bgneal@45 8995 return false;
bgneal@45 8996 },
bgneal@45 8997
bgneal@45 8998 triggerSave : function() {
bgneal@45 8999 each(this.editors, function(e) {
bgneal@45 9000 e.save();
bgneal@45 9001 });
bgneal@45 9002 },
bgneal@45 9003
bgneal@45 9004 addI18n : function(p, o) {
bgneal@45 9005 var lo, i18n = this.i18n;
bgneal@45 9006
bgneal@45 9007 if (!tinymce.is(p, 'string')) {
bgneal@45 9008 each(p, function(o, lc) {
bgneal@45 9009 each(o, function(o, g) {
bgneal@45 9010 each(o, function(o, k) {
bgneal@45 9011 if (g === 'common')
bgneal@45 9012 i18n[lc + '.' + k] = o;
bgneal@45 9013 else
bgneal@45 9014 i18n[lc + '.' + g + '.' + k] = o;
bgneal@45 9015 });
bgneal@45 9016 });
bgneal@45 9017 });
bgneal@45 9018 } else {
bgneal@45 9019 each(o, function(o, k) {
bgneal@45 9020 i18n[p + '.' + k] = o;
bgneal@45 9021 });
bgneal@45 9022 }
bgneal@45 9023 },
bgneal@45 9024
bgneal@45 9025 // Private methods
bgneal@45 9026
bgneal@183 9027 _setActive : function(editor) {
bgneal@183 9028 this.selectedInstance = this.activeEditor = editor;
bgneal@45 9029 }
bgneal@183 9030 });
bgneal@45 9031 })(tinymce);
bgneal@45 9032
bgneal@45 9033 (function(tinymce) {
bgneal@183 9034 // Shorten these names
bgneal@183 9035 var DOM = tinymce.DOM, Event = tinymce.dom.Event, extend = tinymce.extend,
bgneal@183 9036 Dispatcher = tinymce.util.Dispatcher, each = tinymce.each, isGecko = tinymce.isGecko,
bgneal@183 9037 isIE = tinymce.isIE, isWebKit = tinymce.isWebKit, is = tinymce.is,
bgneal@183 9038 ThemeManager = tinymce.ThemeManager, PluginManager = tinymce.PluginManager,
bgneal@183 9039 inArray = tinymce.inArray, grep = tinymce.grep, explode = tinymce.explode;
bgneal@45 9040
bgneal@45 9041 tinymce.create('tinymce.Editor', {
bgneal@45 9042 Editor : function(id, s) {
bgneal@45 9043 var t = this;
bgneal@45 9044
bgneal@45 9045 t.id = t.editorId = id;
bgneal@183 9046
bgneal@45 9047 t.execCommands = {};
bgneal@45 9048 t.queryStateCommands = {};
bgneal@45 9049 t.queryValueCommands = {};
bgneal@183 9050
bgneal@183 9051 t.isNotDirty = false;
bgneal@183 9052
bgneal@45 9053 t.plugins = {};
bgneal@45 9054
bgneal@45 9055 // Add events to the editor
bgneal@45 9056 each([
bgneal@45 9057 'onPreInit',
bgneal@183 9058
bgneal@45 9059 'onBeforeRenderUI',
bgneal@183 9060
bgneal@45 9061 'onPostRender',
bgneal@183 9062
bgneal@45 9063 'onInit',
bgneal@183 9064
bgneal@45 9065 'onRemove',
bgneal@183 9066
bgneal@45 9067 'onActivate',
bgneal@183 9068
bgneal@45 9069 'onDeactivate',
bgneal@183 9070
bgneal@45 9071 'onClick',
bgneal@183 9072
bgneal@45 9073 'onEvent',
bgneal@183 9074
bgneal@45 9075 'onMouseUp',
bgneal@183 9076
bgneal@45 9077 'onMouseDown',
bgneal@183 9078
bgneal@45 9079 'onDblClick',
bgneal@183 9080
bgneal@45 9081 'onKeyDown',
bgneal@183 9082
bgneal@45 9083 'onKeyUp',
bgneal@183 9084
bgneal@45 9085 'onKeyPress',
bgneal@183 9086
bgneal@45 9087 'onContextMenu',
bgneal@183 9088
bgneal@45 9089 'onSubmit',
bgneal@183 9090
bgneal@45 9091 'onReset',
bgneal@183 9092
bgneal@45 9093 'onPaste',
bgneal@183 9094
bgneal@45 9095 'onPreProcess',
bgneal@183 9096
bgneal@45 9097 'onPostProcess',
bgneal@183 9098
bgneal@45 9099 'onBeforeSetContent',
bgneal@183 9100
bgneal@45 9101 'onBeforeGetContent',
bgneal@183 9102
bgneal@45 9103 'onSetContent',
bgneal@183 9104
bgneal@45 9105 'onGetContent',
bgneal@183 9106
bgneal@45 9107 'onLoadContent',
bgneal@183 9108
bgneal@45 9109 'onSaveContent',
bgneal@183 9110
bgneal@45 9111 'onNodeChange',
bgneal@183 9112
bgneal@45 9113 'onChange',
bgneal@183 9114
bgneal@45 9115 'onBeforeExecCommand',
bgneal@183 9116
bgneal@45 9117 'onExecCommand',
bgneal@183 9118
bgneal@45 9119 'onUndo',
bgneal@183 9120
bgneal@45 9121 'onRedo',
bgneal@183 9122
bgneal@45 9123 'onVisualAid',
bgneal@183 9124
bgneal@45 9125 'onSetProgressState'
bgneal@45 9126 ], function(e) {
bgneal@45 9127 t[e] = new Dispatcher(t);
bgneal@45 9128 });
bgneal@45 9129
bgneal@45 9130 t.settings = s = extend({
bgneal@45 9131 id : id,
bgneal@45 9132 language : 'en',
bgneal@45 9133 docs_language : 'en',
bgneal@45 9134 theme : 'simple',
bgneal@45 9135 skin : 'default',
bgneal@45 9136 delta_width : 0,
bgneal@45 9137 delta_height : 0,
bgneal@45 9138 popup_css : '',
bgneal@45 9139 plugins : '',
bgneal@45 9140 document_base_url : tinymce.documentBaseURL,
bgneal@45 9141 add_form_submit_trigger : 1,
bgneal@45 9142 submit_patch : 1,
bgneal@45 9143 add_unload_trigger : 1,
bgneal@45 9144 convert_urls : 1,
bgneal@45 9145 relative_urls : 1,
bgneal@45 9146 remove_script_host : 1,
bgneal@45 9147 table_inline_editing : 0,
bgneal@45 9148 object_resizing : 1,
bgneal@45 9149 cleanup : 1,
bgneal@45 9150 accessibility_focus : 1,
bgneal@45 9151 custom_shortcuts : 1,
bgneal@45 9152 custom_undo_redo_keyboard_shortcuts : 1,
bgneal@45 9153 custom_undo_redo_restore_selection : 1,
bgneal@45 9154 custom_undo_redo : 1,
bgneal@183 9155 doctype : tinymce.isIE6 ? '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">' : '<!DOCTYPE>', // Use old doctype on IE 6 to avoid horizontal scroll
bgneal@45 9156 visual_table_class : 'mceItemTable',
bgneal@45 9157 visual : 1,
bgneal@45 9158 font_size_style_values : 'xx-small,x-small,small,medium,large,x-large,xx-large',
bgneal@45 9159 apply_source_formatting : 1,
bgneal@45 9160 directionality : 'ltr',
bgneal@45 9161 forced_root_block : 'p',
bgneal@183 9162 valid_elements : '@[id|class|style|title|dir<ltr?rtl|lang|xml::lang|onclick|ondblclick|onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|onkeydown|onkeyup],a[rel|rev|charset|hreflang|tabindex|accesskey|type|name|href|target|title|class|onfocus|onblur],strong/b,em/i,strike,u,#p,-ol[type|compact],-ul[type|compact],-li,br,img[longdesc|usemap|src|border|alt=|title|hspace|vspace|width|height|align],-sub,-sup,-blockquote[cite],-table[border|cellspacing|cellpadding|width|frame|rules|height|align|summary|bgcolor|background|bordercolor],-tr[rowspan|width|height|align|valign|bgcolor|background|bordercolor],tbody,thead,tfoot,#td[colspan|rowspan|width|height|align|valign|bgcolor|background|bordercolor|scope],#th[colspan|rowspan|width|height|align|valign|scope],caption,-div,-span,-code,-pre,address,-h1,-h2,-h3,-h4,-h5,-h6,hr[size|noshade],-font[face|size|color],dd,dl,dt,cite,abbr,acronym,del[datetime|cite],ins[datetime|cite],object[classid|width|height|codebase|*],param[name|value],embed[type|width|height|src|*],script[src|type],map[name],area[shape|coords|href|alt|target],bdo,button,col[align|char|charoff|span|valign|width],colgroup[align|char|charoff|span|valign|width],dfn,fieldset,form[action|accept|accept-charset|enctype|method],input[accept|alt|checked|disabled|maxlength|name|readonly|size|src|type|value|tabindex|accesskey],kbd,label[for],legend,noscript,optgroup[label|disabled],option[disabled|label|selected|value],q[cite],samp,select[disabled|multiple|name|size],small,textarea[cols|rows|disabled|name|readonly],tt,var,big',
bgneal@45 9163 hidden_input : 1,
bgneal@45 9164 padd_empty_editor : 1,
bgneal@45 9165 render_ui : 1,
bgneal@45 9166 init_theme : 1,
bgneal@45 9167 force_p_newlines : 1,
bgneal@45 9168 indentation : '30px',
bgneal@45 9169 keep_styles : 1,
bgneal@45 9170 fix_table_elements : 1,
bgneal@183 9171 inline_styles : 1,
bgneal@183 9172 convert_fonts_to_spans : true
bgneal@45 9173 }, s);
bgneal@45 9174
bgneal@45 9175 t.documentBaseURI = new tinymce.util.URI(s.document_base_url || tinymce.documentBaseURL, {
bgneal@45 9176 base_uri : tinyMCE.baseURI
bgneal@45 9177 });
bgneal@183 9178
bgneal@183 9179 t.baseURI = tinymce.baseURI;
bgneal@45 9180
bgneal@45 9181 // Call setup
bgneal@45 9182 t.execCallback('setup', t);
bgneal@45 9183 },
bgneal@45 9184
bgneal@45 9185 render : function(nst) {
bgneal@45 9186 var t = this, s = t.settings, id = t.id, sl = tinymce.ScriptLoader;
bgneal@45 9187
bgneal@45 9188 // Page is not loaded yet, wait for it
bgneal@45 9189 if (!Event.domLoaded) {
bgneal@45 9190 Event.add(document, 'init', function() {
bgneal@45 9191 t.render();
bgneal@45 9192 });
bgneal@45 9193 return;
bgneal@45 9194 }
bgneal@45 9195
bgneal@183 9196 tinyMCE.settings = s;
bgneal@45 9197
bgneal@45 9198 // Element not found, then skip initialization
bgneal@45 9199 if (!t.getElement())
bgneal@45 9200 return;
bgneal@45 9201
bgneal@217 9202 // Is a iPad/iPhone, then skip initialization. We need to sniff here since the
bgneal@217 9203 // browser says it has contentEditable support but there is no visible caret
bgneal@217 9204 // We will remove this check ones Apple implements full contentEditable support
bgneal@217 9205 if (tinymce.isIDevice)
bgneal@217 9206 return;
bgneal@217 9207
bgneal@45 9208 // Add hidden input for non input elements inside form elements
bgneal@45 9209 if (!/TEXTAREA|INPUT/i.test(t.getElement().nodeName) && s.hidden_input && DOM.getParent(id, 'form'))
bgneal@45 9210 DOM.insertAfter(DOM.create('input', {type : 'hidden', name : id}), id);
bgneal@45 9211
bgneal@45 9212 if (tinymce.WindowManager)
bgneal@45 9213 t.windowManager = new tinymce.WindowManager(t);
bgneal@45 9214
bgneal@45 9215 if (s.encoding == 'xml') {
bgneal@45 9216 t.onGetContent.add(function(ed, o) {
bgneal@45 9217 if (o.save)
bgneal@45 9218 o.content = DOM.encode(o.content);
bgneal@45 9219 });
bgneal@45 9220 }
bgneal@45 9221
bgneal@45 9222 if (s.add_form_submit_trigger) {
bgneal@45 9223 t.onSubmit.addToTop(function() {
bgneal@45 9224 if (t.initialized) {
bgneal@45 9225 t.save();
bgneal@45 9226 t.isNotDirty = 1;
bgneal@45 9227 }
bgneal@45 9228 });
bgneal@45 9229 }
bgneal@45 9230
bgneal@45 9231 if (s.add_unload_trigger) {
bgneal@45 9232 t._beforeUnload = tinyMCE.onBeforeUnload.add(function() {
bgneal@45 9233 if (t.initialized && !t.destroyed && !t.isHidden())
bgneal@45 9234 t.save({format : 'raw', no_events : true});
bgneal@45 9235 });
bgneal@45 9236 }
bgneal@45 9237
bgneal@45 9238 tinymce.addUnload(t.destroy, t);
bgneal@45 9239
bgneal@45 9240 if (s.submit_patch) {
bgneal@45 9241 t.onBeforeRenderUI.add(function() {
bgneal@45 9242 var n = t.getElement().form;
bgneal@45 9243
bgneal@45 9244 if (!n)
bgneal@45 9245 return;
bgneal@45 9246
bgneal@45 9247 // Already patched
bgneal@45 9248 if (n._mceOldSubmit)
bgneal@45 9249 return;
bgneal@45 9250
bgneal@45 9251 // Check page uses id="submit" or name="submit" for it's submit button
bgneal@45 9252 if (!n.submit.nodeType && !n.submit.length) {
bgneal@45 9253 t.formElement = n;
bgneal@45 9254 n._mceOldSubmit = n.submit;
bgneal@45 9255 n.submit = function() {
bgneal@45 9256 // Save all instances
bgneal@183 9257 tinymce.triggerSave();
bgneal@45 9258 t.isNotDirty = 1;
bgneal@45 9259
bgneal@183 9260 return t.formElement._mceOldSubmit(t.formElement);
bgneal@45 9261 };
bgneal@45 9262 }
bgneal@45 9263
bgneal@45 9264 n = null;
bgneal@45 9265 });
bgneal@45 9266 }
bgneal@45 9267
bgneal@45 9268 // Load scripts
bgneal@45 9269 function loadScripts() {
bgneal@45 9270 if (s.language)
bgneal@45 9271 sl.add(tinymce.baseURL + '/langs/' + s.language + '.js');
bgneal@45 9272
bgneal@45 9273 if (s.theme && s.theme.charAt(0) != '-' && !ThemeManager.urls[s.theme])
bgneal@45 9274 ThemeManager.load(s.theme, 'themes/' + s.theme + '/editor_template' + tinymce.suffix + '.js');
bgneal@45 9275
bgneal@45 9276 each(explode(s.plugins), function(p) {
bgneal@45 9277 if (p && p.charAt(0) != '-' && !PluginManager.urls[p]) {
bgneal@183 9278 // Skip safari plugin, since it is removed as of 3.3b1
bgneal@183 9279 if (p == 'safari')
bgneal@45 9280 return;
bgneal@45 9281
bgneal@45 9282 PluginManager.load(p, 'plugins/' + p + '/editor_plugin' + tinymce.suffix + '.js');
bgneal@45 9283 }
bgneal@45 9284 });
bgneal@45 9285
bgneal@45 9286 // Init when que is loaded
bgneal@45 9287 sl.loadQueue(function() {
bgneal@45 9288 if (!t.removed)
bgneal@45 9289 t.init();
bgneal@45 9290 });
bgneal@45 9291 };
bgneal@45 9292
bgneal@183 9293 loadScripts();
bgneal@45 9294 },
bgneal@45 9295
bgneal@45 9296 init : function() {
bgneal@45 9297 var n, t = this, s = t.settings, w, h, e = t.getElement(), o, ti, u, bi, bc, re;
bgneal@45 9298
bgneal@183 9299 tinymce.add(t);
bgneal@183 9300
bgneal@45 9301 if (s.theme) {
bgneal@45 9302 s.theme = s.theme.replace(/-/, '');
bgneal@45 9303 o = ThemeManager.get(s.theme);
bgneal@45 9304 t.theme = new o();
bgneal@45 9305
bgneal@45 9306 if (t.theme.init && s.init_theme)
bgneal@45 9307 t.theme.init(t, ThemeManager.urls[s.theme] || tinymce.documentBaseURL.replace(/\/$/, ''));
bgneal@45 9308 }
bgneal@45 9309
bgneal@45 9310 // Create all plugins
bgneal@45 9311 each(explode(s.plugins.replace(/\-/g, '')), function(p) {
bgneal@45 9312 var c = PluginManager.get(p), u = PluginManager.urls[p] || tinymce.documentBaseURL.replace(/\/$/, ''), po;
bgneal@45 9313
bgneal@45 9314 if (c) {
bgneal@45 9315 po = new c(t, u);
bgneal@45 9316
bgneal@45 9317 t.plugins[p] = po;
bgneal@45 9318
bgneal@45 9319 if (po.init)
bgneal@45 9320 po.init(t, u);
bgneal@45 9321 }
bgneal@45 9322 });
bgneal@45 9323
bgneal@45 9324 // Setup popup CSS path(s)
bgneal@45 9325 if (s.popup_css !== false) {
bgneal@45 9326 if (s.popup_css)
bgneal@45 9327 s.popup_css = t.documentBaseURI.toAbsolute(s.popup_css);
bgneal@45 9328 else
bgneal@45 9329 s.popup_css = t.baseURI.toAbsolute("themes/" + s.theme + "/skins/" + s.skin + "/dialog.css");
bgneal@45 9330 }
bgneal@45 9331
bgneal@45 9332 if (s.popup_css_add)
bgneal@45 9333 s.popup_css += ',' + t.documentBaseURI.toAbsolute(s.popup_css_add);
bgneal@45 9334
bgneal@45 9335 t.controlManager = new tinymce.ControlManager(t);
bgneal@45 9336
bgneal@45 9337 if (s.custom_undo_redo) {
bgneal@183 9338 // Add initial undo level
bgneal@183 9339 t.onBeforeExecCommand.add(function(ed, cmd, ui, val, a) {
bgneal@183 9340 if (cmd != 'Undo' && cmd != 'Redo' && cmd != 'mceRepaint' && (!a || !a.skip_undo)) {
bgneal@183 9341 if (!t.undoManager.hasUndo())
bgneal@183 9342 t.undoManager.add();
bgneal@183 9343 }
bgneal@183 9344 });
bgneal@183 9345
bgneal@45 9346 t.onExecCommand.add(function(ed, cmd, ui, val, a) {
bgneal@45 9347 if (cmd != 'Undo' && cmd != 'Redo' && cmd != 'mceRepaint' && (!a || !a.skip_undo))
bgneal@45 9348 t.undoManager.add();
bgneal@45 9349 });
bgneal@45 9350 }
bgneal@45 9351
bgneal@45 9352 t.onExecCommand.add(function(ed, c) {
bgneal@45 9353 // Don't refresh the select lists until caret move
bgneal@45 9354 if (!/^(FontName|FontSize)$/.test(c))
bgneal@45 9355 t.nodeChanged();
bgneal@45 9356 });
bgneal@45 9357
bgneal@45 9358 // Remove ghost selections on images and tables in Gecko
bgneal@45 9359 if (isGecko) {
bgneal@45 9360 function repaint(a, o) {
bgneal@45 9361 if (!o || !o.initial)
bgneal@45 9362 t.execCommand('mceRepaint');
bgneal@45 9363 };
bgneal@45 9364
bgneal@45 9365 t.onUndo.add(repaint);
bgneal@45 9366 t.onRedo.add(repaint);
bgneal@45 9367 t.onSetContent.add(repaint);
bgneal@45 9368 }
bgneal@45 9369
bgneal@45 9370 // Enables users to override the control factory
bgneal@45 9371 t.onBeforeRenderUI.dispatch(t, t.controlManager);
bgneal@45 9372
bgneal@45 9373 // Measure box
bgneal@45 9374 if (s.render_ui) {
bgneal@45 9375 w = s.width || e.style.width || e.offsetWidth;
bgneal@45 9376 h = s.height || e.style.height || e.offsetHeight;
bgneal@45 9377 t.orgDisplay = e.style.display;
bgneal@45 9378 re = /^[0-9\.]+(|px)$/i;
bgneal@45 9379
bgneal@45 9380 if (re.test('' + w))
bgneal@45 9381 w = Math.max(parseInt(w) + (o.deltaWidth || 0), 100);
bgneal@45 9382
bgneal@45 9383 if (re.test('' + h))
bgneal@45 9384 h = Math.max(parseInt(h) + (o.deltaHeight || 0), 100);
bgneal@45 9385
bgneal@45 9386 // Render UI
bgneal@45 9387 o = t.theme.renderUI({
bgneal@45 9388 targetNode : e,
bgneal@45 9389 width : w,
bgneal@45 9390 height : h,
bgneal@45 9391 deltaWidth : s.delta_width,
bgneal@45 9392 deltaHeight : s.delta_height
bgneal@45 9393 });
bgneal@45 9394
bgneal@45 9395 t.editorContainer = o.editorContainer;
bgneal@45 9396 }
bgneal@45 9397
bgneal@45 9398
bgneal@183 9399 // User specified a document.domain value
bgneal@183 9400 if (document.domain && location.hostname != document.domain)
bgneal@183 9401 tinymce.relaxedDomain = document.domain;
bgneal@183 9402
bgneal@45 9403 // Resize editor
bgneal@45 9404 DOM.setStyles(o.sizeContainer || o.editorContainer, {
bgneal@45 9405 width : w,
bgneal@45 9406 height : h
bgneal@45 9407 });
bgneal@45 9408
bgneal@45 9409 h = (o.iframeHeight || h) + (typeof(h) == 'number' ? (o.deltaHeight || 0) : '');
bgneal@45 9410 if (h < 100)
bgneal@45 9411 h = 100;
bgneal@45 9412
bgneal@183 9413 t.iframeHTML = s.doctype + '<html><head xmlns="http://www.w3.org/1999/xhtml">';
bgneal@183 9414
bgneal@183 9415 // We only need to override paths if we have to
bgneal@183 9416 // IE has a bug where it remove site absolute urls to relative ones if this is specified
bgneal@183 9417 if (s.document_base_url != tinymce.documentBaseURL)
bgneal@183 9418 t.iframeHTML += '<base href="' + t.documentBaseURI.getURI() + '" />';
bgneal@183 9419
bgneal@183 9420 t.iframeHTML += '<meta http-equiv="X-UA-Compatible" content="IE=7" /><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
bgneal@45 9421
bgneal@45 9422 if (tinymce.relaxedDomain)
bgneal@45 9423 t.iframeHTML += '<script type="text/javascript">document.domain = "' + tinymce.relaxedDomain + '";</script>';
bgneal@45 9424
bgneal@45 9425 bi = s.body_id || 'tinymce';
bgneal@45 9426 if (bi.indexOf('=') != -1) {
bgneal@45 9427 bi = t.getParam('body_id', '', 'hash');
bgneal@45 9428 bi = bi[t.id] || bi;
bgneal@45 9429 }
bgneal@45 9430
bgneal@45 9431 bc = s.body_class || '';
bgneal@45 9432 if (bc.indexOf('=') != -1) {
bgneal@45 9433 bc = t.getParam('body_class', '', 'hash');
bgneal@45 9434 bc = bc[t.id] || '';
bgneal@45 9435 }
bgneal@45 9436
bgneal@45 9437 t.iframeHTML += '</head><body id="' + bi + '" class="mceContentBody ' + bc + '"></body></html>';
bgneal@45 9438
bgneal@45 9439 // Domain relaxing enabled, then set document domain
bgneal@45 9440 if (tinymce.relaxedDomain) {
bgneal@45 9441 // We need to write the contents here in IE since multiple writes messes up refresh button and back button
bgneal@45 9442 if (isIE || (tinymce.isOpera && parseFloat(opera.version()) >= 9.5))
bgneal@45 9443 u = 'javascript:(function(){document.open();document.domain="' + document.domain + '";var ed = window.parent.tinyMCE.get("' + t.id + '");document.write(ed.iframeHTML);document.close();ed.setupIframe();})()';
bgneal@45 9444 else if (tinymce.isOpera)
bgneal@45 9445 u = 'javascript:(function(){document.open();document.domain="' + document.domain + '";document.close();ed.setupIframe();})()';
bgneal@45 9446 }
bgneal@45 9447
bgneal@45 9448 // Create iframe
bgneal@45 9449 n = DOM.add(o.iframeContainer, 'iframe', {
bgneal@45 9450 id : t.id + "_ifr",
bgneal@45 9451 src : u || 'javascript:""', // Workaround for HTTPS warning in IE6/7
bgneal@45 9452 frameBorder : '0',
bgneal@45 9453 style : {
bgneal@45 9454 width : '100%',
bgneal@45 9455 height : h
bgneal@45 9456 }
bgneal@45 9457 });
bgneal@45 9458
bgneal@45 9459 t.contentAreaContainer = o.iframeContainer;
bgneal@45 9460 DOM.get(o.editorContainer).style.display = t.orgDisplay;
bgneal@45 9461 DOM.get(t.id).style.display = 'none';
bgneal@45 9462
bgneal@45 9463 if (!isIE || !tinymce.relaxedDomain)
bgneal@45 9464 t.setupIframe();
bgneal@45 9465
bgneal@45 9466 e = n = o = null; // Cleanup
bgneal@45 9467 },
bgneal@45 9468
bgneal@45 9469 setupIframe : function() {
bgneal@45 9470 var t = this, s = t.settings, e = DOM.get(t.id), d = t.getDoc(), h, b;
bgneal@45 9471
bgneal@45 9472 // Setup iframe body
bgneal@45 9473 if (!isIE || !tinymce.relaxedDomain) {
bgneal@45 9474 d.open();
bgneal@45 9475 d.write(t.iframeHTML);
bgneal@45 9476 d.close();
bgneal@45 9477 }
bgneal@45 9478
bgneal@45 9479 // Design mode needs to be added here Ctrl+A will fail otherwise
bgneal@45 9480 if (!isIE) {
bgneal@45 9481 try {
bgneal@45 9482 if (!s.readonly)
bgneal@45 9483 d.designMode = 'On';
bgneal@45 9484 } catch (ex) {
bgneal@45 9485 // Will fail on Gecko if the editor is placed in an hidden container element
bgneal@45 9486 // The design mode will be set ones the editor is focused
bgneal@45 9487 }
bgneal@45 9488 }
bgneal@45 9489
bgneal@45 9490 // IE needs to use contentEditable or it will display non secure items for HTTPS
bgneal@45 9491 if (isIE) {
bgneal@45 9492 // It will not steal focus if we hide it while setting contentEditable
bgneal@45 9493 b = t.getBody();
bgneal@45 9494 DOM.hide(b);
bgneal@45 9495
bgneal@45 9496 if (!s.readonly)
bgneal@45 9497 b.contentEditable = true;
bgneal@45 9498
bgneal@45 9499 DOM.show(b);
bgneal@45 9500 }
bgneal@45 9501
bgneal@183 9502 t.dom = new tinymce.dom.DOMUtils(t.getDoc(), {
bgneal@45 9503 keep_values : true,
bgneal@45 9504 url_converter : t.convertURL,
bgneal@45 9505 url_converter_scope : t,
bgneal@45 9506 hex_colors : s.force_hex_style_colors,
bgneal@45 9507 class_filter : s.class_filter,
bgneal@45 9508 update_styles : 1,
bgneal@183 9509 fix_ie_paragraphs : 1,
bgneal@183 9510 valid_styles : s.valid_styles
bgneal@183 9511 });
bgneal@183 9512
bgneal@183 9513 t.schema = new tinymce.dom.Schema();
bgneal@183 9514
bgneal@183 9515 t.serializer = new tinymce.dom.Serializer(extend(s, {
bgneal@45 9516 valid_elements : s.verify_html === false ? '*[*]' : s.valid_elements,
bgneal@183 9517 dom : t.dom,
bgneal@183 9518 schema : t.schema
bgneal@183 9519 }));
bgneal@45 9520
bgneal@45 9521 t.selection = new tinymce.dom.Selection(t.dom, t.getWin(), t.serializer);
bgneal@183 9522
bgneal@183 9523 t.formatter = new tinymce.Formatter(this);
bgneal@183 9524
bgneal@183 9525 // Register default formats
bgneal@183 9526 t.formatter.register({
bgneal@183 9527 alignleft : [
bgneal@183 9528 {selector : 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles : {textAlign : 'left'}},
bgneal@183 9529 {selector : 'img,table', styles : {'float' : 'left'}}
bgneal@183 9530 ],
bgneal@183 9531
bgneal@183 9532 aligncenter : [
bgneal@183 9533 {selector : 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles : {textAlign : 'center'}},
bgneal@183 9534 {selector : 'img', styles : {display : 'block', marginLeft : 'auto', marginRight : 'auto'}},
bgneal@183 9535 {selector : 'table', styles : {marginLeft : 'auto', marginRight : 'auto'}}
bgneal@183 9536 ],
bgneal@183 9537
bgneal@183 9538 alignright : [
bgneal@183 9539 {selector : 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles : {textAlign : 'right'}},
bgneal@183 9540 {selector : 'img,table', styles : {'float' : 'right'}}
bgneal@183 9541 ],
bgneal@183 9542
bgneal@183 9543 alignfull : [
bgneal@183 9544 {selector : 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles : {textAlign : 'justify'}}
bgneal@183 9545 ],
bgneal@183 9546
bgneal@183 9547 bold : [
bgneal@183 9548 {inline : 'strong'},
bgneal@183 9549 {inline : 'span', styles : {fontWeight : 'bold'}},
bgneal@183 9550 {inline : 'b'}
bgneal@183 9551 ],
bgneal@183 9552
bgneal@183 9553 italic : [
bgneal@183 9554 {inline : 'em'},
bgneal@183 9555 {inline : 'span', styles : {fontStyle : 'italic'}},
bgneal@183 9556 {inline : 'i'}
bgneal@183 9557 ],
bgneal@183 9558
bgneal@183 9559 underline : [
bgneal@183 9560 {inline : 'span', styles : {textDecoration : 'underline'}, exact : true},
bgneal@183 9561 {inline : 'u'}
bgneal@183 9562 ],
bgneal@183 9563
bgneal@183 9564 strikethrough : [
bgneal@183 9565 {inline : 'span', styles : {textDecoration : 'line-through'}, exact : true},
bgneal@183 9566 {inline : 'u'}
bgneal@183 9567 ],
bgneal@183 9568
bgneal@183 9569 forecolor : {inline : 'span', styles : {color : '%value'}},
bgneal@183 9570 hilitecolor : {inline : 'span', styles : {backgroundColor : '%value'}},
bgneal@183 9571 fontname : {inline : 'span', styles : {fontFamily : '%value'}},
bgneal@183 9572 fontsize : {inline : 'span', styles : {fontSize : '%value'}},
bgneal@217 9573 fontsize_class : {inline : 'span', attributes : {'class' : '%value'}},
bgneal@183 9574 blockquote : {block : 'blockquote', wrapper : 1, remove : 'all'},
bgneal@183 9575
bgneal@183 9576 removeformat : [
bgneal@183 9577 {selector : 'b,strong,em,i,font,u,strike', remove : 'all', split : true, expand : false, block_expand : true, deep : true},
bgneal@183 9578 {selector : 'span', attributes : ['style', 'class'], remove : 'empty', split : true, expand : false, deep : true},
bgneal@183 9579 {selector : '*', attributes : ['style', 'class'], split : false, expand : false, deep : true}
bgneal@183 9580 ]
bgneal@183 9581 });
bgneal@183 9582
bgneal@183 9583 // Register default block formats
bgneal@183 9584 each('p h1 h2 h3 h4 h5 h6 div address pre div code dt dd samp'.split(/\s/), function(name) {
bgneal@183 9585 t.formatter.register(name, {block : name, remove : 'all'});
bgneal@183 9586 });
bgneal@183 9587
bgneal@183 9588 // Register user defined formats
bgneal@183 9589 t.formatter.register(t.settings.formats);
bgneal@183 9590
bgneal@183 9591 t.undoManager = new tinymce.UndoManager(t);
bgneal@183 9592
bgneal@183 9593 // Pass through
bgneal@183 9594 t.undoManager.onAdd.add(function(um, l) {
bgneal@183 9595 if (!l.initial)
bgneal@183 9596 return t.onChange.dispatch(t, l, um);
bgneal@183 9597 });
bgneal@183 9598
bgneal@183 9599 t.undoManager.onUndo.add(function(um, l) {
bgneal@183 9600 return t.onUndo.dispatch(t, l, um);
bgneal@183 9601 });
bgneal@183 9602
bgneal@183 9603 t.undoManager.onRedo.add(function(um, l) {
bgneal@183 9604 return t.onRedo.dispatch(t, l, um);
bgneal@183 9605 });
bgneal@183 9606
bgneal@45 9607 t.forceBlocks = new tinymce.ForceBlocks(t, {
bgneal@45 9608 forced_root_block : s.forced_root_block
bgneal@45 9609 });
bgneal@183 9610
bgneal@45 9611 t.editorCommands = new tinymce.EditorCommands(t);
bgneal@45 9612
bgneal@45 9613 // Pass through
bgneal@45 9614 t.serializer.onPreProcess.add(function(se, o) {
bgneal@45 9615 return t.onPreProcess.dispatch(t, o, se);
bgneal@45 9616 });
bgneal@45 9617
bgneal@45 9618 t.serializer.onPostProcess.add(function(se, o) {
bgneal@45 9619 return t.onPostProcess.dispatch(t, o, se);
bgneal@45 9620 });
bgneal@45 9621
bgneal@45 9622 t.onPreInit.dispatch(t);
bgneal@45 9623
bgneal@45 9624 if (!s.gecko_spellcheck)
bgneal@45 9625 t.getBody().spellcheck = 0;
bgneal@45 9626
bgneal@45 9627 if (!s.readonly)
bgneal@45 9628 t._addEvents();
bgneal@45 9629
bgneal@45 9630 t.controlManager.onPostRender.dispatch(t, t.controlManager);
bgneal@45 9631 t.onPostRender.dispatch(t);
bgneal@45 9632
bgneal@45 9633 if (s.directionality)
bgneal@45 9634 t.getBody().dir = s.directionality;
bgneal@45 9635
bgneal@45 9636 if (s.nowrap)
bgneal@45 9637 t.getBody().style.whiteSpace = "nowrap";
bgneal@45 9638
bgneal@45 9639 if (s.custom_elements) {
bgneal@45 9640 function handleCustom(ed, o) {
bgneal@45 9641 each(explode(s.custom_elements), function(v) {
bgneal@45 9642 var n;
bgneal@45 9643
bgneal@45 9644 if (v.indexOf('~') === 0) {
bgneal@45 9645 v = v.substring(1);
bgneal@45 9646 n = 'span';
bgneal@45 9647 } else
bgneal@45 9648 n = 'div';
bgneal@45 9649
bgneal@183 9650 o.content = o.content.replace(new RegExp('<(' + v + ')([^>]*)>', 'g'), '<' + n + ' _mce_name="$1"$2>');
bgneal@45 9651 o.content = o.content.replace(new RegExp('</(' + v + ')>', 'g'), '</' + n + '>');
bgneal@45 9652 });
bgneal@45 9653 };
bgneal@45 9654
bgneal@45 9655 t.onBeforeSetContent.add(handleCustom);
bgneal@45 9656 t.onPostProcess.add(function(ed, o) {
bgneal@45 9657 if (o.set)
bgneal@183 9658 handleCustom(ed, o);
bgneal@45 9659 });
bgneal@45 9660 }
bgneal@45 9661
bgneal@45 9662 if (s.handle_node_change_callback) {
bgneal@45 9663 t.onNodeChange.add(function(ed, cm, n) {
bgneal@45 9664 t.execCallback('handle_node_change_callback', t.id, n, -1, -1, true, t.selection.isCollapsed());
bgneal@45 9665 });
bgneal@45 9666 }
bgneal@45 9667
bgneal@45 9668 if (s.save_callback) {
bgneal@45 9669 t.onSaveContent.add(function(ed, o) {
bgneal@45 9670 var h = t.execCallback('save_callback', t.id, o.content, t.getBody());
bgneal@45 9671
bgneal@45 9672 if (h)
bgneal@45 9673 o.content = h;
bgneal@45 9674 });
bgneal@45 9675 }
bgneal@45 9676
bgneal@45 9677 if (s.onchange_callback) {
bgneal@45 9678 t.onChange.add(function(ed, l) {
bgneal@45 9679 t.execCallback('onchange_callback', t, l);
bgneal@45 9680 });
bgneal@45 9681 }
bgneal@45 9682
bgneal@45 9683 if (s.convert_newlines_to_brs) {
bgneal@45 9684 t.onBeforeSetContent.add(function(ed, o) {
bgneal@45 9685 if (o.initial)
bgneal@45 9686 o.content = o.content.replace(/\r?\n/g, '<br />');
bgneal@45 9687 });
bgneal@45 9688 }
bgneal@45 9689
bgneal@45 9690 if (s.fix_nesting && isIE) {
bgneal@45 9691 t.onBeforeSetContent.add(function(ed, o) {
bgneal@45 9692 o.content = t._fixNesting(o.content);
bgneal@45 9693 });
bgneal@45 9694 }
bgneal@45 9695
bgneal@45 9696 if (s.preformatted) {
bgneal@45 9697 t.onPostProcess.add(function(ed, o) {
bgneal@45 9698 o.content = o.content.replace(/^\s*<pre.*?>/, '');
bgneal@45 9699 o.content = o.content.replace(/<\/pre>\s*$/, '');
bgneal@45 9700
bgneal@45 9701 if (o.set)
bgneal@45 9702 o.content = '<pre class="mceItemHidden">' + o.content + '</pre>';
bgneal@45 9703 });
bgneal@45 9704 }
bgneal@45 9705
bgneal@45 9706 if (s.verify_css_classes) {
bgneal@45 9707 t.serializer.attribValueFilter = function(n, v) {
bgneal@45 9708 var s, cl;
bgneal@45 9709
bgneal@45 9710 if (n == 'class') {
bgneal@45 9711 // Build regexp for classes
bgneal@45 9712 if (!t.classesRE) {
bgneal@45 9713 cl = t.dom.getClasses();
bgneal@45 9714
bgneal@45 9715 if (cl.length > 0) {
bgneal@45 9716 s = '';
bgneal@45 9717
bgneal@45 9718 each (cl, function(o) {
bgneal@45 9719 s += (s ? '|' : '') + o['class'];
bgneal@45 9720 });
bgneal@45 9721
bgneal@45 9722 t.classesRE = new RegExp('(' + s + ')', 'gi');
bgneal@45 9723 }
bgneal@45 9724 }
bgneal@45 9725
bgneal@45 9726 return !t.classesRE || /(\bmceItem\w+\b|\bmceTemp\w+\b)/g.test(v) || t.classesRE.test(v) ? v : '';
bgneal@45 9727 }
bgneal@45 9728
bgneal@45 9729 return v;
bgneal@45 9730 };
bgneal@45 9731 }
bgneal@45 9732
bgneal@45 9733 if (s.cleanup_callback) {
bgneal@45 9734 t.onBeforeSetContent.add(function(ed, o) {
bgneal@45 9735 o.content = t.execCallback('cleanup_callback', 'insert_to_editor', o.content, o);
bgneal@45 9736 });
bgneal@45 9737
bgneal@45 9738 t.onPreProcess.add(function(ed, o) {
bgneal@45 9739 if (o.set)
bgneal@45 9740 t.execCallback('cleanup_callback', 'insert_to_editor_dom', o.node, o);
bgneal@45 9741
bgneal@45 9742 if (o.get)
bgneal@45 9743 t.execCallback('cleanup_callback', 'get_from_editor_dom', o.node, o);
bgneal@45 9744 });
bgneal@45 9745
bgneal@45 9746 t.onPostProcess.add(function(ed, o) {
bgneal@45 9747 if (o.set)
bgneal@45 9748 o.content = t.execCallback('cleanup_callback', 'insert_to_editor', o.content, o);
bgneal@45 9749
bgneal@45 9750 if (o.get)
bgneal@45 9751 o.content = t.execCallback('cleanup_callback', 'get_from_editor', o.content, o);
bgneal@45 9752 });
bgneal@45 9753 }
bgneal@45 9754
bgneal@45 9755 if (s.save_callback) {
bgneal@45 9756 t.onGetContent.add(function(ed, o) {
bgneal@45 9757 if (o.save)
bgneal@45 9758 o.content = t.execCallback('save_callback', t.id, o.content, t.getBody());
bgneal@45 9759 });
bgneal@45 9760 }
bgneal@45 9761
bgneal@45 9762 if (s.handle_event_callback) {
bgneal@45 9763 t.onEvent.add(function(ed, e, o) {
bgneal@45 9764 if (t.execCallback('handle_event_callback', e, ed, o) === false)
bgneal@45 9765 Event.cancel(e);
bgneal@45 9766 });
bgneal@45 9767 }
bgneal@45 9768
bgneal@45 9769 // Add visual aids when new contents is added
bgneal@45 9770 t.onSetContent.add(function() {
bgneal@45 9771 t.addVisual(t.getBody());
bgneal@45 9772 });
bgneal@45 9773
bgneal@45 9774 // Remove empty contents
bgneal@45 9775 if (s.padd_empty_editor) {
bgneal@45 9776 t.onPostProcess.add(function(ed, o) {
bgneal@45 9777 o.content = o.content.replace(/^(<p[^>]*>(&nbsp;|&#160;|\s|\u00a0|)<\/p>[\r\n]*|<br \/>[\r\n]*)$/, '');
bgneal@45 9778 });
bgneal@45 9779 }
bgneal@45 9780
bgneal@45 9781 if (isGecko) {
bgneal@183 9782 // Fix gecko link bug, when a link is placed at the end of block elements there is
bgneal@183 9783 // no way to move the caret behind the link. This fix adds a bogus br element after the link
bgneal@45 9784 function fixLinks(ed, o) {
bgneal@45 9785 each(ed.dom.select('a'), function(n) {
bgneal@45 9786 var pn = n.parentNode;
bgneal@45 9787
bgneal@45 9788 if (ed.dom.isBlock(pn) && pn.lastChild === n)
bgneal@183 9789 ed.dom.add(pn, 'br', {'_mce_bogus' : 1});
bgneal@45 9790 });
bgneal@45 9791 };
bgneal@45 9792
bgneal@45 9793 t.onExecCommand.add(function(ed, cmd) {
bgneal@45 9794 if (cmd === 'CreateLink')
bgneal@45 9795 fixLinks(ed);
bgneal@45 9796 });
bgneal@45 9797
bgneal@45 9798 t.onSetContent.add(t.selection.onSetContent.add(fixLinks));
bgneal@183 9799
bgneal@183 9800 if (!s.readonly) {
bgneal@183 9801 try {
bgneal@183 9802 // Design mode must be set here once again to fix a bug where
bgneal@183 9803 // Ctrl+A/Delete/Backspace didn't work if the editor was added using mceAddControl then removed then added again
bgneal@183 9804 d.designMode = 'Off';
bgneal@183 9805 d.designMode = 'On';
bgneal@183 9806 } catch (ex) {
bgneal@183 9807 // Will fail on Gecko if the editor is placed in an hidden container element
bgneal@183 9808 // The design mode will be set ones the editor is focused
bgneal@183 9809 }
bgneal@45 9810 }
bgneal@45 9811 }
bgneal@45 9812
bgneal@45 9813 // A small timeout was needed since firefox will remove. Bug: #1838304
bgneal@45 9814 setTimeout(function () {
bgneal@45 9815 if (t.removed)
bgneal@45 9816 return;
bgneal@45 9817
bgneal@45 9818 t.load({initial : true, format : (s.cleanup_on_startup ? 'html' : 'raw')});
bgneal@45 9819 t.startContent = t.getContent({format : 'raw'});
bgneal@45 9820 t.initialized = true;
bgneal@45 9821
bgneal@45 9822 t.onInit.dispatch(t);
bgneal@45 9823 t.execCallback('setupcontent_callback', t.id, t.getBody(), t.getDoc());
bgneal@45 9824 t.execCallback('init_instance_callback', t);
bgneal@45 9825 t.focus(true);
bgneal@45 9826 t.nodeChanged({initial : 1});
bgneal@45 9827
bgneal@45 9828 // Load specified content CSS last
bgneal@45 9829 if (s.content_css) {
bgneal@45 9830 tinymce.each(explode(s.content_css), function(u) {
bgneal@45 9831 t.dom.loadCSS(t.documentBaseURI.toAbsolute(u));
bgneal@45 9832 });
bgneal@45 9833 }
bgneal@45 9834
bgneal@45 9835 // Handle auto focus
bgneal@45 9836 if (s.auto_focus) {
bgneal@45 9837 setTimeout(function () {
bgneal@183 9838 var ed = tinymce.get(s.auto_focus);
bgneal@45 9839
bgneal@45 9840 ed.selection.select(ed.getBody(), 1);
bgneal@45 9841 ed.selection.collapse(1);
bgneal@45 9842 ed.getWin().focus();
bgneal@45 9843 }, 100);
bgneal@45 9844 }
bgneal@45 9845 }, 1);
bgneal@45 9846
bgneal@45 9847 e = null;
bgneal@45 9848 },
bgneal@45 9849
bgneal@45 9850
bgneal@45 9851 focus : function(sf) {
bgneal@217 9852 var oed, t = this, ce = t.settings.content_editable, ieRng, controlElm, doc = t.getDoc();
bgneal@45 9853
bgneal@45 9854 if (!sf) {
bgneal@217 9855 // Get selected control element
bgneal@217 9856 ieRng = t.selection.getRng();
bgneal@217 9857 if (ieRng.item) {
bgneal@217 9858 controlElm = ieRng.item(0);
bgneal@217 9859 }
bgneal@217 9860
bgneal@183 9861 // Is not content editable
bgneal@183 9862 if (!ce)
bgneal@45 9863 t.getWin().focus();
bgneal@45 9864
bgneal@217 9865 // Restore selected control element
bgneal@217 9866 // This is needed when for example an image is selected within a
bgneal@217 9867 // layer a call to focus will then remove the control selection
bgneal@217 9868 if (controlElm && controlElm.ownerDocument == doc) {
bgneal@217 9869 ieRng = doc.body.createControlRange();
bgneal@217 9870 ieRng.addElement(controlElm);
bgneal@217 9871 ieRng.select();
bgneal@217 9872 }
bgneal@217 9873
bgneal@45 9874 }
bgneal@45 9875
bgneal@183 9876 if (tinymce.activeEditor != t) {
bgneal@183 9877 if ((oed = tinymce.activeEditor) != null)
bgneal@45 9878 oed.onDeactivate.dispatch(oed, t);
bgneal@45 9879
bgneal@45 9880 t.onActivate.dispatch(t, oed);
bgneal@45 9881 }
bgneal@45 9882
bgneal@183 9883 tinymce._setActive(t);
bgneal@45 9884 },
bgneal@45 9885
bgneal@45 9886 execCallback : function(n) {
bgneal@45 9887 var t = this, f = t.settings[n], s;
bgneal@45 9888
bgneal@45 9889 if (!f)
bgneal@45 9890 return;
bgneal@45 9891
bgneal@45 9892 // Look through lookup
bgneal@45 9893 if (t.callbackLookup && (s = t.callbackLookup[n])) {
bgneal@45 9894 f = s.func;
bgneal@45 9895 s = s.scope;
bgneal@45 9896 }
bgneal@45 9897
bgneal@45 9898 if (is(f, 'string')) {
bgneal@45 9899 s = f.replace(/\.\w+$/, '');
bgneal@45 9900 s = s ? tinymce.resolve(s) : 0;
bgneal@45 9901 f = tinymce.resolve(f);
bgneal@45 9902 t.callbackLookup = t.callbackLookup || {};
bgneal@45 9903 t.callbackLookup[n] = {func : f, scope : s};
bgneal@45 9904 }
bgneal@45 9905
bgneal@45 9906 return f.apply(s || t, Array.prototype.slice.call(arguments, 1));
bgneal@45 9907 },
bgneal@45 9908
bgneal@45 9909 translate : function(s) {
bgneal@183 9910 var c = this.settings.language || 'en', i18n = tinymce.i18n;
bgneal@45 9911
bgneal@45 9912 if (!s)
bgneal@45 9913 return '';
bgneal@45 9914
bgneal@45 9915 return i18n[c + '.' + s] || s.replace(/{\#([^}]+)\}/g, function(a, b) {
bgneal@45 9916 return i18n[c + '.' + b] || '{#' + b + '}';
bgneal@45 9917 });
bgneal@45 9918 },
bgneal@45 9919
bgneal@45 9920 getLang : function(n, dv) {
bgneal@183 9921 return tinymce.i18n[(this.settings.language || 'en') + '.' + n] || (is(dv) ? dv : '{#' + n + '}');
bgneal@45 9922 },
bgneal@45 9923
bgneal@45 9924 getParam : function(n, dv, ty) {
bgneal@45 9925 var tr = tinymce.trim, v = is(this.settings[n]) ? this.settings[n] : dv, o;
bgneal@45 9926
bgneal@45 9927 if (ty === 'hash') {
bgneal@45 9928 o = {};
bgneal@45 9929
bgneal@45 9930 if (is(v, 'string')) {
bgneal@45 9931 each(v.indexOf('=') > 0 ? v.split(/[;,](?![^=;,]*(?:[;,]|$))/) : v.split(','), function(v) {
bgneal@45 9932 v = v.split('=');
bgneal@45 9933
bgneal@45 9934 if (v.length > 1)
bgneal@45 9935 o[tr(v[0])] = tr(v[1]);
bgneal@45 9936 else
bgneal@45 9937 o[tr(v[0])] = tr(v);
bgneal@45 9938 });
bgneal@45 9939 } else
bgneal@45 9940 o = v;
bgneal@45 9941
bgneal@45 9942 return o;
bgneal@45 9943 }
bgneal@45 9944
bgneal@45 9945 return v;
bgneal@45 9946 },
bgneal@45 9947
bgneal@45 9948 nodeChanged : function(o) {
bgneal@183 9949 var t = this, s = t.selection, n = (isIE ? s.getNode() : s.getStart()) || t.getBody();
bgneal@45 9950
bgneal@45 9951 // Fix for bug #1896577 it seems that this can not be fired while the editor is loading
bgneal@45 9952 if (t.initialized) {
bgneal@183 9953 o = o || {};
bgneal@183 9954 n = isIE && n.ownerDocument != t.getDoc() ? t.getBody() : n; // Fix for IE initial state
bgneal@183 9955
bgneal@183 9956 // Get parents and add them to object
bgneal@183 9957 o.parents = [];
bgneal@183 9958 t.dom.getParent(n, function(node) {
bgneal@183 9959 if (node.nodeName == 'BODY')
bgneal@183 9960 return true;
bgneal@183 9961
bgneal@183 9962 o.parents.push(node);
bgneal@183 9963 });
bgneal@183 9964
bgneal@45 9965 t.onNodeChange.dispatch(
bgneal@45 9966 t,
bgneal@45 9967 o ? o.controlManager || t.controlManager : t.controlManager,
bgneal@183 9968 n,
bgneal@45 9969 s.isCollapsed(),
bgneal@45 9970 o
bgneal@45 9971 );
bgneal@45 9972 }
bgneal@45 9973 },
bgneal@45 9974
bgneal@45 9975 addButton : function(n, s) {
bgneal@45 9976 var t = this;
bgneal@45 9977
bgneal@45 9978 t.buttons = t.buttons || {};
bgneal@45 9979 t.buttons[n] = s;
bgneal@45 9980 },
bgneal@45 9981
bgneal@45 9982 addCommand : function(n, f, s) {
bgneal@45 9983 this.execCommands[n] = {func : f, scope : s || this};
bgneal@45 9984 },
bgneal@45 9985
bgneal@45 9986 addQueryStateHandler : function(n, f, s) {
bgneal@45 9987 this.queryStateCommands[n] = {func : f, scope : s || this};
bgneal@45 9988 },
bgneal@45 9989
bgneal@45 9990 addQueryValueHandler : function(n, f, s) {
bgneal@45 9991 this.queryValueCommands[n] = {func : f, scope : s || this};
bgneal@45 9992 },
bgneal@45 9993
bgneal@45 9994 addShortcut : function(pa, desc, cmd_func, sc) {
bgneal@45 9995 var t = this, c;
bgneal@45 9996
bgneal@45 9997 if (!t.settings.custom_shortcuts)
bgneal@45 9998 return false;
bgneal@45 9999
bgneal@45 10000 t.shortcuts = t.shortcuts || {};
bgneal@45 10001
bgneal@45 10002 if (is(cmd_func, 'string')) {
bgneal@45 10003 c = cmd_func;
bgneal@45 10004
bgneal@45 10005 cmd_func = function() {
bgneal@45 10006 t.execCommand(c, false, null);
bgneal@45 10007 };
bgneal@45 10008 }
bgneal@45 10009
bgneal@45 10010 if (is(cmd_func, 'object')) {
bgneal@45 10011 c = cmd_func;
bgneal@45 10012
bgneal@45 10013 cmd_func = function() {
bgneal@45 10014 t.execCommand(c[0], c[1], c[2]);
bgneal@45 10015 };
bgneal@45 10016 }
bgneal@45 10017
bgneal@45 10018 each(explode(pa), function(pa) {
bgneal@45 10019 var o = {
bgneal@45 10020 func : cmd_func,
bgneal@45 10021 scope : sc || this,
bgneal@45 10022 desc : desc,
bgneal@45 10023 alt : false,
bgneal@45 10024 ctrl : false,
bgneal@45 10025 shift : false
bgneal@45 10026 };
bgneal@45 10027
bgneal@45 10028 each(explode(pa, '+'), function(v) {
bgneal@45 10029 switch (v) {
bgneal@45 10030 case 'alt':
bgneal@45 10031 case 'ctrl':
bgneal@45 10032 case 'shift':
bgneal@45 10033 o[v] = true;
bgneal@45 10034 break;
bgneal@45 10035
bgneal@45 10036 default:
bgneal@45 10037 o.charCode = v.charCodeAt(0);
bgneal@45 10038 o.keyCode = v.toUpperCase().charCodeAt(0);
bgneal@45 10039 }
bgneal@45 10040 });
bgneal@45 10041
bgneal@45 10042 t.shortcuts[(o.ctrl ? 'ctrl' : '') + ',' + (o.alt ? 'alt' : '') + ',' + (o.shift ? 'shift' : '') + ',' + o.keyCode] = o;
bgneal@45 10043 });
bgneal@45 10044
bgneal@45 10045 return true;
bgneal@45 10046 },
bgneal@45 10047
bgneal@45 10048 execCommand : function(cmd, ui, val, a) {
bgneal@45 10049 var t = this, s = 0, o, st;
bgneal@45 10050
bgneal@45 10051 if (!/^(mceAddUndoLevel|mceEndUndoLevel|mceBeginUndoLevel|mceRepaint|SelectAll)$/.test(cmd) && (!a || !a.skip_focus))
bgneal@45 10052 t.focus();
bgneal@45 10053
bgneal@45 10054 o = {};
bgneal@45 10055 t.onBeforeExecCommand.dispatch(t, cmd, ui, val, o);
bgneal@45 10056 if (o.terminate)
bgneal@45 10057 return false;
bgneal@45 10058
bgneal@45 10059 // Command callback
bgneal@45 10060 if (t.execCallback('execcommand_callback', t.id, t.selection.getNode(), cmd, ui, val)) {
bgneal@45 10061 t.onExecCommand.dispatch(t, cmd, ui, val, a);
bgneal@45 10062 return true;
bgneal@45 10063 }
bgneal@45 10064
bgneal@45 10065 // Registred commands
bgneal@45 10066 if (o = t.execCommands[cmd]) {
bgneal@45 10067 st = o.func.call(o.scope, ui, val);
bgneal@45 10068
bgneal@45 10069 // Fall through on true
bgneal@45 10070 if (st !== true) {
bgneal@45 10071 t.onExecCommand.dispatch(t, cmd, ui, val, a);
bgneal@45 10072 return st;
bgneal@45 10073 }
bgneal@45 10074 }
bgneal@45 10075
bgneal@45 10076 // Plugin commands
bgneal@45 10077 each(t.plugins, function(p) {
bgneal@45 10078 if (p.execCommand && p.execCommand(cmd, ui, val)) {
bgneal@45 10079 t.onExecCommand.dispatch(t, cmd, ui, val, a);
bgneal@45 10080 s = 1;
bgneal@45 10081 return false;
bgneal@45 10082 }
bgneal@45 10083 });
bgneal@45 10084
bgneal@45 10085 if (s)
bgneal@45 10086 return true;
bgneal@45 10087
bgneal@45 10088 // Theme commands
bgneal@45 10089 if (t.theme && t.theme.execCommand && t.theme.execCommand(cmd, ui, val)) {
bgneal@45 10090 t.onExecCommand.dispatch(t, cmd, ui, val, a);
bgneal@45 10091 return true;
bgneal@45 10092 }
bgneal@45 10093
bgneal@45 10094 // Execute global commands
bgneal@45 10095 if (tinymce.GlobalCommands.execCommand(t, cmd, ui, val)) {
bgneal@45 10096 t.onExecCommand.dispatch(t, cmd, ui, val, a);
bgneal@45 10097 return true;
bgneal@45 10098 }
bgneal@45 10099
bgneal@45 10100 // Editor commands
bgneal@45 10101 if (t.editorCommands.execCommand(cmd, ui, val)) {
bgneal@45 10102 t.onExecCommand.dispatch(t, cmd, ui, val, a);
bgneal@45 10103 return true;
bgneal@45 10104 }
bgneal@45 10105
bgneal@45 10106 // Browser commands
bgneal@45 10107 t.getDoc().execCommand(cmd, ui, val);
bgneal@45 10108 t.onExecCommand.dispatch(t, cmd, ui, val, a);
bgneal@45 10109 },
bgneal@45 10110
bgneal@183 10111 queryCommandState : function(cmd) {
bgneal@45 10112 var t = this, o, s;
bgneal@45 10113
bgneal@45 10114 // Is hidden then return undefined
bgneal@45 10115 if (t._isHidden())
bgneal@45 10116 return;
bgneal@45 10117
bgneal@45 10118 // Registred commands
bgneal@183 10119 if (o = t.queryStateCommands[cmd]) {
bgneal@45 10120 s = o.func.call(o.scope);
bgneal@45 10121
bgneal@45 10122 // Fall though on true
bgneal@45 10123 if (s !== true)
bgneal@45 10124 return s;
bgneal@45 10125 }
bgneal@45 10126
bgneal@45 10127 // Registred commands
bgneal@183 10128 o = t.editorCommands.queryCommandState(cmd);
bgneal@45 10129 if (o !== -1)
bgneal@45 10130 return o;
bgneal@45 10131
bgneal@45 10132 // Browser commands
bgneal@45 10133 try {
bgneal@183 10134 return this.getDoc().queryCommandState(cmd);
bgneal@45 10135 } catch (ex) {
bgneal@45 10136 // Fails sometimes see bug: 1896577
bgneal@45 10137 }
bgneal@45 10138 },
bgneal@45 10139
bgneal@45 10140 queryCommandValue : function(c) {
bgneal@45 10141 var t = this, o, s;
bgneal@45 10142
bgneal@45 10143 // Is hidden then return undefined
bgneal@45 10144 if (t._isHidden())
bgneal@45 10145 return;
bgneal@45 10146
bgneal@45 10147 // Registred commands
bgneal@45 10148 if (o = t.queryValueCommands[c]) {
bgneal@45 10149 s = o.func.call(o.scope);
bgneal@45 10150
bgneal@45 10151 // Fall though on true
bgneal@45 10152 if (s !== true)
bgneal@45 10153 return s;
bgneal@45 10154 }
bgneal@45 10155
bgneal@45 10156 // Registred commands
bgneal@45 10157 o = t.editorCommands.queryCommandValue(c);
bgneal@45 10158 if (is(o))
bgneal@45 10159 return o;
bgneal@45 10160
bgneal@45 10161 // Browser commands
bgneal@45 10162 try {
bgneal@45 10163 return this.getDoc().queryCommandValue(c);
bgneal@45 10164 } catch (ex) {
bgneal@45 10165 // Fails sometimes see bug: 1896577
bgneal@45 10166 }
bgneal@45 10167 },
bgneal@45 10168
bgneal@45 10169 show : function() {
bgneal@45 10170 var t = this;
bgneal@45 10171
bgneal@45 10172 DOM.show(t.getContainer());
bgneal@45 10173 DOM.hide(t.id);
bgneal@45 10174 t.load();
bgneal@45 10175 },
bgneal@45 10176
bgneal@45 10177 hide : function() {
bgneal@45 10178 var t = this, d = t.getDoc();
bgneal@45 10179
bgneal@45 10180 // Fixed bug where IE has a blinking cursor left from the editor
bgneal@45 10181 if (isIE && d)
bgneal@45 10182 d.execCommand('SelectAll');
bgneal@45 10183
bgneal@45 10184 // We must save before we hide so Safari doesn't crash
bgneal@45 10185 t.save();
bgneal@45 10186 DOM.hide(t.getContainer());
bgneal@45 10187 DOM.setStyle(t.id, 'display', t.orgDisplay);
bgneal@45 10188 },
bgneal@45 10189
bgneal@45 10190 isHidden : function() {
bgneal@45 10191 return !DOM.isHidden(this.id);
bgneal@45 10192 },
bgneal@45 10193
bgneal@45 10194 setProgressState : function(b, ti, o) {
bgneal@45 10195 this.onSetProgressState.dispatch(this, b, ti, o);
bgneal@45 10196
bgneal@45 10197 return b;
bgneal@45 10198 },
bgneal@45 10199
bgneal@45 10200 load : function(o) {
bgneal@45 10201 var t = this, e = t.getElement(), h;
bgneal@45 10202
bgneal@45 10203 if (e) {
bgneal@45 10204 o = o || {};
bgneal@45 10205 o.load = true;
bgneal@45 10206
bgneal@45 10207 // Double encode existing entities in the value
bgneal@45 10208 h = t.setContent(is(e.value) ? e.value : e.innerHTML, o);
bgneal@45 10209 o.element = e;
bgneal@45 10210
bgneal@45 10211 if (!o.no_events)
bgneal@45 10212 t.onLoadContent.dispatch(t, o);
bgneal@45 10213
bgneal@45 10214 o.element = e = null;
bgneal@45 10215
bgneal@45 10216 return h;
bgneal@45 10217 }
bgneal@45 10218 },
bgneal@45 10219
bgneal@45 10220 save : function(o) {
bgneal@45 10221 var t = this, e = t.getElement(), h, f;
bgneal@45 10222
bgneal@45 10223 if (!e || !t.initialized)
bgneal@45 10224 return;
bgneal@45 10225
bgneal@45 10226 o = o || {};
bgneal@45 10227 o.save = true;
bgneal@45 10228
bgneal@45 10229 // Add undo level will trigger onchange event
bgneal@45 10230 if (!o.no_events) {
bgneal@45 10231 t.undoManager.typing = 0;
bgneal@45 10232 t.undoManager.add();
bgneal@45 10233 }
bgneal@45 10234
bgneal@45 10235 o.element = e;
bgneal@45 10236 h = o.content = t.getContent(o);
bgneal@45 10237
bgneal@45 10238 if (!o.no_events)
bgneal@45 10239 t.onSaveContent.dispatch(t, o);
bgneal@45 10240
bgneal@45 10241 h = o.content;
bgneal@45 10242
bgneal@45 10243 if (!/TEXTAREA|INPUT/i.test(e.nodeName)) {
bgneal@45 10244 e.innerHTML = h;
bgneal@45 10245
bgneal@45 10246 // Update hidden form element
bgneal@45 10247 if (f = DOM.getParent(t.id, 'form')) {
bgneal@45 10248 each(f.elements, function(e) {
bgneal@45 10249 if (e.name == t.id) {
bgneal@45 10250 e.value = h;
bgneal@45 10251 return false;
bgneal@45 10252 }
bgneal@45 10253 });
bgneal@45 10254 }
bgneal@45 10255 } else
bgneal@45 10256 e.value = h;
bgneal@45 10257
bgneal@45 10258 o.element = e = null;
bgneal@45 10259
bgneal@45 10260 return h;
bgneal@45 10261 },
bgneal@45 10262
bgneal@45 10263 setContent : function(h, o) {
bgneal@45 10264 var t = this;
bgneal@45 10265
bgneal@45 10266 o = o || {};
bgneal@45 10267 o.format = o.format || 'html';
bgneal@45 10268 o.set = true;
bgneal@45 10269 o.content = h;
bgneal@45 10270
bgneal@45 10271 if (!o.no_events)
bgneal@45 10272 t.onBeforeSetContent.dispatch(t, o);
bgneal@45 10273
bgneal@45 10274 // Padd empty content in Gecko and Safari. Commands will otherwise fail on the content
bgneal@45 10275 // It will also be impossible to place the caret in the editor unless there is a BR element present
bgneal@45 10276 if (!tinymce.isIE && (h.length === 0 || /^\s+$/.test(h))) {
bgneal@183 10277 o.content = t.dom.setHTML(t.getBody(), '<br _mce_bogus="1" />');
bgneal@45 10278 o.format = 'raw';
bgneal@45 10279 }
bgneal@45 10280
bgneal@45 10281 o.content = t.dom.setHTML(t.getBody(), tinymce.trim(o.content));
bgneal@45 10282
bgneal@45 10283 if (o.format != 'raw' && t.settings.cleanup) {
bgneal@45 10284 o.getInner = true;
bgneal@45 10285 o.content = t.dom.setHTML(t.getBody(), t.serializer.serialize(t.getBody(), o));
bgneal@45 10286 }
bgneal@45 10287
bgneal@45 10288 if (!o.no_events)
bgneal@45 10289 t.onSetContent.dispatch(t, o);
bgneal@45 10290
bgneal@45 10291 return o.content;
bgneal@45 10292 },
bgneal@45 10293
bgneal@45 10294 getContent : function(o) {
bgneal@45 10295 var t = this, h;
bgneal@45 10296
bgneal@45 10297 o = o || {};
bgneal@45 10298 o.format = o.format || 'html';
bgneal@45 10299 o.get = true;
bgneal@45 10300
bgneal@45 10301 if (!o.no_events)
bgneal@45 10302 t.onBeforeGetContent.dispatch(t, o);
bgneal@45 10303
bgneal@45 10304 if (o.format != 'raw' && t.settings.cleanup) {
bgneal@45 10305 o.getInner = true;
bgneal@45 10306 h = t.serializer.serialize(t.getBody(), o);
bgneal@45 10307 } else
bgneal@45 10308 h = t.getBody().innerHTML;
bgneal@45 10309
bgneal@45 10310 h = h.replace(/^\s*|\s*$/g, '');
bgneal@45 10311 o.content = h;
bgneal@45 10312
bgneal@45 10313 if (!o.no_events)
bgneal@45 10314 t.onGetContent.dispatch(t, o);
bgneal@45 10315
bgneal@45 10316 return o.content;
bgneal@45 10317 },
bgneal@45 10318
bgneal@45 10319 isDirty : function() {
bgneal@45 10320 var t = this;
bgneal@45 10321
bgneal@45 10322 return tinymce.trim(t.startContent) != tinymce.trim(t.getContent({format : 'raw', no_events : 1})) && !t.isNotDirty;
bgneal@45 10323 },
bgneal@45 10324
bgneal@45 10325 getContainer : function() {
bgneal@45 10326 var t = this;
bgneal@45 10327
bgneal@45 10328 if (!t.container)
bgneal@45 10329 t.container = DOM.get(t.editorContainer || t.id + '_parent');
bgneal@45 10330
bgneal@45 10331 return t.container;
bgneal@45 10332 },
bgneal@45 10333
bgneal@45 10334 getContentAreaContainer : function() {
bgneal@45 10335 return this.contentAreaContainer;
bgneal@45 10336 },
bgneal@45 10337
bgneal@45 10338 getElement : function() {
bgneal@45 10339 return DOM.get(this.settings.content_element || this.id);
bgneal@45 10340 },
bgneal@45 10341
bgneal@45 10342 getWin : function() {
bgneal@45 10343 var t = this, e;
bgneal@45 10344
bgneal@45 10345 if (!t.contentWindow) {
bgneal@45 10346 e = DOM.get(t.id + "_ifr");
bgneal@45 10347
bgneal@45 10348 if (e)
bgneal@45 10349 t.contentWindow = e.contentWindow;
bgneal@45 10350 }
bgneal@45 10351
bgneal@45 10352 return t.contentWindow;
bgneal@45 10353 },
bgneal@45 10354
bgneal@45 10355 getDoc : function() {
bgneal@45 10356 var t = this, w;
bgneal@45 10357
bgneal@45 10358 if (!t.contentDocument) {
bgneal@45 10359 w = t.getWin();
bgneal@45 10360
bgneal@45 10361 if (w)
bgneal@45 10362 t.contentDocument = w.document;
bgneal@45 10363 }
bgneal@45 10364
bgneal@45 10365 return t.contentDocument;
bgneal@45 10366 },
bgneal@45 10367
bgneal@45 10368 getBody : function() {
bgneal@45 10369 return this.bodyElement || this.getDoc().body;
bgneal@45 10370 },
bgneal@45 10371
bgneal@45 10372 convertURL : function(u, n, e) {
bgneal@45 10373 var t = this, s = t.settings;
bgneal@45 10374
bgneal@45 10375 // Use callback instead
bgneal@45 10376 if (s.urlconverter_callback)
bgneal@45 10377 return t.execCallback('urlconverter_callback', u, e, true, n);
bgneal@45 10378
bgneal@45 10379 // Don't convert link href since thats the CSS files that gets loaded into the editor also skip local file URLs
bgneal@45 10380 if (!s.convert_urls || (e && e.nodeName == 'LINK') || u.indexOf('file:') === 0)
bgneal@45 10381 return u;
bgneal@45 10382
bgneal@45 10383 // Convert to relative
bgneal@45 10384 if (s.relative_urls)
bgneal@45 10385 return t.documentBaseURI.toRelative(u);
bgneal@45 10386
bgneal@45 10387 // Convert to absolute
bgneal@45 10388 u = t.documentBaseURI.toAbsolute(u, s.remove_script_host);
bgneal@45 10389
bgneal@45 10390 return u;
bgneal@45 10391 },
bgneal@45 10392
bgneal@45 10393 addVisual : function(e) {
bgneal@45 10394 var t = this, s = t.settings;
bgneal@45 10395
bgneal@45 10396 e = e || t.getBody();
bgneal@45 10397
bgneal@45 10398 if (!is(t.hasVisual))
bgneal@45 10399 t.hasVisual = s.visual;
bgneal@45 10400
bgneal@45 10401 each(t.dom.select('table,a', e), function(e) {
bgneal@45 10402 var v;
bgneal@45 10403
bgneal@45 10404 switch (e.nodeName) {
bgneal@45 10405 case 'TABLE':
bgneal@45 10406 v = t.dom.getAttrib(e, 'border');
bgneal@45 10407
bgneal@45 10408 if (!v || v == '0') {
bgneal@45 10409 if (t.hasVisual)
bgneal@45 10410 t.dom.addClass(e, s.visual_table_class);
bgneal@45 10411 else
bgneal@45 10412 t.dom.removeClass(e, s.visual_table_class);
bgneal@45 10413 }
bgneal@45 10414
bgneal@45 10415 return;
bgneal@45 10416
bgneal@45 10417 case 'A':
bgneal@45 10418 v = t.dom.getAttrib(e, 'name');
bgneal@45 10419
bgneal@45 10420 if (v) {
bgneal@45 10421 if (t.hasVisual)
bgneal@45 10422 t.dom.addClass(e, 'mceItemAnchor');
bgneal@45 10423 else
bgneal@45 10424 t.dom.removeClass(e, 'mceItemAnchor');
bgneal@45 10425 }
bgneal@45 10426
bgneal@45 10427 return;
bgneal@45 10428 }
bgneal@45 10429 });
bgneal@45 10430
bgneal@45 10431 t.onVisualAid.dispatch(t, e, t.hasVisual);
bgneal@45 10432 },
bgneal@45 10433
bgneal@45 10434 remove : function() {
bgneal@45 10435 var t = this, e = t.getContainer();
bgneal@45 10436
bgneal@45 10437 t.removed = 1; // Cancels post remove event execution
bgneal@45 10438 t.hide();
bgneal@45 10439
bgneal@45 10440 t.execCallback('remove_instance_callback', t);
bgneal@45 10441 t.onRemove.dispatch(t);
bgneal@45 10442
bgneal@45 10443 // Clear all execCommand listeners this is required to avoid errors if the editor was removed inside another command
bgneal@45 10444 t.onExecCommand.listeners = [];
bgneal@45 10445
bgneal@183 10446 tinymce.remove(t);
bgneal@45 10447 DOM.remove(e);
bgneal@45 10448 },
bgneal@45 10449
bgneal@45 10450 destroy : function(s) {
bgneal@45 10451 var t = this;
bgneal@45 10452
bgneal@45 10453 // One time is enough
bgneal@45 10454 if (t.destroyed)
bgneal@45 10455 return;
bgneal@45 10456
bgneal@45 10457 if (!s) {
bgneal@45 10458 tinymce.removeUnload(t.destroy);
bgneal@45 10459 tinyMCE.onBeforeUnload.remove(t._beforeUnload);
bgneal@45 10460
bgneal@45 10461 // Manual destroy
bgneal@45 10462 if (t.theme && t.theme.destroy)
bgneal@45 10463 t.theme.destroy();
bgneal@45 10464
bgneal@45 10465 // Destroy controls, selection and dom
bgneal@45 10466 t.controlManager.destroy();
bgneal@45 10467 t.selection.destroy();
bgneal@45 10468 t.dom.destroy();
bgneal@45 10469
bgneal@45 10470 // Remove all events
bgneal@45 10471
bgneal@45 10472 // Don't clear the window or document if content editable
bgneal@45 10473 // is enabled since other instances might still be present
bgneal@45 10474 if (!t.settings.content_editable) {
bgneal@45 10475 Event.clear(t.getWin());
bgneal@45 10476 Event.clear(t.getDoc());
bgneal@45 10477 }
bgneal@45 10478
bgneal@45 10479 Event.clear(t.getBody());
bgneal@45 10480 Event.clear(t.formElement);
bgneal@45 10481 }
bgneal@45 10482
bgneal@45 10483 if (t.formElement) {
bgneal@45 10484 t.formElement.submit = t.formElement._mceOldSubmit;
bgneal@45 10485 t.formElement._mceOldSubmit = null;
bgneal@45 10486 }
bgneal@45 10487
bgneal@45 10488 t.contentAreaContainer = t.formElement = t.container = t.settings.content_element = t.bodyElement = t.contentDocument = t.contentWindow = null;
bgneal@45 10489
bgneal@45 10490 if (t.selection)
bgneal@45 10491 t.selection = t.selection.win = t.selection.dom = t.selection.dom.doc = null;
bgneal@45 10492
bgneal@45 10493 t.destroyed = 1;
bgneal@45 10494 },
bgneal@45 10495
bgneal@45 10496 // Internal functions
bgneal@45 10497
bgneal@45 10498 _addEvents : function() {
bgneal@45 10499 // 'focus', 'blur', 'dblclick', 'beforedeactivate', submit, reset
bgneal@45 10500 var t = this, i, s = t.settings, lo = {
bgneal@45 10501 mouseup : 'onMouseUp',
bgneal@45 10502 mousedown : 'onMouseDown',
bgneal@45 10503 click : 'onClick',
bgneal@45 10504 keyup : 'onKeyUp',
bgneal@45 10505 keydown : 'onKeyDown',
bgneal@45 10506 keypress : 'onKeyPress',
bgneal@45 10507 submit : 'onSubmit',
bgneal@45 10508 reset : 'onReset',
bgneal@45 10509 contextmenu : 'onContextMenu',
bgneal@45 10510 dblclick : 'onDblClick',
bgneal@45 10511 paste : 'onPaste' // Doesn't work in all browsers yet
bgneal@45 10512 };
bgneal@45 10513
bgneal@45 10514 function eventHandler(e, o) {
bgneal@45 10515 var ty = e.type;
bgneal@45 10516
bgneal@45 10517 // Don't fire events when it's removed
bgneal@45 10518 if (t.removed)
bgneal@45 10519 return;
bgneal@45 10520
bgneal@45 10521 // Generic event handler
bgneal@45 10522 if (t.onEvent.dispatch(t, e, o) !== false) {
bgneal@45 10523 // Specific event handler
bgneal@45 10524 t[lo[e.fakeType || e.type]].dispatch(t, e, o);
bgneal@45 10525 }
bgneal@45 10526 };
bgneal@45 10527
bgneal@45 10528 // Add DOM events
bgneal@45 10529 each(lo, function(v, k) {
bgneal@45 10530 switch (k) {
bgneal@45 10531 case 'contextmenu':
bgneal@45 10532 if (tinymce.isOpera) {
bgneal@45 10533 // Fake contextmenu on Opera
bgneal@183 10534 t.dom.bind(t.getBody(), 'mousedown', function(e) {
bgneal@45 10535 if (e.ctrlKey) {
bgneal@45 10536 e.fakeType = 'contextmenu';
bgneal@45 10537 eventHandler(e);
bgneal@45 10538 }
bgneal@45 10539 });
bgneal@45 10540 } else
bgneal@183 10541 t.dom.bind(t.getBody(), k, eventHandler);
bgneal@45 10542 break;
bgneal@45 10543
bgneal@45 10544 case 'paste':
bgneal@183 10545 t.dom.bind(t.getBody(), k, function(e) {
bgneal@183 10546 eventHandler(e);
bgneal@45 10547 });
bgneal@45 10548 break;
bgneal@45 10549
bgneal@45 10550 case 'submit':
bgneal@45 10551 case 'reset':
bgneal@183 10552 t.dom.bind(t.getElement().form || DOM.getParent(t.id, 'form'), k, eventHandler);
bgneal@45 10553 break;
bgneal@45 10554
bgneal@45 10555 default:
bgneal@183 10556 t.dom.bind(s.content_editable ? t.getBody() : t.getDoc(), k, eventHandler);
bgneal@183 10557 }
bgneal@183 10558 });
bgneal@183 10559
bgneal@183 10560 t.dom.bind(s.content_editable ? t.getBody() : (isGecko ? t.getDoc() : t.getWin()), 'focus', function(e) {
bgneal@45 10561 t.focus(true);
bgneal@45 10562 });
bgneal@45 10563
bgneal@45 10564
bgneal@45 10565 // Fixes bug where a specified document_base_uri could result in broken images
bgneal@45 10566 // This will also fix drag drop of images in Gecko
bgneal@45 10567 if (tinymce.isGecko) {
bgneal@45 10568 // Convert all images to absolute URLs
bgneal@45 10569 /* t.onSetContent.add(function(ed, o) {
bgneal@45 10570 each(ed.dom.select('img'), function(e) {
bgneal@45 10571 var v;
bgneal@45 10572
bgneal@183 10573 if (v = e.getAttribute('_mce_src'))
bgneal@45 10574 e.src = t.documentBaseURI.toAbsolute(v);
bgneal@45 10575 })
bgneal@45 10576 });*/
bgneal@45 10577
bgneal@183 10578 t.dom.bind(t.getDoc(), 'DOMNodeInserted', function(e) {
bgneal@45 10579 var v;
bgneal@45 10580
bgneal@45 10581 e = e.target;
bgneal@45 10582
bgneal@183 10583 if (e.nodeType === 1 && e.nodeName === 'IMG' && (v = e.getAttribute('_mce_src')))
bgneal@45 10584 e.src = t.documentBaseURI.toAbsolute(v);
bgneal@45 10585 });
bgneal@45 10586 }
bgneal@45 10587
bgneal@45 10588 // Set various midas options in Gecko
bgneal@45 10589 if (isGecko) {
bgneal@45 10590 function setOpts() {
bgneal@45 10591 var t = this, d = t.getDoc(), s = t.settings;
bgneal@45 10592
bgneal@45 10593 if (isGecko && !s.readonly) {
bgneal@45 10594 if (t._isHidden()) {
bgneal@45 10595 try {
bgneal@45 10596 if (!s.content_editable)
bgneal@45 10597 d.designMode = 'On';
bgneal@45 10598 } catch (ex) {
bgneal@45 10599 // Fails if it's hidden
bgneal@45 10600 }
bgneal@45 10601 }
bgneal@45 10602
bgneal@45 10603 try {
bgneal@45 10604 // Try new Gecko method
bgneal@45 10605 d.execCommand("styleWithCSS", 0, false);
bgneal@45 10606 } catch (ex) {
bgneal@45 10607 // Use old method
bgneal@45 10608 if (!t._isHidden())
bgneal@45 10609 try {d.execCommand("useCSS", 0, true);} catch (ex) {}
bgneal@45 10610 }
bgneal@45 10611
bgneal@45 10612 if (!s.table_inline_editing)
bgneal@45 10613 try {d.execCommand('enableInlineTableEditing', false, false);} catch (ex) {}
bgneal@45 10614
bgneal@45 10615 if (!s.object_resizing)
bgneal@45 10616 try {d.execCommand('enableObjectResizing', false, false);} catch (ex) {}
bgneal@45 10617 }
bgneal@45 10618 };
bgneal@45 10619
bgneal@45 10620 t.onBeforeExecCommand.add(setOpts);
bgneal@45 10621 t.onMouseDown.add(setOpts);
bgneal@45 10622 }
bgneal@45 10623
bgneal@183 10624 // Workaround for bug, http://bugs.webkit.org/show_bug.cgi?id=12250
bgneal@183 10625 // WebKit can't even do simple things like selecting an image
bgneal@183 10626 // This also fixes so it's possible to select mceItemAnchors
bgneal@183 10627 if (tinymce.isWebKit) {
bgneal@183 10628 t.onClick.add(function(ed, e) {
bgneal@183 10629 e = e.target;
bgneal@183 10630
bgneal@183 10631 // Needs tobe the setBaseAndExtend or it will fail to select floated images
bgneal@183 10632 if (e.nodeName == 'IMG' || (e.nodeName == 'A' && t.dom.hasClass(e, 'mceItemAnchor')))
bgneal@183 10633 t.selection.getSel().setBaseAndExtent(e, 0, e, 1);
bgneal@183 10634 });
bgneal@183 10635 }
bgneal@183 10636
bgneal@45 10637 // Add node change handlers
bgneal@45 10638 t.onMouseUp.add(t.nodeChanged);
bgneal@45 10639 t.onClick.add(t.nodeChanged);
bgneal@45 10640 t.onKeyUp.add(function(ed, e) {
bgneal@45 10641 var c = e.keyCode;
bgneal@45 10642
bgneal@45 10643 if ((c >= 33 && c <= 36) || (c >= 37 && c <= 40) || c == 13 || c == 45 || c == 46 || c == 8 || (tinymce.isMac && (c == 91 || c == 93)) || e.ctrlKey)
bgneal@45 10644 t.nodeChanged();
bgneal@45 10645 });
bgneal@45 10646
bgneal@45 10647 // Add reset handler
bgneal@45 10648 t.onReset.add(function() {
bgneal@45 10649 t.setContent(t.startContent, {format : 'raw'});
bgneal@45 10650 });
bgneal@45 10651
bgneal@45 10652 // Add shortcuts
bgneal@45 10653 if (s.custom_shortcuts) {
bgneal@45 10654 if (s.custom_undo_redo_keyboard_shortcuts) {
bgneal@45 10655 t.addShortcut('ctrl+z', t.getLang('undo_desc'), 'Undo');
bgneal@45 10656 t.addShortcut('ctrl+y', t.getLang('redo_desc'), 'Redo');
bgneal@45 10657 }
bgneal@45 10658
bgneal@45 10659 // Add default shortcuts for gecko
bgneal@217 10660 t.addShortcut('ctrl+b', t.getLang('bold_desc'), 'Bold');
bgneal@217 10661 t.addShortcut('ctrl+i', t.getLang('italic_desc'), 'Italic');
bgneal@217 10662 t.addShortcut('ctrl+u', t.getLang('underline_desc'), 'Underline');
bgneal@45 10663
bgneal@45 10664 // BlockFormat shortcuts keys
bgneal@45 10665 for (i=1; i<=6; i++)
bgneal@183 10666 t.addShortcut('ctrl+' + i, '', ['FormatBlock', false, 'h' + i]);
bgneal@45 10667
bgneal@45 10668 t.addShortcut('ctrl+7', '', ['FormatBlock', false, '<p>']);
bgneal@45 10669 t.addShortcut('ctrl+8', '', ['FormatBlock', false, '<div>']);
bgneal@45 10670 t.addShortcut('ctrl+9', '', ['FormatBlock', false, '<address>']);
bgneal@45 10671
bgneal@45 10672 function find(e) {
bgneal@45 10673 var v = null;
bgneal@45 10674
bgneal@45 10675 if (!e.altKey && !e.ctrlKey && !e.metaKey)
bgneal@45 10676 return v;
bgneal@45 10677
bgneal@45 10678 each(t.shortcuts, function(o) {
bgneal@45 10679 if (tinymce.isMac && o.ctrl != e.metaKey)
bgneal@45 10680 return;
bgneal@45 10681 else if (!tinymce.isMac && o.ctrl != e.ctrlKey)
bgneal@45 10682 return;
bgneal@45 10683
bgneal@45 10684 if (o.alt != e.altKey)
bgneal@45 10685 return;
bgneal@45 10686
bgneal@45 10687 if (o.shift != e.shiftKey)
bgneal@45 10688 return;
bgneal@45 10689
bgneal@45 10690 if (e.keyCode == o.keyCode || (e.charCode && e.charCode == o.charCode)) {
bgneal@45 10691 v = o;
bgneal@45 10692 return false;
bgneal@45 10693 }
bgneal@45 10694 });
bgneal@45 10695
bgneal@45 10696 return v;
bgneal@45 10697 };
bgneal@45 10698
bgneal@45 10699 t.onKeyUp.add(function(ed, e) {
bgneal@45 10700 var o = find(e);
bgneal@45 10701
bgneal@45 10702 if (o)
bgneal@45 10703 return Event.cancel(e);
bgneal@45 10704 });
bgneal@45 10705
bgneal@45 10706 t.onKeyPress.add(function(ed, e) {
bgneal@45 10707 var o = find(e);
bgneal@45 10708
bgneal@45 10709 if (o)
bgneal@45 10710 return Event.cancel(e);
bgneal@45 10711 });
bgneal@45 10712
bgneal@45 10713 t.onKeyDown.add(function(ed, e) {
bgneal@45 10714 var o = find(e);
bgneal@45 10715
bgneal@45 10716 if (o) {
bgneal@45 10717 o.func.call(o.scope);
bgneal@45 10718 return Event.cancel(e);
bgneal@45 10719 }
bgneal@45 10720 });
bgneal@45 10721 }
bgneal@45 10722
bgneal@45 10723 if (tinymce.isIE) {
bgneal@45 10724 // Fix so resize will only update the width and height attributes not the styles of an image
bgneal@45 10725 // It will also block mceItemNoResize items
bgneal@183 10726 t.dom.bind(t.getDoc(), 'controlselect', function(e) {
bgneal@45 10727 var re = t.resizeInfo, cb;
bgneal@45 10728
bgneal@45 10729 e = e.target;
bgneal@45 10730
bgneal@45 10731 // Don't do this action for non image elements
bgneal@45 10732 if (e.nodeName !== 'IMG')
bgneal@45 10733 return;
bgneal@45 10734
bgneal@45 10735 if (re)
bgneal@183 10736 t.dom.unbind(re.node, re.ev, re.cb);
bgneal@45 10737
bgneal@45 10738 if (!t.dom.hasClass(e, 'mceItemNoResize')) {
bgneal@45 10739 ev = 'resizeend';
bgneal@183 10740 cb = t.dom.bind(e, ev, function(e) {
bgneal@45 10741 var v;
bgneal@45 10742
bgneal@45 10743 e = e.target;
bgneal@45 10744
bgneal@45 10745 if (v = t.dom.getStyle(e, 'width')) {
bgneal@45 10746 t.dom.setAttrib(e, 'width', v.replace(/[^0-9%]+/g, ''));
bgneal@45 10747 t.dom.setStyle(e, 'width', '');
bgneal@45 10748 }
bgneal@45 10749
bgneal@45 10750 if (v = t.dom.getStyle(e, 'height')) {
bgneal@45 10751 t.dom.setAttrib(e, 'height', v.replace(/[^0-9%]+/g, ''));
bgneal@45 10752 t.dom.setStyle(e, 'height', '');
bgneal@45 10753 }
bgneal@45 10754 });
bgneal@45 10755 } else {
bgneal@45 10756 ev = 'resizestart';
bgneal@183 10757 cb = t.dom.bind(e, 'resizestart', Event.cancel, Event);
bgneal@45 10758 }
bgneal@45 10759
bgneal@45 10760 re = t.resizeInfo = {
bgneal@45 10761 node : e,
bgneal@45 10762 ev : ev,
bgneal@45 10763 cb : cb
bgneal@45 10764 };
bgneal@45 10765 });
bgneal@45 10766
bgneal@45 10767 t.onKeyDown.add(function(ed, e) {
bgneal@45 10768 switch (e.keyCode) {
bgneal@45 10769 case 8:
bgneal@45 10770 // Fix IE control + backspace browser bug
bgneal@45 10771 if (t.selection.getRng().item) {
bgneal@183 10772 ed.dom.remove(t.selection.getRng().item(0));
bgneal@45 10773 return Event.cancel(e);
bgneal@45 10774 }
bgneal@45 10775 }
bgneal@45 10776 });
bgneal@183 10777
bgneal@183 10778 /*if (t.dom.boxModel) {
bgneal@183 10779 t.getBody().style.height = '100%';
bgneal@183 10780
bgneal@183 10781 Event.add(t.getWin(), 'resize', function(e) {
bgneal@183 10782 var docElm = t.getDoc().documentElement;
bgneal@183 10783
bgneal@183 10784 docElm.style.height = (docElm.offsetHeight - 10) + 'px';
bgneal@183 10785 });
bgneal@183 10786 }*/
bgneal@45 10787 }
bgneal@45 10788
bgneal@45 10789 if (tinymce.isOpera) {
bgneal@45 10790 t.onClick.add(function(ed, e) {
bgneal@45 10791 Event.prevent(e);
bgneal@45 10792 });
bgneal@45 10793 }
bgneal@45 10794
bgneal@45 10795 // Add custom undo/redo handlers
bgneal@45 10796 if (s.custom_undo_redo) {
bgneal@45 10797 function addUndo() {
bgneal@45 10798 t.undoManager.typing = 0;
bgneal@45 10799 t.undoManager.add();
bgneal@45 10800 };
bgneal@45 10801
bgneal@183 10802 t.dom.bind(t.getDoc(), 'focusout', function(e) {
bgneal@183 10803 if (!t.removed && t.undoManager.typing)
bgneal@183 10804 addUndo();
bgneal@183 10805 });
bgneal@45 10806
bgneal@45 10807 t.onKeyUp.add(function(ed, e) {
bgneal@183 10808 if ((e.keyCode >= 33 && e.keyCode <= 36) || (e.keyCode >= 37 && e.keyCode <= 40) || e.keyCode == 13 || e.keyCode == 45 || e.ctrlKey)
bgneal@183 10809 addUndo();
bgneal@45 10810 });
bgneal@45 10811
bgneal@45 10812 t.onKeyDown.add(function(ed, e) {
bgneal@45 10813 // Is caracter positon keys
bgneal@45 10814 if ((e.keyCode >= 33 && e.keyCode <= 36) || (e.keyCode >= 37 && e.keyCode <= 40) || e.keyCode == 13 || e.keyCode == 45) {
bgneal@183 10815 if (t.undoManager.typing)
bgneal@183 10816 addUndo();
bgneal@45 10817
bgneal@45 10818 return;
bgneal@45 10819 }
bgneal@45 10820
bgneal@45 10821 if (!t.undoManager.typing) {
bgneal@45 10822 t.undoManager.add();
bgneal@45 10823 t.undoManager.typing = 1;
bgneal@45 10824 }
bgneal@45 10825 });
bgneal@183 10826
bgneal@183 10827 t.onMouseDown.add(function() {
bgneal@183 10828 if (t.undoManager.typing)
bgneal@183 10829 addUndo();
bgneal@183 10830 });
bgneal@183 10831 }
bgneal@45 10832 },
bgneal@45 10833
bgneal@45 10834 _isHidden : function() {
bgneal@45 10835 var s;
bgneal@45 10836
bgneal@45 10837 if (!isGecko)
bgneal@45 10838 return 0;
bgneal@45 10839
bgneal@45 10840 // Weird, wheres that cursor selection?
bgneal@45 10841 s = this.selection.getSel();
bgneal@45 10842 return (!s || !s.rangeCount || s.rangeCount == 0);
bgneal@45 10843 },
bgneal@45 10844
bgneal@45 10845 // Fix for bug #1867292
bgneal@45 10846 _fixNesting : function(s) {
bgneal@45 10847 var d = [], i;
bgneal@45 10848
bgneal@45 10849 s = s.replace(/<(\/)?([^\s>]+)[^>]*?>/g, function(a, b, c) {
bgneal@45 10850 var e;
bgneal@45 10851
bgneal@45 10852 // Handle end element
bgneal@45 10853 if (b === '/') {
bgneal@45 10854 if (!d.length)
bgneal@45 10855 return '';
bgneal@45 10856
bgneal@45 10857 if (c !== d[d.length - 1].tag) {
bgneal@45 10858 for (i=d.length - 1; i>=0; i--) {
bgneal@45 10859 if (d[i].tag === c) {
bgneal@45 10860 d[i].close = 1;
bgneal@45 10861 break;
bgneal@45 10862 }
bgneal@45 10863 }
bgneal@45 10864
bgneal@45 10865 return '';
bgneal@45 10866 } else {
bgneal@45 10867 d.pop();
bgneal@45 10868
bgneal@45 10869 if (d.length && d[d.length - 1].close) {
bgneal@45 10870 a = a + '</' + d[d.length - 1].tag + '>';
bgneal@45 10871 d.pop();
bgneal@45 10872 }
bgneal@45 10873 }
bgneal@45 10874 } else {
bgneal@45 10875 // Ignore these
bgneal@45 10876 if (/^(br|hr|input|meta|img|link|param)$/i.test(c))
bgneal@45 10877 return a;
bgneal@45 10878
bgneal@45 10879 // Ignore closed ones
bgneal@45 10880 if (/\/>$/.test(a))
bgneal@45 10881 return a;
bgneal@45 10882
bgneal@45 10883 d.push({tag : c}); // Push start element
bgneal@45 10884 }
bgneal@45 10885
bgneal@45 10886 return a;
bgneal@45 10887 });
bgneal@45 10888
bgneal@45 10889 // End all open tags
bgneal@45 10890 for (i=d.length - 1; i>=0; i--)
bgneal@45 10891 s += '</' + d[i].tag + '>';
bgneal@45 10892
bgneal@45 10893 return s;
bgneal@45 10894 }
bgneal@183 10895 });
bgneal@183 10896 })(tinymce);
bgneal@183 10897
bgneal@183 10898 (function(tinymce) {
bgneal@183 10899 // Added for compression purposes
bgneal@183 10900 var each = tinymce.each, undefined, TRUE = true, FALSE = false;
bgneal@183 10901
bgneal@183 10902 tinymce.EditorCommands = function(editor) {
bgneal@183 10903 var dom = editor.dom,
bgneal@183 10904 selection = editor.selection,
bgneal@183 10905 commands = {state: {}, exec : {}, value : {}},
bgneal@183 10906 settings = editor.settings,
bgneal@183 10907 bookmark;
bgneal@183 10908
bgneal@183 10909 function execCommand(command, ui, value) {
bgneal@183 10910 var func;
bgneal@183 10911
bgneal@183 10912 command = command.toLowerCase();
bgneal@183 10913 if (func = commands.exec[command]) {
bgneal@183 10914 func(command, ui, value);
bgneal@183 10915 return TRUE;
bgneal@183 10916 }
bgneal@183 10917
bgneal@183 10918 return FALSE;
bgneal@183 10919 };
bgneal@183 10920
bgneal@183 10921 function queryCommandState(command) {
bgneal@183 10922 var func;
bgneal@183 10923
bgneal@183 10924 command = command.toLowerCase();
bgneal@183 10925 if (func = commands.state[command])
bgneal@183 10926 return func(command);
bgneal@183 10927
bgneal@183 10928 return -1;
bgneal@183 10929 };
bgneal@183 10930
bgneal@183 10931 function queryCommandValue(command) {
bgneal@183 10932 var func;
bgneal@183 10933
bgneal@183 10934 command = command.toLowerCase();
bgneal@183 10935 if (func = commands.value[command])
bgneal@183 10936 return func(command);
bgneal@183 10937
bgneal@183 10938 return FALSE;
bgneal@183 10939 };
bgneal@183 10940
bgneal@183 10941 function addCommands(command_list, type) {
bgneal@183 10942 type = type || 'exec';
bgneal@183 10943
bgneal@183 10944 each(command_list, function(callback, command) {
bgneal@183 10945 each(command.toLowerCase().split(','), function(command) {
bgneal@183 10946 commands[type][command] = callback;
bgneal@183 10947 });
bgneal@183 10948 });
bgneal@183 10949 };
bgneal@183 10950
bgneal@183 10951 // Expose public methods
bgneal@183 10952 tinymce.extend(this, {
bgneal@183 10953 execCommand : execCommand,
bgneal@183 10954 queryCommandState : queryCommandState,
bgneal@183 10955 queryCommandValue : queryCommandValue,
bgneal@183 10956 addCommands : addCommands
bgneal@45 10957 });
bgneal@183 10958
bgneal@183 10959 // Private methods
bgneal@183 10960
bgneal@183 10961 function execNativeCommand(command, ui, value) {
bgneal@183 10962 if (ui === undefined)
bgneal@183 10963 ui = FALSE;
bgneal@183 10964
bgneal@183 10965 if (value === undefined)
bgneal@183 10966 value = null;
bgneal@183 10967
bgneal@183 10968 return editor.getDoc().execCommand(command, ui, value);
bgneal@183 10969 };
bgneal@183 10970
bgneal@183 10971 function isFormatMatch(name) {
bgneal@183 10972 return editor.formatter.match(name);
bgneal@183 10973 };
bgneal@183 10974
bgneal@183 10975 function toggleFormat(name, value) {
bgneal@183 10976 editor.formatter.toggle(name, value ? {value : value} : undefined);
bgneal@183 10977 };
bgneal@183 10978
bgneal@183 10979 function storeSelection(type) {
bgneal@183 10980 bookmark = selection.getBookmark(type);
bgneal@183 10981 };
bgneal@183 10982
bgneal@183 10983 function restoreSelection() {
bgneal@183 10984 selection.moveToBookmark(bookmark);
bgneal@183 10985 };
bgneal@183 10986
bgneal@183 10987 // Add execCommand overrides
bgneal@183 10988 addCommands({
bgneal@183 10989 // Ignore these, added for compatibility
bgneal@183 10990 'mceResetDesignMode,mceBeginUndoLevel' : function() {},
bgneal@183 10991
bgneal@183 10992 // Add undo manager logic
bgneal@183 10993 'mceEndUndoLevel,mceAddUndoLevel' : function() {
bgneal@183 10994 editor.undoManager.add();
bgneal@183 10995 },
bgneal@183 10996
bgneal@183 10997 'Cut,Copy,Paste' : function(command) {
bgneal@183 10998 var doc = editor.getDoc(), failed;
bgneal@183 10999
bgneal@183 11000 // Try executing the native command
bgneal@183 11001 try {
bgneal@183 11002 execNativeCommand(command);
bgneal@183 11003 } catch (ex) {
bgneal@183 11004 // Command failed
bgneal@183 11005 failed = TRUE;
bgneal@183 11006 }
bgneal@183 11007
bgneal@183 11008 // Present alert message about clipboard access not being available
bgneal@217 11009 if (failed || !doc.queryCommandSupported(command)) {
bgneal@183 11010 if (tinymce.isGecko) {
bgneal@183 11011 editor.windowManager.confirm(editor.getLang('clipboard_msg'), function(state) {
bgneal@183 11012 if (state)
bgneal@183 11013 open('http://www.mozilla.org/editor/midasdemo/securityprefs.html', '_blank');
bgneal@183 11014 });
bgneal@183 11015 } else
bgneal@183 11016 editor.windowManager.alert(editor.getLang('clipboard_no_support'));
bgneal@183 11017 }
bgneal@183 11018 },
bgneal@183 11019
bgneal@183 11020 // Override unlink command
bgneal@183 11021 unlink : function(command) {
bgneal@183 11022 if (selection.isCollapsed())
bgneal@183 11023 selection.select(selection.getNode());
bgneal@183 11024
bgneal@183 11025 execNativeCommand(command);
bgneal@183 11026 selection.collapse(FALSE);
bgneal@183 11027 },
bgneal@183 11028
bgneal@183 11029 // Override justify commands to use the text formatter engine
bgneal@183 11030 'JustifyLeft,JustifyCenter,JustifyRight,JustifyFull' : function(command) {
bgneal@183 11031 var align = command.substring(7);
bgneal@183 11032
bgneal@183 11033 // Remove all other alignments first
bgneal@183 11034 each('left,center,right,full'.split(','), function(name) {
bgneal@183 11035 if (align != name)
bgneal@183 11036 editor.formatter.remove('align' + name);
bgneal@183 11037 });
bgneal@183 11038
bgneal@183 11039 toggleFormat('align' + align);
bgneal@183 11040 },
bgneal@183 11041
bgneal@183 11042 // Override list commands to fix WebKit bug
bgneal@183 11043 'InsertUnorderedList,InsertOrderedList' : function(command) {
bgneal@183 11044 var listElm, listParent;
bgneal@183 11045
bgneal@183 11046 execNativeCommand(command);
bgneal@183 11047
bgneal@183 11048 // WebKit produces lists within block elements so we need to split them
bgneal@183 11049 // we will replace the native list creation logic to custom logic later on
bgneal@183 11050 // TODO: Remove this when the list creation logic is removed
bgneal@183 11051 listElm = dom.getParent(selection.getNode(), 'ol,ul');
bgneal@183 11052 if (listElm) {
bgneal@183 11053 listParent = listElm.parentNode;
bgneal@183 11054
bgneal@183 11055 // If list is within a text block then split that block
bgneal@183 11056 if (/^(H[1-6]|P|ADDRESS|PRE)$/.test(listParent.nodeName)) {
bgneal@183 11057 storeSelection();
bgneal@183 11058 dom.split(listParent, listElm);
bgneal@183 11059 restoreSelection();
bgneal@183 11060 }
bgneal@183 11061 }
bgneal@183 11062 },
bgneal@183 11063
bgneal@183 11064 // Override commands to use the text formatter engine
bgneal@183 11065 'Bold,Italic,Underline,Strikethrough' : function(command) {
bgneal@183 11066 toggleFormat(command);
bgneal@183 11067 },
bgneal@183 11068
bgneal@183 11069 // Override commands to use the text formatter engine
bgneal@183 11070 'ForeColor,HiliteColor,FontName' : function(command, ui, value) {
bgneal@183 11071 toggleFormat(command, value);
bgneal@183 11072 },
bgneal@183 11073
bgneal@183 11074 FontSize : function(command, ui, value) {
bgneal@183 11075 var fontClasses, fontSizes;
bgneal@183 11076
bgneal@183 11077 // Convert font size 1-7 to styles
bgneal@183 11078 if (value >= 1 && value <= 7) {
bgneal@183 11079 fontSizes = tinymce.explode(settings.font_size_style_values);
bgneal@183 11080 fontClasses = tinymce.explode(settings.font_size_classes);
bgneal@183 11081
bgneal@183 11082 if (fontClasses)
bgneal@183 11083 value = fontClasses[value - 1] || value;
bgneal@183 11084 else
bgneal@183 11085 value = fontSizes[value - 1] || value;
bgneal@183 11086 }
bgneal@183 11087
bgneal@183 11088 toggleFormat(command, value);
bgneal@183 11089 },
bgneal@183 11090
bgneal@183 11091 RemoveFormat : function(command) {
bgneal@183 11092 editor.formatter.remove(command);
bgneal@183 11093 },
bgneal@183 11094
bgneal@183 11095 mceBlockQuote : function(command) {
bgneal@183 11096 toggleFormat('blockquote');
bgneal@183 11097 },
bgneal@183 11098
bgneal@183 11099 FormatBlock : function(command, ui, value) {
bgneal@183 11100 return toggleFormat(value);
bgneal@183 11101 },
bgneal@183 11102
bgneal@183 11103 mceCleanup : function() {
bgneal@183 11104 storeSelection();
bgneal@183 11105 editor.setContent(editor.getContent({cleanup : TRUE}), {cleanup : TRUE});
bgneal@183 11106 restoreSelection();
bgneal@183 11107 },
bgneal@183 11108
bgneal@183 11109 mceRemoveNode : function(command, ui, value) {
bgneal@183 11110 var node = value || selection.getNode();
bgneal@183 11111
bgneal@183 11112 // Make sure that the body node isn't removed
bgneal@217 11113 if (node != editor.getBody()) {
bgneal@183 11114 storeSelection();
bgneal@183 11115 editor.dom.remove(node, TRUE);
bgneal@183 11116 restoreSelection();
bgneal@183 11117 }
bgneal@183 11118 },
bgneal@183 11119
bgneal@183 11120 mceSelectNodeDepth : function(command, ui, value) {
bgneal@183 11121 var counter = 0;
bgneal@183 11122
bgneal@183 11123 dom.getParent(selection.getNode(), function(node) {
bgneal@183 11124 if (node.nodeType == 1 && counter++ == value) {
bgneal@183 11125 selection.select(node);
bgneal@183 11126 return FALSE;
bgneal@183 11127 }
bgneal@183 11128 }, editor.getBody());
bgneal@183 11129 },
bgneal@183 11130
bgneal@183 11131 mceSelectNode : function(command, ui, value) {
bgneal@183 11132 selection.select(value);
bgneal@183 11133 },
bgneal@183 11134
bgneal@183 11135 mceInsertContent : function(command, ui, value) {
bgneal@183 11136 selection.setContent(value);
bgneal@183 11137 },
bgneal@183 11138
bgneal@183 11139 mceInsertRawHTML : function(command, ui, value) {
bgneal@183 11140 selection.setContent('tiny_mce_marker');
bgneal@183 11141 editor.setContent(editor.getContent().replace(/tiny_mce_marker/g, value));
bgneal@183 11142 },
bgneal@183 11143
bgneal@183 11144 mceSetContent : function(command, ui, value) {
bgneal@183 11145 editor.setContent(value);
bgneal@183 11146 },
bgneal@183 11147
bgneal@183 11148 'Indent,Outdent' : function(command) {
bgneal@183 11149 var intentValue, indentUnit, value;
bgneal@183 11150
bgneal@183 11151 // Setup indent level
bgneal@183 11152 intentValue = settings.indentation;
bgneal@183 11153 indentUnit = /[a-z%]+$/i.exec(intentValue);
bgneal@183 11154 intentValue = parseInt(intentValue);
bgneal@183 11155
bgneal@183 11156 if (!queryCommandState('InsertUnorderedList') && !queryCommandState('InsertOrderedList')) {
bgneal@183 11157 each(selection.getSelectedBlocks(), function(element) {
bgneal@183 11158 if (command == 'outdent') {
bgneal@183 11159 value = Math.max(0, parseInt(element.style.paddingLeft || 0) - intentValue);
bgneal@183 11160 dom.setStyle(element, 'paddingLeft', value ? value + indentUnit : '');
bgneal@183 11161 } else
bgneal@183 11162 dom.setStyle(element, 'paddingLeft', (parseInt(element.style.paddingLeft || 0) + intentValue) + indentUnit);
bgneal@183 11163 });
bgneal@183 11164 } else
bgneal@183 11165 execNativeCommand(command);
bgneal@183 11166 },
bgneal@183 11167
bgneal@183 11168 mceRepaint : function() {
bgneal@183 11169 var bookmark;
bgneal@183 11170
bgneal@183 11171 if (tinymce.isGecko) {
bgneal@183 11172 try {
bgneal@183 11173 storeSelection(TRUE);
bgneal@183 11174
bgneal@183 11175 if (selection.getSel())
bgneal@183 11176 selection.getSel().selectAllChildren(editor.getBody());
bgneal@183 11177
bgneal@183 11178 selection.collapse(TRUE);
bgneal@183 11179 restoreSelection();
bgneal@183 11180 } catch (ex) {
bgneal@183 11181 // Ignore
bgneal@183 11182 }
bgneal@183 11183 }
bgneal@183 11184 },
bgneal@183 11185
bgneal@183 11186 mceToggleFormat : function(command, ui, value) {
bgneal@183 11187 editor.formatter.toggle(value);
bgneal@183 11188 },
bgneal@183 11189
bgneal@183 11190 InsertHorizontalRule : function() {
bgneal@183 11191 selection.setContent('<hr />');
bgneal@183 11192 },
bgneal@183 11193
bgneal@183 11194 mceToggleVisualAid : function() {
bgneal@183 11195 editor.hasVisual = !editor.hasVisual;
bgneal@183 11196 editor.addVisual();
bgneal@183 11197 },
bgneal@183 11198
bgneal@183 11199 mceReplaceContent : function(command, ui, value) {
bgneal@183 11200 selection.setContent(value.replace(/\{\$selection\}/g, selection.getContent({format : 'text'})));
bgneal@183 11201 },
bgneal@183 11202
bgneal@183 11203 mceInsertLink : function(command, ui, value) {
bgneal@183 11204 var link = dom.getParent(selection.getNode(), 'a');
bgneal@183 11205
bgneal@183 11206 if (tinymce.is(value, 'string'))
bgneal@183 11207 value = {href : value};
bgneal@183 11208
bgneal@183 11209 if (!link) {
bgneal@183 11210 execNativeCommand('CreateLink', FALSE, 'javascript:mctmp(0);');
bgneal@183 11211 each(dom.select('a[href=javascript:mctmp(0);]'), function(link) {
bgneal@183 11212 dom.setAttribs(link, value);
bgneal@183 11213 });
bgneal@183 11214 } else {
bgneal@183 11215 if (value.href)
bgneal@183 11216 dom.setAttribs(link, value);
bgneal@183 11217 else
bgneal@217 11218 editor.dom.remove(link, TRUE);
bgneal@217 11219 }
bgneal@217 11220 },
bgneal@217 11221
bgneal@217 11222 selectAll : function() {
bgneal@217 11223 var root = dom.getRoot();
bgneal@217 11224 var rng = dom.createRng();
bgneal@217 11225 rng.setStart(root, 0);
bgneal@217 11226 rng.setEnd(root, root.childNodes.length);
bgneal@217 11227 editor.selection.setRng(rng);
bgneal@183 11228 }
bgneal@183 11229 });
bgneal@183 11230
bgneal@183 11231 // Add queryCommandState overrides
bgneal@183 11232 addCommands({
bgneal@183 11233 // Override justify commands
bgneal@183 11234 'JustifyLeft,JustifyCenter,JustifyRight,JustifyFull' : function(command) {
bgneal@183 11235 return isFormatMatch('align' + command.substring(7));
bgneal@183 11236 },
bgneal@183 11237
bgneal@183 11238 'Bold,Italic,Underline,Strikethrough' : function(command) {
bgneal@183 11239 return isFormatMatch(command);
bgneal@183 11240 },
bgneal@183 11241
bgneal@183 11242 mceBlockQuote : function() {
bgneal@183 11243 return isFormatMatch('blockquote');
bgneal@183 11244 },
bgneal@183 11245
bgneal@183 11246 Outdent : function() {
bgneal@183 11247 var node;
bgneal@183 11248
bgneal@183 11249 if (settings.inline_styles) {
bgneal@183 11250 if ((node = dom.getParent(selection.getStart(), dom.isBlock)) && parseInt(node.style.paddingLeft) > 0)
bgneal@183 11251 return TRUE;
bgneal@183 11252
bgneal@183 11253 if ((node = dom.getParent(selection.getEnd(), dom.isBlock)) && parseInt(node.style.paddingLeft) > 0)
bgneal@183 11254 return TRUE;
bgneal@183 11255 }
bgneal@183 11256
bgneal@183 11257 return queryCommandState('InsertUnorderedList') || queryCommandState('InsertOrderedList') || (!settings.inline_styles && !!dom.getParent(selection.getNode(), 'BLOCKQUOTE'));
bgneal@183 11258 },
bgneal@183 11259
bgneal@183 11260 'InsertUnorderedList,InsertOrderedList' : function(command) {
bgneal@183 11261 return dom.getParent(selection.getNode(), command == 'insertunorderedlist' ? 'UL' : 'OL');
bgneal@183 11262 }
bgneal@183 11263 }, 'state');
bgneal@183 11264
bgneal@183 11265 // Add queryCommandValue overrides
bgneal@183 11266 addCommands({
bgneal@183 11267 'FontSize,FontName' : function(command) {
bgneal@183 11268 var value = 0, parent;
bgneal@183 11269
bgneal@183 11270 if (parent = dom.getParent(selection.getNode(), 'span')) {
bgneal@183 11271 if (command == 'fontsize')
bgneal@183 11272 value = parent.style.fontSize;
bgneal@183 11273 else
bgneal@183 11274 value = parent.style.fontFamily.replace(/, /g, ',').replace(/[\'\"]/g, '').toLowerCase();
bgneal@183 11275 }
bgneal@183 11276
bgneal@183 11277 return value;
bgneal@183 11278 }
bgneal@183 11279 }, 'value');
bgneal@183 11280
bgneal@183 11281 // Add undo manager logic
bgneal@183 11282 if (settings.custom_undo_redo) {
bgneal@183 11283 addCommands({
bgneal@183 11284 Undo : function() {
bgneal@183 11285 editor.undoManager.undo();
bgneal@183 11286 },
bgneal@183 11287
bgneal@183 11288 Redo : function() {
bgneal@183 11289 editor.undoManager.redo();
bgneal@183 11290 }
bgneal@183 11291 });
bgneal@183 11292 }
bgneal@183 11293 };
bgneal@45 11294 })(tinymce);
bgneal@45 11295 (function(tinymce) {
bgneal@183 11296 var Dispatcher = tinymce.util.Dispatcher;
bgneal@183 11297
bgneal@183 11298 tinymce.UndoManager = function(editor) {
bgneal@183 11299 var self, index = 0, data = [];
bgneal@183 11300
bgneal@183 11301 function getContent() {
bgneal@183 11302 return tinymce.trim(editor.getContent({format : 'raw', no_events : 1}));
bgneal@183 11303 };
bgneal@183 11304
bgneal@183 11305 return self = {
bgneal@183 11306 typing : 0,
bgneal@183 11307
bgneal@183 11308 onAdd : new Dispatcher(self),
bgneal@183 11309 onUndo : new Dispatcher(self),
bgneal@183 11310 onRedo : new Dispatcher(self),
bgneal@183 11311
bgneal@183 11312 add : function(level) {
bgneal@183 11313 var i, settings = editor.settings, lastLevel;
bgneal@183 11314
bgneal@183 11315 level = level || {};
bgneal@183 11316 level.content = getContent();
bgneal@183 11317
bgneal@183 11318 // Add undo level if needed
bgneal@183 11319 lastLevel = data[index];
bgneal@183 11320 if (lastLevel && lastLevel.content == level.content) {
bgneal@183 11321 if (index > 0 || data.length == 1)
bgneal@183 11322 return null;
bgneal@183 11323 }
bgneal@183 11324
bgneal@183 11325 // Time to compress
bgneal@183 11326 if (settings.custom_undo_redo_levels) {
bgneal@183 11327 if (data.length > settings.custom_undo_redo_levels) {
bgneal@183 11328 for (i = 0; i < data.length - 1; i++)
bgneal@183 11329 data[i] = data[i + 1];
bgneal@183 11330
bgneal@183 11331 data.length--;
bgneal@183 11332 index = data.length;
bgneal@183 11333 }
bgneal@183 11334 }
bgneal@183 11335
bgneal@183 11336 // Get a non intrusive normalized bookmark
bgneal@183 11337 level.bookmark = editor.selection.getBookmark(2, true);
bgneal@183 11338
bgneal@183 11339 // Crop array if needed
bgneal@183 11340 if (index < data.length - 1) {
bgneal@183 11341 // Treat first level as initial
bgneal@183 11342 if (index == 0)
bgneal@183 11343 data = [];
bgneal@183 11344 else
bgneal@183 11345 data.length = index + 1;
bgneal@183 11346 }
bgneal@183 11347
bgneal@183 11348 data.push(level);
bgneal@183 11349 index = data.length - 1;
bgneal@183 11350
bgneal@183 11351 self.onAdd.dispatch(self, level);
bgneal@183 11352 editor.isNotDirty = 0;
bgneal@183 11353
bgneal@183 11354 return level;
bgneal@183 11355 },
bgneal@183 11356
bgneal@183 11357 undo : function() {
bgneal@183 11358 var level, i;
bgneal@183 11359
bgneal@183 11360 if (self.typing) {
bgneal@183 11361 self.add();
bgneal@183 11362 self.typing = 0;
bgneal@183 11363 }
bgneal@183 11364
bgneal@183 11365 if (index > 0) {
bgneal@183 11366 level = data[--index];
bgneal@183 11367
bgneal@183 11368 editor.setContent(level.content, {format : 'raw'});
bgneal@183 11369 editor.selection.moveToBookmark(level.bookmark);
bgneal@183 11370
bgneal@183 11371 self.onUndo.dispatch(self, level);
bgneal@183 11372 }
bgneal@183 11373
bgneal@183 11374 return level;
bgneal@183 11375 },
bgneal@183 11376
bgneal@183 11377 redo : function() {
bgneal@183 11378 var level;
bgneal@183 11379
bgneal@183 11380 if (index < data.length - 1) {
bgneal@183 11381 level = data[++index];
bgneal@183 11382
bgneal@183 11383 editor.setContent(level.content, {format : 'raw'});
bgneal@183 11384 editor.selection.moveToBookmark(level.bookmark);
bgneal@183 11385
bgneal@183 11386 self.onRedo.dispatch(self, level);
bgneal@183 11387 }
bgneal@183 11388
bgneal@183 11389 return level;
bgneal@183 11390 },
bgneal@183 11391
bgneal@183 11392 clear : function() {
bgneal@183 11393 data = [];
bgneal@183 11394 index = self.typing = 0;
bgneal@183 11395 },
bgneal@183 11396
bgneal@183 11397 hasUndo : function() {
bgneal@183 11398 return index > 0 || self.typing;
bgneal@183 11399 },
bgneal@183 11400
bgneal@183 11401 hasRedo : function() {
bgneal@183 11402 return index < data.length - 1;
bgneal@183 11403 }
bgneal@183 11404 };
bgneal@183 11405 };
bgneal@45 11406 })(tinymce);
bgneal@183 11407
bgneal@45 11408 (function(tinymce) {
bgneal@45 11409 // Shorten names
bgneal@183 11410 var Event = tinymce.dom.Event,
bgneal@183 11411 isIE = tinymce.isIE,
bgneal@183 11412 isGecko = tinymce.isGecko,
bgneal@183 11413 isOpera = tinymce.isOpera,
bgneal@183 11414 each = tinymce.each,
bgneal@183 11415 extend = tinymce.extend,
bgneal@183 11416 TRUE = true,
bgneal@183 11417 FALSE = false;
bgneal@183 11418
bgneal@183 11419 // Checks if the selection/caret is at the end of the specified block element
bgneal@183 11420 function isAtEnd(rng, par) {
bgneal@183 11421 var rng2 = par.ownerDocument.createRange();
bgneal@183 11422
bgneal@183 11423 rng2.setStart(rng.endContainer, rng.endOffset);
bgneal@183 11424 rng2.setEndAfter(par);
bgneal@183 11425
bgneal@183 11426 // Get number of characters to the right of the cursor if it's zero then we are at the end and need to merge the next block element
bgneal@183 11427 return rng2.cloneContents().textContent.length == 0;
bgneal@183 11428 };
bgneal@183 11429
bgneal@183 11430 function isEmpty(n) {
bgneal@183 11431 n = n.innerHTML;
bgneal@183 11432
bgneal@183 11433 n = n.replace(/<(img|hr|table|input|select|textarea)[ \>]/gi, '-'); // Keep these convert them to - chars
bgneal@183 11434 n = n.replace(/<[^>]+>/g, ''); // Remove all tags
bgneal@183 11435
bgneal@183 11436 return n.replace(/[ \u00a0\t\r\n]+/g, '') == '';
bgneal@183 11437 };
bgneal@183 11438
bgneal@183 11439 function splitList(selection, dom, li) {
bgneal@183 11440 var listBlock, block;
bgneal@183 11441
bgneal@183 11442 if (isEmpty(li)) {
bgneal@183 11443 listBlock = dom.getParent(li, 'ul,ol');
bgneal@183 11444
bgneal@183 11445 if (!dom.getParent(listBlock.parentNode, 'ul,ol')) {
bgneal@183 11446 dom.split(listBlock, li);
bgneal@183 11447 block = dom.create('p', 0, '<br _mce_bogus="1" />');
bgneal@183 11448 dom.replace(block, li);
bgneal@183 11449 selection.select(block, 1);
bgneal@183 11450 }
bgneal@183 11451
bgneal@183 11452 return FALSE;
bgneal@183 11453 }
bgneal@183 11454
bgneal@183 11455 return TRUE;
bgneal@183 11456 };
bgneal@45 11457
bgneal@45 11458 tinymce.create('tinymce.ForceBlocks', {
bgneal@45 11459 ForceBlocks : function(ed) {
bgneal@45 11460 var t = this, s = ed.settings, elm;
bgneal@45 11461
bgneal@45 11462 t.editor = ed;
bgneal@45 11463 t.dom = ed.dom;
bgneal@45 11464 elm = (s.forced_root_block || 'p').toLowerCase();
bgneal@45 11465 s.element = elm.toUpperCase();
bgneal@45 11466
bgneal@45 11467 ed.onPreInit.add(t.setup, t);
bgneal@45 11468
bgneal@45 11469 t.reOpera = new RegExp('(\\u00a0|&#160;|&nbsp;)<\/' + elm + '>', 'gi');
bgneal@45 11470 t.rePadd = new RegExp('<p( )([^>]+)><\\\/p>|<p( )([^>]+)\\\/>|<p( )([^>]+)>\\s+<\\\/p>|<p><\\\/p>|<p\\\/>|<p>\\s+<\\\/p>'.replace(/p/g, elm), 'gi');
bgneal@45 11471 t.reNbsp2BR1 = new RegExp('<p( )([^>]+)>[\\s\\u00a0]+<\\\/p>|<p>[\\s\\u00a0]+<\\\/p>'.replace(/p/g, elm), 'gi');
bgneal@45 11472 t.reNbsp2BR2 = new RegExp('<%p()([^>]+)>(&nbsp;|&#160;)<\\\/%p>|<%p>(&nbsp;|&#160;)<\\\/%p>'.replace(/%p/g, elm), 'gi');
bgneal@45 11473 t.reBR2Nbsp = new RegExp('<p( )([^>]+)>\\s*<br \\\/>\\s*<\\\/p>|<p>\\s*<br \\\/>\\s*<\\\/p>'.replace(/p/g, elm), 'gi');
bgneal@45 11474
bgneal@45 11475 function padd(ed, o) {
bgneal@45 11476 if (isOpera)
bgneal@45 11477 o.content = o.content.replace(t.reOpera, '</' + elm + '>');
bgneal@45 11478
bgneal@45 11479 o.content = o.content.replace(t.rePadd, '<' + elm + '$1$2$3$4$5$6>\u00a0</' + elm + '>');
bgneal@45 11480
bgneal@45 11481 if (!isIE && !isOpera && o.set) {
bgneal@45 11482 // Use &nbsp; instead of BR in padded paragraphs
bgneal@45 11483 o.content = o.content.replace(t.reNbsp2BR1, '<' + elm + '$1$2><br /></' + elm + '>');
bgneal@45 11484 o.content = o.content.replace(t.reNbsp2BR2, '<' + elm + '$1$2><br /></' + elm + '>');
bgneal@183 11485 } else
bgneal@45 11486 o.content = o.content.replace(t.reBR2Nbsp, '<' + elm + '$1$2>\u00a0</' + elm + '>');
bgneal@45 11487 };
bgneal@45 11488
bgneal@45 11489 ed.onBeforeSetContent.add(padd);
bgneal@45 11490 ed.onPostProcess.add(padd);
bgneal@45 11491
bgneal@45 11492 if (s.forced_root_block) {
bgneal@45 11493 ed.onInit.add(t.forceRoots, t);
bgneal@45 11494 ed.onSetContent.add(t.forceRoots, t);
bgneal@45 11495 ed.onBeforeGetContent.add(t.forceRoots, t);
bgneal@45 11496 }
bgneal@45 11497 },
bgneal@45 11498
bgneal@45 11499 setup : function() {
bgneal@183 11500 var t = this, ed = t.editor, s = ed.settings, dom = ed.dom, selection = ed.selection;
bgneal@45 11501
bgneal@45 11502 // Force root blocks when typing and when getting output
bgneal@45 11503 if (s.forced_root_block) {
bgneal@183 11504 ed.onBeforeExecCommand.add(t.forceRoots, t);
bgneal@45 11505 ed.onKeyUp.add(t.forceRoots, t);
bgneal@45 11506 ed.onPreProcess.add(t.forceRoots, t);
bgneal@45 11507 }
bgneal@45 11508
bgneal@45 11509 if (s.force_br_newlines) {
bgneal@45 11510 // Force IE to produce BRs on enter
bgneal@45 11511 if (isIE) {
bgneal@45 11512 ed.onKeyPress.add(function(ed, e) {
bgneal@183 11513 var n;
bgneal@183 11514
bgneal@183 11515 if (e.keyCode == 13 && selection.getNode().nodeName != 'LI') {
bgneal@183 11516 selection.setContent('<br id="__" /> ', {format : 'raw'});
bgneal@183 11517 n = dom.get('__');
bgneal@45 11518 n.removeAttribute('id');
bgneal@183 11519 selection.select(n);
bgneal@183 11520 selection.collapse();
bgneal@45 11521 return Event.cancel(e);
bgneal@45 11522 }
bgneal@45 11523 });
bgneal@45 11524 }
bgneal@45 11525 }
bgneal@45 11526
bgneal@45 11527 if (!isIE && s.force_p_newlines) {
bgneal@45 11528 ed.onKeyPress.add(function(ed, e) {
bgneal@183 11529 if (e.keyCode == 13 && !e.shiftKey && !t.insertPara(e))
bgneal@183 11530 Event.cancel(e);
bgneal@45 11531 });
bgneal@45 11532
bgneal@45 11533 if (isGecko) {
bgneal@45 11534 ed.onKeyDown.add(function(ed, e) {
bgneal@45 11535 if ((e.keyCode == 8 || e.keyCode == 46) && !e.shiftKey)
bgneal@45 11536 t.backspaceDelete(e, e.keyCode == 8);
bgneal@45 11537 });
bgneal@45 11538 }
bgneal@45 11539 }
bgneal@45 11540
bgneal@183 11541 // Workaround for missing shift+enter support, http://bugs.webkit.org/show_bug.cgi?id=16973
bgneal@183 11542 if (tinymce.isWebKit) {
bgneal@183 11543 function insertBr(ed) {
bgneal@183 11544 var rng = selection.getRng(), br, div = dom.create('div', null, ' '), divYPos, vpHeight = dom.getViewPort(ed.getWin()).h;
bgneal@183 11545
bgneal@183 11546 // Insert BR element
bgneal@183 11547 rng.insertNode(br = dom.create('br'));
bgneal@183 11548
bgneal@183 11549 // Place caret after BR
bgneal@183 11550 rng.setStartAfter(br);
bgneal@183 11551 rng.setEndAfter(br);
bgneal@183 11552 selection.setRng(rng);
bgneal@183 11553
bgneal@183 11554 // Could not place caret after BR then insert an nbsp entity and move the caret
bgneal@183 11555 if (selection.getSel().focusNode == br.previousSibling) {
bgneal@183 11556 selection.select(dom.insertAfter(dom.doc.createTextNode('\u00a0'), br));
bgneal@183 11557 selection.collapse(TRUE);
bgneal@183 11558 }
bgneal@183 11559
bgneal@183 11560 // Create a temporary DIV after the BR and get the position as it
bgneal@183 11561 // seems like getPos() returns 0 for text nodes and BR elements.
bgneal@183 11562 dom.insertAfter(div, br);
bgneal@183 11563 divYPos = dom.getPos(div).y;
bgneal@183 11564 dom.remove(div);
bgneal@183 11565
bgneal@183 11566 // Scroll to new position, scrollIntoView can't be used due to bug: http://bugs.webkit.org/show_bug.cgi?id=16117
bgneal@183 11567 if (divYPos > vpHeight) // It is not necessary to scroll if the DIV is inside the view port.
bgneal@183 11568 ed.getWin().scrollTo(0, divYPos);
bgneal@183 11569 };
bgneal@183 11570
bgneal@45 11571 ed.onKeyPress.add(function(ed, e) {
bgneal@217 11572 if (e.keyCode == 13 && (e.shiftKey || (s.force_br_newlines && !dom.getParent(selection.getNode(), 'h1,h2,h3,h4,h5,h6,ol,ul')))) {
bgneal@183 11573 insertBr(ed);
bgneal@183 11574 Event.cancel(e);
bgneal@183 11575 }
bgneal@183 11576 });
bgneal@183 11577 }
bgneal@183 11578
bgneal@183 11579 // Padd empty inline elements within block elements
bgneal@183 11580 // For example: <p><strong><em></em></strong></p> becomes <p><strong><em>&nbsp;</em></strong></p>
bgneal@183 11581 ed.onPreProcess.add(function(ed, o) {
bgneal@183 11582 each(dom.select('p,h1,h2,h3,h4,h5,h6,div', o.node), function(p) {
bgneal@183 11583 if (isEmpty(p)) {
bgneal@183 11584 each(dom.select('span,em,strong,b,i', o.node), function(n) {
bgneal@183 11585 if (!n.hasChildNodes()) {
bgneal@183 11586 n.appendChild(ed.getDoc().createTextNode('\u00a0'));
bgneal@183 11587 return FALSE; // Break the loop one padding is enough
bgneal@183 11588 }
bgneal@183 11589 });
bgneal@183 11590 }
bgneal@183 11591 });
bgneal@183 11592 });
bgneal@183 11593
bgneal@183 11594 // IE specific fixes
bgneal@183 11595 if (isIE) {
bgneal@183 11596 // Replaces IE:s auto generated paragraphs with the specified element name
bgneal@183 11597 if (s.element != 'P') {
bgneal@183 11598 ed.onKeyPress.add(function(ed, e) {
bgneal@183 11599 t.lastElm = selection.getNode().nodeName;
bgneal@183 11600 });
bgneal@183 11601
bgneal@183 11602 ed.onKeyUp.add(function(ed, e) {
bgneal@183 11603 var bl, n = selection.getNode(), b = ed.getBody();
bgneal@183 11604
bgneal@183 11605 if (b.childNodes.length === 1 && n.nodeName == 'P') {
bgneal@183 11606 n = dom.rename(n, s.element);
bgneal@183 11607 selection.select(n);
bgneal@183 11608 selection.collapse();
bgneal@45 11609 ed.nodeChanged();
bgneal@183 11610 } else if (e.keyCode == 13 && !e.shiftKey && t.lastElm != 'P') {
bgneal@183 11611 bl = dom.getParent(n, 'p');
bgneal@183 11612
bgneal@183 11613 if (bl) {
bgneal@183 11614 dom.rename(bl, s.element);
bgneal@183 11615 ed.nodeChanged();
bgneal@183 11616 }
bgneal@183 11617 }
bgneal@183 11618 });
bgneal@183 11619 }
bgneal@45 11620 }
bgneal@45 11621 },
bgneal@45 11622
bgneal@45 11623 find : function(n, t, s) {
bgneal@183 11624 var ed = this.editor, w = ed.getDoc().createTreeWalker(n, 4, null, FALSE), c = -1;
bgneal@45 11625
bgneal@45 11626 while (n = w.nextNode()) {
bgneal@45 11627 c++;
bgneal@45 11628
bgneal@45 11629 // Index by node
bgneal@45 11630 if (t == 0 && n == s)
bgneal@45 11631 return c;
bgneal@45 11632
bgneal@45 11633 // Node by index
bgneal@45 11634 if (t == 1 && c == s)
bgneal@45 11635 return n;
bgneal@45 11636 }
bgneal@45 11637
bgneal@45 11638 return -1;
bgneal@45 11639 },
bgneal@45 11640
bgneal@45 11641 forceRoots : function(ed, e) {
bgneal@45 11642 var t = this, ed = t.editor, b = ed.getBody(), d = ed.getDoc(), se = ed.selection, s = se.getSel(), r = se.getRng(), si = -2, ei, so, eo, tr, c = -0xFFFFFF;
bgneal@45 11643 var nx, bl, bp, sp, le, nl = b.childNodes, i, n, eid;
bgneal@45 11644
bgneal@45 11645 // Fix for bug #1863847
bgneal@45 11646 //if (e && e.keyCode == 13)
bgneal@183 11647 // return TRUE;
bgneal@45 11648
bgneal@45 11649 // Wrap non blocks into blocks
bgneal@45 11650 for (i = nl.length - 1; i >= 0; i--) {
bgneal@45 11651 nx = nl[i];
bgneal@45 11652
bgneal@183 11653 // Ignore internal elements
bgneal@183 11654 if (nx.nodeType === 1 && nx.getAttribute('_mce_type')) {
bgneal@183 11655 bl = null;
bgneal@183 11656 continue;
bgneal@183 11657 }
bgneal@183 11658
bgneal@45 11659 // Is text or non block element
bgneal@183 11660 if (nx.nodeType === 3 || (!t.dom.isBlock(nx) && nx.nodeType !== 8 && !/^(script|mce:script|style|mce:style)$/i.test(nx.nodeName))) {
bgneal@45 11661 if (!bl) {
bgneal@45 11662 // Create new block but ignore whitespace
bgneal@45 11663 if (nx.nodeType != 3 || /[^\s]/g.test(nx.nodeValue)) {
bgneal@45 11664 // Store selection
bgneal@45 11665 if (si == -2 && r) {
bgneal@45 11666 if (!isIE) {
bgneal@45 11667 // If selection is element then mark it
bgneal@45 11668 if (r.startContainer.nodeType == 1 && (n = r.startContainer.childNodes[r.startOffset]) && n.nodeType == 1) {
bgneal@45 11669 // Save the id of the selected element
bgneal@45 11670 eid = n.getAttribute("id");
bgneal@45 11671 n.setAttribute("id", "__mce");
bgneal@45 11672 } else {
bgneal@45 11673 // If element is inside body, might not be the case in contentEdiable mode
bgneal@45 11674 if (ed.dom.getParent(r.startContainer, function(e) {return e === b;})) {
bgneal@45 11675 so = r.startOffset;
bgneal@45 11676 eo = r.endOffset;
bgneal@45 11677 si = t.find(b, 0, r.startContainer);
bgneal@45 11678 ei = t.find(b, 0, r.endContainer);
bgneal@45 11679 }
bgneal@45 11680 }
bgneal@45 11681 } else {
bgneal@217 11682 // Force control range into text range
bgneal@217 11683 if (r.item) {
bgneal@217 11684 tr = d.body.createTextRange();
bgneal@217 11685 tr.moveToElementText(r.item(0));
bgneal@217 11686 r = tr;
bgneal@217 11687 }
bgneal@217 11688
bgneal@45 11689 tr = d.body.createTextRange();
bgneal@45 11690 tr.moveToElementText(b);
bgneal@45 11691 tr.collapse(1);
bgneal@45 11692 bp = tr.move('character', c) * -1;
bgneal@45 11693
bgneal@45 11694 tr = r.duplicate();
bgneal@45 11695 tr.collapse(1);
bgneal@45 11696 sp = tr.move('character', c) * -1;
bgneal@45 11697
bgneal@45 11698 tr = r.duplicate();
bgneal@45 11699 tr.collapse(0);
bgneal@45 11700 le = (tr.move('character', c) * -1) - sp;
bgneal@45 11701
bgneal@45 11702 si = sp - bp;
bgneal@45 11703 ei = le;
bgneal@45 11704 }
bgneal@45 11705 }
bgneal@45 11706
bgneal@183 11707 // Uses replaceChild instead of cloneNode since it removes selected attribute from option elements on IE
bgneal@183 11708 // See: http://support.microsoft.com/kb/829907
bgneal@45 11709 bl = ed.dom.create(ed.settings.forced_root_block);
bgneal@45 11710 nx.parentNode.replaceChild(bl, nx);
bgneal@183 11711 bl.appendChild(nx);
bgneal@45 11712 }
bgneal@45 11713 } else {
bgneal@45 11714 if (bl.hasChildNodes())
bgneal@45 11715 bl.insertBefore(nx, bl.firstChild);
bgneal@45 11716 else
bgneal@45 11717 bl.appendChild(nx);
bgneal@45 11718 }
bgneal@45 11719 } else
bgneal@45 11720 bl = null; // Time to create new block
bgneal@45 11721 }
bgneal@45 11722
bgneal@45 11723 // Restore selection
bgneal@45 11724 if (si != -2) {
bgneal@45 11725 if (!isIE) {
bgneal@45 11726 bl = b.getElementsByTagName(ed.settings.element)[0];
bgneal@45 11727 r = d.createRange();
bgneal@45 11728
bgneal@45 11729 // Select last location or generated block
bgneal@45 11730 if (si != -1)
bgneal@45 11731 r.setStart(t.find(b, 1, si), so);
bgneal@45 11732 else
bgneal@45 11733 r.setStart(bl, 0);
bgneal@45 11734
bgneal@45 11735 // Select last location or generated block
bgneal@45 11736 if (ei != -1)
bgneal@45 11737 r.setEnd(t.find(b, 1, ei), eo);
bgneal@45 11738 else
bgneal@45 11739 r.setEnd(bl, 0);
bgneal@45 11740
bgneal@45 11741 if (s) {
bgneal@45 11742 s.removeAllRanges();
bgneal@45 11743 s.addRange(r);
bgneal@45 11744 }
bgneal@45 11745 } else {
bgneal@45 11746 try {
bgneal@45 11747 r = s.createRange();
bgneal@45 11748 r.moveToElementText(b);
bgneal@45 11749 r.collapse(1);
bgneal@45 11750 r.moveStart('character', si);
bgneal@45 11751 r.moveEnd('character', ei);
bgneal@45 11752 r.select();
bgneal@45 11753 } catch (ex) {
bgneal@45 11754 // Ignore
bgneal@45 11755 }
bgneal@45 11756 }
bgneal@45 11757 } else if (!isIE && (n = ed.dom.get('__mce'))) {
bgneal@45 11758 // Restore the id of the selected element
bgneal@45 11759 if (eid)
bgneal@45 11760 n.setAttribute('id', eid);
bgneal@45 11761 else
bgneal@45 11762 n.removeAttribute('id');
bgneal@45 11763
bgneal@45 11764 // Move caret before selected element
bgneal@45 11765 r = d.createRange();
bgneal@45 11766 r.setStartBefore(n);
bgneal@45 11767 r.setEndBefore(n);
bgneal@45 11768 se.setRng(r);
bgneal@45 11769 }
bgneal@45 11770 },
bgneal@45 11771
bgneal@45 11772 getParentBlock : function(n) {
bgneal@45 11773 var d = this.dom;
bgneal@45 11774
bgneal@45 11775 return d.getParent(n, d.isBlock);
bgneal@45 11776 },
bgneal@45 11777
bgneal@45 11778 insertPara : function(e) {
bgneal@45 11779 var t = this, ed = t.editor, dom = ed.dom, d = ed.getDoc(), se = ed.settings, s = ed.selection.getSel(), r = s.getRangeAt(0), b = d.body;
bgneal@45 11780 var rb, ra, dir, sn, so, en, eo, sb, eb, bn, bef, aft, sc, ec, n, vp = dom.getViewPort(ed.getWin()), y, ch, car;
bgneal@45 11781
bgneal@45 11782 // If root blocks are forced then use Operas default behavior since it's really good
bgneal@45 11783 // Removed due to bug: #1853816
bgneal@45 11784 // if (se.forced_root_block && isOpera)
bgneal@183 11785 // return TRUE;
bgneal@45 11786
bgneal@45 11787 // Setup before range
bgneal@45 11788 rb = d.createRange();
bgneal@45 11789
bgneal@45 11790 // If is before the first block element and in body, then move it into first block element
bgneal@45 11791 rb.setStart(s.anchorNode, s.anchorOffset);
bgneal@183 11792 rb.collapse(TRUE);
bgneal@45 11793
bgneal@45 11794 // Setup after range
bgneal@45 11795 ra = d.createRange();
bgneal@45 11796
bgneal@45 11797 // If is before the first block element and in body, then move it into first block element
bgneal@45 11798 ra.setStart(s.focusNode, s.focusOffset);
bgneal@183 11799 ra.collapse(TRUE);
bgneal@45 11800
bgneal@45 11801 // Setup start/end points
bgneal@45 11802 dir = rb.compareBoundaryPoints(rb.START_TO_END, ra) < 0;
bgneal@45 11803 sn = dir ? s.anchorNode : s.focusNode;
bgneal@45 11804 so = dir ? s.anchorOffset : s.focusOffset;
bgneal@45 11805 en = dir ? s.focusNode : s.anchorNode;
bgneal@45 11806 eo = dir ? s.focusOffset : s.anchorOffset;
bgneal@45 11807
bgneal@45 11808 // If selection is in empty table cell
bgneal@45 11809 if (sn === en && /^(TD|TH)$/.test(sn.nodeName)) {
bgneal@183 11810 if (sn.firstChild.nodeName == 'BR')
bgneal@183 11811 dom.remove(sn.firstChild); // Remove BR
bgneal@45 11812
bgneal@45 11813 // Create two new block elements
bgneal@183 11814 if (sn.childNodes.length == 0) {
bgneal@183 11815 ed.dom.add(sn, se.element, null, '<br />');
bgneal@183 11816 aft = ed.dom.add(sn, se.element, null, '<br />');
bgneal@183 11817 } else {
bgneal@183 11818 n = sn.innerHTML;
bgneal@183 11819 sn.innerHTML = '';
bgneal@183 11820 ed.dom.add(sn, se.element, null, n);
bgneal@183 11821 aft = ed.dom.add(sn, se.element, null, '<br />');
bgneal@183 11822 }
bgneal@45 11823
bgneal@45 11824 // Move caret into the last one
bgneal@45 11825 r = d.createRange();
bgneal@45 11826 r.selectNodeContents(aft);
bgneal@45 11827 r.collapse(1);
bgneal@45 11828 ed.selection.setRng(r);
bgneal@45 11829
bgneal@183 11830 return FALSE;
bgneal@45 11831 }
bgneal@45 11832
bgneal@45 11833 // If the caret is in an invalid location in FF we need to move it into the first block
bgneal@45 11834 if (sn == b && en == b && b.firstChild && ed.dom.isBlock(b.firstChild)) {
bgneal@45 11835 sn = en = sn.firstChild;
bgneal@45 11836 so = eo = 0;
bgneal@45 11837 rb = d.createRange();
bgneal@45 11838 rb.setStart(sn, 0);
bgneal@45 11839 ra = d.createRange();
bgneal@45 11840 ra.setStart(en, 0);
bgneal@45 11841 }
bgneal@45 11842
bgneal@45 11843 // Never use body as start or end node
bgneal@45 11844 sn = sn.nodeName == "HTML" ? d.body : sn; // Fix for Opera bug: https://bugs.opera.com/show_bug.cgi?id=273224&comments=yes
bgneal@45 11845 sn = sn.nodeName == "BODY" ? sn.firstChild : sn;
bgneal@45 11846 en = en.nodeName == "HTML" ? d.body : en; // Fix for Opera bug: https://bugs.opera.com/show_bug.cgi?id=273224&comments=yes
bgneal@45 11847 en = en.nodeName == "BODY" ? en.firstChild : en;
bgneal@45 11848
bgneal@45 11849 // Get start and end blocks
bgneal@45 11850 sb = t.getParentBlock(sn);
bgneal@45 11851 eb = t.getParentBlock(en);
bgneal@45 11852 bn = sb ? sb.nodeName : se.element; // Get block name to create
bgneal@45 11853
bgneal@45 11854 // Return inside list use default browser behavior
bgneal@183 11855 if (n = t.dom.getParent(sb, 'li,pre')) {
bgneal@183 11856 if (n.nodeName == 'LI')
bgneal@183 11857 return splitList(ed.selection, t.dom, n);
bgneal@183 11858
bgneal@183 11859 return TRUE;
bgneal@183 11860 }
bgneal@45 11861
bgneal@45 11862 // If caption or absolute layers then always generate new blocks within
bgneal@45 11863 if (sb && (sb.nodeName == 'CAPTION' || /absolute|relative|fixed/gi.test(dom.getStyle(sb, 'position', 1)))) {
bgneal@45 11864 bn = se.element;
bgneal@45 11865 sb = null;
bgneal@45 11866 }
bgneal@45 11867
bgneal@45 11868 // If caption or absolute layers then always generate new blocks within
bgneal@45 11869 if (eb && (eb.nodeName == 'CAPTION' || /absolute|relative|fixed/gi.test(dom.getStyle(sb, 'position', 1)))) {
bgneal@45 11870 bn = se.element;
bgneal@45 11871 eb = null;
bgneal@45 11872 }
bgneal@45 11873
bgneal@45 11874 // Use P instead
bgneal@45 11875 if (/(TD|TABLE|TH|CAPTION)/.test(bn) || (sb && bn == "DIV" && /left|right/gi.test(dom.getStyle(sb, 'float', 1)))) {
bgneal@45 11876 bn = se.element;
bgneal@45 11877 sb = eb = null;
bgneal@45 11878 }
bgneal@45 11879
bgneal@45 11880 // Setup new before and after blocks
bgneal@45 11881 bef = (sb && sb.nodeName == bn) ? sb.cloneNode(0) : ed.dom.create(bn);
bgneal@45 11882 aft = (eb && eb.nodeName == bn) ? eb.cloneNode(0) : ed.dom.create(bn);
bgneal@45 11883
bgneal@45 11884 // Remove id from after clone
bgneal@45 11885 aft.removeAttribute('id');
bgneal@45 11886
bgneal@45 11887 // Is header and cursor is at the end, then force paragraph under
bgneal@183 11888 if (/^(H[1-6])$/.test(bn) && isAtEnd(r, sb))
bgneal@45 11889 aft = ed.dom.create(se.element);
bgneal@45 11890
bgneal@45 11891 // Find start chop node
bgneal@45 11892 n = sc = sn;
bgneal@45 11893 do {
bgneal@45 11894 if (n == b || n.nodeType == 9 || t.dom.isBlock(n) || /(TD|TABLE|TH|CAPTION)/.test(n.nodeName))
bgneal@45 11895 break;
bgneal@45 11896
bgneal@45 11897 sc = n;
bgneal@45 11898 } while ((n = n.previousSibling ? n.previousSibling : n.parentNode));
bgneal@45 11899
bgneal@45 11900 // Find end chop node
bgneal@45 11901 n = ec = en;
bgneal@45 11902 do {
bgneal@45 11903 if (n == b || n.nodeType == 9 || t.dom.isBlock(n) || /(TD|TABLE|TH|CAPTION)/.test(n.nodeName))
bgneal@45 11904 break;
bgneal@45 11905
bgneal@45 11906 ec = n;
bgneal@45 11907 } while ((n = n.nextSibling ? n.nextSibling : n.parentNode));
bgneal@45 11908
bgneal@45 11909 // Place first chop part into before block element
bgneal@45 11910 if (sc.nodeName == bn)
bgneal@45 11911 rb.setStart(sc, 0);
bgneal@45 11912 else
bgneal@45 11913 rb.setStartBefore(sc);
bgneal@45 11914
bgneal@45 11915 rb.setEnd(sn, so);
bgneal@45 11916 bef.appendChild(rb.cloneContents() || d.createTextNode('')); // Empty text node needed for Safari
bgneal@45 11917
bgneal@45 11918 // Place secnd chop part within new block element
bgneal@45 11919 try {
bgneal@45 11920 ra.setEndAfter(ec);
bgneal@45 11921 } catch(ex) {
bgneal@45 11922 //console.debug(s.focusNode, s.focusOffset);
bgneal@45 11923 }
bgneal@45 11924
bgneal@45 11925 ra.setStart(en, eo);
bgneal@45 11926 aft.appendChild(ra.cloneContents() || d.createTextNode('')); // Empty text node needed for Safari
bgneal@45 11927
bgneal@45 11928 // Create range around everything
bgneal@45 11929 r = d.createRange();
bgneal@45 11930 if (!sc.previousSibling && sc.parentNode.nodeName == bn) {
bgneal@45 11931 r.setStartBefore(sc.parentNode);
bgneal@45 11932 } else {
bgneal@45 11933 if (rb.startContainer.nodeName == bn && rb.startOffset == 0)
bgneal@45 11934 r.setStartBefore(rb.startContainer);
bgneal@45 11935 else
bgneal@45 11936 r.setStart(rb.startContainer, rb.startOffset);
bgneal@45 11937 }
bgneal@45 11938
bgneal@45 11939 if (!ec.nextSibling && ec.parentNode.nodeName == bn)
bgneal@45 11940 r.setEndAfter(ec.parentNode);
bgneal@45 11941 else
bgneal@45 11942 r.setEnd(ra.endContainer, ra.endOffset);
bgneal@45 11943
bgneal@45 11944 // Delete and replace it with new block elements
bgneal@45 11945 r.deleteContents();
bgneal@45 11946
bgneal@45 11947 if (isOpera)
bgneal@45 11948 ed.getWin().scrollTo(0, vp.y);
bgneal@45 11949
bgneal@45 11950 // Never wrap blocks in blocks
bgneal@45 11951 if (bef.firstChild && bef.firstChild.nodeName == bn)
bgneal@45 11952 bef.innerHTML = bef.firstChild.innerHTML;
bgneal@45 11953
bgneal@45 11954 if (aft.firstChild && aft.firstChild.nodeName == bn)
bgneal@45 11955 aft.innerHTML = aft.firstChild.innerHTML;
bgneal@45 11956
bgneal@45 11957 // Padd empty blocks
bgneal@45 11958 if (isEmpty(bef))
bgneal@45 11959 bef.innerHTML = '<br />';
bgneal@45 11960
bgneal@45 11961 function appendStyles(e, en) {
bgneal@45 11962 var nl = [], nn, n, i;
bgneal@45 11963
bgneal@45 11964 e.innerHTML = '';
bgneal@45 11965
bgneal@45 11966 // Make clones of style elements
bgneal@45 11967 if (se.keep_styles) {
bgneal@45 11968 n = en;
bgneal@45 11969 do {
bgneal@45 11970 // We only want style specific elements
bgneal@45 11971 if (/^(SPAN|STRONG|B|EM|I|FONT|STRIKE|U)$/.test(n.nodeName)) {
bgneal@183 11972 nn = n.cloneNode(FALSE);
bgneal@45 11973 dom.setAttrib(nn, 'id', ''); // Remove ID since it needs to be unique
bgneal@45 11974 nl.push(nn);
bgneal@45 11975 }
bgneal@45 11976 } while (n = n.parentNode);
bgneal@45 11977 }
bgneal@45 11978
bgneal@45 11979 // Append style elements to aft
bgneal@45 11980 if (nl.length > 0) {
bgneal@45 11981 for (i = nl.length - 1, nn = e; i >= 0; i--)
bgneal@45 11982 nn = nn.appendChild(nl[i]);
bgneal@45 11983
bgneal@45 11984 // Padd most inner style element
bgneal@45 11985 nl[0].innerHTML = isOpera ? '&nbsp;' : '<br />'; // Extra space for Opera so that the caret can move there
bgneal@45 11986 return nl[0]; // Move caret to most inner element
bgneal@45 11987 } else
bgneal@45 11988 e.innerHTML = isOpera ? '&nbsp;' : '<br />'; // Extra space for Opera so that the caret can move there
bgneal@45 11989 };
bgneal@45 11990
bgneal@45 11991 // Fill empty afterblook with current style
bgneal@45 11992 if (isEmpty(aft))
bgneal@45 11993 car = appendStyles(aft, en);
bgneal@45 11994
bgneal@45 11995 // Opera needs this one backwards for older versions
bgneal@45 11996 if (isOpera && parseFloat(opera.version()) < 9.5) {
bgneal@45 11997 r.insertNode(bef);
bgneal@45 11998 r.insertNode(aft);
bgneal@45 11999 } else {
bgneal@45 12000 r.insertNode(aft);
bgneal@45 12001 r.insertNode(bef);
bgneal@45 12002 }
bgneal@45 12003
bgneal@45 12004 // Normalize
bgneal@45 12005 aft.normalize();
bgneal@45 12006 bef.normalize();
bgneal@45 12007
bgneal@45 12008 function first(n) {
bgneal@183 12009 return d.createTreeWalker(n, NodeFilter.SHOW_TEXT, null, FALSE).nextNode() || n;
bgneal@45 12010 };
bgneal@45 12011
bgneal@45 12012 // Move cursor and scroll into view
bgneal@45 12013 r = d.createRange();
bgneal@45 12014 r.selectNodeContents(isGecko ? first(car || aft) : car || aft);
bgneal@45 12015 r.collapse(1);
bgneal@45 12016 s.removeAllRanges();
bgneal@45 12017 s.addRange(r);
bgneal@45 12018
bgneal@45 12019 // scrollIntoView seems to scroll the parent window in most browsers now including FF 3.0b4 so it's time to stop using it and do it our selfs
bgneal@45 12020 y = ed.dom.getPos(aft).y;
bgneal@45 12021 ch = aft.clientHeight;
bgneal@45 12022
bgneal@45 12023 // Is element within viewport
bgneal@45 12024 if (y < vp.y || y + ch > vp.y + vp.h) {
bgneal@45 12025 ed.getWin().scrollTo(0, y < vp.y ? y : y - vp.h + 25); // Needs to be hardcoded to roughly one line of text if a huge text block is broken into two blocks
bgneal@45 12026 //console.debug('SCROLL!', 'vp.y: ' + vp.y, 'y' + y, 'vp.h' + vp.h, 'clientHeight' + aft.clientHeight, 'yyy: ' + (y < vp.y ? y : y - vp.h + aft.clientHeight));
bgneal@45 12027 }
bgneal@45 12028
bgneal@183 12029 return FALSE;
bgneal@45 12030 },
bgneal@45 12031
bgneal@45 12032 backspaceDelete : function(e, bs) {
bgneal@183 12033 var t = this, ed = t.editor, b = ed.getBody(), dom = ed.dom, n, se = ed.selection, r = se.getRng(), sc = r.startContainer, n, w, tn;
bgneal@45 12034
bgneal@45 12035 // The caret sometimes gets stuck in Gecko if you delete empty paragraphs
bgneal@45 12036 // This workaround removes the element by hand and moves the caret to the previous element
bgneal@45 12037 if (sc && ed.dom.isBlock(sc) && !/^(TD|TH)$/.test(sc.nodeName) && bs) {
bgneal@45 12038 if (sc.childNodes.length == 0 || (sc.childNodes.length == 1 && sc.firstChild.nodeName == 'BR')) {
bgneal@45 12039 // Find previous block element
bgneal@45 12040 n = sc;
bgneal@45 12041 while ((n = n.previousSibling) && !ed.dom.isBlock(n)) ;
bgneal@45 12042
bgneal@45 12043 if (n) {
bgneal@45 12044 if (sc != b.firstChild) {
bgneal@45 12045 // Find last text node
bgneal@183 12046 w = ed.dom.doc.createTreeWalker(n, NodeFilter.SHOW_TEXT, null, FALSE);
bgneal@45 12047 while (tn = w.nextNode())
bgneal@45 12048 n = tn;
bgneal@45 12049
bgneal@45 12050 // Place caret at the end of last text node
bgneal@45 12051 r = ed.getDoc().createRange();
bgneal@45 12052 r.setStart(n, n.nodeValue ? n.nodeValue.length : 0);
bgneal@45 12053 r.setEnd(n, n.nodeValue ? n.nodeValue.length : 0);
bgneal@45 12054 se.setRng(r);
bgneal@45 12055
bgneal@45 12056 // Remove the target container
bgneal@45 12057 ed.dom.remove(sc);
bgneal@45 12058 }
bgneal@45 12059
bgneal@45 12060 return Event.cancel(e);
bgneal@45 12061 }
bgneal@45 12062 }
bgneal@45 12063 }
bgneal@45 12064
bgneal@45 12065 // Gecko generates BR elements here and there, we don't like those so lets remove them
bgneal@45 12066 function handler(e) {
bgneal@45 12067 var pr;
bgneal@45 12068
bgneal@45 12069 e = e.target;
bgneal@45 12070
bgneal@45 12071 // A new BR was created in a block element, remove it
bgneal@45 12072 if (e && e.parentNode && e.nodeName == 'BR' && (n = t.getParentBlock(e))) {
bgneal@45 12073 pr = e.previousSibling;
bgneal@45 12074
bgneal@45 12075 Event.remove(b, 'DOMNodeInserted', handler);
bgneal@45 12076
bgneal@45 12077 // Is there whitespace at the end of the node before then we might need the pesky BR
bgneal@45 12078 // to place the caret at a correct location see bug: #2013943
bgneal@45 12079 if (pr && pr.nodeType == 3 && /\s+$/.test(pr.nodeValue))
bgneal@45 12080 return;
bgneal@45 12081
bgneal@45 12082 // Only remove BR elements that got inserted in the middle of the text
bgneal@45 12083 if (e.previousSibling || e.nextSibling)
bgneal@45 12084 ed.dom.remove(e);
bgneal@45 12085 }
bgneal@45 12086 };
bgneal@45 12087
bgneal@45 12088 // Listen for new nodes
bgneal@45 12089 Event._add(b, 'DOMNodeInserted', handler);
bgneal@45 12090
bgneal@45 12091 // Remove listener
bgneal@45 12092 window.setTimeout(function() {
bgneal@45 12093 Event._remove(b, 'DOMNodeInserted', handler);
bgneal@45 12094 }, 1);
bgneal@45 12095 }
bgneal@45 12096 });
bgneal@45 12097 })(tinymce);
bgneal@183 12098
bgneal@45 12099 (function(tinymce) {
bgneal@45 12100 // Shorten names
bgneal@45 12101 var DOM = tinymce.DOM, Event = tinymce.dom.Event, each = tinymce.each, extend = tinymce.extend;
bgneal@45 12102
bgneal@45 12103 tinymce.create('tinymce.ControlManager', {
bgneal@45 12104 ControlManager : function(ed, s) {
bgneal@45 12105 var t = this, i;
bgneal@45 12106
bgneal@45 12107 s = s || {};
bgneal@45 12108 t.editor = ed;
bgneal@45 12109 t.controls = {};
bgneal@45 12110 t.onAdd = new tinymce.util.Dispatcher(t);
bgneal@45 12111 t.onPostRender = new tinymce.util.Dispatcher(t);
bgneal@45 12112 t.prefix = s.prefix || ed.id + '_';
bgneal@45 12113 t._cls = {};
bgneal@45 12114
bgneal@45 12115 t.onPostRender.add(function() {
bgneal@45 12116 each(t.controls, function(c) {
bgneal@45 12117 c.postRender();
bgneal@45 12118 });
bgneal@45 12119 });
bgneal@45 12120 },
bgneal@45 12121
bgneal@45 12122 get : function(id) {
bgneal@45 12123 return this.controls[this.prefix + id] || this.controls[id];
bgneal@45 12124 },
bgneal@45 12125
bgneal@45 12126 setActive : function(id, s) {
bgneal@45 12127 var c = null;
bgneal@45 12128
bgneal@45 12129 if (c = this.get(id))
bgneal@45 12130 c.setActive(s);
bgneal@45 12131
bgneal@45 12132 return c;
bgneal@45 12133 },
bgneal@45 12134
bgneal@45 12135 setDisabled : function(id, s) {
bgneal@45 12136 var c = null;
bgneal@45 12137
bgneal@45 12138 if (c = this.get(id))
bgneal@45 12139 c.setDisabled(s);
bgneal@45 12140
bgneal@45 12141 return c;
bgneal@45 12142 },
bgneal@45 12143
bgneal@45 12144 add : function(c) {
bgneal@45 12145 var t = this;
bgneal@45 12146
bgneal@45 12147 if (c) {
bgneal@45 12148 t.controls[c.id] = c;
bgneal@45 12149 t.onAdd.dispatch(c, t);
bgneal@45 12150 }
bgneal@45 12151
bgneal@45 12152 return c;
bgneal@45 12153 },
bgneal@45 12154
bgneal@45 12155 createControl : function(n) {
bgneal@45 12156 var c, t = this, ed = t.editor;
bgneal@45 12157
bgneal@45 12158 each(ed.plugins, function(p) {
bgneal@45 12159 if (p.createControl) {
bgneal@45 12160 c = p.createControl(n, t);
bgneal@45 12161
bgneal@45 12162 if (c)
bgneal@45 12163 return false;
bgneal@45 12164 }
bgneal@45 12165 });
bgneal@45 12166
bgneal@45 12167 switch (n) {
bgneal@45 12168 case "|":
bgneal@45 12169 case "separator":
bgneal@45 12170 return t.createSeparator();
bgneal@45 12171 }
bgneal@45 12172
bgneal@45 12173 if (!c && ed.buttons && (c = ed.buttons[n]))
bgneal@45 12174 return t.createButton(n, c);
bgneal@45 12175
bgneal@45 12176 return t.add(c);
bgneal@45 12177 },
bgneal@45 12178
bgneal@45 12179 createDropMenu : function(id, s, cc) {
bgneal@45 12180 var t = this, ed = t.editor, c, bm, v, cls;
bgneal@45 12181
bgneal@45 12182 s = extend({
bgneal@45 12183 'class' : 'mceDropDown',
bgneal@45 12184 constrain : ed.settings.constrain_menus
bgneal@45 12185 }, s);
bgneal@45 12186
bgneal@45 12187 s['class'] = s['class'] + ' ' + ed.getParam('skin') + 'Skin';
bgneal@45 12188 if (v = ed.getParam('skin_variant'))
bgneal@45 12189 s['class'] += ' ' + ed.getParam('skin') + 'Skin' + v.substring(0, 1).toUpperCase() + v.substring(1);
bgneal@45 12190
bgneal@45 12191 id = t.prefix + id;
bgneal@45 12192 cls = cc || t._cls.dropmenu || tinymce.ui.DropMenu;
bgneal@45 12193 c = t.controls[id] = new cls(id, s);
bgneal@45 12194 c.onAddItem.add(function(c, o) {
bgneal@45 12195 var s = o.settings;
bgneal@45 12196
bgneal@45 12197 s.title = ed.getLang(s.title, s.title);
bgneal@45 12198
bgneal@45 12199 if (!s.onclick) {
bgneal@45 12200 s.onclick = function(v) {
bgneal@183 12201 if (s.cmd)
bgneal@183 12202 ed.execCommand(s.cmd, s.ui || false, s.value);
bgneal@45 12203 };
bgneal@45 12204 }
bgneal@45 12205 });
bgneal@45 12206
bgneal@45 12207 ed.onRemove.add(function() {
bgneal@45 12208 c.destroy();
bgneal@45 12209 });
bgneal@45 12210
bgneal@45 12211 // Fix for bug #1897785, #1898007
bgneal@45 12212 if (tinymce.isIE) {
bgneal@45 12213 c.onShowMenu.add(function() {
bgneal@183 12214 // IE 8 needs focus in order to store away a range with the current collapsed caret location
bgneal@183 12215 ed.focus();
bgneal@183 12216
bgneal@45 12217 bm = ed.selection.getBookmark(1);
bgneal@45 12218 });
bgneal@45 12219
bgneal@45 12220 c.onHideMenu.add(function() {
bgneal@45 12221 if (bm) {
bgneal@45 12222 ed.selection.moveToBookmark(bm);
bgneal@45 12223 bm = 0;
bgneal@45 12224 }
bgneal@45 12225 });
bgneal@45 12226 }
bgneal@45 12227
bgneal@45 12228 return t.add(c);
bgneal@45 12229 },
bgneal@45 12230
bgneal@45 12231 createListBox : function(id, s, cc) {
bgneal@45 12232 var t = this, ed = t.editor, cmd, c, cls;
bgneal@45 12233
bgneal@45 12234 if (t.get(id))
bgneal@45 12235 return null;
bgneal@45 12236
bgneal@45 12237 s.title = ed.translate(s.title);
bgneal@45 12238 s.scope = s.scope || ed;
bgneal@45 12239
bgneal@45 12240 if (!s.onselect) {
bgneal@45 12241 s.onselect = function(v) {
bgneal@45 12242 ed.execCommand(s.cmd, s.ui || false, v || s.value);
bgneal@45 12243 };
bgneal@45 12244 }
bgneal@45 12245
bgneal@45 12246 s = extend({
bgneal@45 12247 title : s.title,
bgneal@45 12248 'class' : 'mce_' + id,
bgneal@45 12249 scope : s.scope,
bgneal@45 12250 control_manager : t
bgneal@45 12251 }, s);
bgneal@45 12252
bgneal@45 12253 id = t.prefix + id;
bgneal@45 12254
bgneal@45 12255 if (ed.settings.use_native_selects)
bgneal@45 12256 c = new tinymce.ui.NativeListBox(id, s);
bgneal@45 12257 else {
bgneal@45 12258 cls = cc || t._cls.listbox || tinymce.ui.ListBox;
bgneal@45 12259 c = new cls(id, s);
bgneal@45 12260 }
bgneal@45 12261
bgneal@45 12262 t.controls[id] = c;
bgneal@45 12263
bgneal@45 12264 // Fix focus problem in Safari
bgneal@45 12265 if (tinymce.isWebKit) {
bgneal@45 12266 c.onPostRender.add(function(c, n) {
bgneal@45 12267 // Store bookmark on mousedown
bgneal@45 12268 Event.add(n, 'mousedown', function() {
bgneal@183 12269 ed.bookmark = ed.selection.getBookmark(1);
bgneal@45 12270 });
bgneal@45 12271
bgneal@45 12272 // Restore on focus, since it might be lost
bgneal@45 12273 Event.add(n, 'focus', function() {
bgneal@45 12274 ed.selection.moveToBookmark(ed.bookmark);
bgneal@45 12275 ed.bookmark = null;
bgneal@45 12276 });
bgneal@45 12277 });
bgneal@45 12278 }
bgneal@45 12279
bgneal@45 12280 if (c.hideMenu)
bgneal@45 12281 ed.onMouseDown.add(c.hideMenu, c);
bgneal@45 12282
bgneal@45 12283 return t.add(c);
bgneal@45 12284 },
bgneal@45 12285
bgneal@45 12286 createButton : function(id, s, cc) {
bgneal@45 12287 var t = this, ed = t.editor, o, c, cls;
bgneal@45 12288
bgneal@45 12289 if (t.get(id))
bgneal@45 12290 return null;
bgneal@45 12291
bgneal@45 12292 s.title = ed.translate(s.title);
bgneal@45 12293 s.label = ed.translate(s.label);
bgneal@45 12294 s.scope = s.scope || ed;
bgneal@45 12295
bgneal@45 12296 if (!s.onclick && !s.menu_button) {
bgneal@45 12297 s.onclick = function() {
bgneal@45 12298 ed.execCommand(s.cmd, s.ui || false, s.value);
bgneal@45 12299 };
bgneal@45 12300 }
bgneal@45 12301
bgneal@45 12302 s = extend({
bgneal@45 12303 title : s.title,
bgneal@45 12304 'class' : 'mce_' + id,
bgneal@45 12305 unavailable_prefix : ed.getLang('unavailable', ''),
bgneal@45 12306 scope : s.scope,
bgneal@45 12307 control_manager : t
bgneal@45 12308 }, s);
bgneal@45 12309
bgneal@45 12310 id = t.prefix + id;
bgneal@45 12311
bgneal@45 12312 if (s.menu_button) {
bgneal@45 12313 cls = cc || t._cls.menubutton || tinymce.ui.MenuButton;
bgneal@45 12314 c = new cls(id, s);
bgneal@45 12315 ed.onMouseDown.add(c.hideMenu, c);
bgneal@45 12316 } else {
bgneal@45 12317 cls = t._cls.button || tinymce.ui.Button;
bgneal@45 12318 c = new cls(id, s);
bgneal@45 12319 }
bgneal@45 12320
bgneal@45 12321 return t.add(c);
bgneal@45 12322 },
bgneal@45 12323
bgneal@45 12324 createMenuButton : function(id, s, cc) {
bgneal@45 12325 s = s || {};
bgneal@45 12326 s.menu_button = 1;
bgneal@45 12327
bgneal@45 12328 return this.createButton(id, s, cc);
bgneal@45 12329 },
bgneal@45 12330
bgneal@45 12331 createSplitButton : function(id, s, cc) {
bgneal@45 12332 var t = this, ed = t.editor, cmd, c, cls;
bgneal@45 12333
bgneal@45 12334 if (t.get(id))
bgneal@45 12335 return null;
bgneal@45 12336
bgneal@45 12337 s.title = ed.translate(s.title);
bgneal@45 12338 s.scope = s.scope || ed;
bgneal@45 12339
bgneal@45 12340 if (!s.onclick) {
bgneal@45 12341 s.onclick = function(v) {
bgneal@45 12342 ed.execCommand(s.cmd, s.ui || false, v || s.value);
bgneal@45 12343 };
bgneal@45 12344 }
bgneal@45 12345
bgneal@45 12346 if (!s.onselect) {
bgneal@45 12347 s.onselect = function(v) {
bgneal@45 12348 ed.execCommand(s.cmd, s.ui || false, v || s.value);
bgneal@45 12349 };
bgneal@45 12350 }
bgneal@45 12351
bgneal@45 12352 s = extend({
bgneal@45 12353 title : s.title,
bgneal@45 12354 'class' : 'mce_' + id,
bgneal@45 12355 scope : s.scope,
bgneal@45 12356 control_manager : t
bgneal@45 12357 }, s);
bgneal@45 12358
bgneal@45 12359 id = t.prefix + id;
bgneal@45 12360 cls = cc || t._cls.splitbutton || tinymce.ui.SplitButton;
bgneal@45 12361 c = t.add(new cls(id, s));
bgneal@45 12362 ed.onMouseDown.add(c.hideMenu, c);
bgneal@45 12363
bgneal@45 12364 return c;
bgneal@45 12365 },
bgneal@45 12366
bgneal@45 12367 createColorSplitButton : function(id, s, cc) {
bgneal@45 12368 var t = this, ed = t.editor, cmd, c, cls, bm;
bgneal@45 12369
bgneal@45 12370 if (t.get(id))
bgneal@45 12371 return null;
bgneal@45 12372
bgneal@45 12373 s.title = ed.translate(s.title);
bgneal@45 12374 s.scope = s.scope || ed;
bgneal@45 12375
bgneal@45 12376 if (!s.onclick) {
bgneal@45 12377 s.onclick = function(v) {
bgneal@45 12378 if (tinymce.isIE)
bgneal@45 12379 bm = ed.selection.getBookmark(1);
bgneal@183 12380
bgneal@45 12381 ed.execCommand(s.cmd, s.ui || false, v || s.value);
bgneal@45 12382 };
bgneal@45 12383 }
bgneal@45 12384
bgneal@45 12385 if (!s.onselect) {
bgneal@45 12386 s.onselect = function(v) {
bgneal@45 12387 ed.execCommand(s.cmd, s.ui || false, v || s.value);
bgneal@45 12388 };
bgneal@45 12389 }
bgneal@45 12390
bgneal@45 12391 s = extend({
bgneal@45 12392 title : s.title,
bgneal@45 12393 'class' : 'mce_' + id,
bgneal@45 12394 'menu_class' : ed.getParam('skin') + 'Skin',
bgneal@45 12395 scope : s.scope,
bgneal@45 12396 more_colors_title : ed.getLang('more_colors')
bgneal@45 12397 }, s);
bgneal@45 12398
bgneal@45 12399 id = t.prefix + id;
bgneal@45 12400 cls = cc || t._cls.colorsplitbutton || tinymce.ui.ColorSplitButton;
bgneal@45 12401 c = new cls(id, s);
bgneal@45 12402 ed.onMouseDown.add(c.hideMenu, c);
bgneal@45 12403
bgneal@45 12404 // Remove the menu element when the editor is removed
bgneal@45 12405 ed.onRemove.add(function() {
bgneal@45 12406 c.destroy();
bgneal@45 12407 });
bgneal@45 12408
bgneal@45 12409 // Fix for bug #1897785, #1898007
bgneal@45 12410 if (tinymce.isIE) {
bgneal@183 12411 c.onShowMenu.add(function() {
bgneal@183 12412 // IE 8 needs focus in order to store away a range with the current collapsed caret location
bgneal@183 12413 ed.focus();
bgneal@183 12414 bm = ed.selection.getBookmark(1);
bgneal@183 12415 });
bgneal@183 12416
bgneal@45 12417 c.onHideMenu.add(function() {
bgneal@45 12418 if (bm) {
bgneal@45 12419 ed.selection.moveToBookmark(bm);
bgneal@45 12420 bm = 0;
bgneal@45 12421 }
bgneal@45 12422 });
bgneal@45 12423 }
bgneal@45 12424
bgneal@45 12425 return t.add(c);
bgneal@45 12426 },
bgneal@45 12427
bgneal@45 12428 createToolbar : function(id, s, cc) {
bgneal@45 12429 var c, t = this, cls;
bgneal@45 12430
bgneal@45 12431 id = t.prefix + id;
bgneal@45 12432 cls = cc || t._cls.toolbar || tinymce.ui.Toolbar;
bgneal@45 12433 c = new cls(id, s);
bgneal@45 12434
bgneal@45 12435 if (t.get(id))
bgneal@45 12436 return null;
bgneal@45 12437
bgneal@45 12438 return t.add(c);
bgneal@45 12439 },
bgneal@45 12440
bgneal@45 12441 createSeparator : function(cc) {
bgneal@45 12442 var cls = cc || this._cls.separator || tinymce.ui.Separator;
bgneal@45 12443
bgneal@45 12444 return new cls();
bgneal@45 12445 },
bgneal@45 12446
bgneal@45 12447 setControlType : function(n, c) {
bgneal@45 12448 return this._cls[n.toLowerCase()] = c;
bgneal@45 12449 },
bgneal@183 12450
bgneal@45 12451 destroy : function() {
bgneal@45 12452 each(this.controls, function(c) {
bgneal@45 12453 c.destroy();
bgneal@45 12454 });
bgneal@45 12455
bgneal@45 12456 this.controls = null;
bgneal@45 12457 }
bgneal@183 12458 });
bgneal@45 12459 })(tinymce);
bgneal@183 12460
bgneal@45 12461 (function(tinymce) {
bgneal@45 12462 var Dispatcher = tinymce.util.Dispatcher, each = tinymce.each, isIE = tinymce.isIE, isOpera = tinymce.isOpera;
bgneal@45 12463
bgneal@45 12464 tinymce.create('tinymce.WindowManager', {
bgneal@45 12465 WindowManager : function(ed) {
bgneal@45 12466 var t = this;
bgneal@45 12467
bgneal@45 12468 t.editor = ed;
bgneal@45 12469 t.onOpen = new Dispatcher(t);
bgneal@45 12470 t.onClose = new Dispatcher(t);
bgneal@45 12471 t.params = {};
bgneal@45 12472 t.features = {};
bgneal@45 12473 },
bgneal@45 12474
bgneal@45 12475 open : function(s, p) {
bgneal@45 12476 var t = this, f = '', x, y, mo = t.editor.settings.dialog_type == 'modal', w, sw, sh, vp = tinymce.DOM.getViewPort(), u;
bgneal@45 12477
bgneal@45 12478 // Default some options
bgneal@45 12479 s = s || {};
bgneal@45 12480 p = p || {};
bgneal@45 12481 sw = isOpera ? vp.w : screen.width; // Opera uses windows inside the Opera window
bgneal@45 12482 sh = isOpera ? vp.h : screen.height;
bgneal@45 12483 s.name = s.name || 'mc_' + new Date().getTime();
bgneal@45 12484 s.width = parseInt(s.width || 320);
bgneal@45 12485 s.height = parseInt(s.height || 240);
bgneal@45 12486 s.resizable = true;
bgneal@45 12487 s.left = s.left || parseInt(sw / 2.0) - (s.width / 2.0);
bgneal@45 12488 s.top = s.top || parseInt(sh / 2.0) - (s.height / 2.0);
bgneal@45 12489 p.inline = false;
bgneal@45 12490 p.mce_width = s.width;
bgneal@45 12491 p.mce_height = s.height;
bgneal@45 12492 p.mce_auto_focus = s.auto_focus;
bgneal@45 12493
bgneal@45 12494 if (mo) {
bgneal@45 12495 if (isIE) {
bgneal@45 12496 s.center = true;
bgneal@45 12497 s.help = false;
bgneal@45 12498 s.dialogWidth = s.width + 'px';
bgneal@45 12499 s.dialogHeight = s.height + 'px';
bgneal@45 12500 s.scroll = s.scrollbars || false;
bgneal@45 12501 }
bgneal@45 12502 }
bgneal@45 12503
bgneal@45 12504 // Build features string
bgneal@45 12505 each(s, function(v, k) {
bgneal@45 12506 if (tinymce.is(v, 'boolean'))
bgneal@45 12507 v = v ? 'yes' : 'no';
bgneal@45 12508
bgneal@45 12509 if (!/^(name|url)$/.test(k)) {
bgneal@45 12510 if (isIE && mo)
bgneal@45 12511 f += (f ? ';' : '') + k + ':' + v;
bgneal@45 12512 else
bgneal@45 12513 f += (f ? ',' : '') + k + '=' + v;
bgneal@45 12514 }
bgneal@45 12515 });
bgneal@45 12516
bgneal@45 12517 t.features = s;
bgneal@45 12518 t.params = p;
bgneal@45 12519 t.onOpen.dispatch(t, s, p);
bgneal@45 12520
bgneal@45 12521 u = s.url || s.file;
bgneal@45 12522 u = tinymce._addVer(u);
bgneal@45 12523
bgneal@45 12524 try {
bgneal@45 12525 if (isIE && mo) {
bgneal@45 12526 w = 1;
bgneal@45 12527 window.showModalDialog(u, window, f);
bgneal@45 12528 } else
bgneal@45 12529 w = window.open(u, s.name, f);
bgneal@45 12530 } catch (ex) {
bgneal@45 12531 // Ignore
bgneal@45 12532 }
bgneal@45 12533
bgneal@45 12534 if (!w)
bgneal@45 12535 alert(t.editor.getLang('popup_blocked'));
bgneal@45 12536 },
bgneal@45 12537
bgneal@45 12538 close : function(w) {
bgneal@45 12539 w.close();
bgneal@45 12540 this.onClose.dispatch(this);
bgneal@45 12541 },
bgneal@45 12542
bgneal@45 12543 createInstance : function(cl, a, b, c, d, e) {
bgneal@45 12544 var f = tinymce.resolve(cl);
bgneal@45 12545
bgneal@45 12546 return new f(a, b, c, d, e);
bgneal@45 12547 },
bgneal@45 12548
bgneal@45 12549 confirm : function(t, cb, s, w) {
bgneal@45 12550 w = w || window;
bgneal@45 12551
bgneal@45 12552 cb.call(s || this, w.confirm(this._decode(this.editor.getLang(t, t))));
bgneal@45 12553 },
bgneal@45 12554
bgneal@45 12555 alert : function(tx, cb, s, w) {
bgneal@45 12556 var t = this;
bgneal@45 12557
bgneal@45 12558 w = w || window;
bgneal@45 12559 w.alert(t._decode(t.editor.getLang(tx, tx)));
bgneal@45 12560
bgneal@45 12561 if (cb)
bgneal@45 12562 cb.call(s || t);
bgneal@45 12563 },
bgneal@45 12564
bgneal@183 12565 resizeBy : function(dw, dh, win) {
bgneal@183 12566 win.resizeBy(dw, dh);
bgneal@183 12567 },
bgneal@183 12568
bgneal@45 12569 // Internal functions
bgneal@45 12570
bgneal@45 12571 _decode : function(s) {
bgneal@45 12572 return tinymce.DOM.decode(s).replace(/\\n/g, '\n');
bgneal@45 12573 }
bgneal@183 12574 });
bgneal@183 12575 }(tinymce));
bgneal@183 12576 (function(tinymce) {
bgneal@183 12577 function CommandManager() {
bgneal@45 12578 var execCommands = {}, queryStateCommands = {}, queryValueCommands = {};
bgneal@45 12579
bgneal@45 12580 function add(collection, cmd, func, scope) {
bgneal@45 12581 if (typeof(cmd) == 'string')
bgneal@45 12582 cmd = [cmd];
bgneal@45 12583
bgneal@45 12584 tinymce.each(cmd, function(cmd) {
bgneal@45 12585 collection[cmd.toLowerCase()] = {func : func, scope : scope};
bgneal@45 12586 });
bgneal@45 12587 };
bgneal@45 12588
bgneal@45 12589 tinymce.extend(this, {
bgneal@45 12590 add : function(cmd, func, scope) {
bgneal@45 12591 add(execCommands, cmd, func, scope);
bgneal@45 12592 },
bgneal@45 12593
bgneal@45 12594 addQueryStateHandler : function(cmd, func, scope) {
bgneal@45 12595 add(queryStateCommands, cmd, func, scope);
bgneal@45 12596 },
bgneal@45 12597
bgneal@45 12598 addQueryValueHandler : function(cmd, func, scope) {
bgneal@45 12599 add(queryValueCommands, cmd, func, scope);
bgneal@45 12600 },
bgneal@45 12601
bgneal@45 12602 execCommand : function(scope, cmd, ui, value, args) {
bgneal@45 12603 if (cmd = execCommands[cmd.toLowerCase()]) {
bgneal@45 12604 if (cmd.func.call(scope || cmd.scope, ui, value, args) !== false)
bgneal@45 12605 return true;
bgneal@45 12606 }
bgneal@45 12607 },
bgneal@45 12608
bgneal@45 12609 queryCommandValue : function() {
bgneal@45 12610 if (cmd = queryValueCommands[cmd.toLowerCase()])
bgneal@45 12611 return cmd.func.call(scope || cmd.scope, ui, value, args);
bgneal@45 12612 },
bgneal@45 12613
bgneal@45 12614 queryCommandState : function() {
bgneal@45 12615 if (cmd = queryStateCommands[cmd.toLowerCase()])
bgneal@45 12616 return cmd.func.call(scope || cmd.scope, ui, value, args);
bgneal@45 12617 }
bgneal@45 12618 });
bgneal@45 12619 };
bgneal@45 12620
bgneal@183 12621 tinymce.GlobalCommands = new CommandManager();
bgneal@45 12622 })(tinymce);
bgneal@45 12623 (function(tinymce) {
bgneal@183 12624 tinymce.Formatter = function(ed) {
bgneal@183 12625 var formats = {},
bgneal@183 12626 each = tinymce.each,
bgneal@183 12627 dom = ed.dom,
bgneal@183 12628 selection = ed.selection,
bgneal@183 12629 TreeWalker = tinymce.dom.TreeWalker,
bgneal@183 12630 rangeUtils = new tinymce.dom.RangeUtils(dom),
bgneal@183 12631 isValid = ed.schema.isValid,
bgneal@183 12632 isBlock = dom.isBlock,
bgneal@183 12633 forcedRootBlock = ed.settings.forced_root_block,
bgneal@183 12634 nodeIndex = dom.nodeIndex,
bgneal@183 12635 INVISIBLE_CHAR = '\uFEFF',
bgneal@183 12636 MCE_ATTR_RE = /^(src|href|style)$/,
bgneal@183 12637 FALSE = false,
bgneal@183 12638 TRUE = true,
bgneal@183 12639 undefined,
bgneal@183 12640 pendingFormats = {apply : [], remove : []};
bgneal@183 12641
bgneal@183 12642 function isArray(obj) {
bgneal@183 12643 return obj instanceof Array;
bgneal@183 12644 };
bgneal@183 12645
bgneal@183 12646 function getParents(node, selector) {
bgneal@183 12647 return dom.getParents(node, selector, dom.getRoot());
bgneal@183 12648 };
bgneal@183 12649
bgneal@183 12650 function isCaretNode(node) {
bgneal@183 12651 return node.nodeType === 1 && (node.face === 'mceinline' || node.style.fontFamily === 'mceinline');
bgneal@183 12652 };
bgneal@183 12653
bgneal@183 12654 // Public functions
bgneal@183 12655
bgneal@183 12656 function get(name) {
bgneal@183 12657 return name ? formats[name] : formats;
bgneal@183 12658 };
bgneal@183 12659
bgneal@183 12660 function register(name, format) {
bgneal@183 12661 if (name) {
bgneal@183 12662 if (typeof(name) !== 'string') {
bgneal@183 12663 each(name, function(format, name) {
bgneal@183 12664 register(name, format);
bgneal@183 12665 });
bgneal@45 12666 } else {
bgneal@183 12667 // Force format into array and add it to internal collection
bgneal@183 12668 format = format.length ? format : [format];
bgneal@183 12669
bgneal@183 12670 each(format, function(format) {
bgneal@183 12671 // Set deep to false by default on selector formats this to avoid removing
bgneal@183 12672 // alignment on images inside paragraphs when alignment is changed on paragraphs
bgneal@183 12673 if (format.deep === undefined)
bgneal@183 12674 format.deep = !format.selector;
bgneal@183 12675
bgneal@183 12676 // Default to true
bgneal@183 12677 if (format.split === undefined)
bgneal@183 12678 format.split = !format.selector || format.inline;
bgneal@183 12679
bgneal@183 12680 // Default to true
bgneal@183 12681 if (format.remove === undefined && format.selector && !format.inline)
bgneal@183 12682 format.remove = 'none';
bgneal@183 12683
bgneal@183 12684 // Mark format as a mixed format inline + block level
bgneal@183 12685 if (format.selector && format.inline) {
bgneal@183 12686 format.mixed = true;
bgneal@183 12687 format.block_expand = true;
bgneal@183 12688 }
bgneal@183 12689
bgneal@183 12690 // Split classes if needed
bgneal@183 12691 if (typeof(format.classes) === 'string')
bgneal@183 12692 format.classes = format.classes.split(/\s+/);
bgneal@183 12693 });
bgneal@183 12694
bgneal@183 12695 formats[name] = format;
bgneal@183 12696 }
bgneal@183 12697 }
bgneal@183 12698 };
bgneal@183 12699
bgneal@183 12700 function apply(name, vars, node) {
bgneal@183 12701 var formatList = get(name), format = formatList[0], bookmark, rng, i;
bgneal@183 12702
bgneal@183 12703 function moveStart(rng) {
bgneal@183 12704 var container = rng.startContainer,
bgneal@183 12705 offset = rng.startOffset,
bgneal@183 12706 walker, node;
bgneal@183 12707
bgneal@183 12708 // Move startContainer/startOffset in to a suitable node
bgneal@183 12709 if (container.nodeType == 1 || container.nodeValue === "") {
bgneal@217 12710 container = container.nodeType == 1 ? container.childNodes[offset] : container;
bgneal@217 12711 walker = new TreeWalker(container, container.parentNode);
bgneal@183 12712 for (node = walker.current(); node; node = walker.next()) {
bgneal@183 12713 if (node.nodeType == 3 && !isBlock(node.parentNode) && !isWhiteSpaceNode(node)) {
bgneal@183 12714 rng.setStart(node, 0);
bgneal@183 12715 break;
bgneal@183 12716 }
bgneal@183 12717 }
bgneal@183 12718 }
bgneal@183 12719
bgneal@183 12720 return rng;
bgneal@183 12721 };
bgneal@183 12722
bgneal@183 12723 function setElementFormat(elm, fmt) {
bgneal@183 12724 fmt = fmt || format;
bgneal@183 12725
bgneal@183 12726 if (elm) {
bgneal@183 12727 each(fmt.styles, function(value, name) {
bgneal@183 12728 dom.setStyle(elm, name, replaceVars(value, vars));
bgneal@183 12729 });
bgneal@183 12730
bgneal@183 12731 each(fmt.attributes, function(value, name) {
bgneal@183 12732 dom.setAttrib(elm, name, replaceVars(value, vars));
bgneal@183 12733 });
bgneal@183 12734
bgneal@183 12735 each(fmt.classes, function(value) {
bgneal@183 12736 value = replaceVars(value, vars);
bgneal@183 12737
bgneal@183 12738 if (!dom.hasClass(elm, value))
bgneal@183 12739 dom.addClass(elm, value);
bgneal@183 12740 });
bgneal@183 12741 }
bgneal@183 12742 };
bgneal@183 12743
bgneal@183 12744 function applyRngStyle(rng) {
bgneal@183 12745 var newWrappers = [], wrapName, wrapElm;
bgneal@183 12746
bgneal@183 12747 // Setup wrapper element
bgneal@183 12748 wrapName = format.inline || format.block;
bgneal@183 12749 wrapElm = dom.create(wrapName);
bgneal@183 12750 setElementFormat(wrapElm);
bgneal@183 12751
bgneal@183 12752 rangeUtils.walk(rng, function(nodes) {
bgneal@183 12753 var currentWrapElm;
bgneal@183 12754
bgneal@183 12755 function process(node) {
bgneal@183 12756 var nodeName = node.nodeName.toLowerCase(), parentName = node.parentNode.nodeName.toLowerCase(), found;
bgneal@183 12757
bgneal@183 12758 // Stop wrapping on br elements
bgneal@183 12759 if (isEq(nodeName, 'br')) {
bgneal@183 12760 currentWrapElm = 0;
bgneal@183 12761
bgneal@183 12762 // Remove any br elements when we wrap things
bgneal@183 12763 if (format.block)
bgneal@183 12764 dom.remove(node);
bgneal@183 12765
bgneal@183 12766 return;
bgneal@183 12767 }
bgneal@183 12768
bgneal@183 12769 // If node is wrapper type
bgneal@183 12770 if (format.wrapper && matchNode(node, name, vars)) {
bgneal@183 12771 currentWrapElm = 0;
bgneal@183 12772 return;
bgneal@183 12773 }
bgneal@183 12774
bgneal@183 12775 // Can we rename the block
bgneal@183 12776 if (format.block && !format.wrapper && isTextBlock(nodeName)) {
bgneal@183 12777 node = dom.rename(node, wrapName);
bgneal@183 12778 setElementFormat(node);
bgneal@183 12779 newWrappers.push(node);
bgneal@183 12780 currentWrapElm = 0;
bgneal@183 12781 return;
bgneal@183 12782 }
bgneal@183 12783
bgneal@183 12784 // Handle selector patterns
bgneal@183 12785 if (format.selector) {
bgneal@183 12786 // Look for matching formats
bgneal@183 12787 each(formatList, function(format) {
bgneal@183 12788 if (dom.is(node, format.selector) && !isCaretNode(node)) {
bgneal@183 12789 setElementFormat(node, format);
bgneal@183 12790 found = true;
bgneal@183 12791 }
bgneal@183 12792 });
bgneal@183 12793
bgneal@217 12794 // Continue processing if a selector match wasn't found and a inline element is defined
bgneal@183 12795 if (!format.inline || found) {
bgneal@183 12796 currentWrapElm = 0;
bgneal@183 12797 return;
bgneal@183 12798 }
bgneal@183 12799 }
bgneal@183 12800
bgneal@183 12801 // Is it valid to wrap this item
bgneal@183 12802 if (isValid(wrapName, nodeName) && isValid(parentName, wrapName)) {
bgneal@183 12803 // Start wrapping
bgneal@183 12804 if (!currentWrapElm) {
bgneal@183 12805 // Wrap the node
bgneal@183 12806 currentWrapElm = wrapElm.cloneNode(FALSE);
bgneal@183 12807 node.parentNode.insertBefore(currentWrapElm, node);
bgneal@183 12808 newWrappers.push(currentWrapElm);
bgneal@183 12809 }
bgneal@183 12810
bgneal@183 12811 currentWrapElm.appendChild(node);
bgneal@183 12812 } else {
bgneal@183 12813 // Start a new wrapper for possible children
bgneal@183 12814 currentWrapElm = 0;
bgneal@183 12815
bgneal@183 12816 each(tinymce.grep(node.childNodes), process);
bgneal@183 12817
bgneal@183 12818 // End the last wrapper
bgneal@183 12819 currentWrapElm = 0;
bgneal@183 12820 }
bgneal@183 12821 };
bgneal@183 12822
bgneal@183 12823 // Process siblings from range
bgneal@183 12824 each(nodes, process);
bgneal@183 12825 });
bgneal@183 12826
bgneal@183 12827 // Cleanup
bgneal@183 12828 each(newWrappers, function(node) {
bgneal@183 12829 var childCount;
bgneal@183 12830
bgneal@183 12831 function getChildCount(node) {
bgneal@183 12832 var count = 0;
bgneal@183 12833
bgneal@183 12834 each(node.childNodes, function(node) {
bgneal@183 12835 if (!isWhiteSpaceNode(node) && !isBookmarkNode(node))
bgneal@183 12836 count++;
bgneal@183 12837 });
bgneal@183 12838
bgneal@183 12839 return count;
bgneal@183 12840 };
bgneal@183 12841
bgneal@183 12842 function mergeStyles(node) {
bgneal@183 12843 var child, clone;
bgneal@183 12844
bgneal@183 12845 each(node.childNodes, function(node) {
bgneal@183 12846 if (node.nodeType == 1 && !isBookmarkNode(node) && !isCaretNode(node)) {
bgneal@183 12847 child = node;
bgneal@183 12848 return FALSE; // break loop
bgneal@183 12849 }
bgneal@183 12850 });
bgneal@183 12851
bgneal@183 12852 // If child was found and of the same type as the current node
bgneal@183 12853 if (child && matchName(child, format)) {
bgneal@183 12854 clone = child.cloneNode(FALSE);
bgneal@183 12855 setElementFormat(clone);
bgneal@183 12856
bgneal@183 12857 dom.replace(clone, node, TRUE);
bgneal@183 12858 dom.remove(child, 1);
bgneal@183 12859 }
bgneal@183 12860
bgneal@183 12861 return clone || node;
bgneal@183 12862 };
bgneal@183 12863
bgneal@183 12864 childCount = getChildCount(node);
bgneal@183 12865
bgneal@183 12866 // Remove empty nodes
bgneal@183 12867 if (childCount === 0) {
bgneal@183 12868 dom.remove(node, 1);
bgneal@183 12869 return;
bgneal@183 12870 }
bgneal@183 12871
bgneal@183 12872 if (format.inline || format.wrapper) {
bgneal@183 12873 // Merges the current node with it's children of similar type to reduce the number of elements
bgneal@183 12874 if (!format.exact && childCount === 1)
bgneal@183 12875 node = mergeStyles(node);
bgneal@183 12876
bgneal@183 12877 // Remove/merge children
bgneal@183 12878 each(formatList, function(format) {
bgneal@183 12879 // Merge all children of similar type will move styles from child to parent
bgneal@183 12880 // this: <span style="color:red"><b><span style="color:red; font-size:10px">text</span></b></span>
bgneal@183 12881 // will become: <span style="color:red"><b><span style="font-size:10px">text</span></b></span>
bgneal@183 12882 each(dom.select(format.inline, node), function(child) {
bgneal@183 12883 removeFormat(format, vars, child, format.exact ? child : null);
bgneal@183 12884 });
bgneal@183 12885 });
bgneal@183 12886
bgneal@217 12887 // Remove child if direct parent is of same type
bgneal@217 12888 if (matchNode(node.parentNode, name, vars)) {
bgneal@217 12889 dom.remove(node, 1);
bgneal@217 12890 node = 0;
bgneal@217 12891 return TRUE;
bgneal@217 12892 }
bgneal@217 12893
bgneal@183 12894 // Look for parent with similar style format
bgneal@217 12895 if (format.merge_with_parents) {
bgneal@217 12896 dom.getParent(node.parentNode, function(parent) {
bgneal@217 12897 if (matchNode(parent, name, vars)) {
bgneal@217 12898 dom.remove(node, 1);
bgneal@217 12899 node = 0;
bgneal@217 12900 return TRUE;
bgneal@217 12901 }
bgneal@217 12902 });
bgneal@217 12903 }
bgneal@183 12904
bgneal@183 12905 // Merge next and previous siblings if they are similar <b>text</b><b>text</b> becomes <b>texttext</b>
bgneal@183 12906 if (node) {
bgneal@183 12907 node = mergeSiblings(getNonWhiteSpaceSibling(node), node);
bgneal@183 12908 node = mergeSiblings(node, getNonWhiteSpaceSibling(node, TRUE));
bgneal@183 12909 }
bgneal@183 12910 }
bgneal@183 12911 });
bgneal@183 12912 };
bgneal@183 12913
bgneal@183 12914 if (format) {
bgneal@183 12915 if (node) {
bgneal@183 12916 rng = dom.createRng();
bgneal@183 12917
bgneal@183 12918 rng.setStartBefore(node);
bgneal@183 12919 rng.setEndAfter(node);
bgneal@183 12920
bgneal@183 12921 applyRngStyle(rng);
bgneal@183 12922 } else {
bgneal@183 12923 if (!selection.isCollapsed() || !format.inline) {
bgneal@183 12924 // Apply formatting to selection
bgneal@183 12925 bookmark = selection.getBookmark();
bgneal@183 12926 applyRngStyle(expandRng(selection.getRng(TRUE), formatList));
bgneal@183 12927
bgneal@183 12928 selection.moveToBookmark(bookmark);
bgneal@183 12929 selection.setRng(moveStart(selection.getRng(TRUE)));
bgneal@183 12930 ed.nodeChanged();
bgneal@183 12931 } else
bgneal@183 12932 performCaretAction('apply', name, vars);
bgneal@183 12933 }
bgneal@183 12934 }
bgneal@183 12935 };
bgneal@183 12936
bgneal@183 12937 function remove(name, vars, node) {
bgneal@183 12938 var formatList = get(name), format = formatList[0], bookmark, i, rng;
bgneal@183 12939
bgneal@183 12940 // Merges the styles for each node
bgneal@183 12941 function process(node) {
bgneal@183 12942 var children, i, l;
bgneal@183 12943
bgneal@183 12944 // Grab the children first since the nodelist might be changed
bgneal@183 12945 children = tinymce.grep(node.childNodes);
bgneal@183 12946
bgneal@183 12947 // Process current node
bgneal@183 12948 for (i = 0, l = formatList.length; i < l; i++) {
bgneal@183 12949 if (removeFormat(formatList[i], vars, node, node))
bgneal@183 12950 break;
bgneal@183 12951 }
bgneal@183 12952
bgneal@183 12953 // Process the children
bgneal@183 12954 if (format.deep) {
bgneal@183 12955 for (i = 0, l = children.length; i < l; i++)
bgneal@183 12956 process(children[i]);
bgneal@183 12957 }
bgneal@183 12958 };
bgneal@183 12959
bgneal@183 12960 function findFormatRoot(container) {
bgneal@183 12961 var formatRoot;
bgneal@183 12962
bgneal@183 12963 // Find format root
bgneal@183 12964 each(getParents(container.parentNode).reverse(), function(parent) {
bgneal@183 12965 var format;
bgneal@183 12966
bgneal@183 12967 // Find format root element
bgneal@183 12968 if (!formatRoot && parent.id != '_start' && parent.id != '_end') {
bgneal@183 12969 // Is the node matching the format we are looking for
bgneal@183 12970 format = matchNode(parent, name, vars);
bgneal@183 12971 if (format && format.split !== false)
bgneal@183 12972 formatRoot = parent;
bgneal@183 12973 }
bgneal@183 12974 });
bgneal@183 12975
bgneal@183 12976 return formatRoot;
bgneal@183 12977 };
bgneal@183 12978
bgneal@183 12979 function wrapAndSplit(format_root, container, target, split) {
bgneal@183 12980 var parent, clone, lastClone, firstClone, i, formatRootParent;
bgneal@183 12981
bgneal@183 12982 // Format root found then clone formats and split it
bgneal@183 12983 if (format_root) {
bgneal@183 12984 formatRootParent = format_root.parentNode;
bgneal@183 12985
bgneal@183 12986 for (parent = container.parentNode; parent && parent != formatRootParent; parent = parent.parentNode) {
bgneal@183 12987 clone = parent.cloneNode(FALSE);
bgneal@183 12988
bgneal@183 12989 for (i = 0; i < formatList.length; i++) {
bgneal@183 12990 if (removeFormat(formatList[i], vars, clone, clone)) {
bgneal@183 12991 clone = 0;
bgneal@183 12992 break;
bgneal@183 12993 }
bgneal@183 12994 }
bgneal@183 12995
bgneal@183 12996 // Build wrapper node
bgneal@183 12997 if (clone) {
bgneal@183 12998 if (lastClone)
bgneal@183 12999 clone.appendChild(lastClone);
bgneal@183 13000
bgneal@183 13001 if (!firstClone)
bgneal@183 13002 firstClone = clone;
bgneal@183 13003
bgneal@183 13004 lastClone = clone;
bgneal@183 13005 }
bgneal@183 13006 }
bgneal@183 13007
bgneal@183 13008 // Never split block elements if the format is mixed
bgneal@183 13009 if (split && (!format.mixed || !isBlock(format_root)))
bgneal@183 13010 container = dom.split(format_root, container);
bgneal@183 13011
bgneal@183 13012 // Wrap container in cloned formats
bgneal@183 13013 if (lastClone) {
bgneal@183 13014 target.parentNode.insertBefore(lastClone, target);
bgneal@183 13015 firstClone.appendChild(target);
bgneal@183 13016 }
bgneal@183 13017 }
bgneal@183 13018
bgneal@183 13019 return container;
bgneal@183 13020 };
bgneal@183 13021
bgneal@183 13022 function splitToFormatRoot(container) {
bgneal@183 13023 return wrapAndSplit(findFormatRoot(container), container, container, true);
bgneal@183 13024 };
bgneal@183 13025
bgneal@183 13026 function unwrap(start) {
bgneal@183 13027 var node = dom.get(start ? '_start' : '_end'),
bgneal@183 13028 out = node[start ? 'firstChild' : 'lastChild'];
bgneal@183 13029
bgneal@217 13030 // If the end is placed within the start the result will be removed
bgneal@217 13031 // So this checks if the out node is a bookmark node if it is it
bgneal@217 13032 // checks for another more suitable node
bgneal@217 13033 if (isBookmarkNode(out))
bgneal@217 13034 out = out[start ? 'firstChild' : 'lastChild'];
bgneal@217 13035
bgneal@217 13036 dom.remove(node, true);
bgneal@183 13037
bgneal@183 13038 return out;
bgneal@183 13039 };
bgneal@183 13040
bgneal@183 13041 function removeRngStyle(rng) {
bgneal@183 13042 var startContainer, endContainer;
bgneal@183 13043
bgneal@183 13044 rng = expandRng(rng, formatList, TRUE);
bgneal@183 13045
bgneal@183 13046 if (format.split) {
bgneal@183 13047 startContainer = getContainer(rng, TRUE);
bgneal@183 13048 endContainer = getContainer(rng);
bgneal@183 13049
bgneal@183 13050 if (startContainer != endContainer) {
bgneal@183 13051 // Wrap start/end nodes in span element since these might be cloned/moved
bgneal@183 13052 startContainer = wrap(startContainer, 'span', {id : '_start', _mce_type : 'bookmark'});
bgneal@183 13053 endContainer = wrap(endContainer, 'span', {id : '_end', _mce_type : 'bookmark'});
bgneal@183 13054
bgneal@183 13055 // Split start/end
bgneal@183 13056 splitToFormatRoot(startContainer);
bgneal@183 13057 splitToFormatRoot(endContainer);
bgneal@183 13058
bgneal@183 13059 // Unwrap start/end to get real elements again
bgneal@183 13060 startContainer = unwrap(TRUE);
bgneal@183 13061 endContainer = unwrap();
bgneal@183 13062 } else
bgneal@183 13063 startContainer = endContainer = splitToFormatRoot(startContainer);
bgneal@183 13064
bgneal@183 13065 // Update range positions since they might have changed after the split operations
bgneal@183 13066 rng.startContainer = startContainer.parentNode;
bgneal@183 13067 rng.startOffset = nodeIndex(startContainer);
bgneal@183 13068 rng.endContainer = endContainer.parentNode;
bgneal@183 13069 rng.endOffset = nodeIndex(endContainer) + 1;
bgneal@183 13070 }
bgneal@183 13071
bgneal@183 13072 // Remove items between start/end
bgneal@183 13073 rangeUtils.walk(rng, function(nodes) {
bgneal@183 13074 each(nodes, function(node) {
bgneal@183 13075 process(node);
bgneal@183 13076 });
bgneal@183 13077 });
bgneal@183 13078 };
bgneal@183 13079
bgneal@183 13080 // Handle node
bgneal@183 13081 if (node) {
bgneal@183 13082 rng = dom.createRng();
bgneal@183 13083 rng.setStartBefore(node);
bgneal@183 13084 rng.setEndAfter(node);
bgneal@183 13085 removeRngStyle(rng);
bgneal@183 13086 return;
bgneal@183 13087 }
bgneal@183 13088
bgneal@183 13089 if (!selection.isCollapsed() || !format.inline) {
bgneal@183 13090 bookmark = selection.getBookmark();
bgneal@183 13091 removeRngStyle(selection.getRng(TRUE));
bgneal@183 13092 selection.moveToBookmark(bookmark);
bgneal@183 13093 ed.nodeChanged();
bgneal@45 13094 } else
bgneal@183 13095 performCaretAction('remove', name, vars);
bgneal@183 13096 };
bgneal@183 13097
bgneal@183 13098 function toggle(name, vars, node) {
bgneal@183 13099 if (match(name, vars, node))
bgneal@183 13100 remove(name, vars, node);
bgneal@183 13101 else
bgneal@183 13102 apply(name, vars, node);
bgneal@183 13103 };
bgneal@183 13104
bgneal@217 13105 function matchNode(node, name, vars, similar) {
bgneal@183 13106 var formatList = get(name), format, i, classes;
bgneal@183 13107
bgneal@183 13108 function matchItems(node, format, item_name) {
bgneal@183 13109 var key, value, items = format[item_name], i;
bgneal@183 13110
bgneal@183 13111 // Check all items
bgneal@183 13112 if (items) {
bgneal@183 13113 // Non indexed object
bgneal@183 13114 if (items.length === undefined) {
bgneal@183 13115 for (key in items) {
bgneal@183 13116 if (items.hasOwnProperty(key)) {
bgneal@183 13117 if (item_name === 'attributes')
bgneal@183 13118 value = dom.getAttrib(node, key);
bgneal@183 13119 else
bgneal@183 13120 value = getStyle(node, key);
bgneal@183 13121
bgneal@217 13122 if (similar && !value && !format.exact)
bgneal@217 13123 return;
bgneal@217 13124
bgneal@217 13125 if ((!similar || format.exact) && !isEq(value, replaceVars(items[key], vars)))
bgneal@183 13126 return;
bgneal@183 13127 }
bgneal@183 13128 }
bgneal@183 13129 } else {
bgneal@183 13130 // Only one match needed for indexed arrays
bgneal@183 13131 for (i = 0; i < items.length; i++) {
bgneal@183 13132 if (item_name === 'attributes' ? dom.getAttrib(node, items[i]) : getStyle(node, items[i]))
bgneal@183 13133 return format;
bgneal@183 13134 }
bgneal@183 13135 }
bgneal@183 13136 }
bgneal@183 13137
bgneal@183 13138 return format;
bgneal@183 13139 };
bgneal@183 13140
bgneal@183 13141 if (formatList && node) {
bgneal@183 13142 // Check each format in list
bgneal@183 13143 for (i = 0; i < formatList.length; i++) {
bgneal@183 13144 format = formatList[i];
bgneal@183 13145
bgneal@183 13146 // Name name, attributes, styles and classes
bgneal@183 13147 if (matchName(node, format) && matchItems(node, format, 'attributes') && matchItems(node, format, 'styles')) {
bgneal@183 13148 // Match classes
bgneal@183 13149 if (classes = format.classes) {
bgneal@183 13150 for (i = 0; i < classes.length; i++) {
bgneal@183 13151 if (!dom.hasClass(node, classes[i]))
bgneal@183 13152 return;
bgneal@183 13153 }
bgneal@183 13154 }
bgneal@183 13155
bgneal@183 13156 return format;
bgneal@183 13157 }
bgneal@183 13158 }
bgneal@183 13159 }
bgneal@183 13160 };
bgneal@183 13161
bgneal@183 13162 function match(name, vars, node) {
bgneal@183 13163 var startNode, i;
bgneal@183 13164
bgneal@183 13165 function matchParents(node) {
bgneal@183 13166 // Find first node with similar format settings
bgneal@183 13167 node = dom.getParent(node, function(node) {
bgneal@217 13168 return !!matchNode(node, name, vars, true);
bgneal@183 13169 });
bgneal@183 13170
bgneal@183 13171 // Do an exact check on the similar format element
bgneal@183 13172 return matchNode(node, name, vars);
bgneal@183 13173 };
bgneal@183 13174
bgneal@183 13175 // Check specified node
bgneal@183 13176 if (node)
bgneal@183 13177 return matchParents(node);
bgneal@183 13178
bgneal@183 13179 // Check pending formats
bgneal@183 13180 if (selection.isCollapsed()) {
bgneal@183 13181 for (i = pendingFormats.apply.length - 1; i >= 0; i--) {
bgneal@183 13182 if (pendingFormats.apply[i].name == name)
bgneal@183 13183 return true;
bgneal@183 13184 }
bgneal@183 13185
bgneal@183 13186 for (i = pendingFormats.remove.length - 1; i >= 0; i--) {
bgneal@183 13187 if (pendingFormats.remove[i].name == name)
bgneal@183 13188 return false;
bgneal@183 13189 }
bgneal@183 13190
bgneal@183 13191 return matchParents(selection.getNode());
bgneal@183 13192 }
bgneal@183 13193
bgneal@183 13194 // Check selected node
bgneal@183 13195 node = selection.getNode();
bgneal@183 13196 if (matchParents(node))
bgneal@183 13197 return TRUE;
bgneal@183 13198
bgneal@183 13199 // Check start node if it's different
bgneal@183 13200 startNode = selection.getStart();
bgneal@183 13201 if (startNode != node) {
bgneal@183 13202 if (matchParents(startNode))
bgneal@183 13203 return TRUE;
bgneal@183 13204 }
bgneal@183 13205
bgneal@183 13206 return FALSE;
bgneal@183 13207 };
bgneal@183 13208
bgneal@183 13209 function matchAll(names, vars) {
bgneal@183 13210 var startElement, matchedFormatNames = [], checkedMap = {}, i, ni, name;
bgneal@183 13211
bgneal@183 13212 // If the selection is collapsed then check pending formats
bgneal@183 13213 if (selection.isCollapsed()) {
bgneal@183 13214 for (ni = 0; ni < names.length; ni++) {
bgneal@183 13215 // If the name is to be removed, then stop it from being added
bgneal@183 13216 for (i = pendingFormats.remove.length - 1; i >= 0; i--) {
bgneal@183 13217 name = names[ni];
bgneal@183 13218
bgneal@183 13219 if (pendingFormats.remove[i].name == name) {
bgneal@183 13220 checkedMap[name] = true;
bgneal@183 13221 break;
bgneal@183 13222 }
bgneal@183 13223 }
bgneal@183 13224 }
bgneal@183 13225
bgneal@183 13226 // If the format is to be applied
bgneal@183 13227 for (i = pendingFormats.apply.length - 1; i >= 0; i--) {
bgneal@183 13228 for (ni = 0; ni < names.length; ni++) {
bgneal@183 13229 name = names[ni];
bgneal@183 13230
bgneal@183 13231 if (!checkedMap[name] && pendingFormats.apply[i].name == name) {
bgneal@183 13232 checkedMap[name] = true;
bgneal@183 13233 matchedFormatNames.push(name);
bgneal@183 13234 }
bgneal@183 13235 }
bgneal@183 13236 }
bgneal@183 13237 }
bgneal@183 13238
bgneal@183 13239 // Check start of selection for formats
bgneal@183 13240 startElement = selection.getStart();
bgneal@183 13241 dom.getParent(startElement, function(node) {
bgneal@183 13242 var i, name;
bgneal@183 13243
bgneal@183 13244 for (i = 0; i < names.length; i++) {
bgneal@183 13245 name = names[i];
bgneal@183 13246
bgneal@183 13247 if (!checkedMap[name] && matchNode(node, name, vars)) {
bgneal@183 13248 checkedMap[name] = true;
bgneal@183 13249 matchedFormatNames.push(name);
bgneal@183 13250 }
bgneal@183 13251 }
bgneal@183 13252 });
bgneal@183 13253
bgneal@183 13254 return matchedFormatNames;
bgneal@183 13255 };
bgneal@183 13256
bgneal@183 13257 function canApply(name) {
bgneal@183 13258 var formatList = get(name), startNode, parents, i, x, selector;
bgneal@183 13259
bgneal@183 13260 if (formatList) {
bgneal@183 13261 startNode = selection.getStart();
bgneal@183 13262 parents = getParents(startNode);
bgneal@183 13263
bgneal@183 13264 for (x = formatList.length - 1; x >= 0; x--) {
bgneal@183 13265 selector = formatList[x].selector;
bgneal@183 13266
bgneal@183 13267 // Format is not selector based, then always return TRUE
bgneal@183 13268 if (!selector)
bgneal@183 13269 return TRUE;
bgneal@183 13270
bgneal@183 13271 for (i = parents.length - 1; i >= 0; i--) {
bgneal@183 13272 if (dom.is(parents[i], selector))
bgneal@183 13273 return TRUE;
bgneal@183 13274 }
bgneal@183 13275 }
bgneal@183 13276 }
bgneal@183 13277
bgneal@183 13278 return FALSE;
bgneal@183 13279 };
bgneal@183 13280
bgneal@183 13281 // Expose to public
bgneal@183 13282 tinymce.extend(this, {
bgneal@183 13283 get : get,
bgneal@183 13284 register : register,
bgneal@183 13285 apply : apply,
bgneal@183 13286 remove : remove,
bgneal@183 13287 toggle : toggle,
bgneal@183 13288 match : match,
bgneal@183 13289 matchAll : matchAll,
bgneal@183 13290 matchNode : matchNode,
bgneal@183 13291 canApply : canApply
bgneal@183 13292 });
bgneal@183 13293
bgneal@183 13294 // Private functions
bgneal@183 13295
bgneal@183 13296 function matchName(node, format) {
bgneal@183 13297 // Check for inline match
bgneal@183 13298 if (isEq(node, format.inline))
bgneal@183 13299 return TRUE;
bgneal@183 13300
bgneal@183 13301 // Check for block match
bgneal@183 13302 if (isEq(node, format.block))
bgneal@183 13303 return TRUE;
bgneal@183 13304
bgneal@183 13305 // Check for selector match
bgneal@183 13306 if (format.selector)
bgneal@183 13307 return dom.is(node, format.selector);
bgneal@183 13308 };
bgneal@183 13309
bgneal@183 13310 function isEq(str1, str2) {
bgneal@183 13311 str1 = str1 || '';
bgneal@183 13312 str2 = str2 || '';
bgneal@183 13313
bgneal@183 13314 str1 = '' + (str1.nodeName || str1);
bgneal@183 13315 str2 = '' + (str2.nodeName || str2);
bgneal@183 13316
bgneal@183 13317 return str1.toLowerCase() == str2.toLowerCase();
bgneal@183 13318 };
bgneal@183 13319
bgneal@183 13320 function getStyle(node, name) {
bgneal@183 13321 var styleVal = dom.getStyle(node, name);
bgneal@183 13322
bgneal@183 13323 // Force the format to hex
bgneal@183 13324 if (name == 'color' || name == 'backgroundColor')
bgneal@183 13325 styleVal = dom.toHex(styleVal);
bgneal@183 13326
bgneal@183 13327 // Opera will return bold as 700
bgneal@183 13328 if (name == 'fontWeight' && styleVal == 700)
bgneal@183 13329 styleVal = 'bold';
bgneal@183 13330
bgneal@183 13331 return '' + styleVal;
bgneal@183 13332 };
bgneal@183 13333
bgneal@183 13334 function replaceVars(value, vars) {
bgneal@183 13335 if (typeof(value) != "string")
bgneal@183 13336 value = value(vars);
bgneal@183 13337 else if (vars) {
bgneal@183 13338 value = value.replace(/%(\w+)/g, function(str, name) {
bgneal@183 13339 return vars[name] || str;
bgneal@183 13340 });
bgneal@183 13341 }
bgneal@183 13342
bgneal@183 13343 return value;
bgneal@183 13344 };
bgneal@183 13345
bgneal@183 13346 function isWhiteSpaceNode(node) {
bgneal@183 13347 return node && node.nodeType === 3 && /^\s*$/.test(node.nodeValue);
bgneal@183 13348 };
bgneal@183 13349
bgneal@183 13350 function wrap(node, name, attrs) {
bgneal@183 13351 var wrapper = dom.create(name, attrs);
bgneal@183 13352
bgneal@183 13353 node.parentNode.insertBefore(wrapper, node);
bgneal@183 13354 wrapper.appendChild(node);
bgneal@183 13355
bgneal@183 13356 return wrapper;
bgneal@183 13357 };
bgneal@183 13358
bgneal@183 13359 function expandRng(rng, format, remove) {
bgneal@183 13360 var startContainer = rng.startContainer,
bgneal@183 13361 startOffset = rng.startOffset,
bgneal@183 13362 endContainer = rng.endContainer,
bgneal@183 13363 endOffset = rng.endOffset, sibling, lastIdx;
bgneal@183 13364
bgneal@183 13365 // This function walks up the tree if there is no siblings before/after the node
bgneal@183 13366 function findParentContainer(container, child_name, sibling_name, root) {
bgneal@183 13367 var parent, child;
bgneal@183 13368
bgneal@183 13369 root = root || dom.getRoot();
bgneal@183 13370
bgneal@183 13371 for (;;) {
bgneal@183 13372 // Check if we can move up are we at root level or body level
bgneal@183 13373 parent = container.parentNode;
bgneal@183 13374
bgneal@183 13375 // Stop expanding on block elements or root depending on format
bgneal@183 13376 if (parent == root || (!format[0].block_expand && isBlock(parent)))
bgneal@183 13377 return container;
bgneal@183 13378
bgneal@183 13379 for (sibling = parent[child_name]; sibling && sibling != container; sibling = sibling[sibling_name]) {
bgneal@183 13380 if (sibling.nodeType == 1 && !isBookmarkNode(sibling))
bgneal@183 13381 return container;
bgneal@183 13382
bgneal@183 13383 if (sibling.nodeType == 3 && !isWhiteSpaceNode(sibling))
bgneal@183 13384 return container;
bgneal@183 13385 }
bgneal@183 13386
bgneal@183 13387 container = container.parentNode;
bgneal@183 13388 }
bgneal@183 13389
bgneal@183 13390 return container;
bgneal@183 13391 };
bgneal@183 13392
bgneal@183 13393 // If index based start position then resolve it
bgneal@183 13394 if (startContainer.nodeType == 1 && startContainer.hasChildNodes()) {
bgneal@183 13395 lastIdx = startContainer.childNodes.length - 1;
bgneal@183 13396 startContainer = startContainer.childNodes[startOffset > lastIdx ? lastIdx : startOffset];
bgneal@183 13397
bgneal@183 13398 if (startContainer.nodeType == 3)
bgneal@183 13399 startOffset = 0;
bgneal@183 13400 }
bgneal@183 13401
bgneal@183 13402 // If index based end position then resolve it
bgneal@183 13403 if (endContainer.nodeType == 1 && endContainer.hasChildNodes()) {
bgneal@183 13404 lastIdx = endContainer.childNodes.length - 1;
bgneal@183 13405 endContainer = endContainer.childNodes[endOffset > lastIdx ? lastIdx : endOffset - 1];
bgneal@183 13406
bgneal@183 13407 if (endContainer.nodeType == 3)
bgneal@183 13408 endOffset = endContainer.nodeValue.length;
bgneal@183 13409 }
bgneal@183 13410
bgneal@183 13411 // Exclude bookmark nodes if possible
bgneal@183 13412 if (isBookmarkNode(startContainer.parentNode))
bgneal@183 13413 startContainer = startContainer.parentNode;
bgneal@183 13414
bgneal@183 13415 if (isBookmarkNode(startContainer))
bgneal@183 13416 startContainer = startContainer.nextSibling || startContainer;
bgneal@183 13417
bgneal@183 13418 if (isBookmarkNode(endContainer.parentNode))
bgneal@183 13419 endContainer = endContainer.parentNode;
bgneal@183 13420
bgneal@183 13421 if (isBookmarkNode(endContainer))
bgneal@183 13422 endContainer = endContainer.previousSibling || endContainer;
bgneal@183 13423
bgneal@183 13424 // Move start/end point up the tree if the leaves are sharp and if we are in different containers
bgneal@183 13425 // Example * becomes !: !<p><b><i>*text</i><i>text*</i></b></p>!
bgneal@183 13426 // This will reduce the number of wrapper elements that needs to be created
bgneal@183 13427 // Move start point up the tree
bgneal@183 13428 if (format[0].inline || format[0].block_expand) {
bgneal@183 13429 startContainer = findParentContainer(startContainer, 'firstChild', 'nextSibling');
bgneal@183 13430 endContainer = findParentContainer(endContainer, 'lastChild', 'previousSibling');
bgneal@183 13431 }
bgneal@183 13432
bgneal@183 13433 // Expand start/end container to matching selector
bgneal@183 13434 if (format[0].selector && format[0].expand !== FALSE && !format[0].inline) {
bgneal@183 13435 function findSelectorEndPoint(container, sibling_name) {
bgneal@183 13436 var parents, i, y;
bgneal@183 13437
bgneal@183 13438 if (container.nodeType == 3 && container.nodeValue.length == 0 && container[sibling_name])
bgneal@183 13439 container = container[sibling_name];
bgneal@183 13440
bgneal@183 13441 parents = getParents(container);
bgneal@183 13442 for (i = 0; i < parents.length; i++) {
bgneal@183 13443 for (y = 0; y < format.length; y++) {
bgneal@183 13444 if (dom.is(parents[i], format[y].selector))
bgneal@183 13445 return parents[i];
bgneal@183 13446 }
bgneal@183 13447 }
bgneal@183 13448
bgneal@183 13449 return container;
bgneal@183 13450 };
bgneal@183 13451
bgneal@183 13452 // Find new startContainer/endContainer if there is better one
bgneal@183 13453 startContainer = findSelectorEndPoint(startContainer, 'previousSibling');
bgneal@183 13454 endContainer = findSelectorEndPoint(endContainer, 'nextSibling');
bgneal@183 13455 }
bgneal@183 13456
bgneal@183 13457 // Expand start/end container to matching block element or text node
bgneal@183 13458 if (format[0].block || format[0].selector) {
bgneal@183 13459 function findBlockEndPoint(container, sibling_name, sibling_name2) {
bgneal@183 13460 var node;
bgneal@183 13461
bgneal@183 13462 // Expand to block of similar type
bgneal@183 13463 if (!format[0].wrapper)
bgneal@183 13464 node = dom.getParent(container, format[0].block);
bgneal@183 13465
bgneal@183 13466 // Expand to first wrappable block element or any block element
bgneal@183 13467 if (!node)
bgneal@183 13468 node = dom.getParent(container.nodeType == 3 ? container.parentNode : container, isBlock);
bgneal@183 13469
bgneal@183 13470 // Exclude inner lists from wrapping
bgneal@183 13471 if (node && format[0].wrapper)
bgneal@183 13472 node = getParents(node, 'ul,ol').reverse()[0] || node;
bgneal@183 13473
bgneal@183 13474 // Didn't find a block element look for first/last wrappable element
bgneal@183 13475 if (!node) {
bgneal@183 13476 node = container;
bgneal@183 13477
bgneal@183 13478 while (node[sibling_name] && !isBlock(node[sibling_name])) {
bgneal@183 13479 node = node[sibling_name];
bgneal@183 13480
bgneal@183 13481 // Break on BR but include it will be removed later on
bgneal@183 13482 // we can't remove it now since we need to check if it can be wrapped
bgneal@183 13483 if (isEq(node, 'br'))
bgneal@183 13484 break;
bgneal@183 13485 }
bgneal@183 13486 }
bgneal@183 13487
bgneal@183 13488 return node || container;
bgneal@183 13489 };
bgneal@183 13490
bgneal@183 13491 // Find new startContainer/endContainer if there is better one
bgneal@183 13492 startContainer = findBlockEndPoint(startContainer, 'previousSibling');
bgneal@183 13493 endContainer = findBlockEndPoint(endContainer, 'nextSibling');
bgneal@183 13494
bgneal@183 13495 // Non block element then try to expand up the leaf
bgneal@183 13496 if (format[0].block) {
bgneal@183 13497 if (!isBlock(startContainer))
bgneal@183 13498 startContainer = findParentContainer(startContainer, 'firstChild', 'nextSibling');
bgneal@183 13499
bgneal@183 13500 if (!isBlock(endContainer))
bgneal@183 13501 endContainer = findParentContainer(endContainer, 'lastChild', 'previousSibling');
bgneal@183 13502 }
bgneal@183 13503 }
bgneal@183 13504
bgneal@183 13505 // Setup index for startContainer
bgneal@183 13506 if (startContainer.nodeType == 1) {
bgneal@183 13507 startOffset = nodeIndex(startContainer);
bgneal@183 13508 startContainer = startContainer.parentNode;
bgneal@183 13509 }
bgneal@183 13510
bgneal@183 13511 // Setup index for endContainer
bgneal@183 13512 if (endContainer.nodeType == 1) {
bgneal@183 13513 endOffset = nodeIndex(endContainer) + 1;
bgneal@183 13514 endContainer = endContainer.parentNode;
bgneal@183 13515 }
bgneal@183 13516
bgneal@183 13517 // Return new range like object
bgneal@183 13518 return {
bgneal@183 13519 startContainer : startContainer,
bgneal@183 13520 startOffset : startOffset,
bgneal@183 13521 endContainer : endContainer,
bgneal@183 13522 endOffset : endOffset
bgneal@183 13523 };
bgneal@45 13524 }
bgneal@45 13525
bgneal@183 13526 function removeFormat(format, vars, node, compare_node) {
bgneal@183 13527 var i, attrs, stylesModified;
bgneal@183 13528
bgneal@183 13529 // Check if node matches format
bgneal@183 13530 if (!matchName(node, format))
bgneal@183 13531 return FALSE;
bgneal@183 13532
bgneal@183 13533 // Should we compare with format attribs and styles
bgneal@183 13534 if (format.remove != 'all') {
bgneal@183 13535 // Remove styles
bgneal@183 13536 each(format.styles, function(value, name) {
bgneal@183 13537 value = replaceVars(value, vars);
bgneal@183 13538
bgneal@183 13539 // Indexed array
bgneal@183 13540 if (typeof(name) === 'number') {
bgneal@183 13541 name = value;
bgneal@183 13542 compare_node = 0;
bgneal@183 13543 }
bgneal@183 13544
bgneal@183 13545 if (!compare_node || isEq(getStyle(compare_node, name), value))
bgneal@183 13546 dom.setStyle(node, name, '');
bgneal@183 13547
bgneal@183 13548 stylesModified = 1;
bgneal@183 13549 });
bgneal@183 13550
bgneal@183 13551 // Remove style attribute if it's empty
bgneal@183 13552 if (stylesModified && dom.getAttrib(node, 'style') == '') {
bgneal@183 13553 node.removeAttribute('style');
bgneal@183 13554 node.removeAttribute('_mce_style');
bgneal@183 13555 }
bgneal@183 13556
bgneal@183 13557 // Remove attributes
bgneal@183 13558 each(format.attributes, function(value, name) {
bgneal@183 13559 var valueOut;
bgneal@183 13560
bgneal@183 13561 value = replaceVars(value, vars);
bgneal@183 13562
bgneal@183 13563 // Indexed array
bgneal@183 13564 if (typeof(name) === 'number') {
bgneal@183 13565 name = value;
bgneal@183 13566 compare_node = 0;
bgneal@183 13567 }
bgneal@183 13568
bgneal@183 13569 if (!compare_node || isEq(dom.getAttrib(compare_node, name), value)) {
bgneal@183 13570 // Keep internal classes
bgneal@183 13571 if (name == 'class') {
bgneal@183 13572 value = dom.getAttrib(node, name);
bgneal@183 13573 if (value) {
bgneal@183 13574 // Build new class value where everything is removed except the internal prefixed classes
bgneal@183 13575 valueOut = '';
bgneal@183 13576 each(value.split(/\s+/), function(cls) {
bgneal@183 13577 if (/mce\w+/.test(cls))
bgneal@183 13578 valueOut += (valueOut ? ' ' : '') + cls;
bgneal@183 13579 });
bgneal@183 13580
bgneal@183 13581 // We got some internal classes left
bgneal@183 13582 if (valueOut) {
bgneal@183 13583 dom.setAttrib(node, name, valueOut);
bgneal@183 13584 return;
bgneal@183 13585 }
bgneal@183 13586 }
bgneal@183 13587 }
bgneal@183 13588
bgneal@183 13589 // IE6 has a bug where the attribute doesn't get removed correctly
bgneal@183 13590 if (name == "class")
bgneal@183 13591 node.removeAttribute('className');
bgneal@183 13592
bgneal@183 13593 // Remove mce prefixed attributes
bgneal@183 13594 if (MCE_ATTR_RE.test(name))
bgneal@183 13595 node.removeAttribute('_mce_' + name);
bgneal@183 13596
bgneal@183 13597 node.removeAttribute(name);
bgneal@183 13598 }
bgneal@183 13599 });
bgneal@183 13600
bgneal@183 13601 // Remove classes
bgneal@183 13602 each(format.classes, function(value) {
bgneal@183 13603 value = replaceVars(value, vars);
bgneal@183 13604
bgneal@183 13605 if (!compare_node || dom.hasClass(compare_node, value))
bgneal@183 13606 dom.removeClass(node, value);
bgneal@183 13607 });
bgneal@183 13608
bgneal@183 13609 // Check for non internal attributes
bgneal@183 13610 attrs = dom.getAttribs(node);
bgneal@183 13611 for (i = 0; i < attrs.length; i++) {
bgneal@183 13612 if (attrs[i].nodeName.indexOf('_') !== 0)
bgneal@183 13613 return FALSE;
bgneal@183 13614 }
bgneal@183 13615 }
bgneal@183 13616
bgneal@183 13617 // Remove the inline child if it's empty for example <b> or <span>
bgneal@183 13618 if (format.remove != 'none') {
bgneal@183 13619 removeNode(node, format);
bgneal@183 13620 return TRUE;
bgneal@183 13621 }
bgneal@183 13622 };
bgneal@183 13623
bgneal@183 13624 function removeNode(node, format) {
bgneal@183 13625 var parentNode = node.parentNode, rootBlockElm;
bgneal@183 13626
bgneal@183 13627 if (format.block) {
bgneal@183 13628 if (!forcedRootBlock) {
bgneal@183 13629 function find(node, next, inc) {
bgneal@183 13630 node = getNonWhiteSpaceSibling(node, next, inc);
bgneal@183 13631
bgneal@183 13632 return !node || (node.nodeName == 'BR' || isBlock(node));
bgneal@183 13633 };
bgneal@183 13634
bgneal@183 13635 // Append BR elements if needed before we remove the block
bgneal@183 13636 if (isBlock(node) && !isBlock(parentNode)) {
bgneal@183 13637 if (!find(node, FALSE) && !find(node.firstChild, TRUE, 1))
bgneal@183 13638 node.insertBefore(dom.create('br'), node.firstChild);
bgneal@183 13639
bgneal@183 13640 if (!find(node, TRUE) && !find(node.lastChild, FALSE, 1))
bgneal@183 13641 node.appendChild(dom.create('br'));
bgneal@183 13642 }
bgneal@183 13643 } else {
bgneal@183 13644 // Wrap the block in a forcedRootBlock if we are at the root of document
bgneal@183 13645 if (parentNode == dom.getRoot()) {
bgneal@183 13646 if (!format.list_block || !isEq(node, format.list_block)) {
bgneal@183 13647 each(tinymce.grep(node.childNodes), function(node) {
bgneal@183 13648 if (isValid(forcedRootBlock, node.nodeName.toLowerCase())) {
bgneal@183 13649 if (!rootBlockElm)
bgneal@183 13650 rootBlockElm = wrap(node, forcedRootBlock);
bgneal@183 13651 else
bgneal@183 13652 rootBlockElm.appendChild(node);
bgneal@183 13653 } else
bgneal@183 13654 rootBlockElm = 0;
bgneal@183 13655 });
bgneal@183 13656 }
bgneal@183 13657 }
bgneal@183 13658 }
bgneal@183 13659 }
bgneal@183 13660
bgneal@183 13661 // Never remove nodes that isn't the specified inline element if a selector is specified too
bgneal@183 13662 if (format.selector && format.inline && !isEq(format.inline, node))
bgneal@45 13663 return;
bgneal@183 13664
bgneal@183 13665 dom.remove(node, 1);
bgneal@183 13666 };
bgneal@183 13667
bgneal@183 13668 function getNonWhiteSpaceSibling(node, next, inc) {
bgneal@183 13669 if (node) {
bgneal@183 13670 next = next ? 'nextSibling' : 'previousSibling';
bgneal@183 13671
bgneal@183 13672 for (node = inc ? node : node[next]; node; node = node[next]) {
bgneal@183 13673 if (node.nodeType == 1 || !isWhiteSpaceNode(node))
bgneal@183 13674 return node;
bgneal@183 13675 }
bgneal@183 13676 }
bgneal@183 13677 };
bgneal@183 13678
bgneal@183 13679 function isBookmarkNode(node) {
bgneal@183 13680 return node && node.nodeType == 1 && node.getAttribute('_mce_type') == 'bookmark';
bgneal@183 13681 };
bgneal@183 13682
bgneal@183 13683 function mergeSiblings(prev, next) {
bgneal@183 13684 var marker, sibling, tmpSibling;
bgneal@183 13685
bgneal@183 13686 function compareElements(node1, node2) {
bgneal@183 13687 // Not the same name
bgneal@183 13688 if (node1.nodeName != node2.nodeName)
bgneal@183 13689 return FALSE;
bgneal@183 13690
bgneal@183 13691 function getAttribs(node) {
bgneal@183 13692 var attribs = {};
bgneal@183 13693
bgneal@183 13694 each(dom.getAttribs(node), function(attr) {
bgneal@183 13695 var name = attr.nodeName.toLowerCase();
bgneal@183 13696
bgneal@183 13697 // Don't compare internal attributes or style
bgneal@183 13698 if (name.indexOf('_') !== 0 && name !== 'style')
bgneal@183 13699 attribs[name] = dom.getAttrib(node, name);
bgneal@183 13700 });
bgneal@183 13701
bgneal@183 13702 return attribs;
bgneal@183 13703 };
bgneal@183 13704
bgneal@183 13705 function compareObjects(obj1, obj2) {
bgneal@183 13706 var value, name;
bgneal@183 13707
bgneal@183 13708 for (name in obj1) {
bgneal@183 13709 // Obj1 has item obj2 doesn't have
bgneal@183 13710 if (obj1.hasOwnProperty(name)) {
bgneal@183 13711 value = obj2[name];
bgneal@183 13712
bgneal@183 13713 // Obj2 doesn't have obj1 item
bgneal@183 13714 if (value === undefined)
bgneal@183 13715 return FALSE;
bgneal@183 13716
bgneal@183 13717 // Obj2 item has a different value
bgneal@183 13718 if (obj1[name] != value)
bgneal@183 13719 return FALSE;
bgneal@183 13720
bgneal@183 13721 // Delete similar value
bgneal@183 13722 delete obj2[name];
bgneal@183 13723 }
bgneal@183 13724 }
bgneal@183 13725
bgneal@183 13726 // Check if obj 2 has something obj 1 doesn't have
bgneal@183 13727 for (name in obj2) {
bgneal@183 13728 // Obj2 has item obj1 doesn't have
bgneal@183 13729 if (obj2.hasOwnProperty(name))
bgneal@183 13730 return FALSE;
bgneal@183 13731 }
bgneal@183 13732
bgneal@183 13733 return TRUE;
bgneal@183 13734 };
bgneal@183 13735
bgneal@183 13736 // Attribs are not the same
bgneal@183 13737 if (!compareObjects(getAttribs(node1), getAttribs(node2)))
bgneal@183 13738 return FALSE;
bgneal@183 13739
bgneal@183 13740 // Styles are not the same
bgneal@183 13741 if (!compareObjects(dom.parseStyle(dom.getAttrib(node1, 'style')), dom.parseStyle(dom.getAttrib(node2, 'style'))))
bgneal@183 13742 return FALSE;
bgneal@183 13743
bgneal@183 13744 return TRUE;
bgneal@183 13745 };
bgneal@183 13746
bgneal@183 13747 // Check if next/prev exists and that they are elements
bgneal@183 13748 if (prev && next) {
bgneal@183 13749 function findElementSibling(node, sibling_name) {
bgneal@183 13750 for (sibling = node; sibling; sibling = sibling[sibling_name]) {
bgneal@183 13751 if (sibling.nodeType == 3 && !isWhiteSpaceNode(sibling))
bgneal@183 13752 return node;
bgneal@183 13753
bgneal@183 13754 if (sibling.nodeType == 1 && !isBookmarkNode(sibling))
bgneal@183 13755 return sibling;
bgneal@183 13756 }
bgneal@183 13757
bgneal@183 13758 return node;
bgneal@183 13759 };
bgneal@183 13760
bgneal@183 13761 // If previous sibling is empty then jump over it
bgneal@183 13762 prev = findElementSibling(prev, 'previousSibling');
bgneal@183 13763 next = findElementSibling(next, 'nextSibling');
bgneal@183 13764
bgneal@183 13765 // Compare next and previous nodes
bgneal@183 13766 if (compareElements(prev, next)) {
bgneal@183 13767 // Append nodes between
bgneal@183 13768 for (sibling = prev.nextSibling; sibling && sibling != next;) {
bgneal@183 13769 tmpSibling = sibling;
bgneal@183 13770 sibling = sibling.nextSibling;
bgneal@183 13771 prev.appendChild(tmpSibling);
bgneal@183 13772 }
bgneal@183 13773
bgneal@183 13774 // Remove next node
bgneal@183 13775 dom.remove(next);
bgneal@183 13776
bgneal@183 13777 // Move children into prev node
bgneal@183 13778 each(tinymce.grep(next.childNodes), function(node) {
bgneal@183 13779 prev.appendChild(node);
bgneal@183 13780 });
bgneal@183 13781
bgneal@183 13782 return prev;
bgneal@183 13783 }
bgneal@183 13784 }
bgneal@183 13785
bgneal@183 13786 return next;
bgneal@183 13787 };
bgneal@183 13788
bgneal@183 13789 function isTextBlock(name) {
bgneal@217 13790 return /^(h[1-6]|p|div|pre|address|dl|dt|dd)$/.test(name);
bgneal@183 13791 };
bgneal@183 13792
bgneal@183 13793 function getContainer(rng, start) {
bgneal@183 13794 var container, offset, lastIdx;
bgneal@183 13795
bgneal@183 13796 container = rng[start ? 'startContainer' : 'endContainer'];
bgneal@183 13797 offset = rng[start ? 'startOffset' : 'endOffset'];
bgneal@183 13798
bgneal@183 13799 if (container.nodeType == 1) {
bgneal@183 13800 lastIdx = container.childNodes.length - 1;
bgneal@183 13801
bgneal@183 13802 if (!start && offset)
bgneal@183 13803 offset--;
bgneal@183 13804
bgneal@183 13805 container = container.childNodes[offset > lastIdx ? lastIdx : offset];
bgneal@183 13806 }
bgneal@183 13807
bgneal@183 13808 return container;
bgneal@183 13809 };
bgneal@183 13810
bgneal@183 13811 function performCaretAction(type, name, vars) {
bgneal@183 13812 var i, currentPendingFormats = pendingFormats[type],
bgneal@183 13813 otherPendingFormats = pendingFormats[type == 'apply' ? 'remove' : 'apply'];
bgneal@183 13814
bgneal@183 13815 function hasPending() {
bgneal@183 13816 return pendingFormats.apply.length || pendingFormats.remove.length;
bgneal@183 13817 };
bgneal@183 13818
bgneal@183 13819 function resetPending() {
bgneal@183 13820 pendingFormats.apply = [];
bgneal@183 13821 pendingFormats.remove = [];
bgneal@183 13822 };
bgneal@183 13823
bgneal@183 13824 function perform(caret_node) {
bgneal@183 13825 // Apply pending formats
bgneal@183 13826 each(pendingFormats.apply.reverse(), function(item) {
bgneal@183 13827 apply(item.name, item.vars, caret_node);
bgneal@183 13828 });
bgneal@183 13829
bgneal@183 13830 // Remove pending formats
bgneal@183 13831 each(pendingFormats.remove.reverse(), function(item) {
bgneal@183 13832 remove(item.name, item.vars, caret_node);
bgneal@183 13833 });
bgneal@183 13834
bgneal@183 13835 dom.remove(caret_node, 1);
bgneal@183 13836 resetPending();
bgneal@183 13837 };
bgneal@183 13838
bgneal@183 13839 // Check if it already exists then ignore it
bgneal@183 13840 for (i = currentPendingFormats.length - 1; i >= 0; i--) {
bgneal@183 13841 if (currentPendingFormats[i].name == name)
bgneal@183 13842 return;
bgneal@183 13843 }
bgneal@183 13844
bgneal@183 13845 currentPendingFormats.push({name : name, vars : vars});
bgneal@183 13846
bgneal@183 13847 // Check if it's in the other type, then remove it
bgneal@183 13848 for (i = otherPendingFormats.length - 1; i >= 0; i--) {
bgneal@183 13849 if (otherPendingFormats[i].name == name)
bgneal@183 13850 otherPendingFormats.splice(i, 1);
bgneal@183 13851 }
bgneal@183 13852
bgneal@183 13853 // Pending apply or remove formats
bgneal@183 13854 if (hasPending()) {
bgneal@183 13855 ed.getDoc().execCommand('FontName', false, 'mceinline');
bgneal@217 13856 pendingFormats.lastRng = selection.getRng();
bgneal@183 13857
bgneal@183 13858 // IE will convert the current word
bgneal@183 13859 each(dom.select('font,span'), function(node) {
bgneal@183 13860 var bookmark;
bgneal@183 13861
bgneal@183 13862 if (isCaretNode(node)) {
bgneal@183 13863 bookmark = selection.getBookmark();
bgneal@183 13864 perform(node);
bgneal@183 13865 selection.moveToBookmark(bookmark);
bgneal@183 13866 ed.nodeChanged();
bgneal@183 13867 }
bgneal@183 13868 });
bgneal@183 13869
bgneal@183 13870 // Only register listeners once if we need to
bgneal@183 13871 if (!pendingFormats.isListening && hasPending()) {
bgneal@183 13872 pendingFormats.isListening = true;
bgneal@183 13873
bgneal@183 13874 each('onKeyDown,onKeyUp,onKeyPress,onMouseUp'.split(','), function(event) {
bgneal@183 13875 ed[event].addToTop(function(ed, e) {
bgneal@217 13876 // Do we have pending formats and is the selection moved has moved
bgneal@217 13877 if (hasPending() && !tinymce.dom.RangeUtils.compareRanges(pendingFormats.lastRng, selection.getRng())) {
bgneal@183 13878 each(dom.select('font,span'), function(node) {
bgneal@217 13879 var textNode, rng;
bgneal@183 13880
bgneal@183 13881 // Look for marker
bgneal@183 13882 if (isCaretNode(node)) {
bgneal@183 13883 textNode = node.firstChild;
bgneal@183 13884
bgneal@183 13885 perform(node);
bgneal@183 13886
bgneal@183 13887 rng = dom.createRng();
bgneal@183 13888 rng.setStart(textNode, textNode.nodeValue.length);
bgneal@183 13889 rng.setEnd(textNode, textNode.nodeValue.length);
bgneal@183 13890 selection.setRng(rng);
bgneal@183 13891 ed.nodeChanged();
bgneal@183 13892 }
bgneal@183 13893 });
bgneal@183 13894
bgneal@183 13895 // Always unbind and clear pending styles on keyup
bgneal@183 13896 if (e.type == 'keyup' || e.type == 'mouseup')
bgneal@183 13897 resetPending();
bgneal@183 13898 }
bgneal@183 13899 });
bgneal@183 13900 });
bgneal@183 13901 }
bgneal@183 13902 }
bgneal@183 13903 };
bgneal@183 13904 };
bgneal@183 13905 })(tinymce);
bgneal@183 13906
bgneal@183 13907 tinymce.onAddEditor.add(function(tinymce, ed) {
bgneal@183 13908 var filters, fontSizes, dom, settings = ed.settings;
bgneal@183 13909
bgneal@183 13910 if (settings.inline_styles) {
bgneal@183 13911 fontSizes = tinymce.explode(settings.font_size_style_values);
bgneal@183 13912
bgneal@183 13913 function replaceWithSpan(node, styles) {
bgneal@183 13914 dom.replace(dom.create('span', {
bgneal@183 13915 style : styles
bgneal@183 13916 }), node, 1);
bgneal@183 13917 };
bgneal@183 13918
bgneal@183 13919 filters = {
bgneal@183 13920 font : function(dom, node) {
bgneal@183 13921 replaceWithSpan(node, {
bgneal@183 13922 backgroundColor : node.style.backgroundColor,
bgneal@183 13923 color : node.color,
bgneal@183 13924 fontFamily : node.face,
bgneal@183 13925 fontSize : fontSizes[parseInt(node.size) - 1]
bgneal@183 13926 });
bgneal@183 13927 },
bgneal@183 13928
bgneal@183 13929 u : function(dom, node) {
bgneal@183 13930 replaceWithSpan(node, {
bgneal@183 13931 textDecoration : 'underline'
bgneal@183 13932 });
bgneal@183 13933 },
bgneal@183 13934
bgneal@183 13935 strike : function(dom, node) {
bgneal@183 13936 replaceWithSpan(node, {
bgneal@183 13937 textDecoration : 'line-through'
bgneal@183 13938 });
bgneal@183 13939 }
bgneal@183 13940 };
bgneal@183 13941
bgneal@183 13942 function convert(editor, params) {
bgneal@183 13943 dom = editor.dom;
bgneal@183 13944
bgneal@183 13945 if (settings.convert_fonts_to_spans) {
bgneal@183 13946 tinymce.each(dom.select('font,u,strike', params.node), function(node) {
bgneal@183 13947 filters[node.nodeName.toLowerCase()](ed.dom, node);
bgneal@183 13948 });
bgneal@183 13949 }
bgneal@183 13950 };
bgneal@183 13951
bgneal@183 13952 ed.onPreProcess.add(convert);
bgneal@183 13953
bgneal@183 13954 ed.onInit.add(function() {
bgneal@183 13955 ed.selection.onSetContent.add(convert);
bgneal@45 13956 });
bgneal@183 13957 }
bgneal@183 13958 });
bgneal@183 13959