| | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| | 2 | <html xmlns="http://www.w3.org/1999/xhtml"> |
| | 3 | <head> |
| | 4 | <title>Plugin: selection</title> |
| | 5 | <link rel="stylesheet" type="text/css" href="../../test.css" /> |
| | 6 | <script type="text/javascript" src="../../../../ckeditor_source.js"></script> |
| | 7 | <script type="text/javascript" src="../../test.js"></script> |
| | 8 | <script type="text/javascript"> |
| | 9 | //<![CDATA[ |
| | 10 | |
| | 11 | CKEDITOR.plugins.load( 'selection' ); |
| | 12 | |
| | 13 | /** |
| | 14 | * Helper to determine whether two {@link CKEDITOR.dom.range}'s equality. |
| | 15 | */ |
| | 16 | function rangesAreSame() |
| | 17 | { |
| | 18 | var range1 = arguments[ 0 ], range2 = arguments[ 1 ]; |
| | 19 | |
| | 20 | if ( ! ( range1 && range2 ) ) |
| | 21 | return false; |
| | 22 | |
| | 23 | if ( range1.collapsed && range2.collapsed ) |
| | 24 | return ( range1.startContainer.$ == range2.startContainer.$ |
| | 25 | && range1.startOffset.$ == range2.startOffset.$ ) |
| | 26 | || ( range1.endContainer.$ == range2.endContainer.$ |
| | 27 | && range1.endOffset.$ == range2.endOffset.$ ); |
| | 28 | |
| | 29 | return range1.startContainer.$ == range2.startContainer.$ |
| | 30 | && range1.startOffset == range2.startOffset |
| | 31 | && range1.endContainer.$ == range2.endContainer.$ |
| | 32 | && range1.endOffset == range2.endOffset; |
| | 33 | |
| | 34 | } |
| | 35 | |
| | 36 | var testCase; |
| | 37 | |
| | 38 | CKEDITOR.test |
| | 39 | .addTestCase( ( function() |
| | 40 | { |
| | 41 | // Local references. |
| | 42 | var assert = YAHOO.util.Assert, |
| | 43 | doc = new CKEDITOR.dom.document( document ); |
| | 44 | |
| | 45 | return { |
| | 46 | |
| | 47 | /** |
| | 48 | * Test no range when document was leaved unselected. |
| | 49 | */ |
| | 50 | test_selection1 : function() |
| | 51 | { |
| | 52 | var sel = new CKEDITOR.dom.selection( doc ); |
| | 53 | var nativeSel = sel.getNative(); |
| | 54 | assert.isNotNull( nativeSel, |
| | 55 | 'Native selection should not null.' ); |
| | 56 | |
| | 57 | assert.areSame( CKEDITOR.SELECTION_NONE, sel.getType(), |
| | 58 | 'Selection type should be "none".' ); |
| | 59 | |
| | 60 | var range = sel.getRanges()[ 0 ]; |
| | 61 | |
| | 62 | // IE collpased the range at the position right after the first |
| | 63 | // element in body when no selection is made. |
| | 64 | assert.isUndefined( range, 'range should not existing.' ); |
| | 65 | }, |
| | 66 | |
| | 67 | /** |
| | 68 | * Try a normal selection |
| | 69 | */ |
| | 70 | test_selection2 : function() |
| | 71 | { |
| | 72 | var sel = new CKEDITOR.dom.selection( doc ); |
| | 73 | var range = new CKEDITOR.dom.range( doc ); |
| | 74 | range.selectNodeContents( doc.getById( 'selectedNode' ) ); |
| | 75 | sel.selectRanges( [ range ] ); |
| | 76 | assert.areSame( CKEDITOR.SELECTION_TEXT, sel.getType(), |
| | 77 | 'Selection type should be "text".' ); |
| | 78 | |
| | 79 | var selectedRange = sel.getRanges()[ 0 ]; |
| | 80 | assert.isTrue( rangesAreSame( range, selectedRange ), |
| | 81 | 'Result range should be the same.' ); |
| | 82 | }, |
| | 83 | |
| | 84 | /** |
| | 85 | * Try to make selection when document is blurred already. |
| | 86 | */ |
| | 87 | test_selection3 : function() |
| | 88 | { |
| | 89 | var range = new CKEDITOR.dom.range( doc ); |
| | 90 | range.selectNodeContents( doc.getById( 'selectedNode' ) ); |
| | 91 | |
| | 92 | window.blur(); |
| | 93 | |
| | 94 | var sel = new CKEDITOR.dom.selection( doc ); |
| | 95 | sel.selectRanges( [ range ] ); |
| | 96 | assert.areSame( CKEDITOR.SELECTION_TEXT, sel.getType(), |
| | 97 | 'Selection type should be "text".' ); |
| | 98 | var selectedRange = sel.getRanges()[ 0 ], |
| | 99 | selectedElement = sel.getSelectedElement(), |
| | 100 | firstElement = sel.getStartElement(); |
| | 101 | |
| | 102 | assert.isNull( selectedElement, |
| | 103 | 'Selected element should not exist.' ); |
| | 104 | assert.areSame( doc.getById( 'selectedNode' ).$, |
| | 105 | firstElement && firstElement.$, 'Start element doesn\'t match.' ); |
| | 106 | assert.isTrue( rangesAreSame( range, selectedRange ), |
| | 107 | 'Result range should be the same.' ); |
| | 108 | |
| | 109 | document.body.focus(); |
| | 110 | }, |
| | 111 | |
| | 112 | /** |
| | 113 | * Selecting text node |
| | 114 | */ |
| | 115 | test_getSelectedElement1 : function() |
| | 116 | { |
| | 117 | var range = new CKEDITOR.dom.range( doc ); |
| | 118 | range.selectNodeContents( doc.getById( 'selectedNode' ) ); |
| | 119 | var sel = new CKEDITOR.dom.selection( doc ); |
| | 120 | sel.selectRanges( [ range ] ); |
| | 121 | var selectedElement = sel.getSelectedElement(), |
| | 122 | firstElement = sel.getStartElement(); |
| | 123 | |
| | 124 | assert.isNull( selectedElement, |
| | 125 | 'Selected element should not exist.' ); |
| | 126 | assert.areSame( firstElement.$, |
| | 127 | doc.getById( 'selectedNode' ).$, |
| | 128 | 'Start element doesn\'t match.' ); |
| | 129 | }, |
| | 130 | |
| | 131 | /** |
| | 132 | * Selecting element |
| | 133 | */ |
| | 134 | test_getSelectedElement2 : function() |
| | 135 | { |
| | 136 | var i, l = 10; |
| | 137 | for ( i = 1 ; i <= l ; i++ ) |
| | 138 | { |
| | 139 | var element = doc.getById( 'selectedElement' + i ); |
| | 140 | if ( element ) |
| | 141 | { |
| | 142 | var range = new CKEDITOR.dom.range( doc ); |
| | 143 | range.setStartBefore( element ); |
| | 144 | range.setEndAfter( element ); |
| | 145 | var sel = new CKEDITOR.dom.selection( doc ); |
| | 146 | sel.selectRanges( [ range ] ); |
| | 147 | var selectedElement = sel.getSelectedElement(), |
| | 148 | firstElement = sel.getStartElement(); |
| | 149 | |
| | 150 | assert.areSame( element.$, selectedElement |
| | 151 | && selectedElement.$, |
| | 152 | 'Selected element ' + |
| | 153 | element.getName().toUpperCase() + ' doesn\'t match.' ); |
| | 154 | assert.areSame( element.$, firstElement |
| | 155 | && firstElement.$, 'Start element ' + |
| | 156 | element.getName() + 'doesn\'t match.' ); |
| | 157 | } |
| | 158 | } |
| | 159 | }, |
| | 160 | |
| | 161 | test_selectElement : function() |
| | 162 | { |
| | 163 | var i, l = 10; |
| | 164 | for ( i = 1 ; i <= l ; i++ ) |
| | 165 | { |
| | 166 | var element = doc.getById( 'selectedElement' + i ); |
| | 167 | if ( element ) |
| | 168 | { |
| | 169 | var sel = new CKEDITOR.dom.selection( doc ); |
| | 170 | sel.selectElement( element ); |
| | 171 | var selectedElement = sel.getSelectedElement(); |
| | 172 | |
| | 173 | assert.areSame( element.$, |
| | 174 | selectedElement && selectedElement.$, |
| | 175 | 'Selected element ' + |
| | 176 | element.getName().toUpperCase() + ' doesn\'t match.' ); |
| | 177 | } |
| | 178 | } |
| | 179 | }, |
| | 180 | name :document.title |
| | 181 | }; |
| | 182 | } )() ); |
| | 183 | |
| | 184 | //]]> |
| | 185 | </script> |
| | 186 | </head> |
| | 187 | <body> |
| | 188 | <span id="selectedNode">text1</span> |
| | 189 | <p> |
| | 190 | <a id="selectedElement1"></a> |
| | 191 | <input id="selectedElement2" /> |
| | 192 | <img id="selectedElement3" /> |
| | 193 | <hr id="selectedElement4"> |
| | 194 | </hr> |
| | 195 | <ul> |
| | 196 | <li id="selectedElement5"> |
| | 197 | </li> |
| | 198 | </ul> |
| | 199 | <table> |
| | 200 | <tr> |
| | 201 | <td id="selectedElement6"> |
| | 202 | </td> |
| | 203 | </tr> |
| | 204 | </table> |
| | 205 | <embed id="selectedElement7"> |
| | 206 | </embed> |
| | 207 | <object id="selectedElement8"> |
| | 208 | </object> |
| | 209 | <ol> |
| | 210 | <li id="selectedElement9"> |
| | 211 | </li> |
| | 212 | </ol> |
| | 213 | <form id="selectedElement10"> |
| | 214 | </form> |
| | 215 | </p> |
| | 216 | </body> |
| | 217 | </html> |
| | 218 | No newline at end of file |