michael@0: var Pos = CodeMirror.Pos;
michael@0:
michael@0: CodeMirror.defaults.rtlMoveVisually = true;
michael@0:
michael@0: function forEach(arr, f) {
michael@0: for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]);
michael@0: }
michael@0:
michael@0: function addDoc(cm, width, height) {
michael@0: var content = [], line = "";
michael@0: for (var i = 0; i < width; ++i) line += "x";
michael@0: for (var i = 0; i < height; ++i) content.push(line);
michael@0: cm.setValue(content.join("\n"));
michael@0: }
michael@0:
michael@0: function byClassName(elt, cls) {
michael@0: if (elt.getElementsByClassName) return elt.getElementsByClassName(cls);
michael@0: var found = [], re = new RegExp("\\b" + cls + "\\b");
michael@0: function search(elt) {
michael@0: if (elt.nodeType == 3) return;
michael@0: if (re.test(elt.className)) found.push(elt);
michael@0: for (var i = 0, e = elt.childNodes.length; i < e; ++i)
michael@0: search(elt.childNodes[i]);
michael@0: }
michael@0: search(elt);
michael@0: return found;
michael@0: }
michael@0:
michael@0: var ie_lt8 = /MSIE [1-7]\b/.test(navigator.userAgent);
michael@0: var ie_lt9 = /MSIE [1-8]\b/.test(navigator.userAgent);
michael@0: var mac = /Mac/.test(navigator.platform);
michael@0: var phantom = /PhantomJS/.test(navigator.userAgent);
michael@0: var opera = /Opera\/\./.test(navigator.userAgent);
michael@0: var opera_version = opera && navigator.userAgent.match(/Version\/(\d+\.\d+)/);
michael@0: if (opera_version) opera_version = Number(opera_version);
michael@0: var opera_lt10 = opera && (!opera_version || opera_version < 10);
michael@0:
michael@0: namespace = "core_";
michael@0:
michael@0: test("core_fromTextArea", function() {
michael@0: var te = document.getElementById("code");
michael@0: te.value = "CONTENT";
michael@0: var cm = CodeMirror.fromTextArea(te);
michael@0: is(!te.offsetHeight);
michael@0: eq(cm.getValue(), "CONTENT");
michael@0: cm.setValue("foo\nbar");
michael@0: eq(cm.getValue(), "foo\nbar");
michael@0: cm.save();
michael@0: is(/^foo\r?\nbar$/.test(te.value));
michael@0: cm.setValue("xxx");
michael@0: cm.toTextArea();
michael@0: is(te.offsetHeight);
michael@0: eq(te.value, "xxx");
michael@0: });
michael@0:
michael@0: testCM("getRange", function(cm) {
michael@0: eq(cm.getLine(0), "1234");
michael@0: eq(cm.getLine(1), "5678");
michael@0: eq(cm.getLine(2), null);
michael@0: eq(cm.getLine(-1), null);
michael@0: eq(cm.getRange(Pos(0, 0), Pos(0, 3)), "123");
michael@0: eq(cm.getRange(Pos(0, -1), Pos(0, 200)), "1234");
michael@0: eq(cm.getRange(Pos(0, 2), Pos(1, 2)), "34\n56");
michael@0: eq(cm.getRange(Pos(1, 2), Pos(100, 0)), "78");
michael@0: }, {value: "1234\n5678"});
michael@0:
michael@0: testCM("replaceRange", function(cm) {
michael@0: eq(cm.getValue(), "");
michael@0: cm.replaceRange("foo\n", Pos(0, 0));
michael@0: eq(cm.getValue(), "foo\n");
michael@0: cm.replaceRange("a\nb", Pos(0, 1));
michael@0: eq(cm.getValue(), "fa\nboo\n");
michael@0: eq(cm.lineCount(), 3);
michael@0: cm.replaceRange("xyzzy", Pos(0, 0), Pos(1, 1));
michael@0: eq(cm.getValue(), "xyzzyoo\n");
michael@0: cm.replaceRange("abc", Pos(0, 0), Pos(10, 0));
michael@0: eq(cm.getValue(), "abc");
michael@0: eq(cm.lineCount(), 1);
michael@0: });
michael@0:
michael@0: testCM("selection", function(cm) {
michael@0: cm.setSelection(Pos(0, 4), Pos(2, 2));
michael@0: is(cm.somethingSelected());
michael@0: eq(cm.getSelection(), "11\n222222\n33");
michael@0: eqPos(cm.getCursor(false), Pos(2, 2));
michael@0: eqPos(cm.getCursor(true), Pos(0, 4));
michael@0: cm.setSelection(Pos(1, 0));
michael@0: is(!cm.somethingSelected());
michael@0: eq(cm.getSelection(), "");
michael@0: eqPos(cm.getCursor(true), Pos(1, 0));
michael@0: cm.replaceSelection("abc", "around");
michael@0: eq(cm.getSelection(), "abc");
michael@0: eq(cm.getValue(), "111111\nabc222222\n333333");
michael@0: cm.replaceSelection("def", "end");
michael@0: eq(cm.getSelection(), "");
michael@0: eqPos(cm.getCursor(true), Pos(1, 3));
michael@0: cm.setCursor(Pos(2, 1));
michael@0: eqPos(cm.getCursor(true), Pos(2, 1));
michael@0: cm.setCursor(1, 2);
michael@0: eqPos(cm.getCursor(true), Pos(1, 2));
michael@0: }, {value: "111111\n222222\n333333"});
michael@0:
michael@0: testCM("extendSelection", function(cm) {
michael@0: cm.setExtending(true);
michael@0: addDoc(cm, 10, 10);
michael@0: cm.setSelection(Pos(3, 5));
michael@0: eqPos(cm.getCursor("head"), Pos(3, 5));
michael@0: eqPos(cm.getCursor("anchor"), Pos(3, 5));
michael@0: cm.setSelection(Pos(2, 5), Pos(5, 5));
michael@0: eqPos(cm.getCursor("head"), Pos(5, 5));
michael@0: eqPos(cm.getCursor("anchor"), Pos(2, 5));
michael@0: eqPos(cm.getCursor("start"), Pos(2, 5));
michael@0: eqPos(cm.getCursor("end"), Pos(5, 5));
michael@0: cm.setSelection(Pos(5, 5), Pos(2, 5));
michael@0: eqPos(cm.getCursor("head"), Pos(2, 5));
michael@0: eqPos(cm.getCursor("anchor"), Pos(5, 5));
michael@0: eqPos(cm.getCursor("start"), Pos(2, 5));
michael@0: eqPos(cm.getCursor("end"), Pos(5, 5));
michael@0: cm.extendSelection(Pos(3, 2));
michael@0: eqPos(cm.getCursor("head"), Pos(3, 2));
michael@0: eqPos(cm.getCursor("anchor"), Pos(5, 5));
michael@0: cm.extendSelection(Pos(6, 2));
michael@0: eqPos(cm.getCursor("head"), Pos(6, 2));
michael@0: eqPos(cm.getCursor("anchor"), Pos(5, 5));
michael@0: cm.extendSelection(Pos(6, 3), Pos(6, 4));
michael@0: eqPos(cm.getCursor("head"), Pos(6, 4));
michael@0: eqPos(cm.getCursor("anchor"), Pos(5, 5));
michael@0: cm.extendSelection(Pos(0, 3), Pos(0, 4));
michael@0: eqPos(cm.getCursor("head"), Pos(0, 3));
michael@0: eqPos(cm.getCursor("anchor"), Pos(5, 5));
michael@0: cm.extendSelection(Pos(4, 5), Pos(6, 5));
michael@0: eqPos(cm.getCursor("head"), Pos(6, 5));
michael@0: eqPos(cm.getCursor("anchor"), Pos(4, 5));
michael@0: cm.setExtending(false);
michael@0: cm.extendSelection(Pos(0, 3), Pos(0, 4));
michael@0: eqPos(cm.getCursor("head"), Pos(0, 3));
michael@0: eqPos(cm.getCursor("anchor"), Pos(0, 4));
michael@0: });
michael@0:
michael@0: testCM("lines", function(cm) {
michael@0: eq(cm.getLine(0), "111111");
michael@0: eq(cm.getLine(1), "222222");
michael@0: eq(cm.getLine(-1), null);
michael@0: cm.replaceRange("", Pos(1, 0), Pos(2, 0))
michael@0: cm.replaceRange("abc", Pos(1, 0), Pos(1));
michael@0: eq(cm.getValue(), "111111\nabc");
michael@0: }, {value: "111111\n222222\n333333"});
michael@0:
michael@0: testCM("indent", function(cm) {
michael@0: cm.indentLine(1);
michael@0: eq(cm.getLine(1), " blah();");
michael@0: cm.setOption("indentUnit", 8);
michael@0: cm.indentLine(1);
michael@0: eq(cm.getLine(1), "\tblah();");
michael@0: cm.setOption("indentUnit", 10);
michael@0: cm.setOption("tabSize", 4);
michael@0: cm.indentLine(1);
michael@0: eq(cm.getLine(1), "\t\t blah();");
michael@0: }, {value: "if (x) {\nblah();\n}", indentUnit: 3, indentWithTabs: true, tabSize: 8});
michael@0:
michael@0: testCM("indentByNumber", function(cm) {
michael@0: cm.indentLine(0, 2);
michael@0: eq(cm.getLine(0), " foo");
michael@0: cm.indentLine(0, -200);
michael@0: eq(cm.getLine(0), "foo");
michael@0: cm.setSelection(Pos(0, 0), Pos(1, 2));
michael@0: cm.indentSelection(3);
michael@0: eq(cm.getValue(), " foo\n bar\nbaz");
michael@0: }, {value: "foo\nbar\nbaz"});
michael@0:
michael@0: test("core_defaults", function() {
michael@0: var defsCopy = {}, defs = CodeMirror.defaults;
michael@0: for (var opt in defs) defsCopy[opt] = defs[opt];
michael@0: defs.indentUnit = 5;
michael@0: defs.value = "uu";
michael@0: defs.indentWithTabs = true;
michael@0: defs.tabindex = 55;
michael@0: var place = document.getElementById("testground"), cm = CodeMirror(place);
michael@0: try {
michael@0: eq(cm.getOption("indentUnit"), 5);
michael@0: cm.setOption("indentUnit", 10);
michael@0: eq(defs.indentUnit, 5);
michael@0: eq(cm.getValue(), "uu");
michael@0: eq(cm.getOption("indentWithTabs"), true);
michael@0: eq(cm.getInputField().tabIndex, 55);
michael@0: }
michael@0: finally {
michael@0: for (var opt in defsCopy) defs[opt] = defsCopy[opt];
michael@0: place.removeChild(cm.getWrapperElement());
michael@0: }
michael@0: });
michael@0:
michael@0: testCM("lineInfo", function(cm) {
michael@0: eq(cm.lineInfo(-1), null);
michael@0: var mark = document.createElement("span");
michael@0: var lh = cm.setGutterMarker(1, "FOO", mark);
michael@0: var info = cm.lineInfo(1);
michael@0: eq(info.text, "222222");
michael@0: eq(info.gutterMarkers.FOO, mark);
michael@0: eq(info.line, 1);
michael@0: eq(cm.lineInfo(2).gutterMarkers, null);
michael@0: cm.setGutterMarker(lh, "FOO", null);
michael@0: eq(cm.lineInfo(1).gutterMarkers, null);
michael@0: cm.setGutterMarker(1, "FOO", mark);
michael@0: cm.setGutterMarker(0, "FOO", mark);
michael@0: cm.clearGutter("FOO");
michael@0: eq(cm.lineInfo(0).gutterMarkers, null);
michael@0: eq(cm.lineInfo(1).gutterMarkers, null);
michael@0: }, {value: "111111\n222222\n333333"});
michael@0:
michael@0: testCM("coords", function(cm) {
michael@0: cm.setSize(null, 100);
michael@0: addDoc(cm, 32, 200);
michael@0: var top = cm.charCoords(Pos(0, 0));
michael@0: var bot = cm.charCoords(Pos(200, 30));
michael@0: is(top.left < bot.left);
michael@0: is(top.top < bot.top);
michael@0: is(top.top < top.bottom);
michael@0: cm.scrollTo(null, 100);
michael@0: var top2 = cm.charCoords(Pos(0, 0));
michael@0: is(top.top > top2.top);
michael@0: eq(top.left, top2.left);
michael@0: });
michael@0:
michael@0: testCM("coordsChar", function(cm) {
michael@0: addDoc(cm, 35, 70);
michael@0: for (var i = 0; i < 2; ++i) {
michael@0: var sys = i ? "local" : "page";
michael@0: for (var ch = 0; ch <= 35; ch += 5) {
michael@0: for (var line = 0; line < 70; line += 5) {
michael@0: cm.setCursor(line, ch);
michael@0: var coords = cm.charCoords(Pos(line, ch), sys);
michael@0: var pos = cm.coordsChar({left: coords.left + 1, top: coords.top + 1}, sys);
michael@0: eqPos(pos, Pos(line, ch));
michael@0: }
michael@0: }
michael@0: }
michael@0: }, {lineNumbers: true});
michael@0:
michael@0: testCM("posFromIndex", function(cm) {
michael@0: cm.setValue(
michael@0: "This function should\n" +
michael@0: "convert a zero based index\n" +
michael@0: "to line and ch."
michael@0: );
michael@0:
michael@0: var examples = [
michael@0: { index: -1, line: 0, ch: 0 }, // <- Tests clipping
michael@0: { index: 0, line: 0, ch: 0 },
michael@0: { index: 10, line: 0, ch: 10 },
michael@0: { index: 39, line: 1, ch: 18 },
michael@0: { index: 55, line: 2, ch: 7 },
michael@0: { index: 63, line: 2, ch: 15 },
michael@0: { index: 64, line: 2, ch: 15 } // <- Tests clipping
michael@0: ];
michael@0:
michael@0: for (var i = 0; i < examples.length; i++) {
michael@0: var example = examples[i];
michael@0: var pos = cm.posFromIndex(example.index);
michael@0: eq(pos.line, example.line);
michael@0: eq(pos.ch, example.ch);
michael@0: if (example.index >= 0 && example.index < 64)
michael@0: eq(cm.indexFromPos(pos), example.index);
michael@0: }
michael@0: });
michael@0:
michael@0: testCM("undo", function(cm) {
michael@0: cm.replaceRange("def", Pos(0, 0), Pos(0));
michael@0: eq(cm.historySize().undo, 1);
michael@0: cm.undo();
michael@0: eq(cm.getValue(), "abc");
michael@0: eq(cm.historySize().undo, 0);
michael@0: eq(cm.historySize().redo, 1);
michael@0: cm.redo();
michael@0: eq(cm.getValue(), "def");
michael@0: eq(cm.historySize().undo, 1);
michael@0: eq(cm.historySize().redo, 0);
michael@0: cm.setValue("1\n\n\n2");
michael@0: cm.clearHistory();
michael@0: eq(cm.historySize().undo, 0);
michael@0: for (var i = 0; i < 20; ++i) {
michael@0: cm.replaceRange("a", Pos(0, 0));
michael@0: cm.replaceRange("b", Pos(3, 0));
michael@0: }
michael@0: eq(cm.historySize().undo, 40);
michael@0: for (var i = 0; i < 40; ++i)
michael@0: cm.undo();
michael@0: eq(cm.historySize().redo, 40);
michael@0: eq(cm.getValue(), "1\n\n\n2");
michael@0: }, {value: "abc"});
michael@0:
michael@0: testCM("undoDepth", function(cm) {
michael@0: cm.replaceRange("d", Pos(0));
michael@0: cm.replaceRange("e", Pos(0));
michael@0: cm.replaceRange("f", Pos(0));
michael@0: cm.undo(); cm.undo(); cm.undo();
michael@0: eq(cm.getValue(), "abcd");
michael@0: }, {value: "abc", undoDepth: 4});
michael@0:
michael@0: testCM("undoDoesntClearValue", function(cm) {
michael@0: cm.undo();
michael@0: eq(cm.getValue(), "x");
michael@0: }, {value: "x"});
michael@0:
michael@0: testCM("undoMultiLine", function(cm) {
michael@0: cm.operation(function() {
michael@0: cm.replaceRange("x", Pos(0, 0));
michael@0: cm.replaceRange("y", Pos(1, 0));
michael@0: });
michael@0: cm.undo();
michael@0: eq(cm.getValue(), "abc\ndef\nghi");
michael@0: cm.operation(function() {
michael@0: cm.replaceRange("y", Pos(1, 0));
michael@0: cm.replaceRange("x", Pos(0, 0));
michael@0: });
michael@0: cm.undo();
michael@0: eq(cm.getValue(), "abc\ndef\nghi");
michael@0: cm.operation(function() {
michael@0: cm.replaceRange("y", Pos(2, 0));
michael@0: cm.replaceRange("x", Pos(1, 0));
michael@0: cm.replaceRange("z", Pos(2, 0));
michael@0: });
michael@0: cm.undo();
michael@0: eq(cm.getValue(), "abc\ndef\nghi", 3);
michael@0: }, {value: "abc\ndef\nghi"});
michael@0:
michael@0: testCM("undoComposite", function(cm) {
michael@0: cm.replaceRange("y", Pos(1));
michael@0: cm.operation(function() {
michael@0: cm.replaceRange("x", Pos(0));
michael@0: cm.replaceRange("z", Pos(2));
michael@0: });
michael@0: eq(cm.getValue(), "ax\nby\ncz\n");
michael@0: cm.undo();
michael@0: eq(cm.getValue(), "a\nby\nc\n");
michael@0: cm.undo();
michael@0: eq(cm.getValue(), "a\nb\nc\n");
michael@0: cm.redo(); cm.redo();
michael@0: eq(cm.getValue(), "ax\nby\ncz\n");
michael@0: }, {value: "a\nb\nc\n"});
michael@0:
michael@0: testCM("undoSelection", function(cm) {
michael@0: cm.setSelection(Pos(0, 2), Pos(0, 4));
michael@0: cm.replaceSelection("");
michael@0: cm.setCursor(Pos(1, 0));
michael@0: cm.undo();
michael@0: eqPos(cm.getCursor(true), Pos(0, 2));
michael@0: eqPos(cm.getCursor(false), Pos(0, 4));
michael@0: cm.setCursor(Pos(1, 0));
michael@0: cm.redo();
michael@0: eqPos(cm.getCursor(true), Pos(0, 2));
michael@0: eqPos(cm.getCursor(false), Pos(0, 2));
michael@0: }, {value: "abcdefgh\n"});
michael@0:
michael@0: testCM("undoSelectionAsBefore", function(cm) {
michael@0: cm.replaceSelection("abc", "around");
michael@0: cm.undo();
michael@0: cm.redo();
michael@0: eq(cm.getSelection(), "abc");
michael@0: });
michael@0:
michael@0: testCM("markTextSingleLine", function(cm) {
michael@0: forEach([{a: 0, b: 1, c: "", f: 2, t: 5},
michael@0: {a: 0, b: 4, c: "", f: 0, t: 2},
michael@0: {a: 1, b: 2, c: "x", f: 3, t: 6},
michael@0: {a: 4, b: 5, c: "", f: 3, t: 5},
michael@0: {a: 4, b: 5, c: "xx", f: 3, t: 7},
michael@0: {a: 2, b: 5, c: "", f: 2, t: 3},
michael@0: {a: 2, b: 5, c: "abcd", f: 6, t: 7},
michael@0: {a: 2, b: 6, c: "x", f: null, t: null},
michael@0: {a: 3, b: 6, c: "", f: null, t: null},
michael@0: {a: 0, b: 9, c: "hallo", f: null, t: null},
michael@0: {a: 4, b: 6, c: "x", f: 3, t: 4},
michael@0: {a: 4, b: 8, c: "", f: 3, t: 4},
michael@0: {a: 6, b: 6, c: "a", f: 3, t: 6},
michael@0: {a: 8, b: 9, c: "", f: 3, t: 6}], function(test) {
michael@0: cm.setValue("1234567890");
michael@0: var r = cm.markText(Pos(0, 3), Pos(0, 6), {className: "foo"});
michael@0: cm.replaceRange(test.c, Pos(0, test.a), Pos(0, test.b));
michael@0: var f = r.find();
michael@0: eq(f && f.from.ch, test.f); eq(f && f.to.ch, test.t);
michael@0: });
michael@0: });
michael@0:
michael@0: testCM("markTextMultiLine", function(cm) {
michael@0: function p(v) { return v && Pos(v[0], v[1]); }
michael@0: forEach([{a: [0, 0], b: [0, 5], c: "", f: [0, 0], t: [2, 5]},
michael@0: {a: [0, 0], b: [0, 5], c: "foo\n", f: [1, 0], t: [3, 5]},
michael@0: {a: [0, 1], b: [0, 10], c: "", f: [0, 1], t: [2, 5]},
michael@0: {a: [0, 5], b: [0, 6], c: "x", f: [0, 6], t: [2, 5]},
michael@0: {a: [0, 0], b: [1, 0], c: "", f: [0, 0], t: [1, 5]},
michael@0: {a: [0, 6], b: [2, 4], c: "", f: [0, 5], t: [0, 7]},
michael@0: {a: [0, 6], b: [2, 4], c: "aa", f: [0, 5], t: [0, 9]},
michael@0: {a: [1, 2], b: [1, 8], c: "", f: [0, 5], t: [2, 5]},
michael@0: {a: [0, 5], b: [2, 5], c: "xx", f: null, t: null},
michael@0: {a: [0, 0], b: [2, 10], c: "x", f: null, t: null},
michael@0: {a: [1, 5], b: [2, 5], c: "", f: [0, 5], t: [1, 5]},
michael@0: {a: [2, 0], b: [2, 3], c: "", f: [0, 5], t: [2, 2]},
michael@0: {a: [2, 5], b: [3, 0], c: "a\nb", f: [0, 5], t: [2, 5]},
michael@0: {a: [2, 3], b: [3, 0], c: "x", f: [0, 5], t: [2, 3]},
michael@0: {a: [1, 1], b: [1, 9], c: "1\n2\n3", f: [0, 5], t: [4, 5]}], function(test) {
michael@0: cm.setValue("aaaaaaaaaa\nbbbbbbbbbb\ncccccccccc\ndddddddd\n");
michael@0: var r = cm.markText(Pos(0, 5), Pos(2, 5),
michael@0: {className: "CodeMirror-matchingbracket"});
michael@0: cm.replaceRange(test.c, p(test.a), p(test.b));
michael@0: var f = r.find();
michael@0: eqPos(f && f.from, p(test.f)); eqPos(f && f.to, p(test.t));
michael@0: });
michael@0: });
michael@0:
michael@0: testCM("markTextUndo", function(cm) {
michael@0: var marker1, marker2, bookmark;
michael@0: marker1 = cm.markText(Pos(0, 1), Pos(0, 3),
michael@0: {className: "CodeMirror-matchingbracket"});
michael@0: marker2 = cm.markText(Pos(0, 0), Pos(2, 1),
michael@0: {className: "CodeMirror-matchingbracket"});
michael@0: bookmark = cm.setBookmark(Pos(1, 5));
michael@0: cm.operation(function(){
michael@0: cm.replaceRange("foo", Pos(0, 2));
michael@0: cm.replaceRange("bar\nbaz\nbug\n", Pos(2, 0), Pos(3, 0));
michael@0: });
michael@0: var v1 = cm.getValue();
michael@0: cm.setValue("");
michael@0: eq(marker1.find(), null); eq(marker2.find(), null); eq(bookmark.find(), null);
michael@0: cm.undo();
michael@0: eqPos(bookmark.find(), Pos(1, 5), "still there");
michael@0: cm.undo();
michael@0: var m1Pos = marker1.find(), m2Pos = marker2.find();
michael@0: eqPos(m1Pos.from, Pos(0, 1)); eqPos(m1Pos.to, Pos(0, 3));
michael@0: eqPos(m2Pos.from, Pos(0, 0)); eqPos(m2Pos.to, Pos(2, 1));
michael@0: eqPos(bookmark.find(), Pos(1, 5));
michael@0: cm.redo(); cm.redo();
michael@0: eq(bookmark.find(), null);
michael@0: cm.undo();
michael@0: eqPos(bookmark.find(), Pos(1, 5));
michael@0: eq(cm.getValue(), v1);
michael@0: }, {value: "1234\n56789\n00\n"});
michael@0:
michael@0: testCM("markTextStayGone", function(cm) {
michael@0: var m1 = cm.markText(Pos(0, 0), Pos(0, 1));
michael@0: cm.replaceRange("hi", Pos(0, 2));
michael@0: m1.clear();
michael@0: cm.undo();
michael@0: eq(m1.find(), null);
michael@0: }, {value: "hello"});
michael@0:
michael@0: testCM("markTextAllowEmpty", function(cm) {
michael@0: var m1 = cm.markText(Pos(0, 1), Pos(0, 2), {clearWhenEmpty: false});
michael@0: is(m1.find());
michael@0: cm.replaceRange("x", Pos(0, 0));
michael@0: is(m1.find());
michael@0: cm.replaceRange("y", Pos(0, 2));
michael@0: is(m1.find());
michael@0: cm.replaceRange("z", Pos(0, 3), Pos(0, 4));
michael@0: is(!m1.find());
michael@0: var m2 = cm.markText(Pos(0, 1), Pos(0, 2), {clearWhenEmpty: false,
michael@0: inclusiveLeft: true,
michael@0: inclusiveRight: true});
michael@0: cm.replaceRange("q", Pos(0, 1), Pos(0, 2));
michael@0: is(m2.find());
michael@0: cm.replaceRange("", Pos(0, 0), Pos(0, 3));
michael@0: is(!m2.find());
michael@0: var m3 = cm.markText(Pos(0, 1), Pos(0, 1), {clearWhenEmpty: false});
michael@0: cm.replaceRange("a", Pos(0, 3));
michael@0: is(m3.find());
michael@0: cm.replaceRange("b", Pos(0, 1));
michael@0: is(!m3.find());
michael@0: }, {value: "abcde"});
michael@0:
michael@0: testCM("markTextStacked", function(cm) {
michael@0: var m1 = cm.markText(Pos(0, 0), Pos(0, 0), {clearWhenEmpty: false});
michael@0: var m2 = cm.markText(Pos(0, 0), Pos(0, 0), {clearWhenEmpty: false});
michael@0: cm.replaceRange("B", Pos(0, 1));
michael@0: is(m1.find() && m2.find());
michael@0: }, {value: "A"});
michael@0:
michael@0: testCM("undoPreservesNewMarks", function(cm) {
michael@0: cm.markText(Pos(0, 3), Pos(0, 4));
michael@0: cm.markText(Pos(1, 1), Pos(1, 3));
michael@0: cm.replaceRange("", Pos(0, 3), Pos(3, 1));
michael@0: var mBefore = cm.markText(Pos(0, 0), Pos(0, 1));
michael@0: var mAfter = cm.markText(Pos(0, 5), Pos(0, 6));
michael@0: var mAround = cm.markText(Pos(0, 2), Pos(0, 4));
michael@0: cm.undo();
michael@0: eqPos(mBefore.find().from, Pos(0, 0));
michael@0: eqPos(mBefore.find().to, Pos(0, 1));
michael@0: eqPos(mAfter.find().from, Pos(3, 3));
michael@0: eqPos(mAfter.find().to, Pos(3, 4));
michael@0: eqPos(mAround.find().from, Pos(0, 2));
michael@0: eqPos(mAround.find().to, Pos(3, 2));
michael@0: var found = cm.findMarksAt(Pos(2, 2));
michael@0: eq(found.length, 1);
michael@0: eq(found[0], mAround);
michael@0: }, {value: "aaaa\nbbbb\ncccc\ndddd"});
michael@0:
michael@0: testCM("markClearBetween", function(cm) {
michael@0: cm.setValue("aaa\nbbb\nccc\nddd\n");
michael@0: cm.markText(Pos(0, 0), Pos(2));
michael@0: cm.replaceRange("aaa\nbbb\nccc", Pos(0, 0), Pos(2));
michael@0: eq(cm.findMarksAt(Pos(1, 1)).length, 0);
michael@0: });
michael@0:
michael@0: testCM("deleteSpanCollapsedInclusiveLeft", function(cm) {
michael@0: var from = Pos(1, 0), to = Pos(1, 1);
michael@0: var m = cm.markText(from, to, {collapsed: true, inclusiveLeft: true});
michael@0: // Delete collapsed span.
michael@0: cm.replaceRange("", from, to);
michael@0: }, {value: "abc\nX\ndef"});
michael@0:
michael@0: testCM("bookmark", function(cm) {
michael@0: function p(v) { return v && Pos(v[0], v[1]); }
michael@0: forEach([{a: [1, 0], b: [1, 1], c: "", d: [1, 4]},
michael@0: {a: [1, 1], b: [1, 1], c: "xx", d: [1, 7]},
michael@0: {a: [1, 4], b: [1, 5], c: "ab", d: [1, 6]},
michael@0: {a: [1, 4], b: [1, 6], c: "", d: null},
michael@0: {a: [1, 5], b: [1, 6], c: "abc", d: [1, 5]},
michael@0: {a: [1, 6], b: [1, 8], c: "", d: [1, 5]},
michael@0: {a: [1, 4], b: [1, 4], c: "\n\n", d: [3, 1]},
michael@0: {bm: [1, 9], a: [1, 1], b: [1, 1], c: "\n", d: [2, 8]}], function(test) {
michael@0: cm.setValue("1234567890\n1234567890\n1234567890");
michael@0: var b = cm.setBookmark(p(test.bm) || Pos(1, 5));
michael@0: cm.replaceRange(test.c, p(test.a), p(test.b));
michael@0: eqPos(b.find(), p(test.d));
michael@0: });
michael@0: });
michael@0:
michael@0: testCM("bookmarkInsertLeft", function(cm) {
michael@0: var br = cm.setBookmark(Pos(0, 2), {insertLeft: false});
michael@0: var bl = cm.setBookmark(Pos(0, 2), {insertLeft: true});
michael@0: cm.setCursor(Pos(0, 2));
michael@0: cm.replaceSelection("hi");
michael@0: eqPos(br.find(), Pos(0, 2));
michael@0: eqPos(bl.find(), Pos(0, 4));
michael@0: cm.replaceRange("", Pos(0, 4), Pos(0, 5));
michael@0: cm.replaceRange("", Pos(0, 2), Pos(0, 4));
michael@0: cm.replaceRange("", Pos(0, 1), Pos(0, 2));
michael@0: // Verify that deleting next to bookmarks doesn't kill them
michael@0: eqPos(br.find(), Pos(0, 1));
michael@0: eqPos(bl.find(), Pos(0, 1));
michael@0: }, {value: "abcdef"});
michael@0:
michael@0: testCM("bookmarkCursor", function(cm) {
michael@0: var pos01 = cm.cursorCoords(Pos(0, 1)), pos11 = cm.cursorCoords(Pos(1, 1)),
michael@0: pos20 = cm.cursorCoords(Pos(2, 0)), pos30 = cm.cursorCoords(Pos(3, 0)),
michael@0: pos41 = cm.cursorCoords(Pos(4, 1));
michael@0: cm.setBookmark(Pos(0, 1), {widget: document.createTextNode("←"), insertLeft: true});
michael@0: cm.setBookmark(Pos(2, 0), {widget: document.createTextNode("←"), insertLeft: true});
michael@0: cm.setBookmark(Pos(1, 1), {widget: document.createTextNode("→")});
michael@0: cm.setBookmark(Pos(3, 0), {widget: document.createTextNode("→")});
michael@0: var new01 = cm.cursorCoords(Pos(0, 1)), new11 = cm.cursorCoords(Pos(1, 1)),
michael@0: new20 = cm.cursorCoords(Pos(2, 0)), new30 = cm.cursorCoords(Pos(3, 0));
michael@0: near(new01.left, pos01.left, 1);
michael@0: near(new01.top, pos01.top, 1);
michael@0: is(new11.left > pos11.left, "at right, middle of line");
michael@0: near(new11.top == pos11.top, 1);
michael@0: near(new20.left, pos20.left, 1);
michael@0: near(new20.top, pos20.top, 1);
michael@0: is(new30.left > pos30.left, "at right, empty line");
michael@0: near(new30.top, pos30, 1);
michael@0: cm.setBookmark(Pos(4, 0), {widget: document.createTextNode("→")});
michael@0: is(cm.cursorCoords(Pos(4, 1)).left > pos41.left, "single-char bug");
michael@0: }, {value: "foo\nbar\n\n\nx\ny"});
michael@0:
michael@0: testCM("multiBookmarkCursor", function(cm) {
michael@0: if (phantom) return;
michael@0: var ms = [], m;
michael@0: function add(insertLeft) {
michael@0: for (var i = 0; i < 3; ++i) {
michael@0: var node = document.createElement("span");
michael@0: node.innerHTML = "X";
michael@0: ms.push(cm.setBookmark(Pos(0, 1), {widget: node, insertLeft: insertLeft}));
michael@0: }
michael@0: }
michael@0: var base1 = cm.cursorCoords(Pos(0, 1)).left, base4 = cm.cursorCoords(Pos(0, 4)).left;
michael@0: add(true);
michael@0: near(base1, cm.cursorCoords(Pos(0, 1)).left, 1);
michael@0: while (m = ms.pop()) m.clear();
michael@0: add(false);
michael@0: near(base4, cm.cursorCoords(Pos(0, 1)).left, 1);
michael@0: }, {value: "abcdefg"});
michael@0:
michael@0: testCM("getAllMarks", function(cm) {
michael@0: addDoc(cm, 10, 10);
michael@0: var m1 = cm.setBookmark(Pos(0, 2));
michael@0: var m2 = cm.markText(Pos(0, 2), Pos(3, 2));
michael@0: var m3 = cm.markText(Pos(1, 2), Pos(1, 8));
michael@0: var m4 = cm.markText(Pos(8, 0), Pos(9, 0));
michael@0: eq(cm.getAllMarks().length, 4);
michael@0: m1.clear();
michael@0: m3.clear();
michael@0: eq(cm.getAllMarks().length, 2);
michael@0: });
michael@0:
michael@0: testCM("bug577", function(cm) {
michael@0: cm.setValue("a\nb");
michael@0: cm.clearHistory();
michael@0: cm.setValue("fooooo");
michael@0: cm.undo();
michael@0: });
michael@0:
michael@0: testCM("scrollSnap", function(cm) {
michael@0: cm.setSize(100, 100);
michael@0: addDoc(cm, 200, 200);
michael@0: cm.setCursor(Pos(100, 180));
michael@0: var info = cm.getScrollInfo();
michael@0: is(info.left > 0 && info.top > 0);
michael@0: cm.setCursor(Pos(0, 0));
michael@0: info = cm.getScrollInfo();
michael@0: is(info.left == 0 && info.top == 0, "scrolled clean to top");
michael@0: cm.setCursor(Pos(100, 180));
michael@0: cm.setCursor(Pos(199, 0));
michael@0: info = cm.getScrollInfo();
michael@0: is(info.left == 0 && info.top + 2 > info.height - cm.getScrollerElement().clientHeight, "scrolled clean to bottom");
michael@0: });
michael@0:
michael@0: testCM("scrollIntoView", function(cm) {
michael@0: if (phantom) return;
michael@0: var outer = cm.getWrapperElement().getBoundingClientRect();
michael@0: function test(line, ch, msg) {
michael@0: var pos = Pos(line, ch);
michael@0: cm.scrollIntoView(pos);
michael@0: var box = cm.charCoords(pos, "window");
michael@0: is(box.left >= outer.left, msg + " (left)");
michael@0: is(box.right <= outer.right, msg + " (right)");
michael@0: is(box.top >= outer.top, msg + " (top)");
michael@0: is(box.bottom <= outer.bottom, msg + " (bottom)");
michael@0: }
michael@0: addDoc(cm, 200, 200);
michael@0: test(199, 199, "bottom right");
michael@0: test(0, 0, "top left");
michael@0: test(100, 100, "center");
michael@0: test(199, 0, "bottom left");
michael@0: test(0, 199, "top right");
michael@0: test(100, 100, "center again");
michael@0: });
michael@0:
michael@0: testCM("scrollBackAndForth", function(cm) {
michael@0: addDoc(cm, 1, 200);
michael@0: cm.operation(function() {
michael@0: cm.scrollIntoView(Pos(199, 0));
michael@0: cm.scrollIntoView(Pos(4, 0));
michael@0: });
michael@0: is(cm.getScrollInfo().top > 0);
michael@0: });
michael@0:
michael@0: testCM("selectAllNoScroll", function(cm) {
michael@0: addDoc(cm, 1, 200);
michael@0: cm.execCommand("selectAll");
michael@0: eq(cm.getScrollInfo().top, 0);
michael@0: cm.setCursor(199);
michael@0: cm.execCommand("selectAll");
michael@0: is(cm.getScrollInfo().top > 0);
michael@0: });
michael@0:
michael@0: testCM("selectionPos", function(cm) {
michael@0: if (phantom) return;
michael@0: cm.setSize(100, 100);
michael@0: addDoc(cm, 200, 100);
michael@0: cm.setSelection(Pos(1, 100), Pos(98, 100));
michael@0: var lineWidth = cm.charCoords(Pos(0, 200), "local").left;
michael@0: var lineHeight = (cm.charCoords(Pos(99)).top - cm.charCoords(Pos(0)).top) / 100;
michael@0: cm.scrollTo(0, 0);
michael@0: var selElt = byClassName(cm.getWrapperElement(), "CodeMirror-selected");
michael@0: var outer = cm.getWrapperElement().getBoundingClientRect();
michael@0: var sawMiddle, sawTop, sawBottom;
michael@0: for (var i = 0, e = selElt.length; i < e; ++i) {
michael@0: var box = selElt[i].getBoundingClientRect();
michael@0: var atLeft = box.left - outer.left < 30;
michael@0: var width = box.right - box.left;
michael@0: var atRight = box.right - outer.left > .8 * lineWidth;
michael@0: if (atLeft && atRight) {
michael@0: sawMiddle = true;
michael@0: is(box.bottom - box.top > 90 * lineHeight, "middle high");
michael@0: is(width > .9 * lineWidth, "middle wide");
michael@0: } else {
michael@0: is(width > .4 * lineWidth, "top/bot wide enough");
michael@0: is(width < .6 * lineWidth, "top/bot slim enough");
michael@0: if (atLeft) {
michael@0: sawBottom = true;
michael@0: is(box.top - outer.top > 96 * lineHeight, "bot below");
michael@0: } else if (atRight) {
michael@0: sawTop = true;
michael@0: is(box.top - outer.top < 2.1 * lineHeight, "top above");
michael@0: }
michael@0: }
michael@0: }
michael@0: is(sawTop && sawBottom && sawMiddle, "all parts");
michael@0: }, null);
michael@0:
michael@0: testCM("restoreHistory", function(cm) {
michael@0: cm.setValue("abc\ndef");
michael@0: cm.replaceRange("hello", Pos(1, 0), Pos(1));
michael@0: cm.replaceRange("goop", Pos(0, 0), Pos(0));
michael@0: cm.undo();
michael@0: var storedVal = cm.getValue(), storedHist = cm.getHistory();
michael@0: if (window.JSON) storedHist = JSON.parse(JSON.stringify(storedHist));
michael@0: eq(storedVal, "abc\nhello");
michael@0: cm.setValue("");
michael@0: cm.clearHistory();
michael@0: eq(cm.historySize().undo, 0);
michael@0: cm.setValue(storedVal);
michael@0: cm.setHistory(storedHist);
michael@0: cm.redo();
michael@0: eq(cm.getValue(), "goop\nhello");
michael@0: cm.undo(); cm.undo();
michael@0: eq(cm.getValue(), "abc\ndef");
michael@0: });
michael@0:
michael@0: testCM("doubleScrollbar", function(cm) {
michael@0: var dummy = document.body.appendChild(document.createElement("p"));
michael@0: dummy.style.cssText = "height: 50px; overflow: scroll; width: 50px";
michael@0: var scrollbarWidth = dummy.offsetWidth + 1 - dummy.clientWidth;
michael@0: document.body.removeChild(dummy);
michael@0: if (scrollbarWidth < 2) return;
michael@0: cm.setSize(null, 100);
michael@0: addDoc(cm, 1, 300);
michael@0: var wrap = cm.getWrapperElement();
michael@0: is(wrap.offsetWidth - byClassName(wrap, "CodeMirror-lines")[0].offsetWidth <= scrollbarWidth * 1.5);
michael@0: });
michael@0:
michael@0: testCM("weirdLinebreaks", function(cm) {
michael@0: cm.setValue("foo\nbar\rbaz\r\nquux\n\rplop");
michael@0: is(cm.getValue(), "foo\nbar\nbaz\nquux\n\nplop");
michael@0: is(cm.lineCount(), 6);
michael@0: cm.setValue("\n\n");
michael@0: is(cm.lineCount(), 3);
michael@0: });
michael@0:
michael@0: testCM("setSize", function(cm) {
michael@0: cm.setSize(100, 100);
michael@0: var wrap = cm.getWrapperElement();
michael@0: is(wrap.offsetWidth, 100);
michael@0: is(wrap.offsetHeight, 100);
michael@0: cm.setSize("100%", "3em");
michael@0: is(wrap.style.width, "100%");
michael@0: is(wrap.style.height, "3em");
michael@0: cm.setSize(null, 40);
michael@0: is(wrap.style.width, "100%");
michael@0: is(wrap.style.height, "40px");
michael@0: });
michael@0:
michael@0: function foldLines(cm, start, end, autoClear) {
michael@0: return cm.markText(Pos(start, 0), Pos(end - 1), {
michael@0: inclusiveLeft: true,
michael@0: inclusiveRight: true,
michael@0: collapsed: true,
michael@0: clearOnEnter: autoClear
michael@0: });
michael@0: }
michael@0:
michael@0: testCM("collapsedLines", function(cm) {
michael@0: addDoc(cm, 4, 10);
michael@0: var range = foldLines(cm, 4, 5), cleared = 0;
michael@0: CodeMirror.on(range, "clear", function() {cleared++;});
michael@0: cm.setCursor(Pos(3, 0));
michael@0: CodeMirror.commands.goLineDown(cm);
michael@0: eqPos(cm.getCursor(), Pos(5, 0));
michael@0: cm.replaceRange("abcdefg", Pos(3, 0), Pos(3));
michael@0: cm.setCursor(Pos(3, 6));
michael@0: CodeMirror.commands.goLineDown(cm);
michael@0: eqPos(cm.getCursor(), Pos(5, 4));
michael@0: cm.replaceRange("ab", Pos(3, 0), Pos(3));
michael@0: cm.setCursor(Pos(3, 2));
michael@0: CodeMirror.commands.goLineDown(cm);
michael@0: eqPos(cm.getCursor(), Pos(5, 2));
michael@0: cm.operation(function() {range.clear(); range.clear();});
michael@0: eq(cleared, 1);
michael@0: });
michael@0:
michael@0: testCM("collapsedRangeCoordsChar", function(cm) {
michael@0: var pos_1_3 = cm.charCoords(Pos(1, 3));
michael@0: pos_1_3.left += 2; pos_1_3.top += 2;
michael@0: var opts = {collapsed: true, inclusiveLeft: true, inclusiveRight: true};
michael@0: var m1 = cm.markText(Pos(0, 0), Pos(2, 0), opts);
michael@0: eqPos(cm.coordsChar(pos_1_3), Pos(3, 3));
michael@0: m1.clear();
michael@0: var m1 = cm.markText(Pos(0, 0), Pos(1, 1), {collapsed: true, inclusiveLeft: true});
michael@0: var m2 = cm.markText(Pos(1, 1), Pos(2, 0), {collapsed: true, inclusiveRight: true});
michael@0: eqPos(cm.coordsChar(pos_1_3), Pos(3, 3));
michael@0: m1.clear(); m2.clear();
michael@0: var m1 = cm.markText(Pos(0, 0), Pos(1, 6), opts);
michael@0: eqPos(cm.coordsChar(pos_1_3), Pos(3, 3));
michael@0: }, {value: "123456\nabcdef\nghijkl\nmnopqr\n"});
michael@0:
michael@0: testCM("collapsedRangeBetweenLinesSelected", function(cm) {
michael@0: var widget = document.createElement("span");
michael@0: widget.textContent = "\u2194";
michael@0: cm.markText(Pos(0, 3), Pos(1, 0), {replacedWith: widget});
michael@0: cm.setSelection(Pos(0, 3), Pos(1, 0));
michael@0: var selElts = byClassName(cm.getWrapperElement(), "CodeMirror-selected");
michael@0: for (var i = 0, w = 0; i < selElts.length; i++)
michael@0: w += selElts[i].offsetWidth;
michael@0: is(w > 0);
michael@0: }, {value: "one\ntwo"});
michael@0:
michael@0: testCM("randomCollapsedRanges", function(cm) {
michael@0: addDoc(cm, 20, 500);
michael@0: cm.operation(function() {
michael@0: for (var i = 0; i < 200; i++) {
michael@0: var start = Pos(Math.floor(Math.random() * 500), Math.floor(Math.random() * 20));
michael@0: if (i % 4)
michael@0: try { cm.markText(start, Pos(start.line + 2, 1), {collapsed: true}); }
michael@0: catch(e) { if (!/overlapping/.test(String(e))) throw e; }
michael@0: else
michael@0: cm.markText(start, Pos(start.line, start.ch + 4), {"className": "foo"});
michael@0: }
michael@0: });
michael@0: });
michael@0:
michael@0: testCM("hiddenLinesAutoUnfold", function(cm) {
michael@0: var range = foldLines(cm, 1, 3, true), cleared = 0;
michael@0: CodeMirror.on(range, "clear", function() {cleared++;});
michael@0: cm.setCursor(Pos(3, 0));
michael@0: eq(cleared, 0);
michael@0: cm.execCommand("goCharLeft");
michael@0: eq(cleared, 1);
michael@0: range = foldLines(cm, 1, 3, true);
michael@0: CodeMirror.on(range, "clear", function() {cleared++;});
michael@0: eqPos(cm.getCursor(), Pos(3, 0));
michael@0: cm.setCursor(Pos(0, 3));
michael@0: cm.execCommand("goCharRight");
michael@0: eq(cleared, 2);
michael@0: }, {value: "abc\ndef\nghi\njkl"});
michael@0:
michael@0: testCM("hiddenLinesSelectAll", function(cm) { // Issue #484
michael@0: addDoc(cm, 4, 20);
michael@0: foldLines(cm, 0, 10);
michael@0: foldLines(cm, 11, 20);
michael@0: CodeMirror.commands.selectAll(cm);
michael@0: eqPos(cm.getCursor(true), Pos(10, 0));
michael@0: eqPos(cm.getCursor(false), Pos(10, 4));
michael@0: });
michael@0:
michael@0:
michael@0: testCM("everythingFolded", function(cm) {
michael@0: addDoc(cm, 2, 2);
michael@0: function enterPress() {
michael@0: cm.triggerOnKeyDown({type: "keydown", keyCode: 13, preventDefault: function(){}, stopPropagation: function(){}});
michael@0: }
michael@0: var fold = foldLines(cm, 0, 2);
michael@0: enterPress();
michael@0: eq(cm.getValue(), "xx\nxx");
michael@0: fold.clear();
michael@0: fold = foldLines(cm, 0, 2, true);
michael@0: eq(fold.find(), null);
michael@0: enterPress();
michael@0: eq(cm.getValue(), "\nxx\nxx");
michael@0: });
michael@0:
michael@0: testCM("structuredFold", function(cm) {
michael@0: if (phantom) return;
michael@0: addDoc(cm, 4, 8);
michael@0: var range = cm.markText(Pos(1, 2), Pos(6, 2), {
michael@0: replacedWith: document.createTextNode("Q")
michael@0: });
michael@0: cm.setCursor(0, 3);
michael@0: CodeMirror.commands.goLineDown(cm);
michael@0: eqPos(cm.getCursor(), Pos(6, 2));
michael@0: CodeMirror.commands.goCharLeft(cm);
michael@0: eqPos(cm.getCursor(), Pos(1, 2));
michael@0: CodeMirror.commands.delCharAfter(cm);
michael@0: eq(cm.getValue(), "xxxx\nxxxx\nxxxx");
michael@0: addDoc(cm, 4, 8);
michael@0: range = cm.markText(Pos(1, 2), Pos(6, 2), {
michael@0: replacedWith: document.createTextNode("M"),
michael@0: clearOnEnter: true
michael@0: });
michael@0: var cleared = 0;
michael@0: CodeMirror.on(range, "clear", function(){++cleared;});
michael@0: cm.setCursor(0, 3);
michael@0: CodeMirror.commands.goLineDown(cm);
michael@0: eqPos(cm.getCursor(), Pos(6, 2));
michael@0: CodeMirror.commands.goCharLeft(cm);
michael@0: eqPos(cm.getCursor(), Pos(6, 1));
michael@0: eq(cleared, 1);
michael@0: range.clear();
michael@0: eq(cleared, 1);
michael@0: range = cm.markText(Pos(1, 2), Pos(6, 2), {
michael@0: replacedWith: document.createTextNode("Q"),
michael@0: clearOnEnter: true
michael@0: });
michael@0: range.clear();
michael@0: cm.setCursor(1, 2);
michael@0: CodeMirror.commands.goCharRight(cm);
michael@0: eqPos(cm.getCursor(), Pos(1, 3));
michael@0: range = cm.markText(Pos(2, 0), Pos(4, 4), {
michael@0: replacedWith: document.createTextNode("M")
michael@0: });
michael@0: cm.setCursor(1, 0);
michael@0: CodeMirror.commands.goLineDown(cm);
michael@0: eqPos(cm.getCursor(), Pos(2, 0));
michael@0: }, null);
michael@0:
michael@0: testCM("nestedFold", function(cm) {
michael@0: addDoc(cm, 10, 3);
michael@0: function fold(ll, cl, lr, cr) {
michael@0: return cm.markText(Pos(ll, cl), Pos(lr, cr), {collapsed: true});
michael@0: }
michael@0: 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: cm.setCursor(0, 1);
michael@0: CodeMirror.commands.goCharRight(cm);
michael@0: eqPos(cm.getCursor(), Pos(2, 3));
michael@0: inner0.clear();
michael@0: CodeMirror.commands.goCharLeft(cm);
michael@0: eqPos(cm.getCursor(), Pos(0, 1));
michael@0: outer.clear();
michael@0: CodeMirror.commands.goCharRight(cm);
michael@0: eqPos(cm.getCursor(), Pos(0, 2));
michael@0: CodeMirror.commands.goCharRight(cm);
michael@0: eqPos(cm.getCursor(), Pos(1, 8));
michael@0: inner2.clear();
michael@0: CodeMirror.commands.goCharLeft(cm);
michael@0: eqPos(cm.getCursor(), Pos(1, 7));
michael@0: cm.setCursor(0, 5);
michael@0: CodeMirror.commands.goCharRight(cm);
michael@0: eqPos(cm.getCursor(), Pos(0, 6));
michael@0: CodeMirror.commands.goCharRight(cm);
michael@0: eqPos(cm.getCursor(), Pos(1, 3));
michael@0: });
michael@0:
michael@0: testCM("badNestedFold", function(cm) {
michael@0: addDoc(cm, 4, 4);
michael@0: cm.markText(Pos(0, 2), Pos(3, 2), {collapsed: true});
michael@0: var caught;
michael@0: try {cm.markText(Pos(0, 1), Pos(0, 3), {collapsed: true});}
michael@0: catch(e) {caught = e;}
michael@0: is(caught instanceof Error, "no error");
michael@0: is(/overlap/i.test(caught.message), "wrong error");
michael@0: });
michael@0:
michael@0: testCM("nestedFoldOnSide", function(cm) {
michael@0: var m1 = cm.markText(Pos(0, 1), Pos(2, 1), {collapsed: true, inclusiveRight: true});
michael@0: var m2 = cm.markText(Pos(0, 1), Pos(0, 2), {collapsed: true});
michael@0: cm.markText(Pos(0, 1), Pos(0, 2), {collapsed: true}).clear();
michael@0: try { cm.markText(Pos(0, 1), Pos(0, 2), {collapsed: true, inclusiveLeft: true}); }
michael@0: catch(e) { var caught = e; }
michael@0: is(caught && /overlap/i.test(caught.message));
michael@0: var m3 = cm.markText(Pos(2, 0), Pos(2, 1), {collapsed: true});
michael@0: var m4 = cm.markText(Pos(2, 0), Pos(2, 1), {collapse: true, inclusiveRight: true});
michael@0: m1.clear(); m4.clear();
michael@0: m1 = cm.markText(Pos(0, 1), Pos(2, 1), {collapsed: true});
michael@0: cm.markText(Pos(2, 0), Pos(2, 1), {collapsed: true}).clear();
michael@0: try { cm.markText(Pos(2, 0), Pos(2, 1), {collapsed: true, inclusiveRight: true}); }
michael@0: catch(e) { var caught = e; }
michael@0: is(caught && /overlap/i.test(caught.message));
michael@0: }, {value: "ab\ncd\ef"});
michael@0:
michael@0: testCM("editInFold", function(cm) {
michael@0: addDoc(cm, 4, 6);
michael@0: var m = cm.markText(Pos(1, 2), Pos(3, 2), {collapsed: true});
michael@0: cm.replaceRange("", Pos(0, 0), Pos(1, 3));
michael@0: cm.replaceRange("", Pos(2, 1), Pos(3, 3));
michael@0: cm.replaceRange("a\nb\nc\nd", Pos(0, 1), Pos(1, 0));
michael@0: cm.cursorCoords(Pos(0, 0));
michael@0: });
michael@0:
michael@0: testCM("wrappingInlineWidget", function(cm) {
michael@0: cm.setSize("11em");
michael@0: var w = document.createElement("span");
michael@0: w.style.color = "red";
michael@0: w.innerHTML = "one two three four";
michael@0: cm.markText(Pos(0, 6), Pos(0, 9), {replacedWith: w});
michael@0: var cur0 = cm.cursorCoords(Pos(0, 0)), cur1 = cm.cursorCoords(Pos(0, 10));
michael@0: is(cur0.top < cur1.top);
michael@0: is(cur0.bottom < cur1.bottom);
michael@0: var curL = cm.cursorCoords(Pos(0, 6)), curR = cm.cursorCoords(Pos(0, 9));
michael@0: eq(curL.top, cur0.top);
michael@0: eq(curL.bottom, cur0.bottom);
michael@0: eq(curR.top, cur1.top);
michael@0: eq(curR.bottom, cur1.bottom);
michael@0: cm.replaceRange("", Pos(0, 9), Pos(0));
michael@0: curR = cm.cursorCoords(Pos(0, 9));
michael@0: if (phantom) return;
michael@0: eq(curR.top, cur1.top);
michael@0: eq(curR.bottom, cur1.bottom);
michael@0: }, {value: "1 2 3 xxx 4", lineWrapping: true});
michael@0:
michael@0: testCM("changedInlineWidget", function(cm) {
michael@0: cm.setSize("10em");
michael@0: var w = document.createElement("span");
michael@0: w.innerHTML = "x";
michael@0: var m = cm.markText(Pos(0, 4), Pos(0, 5), {replacedWith: w});
michael@0: w.innerHTML = "and now the widget is really really long all of a sudden and a scrollbar is needed";
michael@0: m.changed();
michael@0: var hScroll = byClassName(cm.getWrapperElement(), "CodeMirror-hscrollbar")[0];
michael@0: is(hScroll.scrollWidth > hScroll.clientWidth);
michael@0: }, {value: "hello there"});
michael@0:
michael@0: testCM("changedBookmark", function(cm) {
michael@0: cm.setSize("10em");
michael@0: var w = document.createElement("span");
michael@0: w.innerHTML = "x";
michael@0: var m = cm.setBookmark(Pos(0, 4), {widget: w});
michael@0: w.innerHTML = "and now the widget is really really long all of a sudden and a scrollbar is needed";
michael@0: m.changed();
michael@0: var hScroll = byClassName(cm.getWrapperElement(), "CodeMirror-hscrollbar")[0];
michael@0: is(hScroll.scrollWidth > hScroll.clientWidth);
michael@0: }, {value: "abcdefg"});
michael@0:
michael@0: testCM("inlineWidget", function(cm) {
michael@0: var w = cm.setBookmark(Pos(0, 2), {widget: document.createTextNode("uu")});
michael@0: cm.setCursor(0, 2);
michael@0: CodeMirror.commands.goLineDown(cm);
michael@0: eqPos(cm.getCursor(), Pos(1, 4));
michael@0: cm.setCursor(0, 2);
michael@0: cm.replaceSelection("hi");
michael@0: eqPos(w.find(), Pos(0, 2));
michael@0: cm.setCursor(0, 1);
michael@0: cm.replaceSelection("ay");
michael@0: eqPos(w.find(), Pos(0, 4));
michael@0: eq(cm.getLine(0), "uayuhiuu");
michael@0: }, {value: "uuuu\nuuuuuu"});
michael@0:
michael@0: testCM("wrappingAndResizing", function(cm) {
michael@0: cm.setSize(null, "auto");
michael@0: cm.setOption("lineWrapping", true);
michael@0: var wrap = cm.getWrapperElement(), h0 = wrap.offsetHeight;
michael@0: var doc = "xxx xxx xxx xxx xxx";
michael@0: cm.setValue(doc);
michael@0: for (var step = 10, w = cm.charCoords(Pos(0, 18), "div").right;; w += step) {
michael@0: cm.setSize(w);
michael@0: if (wrap.offsetHeight <= h0 * (opera_lt10 ? 1.2 : 1.5)) {
michael@0: if (step == 10) { w -= 10; step = 1; }
michael@0: else break;
michael@0: }
michael@0: }
michael@0: // Ensure that putting the cursor at the end of the maximally long
michael@0: // line doesn't cause wrapping to happen.
michael@0: cm.setCursor(Pos(0, doc.length));
michael@0: eq(wrap.offsetHeight, h0);
michael@0: cm.replaceSelection("x");
michael@0: is(wrap.offsetHeight > h0, "wrapping happens");
michael@0: // Now add a max-height and, in a document consisting of
michael@0: // almost-wrapped lines, go over it so that a scrollbar appears.
michael@0: cm.setValue(doc + "\n" + doc + "\n");
michael@0: cm.getScrollerElement().style.maxHeight = "100px";
michael@0: 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: forEach([Pos(0, doc.length), Pos(0, doc.length - 1),
michael@0: Pos(0, 0), Pos(1, doc.length), Pos(1, doc.length - 1)],
michael@0: function(pos) {
michael@0: var coords = cm.charCoords(pos);
michael@0: eqPos(pos, cm.coordsChar({left: coords.left + 2, top: coords.top + 5}));
michael@0: });
michael@0: }, null, ie_lt8);
michael@0:
michael@0: testCM("measureEndOfLine", function(cm) {
michael@0: cm.setSize(null, "auto");
michael@0: var inner = byClassName(cm.getWrapperElement(), "CodeMirror-lines")[0].firstChild;
michael@0: var lh = inner.offsetHeight;
michael@0: for (var step = 10, w = cm.charCoords(Pos(0, 7), "div").right;; w += step) {
michael@0: cm.setSize(w);
michael@0: if (inner.offsetHeight < 2.5 * lh) {
michael@0: if (step == 10) { w -= 10; step = 1; }
michael@0: else break;
michael@0: }
michael@0: }
michael@0: cm.setValue(cm.getValue() + "\n\n");
michael@0: var endPos = cm.charCoords(Pos(0, 18), "local");
michael@0: is(endPos.top > lh * .8, "not at top");
michael@0: is(endPos.left > w - 20, "not at right");
michael@0: endPos = cm.charCoords(Pos(0, 18));
michael@0: eqPos(cm.coordsChar({left: endPos.left, top: endPos.top + 5}), Pos(0, 18));
michael@0: }, {mode: "text/html", value: "", lineWrapping: true}, ie_lt8 || opera_lt10);
michael@0:
michael@0: testCM("scrollVerticallyAndHorizontally", function(cm) {
michael@0: cm.setSize(100, 100);
michael@0: addDoc(cm, 40, 40);
michael@0: cm.setCursor(39);
michael@0: var wrap = cm.getWrapperElement(), bar = byClassName(wrap, "CodeMirror-vscrollbar")[0];
michael@0: is(bar.offsetHeight < wrap.offsetHeight, "vertical scrollbar limited by horizontal one");
michael@0: var cursorBox = byClassName(wrap, "CodeMirror-cursor")[0].getBoundingClientRect();
michael@0: var editorBox = wrap.getBoundingClientRect();
michael@0: is(cursorBox.bottom < editorBox.top + cm.getScrollerElement().clientHeight,
michael@0: "bottom line visible");
michael@0: }, {lineNumbers: true});
michael@0:
michael@0: testCM("moveVstuck", function(cm) {
michael@0: var lines = byClassName(cm.getWrapperElement(), "CodeMirror-lines")[0].firstChild, h0 = lines.offsetHeight;
michael@0: var val = "fooooooooooooooooooooooooo baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaar\n";
michael@0: cm.setValue(val);
michael@0: for (var w = cm.charCoords(Pos(0, 26), "div").right * 2.8;; w += 5) {
michael@0: cm.setSize(w);
michael@0: if (lines.offsetHeight <= 3.5 * h0) break;
michael@0: }
michael@0: cm.setCursor(Pos(0, val.length - 1));
michael@0: cm.moveV(-1, "line");
michael@0: eqPos(cm.getCursor(), Pos(0, 26));
michael@0: }, {lineWrapping: true}, ie_lt8 || opera_lt10);
michael@0:
michael@0: testCM("collapseOnMove", function(cm) {
michael@0: cm.setSelection(Pos(0, 1), Pos(2, 4));
michael@0: cm.execCommand("goLineUp");
michael@0: is(!cm.somethingSelected());
michael@0: eqPos(cm.getCursor(), Pos(0, 1));
michael@0: cm.setSelection(Pos(0, 1), Pos(2, 4));
michael@0: cm.execCommand("goPageDown");
michael@0: is(!cm.somethingSelected());
michael@0: eqPos(cm.getCursor(), Pos(2, 4));
michael@0: cm.execCommand("goLineUp");
michael@0: cm.execCommand("goLineUp");
michael@0: eqPos(cm.getCursor(), Pos(0, 4));
michael@0: cm.setSelection(Pos(0, 1), Pos(2, 4));
michael@0: cm.execCommand("goCharLeft");
michael@0: is(!cm.somethingSelected());
michael@0: eqPos(cm.getCursor(), Pos(0, 1));
michael@0: }, {value: "aaaaa\nb\nccccc"});
michael@0:
michael@0: testCM("clickTab", function(cm) {
michael@0: var p0 = cm.charCoords(Pos(0, 0));
michael@0: eqPos(cm.coordsChar({left: p0.left + 5, top: p0.top + 5}), Pos(0, 0));
michael@0: eqPos(cm.coordsChar({left: p0.right - 5, top: p0.top + 5}), Pos(0, 1));
michael@0: }, {value: "\t\n\n", lineWrapping: true, tabSize: 8});
michael@0:
michael@0: testCM("verticalScroll", function(cm) {
michael@0: cm.setSize(100, 200);
michael@0: cm.setValue("foo\nbar\nbaz\n");
michael@0: var sc = cm.getScrollerElement(), baseWidth = sc.scrollWidth;
michael@0: cm.replaceRange("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah", Pos(0, 0), Pos(0));
michael@0: is(sc.scrollWidth > baseWidth, "scrollbar present");
michael@0: cm.replaceRange("foo", Pos(0, 0), Pos(0));
michael@0: if (!phantom) eq(sc.scrollWidth, baseWidth, "scrollbar gone");
michael@0: cm.replaceRange("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah", Pos(0, 0), Pos(0));
michael@0: cm.replaceRange("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbh", Pos(1, 0), Pos(1));
michael@0: is(sc.scrollWidth > baseWidth, "present again");
michael@0: var curWidth = sc.scrollWidth;
michael@0: cm.replaceRange("foo", Pos(0, 0), Pos(0));
michael@0: is(sc.scrollWidth < curWidth, "scrollbar smaller");
michael@0: is(sc.scrollWidth > baseWidth, "but still present");
michael@0: });
michael@0:
michael@0: testCM("extraKeys", function(cm) {
michael@0: var outcome;
michael@0: function fakeKey(expected, code, props) {
michael@0: if (typeof code == "string") code = code.charCodeAt(0);
michael@0: var e = {type: "keydown", keyCode: code, preventDefault: function(){}, stopPropagation: function(){}};
michael@0: if (props) for (var n in props) e[n] = props[n];
michael@0: outcome = null;
michael@0: cm.triggerOnKeyDown(e);
michael@0: eq(outcome, expected);
michael@0: }
michael@0: CodeMirror.commands.testCommand = function() {outcome = "tc";};
michael@0: CodeMirror.commands.goTestCommand = function() {outcome = "gtc";};
michael@0: cm.setOption("extraKeys", {"Shift-X": function() {outcome = "sx";},
michael@0: "X": function() {outcome = "x";},
michael@0: "Ctrl-Alt-U": function() {outcome = "cau";},
michael@0: "End": "testCommand",
michael@0: "Home": "goTestCommand",
michael@0: "Tab": false});
michael@0: fakeKey(null, "U");
michael@0: fakeKey("cau", "U", {ctrlKey: true, altKey: true});
michael@0: fakeKey(null, "U", {shiftKey: true, ctrlKey: true, altKey: true});
michael@0: fakeKey("x", "X");
michael@0: fakeKey("sx", "X", {shiftKey: true});
michael@0: fakeKey("tc", 35);
michael@0: fakeKey(null, 35, {shiftKey: true});
michael@0: fakeKey("gtc", 36);
michael@0: fakeKey("gtc", 36, {shiftKey: true});
michael@0: fakeKey(null, 9);
michael@0: }, null, window.opera && mac);
michael@0:
michael@0: testCM("wordMovementCommands", function(cm) {
michael@0: cm.execCommand("goWordLeft");
michael@0: eqPos(cm.getCursor(), Pos(0, 0));
michael@0: cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
michael@0: eqPos(cm.getCursor(), Pos(0, 7));
michael@0: cm.execCommand("goWordLeft");
michael@0: eqPos(cm.getCursor(), Pos(0, 5));
michael@0: cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
michael@0: eqPos(cm.getCursor(), Pos(0, 12));
michael@0: cm.execCommand("goWordLeft");
michael@0: eqPos(cm.getCursor(), Pos(0, 9));
michael@0: cm.execCommand("goWordRight"); cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
michael@0: eqPos(cm.getCursor(), Pos(0, 24));
michael@0: cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
michael@0: eqPos(cm.getCursor(), Pos(1, 9));
michael@0: cm.execCommand("goWordRight");
michael@0: eqPos(cm.getCursor(), Pos(1, 13));
michael@0: cm.execCommand("goWordRight"); cm.execCommand("goWordRight");
michael@0: eqPos(cm.getCursor(), Pos(2, 0));
michael@0: }, {value: "this is (the) firstline.\na foo12\u00e9\u00f8\u00d7bar\n"});
michael@0:
michael@0: testCM("groupMovementCommands", function(cm) {
michael@0: cm.execCommand("goGroupLeft");
michael@0: eqPos(cm.getCursor(), Pos(0, 0));
michael@0: cm.execCommand("goGroupRight");
michael@0: eqPos(cm.getCursor(), Pos(0, 4));
michael@0: cm.execCommand("goGroupRight");
michael@0: eqPos(cm.getCursor(), Pos(0, 7));
michael@0: cm.execCommand("goGroupRight");
michael@0: eqPos(cm.getCursor(), Pos(0, 10));
michael@0: cm.execCommand("goGroupLeft");
michael@0: eqPos(cm.getCursor(), Pos(0, 7));
michael@0: cm.execCommand("goGroupRight"); cm.execCommand("goGroupRight"); cm.execCommand("goGroupRight");
michael@0: eqPos(cm.getCursor(), Pos(0, 15));
michael@0: cm.setCursor(Pos(0, 17));
michael@0: cm.execCommand("goGroupLeft");
michael@0: eqPos(cm.getCursor(), Pos(0, 16));
michael@0: cm.execCommand("goGroupLeft");
michael@0: eqPos(cm.getCursor(), Pos(0, 14));
michael@0: cm.execCommand("goGroupRight"); cm.execCommand("goGroupRight");
michael@0: eqPos(cm.getCursor(), Pos(0, 20));
michael@0: cm.execCommand("goGroupRight");
michael@0: eqPos(cm.getCursor(), Pos(1, 0));
michael@0: cm.execCommand("goGroupRight");
michael@0: eqPos(cm.getCursor(), Pos(1, 2));
michael@0: cm.execCommand("goGroupRight");
michael@0: eqPos(cm.getCursor(), Pos(1, 5));
michael@0: cm.execCommand("goGroupLeft"); cm.execCommand("goGroupLeft");
michael@0: eqPos(cm.getCursor(), Pos(1, 0));
michael@0: cm.execCommand("goGroupLeft");
michael@0: eqPos(cm.getCursor(), Pos(0, 20));
michael@0: cm.execCommand("goGroupLeft");
michael@0: eqPos(cm.getCursor(), Pos(0, 16));
michael@0: }, {value: "booo ba---quux. ffff\n abc d"});
michael@0:
michael@0: testCM("groupsAndWhitespace", function(cm) {
michael@0: var positions = [Pos(0, 0), Pos(0, 2), Pos(0, 5), Pos(0, 9), Pos(0, 11),
michael@0: Pos(1, 0), Pos(1, 2), Pos(1, 5)];
michael@0: for (var i = 1; i < positions.length; i++) {
michael@0: cm.execCommand("goGroupRight");
michael@0: eqPos(cm.getCursor(), positions[i]);
michael@0: }
michael@0: for (var i = positions.length - 2; i >= 0; i--) {
michael@0: cm.execCommand("goGroupLeft");
michael@0: eqPos(cm.getCursor(), i == 2 ? Pos(0, 6) : positions[i]);
michael@0: }
michael@0: }, {value: " foo +++ \n bar"});
michael@0:
michael@0: testCM("charMovementCommands", function(cm) {
michael@0: cm.execCommand("goCharLeft"); cm.execCommand("goColumnLeft");
michael@0: eqPos(cm.getCursor(), Pos(0, 0));
michael@0: cm.execCommand("goCharRight"); cm.execCommand("goCharRight");
michael@0: eqPos(cm.getCursor(), Pos(0, 2));
michael@0: cm.setCursor(Pos(1, 0));
michael@0: cm.execCommand("goColumnLeft");
michael@0: eqPos(cm.getCursor(), Pos(1, 0));
michael@0: cm.execCommand("goCharLeft");
michael@0: eqPos(cm.getCursor(), Pos(0, 5));
michael@0: cm.execCommand("goColumnRight");
michael@0: eqPos(cm.getCursor(), Pos(0, 5));
michael@0: cm.execCommand("goCharRight");
michael@0: eqPos(cm.getCursor(), Pos(1, 0));
michael@0: cm.execCommand("goLineEnd");
michael@0: eqPos(cm.getCursor(), Pos(1, 5));
michael@0: cm.execCommand("goLineStartSmart");
michael@0: eqPos(cm.getCursor(), Pos(1, 1));
michael@0: cm.execCommand("goLineStartSmart");
michael@0: eqPos(cm.getCursor(), Pos(1, 0));
michael@0: cm.setCursor(Pos(2, 0));
michael@0: cm.execCommand("goCharRight"); cm.execCommand("goColumnRight");
michael@0: eqPos(cm.getCursor(), Pos(2, 0));
michael@0: }, {value: "line1\n ine2\n"});
michael@0:
michael@0: testCM("verticalMovementCommands", function(cm) {
michael@0: cm.execCommand("goLineUp");
michael@0: eqPos(cm.getCursor(), Pos(0, 0));
michael@0: cm.execCommand("goLineDown");
michael@0: if (!phantom) // This fails in PhantomJS, though not in a real Webkit
michael@0: eqPos(cm.getCursor(), Pos(1, 0));
michael@0: cm.setCursor(Pos(1, 12));
michael@0: cm.execCommand("goLineDown");
michael@0: eqPos(cm.getCursor(), Pos(2, 5));
michael@0: cm.execCommand("goLineDown");
michael@0: eqPos(cm.getCursor(), Pos(3, 0));
michael@0: cm.execCommand("goLineUp");
michael@0: eqPos(cm.getCursor(), Pos(2, 5));
michael@0: cm.execCommand("goLineUp");
michael@0: eqPos(cm.getCursor(), Pos(1, 12));
michael@0: cm.execCommand("goPageDown");
michael@0: eqPos(cm.getCursor(), Pos(5, 0));
michael@0: cm.execCommand("goPageDown"); cm.execCommand("goLineDown");
michael@0: eqPos(cm.getCursor(), Pos(5, 0));
michael@0: cm.execCommand("goPageUp");
michael@0: eqPos(cm.getCursor(), Pos(0, 0));
michael@0: }, {value: "line1\nlong long line2\nline3\n\nline5\n"});
michael@0:
michael@0: testCM("verticalMovementCommandsWrapping", function(cm) {
michael@0: cm.setSize(120);
michael@0: cm.setCursor(Pos(0, 5));
michael@0: cm.execCommand("goLineDown");
michael@0: eq(cm.getCursor().line, 0);
michael@0: is(cm.getCursor().ch > 5, "moved beyond wrap");
michael@0: for (var i = 0; ; ++i) {
michael@0: is(i < 20, "no endless loop");
michael@0: cm.execCommand("goLineDown");
michael@0: var cur = cm.getCursor();
michael@0: if (cur.line == 1) eq(cur.ch, 5);
michael@0: if (cur.line == 2) { eq(cur.ch, 1); break; }
michael@0: }
michael@0: }, {value: "a very long line that wraps around somehow so that we can test cursor movement\nshortone\nk",
michael@0: lineWrapping: true});
michael@0:
michael@0: testCM("rtlMovement", function(cm) {
michael@0: forEach(["خحج", "خحabcخحج", "abخحخحجcd", "abخde", "abخح2342خ1حج", "خ1ح2خح3حxج",
michael@0: "خحcd", "1خحcd", "abcdeح1ج", "خمرحبها مها!", "foobarر", "خ ة ق",
michael@0: "
"], function(line) {
michael@0: var inv = line.charAt(0) == "خ";
michael@0: cm.setValue(line + "\n"); cm.execCommand(inv ? "goLineEnd" : "goLineStart");
michael@0: var cursors = byClassName(cm.getWrapperElement(), "CodeMirror-cursors")[0];
michael@0: var cursor = cursors.firstChild;
michael@0: var prevX = cursor.offsetLeft, prevY = cursor.offsetTop;
michael@0: for (var i = 0; i <= line.length; ++i) {
michael@0: cm.execCommand("goCharRight");
michael@0: cursor = cursors.firstChild;
michael@0: if (i == line.length) is(cursor.offsetTop > prevY, "next line");
michael@0: else is(cursor.offsetLeft > prevX, "moved right");
michael@0: prevX = cursor.offsetLeft; prevY = cursor.offsetTop;
michael@0: }
michael@0: cm.setCursor(0, 0); cm.execCommand(inv ? "goLineStart" : "goLineEnd");
michael@0: prevX = cursors.firstChild.offsetLeft;
michael@0: for (var i = 0; i < line.length; ++i) {
michael@0: cm.execCommand("goCharLeft");
michael@0: cursor = cursors.firstChild;
michael@0: is(cursor.offsetLeft < prevX, "moved left");
michael@0: prevX = cursor.offsetLeft;
michael@0: }
michael@0: });
michael@0: }, null, ie_lt9);
michael@0:
michael@0: // Verify that updating a line clears its bidi ordering
michael@0: testCM("bidiUpdate", function(cm) {
michael@0: cm.setCursor(Pos(0, 2));
michael@0: cm.replaceSelection("خحج", "start");
michael@0: cm.execCommand("goCharRight");
michael@0: eqPos(cm.getCursor(), Pos(0, 4));
michael@0: }, {value: "abcd\n"});
michael@0:
michael@0: testCM("movebyTextUnit", function(cm) {
michael@0: cm.setValue("בְּרֵאשִ\nééé́\n");
michael@0: cm.execCommand("goLineEnd");
michael@0: for (var i = 0; i < 4; ++i) cm.execCommand("goCharRight");
michael@0: eqPos(cm.getCursor(), Pos(0, 0));
michael@0: cm.execCommand("goCharRight");
michael@0: eqPos(cm.getCursor(), Pos(1, 0));
michael@0: cm.execCommand("goCharRight");
michael@0: cm.execCommand("goCharRight");
michael@0: eqPos(cm.getCursor(), Pos(1, 4));
michael@0: cm.execCommand("goCharRight");
michael@0: eqPos(cm.getCursor(), Pos(1, 7));
michael@0: });
michael@0:
michael@0: testCM("lineChangeEvents", function(cm) {
michael@0: addDoc(cm, 3, 5);
michael@0: var log = [], want = ["ch 0", "ch 1", "del 2", "ch 0", "ch 0", "del 1", "del 3", "del 4"];
michael@0: for (var i = 0; i < 5; ++i) {
michael@0: CodeMirror.on(cm.getLineHandle(i), "delete", function(i) {
michael@0: return function() {log.push("del " + i);};
michael@0: }(i));
michael@0: CodeMirror.on(cm.getLineHandle(i), "change", function(i) {
michael@0: return function() {log.push("ch " + i);};
michael@0: }(i));
michael@0: }
michael@0: cm.replaceRange("x", Pos(0, 1));
michael@0: cm.replaceRange("xy", Pos(1, 1), Pos(2));
michael@0: cm.replaceRange("foo\nbar", Pos(0, 1));
michael@0: cm.replaceRange("", Pos(0, 0), Pos(cm.lineCount()));
michael@0: eq(log.length, want.length, "same length");
michael@0: for (var i = 0; i < log.length; ++i)
michael@0: eq(log[i], want[i]);
michael@0: });
michael@0:
michael@0: testCM("scrollEntirelyToRight", function(cm) {
michael@0: if (phantom) return;
michael@0: addDoc(cm, 500, 2);
michael@0: cm.setCursor(Pos(0, 500));
michael@0: var wrap = cm.getWrapperElement(), cur = byClassName(wrap, "CodeMirror-cursor")[0];
michael@0: is(wrap.getBoundingClientRect().right > cur.getBoundingClientRect().left);
michael@0: });
michael@0:
michael@0: testCM("lineWidgets", function(cm) {
michael@0: addDoc(cm, 500, 3);
michael@0: var last = cm.charCoords(Pos(2, 0));
michael@0: var node = document.createElement("div");
michael@0: node.innerHTML = "hi";
michael@0: var widget = cm.addLineWidget(1, node);
michael@0: is(last.top < cm.charCoords(Pos(2, 0)).top, "took up space");
michael@0: cm.setCursor(Pos(1, 1));
michael@0: cm.execCommand("goLineDown");
michael@0: eqPos(cm.getCursor(), Pos(2, 1));
michael@0: cm.execCommand("goLineUp");
michael@0: eqPos(cm.getCursor(), Pos(1, 1));
michael@0: });
michael@0:
michael@0: testCM("lineWidgetFocus", function(cm) {
michael@0: var place = document.getElementById("testground");
michael@0: place.className = "offscreen";
michael@0: try {
michael@0: addDoc(cm, 500, 10);
michael@0: var node = document.createElement("input");
michael@0: var widget = cm.addLineWidget(1, node);
michael@0: node.focus();
michael@0: eq(document.activeElement, node);
michael@0: cm.replaceRange("new stuff", Pos(1, 0));
michael@0: eq(document.activeElement, node);
michael@0: } finally {
michael@0: place.className = "";
michael@0: }
michael@0: });
michael@0:
michael@0: testCM("lineWidgetCautiousRedraw", function(cm) {
michael@0: var node = document.createElement("div");
michael@0: node.innerHTML = "hahah";
michael@0: var w = cm.addLineWidget(0, node);
michael@0: var redrawn = false;
michael@0: w.on("redraw", function() { redrawn = true; });
michael@0: cm.replaceSelection("0");
michael@0: is(!redrawn);
michael@0: }, {value: "123\n456"});
michael@0:
michael@0: testCM("lineWidgetChanged", function(cm) {
michael@0: addDoc(cm, 2, 300);
michael@0: cm.setSize(null, cm.defaultTextHeight() * 50);
michael@0: cm.scrollTo(null, cm.heightAtLine(125, "local"));
michael@0: function w() {
michael@0: var node = document.createElement("div");
michael@0: node.style.cssText = "background: yellow; height: 50px;";
michael@0: return node;
michael@0: }
michael@0: var info0 = cm.getScrollInfo();
michael@0: var w0 = cm.addLineWidget(0, w());
michael@0: var w150 = cm.addLineWidget(150, w());
michael@0: var w300 = cm.addLineWidget(300, w());
michael@0: var info1 = cm.getScrollInfo();
michael@0: eq(info0.height + 150, info1.height);
michael@0: eq(info0.top + 50, info1.top);
michael@0: w0.node.style.height = w150.node.style.height = w300.node.style.height = "10px";
michael@0: w0.changed(); w150.changed(); w300.changed();
michael@0: var info2 = cm.getScrollInfo();
michael@0: eq(info0.height + 30, info2.height);
michael@0: eq(info0.top + 10, info2.top);
michael@0: });
michael@0:
michael@0: testCM("getLineNumber", function(cm) {
michael@0: addDoc(cm, 2, 20);
michael@0: var h1 = cm.getLineHandle(1);
michael@0: eq(cm.getLineNumber(h1), 1);
michael@0: cm.replaceRange("hi\nbye\n", Pos(0, 0));
michael@0: eq(cm.getLineNumber(h1), 3);
michael@0: cm.setValue("");
michael@0: eq(cm.getLineNumber(h1), null);
michael@0: });
michael@0:
michael@0: testCM("jumpTheGap", function(cm) {
michael@0: if (phantom) return;
michael@0: var longLine = "abcdef ghiklmnop qrstuvw xyz ";
michael@0: longLine += longLine; longLine += longLine; longLine += longLine;
michael@0: cm.replaceRange(longLine, Pos(2, 0), Pos(2));
michael@0: cm.setSize("200px", null);
michael@0: cm.getWrapperElement().style.lineHeight = 2;
michael@0: cm.refresh();
michael@0: cm.setCursor(Pos(0, 1));
michael@0: cm.execCommand("goLineDown");
michael@0: eqPos(cm.getCursor(), Pos(1, 1));
michael@0: cm.execCommand("goLineDown");
michael@0: eqPos(cm.getCursor(), Pos(2, 1));
michael@0: cm.execCommand("goLineDown");
michael@0: eq(cm.getCursor().line, 2);
michael@0: is(cm.getCursor().ch > 1);
michael@0: cm.execCommand("goLineUp");
michael@0: eqPos(cm.getCursor(), Pos(2, 1));
michael@0: cm.execCommand("goLineUp");
michael@0: eqPos(cm.getCursor(), Pos(1, 1));
michael@0: var node = document.createElement("div");
michael@0: node.innerHTML = "hi"; node.style.height = "30px";
michael@0: cm.addLineWidget(0, node);
michael@0: cm.addLineWidget(1, node.cloneNode(true), {above: true});
michael@0: cm.setCursor(Pos(0, 2));
michael@0: cm.execCommand("goLineDown");
michael@0: eqPos(cm.getCursor(), Pos(1, 2));
michael@0: cm.execCommand("goLineUp");
michael@0: eqPos(cm.getCursor(), Pos(0, 2));
michael@0: }, {lineWrapping: true, value: "abc\ndef\nghi\njkl\n"});
michael@0:
michael@0: testCM("addLineClass", function(cm) {
michael@0: function cls(line, text, bg, wrap) {
michael@0: var i = cm.lineInfo(line);
michael@0: eq(i.textClass, text);
michael@0: eq(i.bgClass, bg);
michael@0: eq(i.wrapClass, wrap);
michael@0: }
michael@0: cm.addLineClass(0, "text", "foo");
michael@0: cm.addLineClass(0, "text", "bar");
michael@0: cm.addLineClass(1, "background", "baz");
michael@0: cm.addLineClass(1, "wrap", "foo");
michael@0: cls(0, "foo bar", null, null);
michael@0: cls(1, null, "baz", "foo");
michael@0: var lines = cm.display.lineDiv;
michael@0: eq(byClassName(lines, "foo").length, 2);
michael@0: eq(byClassName(lines, "bar").length, 1);
michael@0: eq(byClassName(lines, "baz").length, 1);
michael@0: cm.removeLineClass(0, "text", "foo");
michael@0: cls(0, "bar", null, null);
michael@0: cm.removeLineClass(0, "text", "foo");
michael@0: cls(0, "bar", null, null);
michael@0: cm.removeLineClass(0, "text", "bar");
michael@0: cls(0, null, null, null);
michael@0: cm.addLineClass(1, "wrap", "quux");
michael@0: cls(1, null, "baz", "foo quux");
michael@0: cm.removeLineClass(1, "wrap");
michael@0: cls(1, null, "baz", null);
michael@0: }, {value: "hohoho\n"});
michael@0:
michael@0: testCM("atomicMarker", function(cm) {
michael@0: addDoc(cm, 10, 10);
michael@0: function atom(ll, cl, lr, cr, li, ri) {
michael@0: return cm.markText(Pos(ll, cl), Pos(lr, cr),
michael@0: {atomic: true, inclusiveLeft: li, inclusiveRight: ri});
michael@0: }
michael@0: var m = atom(0, 1, 0, 5);
michael@0: cm.setCursor(Pos(0, 1));
michael@0: cm.execCommand("goCharRight");
michael@0: eqPos(cm.getCursor(), Pos(0, 5));
michael@0: cm.execCommand("goCharLeft");
michael@0: eqPos(cm.getCursor(), Pos(0, 1));
michael@0: m.clear();
michael@0: m = atom(0, 0, 0, 5, true);
michael@0: eqPos(cm.getCursor(), Pos(0, 5), "pushed out");
michael@0: cm.execCommand("goCharLeft");
michael@0: eqPos(cm.getCursor(), Pos(0, 5));
michael@0: m.clear();
michael@0: m = atom(8, 4, 9, 10, false, true);
michael@0: cm.setCursor(Pos(9, 8));
michael@0: eqPos(cm.getCursor(), Pos(8, 4), "set");
michael@0: cm.execCommand("goCharRight");
michael@0: eqPos(cm.getCursor(), Pos(8, 4), "char right");
michael@0: cm.execCommand("goLineDown");
michael@0: eqPos(cm.getCursor(), Pos(8, 4), "line down");
michael@0: cm.execCommand("goCharLeft");
michael@0: eqPos(cm.getCursor(), Pos(8, 3));
michael@0: m.clear();
michael@0: m = atom(1, 1, 3, 8);
michael@0: cm.setCursor(Pos(0, 0));
michael@0: cm.setCursor(Pos(2, 0));
michael@0: eqPos(cm.getCursor(), Pos(3, 8));
michael@0: cm.execCommand("goCharLeft");
michael@0: eqPos(cm.getCursor(), Pos(1, 1));
michael@0: cm.execCommand("goCharRight");
michael@0: eqPos(cm.getCursor(), Pos(3, 8));
michael@0: cm.execCommand("goLineUp");
michael@0: eqPos(cm.getCursor(), Pos(1, 1));
michael@0: cm.execCommand("goLineDown");
michael@0: eqPos(cm.getCursor(), Pos(3, 8));
michael@0: cm.execCommand("delCharBefore");
michael@0: eq(cm.getValue().length, 80, "del chunk");
michael@0: m = atom(3, 0, 5, 5);
michael@0: cm.setCursor(Pos(3, 0));
michael@0: cm.execCommand("delWordAfter");
michael@0: eq(cm.getValue().length, 53, "del chunk");
michael@0: });
michael@0:
michael@0: testCM("readOnlyMarker", function(cm) {
michael@0: function mark(ll, cl, lr, cr, at) {
michael@0: return cm.markText(Pos(ll, cl), Pos(lr, cr),
michael@0: {readOnly: true, atomic: at});
michael@0: }
michael@0: var m = mark(0, 1, 0, 4);
michael@0: cm.setCursor(Pos(0, 2));
michael@0: cm.replaceSelection("hi", "end");
michael@0: eqPos(cm.getCursor(), Pos(0, 2));
michael@0: eq(cm.getLine(0), "abcde");
michael@0: cm.execCommand("selectAll");
michael@0: cm.replaceSelection("oops", "around");
michael@0: eq(cm.getValue(), "oopsbcd");
michael@0: cm.undo();
michael@0: eqPos(m.find().from, Pos(0, 1));
michael@0: eqPos(m.find().to, Pos(0, 4));
michael@0: m.clear();
michael@0: cm.setCursor(Pos(0, 2));
michael@0: cm.replaceSelection("hi", "around");
michael@0: eq(cm.getLine(0), "abhicde");
michael@0: eqPos(cm.getCursor(), Pos(0, 4));
michael@0: m = mark(0, 2, 2, 2, true);
michael@0: cm.setSelection(Pos(1, 1), Pos(2, 4));
michael@0: cm.replaceSelection("t", "end");
michael@0: eqPos(cm.getCursor(), Pos(2, 3));
michael@0: eq(cm.getLine(2), "klto");
michael@0: cm.execCommand("goCharLeft");
michael@0: cm.execCommand("goCharLeft");
michael@0: eqPos(cm.getCursor(), Pos(0, 2));
michael@0: cm.setSelection(Pos(0, 1), Pos(0, 3));
michael@0: cm.replaceSelection("xx", "around");
michael@0: eqPos(cm.getCursor(), Pos(0, 3));
michael@0: eq(cm.getLine(0), "axxhicde");
michael@0: }, {value: "abcde\nfghij\nklmno\n"});
michael@0:
michael@0: testCM("dirtyBit", function(cm) {
michael@0: eq(cm.isClean(), true);
michael@0: cm.replaceSelection("boo", null, "test");
michael@0: eq(cm.isClean(), false);
michael@0: cm.undo();
michael@0: eq(cm.isClean(), true);
michael@0: cm.replaceSelection("boo", null, "test");
michael@0: cm.replaceSelection("baz", null, "test");
michael@0: cm.undo();
michael@0: eq(cm.isClean(), false);
michael@0: cm.markClean();
michael@0: eq(cm.isClean(), true);
michael@0: cm.undo();
michael@0: eq(cm.isClean(), false);
michael@0: cm.redo();
michael@0: eq(cm.isClean(), true);
michael@0: });
michael@0:
michael@0: testCM("changeGeneration", function(cm) {
michael@0: cm.replaceSelection("x");
michael@0: var softGen = cm.changeGeneration();
michael@0: cm.replaceSelection("x");
michael@0: cm.undo();
michael@0: eq(cm.getValue(), "");
michael@0: is(!cm.isClean(softGen));
michael@0: cm.replaceSelection("x");
michael@0: var hardGen = cm.changeGeneration(true);
michael@0: cm.replaceSelection("x");
michael@0: cm.undo();
michael@0: eq(cm.getValue(), "x");
michael@0: is(cm.isClean(hardGen));
michael@0: });
michael@0:
michael@0: testCM("addKeyMap", function(cm) {
michael@0: function sendKey(code) {
michael@0: cm.triggerOnKeyDown({type: "keydown", keyCode: code,
michael@0: preventDefault: function(){}, stopPropagation: function(){}});
michael@0: }
michael@0:
michael@0: sendKey(39);
michael@0: eqPos(cm.getCursor(), Pos(0, 1));
michael@0: var test = 0;
michael@0: var map1 = {Right: function() { ++test; }}, map2 = {Right: function() { test += 10; }}
michael@0: cm.addKeyMap(map1);
michael@0: sendKey(39);
michael@0: eqPos(cm.getCursor(), Pos(0, 1));
michael@0: eq(test, 1);
michael@0: cm.addKeyMap(map2, true);
michael@0: sendKey(39);
michael@0: eq(test, 2);
michael@0: cm.removeKeyMap(map1);
michael@0: sendKey(39);
michael@0: eq(test, 12);
michael@0: cm.removeKeyMap(map2);
michael@0: sendKey(39);
michael@0: eq(test, 12);
michael@0: eqPos(cm.getCursor(), Pos(0, 2));
michael@0: cm.addKeyMap({Right: function() { test = 55; }, name: "mymap"});
michael@0: sendKey(39);
michael@0: eq(test, 55);
michael@0: cm.removeKeyMap("mymap");
michael@0: sendKey(39);
michael@0: eqPos(cm.getCursor(), Pos(0, 3));
michael@0: }, {value: "abc"});
michael@0:
michael@0: testCM("findPosH", function(cm) {
michael@0: forEach([{from: Pos(0, 0), to: Pos(0, 1), by: 1},
michael@0: {from: Pos(0, 0), to: Pos(0, 0), by: -1, hitSide: true},
michael@0: {from: Pos(0, 0), to: Pos(0, 4), by: 1, unit: "word"},
michael@0: {from: Pos(0, 0), to: Pos(0, 8), by: 2, unit: "word"},
michael@0: {from: Pos(0, 0), to: Pos(2, 0), by: 20, unit: "word", hitSide: true},
michael@0: {from: Pos(0, 7), to: Pos(0, 5), by: -1, unit: "word"},
michael@0: {from: Pos(0, 4), to: Pos(0, 8), by: 1, unit: "word"},
michael@0: {from: Pos(1, 0), to: Pos(1, 18), by: 3, unit: "word"},
michael@0: {from: Pos(1, 22), to: Pos(1, 5), by: -3, unit: "word"},
michael@0: {from: Pos(1, 15), to: Pos(1, 10), by: -5},
michael@0: {from: Pos(1, 15), to: Pos(1, 10), by: -5, unit: "column"},
michael@0: {from: Pos(1, 15), to: Pos(1, 0), by: -50, unit: "column", hitSide: true},
michael@0: {from: Pos(1, 15), to: Pos(1, 24), by: 50, unit: "column", hitSide: true},
michael@0: {from: Pos(1, 15), to: Pos(2, 0), by: 50, hitSide: true}], function(t) {
michael@0: var r = cm.findPosH(t.from, t.by, t.unit || "char");
michael@0: eqPos(r, t.to);
michael@0: eq(!!r.hitSide, !!t.hitSide);
michael@0: });
michael@0: }, {value: "line one\nline two.something.other\n"});
michael@0:
michael@0: testCM("beforeChange", function(cm) {
michael@0: cm.on("beforeChange", function(cm, change) {
michael@0: var text = [];
michael@0: for (var i = 0; i < change.text.length; ++i)
michael@0: text.push(change.text[i].replace(/\s/g, "_"));
michael@0: change.update(null, null, text);
michael@0: });
michael@0: cm.setValue("hello, i am a\nnew document\n");
michael@0: eq(cm.getValue(), "hello,_i_am_a\nnew_document\n");
michael@0: CodeMirror.on(cm.getDoc(), "beforeChange", function(doc, change) {
michael@0: if (change.from.line == 0) change.cancel();
michael@0: });
michael@0: cm.setValue("oops"); // Canceled
michael@0: eq(cm.getValue(), "hello,_i_am_a\nnew_document\n");
michael@0: cm.replaceRange("hey hey hey", Pos(1, 0), Pos(2, 0));
michael@0: eq(cm.getValue(), "hello,_i_am_a\nhey_hey_hey");
michael@0: }, {value: "abcdefghijk"});
michael@0:
michael@0: testCM("beforeChangeUndo", function(cm) {
michael@0: cm.replaceRange("hi", Pos(0, 0), Pos(0));
michael@0: cm.replaceRange("bye", Pos(0, 0), Pos(0));
michael@0: eq(cm.historySize().undo, 2);
michael@0: cm.on("beforeChange", function(cm, change) {
michael@0: is(!change.update);
michael@0: change.cancel();
michael@0: });
michael@0: cm.undo();
michael@0: eq(cm.historySize().undo, 0);
michael@0: eq(cm.getValue(), "bye\ntwo");
michael@0: }, {value: "one\ntwo"});
michael@0:
michael@0: testCM("beforeSelectionChange", function(cm) {
michael@0: function notAtEnd(cm, pos) {
michael@0: var len = cm.getLine(pos.line).length;
michael@0: if (!len || pos.ch == len) return Pos(pos.line, pos.ch - 1);
michael@0: return pos;
michael@0: }
michael@0: cm.on("beforeSelectionChange", function(cm, obj) {
michael@0: obj.update([{anchor: notAtEnd(cm, obj.ranges[0].anchor),
michael@0: head: notAtEnd(cm, obj.ranges[0].head)}]);
michael@0: });
michael@0:
michael@0: addDoc(cm, 10, 10);
michael@0: cm.execCommand("goLineEnd");
michael@0: eqPos(cm.getCursor(), Pos(0, 9));
michael@0: cm.execCommand("selectAll");
michael@0: eqPos(cm.getCursor("start"), Pos(0, 0));
michael@0: eqPos(cm.getCursor("end"), Pos(9, 9));
michael@0: });
michael@0:
michael@0: testCM("change_removedText", function(cm) {
michael@0: cm.setValue("abc\ndef");
michael@0:
michael@0: var removedText = [];
michael@0: cm.on("change", function(cm, change) {
michael@0: removedText.push(change.removed);
michael@0: });
michael@0:
michael@0: cm.operation(function() {
michael@0: cm.replaceRange("xyz", Pos(0, 0), Pos(1,1));
michael@0: cm.replaceRange("123", Pos(0,0));
michael@0: });
michael@0:
michael@0: eq(removedText.length, 2);
michael@0: eq(removedText[0].join("\n"), "abc\nd");
michael@0: eq(removedText[1].join("\n"), "");
michael@0:
michael@0: var removedText = [];
michael@0: cm.undo();
michael@0: eq(removedText.length, 2);
michael@0: eq(removedText[0].join("\n"), "123");
michael@0: eq(removedText[1].join("\n"), "xyz");
michael@0:
michael@0: var removedText = [];
michael@0: cm.redo();
michael@0: eq(removedText.length, 2);
michael@0: eq(removedText[0].join("\n"), "abc\nd");
michael@0: eq(removedText[1].join("\n"), "");
michael@0: });
michael@0:
michael@0: testCM("lineStyleFromMode", function(cm) {
michael@0: CodeMirror.defineMode("test_mode", function() {
michael@0: return {token: function(stream) {
michael@0: if (stream.match(/^\[[^\]]*\]/)) return " line-brackets ";
michael@0: if (stream.match(/^\([^\)]*\)/)) return " line-background-parens ";
michael@0: if (stream.match(/^<[^>]*>/)) return " span line-line line-background-bg ";
michael@0: stream.match(/^\s+|^\S+/);
michael@0: }};
michael@0: });
michael@0: cm.setOption("mode", "test_mode");
michael@0: var bracketElts = byClassName(cm.getWrapperElement(), "brackets");
michael@0: eq(bracketElts.length, 1, "brackets count");
michael@0: eq(bracketElts[0].nodeName, "PRE");
michael@0: is(!/brackets.*brackets/.test(bracketElts[0].className));
michael@0: var parenElts = byClassName(cm.getWrapperElement(), "parens");
michael@0: eq(parenElts.length, 1, "parens count");
michael@0: eq(parenElts[0].nodeName, "DIV");
michael@0: is(!/parens.*parens/.test(parenElts[0].className));
michael@0: eq(parenElts[0].parentElement.nodeName, "DIV");
michael@0:
michael@0: eq(byClassName(cm.getWrapperElement(), "bg").length, 1);
michael@0: eq(byClassName(cm.getWrapperElement(), "line").length, 1);
michael@0: var spanElts = byClassName(cm.getWrapperElement(), "cm-span");
michael@0: eq(spanElts.length, 2);
michael@0: is(/^\s*cm-span\s*$/.test(spanElts[0].className));
michael@0: }, {value: "line1: [br] [br]\nline2: (par) (par)\nline3: "});
michael@0:
michael@0: CodeMirror.registerHelper("xxx", "a", "A");
michael@0: CodeMirror.registerHelper("xxx", "b", "B");
michael@0: CodeMirror.defineMode("yyy", function() {
michael@0: return {
michael@0: token: function(stream) { stream.skipToEnd(); },
michael@0: xxx: ["a", "b", "q"]
michael@0: };
michael@0: });
michael@0: CodeMirror.registerGlobalHelper("xxx", "c", function(m) { return m.enableC; }, "C");
michael@0:
michael@0: testCM("helpers", function(cm) {
michael@0: cm.setOption("mode", "yyy");
michael@0: eq(cm.getHelpers(Pos(0, 0), "xxx").join("/"), "A/B");
michael@0: cm.setOption("mode", {name: "yyy", modeProps: {xxx: "b", enableC: true}});
michael@0: eq(cm.getHelpers(Pos(0, 0), "xxx").join("/"), "B/C");
michael@0: cm.setOption("mode", "javascript");
michael@0: eq(cm.getHelpers(Pos(0, 0), "xxx").join("/"), "");
michael@0: });
michael@0:
michael@0: testCM("selectionHistory", function(cm) {
michael@0: for (var i = 0; i < 3; i++) {
michael@0: cm.setExtending(true);
michael@0: cm.execCommand("goCharRight");
michael@0: cm.setExtending(false);
michael@0: cm.execCommand("goCharRight");
michael@0: cm.execCommand("goCharRight");
michael@0: }
michael@0: cm.execCommand("undoSelection");
michael@0: eq(cm.getSelection(), "c");
michael@0: cm.execCommand("undoSelection");
michael@0: eq(cm.getSelection(), "");
michael@0: eqPos(cm.getCursor(), Pos(0, 4));
michael@0: cm.execCommand("undoSelection");
michael@0: eq(cm.getSelection(), "b");
michael@0: cm.execCommand("redoSelection");
michael@0: eq(cm.getSelection(), "");
michael@0: eqPos(cm.getCursor(), Pos(0, 4));
michael@0: cm.execCommand("redoSelection");
michael@0: eq(cm.getSelection(), "c");
michael@0: cm.execCommand("redoSelection");
michael@0: eq(cm.getSelection(), "");
michael@0: eqPos(cm.getCursor(), Pos(0, 6));
michael@0: }, {value: "a b c d"});
michael@0:
michael@0: testCM("selectionChangeReducesRedo", function(cm) {
michael@0: cm.replaceSelection("X");
michael@0: cm.execCommand("goCharRight");
michael@0: cm.undoSelection();
michael@0: cm.execCommand("selectAll");
michael@0: cm.undoSelection();
michael@0: eq(cm.getValue(), "Xabc");
michael@0: eqPos(cm.getCursor(), Pos(0, 1));
michael@0: cm.undoSelection();
michael@0: eq(cm.getValue(), "abc");
michael@0: }, {value: "abc"});
michael@0:
michael@0: testCM("selectionHistoryNonOverlapping", function(cm) {
michael@0: cm.setSelection(Pos(0, 0), Pos(0, 1));
michael@0: cm.setSelection(Pos(0, 2), Pos(0, 3));
michael@0: cm.execCommand("undoSelection");
michael@0: eqPos(cm.getCursor("anchor"), Pos(0, 0));
michael@0: eqPos(cm.getCursor("head"), Pos(0, 1));
michael@0: }, {value: "1234"});
michael@0:
michael@0: testCM("cursorMotionSplitsHistory", function(cm) {
michael@0: cm.replaceSelection("a");
michael@0: cm.execCommand("goCharRight");
michael@0: cm.replaceSelection("b");
michael@0: cm.replaceSelection("c");
michael@0: cm.undo();
michael@0: eq(cm.getValue(), "a1234");
michael@0: eqPos(cm.getCursor(), Pos(0, 2));
michael@0: cm.undo();
michael@0: eq(cm.getValue(), "1234");
michael@0: eqPos(cm.getCursor(), Pos(0, 0));
michael@0: }, {value: "1234"});
michael@0:
michael@0: testCM("selChangeInOperationDoesNotSplit", function(cm) {
michael@0: for (var i = 0; i < 4; i++) {
michael@0: cm.operation(function() {
michael@0: cm.replaceSelection("x");
michael@0: cm.setCursor(Pos(0, cm.getCursor().ch - 1));
michael@0: });
michael@0: }
michael@0: eqPos(cm.getCursor(), Pos(0, 0));
michael@0: eq(cm.getValue(), "xxxxa");
michael@0: cm.undo();
michael@0: eq(cm.getValue(), "a");
michael@0: }, {value: "a"});
michael@0:
michael@0: testCM("alwaysMergeSelEventWithChangeOrigin", function(cm) {
michael@0: cm.replaceSelection("U", null, "foo");
michael@0: cm.setSelection(Pos(0, 0), Pos(0, 1), {origin: "foo"});
michael@0: cm.undoSelection();
michael@0: eq(cm.getValue(), "a");
michael@0: cm.replaceSelection("V", null, "foo");
michael@0: cm.setSelection(Pos(0, 0), Pos(0, 1), {origin: "bar"});
michael@0: cm.undoSelection();
michael@0: eq(cm.getValue(), "Va");
michael@0: }, {value: "a"});