| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | Gecko._pluginInfo = { |
|---|
| 35 | name : "Gecko", |
|---|
| 36 | origin : "Xinha Core", |
|---|
| 37 | version : "$LastChangedRevision$".replace(/^[^:]*:\s*(.*)\s*\$$/, '$1'), |
|---|
| 38 | developer : "The Xinha Core Developer Team", |
|---|
| 39 | developer_url : "$HeadURL$".replace(/^[^:]*:\s*(.*)\s*\$$/, '$1'), |
|---|
| 40 | sponsor : "", |
|---|
| 41 | sponsor_url : "", |
|---|
| 42 | license : "htmlArea" |
|---|
| 43 | }; |
|---|
| 44 | |
|---|
| 45 | function Gecko(editor) { |
|---|
| 46 | this.editor = editor; |
|---|
| 47 | editor.Gecko = this; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | Gecko.prototype.onKeyPress = function(ev) |
|---|
| 54 | { |
|---|
| 55 | var editor = this.editor; |
|---|
| 56 | var s = editor.getSelection(); |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | if(editor.isShortCut(ev)) |
|---|
| 60 | { |
|---|
| 61 | switch(editor.getKey(ev).toLowerCase()) |
|---|
| 62 | { |
|---|
| 63 | case 'z': |
|---|
| 64 | { |
|---|
| 65 | if(editor._unLink && editor._unlinkOnUndo) |
|---|
| 66 | { |
|---|
| 67 | Xinha._stopEvent(ev); |
|---|
| 68 | editor._unLink(); |
|---|
| 69 | editor.updateToolbar(); |
|---|
| 70 | return true; |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | break; |
|---|
| 74 | |
|---|
| 75 | case 'a': |
|---|
| 76 | { |
|---|
| 77 | |
|---|
| 78 | sel = editor.getSelection(); |
|---|
| 79 | sel.removeAllRanges(); |
|---|
| 80 | range = editor.createRange(); |
|---|
| 81 | range.selectNodeContents(editor._doc.body); |
|---|
| 82 | sel.addRange(range); |
|---|
| 83 | Xinha._stopEvent(ev); |
|---|
| 84 | return true; |
|---|
| 85 | } |
|---|
| 86 | break; |
|---|
| 87 | |
|---|
| 88 | case 'v': |
|---|
| 89 | { |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | if(!editor.config.htmlareaPaste) |
|---|
| 93 | { |
|---|
| 94 | return true; |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | break; |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | switch(editor.getKey(ev)) |
|---|
| 103 | { |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | case ' ': |
|---|
| 107 | { |
|---|
| 108 | var autoWrap = function (textNode, tag) |
|---|
| 109 | { |
|---|
| 110 | var rightText = textNode.nextSibling; |
|---|
| 111 | if ( typeof tag == 'string') |
|---|
| 112 | { |
|---|
| 113 | tag = editor._doc.createElement(tag); |
|---|
| 114 | } |
|---|
| 115 | var a = textNode.parentNode.insertBefore(tag, rightText); |
|---|
| 116 | Xinha.removeFromParent(textNode); |
|---|
| 117 | a.appendChild(textNode); |
|---|
| 118 | rightText.data = ' ' + rightText.data; |
|---|
| 119 | |
|---|
| 120 | s.collapse(rightText, 1); |
|---|
| 121 | |
|---|
| 122 | editor._unLink = function() |
|---|
| 123 | { |
|---|
| 124 | var t = a.firstChild; |
|---|
| 125 | a.removeChild(t); |
|---|
| 126 | a.parentNode.insertBefore(t, a); |
|---|
| 127 | Xinha.removeFromParent(a); |
|---|
| 128 | editor._unLink = null; |
|---|
| 129 | editor._unlinkOnUndo = false; |
|---|
| 130 | }; |
|---|
| 131 | editor._unlinkOnUndo = true; |
|---|
| 132 | |
|---|
| 133 | return a; |
|---|
| 134 | }; |
|---|
| 135 | |
|---|
| 136 | if ( editor.config.convertUrlsToLinks && s && s.isCollapsed && s.anchorNode.nodeType == 3 && s.anchorNode.data.length > 3 && s.anchorNode.data.indexOf('.') >= 0 ) |
|---|
| 137 | { |
|---|
| 138 | var midStart = s.anchorNode.data.substring(0,s.anchorOffset).search(/\S{4,}$/); |
|---|
| 139 | if ( midStart == -1 ) |
|---|
| 140 | { |
|---|
| 141 | break; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | if ( editor._getFirstAncestor(s, 'a') ) |
|---|
| 145 | { |
|---|
| 146 | break; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | var matchData = s.anchorNode.data.substring(0,s.anchorOffset).replace(/^.*?(\S*)$/, '$1'); |
|---|
| 150 | |
|---|
| 151 | var mEmail = matchData.match(Xinha.RE_email); |
|---|
| 152 | if ( mEmail ) |
|---|
| 153 | { |
|---|
| 154 | var leftTextEmail = s.anchorNode; |
|---|
| 155 | var rightTextEmail = leftTextEmail.splitText(s.anchorOffset); |
|---|
| 156 | var midTextEmail = leftTextEmail.splitText(midStart); |
|---|
| 157 | |
|---|
| 158 | autoWrap(midTextEmail, 'a').href = 'mailto:' + mEmail[0]; |
|---|
| 159 | break; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | RE_date = /([0-9]+\.)+/; |
|---|
| 163 | RE_ip = /(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/; |
|---|
| 164 | var mUrl = matchData.match(Xinha.RE_url); |
|---|
| 165 | if ( mUrl ) |
|---|
| 166 | { |
|---|
| 167 | if (RE_date.test(matchData)) |
|---|
| 168 | { |
|---|
| 169 | break; |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | } |
|---|
| 175 | var leftTextUrl = s.anchorNode; |
|---|
| 176 | var rightTextUrl = leftTextUrl.splitText(s.anchorOffset); |
|---|
| 177 | var midTextUrl = leftTextUrl.splitText(midStart); |
|---|
| 178 | autoWrap(midTextUrl, 'a').href = (mUrl[1] ? mUrl[1] : 'http://') + mUrl[2]; |
|---|
| 179 | break; |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | break; |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | switch ( ev.keyCode ) |
|---|
| 188 | { |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | case 27: |
|---|
| 195 | { |
|---|
| 196 | if ( editor._unLink ) |
|---|
| 197 | { |
|---|
| 198 | editor._unLink(); |
|---|
| 199 | Xinha._stopEvent(ev); |
|---|
| 200 | } |
|---|
| 201 | break; |
|---|
| 202 | } |
|---|
| 203 | break; |
|---|
| 204 | |
|---|
| 205 | case 8: |
|---|
| 206 | case 46: |
|---|
| 207 | { |
|---|
| 208 | |
|---|
| 209 | if ( !ev.shiftKey && this.handleBackspace() ) |
|---|
| 210 | { |
|---|
| 211 | Xinha._stopEvent(ev); |
|---|
| 212 | } |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | default: |
|---|
| 216 | { |
|---|
| 217 | editor._unlinkOnUndo = false; |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | if ( s.anchorNode && s.anchorNode.nodeType == 3 ) |
|---|
| 224 | { |
|---|
| 225 | |
|---|
| 226 | var a = editor._getFirstAncestor(s, 'a'); |
|---|
| 227 | |
|---|
| 228 | if ( !a ) |
|---|
| 229 | { |
|---|
| 230 | break; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | if ( !a._updateAnchTimeout ) |
|---|
| 234 | { |
|---|
| 235 | if ( s.anchorNode.data.match(Xinha.RE_email) && a.href.match('mailto:' + s.anchorNode.data.trim()) ) |
|---|
| 236 | { |
|---|
| 237 | var textNode = s.anchorNode; |
|---|
| 238 | var fnAnchor = function() |
|---|
| 239 | { |
|---|
| 240 | a.href = 'mailto:' + textNode.data.trim(); |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | a._updateAnchTimeout = setTimeout(fnAnchor, 250); |
|---|
| 248 | }; |
|---|
| 249 | a._updateAnchTimeout = setTimeout(fnAnchor, 1000); |
|---|
| 250 | break; |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | var m = s.anchorNode.data.match(Xinha.RE_url); |
|---|
| 254 | |
|---|
| 255 | if ( m && a.href.match(new RegExp( 'http(s)?://' + Xinha.escapeStringForRegExp( s.anchorNode.data.trim() ) ) ) ) |
|---|
| 256 | { |
|---|
| 257 | var txtNode = s.anchorNode; |
|---|
| 258 | var fnUrl = function() |
|---|
| 259 | { |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | m = txtNode.data.match(Xinha.RE_url); |
|---|
| 263 | if(m) |
|---|
| 264 | { |
|---|
| 265 | a.href = (m[1] ? m[1] : 'http://') + m[2]; |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | a._updateAnchTimeout = setTimeout(fnUrl, 250); |
|---|
| 275 | }; |
|---|
| 276 | a._updateAnchTimeout = setTimeout(fnUrl, 1000); |
|---|
| 277 | } |
|---|
| 278 | } |
|---|
| 279 | } |
|---|
| 280 | } |
|---|
| 281 | break; |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | return false; |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | Gecko.prototype.handleBackspace = function() |
|---|
| 292 | { |
|---|
| 293 | var editor = this.editor; |
|---|
| 294 | setTimeout( |
|---|
| 295 | function() |
|---|
| 296 | { |
|---|
| 297 | var sel = editor.getSelection(); |
|---|
| 298 | var range = editor.createRange(sel); |
|---|
| 299 | var SC = range.startContainer; |
|---|
| 300 | var SO = range.startOffset; |
|---|
| 301 | var EC = range.endContainer; |
|---|
| 302 | var EO = range.endOffset; |
|---|
| 303 | var newr = SC.nextSibling; |
|---|
| 304 | if ( SC.nodeType == 3 ) |
|---|
| 305 | { |
|---|
| 306 | SC = SC.parentNode; |
|---|
| 307 | } |
|---|
| 308 | if ( ! ( /\S/.test(SC.tagName) ) ) |
|---|
| 309 | { |
|---|
| 310 | var p = document.createElement("p"); |
|---|
| 311 | while ( SC.firstChild ) |
|---|
| 312 | { |
|---|
| 313 | p.appendChild(SC.firstChild); |
|---|
| 314 | } |
|---|
| 315 | SC.parentNode.insertBefore(p, SC); |
|---|
| 316 | Xinha.removeFromParent(SC); |
|---|
| 317 | var r = range.cloneRange(); |
|---|
| 318 | r.setStartBefore(newr); |
|---|
| 319 | r.setEndAfter(newr); |
|---|
| 320 | r.extractContents(); |
|---|
| 321 | sel.removeAllRanges(); |
|---|
| 322 | sel.addRange(r); |
|---|
| 323 | } |
|---|
| 324 | }, |
|---|
| 325 | 10); |
|---|
| 326 | }; |
|---|
| 327 | |
|---|
| 328 | Gecko.prototype.inwardHtml = function(html) |
|---|
| 329 | { |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | html = html.replace(/<(\/?)strong(\s|>|\/)/ig, "<$1b$2"); |
|---|
| 333 | html = html.replace(/<(\/?)em(\s|>|\/)/ig, "<$1i$2"); |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | |
|---|
| 337 | html = html.replace(/<(\/?)del(\s|>|\/)/ig, "<$1strike$2"); |
|---|
| 338 | |
|---|
| 339 | return html; |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | Gecko.prototype.outwardHtml = function(html) |
|---|
| 343 | { |
|---|
| 344 | |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | html = html.replace(/<script[\s]*src[\s]*=[\s]*['"]chrome:\/\/.*?["']>[\s]*<\/script>/ig, ''); |
|---|
| 348 | |
|---|
| 349 | return html; |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | Gecko.prototype.onExecCommand = function(cmdID, UI, param) |
|---|
| 353 | { |
|---|
| 354 | try |
|---|
| 355 | { |
|---|
| 356 | |
|---|
| 357 | this.editor._doc.execCommand('useCSS', false, true); |
|---|
| 358 | this.editor._doc.execCommand('styleWithCSS', false, false); |
|---|
| 359 | } catch (ex) {} |
|---|
| 360 | |
|---|
| 361 | switch(cmdID) |
|---|
| 362 | { |
|---|
| 363 | case 'paste': |
|---|
| 364 | { |
|---|
| 365 | alert(Xinha._lc("The Paste button does not work in Mozilla based web browsers (technical security reasons). Press CTRL-V on your keyboard to paste directly.")); |
|---|
| 366 | return true; |
|---|
| 367 | } |
|---|
| 368 | break; |
|---|
| 369 | case 'removeformat': |
|---|
| 370 | var editor = this.editor; |
|---|
| 371 | var sel = editor.getSelection(); |
|---|
| 372 | var selSave = editor.saveSelection(sel); |
|---|
| 373 | var range = editor.createRange(sel); |
|---|
| 374 | |
|---|
| 375 | var els = editor._doc.body.getElementsByTagName('*'); |
|---|
| 376 | |
|---|
| 377 | var start = ( range.startContainer.nodeType == 1 ) ? range.startContainer : range.startContainer.parentNode; |
|---|
| 378 | var i, el; |
|---|
| 379 | if (sel.isCollapsed) range.selectNodeContents(editor._doc.body); |
|---|
| 380 | |
|---|
| 381 | for (i=0; i<els.length;i++) |
|---|
| 382 | { |
|---|
| 383 | el = els[i]; |
|---|
| 384 | if ( range.isPointInRange(el, 0) || (els[i] == start && range.startOffset == 0)) |
|---|
| 385 | { |
|---|
| 386 | el.removeAttribute('style'); |
|---|
| 387 | } |
|---|
| 388 | } |
|---|
| 389 | this.editor._doc.execCommand(cmdID, UI, param); |
|---|
| 390 | editor.restoreSelection(selSave); |
|---|
| 391 | return true; |
|---|
| 392 | break; |
|---|
| 393 | } |
|---|
| 394 | |
|---|
| 395 | return false; |
|---|
| 396 | } |
|---|
| 397 | Gecko.prototype.onMouseDown = function(ev) |
|---|
| 398 | { |
|---|
| 399 | |
|---|
| 400 | if (ev.target.tagName.toLowerCase() == "hr") |
|---|
| 401 | { |
|---|
| 402 | var sel = this.editor.getSelection(); |
|---|
| 403 | var range = this.editor.createRange(sel); |
|---|
| 404 | range.selectNode(ev.target); |
|---|
| 405 | } |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | |
|---|
| 409 | |
|---|
| 410 | |
|---|
| 411 | |
|---|
| 412 | |
|---|
| 413 | |
|---|
| 414 | |
|---|
| 415 | |
|---|
| 416 | |
|---|
| 417 | Xinha.prototype.insertNodeAtSelection = function(toBeInserted) |
|---|
| 418 | { |
|---|
| 419 | if ( toBeInserted.ownerDocument != this._doc ) |
|---|
| 420 | { |
|---|
| 421 | try |
|---|
| 422 | { |
|---|
| 423 | toBeInserted = this._doc.adoptNode( toBeInserted ); |
|---|
| 424 | } catch (e) {} |
|---|
| 425 | } |
|---|
| 426 | var sel = this.getSelection(); |
|---|
| 427 | var range = this.createRange(sel); |
|---|
| 428 | |
|---|
| 429 | sel.removeAllRanges(); |
|---|
| 430 | range.deleteContents(); |
|---|
| 431 | var node = range.startContainer; |
|---|
| 432 | var pos = range.startOffset; |
|---|
| 433 | var selnode = toBeInserted; |
|---|
| 434 | |
|---|
| 435 | switch ( node.nodeType ) |
|---|
| 436 | { |
|---|
| 437 | case 3: |
|---|
| 438 | |
|---|
| 439 | if ( toBeInserted.nodeType == 3 ) |
|---|
| 440 | { |
|---|
| 441 | |
|---|
| 442 | node.insertData(pos, toBeInserted.data); |
|---|
| 443 | range = this.createRange(); |
|---|
| 444 | range.setEnd(node, pos + toBeInserted.length); |
|---|
| 445 | range.setStart(node, pos + toBeInserted.length); |
|---|
| 446 | sel.addRange(range); |
|---|
| 447 | } |
|---|
| 448 | else |
|---|
| 449 | { |
|---|
| 450 | node = node.splitText(pos); |
|---|
| 451 | if ( toBeInserted.nodeType == 11 ) |
|---|
| 452 | { |
|---|
| 453 | selnode = selnode.firstChild; |
|---|
| 454 | } |
|---|
| 455 | node.parentNode.insertBefore(toBeInserted, node); |
|---|
| 456 | this.selectNodeContents(selnode); |
|---|
| 457 | this.updateToolbar(); |
|---|
| 458 | } |
|---|
| 459 | break; |
|---|
| 460 | case 1: |
|---|
| 461 | if ( toBeInserted.nodeType == 11 ) |
|---|
| 462 | { |
|---|
| 463 | selnode = selnode.firstChild; |
|---|
| 464 | } |
|---|
| 465 | node.insertBefore(toBeInserted, node.childNodes[pos]); |
|---|
| 466 | this.selectNodeContents(selnode); |
|---|
| 467 | this.updateToolbar(); |
|---|
| 468 | break; |
|---|
| 469 | } |
|---|
| 470 | }; |
|---|
| 471 | |
|---|
| 472 | |
|---|
| 473 | |
|---|
| 474 | |
|---|
| 475 | |
|---|
| 476 | |
|---|
| 477 | Xinha.prototype.getParentElement = function(sel) |
|---|
| 478 | { |
|---|
| 479 | if ( typeof sel == 'undefined' ) |
|---|
| 480 | { |
|---|
| 481 | sel = this.getSelection(); |
|---|
| 482 | } |
|---|
| 483 | var range = this.createRange(sel); |
|---|
| 484 | try |
|---|
| 485 | { |
|---|
| 486 | var p = range.commonAncestorContainer; |
|---|
| 487 | if ( !range.collapsed && range.startContainer == range.endContainer && |
|---|
| 488 | range.startOffset - range.endOffset <= 1 && range.startContainer.hasChildNodes() ) |
|---|
| 489 | { |
|---|
| 490 | p = range.startContainer.childNodes[range.startOffset]; |
|---|
| 491 | } |
|---|
| 492 | |
|---|
| 493 | while ( p.nodeType == 3 ) |
|---|
| 494 | { |
|---|
| 495 | p = p.parentNode; |
|---|
| 496 | } |
|---|
| 497 | return p; |
|---|
| 498 | } |
|---|
| 499 | catch (ex) |
|---|
| 500 | { |
|---|
| 501 | return null; |
|---|
| 502 | } |
|---|
| 503 | }; |
|---|
| 504 | |
|---|
| 505 | |
|---|
| 506 | |
|---|
| 507 | |
|---|
| 508 | |
|---|
| 509 | |
|---|
| 510 | |
|---|
| 511 | |
|---|
| 512 | |
|---|
| 513 | Xinha.prototype.activeElement = function(sel) |
|---|
| 514 | { |
|---|
| 515 | if ( ( sel === null ) || this.selectionEmpty(sel) ) |
|---|
| 516 | { |
|---|
| 517 | return null; |
|---|
| 518 | } |
|---|
| 519 | |
|---|
| 520 | |
|---|
| 521 | |
|---|
| 522 | |
|---|
| 523 | if ( !sel.isCollapsed ) |
|---|
| 524 | { |
|---|
| 525 | if ( sel.anchorNode.childNodes.length > sel.anchorOffset && sel.anchorNode.childNodes[sel.anchorOffset].nodeType == 1 ) |
|---|
| 526 | { |
|---|
| 527 | return sel.anchorNode.childNodes[sel.anchorOffset]; |
|---|
| 528 | } |
|---|
| 529 | else if ( sel.anchorNode.nodeType == 1 ) |
|---|
| 530 | { |
|---|
| 531 | return sel.anchorNode; |
|---|
| 532 | } |
|---|
| 533 | else |
|---|
| 534 | { |
|---|
| 535 | return null; |
|---|
| 536 | } |
|---|
| 537 | } |
|---|
| 538 | return null; |
|---|
| 539 | }; |
|---|
| 540 | |
|---|
| 541 | |
|---|
| 542 | |
|---|
| 543 | |
|---|
| 544 | |
|---|
| 545 | |
|---|
| 546 | |
|---|
| 547 | Xinha.prototype.selectionEmpty = function(sel) |
|---|
| 548 | { |
|---|
| 549 | if ( !sel ) |
|---|
| 550 | { |
|---|
| 551 | return true; |
|---|
| 552 | } |
|---|
| 553 | |
|---|
| 554 | if ( typeof sel.isCollapsed != 'undefined' ) |
|---|
| 555 | { |
|---|
| 556 | return sel.isCollapsed; |
|---|
| 557 | } |
|---|
| 558 | |
|---|
| 559 | return true; |
|---|
| 560 | }; |
|---|
| 561 | |
|---|
| 562 | |
|---|
| 563 | |
|---|
| 564 | |
|---|
| 565 | |
|---|
| 566 | |
|---|
| 567 | |
|---|
| 568 | Xinha.prototype.saveSelection = function() |
|---|
| 569 | { |
|---|
| 570 | return this.createRange(this.getSelection()).cloneRange(); |
|---|
| 571 | } |
|---|
| 572 | |
|---|
| 573 | |
|---|
| 574 | |
|---|
| 575 | |
|---|
| 576 | Xinha.prototype.restoreSelection = function(savedSelection) |
|---|
| 577 | { |
|---|
| 578 | try |
|---|
| 579 | { |
|---|
| 580 | var sel = this.getSelection(); |
|---|
| 581 | sel.removeAllRanges(); |
|---|
| 582 | sel.addRange(savedSelection); |
|---|
| 583 | } |
|---|
| 584 | catch (e) {} |
|---|
| 585 | } |
|---|
| 586 | |
|---|
| 587 | |
|---|
| 588 | |
|---|
| 589 | |
|---|
| 590 | |
|---|
| 591 | |
|---|
| 592 | |
|---|
| 593 | |
|---|
| 594 | Xinha.prototype.selectNodeContents = function(node, collapseToStart) |
|---|
| 595 | { |
|---|
| 596 | this.focusEditor(); |
|---|
| 597 | this.forceRedraw(); |
|---|
| 598 | var range; |
|---|
| 599 | var collapsed = typeof collapseToStart == "undefined" ? true : false; |
|---|
| 600 | var sel = this.getSelection(); |
|---|
| 601 | range = this._doc.createRange(); |
|---|
| 602 | if ( !node ) |
|---|
| 603 | { |
|---|
| 604 | sel.removeAllRanges(); |
|---|
| 605 | return; |
|---|
| 606 | } |
|---|
| 607 | |
|---|
| 608 | if ( collapsed && node.tagName && node.tagName.toLowerCase().match(/table|img|input|textarea|select/) ) |
|---|
| 609 | { |
|---|
| 610 | range.selectNode(node); |
|---|
| 611 | } |
|---|
| 612 | else |
|---|
| 613 | { |
|---|
| 614 | range.selectNodeContents(node); |
|---|
| 615 | } |
|---|
| 616 | sel.removeAllRanges(); |
|---|
| 617 | sel.addRange(range); |
|---|
| 618 | if (typeof collapseToStart != "undefined") |
|---|
| 619 | { |
|---|
| 620 | if (collapseToStart) |
|---|
| 621 | { |
|---|
| 622 | sel.collapse(range.startContainer, range.startOffset); |
|---|
| 623 | } else |
|---|
| 624 | { |
|---|
| 625 | sel.collapse(range.endContainer, range.endOffset); |
|---|
| 626 | } |
|---|
| 627 | } |
|---|
| 628 | }; |
|---|
| 629 | |
|---|
| 630 | |
|---|
| 631 | |
|---|
| 632 | |
|---|
| 633 | |
|---|
| 634 | |
|---|
| 635 | Xinha.prototype.insertHTML = function(html) |
|---|
| 636 | { |
|---|
| 637 | var sel = this.getSelection(); |
|---|
| 638 | var range = this.createRange(sel); |
|---|
| 639 | this.focusEditor(); |
|---|
| 640 | |
|---|
| 641 | var fragment = this._doc.createDocumentFragment(); |
|---|
| 642 | var div = this._doc.createElement("div"); |
|---|
| 643 | div.innerHTML = html; |
|---|
| 644 | while ( div.firstChild ) |
|---|
| 645 | { |
|---|
| 646 | |
|---|
| 647 | fragment.appendChild(div.firstChild); |
|---|
| 648 | } |
|---|
| 649 | |
|---|
| 650 | var node = this.insertNodeAtSelection(fragment); |
|---|
| 651 | }; |
|---|
| 652 | |
|---|
| 653 | |
|---|
| 654 | |
|---|
| 655 | |
|---|
| 656 | |
|---|
| 657 | |
|---|
| 658 | Xinha.prototype.getSelectedHTML = function() |
|---|
| 659 | { |
|---|
| 660 | var sel = this.getSelection(); |
|---|
| 661 | if (sel.isCollapsed) return ''; |
|---|
| 662 | var range = this.createRange(sel); |
|---|
| 663 | return Xinha.getHTML(range.cloneContents(), false, this); |
|---|
| 664 | }; |
|---|
| 665 | |
|---|
| 666 | |
|---|
| 667 | |
|---|
| 668 | |
|---|
| 669 | |
|---|
| 670 | |
|---|
| 671 | |
|---|
| 672 | Xinha.prototype.getSelection = function() |
|---|
| 673 | { |
|---|
| 674 | return this._iframe.contentWindow.getSelection(); |
|---|
| 675 | }; |
|---|
| 676 | |
|---|
| 677 | |
|---|
| 678 | |
|---|
| 679 | |
|---|
| 680 | |
|---|
| 681 | |
|---|
| 682 | |
|---|
| 683 | Xinha.prototype.createRange = function(sel) |
|---|
| 684 | { |
|---|
| 685 | this.activateEditor(); |
|---|
| 686 | if ( typeof sel != "undefined" ) |
|---|
| 687 | { |
|---|
| 688 | try |
|---|
| 689 | { |
|---|
| 690 | return sel.getRangeAt(0); |
|---|
| 691 | } |
|---|
| 692 | catch(ex) |
|---|
| 693 | { |
|---|
| 694 | return this._doc.createRange(); |
|---|
| 695 | } |
|---|
| 696 | } |
|---|
| 697 | else |
|---|
| 698 | { |
|---|
| 699 | return this._doc.createRange(); |
|---|
| 700 | } |
|---|
| 701 | }; |
|---|
| 702 | |
|---|
| 703 | |
|---|
| 704 | |
|---|
| 705 | |
|---|
| 706 | |
|---|
| 707 | |
|---|
| 708 | |
|---|
| 709 | Xinha.prototype.isKeyEvent = function(event) |
|---|
| 710 | { |
|---|
| 711 | return event.type == "keypress"; |
|---|
| 712 | } |
|---|
| 713 | |
|---|
| 714 | |
|---|
| 715 | |
|---|
| 716 | |
|---|
| 717 | |
|---|
| 718 | |
|---|
| 719 | |
|---|
| 720 | |
|---|
| 721 | Xinha.prototype.getKey = function(keyEvent) |
|---|
| 722 | { |
|---|
| 723 | return String.fromCharCode(keyEvent.charCode); |
|---|
| 724 | } |
|---|
| 725 | |
|---|
| 726 | |
|---|
| 727 | |
|---|
| 728 | |
|---|
| 729 | |
|---|
| 730 | |
|---|
| 731 | |
|---|
| 732 | Xinha.getOuterHTML = function(element) |
|---|
| 733 | { |
|---|
| 734 | return (new XMLSerializer()).serializeToString(element); |
|---|
| 735 | }; |
|---|
| 736 | |
|---|
| 737 | |
|---|
| 738 | Xinha.cc = String.fromCharCode(8286); |
|---|
| 739 | |
|---|
| 740 | Xinha.prototype.setCC = function ( target ) |
|---|
| 741 | { |
|---|
| 742 | var cc = Xinha.cc; |
|---|
| 743 | try |
|---|
| 744 | { |
|---|
| 745 | if ( target == "textarea" ) |
|---|
| 746 | { |
|---|
| 747 | var ta = this._textArea; |
|---|
| 748 | var index = ta.selectionStart; |
|---|
| 749 | var before = ta.value.substring( 0, index ) |
|---|
| 750 | var after = ta.value.substring( index, ta.value.length ); |
|---|
| 751 | |
|---|
| 752 | if ( after.match(/^[^<]*>/) ) |
|---|
| 753 | { |
|---|
| 754 | var tagEnd = after.indexOf(">") + 1; |
|---|
| 755 | ta.value = before + after.substring( 0, tagEnd ) + cc + after.substring( tagEnd, after.length ); |
|---|
| 756 | } |
|---|
| 757 | else ta.value = before + cc + after; |
|---|
| 758 | ta.value = ta.value.replace(new RegExp ('(&[^'+cc+';]*?)('+cc+')([^'+cc+']*?;)'), "$1$3$2"); |
|---|
| 759 | ta.value = ta.value.replace(new RegExp ('(<script[^>]*>[^'+cc+']*?)('+cc+')([^'+cc+']*?<\/script>)'), "$1$3$2"); |
|---|
| 760 | ta.value = ta.value.replace(new RegExp ('^([^'+cc+']*)('+cc+')([^'+cc+']*<body[^>]*>)(.*?)'), "$1$3$2$4"); |
|---|
| 761 | } |
|---|
| 762 | else |
|---|
| 763 | { |
|---|
| 764 | var sel = this.getSelection(); |
|---|
| 765 | sel.getRangeAt(0).insertNode( this._doc.createTextNode( cc ) ); |
|---|
| 766 | } |
|---|
| 767 | } catch (e) {} |
|---|
| 768 | }; |
|---|
| 769 | |
|---|
| 770 | Xinha.prototype.findCC = function ( target ) |
|---|
| 771 | { |
|---|
| 772 | if ( target == 'textarea' ) |
|---|
| 773 | { |
|---|
| 774 | var ta = this._textArea; |
|---|
| 775 | var pos = ta.value.indexOf( Xinha.cc ); |
|---|
| 776 | if ( pos == -1 ) return; |
|---|
| 777 | var end = pos + Xinha.cc.length; |
|---|
| 778 | var before = ta.value.substring( 0, pos ); |
|---|
| 779 | var after = ta.value.substring( end, ta.value.length ); |
|---|
| 780 | ta.value = before ; |
|---|
| 781 | |
|---|
| 782 | ta.scrollTop = ta.scrollHeight; |
|---|
| 783 | var scrollPos = ta.scrollTop; |
|---|
| 784 | |
|---|
| 785 | ta.value += after; |
|---|
| 786 | ta.setSelectionRange(pos,pos); |
|---|
| 787 | |
|---|
| 788 | ta.focus(); |
|---|
| 789 | |
|---|
| 790 | ta.scrollTop = scrollPos; |
|---|
| 791 | |
|---|
| 792 | } |
|---|
| 793 | else |
|---|
| 794 | { |
|---|
| 795 | try |
|---|
| 796 | { |
|---|
| 797 | var doc = this._doc; |
|---|
| 798 | doc.body.innerHTML = doc.body.innerHTML.replace(new RegExp(Xinha.cc),'<span id="XinhaEditingPostion"></span>'); |
|---|
| 799 | var posEl = doc.getElementById('XinhaEditingPostion'); |
|---|
| 800 | this.selectNodeContents(posEl); |
|---|
| 801 | this.scrollToElement(posEl); |
|---|
| 802 | posEl.parentNode.removeChild(posEl); |
|---|
| 803 | |
|---|
| 804 | this._iframe.contentWindow.focus(); |
|---|
| 805 | } catch (e) {} |
|---|
| 806 | } |
|---|
| 807 | }; |
|---|
| 808 | |
|---|
| 809 | |
|---|
| 810 | |
|---|
| 811 | |
|---|
| 812 | Xinha.prototype._standardToggleBorders = Xinha.prototype._toggleBorders; |
|---|
| 813 | Xinha.prototype._toggleBorders = function() |
|---|
| 814 | { |
|---|
| 815 | var result = this._standardToggleBorders(); |
|---|
| 816 | |
|---|
| 817 | |
|---|
| 818 | var tables = this._doc.getElementsByTagName('TABLE'); |
|---|
| 819 | for(var i = 0; i < tables.length; i++) |
|---|
| 820 | { |
|---|
| 821 | tables[i].style.display="none"; |
|---|
| 822 | tables[i].style.display="table"; |
|---|
| 823 | } |
|---|
| 824 | |
|---|
| 825 | return result; |
|---|
| 826 | }; |
|---|
| 827 | |
|---|
| 828 | |
|---|
| 829 | |
|---|
| 830 | |
|---|
| 831 | |
|---|
| 832 | |
|---|
| 833 | Xinha.getDoctype = function (doc) |
|---|
| 834 | { |
|---|
| 835 | var d = ''; |
|---|
| 836 | if (doc.doctype) |
|---|
| 837 | { |
|---|
| 838 | d += '<!DOCTYPE ' + doc.doctype.name + " PUBLIC "; |
|---|
| 839 | d += doc.doctype.publicId ? '"' + doc.doctype.publicId + '"' : ''; |
|---|
| 840 | d += doc.doctype.systemId ? ' "'+ doc.doctype.systemId + '"' : ''; |
|---|
| 841 | d += ">"; |
|---|
| 842 | } |
|---|
| 843 | return d; |
|---|
| 844 | }; |
|---|