-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathquill.js
3876 lines (3320 loc) · 118 KB
/
mathquill.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyleft 2010-2011 Jay and Han ([email protected])
* under the GNU Lesser General Public License
* http://www.gnu.org/licenses/lgpl.html
* Project Website: http://mathquill.com
*/
(function() {
var jQuery = window.jQuery,
undefined,
_, //temp variable of prototypes
mqCmdId = 'mathquill-command-id',
mqBlockId = 'mathquill-block-id',
min = Math.min,
max = Math.max;
var __slice = [].slice;
function noop() {}
/**
* sugar to make defining lots of commands easier.
* TODO: rethink this.
*/
function bind(cons /*, args... */) {
var args = __slice.call(arguments, 1);
return function() {
return cons.apply(this, args);
};
}
/**
* a development-only debug method. This definition and all
* calls to `pray` will be stripped from the minified
* build of mathquill.
*
* This function must be called by name to be removed
* at compile time. Do not define another function
* with the same name, and only call this function by
* name.
*/
function pray(message, cond) {
if (!cond) throw new Error('prayer failed: '+message);
}
var P = (function(prototype, ownProperty, undefined) {
// helper functions that also help minification
function isObject(o) { return typeof o === 'object'; }
function isFunction(f) { return typeof f === 'function'; }
// a function that gets reused to make uninitialized objects
function BareConstructor() {}
function P(_superclass /* = Object */, definition) {
// handle the case where no superclass is given
if (definition === undefined) {
definition = _superclass;
_superclass = Object;
}
// C is the class to be returned.
//
// It delegates to instantiating an instance of `Bare`, so that it
// will always return a new instance regardless of the calling
// context.
//
// TODO: the Chrome inspector shows all created objects as `C`
// rather than `Object`. Setting the .name property seems to
// have no effect. Is there a way to override this behavior?
function C() {
var self = new Bare;
if (isFunction(self.init)) self.init.apply(self, arguments);
return self;
}
// C.Bare is a class with a noop constructor. Its prototype is the
// same as C, so that instances of C.Bare are also instances of C.
// New objects can be allocated without initialization by calling
// `new MyClass.Bare`.
function Bare() {}
C.Bare = Bare;
// Set up the prototype of the new class.
var _super = BareConstructor[prototype] = _superclass[prototype];
var proto = Bare[prototype] = C[prototype] = new BareConstructor;
// other variables, as a minifier optimization
var extensions;
// set the constructor property on the prototype, for convenience
proto.constructor = C;
C.mixin = function(def) {
Bare[prototype] = C[prototype] = P(C, def)[prototype];
return C;
}
return (C.open = function(def) {
extensions = {};
if (isFunction(def)) {
// call the defining function with all the arguments you need
// extensions captures the return value.
extensions = def.call(C, proto, _super, C, _superclass);
}
else if (isObject(def)) {
// if you passed an object instead, we'll take it
extensions = def;
}
// ...and extend it
if (isObject(extensions)) {
for (var ext in extensions) {
if (ownProperty.call(extensions, ext)) {
proto[ext] = extensions[ext];
}
}
}
// if there's no init, we assume we're inheriting a non-pjs class, so
// we default to applying the superclass's constructor.
if (!isFunction(proto.init)) {
proto.init = _superclass;
}
return C;
})(definition);
}
// ship it
return P;
// as a minifier optimization, we've closured in a few helper functions
// and the string 'prototype' (C[p] is much shorter than C.prototype)
})('prototype', ({}).hasOwnProperty);
/*************************************************
* Textarea Manager
*
* An abstraction layer wrapping the textarea in
* an object with methods to manipulate and listen
* to events on, that hides all the nasty cross-
* browser incompatibilities behind a uniform API.
*
* Design goal: This is a *HARD* internal
* abstraction barrier. Cross-browser
* inconsistencies are not allowed to leak through
* and be dealt with by event handlers. All future
* cross-browser issues that arise must be dealt
* with here, and if necessary, the API updated.
*
* Organization:
* - key values map and stringify()
* - manageTextarea()
* + defer() and flush()
* + event handler logic
* + attach event handlers and export methods
************************************************/
var manageTextarea = (function() {
// The following [key values][1] map was compiled from the
// [DOM3 Events appendix section on key codes][2] and
// [a widely cited report on cross-browser tests of key codes][3],
// except for 10: 'Enter', which I've empirically observed in Safari on iOS
// and doesn't appear to conflict with any other known key codes.
//
// [1]: http://www.w3.org/TR/2012/WD-DOM-Level-3-Events-20120614/#keys-keyvalues
// [2]: http://www.w3.org/TR/2012/WD-DOM-Level-3-Events-20120614/#fixed-virtual-key-codes
// [3]: http://unixpapa.com/js/key.html
var KEY_VALUES = {
8: 'Backspace',
9: 'Tab',
10: 'Enter', // for Safari on iOS
13: 'Enter',
16: 'Shift',
17: 'Control',
18: 'Alt',
20: 'CapsLock',
27: 'Esc',
32: 'Spacebar',
33: 'PageUp',
34: 'PageDown',
35: 'End',
36: 'Home',
37: 'Left',
38: 'Up',
39: 'Right',
40: 'Down',
45: 'Insert',
46: 'Del',
144: 'NumLock'
};
// To the extent possible, create a normalized string representation
// of the key combo (i.e., key code and modifier keys).
function stringify(evt) {
var which = evt.which || evt.keyCode;
var keyVal = KEY_VALUES[which];
var key;
var modifiers = [];
if (evt.ctrlKey) modifiers.push('Ctrl');
if (evt.originalEvent && evt.originalEvent.metaKey) modifiers.push('Meta');
if (evt.altKey) modifiers.push('Alt');
if (evt.shiftKey) modifiers.push('Shift');
key = keyVal || String.fromCharCode(which);
if (!modifiers.length && !keyVal) return key;
modifiers.push(key);
return modifiers.join('-');
}
// create a textarea manager that calls callbacks at useful times
// and exports useful public methods
return function manageTextarea(el, opts) {
var keydown = null;
var keypress = null;
if (!opts) opts = {};
var textCallback = opts.text || noop;
var keyCallback = opts.key || noop;
var pasteCallback = opts.paste || noop;
var onCut = opts.cut || noop;
var textarea = jQuery(el);
var target = jQuery(opts.container || textarea);
// checkTextareaFor() is called after keypress or paste events to
// say "Hey, I think something was just typed" or "pasted" (resp.),
// so that at all subsequent opportune times (next event or timeout),
// will check for expected typed or pasted text.
// Need to check repeatedly because #135: in Safari 5.1 (at least),
// after selecting something and then typing, the textarea is
// incorrectly reported as selected during the input event (but not
// subsequently).
var checkTextarea = noop, timeoutId;
function checkTextareaFor(checker) {
checkTextarea = checker;
clearTimeout(timeoutId);
timeoutId = setTimeout(checker);
}
target.bind('keydown keypress input keyup focusout paste', function() { checkTextarea(); });
// -*- public methods -*- //
function select(text) {
// check textarea at least once/one last time before munging (so
// no race condition if selection happens after keypress/paste but
// before checkTextarea), then never again ('cos it's been munged)
checkTextarea();
checkTextarea = noop;
clearTimeout(timeoutId);
textarea.val(text);
if (text) textarea[0].select();
}
// -*- helper subroutines -*- //
// Determine whether there's a selection in the textarea.
// This will always return false in IE < 9, which don't support
// HTMLTextareaElement::selection{Start,End}.
function hasSelection() {
var dom = textarea[0];
if (!('selectionStart' in dom)) return false;
return dom.selectionStart !== dom.selectionEnd;
}
function popText(callback) {
var text = textarea.val();
textarea.val('');
if (text) callback(text);
}
function handleKey() {
keyCallback(stringify(keydown), keydown);
}
// -*- event handlers -*- //
function onKeydown(e) {
keydown = e;
keypress = null;
handleKey();
}
function onKeypress(e) {
// call the key handler for repeated keypresses.
// This excludes keypresses that happen directly
// after keydown. In that case, there will be
// no previous keypress, so we skip it here
if (keydown && keypress) handleKey();
keypress = e;
checkTextareaFor(typedText);
}
function typedText() {
// If there is a selection, the contents of the textarea couldn't
// possibly have just been typed in.
// This happens in browsers like Firefox and Opera that fire
// keypress for keystrokes that are not text entry and leave the
// selection in the textarea alone, such as Ctrl-C.
// Note: we assume that browsers that don't support hasSelection()
// also never fire keypress on keystrokes that are not text entry.
// This seems reasonably safe because:
// - all modern browsers including IE 9+ support hasSelection(),
// making it extremely unlikely any browser besides IE < 9 won't
// - as far as we know IE < 9 never fires keypress on keystrokes
// that aren't text entry, which is only as reliable as our
// tests are comprehensive, but the IE < 9 way to do
// hasSelection() is poorly documented and is also only as
// reliable as our tests are comprehensive
// If anything like #40 or #71 is reported in IE < 9, see
// b1318e5349160b665003e36d4eedd64101ceacd8
if (hasSelection()) return;
popText(textCallback);
}
function onBlur() { keydown = keypress = null; }
function onPaste(e) {
// browsers are dumb.
//
// In Linux, middle-click pasting causes onPaste to be called,
// when the textarea is not necessarily focused. We focus it
// here to ensure that the pasted text actually ends up in the
// textarea.
//
// It's pretty nifty that by changing focus in this handler,
// we can change the target of the default action. (This works
// on keydown too, FWIW).
//
// And by nifty, we mean dumb (but useful sometimes).
textarea.focus();
checkTextareaFor(pastedText);
}
function pastedText() {
popText(pasteCallback);
}
// -*- attach event handlers -*- //
target.bind({
keydown: onKeydown,
keypress: onKeypress,
focusout: onBlur,
cut: onCut,
paste: onPaste
});
// -*- export public methods -*- //
return {
select: select
};
};
}());
var Parser = P(function(_, _super, Parser) {
// The Parser object is a wrapper for a parser function.
// Externally, you use one to parse a string by calling
// var result = SomeParser.parse('Me Me Me! Parse Me!');
// You should never call the constructor, rather you should
// construct your Parser from the base parsers and the
// parser combinator methods.
function parseError(stream, message) {
if (stream) {
stream = "'"+stream+"'";
}
else {
stream = 'EOF';
}
throw 'Parse Error: '+message+' at '+stream;
}
_.init = function(body) { this._ = body; };
_.parse = function(stream) {
return this.skip(eof)._(stream, success, parseError);
function success(stream, result) { return result; }
};
// -*- primitive combinators -*- //
_.or = function(alternative) {
pray('or is passed a parser', alternative instanceof Parser);
var self = this;
return Parser(function(stream, onSuccess, onFailure) {
return self._(stream, onSuccess, failure);
function failure(newStream) {
return alternative._(stream, onSuccess, onFailure);
}
});
};
_.then = function(next) {
var self = this;
return Parser(function(stream, onSuccess, onFailure) {
return self._(stream, success, onFailure);
function success(newStream, result) {
var nextParser = (next instanceof Parser ? next : next(result));
pray('a parser is returned', nextParser instanceof Parser);
return nextParser._(newStream, onSuccess, onFailure);
}
});
};
// -*- optimized iterative combinators -*- //
_.many = function() {
var self = this;
return Parser(function(stream, onSuccess, onFailure) {
var xs = [];
while (self._(stream, success, failure));
return onSuccess(stream, xs);
function success(newStream, x) {
stream = newStream;
xs.push(x);
return true;
}
function failure() {
return false;
}
});
};
_.times = function(min, max) {
if (arguments.length < 2) max = min;
var self = this;
return Parser(function(stream, onSuccess, onFailure) {
var xs = [];
var result = true;
var failure;
for (var i = 0; i < min; i += 1) {
result = self._(stream, success, firstFailure);
if (!result) return onFailure(stream, failure);
}
for (; i < max && result; i += 1) {
result = self._(stream, success, secondFailure);
}
return onSuccess(stream, xs);
function success(newStream, x) {
xs.push(x);
stream = newStream;
return true;
}
function firstFailure(newStream, msg) {
failure = msg;
stream = newStream;
return false;
}
function secondFailure(newStream, msg) {
return false;
}
});
};
// -*- higher-level combinators -*- //
_.result = function(res) { return this.then(succeed(res)); };
_.atMost = function(n) { return this.times(0, n); };
_.atLeast = function(n) {
var self = this;
return self.times(n).then(function(start) {
return self.many().map(function(end) {
return start.concat(end);
});
});
};
_.map = function(fn) {
return this.then(function(result) { return succeed(fn(result)); });
};
_.skip = function(two) {
return this.then(function(result) { return two.result(result); });
};
// -*- primitive parsers -*- //
var string = this.string = function(str) {
var len = str.length;
var expected = "expected '"+str+"'";
return Parser(function(stream, onSuccess, onFailure) {
var head = stream.slice(0, len);
if (head === str) {
return onSuccess(stream.slice(len), head);
}
else {
return onFailure(stream, expected);
}
});
};
var regex = this.regex = function(re) {
pray('regexp parser is anchored', re.toString().charAt(1) === '^');
var expected = 'expected '+re;
return Parser(function(stream, onSuccess, onFailure) {
var match = re.exec(stream);
if (match) {
var result = match[0];
return onSuccess(stream.slice(result.length), result);
}
else {
return onFailure(stream, expected);
}
});
};
var succeed = Parser.succeed = function(result) {
return Parser(function(stream, onSuccess) {
return onSuccess(stream, result);
});
};
var fail = Parser.fail = function(msg) {
return Parser(function(stream, _, onFailure) {
return onFailure(stream, msg);
});
};
var letter = Parser.letter = regex(/^[a-z]/i);
var letters = Parser.letters = regex(/^[a-z]*/i);
var digit = Parser.digit = regex(/^[0-9]/);
var digits = Parser.digits = regex(/^[0-9]*/);
var whitespace = Parser.whitespace = regex(/^\s+/);
var optWhitespace = Parser.optWhitespace = regex(/^\s*/);
var any = Parser.any = Parser(function(stream, onSuccess, onFailure) {
if (!stream) return onFailure(stream, 'expected any character');
return onSuccess(stream.slice(1), stream.charAt(0));
});
var all = Parser.all = Parser(function(stream, onSuccess, onFailure) {
return onSuccess('', stream);
});
var eof = Parser.eof = Parser(function(stream, onSuccess, onFailure) {
if (stream) return onFailure(stream, 'expected EOF');
return onSuccess(stream, stream);
});
});
/*************************************************
* Base classes of the MathQuill virtual DOM tree
*
* Only doing tree node manipulation via these
* adopt/ disown methods guarantees well-formedness
* of the tree.
************************************************/
// L = 'left'
// R = 'right'
//
// the contract is that they can be used as object properties
// and (-L) === R, and (-R) === L.
var L = -1;
var R = 1;
function prayDirection(dir) {
pray('a direction was passed', dir === L || dir === R);
}
/**
* Tiny extension of jQuery adding directionalized DOM manipulation methods.
*
* Funny how Pjs v3 almost just works with `jQuery.fn.init`.
*
* jQuery features that don't work on $:
* - jQuery.*, like jQuery.ajax, obviously (Pjs doesn't and shouldn't
* copy constructor properties)
*
* - jQuery(function), the shortcut for `jQuery(document).ready(function)`,
* because `jQuery.fn.init` is idiosyncratic and Pjs doing, essentially,
* `jQuery.fn.init.apply(this, arguments)` isn't quite right, you need:
*
* _.init = function(s, c) { jQuery.fn.init.call(this, s, c, $(document)); };
*
* if you actually give a shit (really, don't bother),
* see https://github.com/jquery/jquery/blob/1.7.2/src/core.js#L889
*
* - jQuery(selector), because jQuery translates that to
* `jQuery(document).find(selector)`, but Pjs doesn't (should it?) let
* you override the result of a constructor call
* + note that because of the jQuery(document) shortcut-ness, there's also
* the 3rd-argument-needs-to-be-`$(document)` thing above, but the fix
* for that (as can be seen above) is really easy. This problem requires
* a way more intrusive fix
*
* And that's it! Everything else just magically works because jQuery internally
* uses `this.constructor()` everywhere (hence calling `$`), but never ever does
* `this.constructor.find` or anything like that, always doing `jQuery.find`.
*/
var $ = P(jQuery, function(_) {
_.insDirOf = function(dir, el) {
return dir === L ?
this.insertBefore(el.first()) : this.insertAfter(el.last());
};
_.insAtDirEnd = function(dir, el) {
return dir === L ? this.prependTo(el) : this.appendTo(el);
};
});
var Point = P(function(_) {
_.parent = 0;
_[L] = 0;
_[R] = 0;
_.init = function(parent, leftward, rightward) {
this.parent = parent;
this[L] = leftward;
this[R] = rightward;
};
});
/**
* MathQuill virtual-DOM tree-node abstract base class
*/
var Node = P(function(_) {
_[L] = 0;
_[R] = 0
_.parent = 0;
_.init = function() {
this.endChild = {};
this.endChild[L] = 0;
this.endChild[R] = 0;
};
_.children = function() {
return Fragment(this.endChild[L], this.endChild[R]);
};
_.eachChild = function(fn) {
return this.children().each(fn);
};
_.foldChildren = function(fold, fn) {
return this.children().fold(fold, fn);
};
_.adopt = function(parent, leftward, rightward) {
Fragment(this, this).adopt(parent, leftward, rightward);
return this;
};
_.disown = function() {
Fragment(this, this).disown();
return this;
};
});
/**
* An entity outside the virtual tree with one-way pointers (so it's only a
* "view" of part of the tree, not an actual node/entity in the tree) that
* delimits a doubly-linked list of sibling nodes.
* It's like a fanfic love-child between HTML DOM DocumentFragment and the Range
* classes: like DocumentFragment, its contents must be sibling nodes
* (unlike Range, whose contents are arbitrary contiguous pieces of subtrees),
* but like Range, it has only one-way pointers to its contents, its contents
* have no reference to it and in fact may still be in the visible tree (unlike
* DocumentFragment, whose contents must be detached from the visible tree
* and have their 'parent' pointers set to the DocumentFragment).
*/
var Fragment = P(function(_) {
_.init = function(leftEnd, rightEnd) {
pray('no half-empty fragments', !leftEnd === !rightEnd);
this.end = {};
if (!leftEnd) return;
pray('left end node is passed to Fragment', leftEnd instanceof Node);
pray('right end node is passed to Fragment', rightEnd instanceof Node);
pray('leftEnd and rightEnd have the same parent',
leftEnd.parent === rightEnd.parent);
this.end[L] = leftEnd;
this.end[R] = rightEnd;
};
function prayWellFormed(parent, leftward, rightward) {
pray('a parent is always present', parent);
pray('leftward is properly set up', (function() {
// either it's empty and rightward is the left end child (possibly empty)
if (!leftward) return parent.endChild[L] === rightward;
// or it's there and its [R] and .parent are properly set up
return leftward[R] === rightward && leftward.parent === parent;
})());
pray('rightward is properly set up', (function() {
// either it's empty and leftward is the right end child (possibly empty)
if (!rightward) return parent.endChild[R] === leftward;
// or it's there and its [L] and .parent are properly set up
return rightward[L] === leftward && rightward.parent === parent;
})());
}
_.adopt = function(parent, leftward, rightward) {
prayWellFormed(parent, leftward, rightward);
var self = this;
self.disowned = false;
var leftEnd = self.end[L];
if (!leftEnd) return this;
var rightEnd = self.end[R];
if (leftward) {
// NB: this is handled in the ::each() block
// leftward[R] = leftEnd
} else {
parent.endChild[L] = leftEnd;
}
if (rightward) {
rightward[L] = rightEnd;
} else {
parent.endChild[R] = rightEnd;
}
self.end[R][R] = rightward;
self.each(function(el) {
el[L] = leftward;
el.parent = parent;
if (leftward) leftward[R] = el;
leftward = el;
});
return self;
};
_.disown = function() {
var self = this;
var leftEnd = self.end[L];
// guard for empty and already-disowned fragments
if (!leftEnd || self.disowned) return self;
self.disowned = true;
var rightEnd = self.end[R]
var parent = leftEnd.parent;
prayWellFormed(parent, leftEnd[L], leftEnd);
prayWellFormed(parent, rightEnd, rightEnd[R]);
if (leftEnd[L]) {
leftEnd[L][R] = rightEnd[R];
} else {
parent.endChild[L] = rightEnd[R];
}
if (rightEnd[R]) {
rightEnd[R][L] = leftEnd[L];
} else {
parent.endChild[R] = leftEnd[L];
}
return self;
};
_.each = function(fn) {
var self = this;
var el = self.end[L];
if (!el) return self;
for (;el !== self.end[R][R]; el = el[R]) {
if (fn.call(self, el) === false) break;
}
return self;
};
_.fold = function(fold, fn) {
this.each(function(el) {
fold = fn.call(this, fold, el);
});
return fold;
};
});
/*************************************************
* Abstract classes of math blocks and commands.
************************************************/
var uuid = (function() {
var id = 0;
return function() { return id += 1; };
})();
/**
* Math tree node base class.
* Some math-tree-specific extensions to Node.
* Both MathBlock's and MathCommand's descend from it.
*/
var MathElement = P(Node, function(_, _super) {
_.init = function(obj) {
_super.init.call(this);
this.id = uuid();
MathElement[this.id] = this;
};
_.toString = function() {
return '[MathElement '+this.id+']';
};
_.bubble = function(event /*, args... */) {
var args = __slice.call(arguments, 1);
for (var ancestor = this; ancestor; ancestor = ancestor.parent) {
var res = ancestor[event] && ancestor[event].apply(ancestor, args);
if (res === false) break;
}
return this;
};
_.postOrder = function(fn /*, args... */) {
var args = __slice.call(arguments, 1);
if (typeof fn === 'string') {
var methodName = fn;
fn = function(el) {
if (methodName in el) el[methodName].apply(el, args);
};
}
(function recurse(desc) {
desc.eachChild(recurse);
fn(desc);
})(this);
};
_.jQ = $();
_.jQadd = function(jQ) { this.jQ = this.jQ.add(jQ); };
this.jQize = function(html) {
// Sets the .jQ of the entire math subtree rooted at this command.
// Expects .createBlocks() to have been called already, since it
// calls .html().
var jQ = $(html);
jQ.find('*').andSelf().each(function() {
var jQ = $(this),
cmdId = jQ.attr('mathquill-command-id'),
blockId = jQ.attr('mathquill-block-id');
if (cmdId) MathElement[cmdId].jQadd(jQ);
if (blockId) MathElement[blockId].jQadd(jQ);
});
return jQ;
};
_.finalizeInsert = function() {
var self = this;
self.postOrder('finalizeTree');
// note: this order is important.
// empty elements need the empty box provided by blur to
// be present in order for their dimensions to be measured
// correctly in redraw.
self.postOrder('blur');
// adjust context-sensitive spacing
self.postOrder('respace');
if (self[R].respace) self[R].respace();
if (self[L].respace) self[L].respace();
self.postOrder('redraw');
self.bubble('redraw');
};
});
/**
* Commands and operators, like subscripts, exponents, or fractions.
* Descendant commands are organized into blocks.
*/
var MathCommand = P(MathElement, function(_, _super) {
_.init = function(ctrlSeq, htmlTemplate, textTemplate) {
var cmd = this;
_super.init.call(cmd);
if (!cmd.ctrlSeq) cmd.ctrlSeq = ctrlSeq;
if (htmlTemplate) cmd.htmlTemplate = htmlTemplate;
if (textTemplate) cmd.textTemplate = textTemplate;
};
// obvious methods
_.replaces = function(replacedFragment) {
replacedFragment.disown();
this.replacedFragment = replacedFragment;
};
_.isEmpty = function() {
return this.foldChildren(true, function(isEmpty, child) {
return isEmpty && child.isEmpty();
});
};
_.parser = function() {
var block = latexMathParser.block;
var self = this;
return block.times(self.numBlocks()).map(function(blocks) {
self.blocks = blocks;
for (var i = 0; i < blocks.length; i += 1) {
blocks[i].adopt(self, self.endChild[R], 0);
}
return self;
});
};
// createLeftOf(cursor) and the methods it calls
_.createLeftOf = function(cursor) {
var cmd = this;
var replacedFragment = cmd.replacedFragment;
cmd.createBlocks();
MathElement.jQize(cmd.html());
if (replacedFragment) {
replacedFragment.adopt(cmd.endChild[L], 0, 0);
replacedFragment.jQ.appendTo(cmd.endChild[L].jQ);
}
cursor.jQ.before(cmd.jQ);
cursor[L] = cmd.adopt(cursor.parent, cursor[L], cursor[R]);
cmd.finalizeInsert(cursor);
cmd.placeCursor(cursor);
};
_.createBlocks = function() {
var cmd = this,
numBlocks = cmd.numBlocks(),
blocks = cmd.blocks = Array(numBlocks);
for (var i = 0; i < numBlocks; i += 1) {
var newBlock = blocks[i] = MathBlock();
newBlock.adopt(cmd, cmd.endChild[R], 0);
}
};
_.respace = noop; //placeholder for context-sensitive spacing
_.placeCursor = function(cursor) {
//insert the cursor at the right end of the first empty child, searching
//left-to-right, or if none empty, the right end child
cursor.insAtRightEnd(this.foldChildren(this.endChild[L], function(leftward, child) {
return leftward.isEmpty() ? leftward : child;
}));
};
// remove()
_.remove = function() {
this.disown();
this.jQ.remove();
this.postOrder(function(el) { delete MathElement[el.id]; });
return this;
};
// methods involved in creating and cross-linking with HTML DOM nodes