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