Merge pull request #666 from laughinghan/refactor.simplify-oppBrack
Simplify Bracket::oppBrack as matchBrack
diff --git a/src/commands/math/commands.js b/src/commands/math/commands.js
index d059765..bdccb61 100644
--- a/src/commands/math/commands.js
+++ b/src/commands/math/commands.js
@@ -563,11 +563,9 @@
_.latex = function() {
return '\\left'+this.sides[L].ctrlSeq+this.ends[L].latex()+'\\right'+this.sides[R].ctrlSeq;
};
- _.oppBrack = function(opts, node, expectedSide) {
- // return node iff it's a 1-sided bracket of expected side (if any, may be
- // undefined), and of opposite side from me if I'm not a pipe
+ _.matchBrack = function(opts, expectedSide, node) {
+ // return node iff it's a matching 1-sided bracket of expected side (if any)
return node instanceof Bracket && node.side && node.side !== -expectedSide
- && (this.sides[this.side].ch === '|' || node.side === -this.side)
&& (!opts.restrictMismatchedBrackets
|| OPP_BRACKS[this.sides[this.side].ch] === node.sides[node.side].ch
|| { '(': ']', '[': ')' }[this.sides[L].ch] === node.sides[R].ch) && node;
@@ -581,11 +579,16 @@
_.createLeftOf = function(cursor) {
if (!this.replacedFragment) { // unless wrapping seln in brackets,
// check if next to or inside an opposing one-sided bracket
- // (must check both sides 'cos I might be a pipe)
var opts = cursor.options;
- var brack = this.oppBrack(opts, cursor[L], L)
- || this.oppBrack(opts, cursor[R], R)
- || this.oppBrack(opts, cursor.parent.parent);
+ if (this.sides[L].ch === '|') { // check both sides if I'm a pipe
+ var brack = this.matchBrack(opts, R, cursor[R])
+ || this.matchBrack(opts, L, cursor[L])
+ || this.matchBrack(opts, 0, cursor.parent.parent);
+ }
+ else {
+ var brack = this.matchBrack(opts, -this.side, cursor[-this.side])
+ || this.matchBrack(opts, -this.side, cursor.parent.parent);
+ }
}
if (brack) {
var side = this.side = -brack.side; // may be pipe with .side not yet set
@@ -627,7 +630,7 @@
var opts = cursor.options, wasSolid = !this.side;
this.side = -side;
// if deleting like, outer close-brace of [(1+2)+3} where inner open-paren
- if (this.oppBrack(opts, this.ends[L].ends[this.side], side)) { // is ghost,
+ if (this.matchBrack(opts, side, this.ends[L].ends[this.side])) { // is ghost,
this.closeOpposing(this.ends[L].ends[this.side]); // then become [1+2)+3
var origEnd = this.ends[L].ends[side];
this.unwrap();
@@ -635,7 +638,7 @@
sib ? cursor.insDirOf(-side, sib) : cursor.insAtDirEnd(side, parent);
}
else { // if deleting like, inner close-brace of ([1+2}+3) where outer
- if (this.oppBrack(opts, this.parent.parent, side)) { // open-paren is
+ if (this.matchBrack(opts, side, this.parent.parent)) { // open-paren is
this.parent.parent.closeOpposing(this); // ghost, then become [1+2+3)
this.parent.parent.unwrap();
} // else if deleting outward from a solid pair, unwrap