Ticket #2909: 2909.patch
File 2909.patch, 8.6 KB (added by , 15 years ago) |
---|
-
_source/core/dom/document.js
87 87 return $ ? new CKEDITOR.dom.element( $ ) : null; 88 88 }, 89 89 90 getByAddress : function( address, normalized ) 91 { 92 var $ = this.$.documentElement; 93 94 for ( var i = 0 ; $ && i < address.length ; i++ ) 95 { 96 var target = address[ i ]; 97 98 if ( !normalized ) 99 { 100 $ = $.childNodes[ target ]; 101 continue; 102 } 103 104 var currentIndex = -1; 105 106 for (var j = 0 ; j < $.childNodes.length ; j++ ) 107 { 108 var candidate = $.childNodes[ j ]; 109 110 if ( normalized === true && 111 candidate.nodeType == 3 && 112 candidate.previousSibling && 113 candidate.previousSibling.nodeType == 3 ) 114 { 115 continue; 116 } 117 118 currentIndex++; 119 120 if ( currentIndex == target ) 121 { 122 $ = candidate; 123 break; 124 } 125 } 126 } 127 128 return $ ? new CKEDITOR.dom.node( $ ) : null; 129 }, 130 90 131 /** 91 132 * Gets the <head> element for this document. 92 133 * @returns {CKEDITOR.dom.element} The <head> element. -
_source/core/dom/node.js
149 149 }, 150 150 151 151 /** 152 * Retrieves a uniquely identifiable tree address for this node. 153 * The tree address returns is an array of integers, with each integer 154 * indicating a child index of a DOM node, starting from 155 * document.documentElement. 156 * 157 * For example, assuming <body> is the second child from <html> (<head> 158 * being the first), and we'd like to address the third child under the 159 * fourth child of body, the tree address returned would be: 160 * [1, 3, 2] 161 * 162 * The tree address cannot be used for finding back the DOM tree node once 163 * the DOM tree structure has been modified. 164 */ 165 getAddress : function( normalized ) 166 { 167 var address = []; 168 var $documentElement = this.getDocument().$.documentElement; 169 var node = this.$; 170 171 while ( node && node != $documentElement ) 172 { 173 var parentNode = node.parentNode; 174 var currentIndex = -1; 175 176 for ( var i = 0 ; i < parentNode.childNodes.length ; i++ ) 177 { 178 var candidate = parentNode.childNodes[i]; 179 180 if ( normalized && 181 candidate.nodeType == 3 && 182 candidate.previousSibling && 183 candidate.previousSibling.nodeType == 3 ) 184 { 185 continue; 186 } 187 188 currentIndex++; 189 190 if ( candidate == node ) 191 break; 192 } 193 194 address.unshift( currentIndex ); 195 196 node = node.parentNode; 197 } 198 199 return address; 200 }, 201 202 /** 152 203 * Gets a DOM tree descendant under the current node. 153 204 * @param {Array|Number} indices The child index or array of child indices under the node. 154 205 * @returns {CKEDITOR.dom.node} The specified DOM child under the current node. Null if child does not exist. … … 511 562 if ( child && child.type == 1 && child.nodeName.toLowerCase() == 'br' ) 512 563 { 513 564 // Use "eChildNode.parentNode" instead of "node" to avoid IE bug (#324). 514 child.parentNode.removeChild( child ) 565 child.parentNode.removeChild( child ); 515 566 } 516 567 } 517 568 } -
_source/core/dom/range.js
429 429 this.collapse( true ); 430 430 }, 431 431 432 createBookmark2 : function() 433 { 434 var startContainer = this.startContainer, 435 endContainer = this.endContainer; 436 437 var startOffset = this.startOffset, 438 endOffset = this.endOffset; 439 440 var previous; 441 442 // If there is no range then get out of here. 443 // It happens on initial load in Safari #962 and if the editor it's 444 // hidden also in Firefox 445 if ( !startContainer || !endContainer ) 446 return { start : 0, end : 0 }; 447 448 // Normalize the start. 449 while ( startContainer.type == CKEDITOR.NODE_TEXT 450 && ( previous = startContainer.getPrevious() ) 451 && previous.type == CKEDITOR.NODE_TEXT ) 452 { 453 startContainer = previous; 454 startOffset += previous.getLength(); 455 } 456 457 // Normalize the end. 458 if ( !this.isCollapsed ) 459 { 460 while ( endContainer.type == CKEDITOR.NODE_TEXT 461 && ( previous = endContainer.getPrevious() ) 462 && previous.type == CKEDITOR.NODE_TEXT ) 463 { 464 endContainer = previous; 465 endOffset += previous.getLength(); 466 } 467 } 468 469 return { 470 start : startContainer.getAddress( true ), 471 end : this.isCollapsed ? null : endContainer.getAddress( true ), 472 startOffset : startOffset, 473 endOffset : endOffset 474 }; 475 }, 476 477 moveToBookmark2 : function( bookmark ) 478 { 479 var startContainer = this.document.getByAddress( bookmark.start, true ), 480 endContainer = bookmark.end && this.document.getByAddress( bookmark.end, true ); 481 482 var startOffset = bookmark.startOffset, 483 endOffset = bookmark.endOffset; 484 485 var next; 486 487 // As we have normalized text containers when creating the bookmark, 488 // we need now to find the proper container. 489 490 while ( startContainer.type == CKEDITOR.NODE_TEXT 491 && startOffset > startContainer.getLength() 492 && ( next = startContainer.getNext() ) 493 && next.type == CKEDITOR.NODE_TEXT ) 494 { 495 startOffset -= startContainer.length; 496 startContainer = next; 497 } 498 499 this.setStart( startContainer, startOffset ); 500 501 if ( endContainer ) 502 { 503 while ( endContainer.type == CKEDITOR.NODE_TEXT 504 && endOffset > endContainer.getLength() 505 && ( next = endContainer.getNext() ) 506 && next.type == CKEDITOR.NODE_TEXT ) 507 { 508 endOffset -= endContainer.length; 509 endContainer = next; 510 } 511 512 this.setEnd( endContainer, endOffset ); 513 } 514 else 515 this.collapse( true ); 516 }, 517 432 518 getBoundaryNodes : function() 433 519 { 434 520 var startNode = this.startContainer, -
_source/tests/core/dom/range.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 3 <head> 4 4 <title>CKEDITOR.dom.range</title> … … 11 11 //<![CDATA[ 12 12 13 13 var html1, html2; 14 var tests; 14 15 15 16 CKEDITOR.test.addTestCase( (function() 16 17 { … … 20 21 21 22 var doc = new CKEDITOR.dom.document( document ); 22 23 23 return {24 return tests = { 24 25 test__constructor : function() 25 26 { 26 27 var range = new CKEDITOR.dom.range( doc ); … … 1510 1511 assert.isFalse( range.collapsed, 'range.collapsed' ); 1511 1512 }, 1512 1513 1514 test_createBookmark2_1 : function() 1515 { 1516 doc.getById( 'playground' ).setHtml( '<p id="P">This is <b id="B">a test</b></p>' ); 1517 1518 var range = new CKEDITOR.dom.range( doc ); 1519 1520 range.setStart( doc.getById( 'P' ), 0 ); 1521 range.setEnd( doc.getById( 'B' ).getFirst(), 3 ); 1522 1523 var bookmark = range.createBookmark2(); 1524 1525 range = new CKEDITOR.dom.range( doc ); 1526 range.moveToBookmark2( bookmark ); 1527 1528 assert.areSame( document.getElementById('P'), range.startContainer.$, 'range.startContainer' ); 1529 assert.areSame( 0, range.startOffset, 'range.startOffset' ); 1530 assert.areSame( document.getElementById('B').firstChild, range.endContainer.$, 'range.endContainer' ); 1531 assert.areSame( 3, range.endOffset, 'range.endOffset' ); 1532 assert.isFalse( range.collapsed, 'range.collapsed' ); 1533 }, 1534 1513 1535 ///////////// 1514 1536 1515 1537 setUp : function() … … 1524 1546 1525 1547 //window.onload = function() 1526 1548 //{ 1527 // // Local references. 1528 // var assert = CKEDITOR.test.assert; 1529 // var getInnerHtml = CKEDITOR.test.getInnerHtml; 1530 1531 // var doc = new CKEDITOR.dom.document( document ); 1532 1533 // doc.getById( '_EnlargeP' ).setHtml( 'this <i>is some </i>sample text' ); 1534 1535 // var range = new CKEDITOR.dom.range( doc ); 1536 // range.setStart( doc.getById( '_EnlargeP' ), 0 ); 1537 // range.setEnd( doc.getById( '_EnlargeP' ).getChild( 1 ), 0 ); 1538 1539 // range.enlarge( CKEDITOR.ENLARGE_ELEMENT ); 1549 // tests.test_createBookmark2_1(); 1540 1550 //} 1541 1551 1542 1552 //]]>