browser/devtools/sourceeditor/test/cm_doc_test.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial