Path: blob/master/web-gui/buildyourownbotnet/assets/js/codemirror/test/doc_test.js
1293 views
(function() {1// A minilanguage for instantiating linked CodeMirror instances and Docs2function instantiateSpec(spec, place, opts) {3var names = {}, pos = 0, l = spec.length, editors = [];4while (spec) {5var m = spec.match(/^(\w+)(\*?)(?:='([^\']*)'|<(~?)(\w+)(?:\/(\d+)-(\d+))?)\s*/);6var name = m[1], isDoc = m[2], cur;7if (m[3]) {8cur = isDoc ? CodeMirror.Doc(m[3]) : CodeMirror(place, clone(opts, {value: m[3]}));9} else {10var other = m[5];11if (!names.hasOwnProperty(other)) {12names[other] = editors.length;13editors.push(CodeMirror(place, opts));14}15var doc = editors[names[other]].linkedDoc({16sharedHist: !m[4],17from: m[6] ? Number(m[6]) : null,18to: m[7] ? Number(m[7]) : null19});20cur = isDoc ? doc : CodeMirror(place, clone(opts, {value: doc}));21}22names[name] = editors.length;23editors.push(cur);24spec = spec.slice(m[0].length);25}26return editors;27}2829function clone(obj, props) {30if (!obj) return;31clone.prototype = obj;32var inst = new clone();33if (props) for (var n in props) if (props.hasOwnProperty(n))34inst[n] = props[n];35return inst;36}3738function eqAll(val) {39var end = arguments.length, msg = null;40if (typeof arguments[end-1] == "string")41msg = arguments[--end];42if (i == end) throw new Error("No editors provided to eqAll");43for (var i = 1; i < end; ++i)44eq(arguments[i].getValue(), val, msg)45}4647function testDoc(name, spec, run, opts, expectFail) {48if (!opts) opts = {};4950return test("doc_" + name, function() {51var place = document.getElementById("testground");52var editors = instantiateSpec(spec, place, opts);53var successful = false;5455try {56run.apply(null, editors);57successful = true;58} finally {59if (!successful || verbose) {60place.style.visibility = "visible";61} else {62for (var i = 0; i < editors.length; ++i)63if (editors[i] instanceof CodeMirror)64place.removeChild(editors[i].getWrapperElement());65}66}67}, expectFail);68}6970var ie_lt8 = /MSIE [1-7]\b/.test(navigator.userAgent);7172function testBasic(a, b) {73eqAll("x", a, b);74a.setValue("hey");75eqAll("hey", a, b);76b.setValue("wow");77eqAll("wow", a, b);78a.replaceRange("u\nv\nw", Pos(0, 3));79b.replaceRange("i", Pos(0, 4));80b.replaceRange("j", Pos(2, 1));81eqAll("wowui\nv\nwj", a, b);82}8384testDoc("basic", "A='x' B<A", testBasic);85testDoc("basicSeparate", "A='x' B<~A", testBasic);8687testDoc("sharedHist", "A='ab\ncd\nef' B<A", function(a, b) {88a.replaceRange("x", Pos(0));89b.replaceRange("y", Pos(1));90a.replaceRange("z", Pos(2));91eqAll("abx\ncdy\nefz", a, b);92a.undo();93a.undo();94eqAll("abx\ncd\nef", a, b);95a.redo();96eqAll("abx\ncdy\nef", a, b);97b.redo();98eqAll("abx\ncdy\nefz", a, b);99a.undo(); b.undo(); a.undo(); a.undo();100eqAll("ab\ncd\nef", a, b);101}, null, ie_lt8);102103testDoc("undoIntact", "A='ab\ncd\nef' B<~A", function(a, b) {104a.replaceRange("x", Pos(0));105b.replaceRange("y", Pos(1));106a.replaceRange("z", Pos(2));107a.replaceRange("q", Pos(0));108eqAll("abxq\ncdy\nefz", a, b);109a.undo();110a.undo();111eqAll("abx\ncdy\nef", a, b);112b.undo();113eqAll("abx\ncd\nef", a, b);114a.redo();115eqAll("abx\ncd\nefz", a, b);116a.redo();117eqAll("abxq\ncd\nefz", a, b);118a.undo(); a.undo(); a.undo(); a.undo();119eqAll("ab\ncd\nef", a, b);120b.redo();121eqAll("ab\ncdy\nef", a, b);122});123124testDoc("undoConflict", "A='ab\ncd\nef' B<~A", function(a, b) {125a.replaceRange("x", Pos(0));126a.replaceRange("z", Pos(2));127// This should clear the first undo event in a, but not the second128b.replaceRange("y", Pos(0));129a.undo(); a.undo();130eqAll("abxy\ncd\nef", a, b);131a.replaceRange("u", Pos(2));132a.replaceRange("v", Pos(0));133// This should clear both events in a134b.replaceRange("w", Pos(0));135a.undo(); a.undo();136eqAll("abxyvw\ncd\nefu", a, b);137});138139testDoc("doubleRebase", "A='ab\ncd\nef\ng' B<~A C<B", function(a, b, c) {140c.replaceRange("u", Pos(3));141a.replaceRange("", Pos(0, 0), Pos(1, 0));142c.undo();143eqAll("cd\nef\ng", a, b, c);144});145146testDoc("undoUpdate", "A='ab\ncd\nef' B<~A", function(a, b) {147a.replaceRange("x", Pos(2));148b.replaceRange("u\nv\nw\n", Pos(0, 0));149a.undo();150eqAll("u\nv\nw\nab\ncd\nef", a, b);151a.redo();152eqAll("u\nv\nw\nab\ncd\nefx", a, b);153a.undo();154eqAll("u\nv\nw\nab\ncd\nef", a, b);155b.undo();156a.redo();157eqAll("ab\ncd\nefx", a, b);158a.undo();159eqAll("ab\ncd\nef", a, b);160});161162testDoc("undoKeepRanges", "A='abcdefg' B<A", function(a, b) {163var m = a.markText(Pos(0, 1), Pos(0, 3), {className: "foo"});164b.replaceRange("x", Pos(0, 0));165eqPos(m.find().from, Pos(0, 2));166b.replaceRange("yzzy", Pos(0, 1), Pos(0));167eq(m.find(), null);168b.undo();169eqPos(m.find().from, Pos(0, 2));170b.undo();171eqPos(m.find().from, Pos(0, 1));172});173174testDoc("longChain", "A='uv' B<A C<B D<C", function(a, b, c, d) {175a.replaceSelection("X");176eqAll("Xuv", a, b, c, d);177d.replaceRange("Y", Pos(0));178eqAll("XuvY", a, b, c, d);179});180181testDoc("broadCast", "B<A C<A D<A E<A", function(a, b, c, d, e) {182b.setValue("uu");183eqAll("uu", a, b, c, d, e);184a.replaceRange("v", Pos(0, 1));185eqAll("uvu", a, b, c, d, e);186});187188// A and B share a history, C and D share a separate one189testDoc("islands", "A='x\ny\nz' B<A C<~A D<C", function(a, b, c, d) {190a.replaceRange("u", Pos(0));191d.replaceRange("v", Pos(2));192b.undo();193eqAll("x\ny\nzv", a, b, c, d);194c.undo();195eqAll("x\ny\nz", a, b, c, d);196a.redo();197eqAll("xu\ny\nz", a, b, c, d);198d.redo();199eqAll("xu\ny\nzv", a, b, c, d);200});201202testDoc("unlink", "B<A C<A D<B", function(a, b, c, d) {203a.setValue("hi");204b.unlinkDoc(a);205d.setValue("aye");206eqAll("hi", a, c);207eqAll("aye", b, d);208a.setValue("oo");209eqAll("oo", a, c);210eqAll("aye", b, d);211});212213testDoc("bareDoc", "A*='foo' B*<A C<B", function(a, b, c) {214is(a instanceof CodeMirror.Doc);215is(b instanceof CodeMirror.Doc);216is(c instanceof CodeMirror);217eqAll("foo", a, b, c);218a.replaceRange("hey", Pos(0, 0), Pos(0));219c.replaceRange("!", Pos(0));220eqAll("hey!", a, b, c);221b.unlinkDoc(a);222b.setValue("x");223eqAll("x", b, c);224eqAll("hey!", a);225});226227testDoc("swapDoc", "A='a' B*='b' C<A", function(a, b, c) {228var d = a.swapDoc(b);229d.setValue("x");230eqAll("x", c, d);231eqAll("b", a, b);232});233234testDoc("docKeepsScroll", "A='x' B*='y'", function(a, b) {235addDoc(a, 200, 200);236a.scrollIntoView(Pos(199, 200));237var c = a.swapDoc(b);238a.swapDoc(c);239var pos = a.getScrollInfo();240is(pos.left > 0, "not at left");241is(pos.top > 0, "not at top");242});243244testDoc("copyDoc", "A='u'", function(a) {245var copy = a.getDoc().copy(true);246a.setValue("foo");247copy.setValue("bar");248var old = a.swapDoc(copy);249eq(a.getValue(), "bar");250a.undo();251eq(a.getValue(), "u");252a.swapDoc(old);253eq(a.getValue(), "foo");254eq(old.historySize().undo, 1);255eq(old.copy(false).historySize().undo, 0);256});257258testDoc("docKeepsMode", "A='1+1'", function(a) {259var other = CodeMirror.Doc("hi", "text/x-markdown");260a.setOption("mode", "text/javascript");261var old = a.swapDoc(other);262eq(a.getOption("mode"), "text/x-markdown");263eq(a.getMode().name, "markdown");264a.swapDoc(old);265eq(a.getOption("mode"), "text/javascript");266eq(a.getMode().name, "javascript");267});268269testDoc("subview", "A='1\n2\n3\n4\n5' B<~A/1-3", function(a, b) {270eq(b.getValue(), "2\n3");271eq(b.firstLine(), 1);272b.setCursor(Pos(4));273eqPos(b.getCursor(), Pos(2, 1));274a.replaceRange("-1\n0\n", Pos(0, 0));275eq(b.firstLine(), 3);276eqPos(b.getCursor(), Pos(4, 1));277a.undo();278eqPos(b.getCursor(), Pos(2, 1));279b.replaceRange("oyoy\n", Pos(2, 0));280eq(a.getValue(), "1\n2\noyoy\n3\n4\n5");281b.undo();282eq(a.getValue(), "1\n2\n3\n4\n5");283});284285testDoc("subviewEditOnBoundary", "A='11\n22\n33\n44\n55' B<~A/1-4", function(a, b) {286a.replaceRange("x\nyy\nz", Pos(0, 1), Pos(2, 1));287eq(b.firstLine(), 2);288eq(b.lineCount(), 2);289eq(b.getValue(), "z3\n44");290a.replaceRange("q\nrr\ns", Pos(3, 1), Pos(4, 1));291eq(b.firstLine(), 2);292eq(b.getValue(), "z3\n4q");293eq(a.getValue(), "1x\nyy\nz3\n4q\nrr\ns5");294a.execCommand("selectAll");295a.replaceSelection("!");296eqAll("!", a, b);297});298299300testDoc("sharedMarker", "A='ab\ncd\nef\ngh' B<A C<~A/1-2", function(a, b, c) {301var mark = b.markText(Pos(0, 1), Pos(3, 1),302{className: "cm-searching", shared: true});303var found = a.findMarksAt(Pos(0, 2));304eq(found.length, 1);305eq(found[0], mark);306eq(c.findMarksAt(Pos(1, 1)).length, 1);307eqPos(mark.find().from, Pos(0, 1));308eqPos(mark.find().to, Pos(3, 1));309b.replaceRange("x\ny\n", Pos(0, 0));310eqPos(mark.find().from, Pos(2, 1));311eqPos(mark.find().to, Pos(5, 1));312var cleared = 0;313CodeMirror.on(mark, "clear", function() {++cleared;});314b.operation(function(){mark.clear();});315eq(a.findMarksAt(Pos(3, 1)).length, 0);316eq(b.findMarksAt(Pos(3, 1)).length, 0);317eq(c.findMarksAt(Pos(3, 1)).length, 0);318eq(mark.find(), null);319eq(cleared, 1);320});321322testDoc("undoInSubview", "A='line 0\nline 1\nline 2\nline 3\nline 4' B<A/1-4", function(a, b) {323b.replaceRange("x", Pos(2, 0));324a.undo();325eq(a.getValue(), "line 0\nline 1\nline 2\nline 3\nline 4");326eq(b.getValue(), "line 1\nline 2\nline 3");327});328})();329330331