Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | var Pos = CodeMirror.Pos; |
michael@0 | 2 | |
michael@0 | 3 | CodeMirror.defaults.rtlMoveVisually = true; |
michael@0 | 4 | |
michael@0 | 5 | function forEach(arr, f) { |
michael@0 | 6 | for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]); |
michael@0 | 7 | } |
michael@0 | 8 | |
michael@0 | 9 | function addDoc(cm, width, height) { |
michael@0 | 10 | var content = [], line = ""; |
michael@0 | 11 | for (var i = 0; i < width; ++i) line += "x"; |
michael@0 | 12 | for (var i = 0; i < height; ++i) content.push(line); |
michael@0 | 13 | cm.setValue(content.join("\n")); |
michael@0 | 14 | } |
michael@0 | 15 | |
michael@0 | 16 | function byClassName(elt, cls) { |
michael@0 | 17 | if (elt.getElementsByClassName) return elt.getElementsByClassName(cls); |
michael@0 | 18 | var found = [], re = new RegExp("\\b" + cls + "\\b"); |
michael@0 | 19 | function search(elt) { |
michael@0 | 20 | if (elt.nodeType == 3) return; |
michael@0 | 21 | if (re.test(elt.className)) found.push(elt); |
michael@0 | 22 | for (var i = 0, e = elt.childNodes.length; i < e; ++i) |
michael@0 | 23 | search(elt.childNodes[i]); |
michael@0 | 24 | } |
michael@0 | 25 | search(elt); |
michael@0 | 26 | return found; |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | var ie_lt8 = /MSIE [1-7]\b/.test(navigator.userAgent); |
michael@0 | 30 | var ie_lt9 = /MSIE [1-8]\b/.test(navigator.userAgent); |
michael@0 | 31 | var mac = /Mac/.test(navigator.platform); |
michael@0 | 32 | var phantom = /PhantomJS/.test(navigator.userAgent); |
michael@0 | 33 | var opera = /Opera\/\./.test(navigator.userAgent); |
michael@0 | 34 | var opera_version = opera && navigator.userAgent.match(/Version\/(\d+\.\d+)/); |
michael@0 | 35 | if (opera_version) opera_version = Number(opera_version); |
michael@0 | 36 | var opera_lt10 = opera && (!opera_version || opera_version < 10); |
michael@0 | 37 | |
michael@0 | 38 | namespace = "core_"; |
michael@0 | 39 | |
michael@0 | 40 | test("core_fromTextArea", function() { |
michael@0 | 41 | var te = document.getElementById("code"); |
michael@0 | 42 | te.value = "CONTENT"; |
michael@0 | 43 | var cm = CodeMirror.fromTextArea(te); |
michael@0 | 44 | is(!te.offsetHeight); |
michael@0 | 45 | eq(cm.getValue(), "CONTENT"); |
michael@0 | 46 | cm.setValue("foo\nbar"); |
michael@0 | 47 | eq(cm.getValue(), "foo\nbar"); |
michael@0 | 48 | cm.save(); |
michael@0 | 49 | is(/^foo\r?\nbar$/.test(te.value)); |
michael@0 | 50 | cm.setValue("xxx"); |
michael@0 | 51 | cm.toTextArea(); |
michael@0 | 52 | is(te.offsetHeight); |
michael@0 | 53 | eq(te.value, "xxx"); |
michael@0 | 54 | }); |
michael@0 | 55 | |
michael@0 | 56 | testCM("getRange", function(cm) { |
michael@0 | 57 | eq(cm.getLine(0), "1234"); |
michael@0 | 58 | eq(cm.getLine(1), "5678"); |
michael@0 | 59 | eq(cm.getLine(2), null); |
michael@0 | 60 | eq(cm.getLine(-1), null); |
michael@0 | 61 | eq(cm.getRange(Pos(0, 0), Pos(0, 3)), "123"); |
michael@0 | 62 | eq(cm.getRange(Pos(0, -1), Pos(0, 200)), "1234"); |
michael@0 | 63 | eq(cm.getRange(Pos(0, 2), Pos(1, 2)), "34\n56"); |
michael@0 | 64 | eq(cm.getRange(Pos(1, 2), Pos(100, 0)), "78"); |
michael@0 | 65 | }, {value: "1234\n5678"}); |
michael@0 | 66 | |
michael@0 | 67 | testCM("replaceRange", function(cm) { |
michael@0 | 68 | eq(cm.getValue(), ""); |
michael@0 | 69 | cm.replaceRange("foo\n", Pos(0, 0)); |
michael@0 | 70 | eq(cm.getValue(), "foo\n"); |
michael@0 | 71 | cm.replaceRange("a\nb", Pos(0, 1)); |
michael@0 | 72 | eq(cm.getValue(), "fa\nboo\n"); |
michael@0 | 73 | eq(cm.lineCount(), 3); |
michael@0 | 74 | cm.replaceRange("xyzzy", Pos(0, 0), Pos(1, 1)); |
michael@0 | 75 | eq(cm.getValue(), "xyzzyoo\n"); |
michael@0 | 76 | cm.replaceRange("abc", Pos(0, 0), Pos(10, 0)); |
michael@0 | 77 | eq(cm.getValue(), "abc"); |
michael@0 | 78 | eq(cm.lineCount(), 1); |
michael@0 | 79 | }); |
michael@0 | 80 | |
michael@0 | 81 | testCM("selection", function(cm) { |
michael@0 | 82 | cm.setSelection(Pos(0, 4), Pos(2, 2)); |
michael@0 | 83 | is(cm.somethingSelected()); |
michael@0 | 84 | eq(cm.getSelection(), "11\n222222\n33"); |
michael@0 | 85 | eqPos(cm.getCursor(false), Pos(2, 2)); |
michael@0 | 86 | eqPos(cm.getCursor(true), Pos(0, 4)); |
michael@0 | 87 | cm.setSelection(Pos(1, 0)); |
michael@0 | 88 | is(!cm.somethingSelected()); |
michael@0 | 89 | eq(cm.getSelection(), ""); |
michael@0 | 90 | eqPos(cm.getCursor(true), Pos(1, 0)); |
michael@0 | 91 | cm.replaceSelection("abc", "around"); |
michael@0 | 92 | eq(cm.getSelection(), "abc"); |
michael@0 | 93 | eq(cm.getValue(), "111111\nabc222222\n333333"); |
michael@0 | 94 | cm.replaceSelection("def", "end"); |
michael@0 | 95 | eq(cm.getSelection(), ""); |
michael@0 | 96 | eqPos(cm.getCursor(true), Pos(1, 3)); |
michael@0 | 97 | cm.setCursor(Pos(2, 1)); |
michael@0 | 98 | eqPos(cm.getCursor(true), Pos(2, 1)); |
michael@0 | 99 | cm.setCursor(1, 2); |
michael@0 | 100 | eqPos(cm.getCursor(true), Pos(1, 2)); |
michael@0 | 101 | }, {value: "111111\n222222\n333333"}); |
michael@0 | 102 | |
michael@0 | 103 | testCM("extendSelection", function(cm) { |
michael@0 | 104 | cm.setExtending(true); |
michael@0 | 105 | addDoc(cm, 10, 10); |
michael@0 | 106 | cm.setSelection(Pos(3, 5)); |
michael@0 | 107 | eqPos(cm.getCursor("head"), Pos(3, 5)); |
michael@0 | 108 | eqPos(cm.getCursor("anchor"), Pos(3, 5)); |
michael@0 | 109 | cm.setSelection(Pos(2, 5), Pos(5, 5)); |
michael@0 | 110 | eqPos(cm.getCursor("head"), Pos(5, 5)); |
michael@0 | 111 | eqPos(cm.getCursor("anchor"), Pos(2, 5)); |
michael@0 | 112 | eqPos(cm.getCursor("start"), Pos(2, 5)); |
michael@0 | 113 | eqPos(cm.getCursor("end"), Pos(5, 5)); |
michael@0 | 114 | cm.setSelection(Pos(5, 5), Pos(2, 5)); |
michael@0 | 115 | eqPos(cm.getCursor("head"), Pos(2, 5)); |
michael@0 | 116 | eqPos(cm.getCursor("anchor"), Pos(5, 5)); |
michael@0 | 117 | eqPos(cm.getCursor("start"), Pos(2, 5)); |
michael@0 | 118 | eqPos(cm.getCursor("end"), Pos(5, 5)); |
michael@0 | 119 | cm.extendSelection(Pos(3, 2)); |
michael@0 | 120 | eqPos(cm.getCursor("head"), Pos(3, 2)); |
michael@0 | 121 | eqPos(cm.getCursor("anchor"), Pos(5, 5)); |
michael@0 | 122 | cm.extendSelection(Pos(6, 2)); |
michael@0 | 123 | eqPos(cm.getCursor("head"), Pos(6, 2)); |
michael@0 | 124 | eqPos(cm.getCursor("anchor"), Pos(5, 5)); |
michael@0 | 125 | cm.extendSelection(Pos(6, 3), Pos(6, 4)); |
michael@0 | 126 | eqPos(cm.getCursor("head"), Pos(6, 4)); |
michael@0 | 127 | eqPos(cm.getCursor("anchor"), Pos(5, 5)); |
michael@0 | 128 | cm.extendSelection(Pos(0, 3), Pos(0, 4)); |
michael@0 | 129 | eqPos(cm.getCursor("head"), Pos(0, 3)); |
michael@0 | 130 | eqPos(cm.getCursor("anchor"), Pos(5, 5)); |
michael@0 | 131 | cm.extendSelection(Pos(4, 5), Pos(6, 5)); |
michael@0 | 132 | eqPos(cm.getCursor("head"), Pos(6, 5)); |
michael@0 | 133 | eqPos(cm.getCursor("anchor"), Pos(4, 5)); |
michael@0 | 134 | cm.setExtending(false); |
michael@0 | 135 | cm.extendSelection(Pos(0, 3), Pos(0, 4)); |
michael@0 | 136 | eqPos(cm.getCursor("head"), Pos(0, 3)); |
michael@0 | 137 | eqPos(cm.getCursor("anchor"), Pos(0, 4)); |
michael@0 | 138 | }); |
michael@0 | 139 | |
michael@0 | 140 | testCM("lines", function(cm) { |
michael@0 | 141 | eq(cm.getLine(0), "111111"); |
michael@0 | 142 | eq(cm.getLine(1), "222222"); |
michael@0 | 143 | eq(cm.getLine(-1), null); |
michael@0 | 144 | cm.replaceRange("", Pos(1, 0), Pos(2, 0)) |
michael@0 | 145 | cm.replaceRange("abc", Pos(1, 0), Pos(1)); |
michael@0 | 146 | eq(cm.getValue(), "111111\nabc"); |
michael@0 | 147 | }, {value: "111111\n222222\n333333"}); |
michael@0 | 148 | |
michael@0 | 149 | testCM("indent", function(cm) { |
michael@0 | 150 | cm.indentLine(1); |
michael@0 | 151 | eq(cm.getLine(1), " blah();"); |
michael@0 | 152 | cm.setOption("indentUnit", 8); |
michael@0 | 153 | cm.indentLine(1); |
michael@0 | 154 | eq(cm.getLine(1), "\tblah();"); |
michael@0 | 155 | cm.setOption("indentUnit", 10); |
michael@0 | 156 | cm.setOption("tabSize", 4); |
michael@0 | 157 | cm.indentLine(1); |
michael@0 | 158 | eq(cm.getLine(1), "\t\t blah();"); |
michael@0 | 159 | }, {value: "if (x) {\nblah();\n}", indentUnit: 3, indentWithTabs: true, tabSize: 8}); |
michael@0 | 160 | |
michael@0 | 161 | testCM("indentByNumber", function(cm) { |
michael@0 | 162 | cm.indentLine(0, 2); |
michael@0 | 163 | eq(cm.getLine(0), " foo"); |
michael@0 | 164 | cm.indentLine(0, -200); |
michael@0 | 165 | eq(cm.getLine(0), "foo"); |
michael@0 | 166 | cm.setSelection(Pos(0, 0), Pos(1, 2)); |
michael@0 | 167 | cm.indentSelection(3); |
michael@0 | 168 | eq(cm.getValue(), " foo\n bar\nbaz"); |
michael@0 | 169 | }, {value: "foo\nbar\nbaz"}); |
michael@0 | 170 | |
michael@0 | 171 | test("core_defaults", function() { |
michael@0 | 172 | var defsCopy = {}, defs = CodeMirror.defaults; |
michael@0 | 173 | for (var opt in defs) defsCopy[opt] = defs[opt]; |
michael@0 | 174 | defs.indentUnit = 5; |
michael@0 | 175 | defs.value = "uu"; |
michael@0 | 176 | defs.indentWithTabs = true; |
michael@0 | 177 | defs.tabindex = 55; |
michael@0 | 178 | var place = document.getElementById("testground"), cm = CodeMirror(place); |
michael@0 | 179 | try { |
michael@0 | 180 | eq(cm.getOption("indentUnit"), 5); |
michael@0 | 181 | cm.setOption("indentUnit", 10); |
michael@0 | 182 | eq(defs.indentUnit, 5); |
michael@0 | 183 | eq(cm.getValue(), "uu"); |
michael@0 | 184 | eq(cm.getOption("indentWithTabs"), true); |
michael@0 | 185 | eq(cm.getInputField().tabIndex, 55); |
michael@0 | 186 | } |
michael@0 | 187 | finally { |
michael@0 | 188 | for (var opt in defsCopy) defs[opt] = defsCopy[opt]; |
michael@0 | 189 | place.removeChild(cm.getWrapperElement()); |
michael@0 | 190 | } |
michael@0 | 191 | }); |
michael@0 | 192 | |
michael@0 | 193 | testCM("lineInfo", function(cm) { |
michael@0 | 194 | eq(cm.lineInfo(-1), null); |
michael@0 | 195 | var mark = document.createElement("span"); |
michael@0 | 196 | var lh = cm.setGutterMarker(1, "FOO", mark); |
michael@0 | 197 | var info = cm.lineInfo(1); |
michael@0 | 198 | eq(info.text, "222222"); |
michael@0 | 199 | eq(info.gutterMarkers.FOO, mark); |
michael@0 | 200 | eq(info.line, 1); |
michael@0 | 201 | eq(cm.lineInfo(2).gutterMarkers, null); |
michael@0 | 202 | cm.setGutterMarker(lh, "FOO", null); |
michael@0 | 203 | eq(cm.lineInfo(1).gutterMarkers, null); |
michael@0 | 204 | cm.setGutterMarker(1, "FOO", mark); |
michael@0 | 205 | cm.setGutterMarker(0, "FOO", mark); |
michael@0 | 206 | cm.clearGutter("FOO"); |
michael@0 | 207 | eq(cm.lineInfo(0).gutterMarkers, null); |
michael@0 | 208 | eq(cm.lineInfo(1).gutterMarkers, null); |
michael@0 | 209 | }, {value: "111111\n222222\n333333"}); |
michael@0 | 210 | |
michael@0 | 211 | testCM("coords", function(cm) { |
michael@0 | 212 | cm.setSize(null, 100); |
michael@0 | 213 | addDoc(cm, 32, 200); |
michael@0 | 214 | var top = cm.charCoords(Pos(0, 0)); |
michael@0 | 215 | var bot = cm.charCoords(Pos(200, 30)); |
michael@0 | 216 | is(top.left < bot.left); |
michael@0 | 217 | is(top.top < bot.top); |
michael@0 | 218 | is(top.top < top.bottom); |
michael@0 | 219 | cm.scrollTo(null, 100); |
michael@0 | 220 | var top2 = cm.charCoords(Pos(0, 0)); |
michael@0 | 221 | is(top.top > top2.top); |
michael@0 | 222 | eq(top.left, top2.left); |
michael@0 | 223 | }); |
michael@0 | 224 | |
michael@0 | 225 | testCM("coordsChar", function(cm) { |
michael@0 | 226 | addDoc(cm, 35, 70); |
michael@0 | 227 | for (var i = 0; i < 2; ++i) { |
michael@0 | 228 | var sys = i ? "local" : "page"; |
michael@0 | 229 | for (var ch = 0; ch <= 35; ch += 5) { |
michael@0 | 230 | for (var line = 0; line < 70; line += 5) { |
michael@0 | 231 | cm.setCursor(line, ch); |
michael@0 | 232 | var coords = cm.charCoords(Pos(line, ch), sys); |
michael@0 | 233 | var pos = cm.coordsChar({left: coords.left + 1, top: coords.top + 1}, sys); |
michael@0 | 234 | eqPos(pos, Pos(line, ch)); |
michael@0 | 235 | } |
michael@0 | 236 | } |
michael@0 | 237 | } |
michael@0 | 238 | }, {lineNumbers: true}); |
michael@0 | 239 | |
michael@0 | 240 | testCM("posFromIndex", function(cm) { |
michael@0 | 241 | cm.setValue( |
michael@0 | 242 | "This function should\n" + |
michael@0 | 243 | "convert a zero based index\n" + |
michael@0 | 244 | "to line and ch." |
michael@0 | 245 | ); |
michael@0 | 246 | |
michael@0 | 247 | var examples = [ |
michael@0 | 248 | { index: -1, line: 0, ch: 0 }, // <- Tests clipping |
michael@0 | 249 | { index: 0, line: 0, ch: 0 }, |
michael@0 | 250 | { index: 10, line: 0, ch: 10 }, |
michael@0 | 251 | { index: 39, line: 1, ch: 18 }, |
michael@0 | 252 | { index: 55, line: 2, ch: 7 }, |
michael@0 | 253 | { index: 63, line: 2, ch: 15 }, |
michael@0 | 254 | { index: 64, line: 2, ch: 15 } // <- Tests clipping |
michael@0 | 255 | ]; |
michael@0 | 256 | |
michael@0 | 257 | for (var i = 0; i < examples.length; i++) { |
michael@0 | 258 | var example = examples[i]; |
michael@0 | 259 | var pos = cm.posFromIndex(example.index); |
michael@0 | 260 | eq(pos.line, example.line); |
michael@0 | 261 | eq(pos.ch, example.ch); |
michael@0 | 262 | if (example.index >= 0 && example.index < 64) |
michael@0 | 263 | eq(cm.indexFromPos(pos), example.index); |
michael@0 | 264 | } |
michael@0 | 265 | }); |
michael@0 | 266 | |
michael@0 | 267 | testCM("undo", function(cm) { |
michael@0 | 268 | cm.replaceRange("def", Pos(0, 0), Pos(0)); |
michael@0 | 269 | eq(cm.historySize().undo, 1); |
michael@0 | 270 | cm.undo(); |
michael@0 | 271 | eq(cm.getValue(), "abc"); |
michael@0 | 272 | eq(cm.historySize().undo, 0); |
michael@0 | 273 | eq(cm.historySize().redo, 1); |
michael@0 | 274 | cm.redo(); |
michael@0 | 275 | eq(cm.getValue(), "def"); |
michael@0 | 276 | eq(cm.historySize().undo, 1); |
michael@0 | 277 | eq(cm.historySize().redo, 0); |
michael@0 | 278 | cm.setValue("1\n\n\n2"); |
michael@0 | 279 | cm.clearHistory(); |
michael@0 | 280 | eq(cm.historySize().undo, 0); |
michael@0 | 281 | for (var i = 0; i < 20; ++i) { |
michael@0 | 282 | cm.replaceRange("a", Pos(0, 0)); |
michael@0 | 283 | cm.replaceRange("b", Pos(3, 0)); |
michael@0 | 284 | } |
michael@0 | 285 | eq(cm.historySize().undo, 40); |
michael@0 | 286 | for (var i = 0; i < 40; ++i) |
michael@0 | 287 | cm.undo(); |
michael@0 | 288 | eq(cm.historySize().redo, 40); |
michael@0 | 289 | eq(cm.getValue(), "1\n\n\n2"); |
michael@0 | 290 | }, {value: "abc"}); |
michael@0 | 291 | |
michael@0 | 292 | testCM("undoDepth", function(cm) { |
michael@0 | 293 | cm.replaceRange("d", Pos(0)); |
michael@0 | 294 | cm.replaceRange("e", Pos(0)); |
michael@0 | 295 | cm.replaceRange("f", Pos(0)); |
michael@0 | 296 | cm.undo(); cm.undo(); cm.undo(); |
michael@0 | 297 | eq(cm.getValue(), "abcd"); |
michael@0 | 298 | }, {value: "abc", undoDepth: 4}); |
michael@0 | 299 | |
michael@0 | 300 | testCM("undoDoesntClearValue", function(cm) { |
michael@0 | 301 | cm.undo(); |
michael@0 | 302 | eq(cm.getValue(), "x"); |
michael@0 | 303 | }, {value: "x"}); |
michael@0 | 304 | |
michael@0 | 305 | testCM("undoMultiLine", function(cm) { |
michael@0 | 306 | cm.operation(function() { |
michael@0 | 307 | cm.replaceRange("x", Pos(0, 0)); |
michael@0 | 308 | cm.replaceRange("y", Pos(1, 0)); |
michael@0 | 309 | }); |
michael@0 | 310 | cm.undo(); |
michael@0 | 311 | eq(cm.getValue(), "abc\ndef\nghi"); |
michael@0 | 312 | cm.operation(function() { |
michael@0 | 313 | cm.replaceRange("y", Pos(1, 0)); |
michael@0 | 314 | cm.replaceRange("x", Pos(0, 0)); |
michael@0 | 315 | }); |
michael@0 | 316 | cm.undo(); |
michael@0 | 317 | eq(cm.getValue(), "abc\ndef\nghi"); |
michael@0 | 318 | cm.operation(function() { |
michael@0 | 319 | cm.replaceRange("y", Pos(2, 0)); |
michael@0 | 320 | cm.replaceRange("x", Pos(1, 0)); |
michael@0 | 321 | cm.replaceRange("z", Pos(2, 0)); |
michael@0 | 322 | }); |
michael@0 | 323 | cm.undo(); |
michael@0 | 324 | eq(cm.getValue(), "abc\ndef\nghi", 3); |
michael@0 | 325 | }, {value: "abc\ndef\nghi"}); |
michael@0 | 326 | |
michael@0 | 327 | testCM("undoComposite", function(cm) { |
michael@0 | 328 | cm.replaceRange("y", Pos(1)); |
michael@0 | 329 | cm.operation(function() { |
michael@0 | 330 | cm.replaceRange("x", Pos(0)); |
michael@0 | 331 | cm.replaceRange("z", Pos(2)); |
michael@0 | 332 | }); |
michael@0 | 333 | eq(cm.getValue(), "ax\nby\ncz\n"); |
michael@0 | 334 | cm.undo(); |
michael@0 | 335 | eq(cm.getValue(), "a\nby\nc\n"); |
michael@0 | 336 | cm.undo(); |
michael@0 | 337 | eq(cm.getValue(), "a\nb\nc\n"); |
michael@0 | 338 | cm.redo(); cm.redo(); |
michael@0 | 339 | eq(cm.getValue(), "ax\nby\ncz\n"); |
michael@0 | 340 | }, {value: "a\nb\nc\n"}); |
michael@0 | 341 | |
michael@0 | 342 | testCM("undoSelection", function(cm) { |
michael@0 | 343 | cm.setSelection(Pos(0, 2), Pos(0, 4)); |
michael@0 | 344 | cm.replaceSelection(""); |
michael@0 | 345 | cm.setCursor(Pos(1, 0)); |
michael@0 | 346 | cm.undo(); |
michael@0 | 347 | eqPos(cm.getCursor(true), Pos(0, 2)); |
michael@0 | 348 | eqPos(cm.getCursor(false), Pos(0, 4)); |
michael@0 | 349 | cm.setCursor(Pos(1, 0)); |
michael@0 | 350 | cm.redo(); |
michael@0 | 351 | eqPos(cm.getCursor(true), Pos(0, 2)); |
michael@0 | 352 | eqPos(cm.getCursor(false), Pos(0, 2)); |
michael@0 | 353 | }, {value: "abcdefgh\n"}); |
michael@0 | 354 | |
michael@0 | 355 | testCM("undoSelectionAsBefore", function(cm) { |
michael@0 | 356 | cm.replaceSelection("abc", "around"); |
michael@0 | 357 | cm.undo(); |
michael@0 | 358 | cm.redo(); |
michael@0 | 359 | eq(cm.getSelection(), "abc"); |
michael@0 | 360 | }); |
michael@0 | 361 | |
michael@0 | 362 | testCM("markTextSingleLine", function(cm) { |
michael@0 | 363 | forEach([{a: 0, b: 1, c: "", f: 2, t: 5}, |
michael@0 | 364 | {a: 0, b: 4, c: "", f: 0, t: 2}, |
michael@0 | 365 | {a: 1, b: 2, c: "x", f: 3, t: 6}, |
michael@0 | 366 | {a: 4, b: 5, c: "", f: 3, t: 5}, |
michael@0 | 367 | {a: 4, b: 5, c: "xx", f: 3, t: 7}, |
michael@0 | 368 | {a: 2, b: 5, c: "", f: 2, t: 3}, |
michael@0 | 369 | {a: 2, b: 5, c: "abcd", f: 6, t: 7}, |
michael@0 | 370 | {a: 2, b: 6, c: "x", f: null, t: null}, |
michael@0 | 371 | {a: 3, b: 6, c: "", f: null, t: null}, |
michael@0 | 372 | {a: 0, b: 9, c: "hallo", f: null, t: null}, |
michael@0 | 373 | {a: 4, b: 6, c: "x", f: 3, t: 4}, |
michael@0 | 374 | {a: 4, b: 8, c: "", f: 3, t: 4}, |
michael@0 | 375 | {a: 6, b: 6, c: "a", f: 3, t: 6}, |
michael@0 | 376 | {a: 8, b: 9, c: "", f: 3, t: 6}], function(test) { |
michael@0 | 377 | cm.setValue("1234567890"); |
michael@0 | 378 | var r = cm.markText(Pos(0, 3), Pos(0, 6), {className: "foo"}); |
michael@0 | 379 | cm.replaceRange(test.c, Pos(0, test.a), Pos(0, test.b)); |
michael@0 | 380 | var f = r.find(); |
michael@0 | 381 | eq(f && f.from.ch, test.f); eq(f && f.to.ch, test.t); |
michael@0 | 382 | }); |
michael@0 | 383 | }); |
michael@0 | 384 | |
michael@0 | 385 | testCM("markTextMultiLine", function(cm) { |
michael@0 | 386 | function p(v) { return v && Pos(v[0], v[1]); } |
michael@0 | 387 | forEach([{a: [0, 0], b: [0, 5], c: "", f: [0, 0], t: [2, 5]}, |
michael@0 | 388 | {a: [0, 0], b: [0, 5], c: "foo\n", f: [1, 0], t: [3, 5]}, |
michael@0 | 389 | {a: [0, 1], b: [0, 10], c: "", f: [0, 1], t: [2, 5]}, |
michael@0 | 390 | {a: [0, 5], b: [0, 6], c: "x", f: [0, 6], t: [2, 5]}, |
michael@0 | 391 | {a: [0, 0], b: [1, 0], c: "", f: [0, 0], t: [1, 5]}, |
michael@0 | 392 | {a: [0, 6], b: [2, 4], c: "", f: [0, 5], t: [0, 7]}, |
michael@0 | 393 | {a: [0, 6], b: [2, 4], c: "aa", f: [0, 5], t: [0, 9]}, |
michael@0 | 394 | {a: [1, 2], b: [1, 8], c: "", f: [0, 5], t: [2, 5]}, |
michael@0 | 395 | {a: [0, 5], b: [2, 5], c: "xx", f: null, t: null}, |
michael@0 | 396 | {a: [0, 0], b: [2, 10], c: "x", f: null, t: null}, |
michael@0 | 397 | {a: [1, 5], b: [2, 5], c: "", f: [0, 5], t: [1, 5]}, |
michael@0 | 398 | {a: [2, 0], b: [2, 3], c: "", f: [0, 5], t: [2, 2]}, |
michael@0 | 399 | {a: [2, 5], b: [3, 0], c: "a\nb", f: [0, 5], t: [2, 5]}, |
michael@0 | 400 | {a: [2, 3], b: [3, 0], c: "x", f: [0, 5], t: [2, 3]}, |
michael@0 | 401 | {a: [1, 1], b: [1, 9], c: "1\n2\n3", f: [0, 5], t: [4, 5]}], function(test) { |
michael@0 | 402 | cm.setValue("aaaaaaaaaa\nbbbbbbbbbb\ncccccccccc\ndddddddd\n"); |
michael@0 | 403 | var r = cm.markText(Pos(0, 5), Pos(2, 5), |
michael@0 | 404 | {className: "CodeMirror-matchingbracket"}); |
michael@0 | 405 | cm.replaceRange(test.c, p(test.a), p(test.b)); |
michael@0 | 406 | var f = r.find(); |
michael@0 | 407 | eqPos(f && f.from, p(test.f)); eqPos(f && f.to, p(test.t)); |
michael@0 | 408 | }); |
michael@0 | 409 | }); |
michael@0 | 410 | |
michael@0 | 411 | testCM("markTextUndo", function(cm) { |
michael@0 | 412 | var marker1, marker2, bookmark; |
michael@0 | 413 | marker1 = cm.markText(Pos(0, 1), Pos(0, 3), |
michael@0 | 414 | {className: "CodeMirror-matchingbracket"}); |
michael@0 | 415 | marker2 = cm.markText(Pos(0, 0), Pos(2, 1), |
michael@0 | 416 | {className: "CodeMirror-matchingbracket"}); |
michael@0 | 417 | bookmark = cm.setBookmark(Pos(1, 5)); |
michael@0 | 418 | cm.operation(function(){ |
michael@0 | 419 | cm.replaceRange("foo", Pos(0, 2)); |
michael@0 | 420 | cm.replaceRange("bar\nbaz\nbug\n", Pos(2, 0), Pos(3, 0)); |
michael@0 | 421 | }); |
michael@0 | 422 | var v1 = cm.getValue(); |
michael@0 | 423 | cm.setValue(""); |
michael@0 | 424 | eq(marker1.find(), null); eq(marker2.find(), null); eq(bookmark.find(), null); |
michael@0 | 425 | cm.undo(); |
michael@0 | 426 | eqPos(bookmark.find(), Pos(1, 5), "still there"); |
michael@0 | 427 | cm.undo(); |
michael@0 | 428 | var m1Pos = marker1.find(), m2Pos = marker2.find(); |
michael@0 | 429 | eqPos(m1Pos.from, Pos(0, 1)); eqPos(m1Pos.to, Pos(0, 3)); |
michael@0 | 430 | eqPos(m2Pos.from, Pos(0, 0)); eqPos(m2Pos.to, Pos(2, 1)); |
michael@0 | 431 | eqPos(bookmark.find(), Pos(1, 5)); |
michael@0 | 432 | cm.redo(); cm.redo(); |
michael@0 | 433 | eq(bookmark.find(), null); |
michael@0 | 434 | cm.undo(); |
michael@0 | 435 | eqPos(bookmark.find(), Pos(1, 5)); |
michael@0 | 436 | eq(cm.getValue(), v1); |
michael@0 | 437 | }, {value: "1234\n56789\n00\n"}); |
michael@0 | 438 | |
michael@0 | 439 | testCM("markTextStayGone", function(cm) { |
michael@0 | 440 | var m1 = cm.markText(Pos(0, 0), Pos(0, 1)); |
michael@0 | 441 | cm.replaceRange("hi", Pos(0, 2)); |
michael@0 | 442 | m1.clear(); |
michael@0 | 443 | cm.undo(); |
michael@0 | 444 | eq(m1.find(), null); |
michael@0 | 445 | }, {value: "hello"}); |
michael@0 | 446 | |
michael@0 | 447 | testCM("markTextAllowEmpty", function(cm) { |
michael@0 | 448 | var m1 = cm.markText(Pos(0, 1), Pos(0, 2), {clearWhenEmpty: false}); |
michael@0 | 449 | is(m1.find()); |
michael@0 | 450 | cm.replaceRange("x", Pos(0, 0)); |
michael@0 | 451 | is(m1.find()); |
michael@0 | 452 | cm.replaceRange("y", Pos(0, 2)); |
michael@0 | 453 | is(m1.find()); |
michael@0 | 454 | cm.replaceRange("z", Pos(0, 3), Pos(0, 4)); |
michael@0 | 455 | is(!m1.find()); |
michael@0 | 456 | var m2 = cm.markText(Pos(0, 1), Pos(0, 2), {clearWhenEmpty: false, |
michael@0 | 457 | inclusiveLeft: true, |
michael@0 | 458 | inclusiveRight: true}); |
michael@0 | 459 | cm.replaceRange("q", Pos(0, 1), Pos(0, 2)); |
michael@0 | 460 | is(m2.find()); |
michael@0 | 461 | cm.replaceRange("", Pos(0, 0), Pos(0, 3)); |
michael@0 | 462 | is(!m2.find()); |
michael@0 | 463 | var m3 = cm.markText(Pos(0, 1), Pos(0, 1), {clearWhenEmpty: false}); |
michael@0 | 464 | cm.replaceRange("a", Pos(0, 3)); |
michael@0 | 465 | is(m3.find()); |
michael@0 | 466 | cm.replaceRange("b", Pos(0, 1)); |
michael@0 | 467 | is(!m3.find()); |
michael@0 | 468 | }, {value: "abcde"}); |
michael@0 | 469 | |
michael@0 | 470 | testCM("markTextStacked", function(cm) { |
michael@0 | 471 | var m1 = cm.markText(Pos(0, 0), Pos(0, 0), {clearWhenEmpty: false}); |
michael@0 | 472 | var m2 = cm.markText(Pos(0, 0), Pos(0, 0), {clearWhenEmpty: false}); |
michael@0 | 473 | cm.replaceRange("B", Pos(0, 1)); |
michael@0 | 474 | is(m1.find() && m2.find()); |
michael@0 | 475 | }, {value: "A"}); |
michael@0 | 476 | |
michael@0 | 477 | testCM("undoPreservesNewMarks", function(cm) { |
michael@0 | 478 | cm.markText(Pos(0, 3), Pos(0, 4)); |
michael@0 | 479 | cm.markText(Pos(1, 1), Pos(1, 3)); |
michael@0 | 480 | cm.replaceRange("", Pos(0, 3), Pos(3, 1)); |
michael@0 | 481 | var mBefore = cm.markText(Pos(0, 0), Pos(0, 1)); |
michael@0 | 482 | var mAfter = cm.markText(Pos(0, 5), Pos(0, 6)); |
michael@0 | 483 | var mAround = cm.markText(Pos(0, 2), Pos(0, 4)); |
michael@0 | 484 | cm.undo(); |
michael@0 | 485 | eqPos(mBefore.find().from, Pos(0, 0)); |
michael@0 | 486 | eqPos(mBefore.find().to, Pos(0, 1)); |
michael@0 | 487 | eqPos(mAfter.find().from, Pos(3, 3)); |
michael@0 | 488 | eqPos(mAfter.find().to, Pos(3, 4)); |
michael@0 | 489 | eqPos(mAround.find().from, Pos(0, 2)); |
michael@0 | 490 | eqPos(mAround.find().to, Pos(3, 2)); |
michael@0 | 491 | var found = cm.findMarksAt(Pos(2, 2)); |
michael@0 | 492 | eq(found.length, 1); |
michael@0 | 493 | eq(found[0], mAround); |
michael@0 | 494 | }, {value: "aaaa\nbbbb\ncccc\ndddd"}); |
michael@0 | 495 | |
michael@0 | 496 | testCM("markClearBetween", function(cm) { |
michael@0 | 497 | cm.setValue("aaa\nbbb\nccc\nddd\n"); |
michael@0 | 498 | cm.markText(Pos(0, 0), Pos(2)); |
michael@0 | 499 | cm.replaceRange("aaa\nbbb\nccc", Pos(0, 0), Pos(2)); |
michael@0 | 500 | eq(cm.findMarksAt(Pos(1, 1)).length, 0); |
michael@0 | 501 | }); |
michael@0 | 502 | |
michael@0 | 503 | testCM("deleteSpanCollapsedInclusiveLeft", function(cm) { |
michael@0 | 504 | var from = Pos(1, 0), to = Pos(1, 1); |
michael@0 | 505 | var m = cm.markText(from, to, {collapsed: true, inclusiveLeft: true}); |
michael@0 | 506 | // Delete collapsed span. |
michael@0 | 507 | cm.replaceRange("", from, to); |
michael@0 | 508 | }, {value: "abc\nX\ndef"}); |
michael@0 | 509 | |
michael@0 | 510 | testCM("bookmark", function(cm) { |
michael@0 | 511 | function p(v) { return v && Pos(v[0], v[1]); } |
michael@0 | 512 | forEach([{a: [1, 0], b: [1, 1], c: "", d: [1, 4]}, |
michael@0 | 513 | {a: [1, 1], b: [1, 1], c: "xx", d: [1, 7]}, |
michael@0 | 514 | {a: [1, 4], b: [1, 5], c: "ab", d: [1, 6]}, |
michael@0 | 515 | {a: [1, 4], b: [1, 6], c: "", d: null}, |
michael@0 | 516 | {a: [1, 5], b: [1, 6], c: "abc", d: [1, 5]}, |
michael@0 | 517 | {a: [1, 6], b: [1, 8], c: "", d: [1, 5]}, |
michael@0 | 518 | {a: [1, 4], b: [1, 4], c: "\n\n", d: [3, 1]}, |
michael@0 | 519 | {bm: [1, 9], a: [1, 1], b: [1, 1], c: "\n", d: [2, 8]}], function(test) { |
michael@0 | 520 | cm.setValue("1234567890\n1234567890\n1234567890"); |
michael@0 | 521 | var b = cm.setBookmark(p(test.bm) || Pos(1, 5)); |
michael@0 | 522 | cm.replaceRange(test.c, p(test.a), p(test.b)); |
michael@0 | 523 | eqPos(b.find(), p(test.d)); |
michael@0 | 524 | }); |
michael@0 | 525 | }); |
michael@0 | 526 | |
michael@0 | 527 | testCM("bookmarkInsertLeft", function(cm) { |
michael@0 | 528 | var br = cm.setBookmark(Pos(0, 2), {insertLeft: false}); |
michael@0 | 529 | var bl = cm.setBookmark(Pos(0, 2), {insertLeft: true}); |
michael@0 | 530 | cm.setCursor(Pos(0, 2)); |
michael@0 | 531 | cm.replaceSelection("hi"); |
michael@0 | 532 | eqPos(br.find(), Pos(0, 2)); |
michael@0 | 533 | eqPos(bl.find(), Pos(0, 4)); |
michael@0 | 534 | cm.replaceRange("", Pos(0, 4), Pos(0, 5)); |
michael@0 | 535 | cm.replaceRange("", Pos(0, 2), Pos(0, 4)); |
michael@0 | 536 | cm.replaceRange("", Pos(0, 1), Pos(0, 2)); |
michael@0 | 537 | // Verify that deleting next to bookmarks doesn't kill them |
michael@0 | 538 | eqPos(br.find(), Pos(0, 1)); |
michael@0 | 539 | eqPos(bl.find(), Pos(0, 1)); |
michael@0 | 540 | }, {value: "abcdef"}); |
michael@0 | 541 | |
michael@0 | 542 | testCM("bookmarkCursor", function(cm) { |
michael@0 | 543 | var pos01 = cm.cursorCoords(Pos(0, 1)), pos11 = cm.cursorCoords(Pos(1, 1)), |
michael@0 | 544 | pos20 = cm.cursorCoords(Pos(2, 0)), pos30 = cm.cursorCoords(Pos(3, 0)), |
michael@0 | 545 | pos41 = cm.cursorCoords(Pos(4, 1)); |
michael@0 | 546 | cm.setBookmark(Pos(0, 1), {widget: document.createTextNode("←"), insertLeft: true}); |
michael@0 | 547 | cm.setBookmark(Pos(2, 0), {widget: document.createTextNode("←"), insertLeft: true}); |
michael@0 | 548 | cm.setBookmark(Pos(1, 1), {widget: document.createTextNode("→")}); |
michael@0 | 549 | cm.setBookmark(Pos(3, 0), {widget: document.createTextNode("→")}); |
michael@0 | 550 | var new01 = cm.cursorCoords(Pos(0, 1)), new11 = cm.cursorCoords(Pos(1, 1)), |
michael@0 | 551 | new20 = cm.cursorCoords(Pos(2, 0)), new30 = cm.cursorCoords(Pos(3, 0)); |
michael@0 | 552 | near(new01.left, pos01.left, 1); |
michael@0 | 553 | near(new01.top, pos01.top, 1); |
michael@0 | 554 | is(new11.left > pos11.left, "at right, middle of line"); |
michael@0 | 555 | near(new11.top == pos11.top, 1); |
michael@0 | 556 | near(new20.left, pos20.left, 1); |
michael@0 | 557 | near(new20.top, pos20.top, 1); |
michael@0 | 558 | is(new30.left > pos30.left, "at right, empty line"); |
michael@0 | 559 | near(new30.top, pos30, 1); |
michael@0 | 560 | cm.setBookmark(Pos(4, 0), {widget: document.createTextNode("→")}); |
michael@0 | 561 | is(cm.cursorCoords(Pos(4, 1)).left > pos41.left, "single-char bug"); |
michael@0 | 562 | }, {value: "foo\nbar\n\n\nx\ny"}); |
michael@0 | 563 | |
michael@0 | 564 | testCM("multiBookmarkCursor", function(cm) { |
michael@0 | 565 | if (phantom) return; |
michael@0 | 566 | var ms = [], m; |
michael@0 | 567 | function add(insertLeft) { |
michael@0 | 568 | for (var i = 0; i < 3; ++i) { |
michael@0 | 569 | var node = document.createElement("span"); |
michael@0 | 570 | node.innerHTML = "X"; |
michael@0 | 571 | ms.push(cm.setBookmark(Pos(0, 1), {widget: node, insertLeft: insertLeft})); |
michael@0 | 572 | } |
michael@0 | 573 | } |
michael@0 | 574 | var base1 = cm.cursorCoords(Pos(0, 1)).left, base4 = cm.cursorCoords(Pos(0, 4)).left; |
michael@0 | 575 | add(true); |
michael@0 | 576 | near(base1, cm.cursorCoords(Pos(0, 1)).left, 1); |
michael@0 | 577 | while (m = ms.pop()) m.clear(); |
michael@0 | 578 | add(false); |
michael@0 | 579 | near(base4, cm.cursorCoords(Pos(0, 1)).left, 1); |
michael@0 | 580 | }, {value: "abcdefg"}); |
michael@0 | 581 | |
michael@0 | 582 | testCM("getAllMarks", function(cm) { |
michael@0 | 583 | addDoc(cm, 10, 10); |
michael@0 | 584 | var m1 = cm.setBookmark(Pos(0, 2)); |
michael@0 | 585 | var m2 = cm.markText(Pos(0, 2), Pos(3, 2)); |
michael@0 | 586 | var m3 = cm.markText(Pos(1, 2), Pos(1, 8)); |
michael@0 | 587 | var m4 = cm.markText(Pos(8, 0), Pos(9, 0)); |
michael@0 | 588 | eq(cm.getAllMarks().length, 4); |
michael@0 | 589 | m1.clear(); |
michael@0 | 590 | m3.clear(); |
michael@0 | 591 | eq(cm.getAllMarks().length, 2); |
michael@0 | 592 | }); |
michael@0 | 593 | |
michael@0 | 594 | testCM("bug577", function(cm) { |
michael@0 | 595 | cm.setValue("a\nb"); |
michael@0 | 596 | cm.clearHistory(); |
michael@0 | 597 | cm.setValue("fooooo"); |
michael@0 | 598 | cm.undo(); |
michael@0 | 599 | }); |
michael@0 | 600 | |
michael@0 | 601 | testCM("scrollSnap", function(cm) { |
michael@0 | 602 | cm.setSize(100, 100); |
michael@0 | 603 | addDoc(cm, 200, 200); |
michael@0 | 604 | cm.setCursor(Pos(100, 180)); |
michael@0 | 605 | var info = cm.getScrollInfo(); |
michael@0 | 606 | is(info.left > 0 && info.top > 0); |
michael@0 | 607 | cm.setCursor(Pos(0, 0)); |
michael@0 | 608 | info = cm.getScrollInfo(); |
michael@0 | 609 | is(info.left == 0 && info.top == 0, "scrolled clean to top"); |
michael@0 | 610 | cm.setCursor(Pos(100, 180)); |
michael@0 | 611 | cm.setCursor(Pos(199, 0)); |
michael@0 | 612 | info = cm.getScrollInfo(); |
michael@0 | 613 | is(info.left == 0 && info.top + 2 > info.height - cm.getScrollerElement().clientHeight, "scrolled clean to bottom"); |
michael@0 | 614 | }); |
michael@0 | 615 | |
michael@0 | 616 | testCM("scrollIntoView", function(cm) { |
michael@0 | 617 | if (phantom) return; |
michael@0 | 618 | var outer = cm.getWrapperElement().getBoundingClientRect(); |
michael@0 | 619 | function test(line, ch, msg) { |
michael@0 | 620 | var pos = Pos(line, ch); |
michael@0 | 621 | cm.scrollIntoView(pos); |
michael@0 | 622 | var box = cm.charCoords(pos, "window"); |
michael@0 | 623 | is(box.left >= outer.left, msg + " (left)"); |
michael@0 | 624 | is(box.right <= outer.right, msg + " (right)"); |
michael@0 | 625 | is(box.top >= outer.top, msg + " (top)"); |
michael@0 | 626 | is(box.bottom <= outer.bottom, msg + " (bottom)"); |
michael@0 | 627 | } |
michael@0 | 628 | addDoc(cm, 200, 200); |
michael@0 | 629 | test(199, 199, "bottom right"); |
michael@0 | 630 | test(0, 0, "top left"); |
michael@0 | 631 | test(100, 100, "center"); |
michael@0 | 632 | test(199, 0, "bottom left"); |
michael@0 | 633 | test(0, 199, "top right"); |
michael@0 | 634 | test(100, 100, "center again"); |
michael@0 | 635 | }); |
michael@0 | 636 | |
michael@0 | 637 | testCM("scrollBackAndForth", function(cm) { |
michael@0 | 638 | addDoc(cm, 1, 200); |
michael@0 | 639 | cm.operation(function() { |
michael@0 | 640 | cm.scrollIntoView(Pos(199, 0)); |
michael@0 | 641 | cm.scrollIntoView(Pos(4, 0)); |
michael@0 | 642 | }); |
michael@0 | 643 | is(cm.getScrollInfo().top > 0); |
michael@0 | 644 | }); |
michael@0 | 645 | |
michael@0 | 646 | testCM("selectAllNoScroll", function(cm) { |
michael@0 | 647 | addDoc(cm, 1, 200); |
michael@0 | 648 | cm.execCommand("selectAll"); |
michael@0 | 649 | eq(cm.getScrollInfo().top, 0); |
michael@0 | 650 | cm.setCursor(199); |
michael@0 | 651 | cm.execCommand("selectAll"); |
michael@0 | 652 | is(cm.getScrollInfo().top > 0); |
michael@0 | 653 | }); |
michael@0 | 654 | |
michael@0 | 655 | testCM("selectionPos", function(cm) { |
michael@0 | 656 | if (phantom) return; |
michael@0 | 657 | cm.setSize(100, 100); |
michael@0 | 658 | addDoc(cm, 200, 100); |
michael@0 | 659 | cm.setSelection(Pos(1, 100), Pos(98, 100)); |
michael@0 | 660 | var lineWidth = cm.charCoords(Pos(0, 200), "local").left; |
michael@0 | 661 | var lineHeight = (cm.charCoords(Pos(99)).top - cm.charCoords(Pos(0)).top) / 100; |
michael@0 | 662 | cm.scrollTo(0, 0); |
michael@0 | 663 | var selElt = byClassName(cm.getWrapperElement(), "CodeMirror-selected"); |
michael@0 | 664 | var outer = cm.getWrapperElement().getBoundingClientRect(); |
michael@0 | 665 | var sawMiddle, sawTop, sawBottom; |
michael@0 | 666 | for (var i = 0, e = selElt.length; i < e; ++i) { |
michael@0 | 667 | var box = selElt[i].getBoundingClientRect(); |
michael@0 | 668 | var atLeft = box.left - outer.left < 30; |
michael@0 | 669 | var width = box.right - box.left; |
michael@0 | 670 | var atRight = box.right - outer.left > .8 * lineWidth; |
michael@0 | 671 | if (atLeft && atRight) { |
michael@0 | 672 | sawMiddle = true; |
michael@0 | 673 | is(box.bottom - box.top > 90 * lineHeight, "middle high"); |
michael@0 | 674 | is(width > .9 * lineWidth, "middle wide"); |
michael@0 | 675 | } else { |
michael@0 | 676 | is(width > .4 * lineWidth, "top/bot wide enough"); |
michael@0 | 677 | is(width < .6 * lineWidth, "top/bot slim enough"); |
michael@0 | 678 | if (atLeft) { |
michael@0 | 679 | sawBottom = true; |
michael@0 | 680 | is(box.top - outer.top > 96 * lineHeight, "bot below"); |
michael@0 | 681 | } else if (atRight) { |
michael@0 | 682 | sawTop = true; |
michael@0 | 683 | is(box.top - outer.top < 2.1 * lineHeight, "top above"); |
michael@0 | 684 | } |
michael@0 | 685 | } |
michael@0 | 686 | } |
michael@0 | 687 | is(sawTop && sawBottom && sawMiddle, "all parts"); |
michael@0 | 688 | }, null); |
michael@0 | 689 | |
michael@0 | 690 | testCM("restoreHistory", function(cm) { |
michael@0 | 691 | cm.setValue("abc\ndef"); |
michael@0 | 692 | cm.replaceRange("hello", Pos(1, 0), Pos(1)); |
michael@0 | 693 | cm.replaceRange("goop", Pos(0, 0), Pos(0)); |
michael@0 | 694 | cm.undo(); |
michael@0 | 695 | var storedVal = cm.getValue(), storedHist = cm.getHistory(); |
michael@0 | 696 | if (window.JSON) storedHist = JSON.parse(JSON.stringify(storedHist)); |
michael@0 | 697 | eq(storedVal, "abc\nhello"); |
michael@0 | 698 | cm.setValue(""); |
michael@0 | 699 | cm.clearHistory(); |
michael@0 | 700 | eq(cm.historySize().undo, 0); |
michael@0 | 701 | cm.setValue(storedVal); |
michael@0 | 702 | cm.setHistory(storedHist); |
michael@0 | 703 | cm.redo(); |
michael@0 | 704 | eq(cm.getValue(), "goop\nhello"); |
michael@0 | 705 | cm.undo(); cm.undo(); |
michael@0 | 706 | eq(cm.getValue(), "abc\ndef"); |
michael@0 | 707 | }); |
michael@0 | 708 | |
michael@0 | 709 | testCM("doubleScrollbar", function(cm) { |
michael@0 | 710 | var dummy = document.body.appendChild(document.createElement("p")); |
michael@0 | 711 | dummy.style.cssText = "height: 50px; overflow: scroll; width: 50px"; |
michael@0 | 712 | var scrollbarWidth = dummy.offsetWidth + 1 - dummy.clientWidth; |
michael@0 | 713 | document.body.removeChild(dummy); |
michael@0 | 714 | if (scrollbarWidth < 2) return; |
michael@0 | 715 | cm.setSize(null, 100); |
michael@0 | 716 | addDoc(cm, 1, 300); |
michael@0 | 717 | var wrap = cm.getWrapperElement(); |
michael@0 | 718 | is(wrap.offsetWidth - byClassName(wrap, "CodeMirror-lines")[0].offsetWidth <= scrollbarWidth * 1.5); |
michael@0 | 719 | }); |
michael@0 | 720 | |
michael@0 | 721 | testCM("weirdLinebreaks", function(cm) { |
michael@0 | 722 | cm.setValue("foo\nbar\rbaz\r\nquux\n\rplop"); |
michael@0 | 723 | is(cm.getValue(), "foo\nbar\nbaz\nquux\n\nplop"); |
michael@0 | 724 | is(cm.lineCount(), 6); |
michael@0 | 725 | cm.setValue("\n\n"); |
michael@0 | 726 | is(cm.lineCount(), 3); |
michael@0 | 727 | }); |
michael@0 | 728 | |
michael@0 | 729 | testCM("setSize", function(cm) { |
michael@0 | 730 | cm.setSize(100, 100); |
michael@0 | 731 | var wrap = cm.getWrapperElement(); |
michael@0 | 732 | is(wrap.offsetWidth, 100); |
michael@0 | 733 | is(wrap.offsetHeight, 100); |
michael@0 | 734 | cm.setSize("100%", "3em"); |
michael@0 | 735 | is(wrap.style.width, "100%"); |
michael@0 | 736 | is(wrap.style.height, "3em"); |
michael@0 | 737 | cm.setSize(null, 40); |
michael@0 | 738 | is(wrap.style.width, "100%"); |
michael@0 | 739 | is(wrap.style.height, "40px"); |
michael@0 | 740 | }); |
michael@0 | 741 | |
michael@0 | 742 | function foldLines(cm, start, end, autoClear) { |
michael@0 | 743 | return cm.markText(Pos(start, 0), Pos(end - 1), { |
michael@0 | 744 | inclusiveLeft: true, |
michael@0 | 745 | inclusiveRight: true, |
michael@0 | 746 | collapsed: true, |
michael@0 | 747 | clearOnEnter: autoClear |
michael@0 | 748 | }); |
michael@0 | 749 | } |
michael@0 | 750 | |
michael@0 | 751 | testCM("collapsedLines", function(cm) { |
michael@0 | 752 | addDoc(cm, 4, 10); |
michael@0 | 753 | var range = foldLines(cm, 4, 5), cleared = 0; |
michael@0 | 754 | CodeMirror.on(range, "clear", function() {cleared++;}); |
michael@0 | 755 | cm.setCursor(Pos(3, 0)); |
michael@0 | 756 | CodeMirror.commands.goLineDown(cm); |
michael@0 | 757 | eqPos(cm.getCursor(), Pos(5, 0)); |
michael@0 | 758 | cm.replaceRange("abcdefg", Pos(3, 0), Pos(3)); |
michael@0 | 759 | cm.setCursor(Pos(3, 6)); |
michael@0 | 760 | CodeMirror.commands.goLineDown(cm); |
michael@0 | 761 | eqPos(cm.getCursor(), Pos(5, 4)); |
michael@0 | 762 | cm.replaceRange("ab", Pos(3, 0), Pos(3)); |
michael@0 | 763 | cm.setCursor(Pos(3, 2)); |
michael@0 | 764 | CodeMirror.commands.goLineDown(cm); |
michael@0 | 765 | eqPos(cm.getCursor(), Pos(5, 2)); |
michael@0 | 766 | cm.operation(function() {range.clear(); range.clear();}); |
michael@0 | 767 | eq(cleared, 1); |
michael@0 | 768 | }); |
michael@0 | 769 | |
michael@0 | 770 | testCM("collapsedRangeCoordsChar", function(cm) { |
michael@0 | 771 | var pos_1_3 = cm.charCoords(Pos(1, 3)); |
michael@0 | 772 | pos_1_3.left += 2; pos_1_3.top += 2; |
michael@0 | 773 | var opts = {collapsed: true, inclusiveLeft: true, inclusiveRight: true}; |
michael@0 | 774 | var m1 = cm.markText(Pos(0, 0), Pos(2, 0), opts); |
michael@0 | 775 | eqPos(cm.coordsChar(pos_1_3), Pos(3, 3)); |
michael@0 | 776 | m1.clear(); |
michael@0 | 777 | var m1 = cm.markText(Pos(0, 0), Pos(1, 1), {collapsed: true, inclusiveLeft: true}); |
michael@0 | 778 | var m2 = cm.markText(Pos(1, 1), Pos(2, 0), {collapsed: true, inclusiveRight: true}); |
michael@0 | 779 | eqPos(cm.coordsChar(pos_1_3), Pos(3, 3)); |
michael@0 | 780 | m1.clear(); m2.clear(); |
michael@0 | 781 | var m1 = cm.markText(Pos(0, 0), Pos(1, 6), opts); |
michael@0 | 782 | eqPos(cm.coordsChar(pos_1_3), Pos(3, 3)); |
michael@0 | 783 | }, {value: "123456\nabcdef\nghijkl\nmnopqr\n"}); |
michael@0 | 784 | |
michael@0 | 785 | testCM("collapsedRangeBetweenLinesSelected", function(cm) { |
michael@0 | 786 | var widget = document.createElement("span"); |
michael@0 | 787 | widget.textContent = "\u2194"; |
michael@0 | 788 | cm.markText(Pos(0, 3), Pos(1, 0), {replacedWith: widget}); |
michael@0 | 789 | cm.setSelection(Pos(0, 3), Pos(1, 0)); |
michael@0 | 790 | var selElts = byClassName(cm.getWrapperElement(), "CodeMirror-selected"); |
michael@0 | 791 | for (var i = 0, w = 0; i < selElts.length; i++) |
michael@0 | 792 | w += selElts[i].offsetWidth; |
michael@0 | 793 | is(w > 0); |
michael@0 | 794 | }, {value: "one\ntwo"}); |
michael@0 | 795 | |
michael@0 | 796 | testCM("randomCollapsedRanges", function(cm) { |
michael@0 | 797 | addDoc(cm, 20, 500); |
michael@0 | 798 | cm.operation(function() { |
michael@0 | 799 | for (var i = 0; i < 200; i++) { |
michael@0 | 800 | var start = Pos(Math.floor(Math.random() * 500), Math.floor(Math.random() * 20)); |
michael@0 | 801 | if (i % 4) |
michael@0 | 802 | try { cm.markText(start, Pos(start.line + 2, 1), {collapsed: true}); } |
michael@0 | 803 | catch(e) { if (!/overlapping/.test(String(e))) throw e; } |
michael@0 | 804 | else |
michael@0 | 805 | cm.markText(start, Pos(start.line, start.ch + 4), {"className": "foo"}); |
michael@0 | 806 | } |
michael@0 | 807 | }); |
michael@0 | 808 | }); |
michael@0 | 809 | |
michael@0 | 810 | testCM("hiddenLinesAutoUnfold", function(cm) { |
michael@0 | 811 | var range = foldLines(cm, 1, 3, true), cleared = 0; |
michael@0 | 812 | CodeMirror.on(range, "clear", function() {cleared++;}); |
michael@0 | 813 | cm.setCursor(Pos(3, 0)); |
michael@0 | 814 | eq(cleared, 0); |
michael@0 | 815 | cm.execCommand("goCharLeft"); |
michael@0 | 816 | eq(cleared, 1); |
michael@0 | 817 | range = foldLines(cm, 1, 3, true); |
michael@0 | 818 | CodeMirror.on(range, "clear", function() {cleared++;}); |
michael@0 | 819 | eqPos(cm.getCursor(), Pos(3, 0)); |
michael@0 | 820 | cm.setCursor(Pos(0, 3)); |
michael@0 | 821 | cm.execCommand("goCharRight"); |
michael@0 | 822 | eq(cleared, 2); |
michael@0 | 823 | }, {value: "abc\ndef\nghi\njkl"}); |
michael@0 | 824 | |
michael@0 | 825 | testCM("hiddenLinesSelectAll", function(cm) { // Issue #484 |
michael@0 | 826 | addDoc(cm, 4, 20); |
michael@0 | 827 | foldLines(cm, 0, 10); |
michael@0 | 828 | foldLines(cm, 11, 20); |
michael@0 | 829 | CodeMirror.commands.selectAll(cm); |
michael@0 | 830 | eqPos(cm.getCursor(true), Pos(10, 0)); |
michael@0 | 831 | eqPos(cm.getCursor(false), Pos(10, 4)); |
michael@0 | 832 | }); |
michael@0 | 833 | |
michael@0 | 834 | |
michael@0 | 835 | testCM("everythingFolded", function(cm) { |
michael@0 | 836 | addDoc(cm, 2, 2); |
michael@0 | 837 | function enterPress() { |
michael@0 | 838 | cm.triggerOnKeyDown({type: "keydown", keyCode: 13, preventDefault: function(){}, stopPropagation: function(){}}); |
michael@0 | 839 | } |
michael@0 | 840 | var fold = foldLines(cm, 0, 2); |
michael@0 | 841 | enterPress(); |
michael@0 | 842 | eq(cm.getValue(), "xx\nxx"); |
michael@0 | 843 | fold.clear(); |
michael@0 | 844 | fold = foldLines(cm, 0, 2, true); |
michael@0 | 845 | eq(fold.find(), null); |
michael@0 | 846 | enterPress(); |
michael@0 | 847 | eq(cm.getValue(), "\nxx\nxx"); |
michael@0 | 848 | }); |
michael@0 | 849 | |
michael@0 | 850 | testCM("structuredFold", function(cm) { |
michael@0 | 851 | if (phantom) return; |
michael@0 | 852 | addDoc(cm, 4, 8); |
michael@0 | 853 | var range = cm.markText(Pos(1, 2), Pos(6, 2), { |
michael@0 | 854 | replacedWith: document.createTextNode("Q") |
michael@0 | 855 | }); |
michael@0 | 856 | cm.setCursor(0, 3); |
michael@0 | 857 | CodeMirror.commands.goLineDown(cm); |
michael@0 | 858 | eqPos(cm.getCursor(), Pos(6, 2)); |
michael@0 | 859 | CodeMirror.commands.goCharLeft(cm); |
michael@0 | 860 | eqPos(cm.getCursor(), Pos(1, 2)); |
michael@0 | 861 | CodeMirror.commands.delCharAfter(cm); |
michael@0 | 862 | eq(cm.getValue(), "xxxx\nxxxx\nxxxx"); |
michael@0 | 863 | addDoc(cm, 4, 8); |
michael@0 | 864 | range = cm.markText(Pos(1, 2), Pos(6, 2), { |
michael@0 | 865 | replacedWith: document.createTextNode("M"), |
michael@0 | 866 | clearOnEnter: true |
michael@0 | 867 | }); |
michael@0 | 868 | var cleared = 0; |
michael@0 | 869 | CodeMirror.on(range, "clear", function(){++cleared;}); |
michael@0 | 870 | cm.setCursor(0, 3); |
michael@0 | 871 | CodeMirror.commands.goLineDown(cm); |
michael@0 | 872 | eqPos(cm.getCursor(), Pos(6, 2)); |
michael@0 | 873 | CodeMirror.commands.goCharLeft(cm); |
michael@0 | 874 | eqPos(cm.getCursor(), Pos(6, 1)); |
michael@0 | 875 | eq(cleared, 1); |
michael@0 | 876 | range.clear(); |
michael@0 | 877 | eq(cleared, 1); |
michael@0 | 878 | range = cm.markText(Pos(1, 2), Pos(6, 2), { |
michael@0 | 879 | replacedWith: document.createTextNode("Q"), |
michael@0 | 880 | clearOnEnter: true |
michael@0 | 881 | }); |
michael@0 | 882 | range.clear(); |
michael@0 | 883 | cm.setCursor(1, 2); |
michael@0 | 884 | CodeMirror.commands.goCharRight(cm); |
michael@0 | 885 | eqPos(cm.getCursor(), Pos(1, 3)); |
michael@0 | 886 | range = cm.markText(Pos(2, 0), Pos(4, 4), { |
michael@0 | 887 | replacedWith: document.createTextNode("M") |
michael@0 | 888 | }); |
michael@0 | 889 | cm.setCursor(1, 0); |
michael@0 | 890 | CodeMirror.commands.goLineDown(cm); |
michael@0 | 891 | eqPos(cm.getCursor(), Pos(2, 0)); |
michael@0 | 892 | }, null); |
michael@0 | 893 | |
michael@0 | 894 | testCM("nestedFold", function(cm) { |
michael@0 | 895 | addDoc(cm, 10, 3); |
michael@0 | 896 | function fold(ll, cl, lr, cr) { |
michael@0 | 897 | return cm.markText(Pos(ll, cl), Pos(lr, cr), {collapsed: true}); |
michael@0 | 898 | } |
michael@0 | 899 | var inner1 = fold(0, 6, 1, 3), inner2 = fold(0, 2, 1, 8), outer = fold(0, 1, 2, 3), inner0 = fold(0, 5, 0, 6); |
michael@0 | 900 | cm.setCursor(0, 1); |
michael@0 | 901 | CodeMirror.commands.goCharRight(cm); |
michael@0 | 902 | eqPos(cm.getCursor(), Pos(2, 3)); |
michael@0 | 903 | inner0.clear(); |
michael@0 | 904 | CodeMirror.commands.goCharLeft(cm); |
michael@0 | 905 | eqPos(cm.getCursor(), Pos(0, 1)); |
michael@0 | 906 | outer.clear(); |
michael@0 | 907 | CodeMirror.commands.goCharRight(cm); |
michael@0 | 908 | eqPos(cm.getCursor(), Pos(0, 2)); |
michael@0 | 909 | CodeMirror.commands.goCharRight(cm); |
michael@0 | 910 | eqPos(cm.getCursor(), Pos(1, 8)); |
michael@0 | 911 | inner2.clear(); |
michael@0 | 912 | CodeMirror.commands.goCharLeft(cm); |
michael@0 | 913 | eqPos(cm.getCursor(), Pos(1, 7)); |
michael@0 | 914 | cm.setCursor(0, 5); |
michael@0 | 915 | CodeMirror.commands.goCharRight(cm); |
michael@0 | 916 | eqPos(cm.getCursor(), Pos(0, 6)); |
michael@0 | 917 | CodeMirror.commands.goCharRight(cm); |
michael@0 | 918 | eqPos(cm.getCursor(), Pos(1, 3)); |
michael@0 | 919 | }); |
michael@0 | 920 | |
michael@0 | 921 | testCM("badNestedFold", function(cm) { |
michael@0 | 922 | addDoc(cm, 4, 4); |
michael@0 | 923 | cm.markText(Pos(0, 2), Pos(3, 2), {collapsed: true}); |
michael@0 | 924 | var caught; |
michael@0 | 925 | try {cm.markText(Pos(0, 1), Pos(0, 3), {collapsed: true});} |
michael@0 | 926 | catch(e) {caught = e;} |
michael@0 | 927 | is(caught instanceof Error, "no error"); |
michael@0 | 928 | is(/overlap/i.test(caught.message), "wrong error"); |
michael@0 | 929 | }); |
michael@0 | 930 | |
michael@0 | 931 | testCM("nestedFoldOnSide", function(cm) { |
michael@0 | 932 | var m1 = cm.markText(Pos(0, 1), Pos(2, 1), {collapsed: true, inclusiveRight: true}); |
michael@0 | 933 | var m2 = cm.markText(Pos(0, 1), Pos(0, 2), {collapsed: true}); |
michael@0 | 934 | cm.markText(Pos(0, 1), Pos(0, 2), {collapsed: true}).clear(); |
michael@0 | 935 | try { cm.markText(Pos(0, 1), Pos(0, 2), {collapsed: true, inclusiveLeft: true}); } |
michael@0 | 936 | catch(e) { var caught = e; } |
michael@0 | 937 | is(caught && /overlap/i.test(caught.message)); |
michael@0 | 938 | var m3 = cm.markText(Pos(2, 0), Pos(2, 1), {collapsed: true}); |
michael@0 | 939 | var m4 = cm.markText(Pos(2, 0), Pos(2, 1), {collapse: true, inclusiveRight: true}); |
michael@0 | 940 | m1.clear(); m4.clear(); |
michael@0 | 941 | m1 = cm.markText(Pos(0, 1), Pos(2, 1), {collapsed: true}); |
michael@0 | 942 | cm.markText(Pos(2, 0), Pos(2, 1), {collapsed: true}).clear(); |
michael@0 | 943 | try { cm.markText(Pos(2, 0), Pos(2, 1), {collapsed: true, inclusiveRight: true}); } |
michael@0 | 944 | catch(e) { var caught = e; } |
michael@0 | 945 | is(caught && /overlap/i.test(caught.message)); |
michael@0 | 946 | }, {value: "ab\ncd\ef"}); |
michael@0 | 947 | |
michael@0 | 948 | testCM("editInFold", function(cm) { |
michael@0 | 949 | addDoc(cm, 4, 6); |
michael@0 | 950 | var m = cm.markText(Pos(1, 2), Pos(3, 2), {collapsed: true}); |
michael@0 | 951 | cm.replaceRange("", Pos(0, 0), Pos(1, 3)); |
michael@0 | 952 | cm.replaceRange("", Pos(2, 1), Pos(3, 3)); |
michael@0 | 953 | cm.replaceRange("a\nb\nc\nd", Pos(0, 1), Pos(1, 0)); |
michael@0 | 954 | cm.cursorCoords(Pos(0, 0)); |
michael@0 | 955 | }); |
michael@0 | 956 | |
michael@0 | 957 | testCM("wrappingInlineWidget", function(cm) { |
michael@0 | 958 | cm.setSize("11em"); |
michael@0 | 959 | var w = document.createElement("span"); |
michael@0 | 960 | w.style.color = "red"; |
michael@0 | 961 | w.innerHTML = "one two three four"; |
michael@0 | 962 | cm.markText(Pos(0, 6), Pos(0, 9), {replacedWith: w}); |
michael@0 | 963 | var cur0 = cm.cursorCoords(Pos(0, 0)), cur1 = cm.cursorCoords(Pos(0, 10)); |
michael@0 | 964 | is(cur0.top < cur1.top); |
michael@0 | 965 | is(cur0.bottom < cur1.bottom); |
michael@0 | 966 | var curL = cm.cursorCoords(Pos(0, 6)), curR = cm.cursorCoords(Pos(0, 9)); |
michael@0 | 967 | eq(curL.top, cur0.top); |
michael@0 | 968 | eq(curL.bottom, cur0.bottom); |
michael@0 | 969 | eq(curR.top, cur1.top); |
michael@0 | 970 | eq(curR.bottom, cur1.bottom); |
michael@0 | 971 | cm.replaceRange("", Pos(0, 9), Pos(0)); |
michael@0 | 972 | curR = cm.cursorCoords(Pos(0, 9)); |
michael@0 | 973 | if (phantom) return; |
michael@0 | 974 | eq(curR.top, cur1.top); |
michael@0 | 975 | eq(curR.bottom, cur1.bottom); |
michael@0 | 976 | }, {value: "1 2 3 xxx 4", lineWrapping: true}); |
michael@0 | 977 | |
michael@0 | 978 | testCM("changedInlineWidget", function(cm) { |
michael@0 | 979 | cm.setSize("10em"); |
michael@0 | 980 | var w = document.createElement("span"); |
michael@0 | 981 | w.innerHTML = "x"; |
michael@0 | 982 | var m = cm.markText(Pos(0, 4), Pos(0, 5), {replacedWith: w}); |
michael@0 | 983 | w.innerHTML = "and now the widget is really really long all of a sudden and a scrollbar is needed"; |
michael@0 | 984 | m.changed(); |
michael@0 | 985 | var hScroll = byClassName(cm.getWrapperElement(), "CodeMirror-hscrollbar")[0]; |
michael@0 | 986 | is(hScroll.scrollWidth > hScroll.clientWidth); |
michael@0 | 987 | }, {value: "hello there"}); |
michael@0 | 988 | |
michael@0 | 989 | testCM("changedBookmark", function(cm) { |
michael@0 | 990 | cm.setSize("10em"); |
michael@0 | 991 | var w = document.createElement("span"); |
michael@0 | 992 | w.innerHTML = "x"; |
michael@0 | 993 | var m = cm.setBookmark(Pos(0, 4), {widget: w}); |
michael@0 | 994 | w.innerHTML = "and now the widget is really really long all of a sudden and a scrollbar is needed"; |
michael@0 | 995 | m.changed(); |
michael@0 | 996 | var hScroll = byClassName(cm.getWrapperElement(), "CodeMirror-hscrollbar")[0]; |
michael@0 | 997 | is(hScroll.scrollWidth > hScroll.clientWidth); |
michael@0 | 998 | }, {value: "abcdefg"}); |
michael@0 | 999 | |
michael@0 | 1000 | testCM("inlineWidget", function(cm) { |
michael@0 | 1001 | var w = cm.setBookmark(Pos(0, 2), {widget: document.createTextNode("uu")}); |
michael@0 | 1002 | cm.setCursor(0, 2); |
michael@0 | 1003 | CodeMirror.commands.goLineDown(cm); |
michael@0 | 1004 | eqPos(cm.getCursor(), Pos(1, 4)); |
michael@0 | 1005 | cm.setCursor(0, 2); |
michael@0 | 1006 | cm.replaceSelection("hi"); |
michael@0 | 1007 | eqPos(w.find(), Pos(0, 2)); |
michael@0 | 1008 | cm.setCursor(0, 1); |
michael@0 | 1009 | cm.replaceSelection("ay"); |
michael@0 | 1010 | eqPos(w.find(), Pos(0, 4)); |
michael@0 | 1011 | eq(cm.getLine(0), "uayuhiuu"); |
michael@0 | 1012 | }, {value: "uuuu\nuuuuuu"}); |
michael@0 | 1013 | |
michael@0 | 1014 | testCM("wrappingAndResizing", function(cm) { |
michael@0 | 1015 | cm.setSize(null, "auto"); |
michael@0 | 1016 | cm.setOption("lineWrapping", true); |
michael@0 | 1017 | var wrap = cm.getWrapperElement(), h0 = wrap.offsetHeight; |
michael@0 | 1018 | var doc = "xxx xxx xxx xxx xxx"; |
michael@0 | 1019 | cm.setValue(doc); |
michael@0 | 1020 | for (var step = 10, w = cm.charCoords(Pos(0, 18), "div").right;; w += step) { |
michael@0 | 1021 | cm.setSize(w); |
michael@0 | 1022 | if (wrap.offsetHeight <= h0 * (opera_lt10 ? 1.2 : 1.5)) { |
michael@0 | 1023 | if (step == 10) { w -= 10; step = 1; } |
michael@0 | 1024 | else break; |
michael@0 | 1025 | } |
michael@0 | 1026 | } |
michael@0 | 1027 | // Ensure that putting the cursor at the end of the maximally long |
michael@0 | 1028 | // line doesn't cause wrapping to happen. |
michael@0 | 1029 | cm.setCursor(Pos(0, doc.length)); |
michael@0 | 1030 | eq(wrap.offsetHeight, h0); |
michael@0 | 1031 | cm.replaceSelection("x"); |
michael@0 | 1032 | is(wrap.offsetHeight > h0, "wrapping happens"); |
michael@0 | 1033 | // Now add a max-height and, in a document consisting of |
michael@0 | 1034 | // almost-wrapped lines, go over it so that a scrollbar appears. |
michael@0 | 1035 | cm.setValue(doc + "\n" + doc + "\n"); |
michael@0 | 1036 | cm.getScrollerElement().style.maxHeight = "100px"; |
michael@0 | 1037 | cm.replaceRange("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n!\n", Pos(2, 0)); |
michael@0 | 1038 | forEach([Pos(0, doc.length), Pos(0, doc.length - 1), |
michael@0 | 1039 | Pos(0, 0), Pos(1, doc.length), Pos(1, doc.length - 1)], |
michael@0 | 1040 | function(pos) { |
michael@0 | 1041 | var coords = cm.charCoords(pos); |
michael@0 | 1042 | eqPos(pos, cm.coordsChar({left: coords.left + 2, top: coords.top + 5})); |
michael@0 | 1043 | }); |
michael@0 | 1044 | }, null, ie_lt8); |
michael@0 | 1045 | |
michael@0 | 1046 | testCM("measureEndOfLine", function(cm) { |
michael@0 | 1047 | cm.setSize(null, "auto"); |
michael@0 | 1048 | var inner = byClassName(cm.getWrapperElement(), "CodeMirror-lines")[0].firstChild; |
michael@0 | 1049 | var lh = inner.offsetHeight; |
michael@0 | 1050 | for (var step = 10, w = cm.charCoords(Pos(0, 7), "div").right;; w += step) { |
michael@0 | 1051 | cm.setSize(w); |
michael@0 | 1052 | if (inner.offsetHeight < 2.5 * lh) { |
michael@0 | 1053 | if (step == 10) { w -= 10; step = 1; } |
michael@0 | 1054 | else break; |
michael@0 | 1055 | } |
michael@0 | 1056 | } |
michael@0 | 1057 | cm.setValue(cm.getValue() + "\n\n"); |
michael@0 | 1058 | var endPos = cm.charCoords(Pos(0, 18), "local"); |
michael@0 | 1059 | is(endPos.top > lh * .8, "not at top"); |
michael@0 | 1060 | is(endPos.left > w - 20, "not at right"); |
michael@0 | 1061 | endPos = cm.charCoords(Pos(0, 18)); |
michael@0 | 1062 | eqPos(cm.coordsChar({left: endPos.left, top: endPos.top + 5}), Pos(0, 18)); |
michael@0 | 1063 | }, {mode: "text/html", value: "<!-- foo barrr -->", lineWrapping: true}, ie_lt8 || opera_lt10); |
michael@0 | 1064 | |
michael@0 | 1065 | testCM("scrollVerticallyAndHorizontally", function(cm) { |
michael@0 | 1066 | cm.setSize(100, 100); |
michael@0 | 1067 | addDoc(cm, 40, 40); |
michael@0 | 1068 | cm.setCursor(39); |
michael@0 | 1069 | var wrap = cm.getWrapperElement(), bar = byClassName(wrap, "CodeMirror-vscrollbar")[0]; |
michael@0 | 1070 | is(bar.offsetHeight < wrap.offsetHeight, "vertical scrollbar limited by horizontal one"); |
michael@0 | 1071 | var cursorBox = byClassName(wrap, "CodeMirror-cursor")[0].getBoundingClientRect(); |
michael@0 | 1072 | var editorBox = wrap.getBoundingClientRect(); |
michael@0 | 1073 | is(cursorBox.bottom < editorBox.top + cm.getScrollerElement().clientHeight, |
michael@0 | 1074 | "bottom line visible"); |
michael@0 | 1075 | }, {lineNumbers: true}); |
michael@0 | 1076 | |
michael@0 | 1077 | testCM("moveVstuck", function(cm) { |
michael@0 | 1078 | var lines = byClassName(cm.getWrapperElement(), "CodeMirror-lines")[0].firstChild, h0 = lines.offsetHeight; |
michael@0 | 1079 | var val = "fooooooooooooooooooooooooo baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaar\n"; |
michael@0 | 1080 | cm.setValue(val); |
michael@0 | 1081 | for (var w = cm.charCoords(Pos(0, 26), "div").right * 2.8;; w += 5) { |
michael@0 | 1082 | cm.setSize(w); |
michael@0 | 1083 | if (lines.offsetHeight <= 3.5 * h0) break; |
michael@0 | 1084 | } |
michael@0 | 1085 | cm.setCursor(Pos(0, val.length - 1)); |
michael@0 | 1086 | cm.moveV(-1, "line"); |
michael@0 | 1087 | eqPos(cm.getCursor(), Pos(0, 26)); |
michael@0 | 1088 | }, {lineWrapping: true}, ie_lt8 || opera_lt10); |
michael@0 | 1089 | |
michael@0 | 1090 | testCM("collapseOnMove", function(cm) { |
michael@0 | 1091 | cm.setSelection(Pos(0, 1), Pos(2, 4)); |
michael@0 | 1092 | cm.execCommand("goLineUp"); |
michael@0 | 1093 | is(!cm.somethingSelected()); |
michael@0 | 1094 | eqPos(cm.getCursor(), Pos(0, 1)); |
michael@0 | 1095 | cm.setSelection(Pos(0, 1), Pos(2, 4)); |
michael@0 | 1096 | cm.execCommand("goPageDown"); |
michael@0 | 1097 | is(!cm.somethingSelected()); |
michael@0 | 1098 | eqPos(cm.getCursor(), Pos(2, 4)); |
michael@0 | 1099 | cm.execCommand("goLineUp"); |
michael@0 | 1100 | cm.execCommand("goLineUp"); |
michael@0 | 1101 | eqPos(cm.getCursor(), Pos(0, 4)); |
michael@0 | 1102 | cm.setSelection(Pos(0, 1), Pos(2, 4)); |
michael@0 | 1103 | cm.execCommand("goCharLeft"); |
michael@0 | 1104 | is(!cm.somethingSelected()); |
michael@0 | 1105 | eqPos(cm.getCursor(), Pos(0, 1)); |
michael@0 | 1106 | }, {value: "aaaaa\nb\nccccc"}); |
michael@0 | 1107 | |
michael@0 | 1108 | testCM("clickTab", function(cm) { |
michael@0 | 1109 | var p0 = cm.charCoords(Pos(0, 0)); |
michael@0 | 1110 | eqPos(cm.coordsChar({left: p0.left + 5, top: p0.top + 5}), Pos(0, 0)); |
michael@0 | 1111 | eqPos(cm.coordsChar({left: p0.right - 5, top: p0.top + 5}), Pos(0, 1)); |
michael@0 | 1112 | }, {value: "\t\n\n", lineWrapping: true, tabSize: 8}); |
michael@0 | 1113 | |
michael@0 | 1114 | testCM("verticalScroll", function(cm) { |
michael@0 | 1115 | cm.setSize(100, 200); |
michael@0 | 1116 | cm.setValue("foo\nbar\nbaz\n"); |
michael@0 | 1117 | var sc = cm.getScrollerElement(), baseWidth = sc.scrollWidth; |
michael@0 | 1118 | cm.replaceRange("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah", Pos(0, 0), Pos(0)); |
michael@0 | 1119 | is(sc.scrollWidth > baseWidth, "scrollbar present"); |
michael@0 | 1120 | cm.replaceRange("foo", Pos(0, 0), Pos(0)); |
michael@0 | 1121 | if (!phantom) eq(sc.scrollWidth, baseWidth, "scrollbar gone"); |
michael@0 | 1122 | cm.replaceRange("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah", Pos(0, 0), Pos(0)); |
michael@0 | 1123 | cm.replaceRange("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbh", Pos(1, 0), Pos(1)); |
michael@0 | 1124 | is(sc.scrollWidth > baseWidth, "present again"); |
michael@0 | 1125 | var curWidth = sc.scrollWidth; |
michael@0 | 1126 | cm.replaceRange("foo", Pos(0, 0), Pos(0)); |
michael@0 | 1127 | is(sc.scrollWidth < curWidth, "scrollbar smaller"); |
michael@0 | 1128 | is(sc.scrollWidth > baseWidth, "but still present"); |
michael@0 | 1129 | }); |
michael@0 | 1130 | |
michael@0 | 1131 | testCM("extraKeys", function(cm) { |
michael@0 | 1132 | var outcome; |
michael@0 | 1133 | function fakeKey(expected, code, props) { |
michael@0 | 1134 | if (typeof code == "string") code = code.charCodeAt(0); |
michael@0 | 1135 | var e = {type: "keydown", keyCode: code, preventDefault: function(){}, stopPropagation: function(){}}; |
michael@0 | 1136 | if (props) for (var n in props) e[n] = props[n]; |
michael@0 | 1137 | outcome = null; |
michael@0 | 1138 | cm.triggerOnKeyDown(e); |
michael@0 | 1139 | eq(outcome, expected); |
michael@0 | 1140 | } |
michael@0 | 1141 | CodeMirror.commands.testCommand = function() {outcome = "tc";}; |
michael@0 | 1142 | CodeMirror.commands.goTestCommand = function() {outcome = "gtc";}; |
michael@0 | 1143 | cm.setOption("extraKeys", {"Shift-X": function() {outcome = "sx";}, |
michael@0 | 1144 | "X": function() {outcome = "x";}, |
michael@0 | 1145 | "Ctrl-Alt-U": function() {outcome = "cau";}, |
michael@0 | 1146 | "End": "testCommand", |
michael@0 | 1147 | "Home": "goTestCommand", |
michael@0 | 1148 | "Tab": false}); |
michael@0 | 1149 | fakeKey(null, "U"); |
michael@0 | 1150 | fakeKey("cau", "U", {ctrlKey: true, altKey: true}); |
michael@0 | 1151 | fakeKey(null, "U", {shiftKey: true, ctrlKey: true, altKey: true}); |
michael@0 | 1152 | fakeKey("x", "X"); |
michael@0 | 1153 | fakeKey("sx", "X", {shiftKey: true}); |
michael@0 | 1154 | fakeKey("tc", 35); |
michael@0 | 1155 | fakeKey(null, 35, {shiftKey: true}); |
michael@0 | 1156 | fakeKey("gtc", 36); |
michael@0 | 1157 | fakeKey("gtc", 36, {shiftKey: true}); |
michael@0 | 1158 | fakeKey(null, 9); |
michael@0 | 1159 | }, null, window.opera && mac); |
michael@0 | 1160 | |
michael@0 | 1161 | testCM("wordMovementCommands", function(cm) { |
michael@0 | 1162 | cm.execCommand("goWordLeft"); |
michael@0 | 1163 | eqPos(cm.getCursor(), Pos(0, 0)); |
michael@0 | 1164 | cm.execCommand("goWordRight"); cm.execCommand("goWordRight"); |
michael@0 | 1165 | eqPos(cm.getCursor(), Pos(0, 7)); |
michael@0 | 1166 | cm.execCommand("goWordLeft"); |
michael@0 | 1167 | eqPos(cm.getCursor(), Pos(0, 5)); |
michael@0 | 1168 | cm.execCommand("goWordRight"); cm.execCommand("goWordRight"); |
michael@0 | 1169 | eqPos(cm.getCursor(), Pos(0, 12)); |
michael@0 | 1170 | cm.execCommand("goWordLeft"); |
michael@0 | 1171 | eqPos(cm.getCursor(), Pos(0, 9)); |
michael@0 | 1172 | cm.execCommand("goWordRight"); cm.execCommand("goWordRight"); cm.execCommand("goWordRight"); |
michael@0 | 1173 | eqPos(cm.getCursor(), Pos(0, 24)); |
michael@0 | 1174 | cm.execCommand("goWordRight"); cm.execCommand("goWordRight"); |
michael@0 | 1175 | eqPos(cm.getCursor(), Pos(1, 9)); |
michael@0 | 1176 | cm.execCommand("goWordRight"); |
michael@0 | 1177 | eqPos(cm.getCursor(), Pos(1, 13)); |
michael@0 | 1178 | cm.execCommand("goWordRight"); cm.execCommand("goWordRight"); |
michael@0 | 1179 | eqPos(cm.getCursor(), Pos(2, 0)); |
michael@0 | 1180 | }, {value: "this is (the) firstline.\na foo12\u00e9\u00f8\u00d7bar\n"}); |
michael@0 | 1181 | |
michael@0 | 1182 | testCM("groupMovementCommands", function(cm) { |
michael@0 | 1183 | cm.execCommand("goGroupLeft"); |
michael@0 | 1184 | eqPos(cm.getCursor(), Pos(0, 0)); |
michael@0 | 1185 | cm.execCommand("goGroupRight"); |
michael@0 | 1186 | eqPos(cm.getCursor(), Pos(0, 4)); |
michael@0 | 1187 | cm.execCommand("goGroupRight"); |
michael@0 | 1188 | eqPos(cm.getCursor(), Pos(0, 7)); |
michael@0 | 1189 | cm.execCommand("goGroupRight"); |
michael@0 | 1190 | eqPos(cm.getCursor(), Pos(0, 10)); |
michael@0 | 1191 | cm.execCommand("goGroupLeft"); |
michael@0 | 1192 | eqPos(cm.getCursor(), Pos(0, 7)); |
michael@0 | 1193 | cm.execCommand("goGroupRight"); cm.execCommand("goGroupRight"); cm.execCommand("goGroupRight"); |
michael@0 | 1194 | eqPos(cm.getCursor(), Pos(0, 15)); |
michael@0 | 1195 | cm.setCursor(Pos(0, 17)); |
michael@0 | 1196 | cm.execCommand("goGroupLeft"); |
michael@0 | 1197 | eqPos(cm.getCursor(), Pos(0, 16)); |
michael@0 | 1198 | cm.execCommand("goGroupLeft"); |
michael@0 | 1199 | eqPos(cm.getCursor(), Pos(0, 14)); |
michael@0 | 1200 | cm.execCommand("goGroupRight"); cm.execCommand("goGroupRight"); |
michael@0 | 1201 | eqPos(cm.getCursor(), Pos(0, 20)); |
michael@0 | 1202 | cm.execCommand("goGroupRight"); |
michael@0 | 1203 | eqPos(cm.getCursor(), Pos(1, 0)); |
michael@0 | 1204 | cm.execCommand("goGroupRight"); |
michael@0 | 1205 | eqPos(cm.getCursor(), Pos(1, 2)); |
michael@0 | 1206 | cm.execCommand("goGroupRight"); |
michael@0 | 1207 | eqPos(cm.getCursor(), Pos(1, 5)); |
michael@0 | 1208 | cm.execCommand("goGroupLeft"); cm.execCommand("goGroupLeft"); |
michael@0 | 1209 | eqPos(cm.getCursor(), Pos(1, 0)); |
michael@0 | 1210 | cm.execCommand("goGroupLeft"); |
michael@0 | 1211 | eqPos(cm.getCursor(), Pos(0, 20)); |
michael@0 | 1212 | cm.execCommand("goGroupLeft"); |
michael@0 | 1213 | eqPos(cm.getCursor(), Pos(0, 16)); |
michael@0 | 1214 | }, {value: "booo ba---quux. ffff\n abc d"}); |
michael@0 | 1215 | |
michael@0 | 1216 | testCM("groupsAndWhitespace", function(cm) { |
michael@0 | 1217 | var positions = [Pos(0, 0), Pos(0, 2), Pos(0, 5), Pos(0, 9), Pos(0, 11), |
michael@0 | 1218 | Pos(1, 0), Pos(1, 2), Pos(1, 5)]; |
michael@0 | 1219 | for (var i = 1; i < positions.length; i++) { |
michael@0 | 1220 | cm.execCommand("goGroupRight"); |
michael@0 | 1221 | eqPos(cm.getCursor(), positions[i]); |
michael@0 | 1222 | } |
michael@0 | 1223 | for (var i = positions.length - 2; i >= 0; i--) { |
michael@0 | 1224 | cm.execCommand("goGroupLeft"); |
michael@0 | 1225 | eqPos(cm.getCursor(), i == 2 ? Pos(0, 6) : positions[i]); |
michael@0 | 1226 | } |
michael@0 | 1227 | }, {value: " foo +++ \n bar"}); |
michael@0 | 1228 | |
michael@0 | 1229 | testCM("charMovementCommands", function(cm) { |
michael@0 | 1230 | cm.execCommand("goCharLeft"); cm.execCommand("goColumnLeft"); |
michael@0 | 1231 | eqPos(cm.getCursor(), Pos(0, 0)); |
michael@0 | 1232 | cm.execCommand("goCharRight"); cm.execCommand("goCharRight"); |
michael@0 | 1233 | eqPos(cm.getCursor(), Pos(0, 2)); |
michael@0 | 1234 | cm.setCursor(Pos(1, 0)); |
michael@0 | 1235 | cm.execCommand("goColumnLeft"); |
michael@0 | 1236 | eqPos(cm.getCursor(), Pos(1, 0)); |
michael@0 | 1237 | cm.execCommand("goCharLeft"); |
michael@0 | 1238 | eqPos(cm.getCursor(), Pos(0, 5)); |
michael@0 | 1239 | cm.execCommand("goColumnRight"); |
michael@0 | 1240 | eqPos(cm.getCursor(), Pos(0, 5)); |
michael@0 | 1241 | cm.execCommand("goCharRight"); |
michael@0 | 1242 | eqPos(cm.getCursor(), Pos(1, 0)); |
michael@0 | 1243 | cm.execCommand("goLineEnd"); |
michael@0 | 1244 | eqPos(cm.getCursor(), Pos(1, 5)); |
michael@0 | 1245 | cm.execCommand("goLineStartSmart"); |
michael@0 | 1246 | eqPos(cm.getCursor(), Pos(1, 1)); |
michael@0 | 1247 | cm.execCommand("goLineStartSmart"); |
michael@0 | 1248 | eqPos(cm.getCursor(), Pos(1, 0)); |
michael@0 | 1249 | cm.setCursor(Pos(2, 0)); |
michael@0 | 1250 | cm.execCommand("goCharRight"); cm.execCommand("goColumnRight"); |
michael@0 | 1251 | eqPos(cm.getCursor(), Pos(2, 0)); |
michael@0 | 1252 | }, {value: "line1\n ine2\n"}); |
michael@0 | 1253 | |
michael@0 | 1254 | testCM("verticalMovementCommands", function(cm) { |
michael@0 | 1255 | cm.execCommand("goLineUp"); |
michael@0 | 1256 | eqPos(cm.getCursor(), Pos(0, 0)); |
michael@0 | 1257 | cm.execCommand("goLineDown"); |
michael@0 | 1258 | if (!phantom) // This fails in PhantomJS, though not in a real Webkit |
michael@0 | 1259 | eqPos(cm.getCursor(), Pos(1, 0)); |
michael@0 | 1260 | cm.setCursor(Pos(1, 12)); |
michael@0 | 1261 | cm.execCommand("goLineDown"); |
michael@0 | 1262 | eqPos(cm.getCursor(), Pos(2, 5)); |
michael@0 | 1263 | cm.execCommand("goLineDown"); |
michael@0 | 1264 | eqPos(cm.getCursor(), Pos(3, 0)); |
michael@0 | 1265 | cm.execCommand("goLineUp"); |
michael@0 | 1266 | eqPos(cm.getCursor(), Pos(2, 5)); |
michael@0 | 1267 | cm.execCommand("goLineUp"); |
michael@0 | 1268 | eqPos(cm.getCursor(), Pos(1, 12)); |
michael@0 | 1269 | cm.execCommand("goPageDown"); |
michael@0 | 1270 | eqPos(cm.getCursor(), Pos(5, 0)); |
michael@0 | 1271 | cm.execCommand("goPageDown"); cm.execCommand("goLineDown"); |
michael@0 | 1272 | eqPos(cm.getCursor(), Pos(5, 0)); |
michael@0 | 1273 | cm.execCommand("goPageUp"); |
michael@0 | 1274 | eqPos(cm.getCursor(), Pos(0, 0)); |
michael@0 | 1275 | }, {value: "line1\nlong long line2\nline3\n\nline5\n"}); |
michael@0 | 1276 | |
michael@0 | 1277 | testCM("verticalMovementCommandsWrapping", function(cm) { |
michael@0 | 1278 | cm.setSize(120); |
michael@0 | 1279 | cm.setCursor(Pos(0, 5)); |
michael@0 | 1280 | cm.execCommand("goLineDown"); |
michael@0 | 1281 | eq(cm.getCursor().line, 0); |
michael@0 | 1282 | is(cm.getCursor().ch > 5, "moved beyond wrap"); |
michael@0 | 1283 | for (var i = 0; ; ++i) { |
michael@0 | 1284 | is(i < 20, "no endless loop"); |
michael@0 | 1285 | cm.execCommand("goLineDown"); |
michael@0 | 1286 | var cur = cm.getCursor(); |
michael@0 | 1287 | if (cur.line == 1) eq(cur.ch, 5); |
michael@0 | 1288 | if (cur.line == 2) { eq(cur.ch, 1); break; } |
michael@0 | 1289 | } |
michael@0 | 1290 | }, {value: "a very long line that wraps around somehow so that we can test cursor movement\nshortone\nk", |
michael@0 | 1291 | lineWrapping: true}); |
michael@0 | 1292 | |
michael@0 | 1293 | testCM("rtlMovement", function(cm) { |
michael@0 | 1294 | forEach(["خحج", "خحabcخحج", "abخحخحجcd", "abخde", "abخح2342خ1حج", "خ1ح2خح3حxج", |
michael@0 | 1295 | "خحcd", "1خحcd", "abcdeح1ج", "خمرحبها مها!", "foobarر", "خ ة ق", |
michael@0 | 1296 | "<img src=\"/בדיקה3.jpg\">"], function(line) { |
michael@0 | 1297 | var inv = line.charAt(0) == "خ"; |
michael@0 | 1298 | cm.setValue(line + "\n"); cm.execCommand(inv ? "goLineEnd" : "goLineStart"); |
michael@0 | 1299 | var cursors = byClassName(cm.getWrapperElement(), "CodeMirror-cursors")[0]; |
michael@0 | 1300 | var cursor = cursors.firstChild; |
michael@0 | 1301 | var prevX = cursor.offsetLeft, prevY = cursor.offsetTop; |
michael@0 | 1302 | for (var i = 0; i <= line.length; ++i) { |
michael@0 | 1303 | cm.execCommand("goCharRight"); |
michael@0 | 1304 | cursor = cursors.firstChild; |
michael@0 | 1305 | if (i == line.length) is(cursor.offsetTop > prevY, "next line"); |
michael@0 | 1306 | else is(cursor.offsetLeft > prevX, "moved right"); |
michael@0 | 1307 | prevX = cursor.offsetLeft; prevY = cursor.offsetTop; |
michael@0 | 1308 | } |
michael@0 | 1309 | cm.setCursor(0, 0); cm.execCommand(inv ? "goLineStart" : "goLineEnd"); |
michael@0 | 1310 | prevX = cursors.firstChild.offsetLeft; |
michael@0 | 1311 | for (var i = 0; i < line.length; ++i) { |
michael@0 | 1312 | cm.execCommand("goCharLeft"); |
michael@0 | 1313 | cursor = cursors.firstChild; |
michael@0 | 1314 | is(cursor.offsetLeft < prevX, "moved left"); |
michael@0 | 1315 | prevX = cursor.offsetLeft; |
michael@0 | 1316 | } |
michael@0 | 1317 | }); |
michael@0 | 1318 | }, null, ie_lt9); |
michael@0 | 1319 | |
michael@0 | 1320 | // Verify that updating a line clears its bidi ordering |
michael@0 | 1321 | testCM("bidiUpdate", function(cm) { |
michael@0 | 1322 | cm.setCursor(Pos(0, 2)); |
michael@0 | 1323 | cm.replaceSelection("خحج", "start"); |
michael@0 | 1324 | cm.execCommand("goCharRight"); |
michael@0 | 1325 | eqPos(cm.getCursor(), Pos(0, 4)); |
michael@0 | 1326 | }, {value: "abcd\n"}); |
michael@0 | 1327 | |
michael@0 | 1328 | testCM("movebyTextUnit", function(cm) { |
michael@0 | 1329 | cm.setValue("בְּרֵאשִ\nééé́\n"); |
michael@0 | 1330 | cm.execCommand("goLineEnd"); |
michael@0 | 1331 | for (var i = 0; i < 4; ++i) cm.execCommand("goCharRight"); |
michael@0 | 1332 | eqPos(cm.getCursor(), Pos(0, 0)); |
michael@0 | 1333 | cm.execCommand("goCharRight"); |
michael@0 | 1334 | eqPos(cm.getCursor(), Pos(1, 0)); |
michael@0 | 1335 | cm.execCommand("goCharRight"); |
michael@0 | 1336 | cm.execCommand("goCharRight"); |
michael@0 | 1337 | eqPos(cm.getCursor(), Pos(1, 4)); |
michael@0 | 1338 | cm.execCommand("goCharRight"); |
michael@0 | 1339 | eqPos(cm.getCursor(), Pos(1, 7)); |
michael@0 | 1340 | }); |
michael@0 | 1341 | |
michael@0 | 1342 | testCM("lineChangeEvents", function(cm) { |
michael@0 | 1343 | addDoc(cm, 3, 5); |
michael@0 | 1344 | var log = [], want = ["ch 0", "ch 1", "del 2", "ch 0", "ch 0", "del 1", "del 3", "del 4"]; |
michael@0 | 1345 | for (var i = 0; i < 5; ++i) { |
michael@0 | 1346 | CodeMirror.on(cm.getLineHandle(i), "delete", function(i) { |
michael@0 | 1347 | return function() {log.push("del " + i);}; |
michael@0 | 1348 | }(i)); |
michael@0 | 1349 | CodeMirror.on(cm.getLineHandle(i), "change", function(i) { |
michael@0 | 1350 | return function() {log.push("ch " + i);}; |
michael@0 | 1351 | }(i)); |
michael@0 | 1352 | } |
michael@0 | 1353 | cm.replaceRange("x", Pos(0, 1)); |
michael@0 | 1354 | cm.replaceRange("xy", Pos(1, 1), Pos(2)); |
michael@0 | 1355 | cm.replaceRange("foo\nbar", Pos(0, 1)); |
michael@0 | 1356 | cm.replaceRange("", Pos(0, 0), Pos(cm.lineCount())); |
michael@0 | 1357 | eq(log.length, want.length, "same length"); |
michael@0 | 1358 | for (var i = 0; i < log.length; ++i) |
michael@0 | 1359 | eq(log[i], want[i]); |
michael@0 | 1360 | }); |
michael@0 | 1361 | |
michael@0 | 1362 | testCM("scrollEntirelyToRight", function(cm) { |
michael@0 | 1363 | if (phantom) return; |
michael@0 | 1364 | addDoc(cm, 500, 2); |
michael@0 | 1365 | cm.setCursor(Pos(0, 500)); |
michael@0 | 1366 | var wrap = cm.getWrapperElement(), cur = byClassName(wrap, "CodeMirror-cursor")[0]; |
michael@0 | 1367 | is(wrap.getBoundingClientRect().right > cur.getBoundingClientRect().left); |
michael@0 | 1368 | }); |
michael@0 | 1369 | |
michael@0 | 1370 | testCM("lineWidgets", function(cm) { |
michael@0 | 1371 | addDoc(cm, 500, 3); |
michael@0 | 1372 | var last = cm.charCoords(Pos(2, 0)); |
michael@0 | 1373 | var node = document.createElement("div"); |
michael@0 | 1374 | node.innerHTML = "hi"; |
michael@0 | 1375 | var widget = cm.addLineWidget(1, node); |
michael@0 | 1376 | is(last.top < cm.charCoords(Pos(2, 0)).top, "took up space"); |
michael@0 | 1377 | cm.setCursor(Pos(1, 1)); |
michael@0 | 1378 | cm.execCommand("goLineDown"); |
michael@0 | 1379 | eqPos(cm.getCursor(), Pos(2, 1)); |
michael@0 | 1380 | cm.execCommand("goLineUp"); |
michael@0 | 1381 | eqPos(cm.getCursor(), Pos(1, 1)); |
michael@0 | 1382 | }); |
michael@0 | 1383 | |
michael@0 | 1384 | testCM("lineWidgetFocus", function(cm) { |
michael@0 | 1385 | var place = document.getElementById("testground"); |
michael@0 | 1386 | place.className = "offscreen"; |
michael@0 | 1387 | try { |
michael@0 | 1388 | addDoc(cm, 500, 10); |
michael@0 | 1389 | var node = document.createElement("input"); |
michael@0 | 1390 | var widget = cm.addLineWidget(1, node); |
michael@0 | 1391 | node.focus(); |
michael@0 | 1392 | eq(document.activeElement, node); |
michael@0 | 1393 | cm.replaceRange("new stuff", Pos(1, 0)); |
michael@0 | 1394 | eq(document.activeElement, node); |
michael@0 | 1395 | } finally { |
michael@0 | 1396 | place.className = ""; |
michael@0 | 1397 | } |
michael@0 | 1398 | }); |
michael@0 | 1399 | |
michael@0 | 1400 | testCM("lineWidgetCautiousRedraw", function(cm) { |
michael@0 | 1401 | var node = document.createElement("div"); |
michael@0 | 1402 | node.innerHTML = "hahah"; |
michael@0 | 1403 | var w = cm.addLineWidget(0, node); |
michael@0 | 1404 | var redrawn = false; |
michael@0 | 1405 | w.on("redraw", function() { redrawn = true; }); |
michael@0 | 1406 | cm.replaceSelection("0"); |
michael@0 | 1407 | is(!redrawn); |
michael@0 | 1408 | }, {value: "123\n456"}); |
michael@0 | 1409 | |
michael@0 | 1410 | testCM("lineWidgetChanged", function(cm) { |
michael@0 | 1411 | addDoc(cm, 2, 300); |
michael@0 | 1412 | cm.setSize(null, cm.defaultTextHeight() * 50); |
michael@0 | 1413 | cm.scrollTo(null, cm.heightAtLine(125, "local")); |
michael@0 | 1414 | function w() { |
michael@0 | 1415 | var node = document.createElement("div"); |
michael@0 | 1416 | node.style.cssText = "background: yellow; height: 50px;"; |
michael@0 | 1417 | return node; |
michael@0 | 1418 | } |
michael@0 | 1419 | var info0 = cm.getScrollInfo(); |
michael@0 | 1420 | var w0 = cm.addLineWidget(0, w()); |
michael@0 | 1421 | var w150 = cm.addLineWidget(150, w()); |
michael@0 | 1422 | var w300 = cm.addLineWidget(300, w()); |
michael@0 | 1423 | var info1 = cm.getScrollInfo(); |
michael@0 | 1424 | eq(info0.height + 150, info1.height); |
michael@0 | 1425 | eq(info0.top + 50, info1.top); |
michael@0 | 1426 | w0.node.style.height = w150.node.style.height = w300.node.style.height = "10px"; |
michael@0 | 1427 | w0.changed(); w150.changed(); w300.changed(); |
michael@0 | 1428 | var info2 = cm.getScrollInfo(); |
michael@0 | 1429 | eq(info0.height + 30, info2.height); |
michael@0 | 1430 | eq(info0.top + 10, info2.top); |
michael@0 | 1431 | }); |
michael@0 | 1432 | |
michael@0 | 1433 | testCM("getLineNumber", function(cm) { |
michael@0 | 1434 | addDoc(cm, 2, 20); |
michael@0 | 1435 | var h1 = cm.getLineHandle(1); |
michael@0 | 1436 | eq(cm.getLineNumber(h1), 1); |
michael@0 | 1437 | cm.replaceRange("hi\nbye\n", Pos(0, 0)); |
michael@0 | 1438 | eq(cm.getLineNumber(h1), 3); |
michael@0 | 1439 | cm.setValue(""); |
michael@0 | 1440 | eq(cm.getLineNumber(h1), null); |
michael@0 | 1441 | }); |
michael@0 | 1442 | |
michael@0 | 1443 | testCM("jumpTheGap", function(cm) { |
michael@0 | 1444 | if (phantom) return; |
michael@0 | 1445 | var longLine = "abcdef ghiklmnop qrstuvw xyz "; |
michael@0 | 1446 | longLine += longLine; longLine += longLine; longLine += longLine; |
michael@0 | 1447 | cm.replaceRange(longLine, Pos(2, 0), Pos(2)); |
michael@0 | 1448 | cm.setSize("200px", null); |
michael@0 | 1449 | cm.getWrapperElement().style.lineHeight = 2; |
michael@0 | 1450 | cm.refresh(); |
michael@0 | 1451 | cm.setCursor(Pos(0, 1)); |
michael@0 | 1452 | cm.execCommand("goLineDown"); |
michael@0 | 1453 | eqPos(cm.getCursor(), Pos(1, 1)); |
michael@0 | 1454 | cm.execCommand("goLineDown"); |
michael@0 | 1455 | eqPos(cm.getCursor(), Pos(2, 1)); |
michael@0 | 1456 | cm.execCommand("goLineDown"); |
michael@0 | 1457 | eq(cm.getCursor().line, 2); |
michael@0 | 1458 | is(cm.getCursor().ch > 1); |
michael@0 | 1459 | cm.execCommand("goLineUp"); |
michael@0 | 1460 | eqPos(cm.getCursor(), Pos(2, 1)); |
michael@0 | 1461 | cm.execCommand("goLineUp"); |
michael@0 | 1462 | eqPos(cm.getCursor(), Pos(1, 1)); |
michael@0 | 1463 | var node = document.createElement("div"); |
michael@0 | 1464 | node.innerHTML = "hi"; node.style.height = "30px"; |
michael@0 | 1465 | cm.addLineWidget(0, node); |
michael@0 | 1466 | cm.addLineWidget(1, node.cloneNode(true), {above: true}); |
michael@0 | 1467 | cm.setCursor(Pos(0, 2)); |
michael@0 | 1468 | cm.execCommand("goLineDown"); |
michael@0 | 1469 | eqPos(cm.getCursor(), Pos(1, 2)); |
michael@0 | 1470 | cm.execCommand("goLineUp"); |
michael@0 | 1471 | eqPos(cm.getCursor(), Pos(0, 2)); |
michael@0 | 1472 | }, {lineWrapping: true, value: "abc\ndef\nghi\njkl\n"}); |
michael@0 | 1473 | |
michael@0 | 1474 | testCM("addLineClass", function(cm) { |
michael@0 | 1475 | function cls(line, text, bg, wrap) { |
michael@0 | 1476 | var i = cm.lineInfo(line); |
michael@0 | 1477 | eq(i.textClass, text); |
michael@0 | 1478 | eq(i.bgClass, bg); |
michael@0 | 1479 | eq(i.wrapClass, wrap); |
michael@0 | 1480 | } |
michael@0 | 1481 | cm.addLineClass(0, "text", "foo"); |
michael@0 | 1482 | cm.addLineClass(0, "text", "bar"); |
michael@0 | 1483 | cm.addLineClass(1, "background", "baz"); |
michael@0 | 1484 | cm.addLineClass(1, "wrap", "foo"); |
michael@0 | 1485 | cls(0, "foo bar", null, null); |
michael@0 | 1486 | cls(1, null, "baz", "foo"); |
michael@0 | 1487 | var lines = cm.display.lineDiv; |
michael@0 | 1488 | eq(byClassName(lines, "foo").length, 2); |
michael@0 | 1489 | eq(byClassName(lines, "bar").length, 1); |
michael@0 | 1490 | eq(byClassName(lines, "baz").length, 1); |
michael@0 | 1491 | cm.removeLineClass(0, "text", "foo"); |
michael@0 | 1492 | cls(0, "bar", null, null); |
michael@0 | 1493 | cm.removeLineClass(0, "text", "foo"); |
michael@0 | 1494 | cls(0, "bar", null, null); |
michael@0 | 1495 | cm.removeLineClass(0, "text", "bar"); |
michael@0 | 1496 | cls(0, null, null, null); |
michael@0 | 1497 | cm.addLineClass(1, "wrap", "quux"); |
michael@0 | 1498 | cls(1, null, "baz", "foo quux"); |
michael@0 | 1499 | cm.removeLineClass(1, "wrap"); |
michael@0 | 1500 | cls(1, null, "baz", null); |
michael@0 | 1501 | }, {value: "hohoho\n"}); |
michael@0 | 1502 | |
michael@0 | 1503 | testCM("atomicMarker", function(cm) { |
michael@0 | 1504 | addDoc(cm, 10, 10); |
michael@0 | 1505 | function atom(ll, cl, lr, cr, li, ri) { |
michael@0 | 1506 | return cm.markText(Pos(ll, cl), Pos(lr, cr), |
michael@0 | 1507 | {atomic: true, inclusiveLeft: li, inclusiveRight: ri}); |
michael@0 | 1508 | } |
michael@0 | 1509 | var m = atom(0, 1, 0, 5); |
michael@0 | 1510 | cm.setCursor(Pos(0, 1)); |
michael@0 | 1511 | cm.execCommand("goCharRight"); |
michael@0 | 1512 | eqPos(cm.getCursor(), Pos(0, 5)); |
michael@0 | 1513 | cm.execCommand("goCharLeft"); |
michael@0 | 1514 | eqPos(cm.getCursor(), Pos(0, 1)); |
michael@0 | 1515 | m.clear(); |
michael@0 | 1516 | m = atom(0, 0, 0, 5, true); |
michael@0 | 1517 | eqPos(cm.getCursor(), Pos(0, 5), "pushed out"); |
michael@0 | 1518 | cm.execCommand("goCharLeft"); |
michael@0 | 1519 | eqPos(cm.getCursor(), Pos(0, 5)); |
michael@0 | 1520 | m.clear(); |
michael@0 | 1521 | m = atom(8, 4, 9, 10, false, true); |
michael@0 | 1522 | cm.setCursor(Pos(9, 8)); |
michael@0 | 1523 | eqPos(cm.getCursor(), Pos(8, 4), "set"); |
michael@0 | 1524 | cm.execCommand("goCharRight"); |
michael@0 | 1525 | eqPos(cm.getCursor(), Pos(8, 4), "char right"); |
michael@0 | 1526 | cm.execCommand("goLineDown"); |
michael@0 | 1527 | eqPos(cm.getCursor(), Pos(8, 4), "line down"); |
michael@0 | 1528 | cm.execCommand("goCharLeft"); |
michael@0 | 1529 | eqPos(cm.getCursor(), Pos(8, 3)); |
michael@0 | 1530 | m.clear(); |
michael@0 | 1531 | m = atom(1, 1, 3, 8); |
michael@0 | 1532 | cm.setCursor(Pos(0, 0)); |
michael@0 | 1533 | cm.setCursor(Pos(2, 0)); |
michael@0 | 1534 | eqPos(cm.getCursor(), Pos(3, 8)); |
michael@0 | 1535 | cm.execCommand("goCharLeft"); |
michael@0 | 1536 | eqPos(cm.getCursor(), Pos(1, 1)); |
michael@0 | 1537 | cm.execCommand("goCharRight"); |
michael@0 | 1538 | eqPos(cm.getCursor(), Pos(3, 8)); |
michael@0 | 1539 | cm.execCommand("goLineUp"); |
michael@0 | 1540 | eqPos(cm.getCursor(), Pos(1, 1)); |
michael@0 | 1541 | cm.execCommand("goLineDown"); |
michael@0 | 1542 | eqPos(cm.getCursor(), Pos(3, 8)); |
michael@0 | 1543 | cm.execCommand("delCharBefore"); |
michael@0 | 1544 | eq(cm.getValue().length, 80, "del chunk"); |
michael@0 | 1545 | m = atom(3, 0, 5, 5); |
michael@0 | 1546 | cm.setCursor(Pos(3, 0)); |
michael@0 | 1547 | cm.execCommand("delWordAfter"); |
michael@0 | 1548 | eq(cm.getValue().length, 53, "del chunk"); |
michael@0 | 1549 | }); |
michael@0 | 1550 | |
michael@0 | 1551 | testCM("readOnlyMarker", function(cm) { |
michael@0 | 1552 | function mark(ll, cl, lr, cr, at) { |
michael@0 | 1553 | return cm.markText(Pos(ll, cl), Pos(lr, cr), |
michael@0 | 1554 | {readOnly: true, atomic: at}); |
michael@0 | 1555 | } |
michael@0 | 1556 | var m = mark(0, 1, 0, 4); |
michael@0 | 1557 | cm.setCursor(Pos(0, 2)); |
michael@0 | 1558 | cm.replaceSelection("hi", "end"); |
michael@0 | 1559 | eqPos(cm.getCursor(), Pos(0, 2)); |
michael@0 | 1560 | eq(cm.getLine(0), "abcde"); |
michael@0 | 1561 | cm.execCommand("selectAll"); |
michael@0 | 1562 | cm.replaceSelection("oops", "around"); |
michael@0 | 1563 | eq(cm.getValue(), "oopsbcd"); |
michael@0 | 1564 | cm.undo(); |
michael@0 | 1565 | eqPos(m.find().from, Pos(0, 1)); |
michael@0 | 1566 | eqPos(m.find().to, Pos(0, 4)); |
michael@0 | 1567 | m.clear(); |
michael@0 | 1568 | cm.setCursor(Pos(0, 2)); |
michael@0 | 1569 | cm.replaceSelection("hi", "around"); |
michael@0 | 1570 | eq(cm.getLine(0), "abhicde"); |
michael@0 | 1571 | eqPos(cm.getCursor(), Pos(0, 4)); |
michael@0 | 1572 | m = mark(0, 2, 2, 2, true); |
michael@0 | 1573 | cm.setSelection(Pos(1, 1), Pos(2, 4)); |
michael@0 | 1574 | cm.replaceSelection("t", "end"); |
michael@0 | 1575 | eqPos(cm.getCursor(), Pos(2, 3)); |
michael@0 | 1576 | eq(cm.getLine(2), "klto"); |
michael@0 | 1577 | cm.execCommand("goCharLeft"); |
michael@0 | 1578 | cm.execCommand("goCharLeft"); |
michael@0 | 1579 | eqPos(cm.getCursor(), Pos(0, 2)); |
michael@0 | 1580 | cm.setSelection(Pos(0, 1), Pos(0, 3)); |
michael@0 | 1581 | cm.replaceSelection("xx", "around"); |
michael@0 | 1582 | eqPos(cm.getCursor(), Pos(0, 3)); |
michael@0 | 1583 | eq(cm.getLine(0), "axxhicde"); |
michael@0 | 1584 | }, {value: "abcde\nfghij\nklmno\n"}); |
michael@0 | 1585 | |
michael@0 | 1586 | testCM("dirtyBit", function(cm) { |
michael@0 | 1587 | eq(cm.isClean(), true); |
michael@0 | 1588 | cm.replaceSelection("boo", null, "test"); |
michael@0 | 1589 | eq(cm.isClean(), false); |
michael@0 | 1590 | cm.undo(); |
michael@0 | 1591 | eq(cm.isClean(), true); |
michael@0 | 1592 | cm.replaceSelection("boo", null, "test"); |
michael@0 | 1593 | cm.replaceSelection("baz", null, "test"); |
michael@0 | 1594 | cm.undo(); |
michael@0 | 1595 | eq(cm.isClean(), false); |
michael@0 | 1596 | cm.markClean(); |
michael@0 | 1597 | eq(cm.isClean(), true); |
michael@0 | 1598 | cm.undo(); |
michael@0 | 1599 | eq(cm.isClean(), false); |
michael@0 | 1600 | cm.redo(); |
michael@0 | 1601 | eq(cm.isClean(), true); |
michael@0 | 1602 | }); |
michael@0 | 1603 | |
michael@0 | 1604 | testCM("changeGeneration", function(cm) { |
michael@0 | 1605 | cm.replaceSelection("x"); |
michael@0 | 1606 | var softGen = cm.changeGeneration(); |
michael@0 | 1607 | cm.replaceSelection("x"); |
michael@0 | 1608 | cm.undo(); |
michael@0 | 1609 | eq(cm.getValue(), ""); |
michael@0 | 1610 | is(!cm.isClean(softGen)); |
michael@0 | 1611 | cm.replaceSelection("x"); |
michael@0 | 1612 | var hardGen = cm.changeGeneration(true); |
michael@0 | 1613 | cm.replaceSelection("x"); |
michael@0 | 1614 | cm.undo(); |
michael@0 | 1615 | eq(cm.getValue(), "x"); |
michael@0 | 1616 | is(cm.isClean(hardGen)); |
michael@0 | 1617 | }); |
michael@0 | 1618 | |
michael@0 | 1619 | testCM("addKeyMap", function(cm) { |
michael@0 | 1620 | function sendKey(code) { |
michael@0 | 1621 | cm.triggerOnKeyDown({type: "keydown", keyCode: code, |
michael@0 | 1622 | preventDefault: function(){}, stopPropagation: function(){}}); |
michael@0 | 1623 | } |
michael@0 | 1624 | |
michael@0 | 1625 | sendKey(39); |
michael@0 | 1626 | eqPos(cm.getCursor(), Pos(0, 1)); |
michael@0 | 1627 | var test = 0; |
michael@0 | 1628 | var map1 = {Right: function() { ++test; }}, map2 = {Right: function() { test += 10; }} |
michael@0 | 1629 | cm.addKeyMap(map1); |
michael@0 | 1630 | sendKey(39); |
michael@0 | 1631 | eqPos(cm.getCursor(), Pos(0, 1)); |
michael@0 | 1632 | eq(test, 1); |
michael@0 | 1633 | cm.addKeyMap(map2, true); |
michael@0 | 1634 | sendKey(39); |
michael@0 | 1635 | eq(test, 2); |
michael@0 | 1636 | cm.removeKeyMap(map1); |
michael@0 | 1637 | sendKey(39); |
michael@0 | 1638 | eq(test, 12); |
michael@0 | 1639 | cm.removeKeyMap(map2); |
michael@0 | 1640 | sendKey(39); |
michael@0 | 1641 | eq(test, 12); |
michael@0 | 1642 | eqPos(cm.getCursor(), Pos(0, 2)); |
michael@0 | 1643 | cm.addKeyMap({Right: function() { test = 55; }, name: "mymap"}); |
michael@0 | 1644 | sendKey(39); |
michael@0 | 1645 | eq(test, 55); |
michael@0 | 1646 | cm.removeKeyMap("mymap"); |
michael@0 | 1647 | sendKey(39); |
michael@0 | 1648 | eqPos(cm.getCursor(), Pos(0, 3)); |
michael@0 | 1649 | }, {value: "abc"}); |
michael@0 | 1650 | |
michael@0 | 1651 | testCM("findPosH", function(cm) { |
michael@0 | 1652 | forEach([{from: Pos(0, 0), to: Pos(0, 1), by: 1}, |
michael@0 | 1653 | {from: Pos(0, 0), to: Pos(0, 0), by: -1, hitSide: true}, |
michael@0 | 1654 | {from: Pos(0, 0), to: Pos(0, 4), by: 1, unit: "word"}, |
michael@0 | 1655 | {from: Pos(0, 0), to: Pos(0, 8), by: 2, unit: "word"}, |
michael@0 | 1656 | {from: Pos(0, 0), to: Pos(2, 0), by: 20, unit: "word", hitSide: true}, |
michael@0 | 1657 | {from: Pos(0, 7), to: Pos(0, 5), by: -1, unit: "word"}, |
michael@0 | 1658 | {from: Pos(0, 4), to: Pos(0, 8), by: 1, unit: "word"}, |
michael@0 | 1659 | {from: Pos(1, 0), to: Pos(1, 18), by: 3, unit: "word"}, |
michael@0 | 1660 | {from: Pos(1, 22), to: Pos(1, 5), by: -3, unit: "word"}, |
michael@0 | 1661 | {from: Pos(1, 15), to: Pos(1, 10), by: -5}, |
michael@0 | 1662 | {from: Pos(1, 15), to: Pos(1, 10), by: -5, unit: "column"}, |
michael@0 | 1663 | {from: Pos(1, 15), to: Pos(1, 0), by: -50, unit: "column", hitSide: true}, |
michael@0 | 1664 | {from: Pos(1, 15), to: Pos(1, 24), by: 50, unit: "column", hitSide: true}, |
michael@0 | 1665 | {from: Pos(1, 15), to: Pos(2, 0), by: 50, hitSide: true}], function(t) { |
michael@0 | 1666 | var r = cm.findPosH(t.from, t.by, t.unit || "char"); |
michael@0 | 1667 | eqPos(r, t.to); |
michael@0 | 1668 | eq(!!r.hitSide, !!t.hitSide); |
michael@0 | 1669 | }); |
michael@0 | 1670 | }, {value: "line one\nline two.something.other\n"}); |
michael@0 | 1671 | |
michael@0 | 1672 | testCM("beforeChange", function(cm) { |
michael@0 | 1673 | cm.on("beforeChange", function(cm, change) { |
michael@0 | 1674 | var text = []; |
michael@0 | 1675 | for (var i = 0; i < change.text.length; ++i) |
michael@0 | 1676 | text.push(change.text[i].replace(/\s/g, "_")); |
michael@0 | 1677 | change.update(null, null, text); |
michael@0 | 1678 | }); |
michael@0 | 1679 | cm.setValue("hello, i am a\nnew document\n"); |
michael@0 | 1680 | eq(cm.getValue(), "hello,_i_am_a\nnew_document\n"); |
michael@0 | 1681 | CodeMirror.on(cm.getDoc(), "beforeChange", function(doc, change) { |
michael@0 | 1682 | if (change.from.line == 0) change.cancel(); |
michael@0 | 1683 | }); |
michael@0 | 1684 | cm.setValue("oops"); // Canceled |
michael@0 | 1685 | eq(cm.getValue(), "hello,_i_am_a\nnew_document\n"); |
michael@0 | 1686 | cm.replaceRange("hey hey hey", Pos(1, 0), Pos(2, 0)); |
michael@0 | 1687 | eq(cm.getValue(), "hello,_i_am_a\nhey_hey_hey"); |
michael@0 | 1688 | }, {value: "abcdefghijk"}); |
michael@0 | 1689 | |
michael@0 | 1690 | testCM("beforeChangeUndo", function(cm) { |
michael@0 | 1691 | cm.replaceRange("hi", Pos(0, 0), Pos(0)); |
michael@0 | 1692 | cm.replaceRange("bye", Pos(0, 0), Pos(0)); |
michael@0 | 1693 | eq(cm.historySize().undo, 2); |
michael@0 | 1694 | cm.on("beforeChange", function(cm, change) { |
michael@0 | 1695 | is(!change.update); |
michael@0 | 1696 | change.cancel(); |
michael@0 | 1697 | }); |
michael@0 | 1698 | cm.undo(); |
michael@0 | 1699 | eq(cm.historySize().undo, 0); |
michael@0 | 1700 | eq(cm.getValue(), "bye\ntwo"); |
michael@0 | 1701 | }, {value: "one\ntwo"}); |
michael@0 | 1702 | |
michael@0 | 1703 | testCM("beforeSelectionChange", function(cm) { |
michael@0 | 1704 | function notAtEnd(cm, pos) { |
michael@0 | 1705 | var len = cm.getLine(pos.line).length; |
michael@0 | 1706 | if (!len || pos.ch == len) return Pos(pos.line, pos.ch - 1); |
michael@0 | 1707 | return pos; |
michael@0 | 1708 | } |
michael@0 | 1709 | cm.on("beforeSelectionChange", function(cm, obj) { |
michael@0 | 1710 | obj.update([{anchor: notAtEnd(cm, obj.ranges[0].anchor), |
michael@0 | 1711 | head: notAtEnd(cm, obj.ranges[0].head)}]); |
michael@0 | 1712 | }); |
michael@0 | 1713 | |
michael@0 | 1714 | addDoc(cm, 10, 10); |
michael@0 | 1715 | cm.execCommand("goLineEnd"); |
michael@0 | 1716 | eqPos(cm.getCursor(), Pos(0, 9)); |
michael@0 | 1717 | cm.execCommand("selectAll"); |
michael@0 | 1718 | eqPos(cm.getCursor("start"), Pos(0, 0)); |
michael@0 | 1719 | eqPos(cm.getCursor("end"), Pos(9, 9)); |
michael@0 | 1720 | }); |
michael@0 | 1721 | |
michael@0 | 1722 | testCM("change_removedText", function(cm) { |
michael@0 | 1723 | cm.setValue("abc\ndef"); |
michael@0 | 1724 | |
michael@0 | 1725 | var removedText = []; |
michael@0 | 1726 | cm.on("change", function(cm, change) { |
michael@0 | 1727 | removedText.push(change.removed); |
michael@0 | 1728 | }); |
michael@0 | 1729 | |
michael@0 | 1730 | cm.operation(function() { |
michael@0 | 1731 | cm.replaceRange("xyz", Pos(0, 0), Pos(1,1)); |
michael@0 | 1732 | cm.replaceRange("123", Pos(0,0)); |
michael@0 | 1733 | }); |
michael@0 | 1734 | |
michael@0 | 1735 | eq(removedText.length, 2); |
michael@0 | 1736 | eq(removedText[0].join("\n"), "abc\nd"); |
michael@0 | 1737 | eq(removedText[1].join("\n"), ""); |
michael@0 | 1738 | |
michael@0 | 1739 | var removedText = []; |
michael@0 | 1740 | cm.undo(); |
michael@0 | 1741 | eq(removedText.length, 2); |
michael@0 | 1742 | eq(removedText[0].join("\n"), "123"); |
michael@0 | 1743 | eq(removedText[1].join("\n"), "xyz"); |
michael@0 | 1744 | |
michael@0 | 1745 | var removedText = []; |
michael@0 | 1746 | cm.redo(); |
michael@0 | 1747 | eq(removedText.length, 2); |
michael@0 | 1748 | eq(removedText[0].join("\n"), "abc\nd"); |
michael@0 | 1749 | eq(removedText[1].join("\n"), ""); |
michael@0 | 1750 | }); |
michael@0 | 1751 | |
michael@0 | 1752 | testCM("lineStyleFromMode", function(cm) { |
michael@0 | 1753 | CodeMirror.defineMode("test_mode", function() { |
michael@0 | 1754 | return {token: function(stream) { |
michael@0 | 1755 | if (stream.match(/^\[[^\]]*\]/)) return " line-brackets "; |
michael@0 | 1756 | if (stream.match(/^\([^\)]*\)/)) return " line-background-parens "; |
michael@0 | 1757 | if (stream.match(/^<[^>]*>/)) return " span line-line line-background-bg "; |
michael@0 | 1758 | stream.match(/^\s+|^\S+/); |
michael@0 | 1759 | }}; |
michael@0 | 1760 | }); |
michael@0 | 1761 | cm.setOption("mode", "test_mode"); |
michael@0 | 1762 | var bracketElts = byClassName(cm.getWrapperElement(), "brackets"); |
michael@0 | 1763 | eq(bracketElts.length, 1, "brackets count"); |
michael@0 | 1764 | eq(bracketElts[0].nodeName, "PRE"); |
michael@0 | 1765 | is(!/brackets.*brackets/.test(bracketElts[0].className)); |
michael@0 | 1766 | var parenElts = byClassName(cm.getWrapperElement(), "parens"); |
michael@0 | 1767 | eq(parenElts.length, 1, "parens count"); |
michael@0 | 1768 | eq(parenElts[0].nodeName, "DIV"); |
michael@0 | 1769 | is(!/parens.*parens/.test(parenElts[0].className)); |
michael@0 | 1770 | eq(parenElts[0].parentElement.nodeName, "DIV"); |
michael@0 | 1771 | |
michael@0 | 1772 | eq(byClassName(cm.getWrapperElement(), "bg").length, 1); |
michael@0 | 1773 | eq(byClassName(cm.getWrapperElement(), "line").length, 1); |
michael@0 | 1774 | var spanElts = byClassName(cm.getWrapperElement(), "cm-span"); |
michael@0 | 1775 | eq(spanElts.length, 2); |
michael@0 | 1776 | is(/^\s*cm-span\s*$/.test(spanElts[0].className)); |
michael@0 | 1777 | }, {value: "line1: [br] [br]\nline2: (par) (par)\nline3: <tag> <tag>"}); |
michael@0 | 1778 | |
michael@0 | 1779 | CodeMirror.registerHelper("xxx", "a", "A"); |
michael@0 | 1780 | CodeMirror.registerHelper("xxx", "b", "B"); |
michael@0 | 1781 | CodeMirror.defineMode("yyy", function() { |
michael@0 | 1782 | return { |
michael@0 | 1783 | token: function(stream) { stream.skipToEnd(); }, |
michael@0 | 1784 | xxx: ["a", "b", "q"] |
michael@0 | 1785 | }; |
michael@0 | 1786 | }); |
michael@0 | 1787 | CodeMirror.registerGlobalHelper("xxx", "c", function(m) { return m.enableC; }, "C"); |
michael@0 | 1788 | |
michael@0 | 1789 | testCM("helpers", function(cm) { |
michael@0 | 1790 | cm.setOption("mode", "yyy"); |
michael@0 | 1791 | eq(cm.getHelpers(Pos(0, 0), "xxx").join("/"), "A/B"); |
michael@0 | 1792 | cm.setOption("mode", {name: "yyy", modeProps: {xxx: "b", enableC: true}}); |
michael@0 | 1793 | eq(cm.getHelpers(Pos(0, 0), "xxx").join("/"), "B/C"); |
michael@0 | 1794 | cm.setOption("mode", "javascript"); |
michael@0 | 1795 | eq(cm.getHelpers(Pos(0, 0), "xxx").join("/"), ""); |
michael@0 | 1796 | }); |
michael@0 | 1797 | |
michael@0 | 1798 | testCM("selectionHistory", function(cm) { |
michael@0 | 1799 | for (var i = 0; i < 3; i++) { |
michael@0 | 1800 | cm.setExtending(true); |
michael@0 | 1801 | cm.execCommand("goCharRight"); |
michael@0 | 1802 | cm.setExtending(false); |
michael@0 | 1803 | cm.execCommand("goCharRight"); |
michael@0 | 1804 | cm.execCommand("goCharRight"); |
michael@0 | 1805 | } |
michael@0 | 1806 | cm.execCommand("undoSelection"); |
michael@0 | 1807 | eq(cm.getSelection(), "c"); |
michael@0 | 1808 | cm.execCommand("undoSelection"); |
michael@0 | 1809 | eq(cm.getSelection(), ""); |
michael@0 | 1810 | eqPos(cm.getCursor(), Pos(0, 4)); |
michael@0 | 1811 | cm.execCommand("undoSelection"); |
michael@0 | 1812 | eq(cm.getSelection(), "b"); |
michael@0 | 1813 | cm.execCommand("redoSelection"); |
michael@0 | 1814 | eq(cm.getSelection(), ""); |
michael@0 | 1815 | eqPos(cm.getCursor(), Pos(0, 4)); |
michael@0 | 1816 | cm.execCommand("redoSelection"); |
michael@0 | 1817 | eq(cm.getSelection(), "c"); |
michael@0 | 1818 | cm.execCommand("redoSelection"); |
michael@0 | 1819 | eq(cm.getSelection(), ""); |
michael@0 | 1820 | eqPos(cm.getCursor(), Pos(0, 6)); |
michael@0 | 1821 | }, {value: "a b c d"}); |
michael@0 | 1822 | |
michael@0 | 1823 | testCM("selectionChangeReducesRedo", function(cm) { |
michael@0 | 1824 | cm.replaceSelection("X"); |
michael@0 | 1825 | cm.execCommand("goCharRight"); |
michael@0 | 1826 | cm.undoSelection(); |
michael@0 | 1827 | cm.execCommand("selectAll"); |
michael@0 | 1828 | cm.undoSelection(); |
michael@0 | 1829 | eq(cm.getValue(), "Xabc"); |
michael@0 | 1830 | eqPos(cm.getCursor(), Pos(0, 1)); |
michael@0 | 1831 | cm.undoSelection(); |
michael@0 | 1832 | eq(cm.getValue(), "abc"); |
michael@0 | 1833 | }, {value: "abc"}); |
michael@0 | 1834 | |
michael@0 | 1835 | testCM("selectionHistoryNonOverlapping", function(cm) { |
michael@0 | 1836 | cm.setSelection(Pos(0, 0), Pos(0, 1)); |
michael@0 | 1837 | cm.setSelection(Pos(0, 2), Pos(0, 3)); |
michael@0 | 1838 | cm.execCommand("undoSelection"); |
michael@0 | 1839 | eqPos(cm.getCursor("anchor"), Pos(0, 0)); |
michael@0 | 1840 | eqPos(cm.getCursor("head"), Pos(0, 1)); |
michael@0 | 1841 | }, {value: "1234"}); |
michael@0 | 1842 | |
michael@0 | 1843 | testCM("cursorMotionSplitsHistory", function(cm) { |
michael@0 | 1844 | cm.replaceSelection("a"); |
michael@0 | 1845 | cm.execCommand("goCharRight"); |
michael@0 | 1846 | cm.replaceSelection("b"); |
michael@0 | 1847 | cm.replaceSelection("c"); |
michael@0 | 1848 | cm.undo(); |
michael@0 | 1849 | eq(cm.getValue(), "a1234"); |
michael@0 | 1850 | eqPos(cm.getCursor(), Pos(0, 2)); |
michael@0 | 1851 | cm.undo(); |
michael@0 | 1852 | eq(cm.getValue(), "1234"); |
michael@0 | 1853 | eqPos(cm.getCursor(), Pos(0, 0)); |
michael@0 | 1854 | }, {value: "1234"}); |
michael@0 | 1855 | |
michael@0 | 1856 | testCM("selChangeInOperationDoesNotSplit", function(cm) { |
michael@0 | 1857 | for (var i = 0; i < 4; i++) { |
michael@0 | 1858 | cm.operation(function() { |
michael@0 | 1859 | cm.replaceSelection("x"); |
michael@0 | 1860 | cm.setCursor(Pos(0, cm.getCursor().ch - 1)); |
michael@0 | 1861 | }); |
michael@0 | 1862 | } |
michael@0 | 1863 | eqPos(cm.getCursor(), Pos(0, 0)); |
michael@0 | 1864 | eq(cm.getValue(), "xxxxa"); |
michael@0 | 1865 | cm.undo(); |
michael@0 | 1866 | eq(cm.getValue(), "a"); |
michael@0 | 1867 | }, {value: "a"}); |
michael@0 | 1868 | |
michael@0 | 1869 | testCM("alwaysMergeSelEventWithChangeOrigin", function(cm) { |
michael@0 | 1870 | cm.replaceSelection("U", null, "foo"); |
michael@0 | 1871 | cm.setSelection(Pos(0, 0), Pos(0, 1), {origin: "foo"}); |
michael@0 | 1872 | cm.undoSelection(); |
michael@0 | 1873 | eq(cm.getValue(), "a"); |
michael@0 | 1874 | cm.replaceSelection("V", null, "foo"); |
michael@0 | 1875 | cm.setSelection(Pos(0, 0), Pos(0, 1), {origin: "bar"}); |
michael@0 | 1876 | cm.undoSelection(); |
michael@0 | 1877 | eq(cm.getValue(), "Va"); |
michael@0 | 1878 | }, {value: "a"}); |