Merge pull request #673 from CarnegieLearningWeb/fix-binaryOpInStyleBlock
Fix binary op in style block
diff --git a/src/commands/math/basicSymbols.js b/src/commands/math/basicSymbols.js
index a8578ae..8c80b32 100644
--- a/src/commands/math/basicSymbols.js
+++ b/src/commands/math/basicSymbols.js
@@ -421,12 +421,28 @@
_.init = VanillaSymbol.prototype.init;
_.contactWeld = _.siblingCreated = _.siblingDeleted = function(opts, dir) {
+ function determineOpClassType(node) {
+ if (node[L]) {
+ // If the left sibling is a binary operator or a separator (comma, semicolon, colon)
+ // or an open bracket (open parenthesis, open square bracket)
+ // consider the operator to be unary
+ if (node[L] instanceof BinaryOperator || /^[,;:\(\[]$/.test(node[L].ctrlSeq)) {
+ return '';
+ }
+ } else if (node.parent && node.parent.parent && node.parent.parent.isStyleBlock()) {
+ //if we are in a style block at the leftmost edge, determine unary/binary based on
+ //the style block
+ //this allows style blocks to be transparent for unary/binary purposes
+ return determineOpClassType(node.parent.parent);
+ } else {
+ return '';
+ }
+
+ return 'mq-binary-operator';
+ };
+
if (dir === R) return; // ignore if sibling only changed on the right
- // If the left sibling is a binary operator or a separator (comma, semicolon, colon)
- // or an open bracket (open parenthesis, open square bracket)
- // consider the operator to be unary, otherwise binary
- this.jQ[0].className =
- (!this[L] || this[L] instanceof BinaryOperator || /^[,;:\(\[]$/.test(this[L].ctrlSeq) ? '' : 'mq-binary-operator');
+ this.jQ[0].className = determineOpClassType(this);
return this;
};
});
diff --git a/src/commands/math/commands.js b/src/commands/math/commands.js
index 51e712a..fdd3cf2 100644
--- a/src/commands/math/commands.js
+++ b/src/commands/math/commands.js
@@ -114,6 +114,9 @@
})
;
};
+ _.isStyleBlock = function() {
+ return true;
+ };
});
// Very similar to the \textcolor command, but will add the given CSS class.
@@ -133,6 +136,9 @@
})
;
};
+ _.isStyleBlock = function() {
+ return true;
+ };
});
var SupSub = P(MathCommand, function(_, super_) {
diff --git a/src/tree.js b/src/tree.js
index 649c517..335dafa 100644
--- a/src/tree.js
+++ b/src/tree.js
@@ -156,6 +156,10 @@
_.isEmpty = function() {
return this.ends[L] === 0 && this.ends[R] === 0;
};
+
+ _.isStyleBlock = function() {
+ return false;
+ };
_.children = function() {
return Fragment(this.ends[L], this.ends[R]);
diff --git a/test/unit/css.test.js b/test/unit/css.test.js
index 639cdf9..56e12d2 100644
--- a/test/unit/css.test.js
+++ b/test/unit/css.test.js
@@ -82,6 +82,37 @@
$(mq.el()).remove();
});
+ test('proper unary/binary within style block', function () {
+ var mq = MQ.MathField($('<span></span>').appendTo('#mock')[0]);
+ mq.latex('\\class{dummy}{-}2\\class{dummy}{+}4');
+ var spans = $(mq.el()).find('.mq-root-block').find('span');
+ assert.equal(spans.length, 6, 'PlusMinus expression parsed incorrectly');
+
+ function isBinaryOperator(i) { return $(spans[i]).hasClass('mq-binary-operator'); }
+ function assertBinaryOperator(i, s) { assert.ok(isBinaryOperator(i), '"' + s + '" should be binary'); }
+ function assertUnaryOperator(i, s) { assert.ok(!isBinaryOperator(i), '"' + s + '" should be unary'); }
+
+ assertUnaryOperator(1, '\\class{dummy}{-}');
+ assertBinaryOperator(4, '\\class{dummy}{-}2\\class{dummy}{+}');
+
+ mq.latex('\\textcolor{red}{-}2\\textcolor{green}{+}4');
+ spans = $(mq.el()).find('.mq-root-block').find('span');
+ assert.equal(spans.length, 6, 'PlusMinus expression parsed incorrectly');
+
+ assertUnaryOperator(1, '\\textcolor{red}{-}');
+ assertBinaryOperator(4, '\\textcolor{red}{-}2\\textcolor{green}{+}');
+
+ //test recursive depths
+ mq.latex('\\textcolor{red}{\\class{dummy}{-}}2\\textcolor{green}{\\class{dummy}{+}}4');
+ spans = $(mq.el()).find('.mq-root-block').find('span');
+ assert.equal(spans.length, 8, 'PlusMinus expression parsed incorrectly');
+
+ assertUnaryOperator(2, '\\textcolor{red}{\\class{dummy}{-}}');
+ assertBinaryOperator(6, '\\textcolor{red}{\\class{dummy}{-}}2\\textcolor{green}{\\class{dummy}{+}}');
+
+ $(mq.el()).remove();
+ });
+
test('operator name spacing e.g. sin x', function() {
var mq = MathQuill.MathField($('<span></span>').appendTo(mock)[0]);