From f6dc9af73c17b0234e1f8a3c30552698894e87df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Wang?= Date: Sat, 13 Apr 2019 10:06:31 -0400 Subject: [PATCH] Avoid extra mstyle attribute when setting mathvariant/mathcolor/mathbackground on a single token element. #61 --- TeXZilla.jison | 31 +++++++++++++++++++++++++------ unit-tests.js | 4 ++-- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/TeXZilla.jison b/TeXZilla.jison index 9c80715..0b2fe61 100644 --- a/TeXZilla.jison +++ b/TeXZilla.jison @@ -99,17 +99,36 @@ function newSpace(aWidth) { return newTag("mspace", null, {"width": aWidth + "em"}); } +function isToken(aTree) { + return ["mi", "mn", "mo", "mtext", "ms"].indexOf(aTree.tag) !== -1; +} + +function areTokenAttributes(aAttributes) { + for (var attribute in aAttributes) { + if (["mathcolor", "mathbackground", "mathvariant"].indexOf(attribute) === -1) + return false; + } + return true; +} + /* FIXME: try to restore the operator grouping when compoundTermList does not contain any fences. https://github.com/fred-wang/TeXZilla/issues/9 */ function newMrow(aList, aTag, aAttributes) { - var tag; - if (!aTag) { - if (aList.length == 1) { - /* This list only has one element so we just return it. */ - return aList[0]; + aTag = aTag || "mrow"; + if (aList.length == 1) { + var child = aList[0]; + if (aTag === "mrow") + return child; + if (aTag === "mstyle" && + isToken(child) && areTokenAttributes(aAttributes)) { + child.attributes = {}; + for (var name in aAttributes) { + if (!child.attributes[name]) + child.attributes[name] = aAttributes[name]; + } + return child; } - aTag = "mrow"; } return newTag(aTag, aList, aAttributes); } diff --git a/unit-tests.js b/unit-tests.js index 129c2dd..1588db5 100644 --- a/unit-tests.js +++ b/unit-tests.js @@ -161,7 +161,7 @@ var tests = [ ["\\mathraisebox{2}x, \\mathraisebox{-2}x", 'x,x\\mathraisebox{2}x, \\mathraisebox{-2}x'], ["\\mathraisebox{negativeveryverythinmathspace}x \\mathraisebox{negativeverythinmathspace}x \\mathraisebox{negativemediummathspace}x \\mathraisebox{negativethickmathspace}x \\mathraisebox{negativeverythickmathspace}x \\mathraisebox{negativeveryverythickmathspace}x \\mathraisebox{veryverythinmathspace}x \\mathraisebox{verythinmathspace}x \\mathraisebox{thinmathspace}x \\mathraisebox{mediummathspace}x \\mathraisebox{thickmathspace}x \\mathraisebox{verythickmathspace}x \\mathraisebox{veryverythickmathspac}x", 'xxxxxxxxxxxxx\\mathraisebox{negativeveryverythinmathspace}x \\mathraisebox{negativeverythinmathspace}x \\mathraisebox{negativemediummathspace}x \\mathraisebox{negativethickmathspace}x \\mathraisebox{negativeverythickmathspace}x \\mathraisebox{negativeveryverythickmathspace}x \\mathraisebox{veryverythinmathspace}x \\mathraisebox{verythinmathspace}x \\mathraisebox{thinmathspace}x \\mathraisebox{mediummathspace}x \\mathraisebox{thickmathspace}x \\mathraisebox{verythickmathspace}x \\mathraisebox{veryverythickmathspac}x'], /* mathvariant */ - ["\\mathbb{x} \\mathbf{x} \\mathit{x} \\mathscr{x} \\mathcal{x} \\mathscr{x} \\mathbscr{x} \\mathsf{x} \\mathfrak{x} \\mathit{x} \\mathtt{x} \\mathrm{x}", 'xxxxxxxxxxxx\\mathbb{x} \\mathbf{x} \\mathit{x} \\mathscr{x} \\mathcal{x} \\mathscr{x} \\mathbscr{x} \\mathsf{x} \\mathfrak{x} \\mathit{x} \\mathtt{x} \\mathrm{x}'], + ["\\mathbb{x} \\mathbf{x} \\mathit{x} \\mathscr{x} \\mathcal{x} \\mathscr{x} \\mathbscr{x} \\mathsf{x} \\mathfrak{x} \\mathit{x} \\mathtt{x} \\mathrm{x}", 'xxxxxxxxxxxx\\mathbb{x} \\mathbf{x} \\mathit{x} \\mathscr{x} \\mathcal{x} \\mathscr{x} \\mathbscr{x} \\mathsf{x} \\mathfrak{x} \\mathit{x} \\mathtt{x} \\mathrm{x}'], ["\\mathbb{x+y} \\mathbf{x+y} \\mathit{x+y} \\mathscr{x+y} \\mathcal{x+y} \\mathscr{x+y} \\mathbscr{x+y} \\mathsf{x+y} \\mathfrak{x+y} \\mathit{x+y} \\mathtt{x+y} \\mathrm{x+y}", 'x+yx+yx+yx+yx+yx+yx+yx+yx+yx+yx+yx+y\\mathbb{x+y} \\mathbf{x+y} \\mathit{x+y} \\mathscr{x+y} \\mathcal{x+y} \\mathscr{x+y} \\mathbscr{x+y} \\mathsf{x+y} \\mathfrak{x+y} \\mathit{x+y} \\mathtt{x+y} \\mathrm{x+y}'], /* \href */ ["\\href{http://www.myurl.org}{\\frac a b}", 'ab\\href{http://www.myurl.org}{\\frac a b}'], @@ -229,7 +229,7 @@ var tests = [ \begin{svg} ... \end{svg} and \includegraphics are not supported "&" can not be used as a colsep */ /* colors */ - ["{\\color{aqua} x} {\\color{#ff00ff} y} {\\bgcolor{aqua} x} {\\bgcolor{#ff00ff} y}", 'xyxy{\\color{aqua} x} {\\color{#ff00ff} y} {\\bgcolor{aqua} x} {\\bgcolor{#ff00ff} y}'], + ["{\\color{aqua} x} {\\color{#ff00ff} y} {\\bgcolor{aqua} x} {\\bgcolor{#ff00ff} y}", 'xyxy{\\color{aqua} x} {\\color{#ff00ff} y} {\\bgcolor{aqua} x} {\\bgcolor{#ff00ff} y}'], /**** Various char commands ****/ /* Greek letters */