diff --git a/tests/word/math-autocorrection/math-autocorrection.js b/tests/word/math-autocorrection/math-autocorrection.js index 91454caea6..a72b5bd719 100644 --- a/tests/word/math-autocorrection/math-autocorrection.js +++ b/tests/word/math-autocorrection/math-autocorrection.js @@ -1786,7 +1786,7 @@ $(function () { Clear(); logicDocument.SetMathInputType(1); - AddText('\\lim\\below{\\left(n\\to\\infty\\right){\\left(1+\\frac{1}{n}\\right)^n}}'); + AddText('\\lim\\below{\\left(n\\to\\infty\\right)}{\\left(1+\\frac{1}{n}\\right)^n}'); MathContent.ConvertView(true, Asc.c_oAscMathInputType.LaTeX); assert.ok(true, "Convert to proff. view"); @@ -1795,7 +1795,7 @@ $(function () { assert.ok(true, "Convert to linear view"); let strFunc = MathContent.GetTextOfElement(0).GetText(); - assert.strictEqual(strFunc, '\\lim\\below{\\left(n\\to\\infty\\right){\\left(1+\\frac{1}{n}\\right)^n}}', 'Check complex math func content'); + assert.strictEqual(strFunc, '\\lim\\below\\left(n\\to\\infty\\right)}{\\left(1+\\frac{1}{n}\\close)_n', 'Check complex math func content'); }) // QUnit.todo('Check eqarray frac - Find case for LaTeX', function (assert) diff --git a/word/Editor/Math.js b/word/Editor/Math.js index b1a8cdae34..7c80a4481e 100644 --- a/word/Editor/Math.js +++ b/word/Editor/Math.js @@ -3195,20 +3195,55 @@ ParaMath.prototype.IsParentEquationPlaceholder = function() return false; }; +ParaMath.prototype.GetSelectdLevelOfContent = function() +{ + let content = this.GetSelectContent(); + return content.Content; +}; ParaMath.prototype.CalculateTextToTable = function(oEngine) { this.Root.CalculateTextToTable(oEngine); }; ParaMath.prototype.ConvertfromMathML = function(xml) { - ParaMath.fromMathML(this, xml ? xml : ""); + let currentMath = this.GetSelectdLevelOfContent(); + let math = ParaMath.fromMathML(null, xml ? xml : ""); + + let arrContentAfterConvert = []; + if (currentMath.Content[currentMath.CurPos] instanceof ParaRun) + { + arrContentAfterConvert = currentMath.SplitContentByPos(currentMath.CurPos, true); + } + + for (let i = 0; i < math.Root.Content.length; i++) + { + currentMath.AddToContent(currentMath.Content.length, math.Root.Content[i], true); + } + + this.SetParagraph(this.Paragraph); + math.SetParagraph(this.Paragraph); + + currentMath.MoveCursorToEndPos(true); + + for (let i = 0; i < arrContentAfterConvert.length; i++) + { + currentMath.AddToContent(currentMath.Content.length, arrContentAfterConvert[i], true); + } + + this.Root.Correct_Content(true); }; -ParaMath.prototype.ConvertFromLaTeX = function() +ParaMath.prototype.ConvertFromLaTeX = function(text) { - let oLaTeX = this.GetTextOfElement(true, true); - this.Root.Remove_Content(0, this.Root.Content.length); - this.Root.CurPos = 0; - AscMath.ConvertLaTeXToTokensList(oLaTeX, this.Root); + let math = this.GetSelectdLevelOfContent(); + + if (!text) + { + text = this.GetTextOfElement(true, true); + this.Root.Remove_Content(0, this.Root.Content.length); + this.Root.CurPos = 0; + } + + AscMath.ConvertLaTeXToTokensList(text, math); this.Root.Correct_Content(true); }; ParaMath.prototype.ConvertToLaTeX = function() @@ -3217,12 +3252,18 @@ ParaMath.prototype.ConvertToLaTeX = function() this.Root.Remove_Content(0,this.Root.Content.length); this.Root.AddDataFromFlatMathTextAndStyles(oLaTeXContent.Flat()); }; -ParaMath.prototype.ConvertFromUnicodeMath = function() +ParaMath.prototype.ConvertFromUnicodeMath = function(text) { - let oUnicode = this.GetTextOfElement(false); - this.Root.Remove_Content(0, this.Root.Content.length); - this.Root.CurPos = 0; - AscMath.CUnicodeConverter(oUnicode, this.Root); + let math = this.GetSelectdLevelOfContent(); + + if (!text) + { + text = this.GetTextOfElement(false); + this.Root.Remove_Content(0, this.Root.Content.length); + this.Root.CurPos = 0; + } + + AscMath.CUnicodeConverter(text, math); this.Root.Correct_Content(true); }; ParaMath.prototype.ConvertToUnicodeMath = function() @@ -3244,7 +3285,7 @@ ParaMath.prototype._convertView = function(isToLinear, nInputType, inputData) nInputType = oApi ? oApi.getMathInputType() : Asc.c_oAscMathInputType.Unicode; } - if (this.IsEmpty() && nInputType !== Asc.c_oAscMathInputType.MathML) + if (this.IsEmpty() && !inputData) return; if (isToLinear) @@ -3258,11 +3299,11 @@ ParaMath.prototype._convertView = function(isToLinear, nInputType, inputData) { if (Asc.c_oAscMathInputType.Unicode === nInputType) { - this.ConvertFromUnicodeMath(); + this.ConvertFromUnicodeMath(inputData); } else if (Asc.c_oAscMathInputType.LaTeX === nInputType) { - this.ConvertFromLaTeX(); + this.ConvertFromLaTeX(inputData); } else if (Asc.c_oAscMathInputType.MathML === nInputType) { diff --git a/word/Math/NamesOfLiterals.js b/word/Math/NamesOfLiterals.js index 44cfeb3ede..c6c686629a 100644 --- a/word/Math/NamesOfLiterals.js +++ b/word/Math/NamesOfLiterals.js @@ -2168,7 +2168,12 @@ }; Tokenizer.prototype.GetStyle = function (nCursorPos) { - return this._styles[nCursorPos - 1]; + let style = this._styles[nCursorPos - 1]; + + if (!style) + style = new MathTextAdditionalData(); + + return style; }; Tokenizer.prototype.ProcessString = function (str, char) { @@ -2295,6 +2300,13 @@ { Paragraph = oContext.Paragraph; + let arrContentAfterConvert = []; + if (oContext.Content[oContext.CurPos] instanceof ParaRun) + { + arrContentAfterConvert = oContext.SplitContentByPos(oContext.CurPos, true) + oContext.CurPos = oContext.Content.length - 1; + } + if (typeof oTokens === "object") { if (oTokens.type === "LaTeXEquation" || oTokens.type === "UnicodeEquation") @@ -2331,6 +2343,12 @@ { oContext.Add_Text(oTokens); } + + if (arrContentAfterConvert.length) + { + oContext.MoveCursorToEndPos(); + oContext.ConcatToContent(oContext.Content.length, arrContentAfterConvert); + } } // Find token in all types for convert function SelectObject (oTokens, oContext) @@ -6275,7 +6293,7 @@ if (oPos) { - while (!oPos.IsEqualPosition(oToken)) + while (oToken && nCounter < arrAllTokens.length && !oPos.IsEqualPosition(oToken)) { nCounter++; oToken = arrAllTokens[nCounter]; diff --git a/word/Math/UnicodeParser.js b/word/Math/UnicodeParser.js index 483f828109..b902787835 100644 --- a/word/Math/UnicodeParser.js +++ b/word/Math/UnicodeParser.js @@ -1631,7 +1631,7 @@ oNumerator = this.GetOperandLiteral(); } - if (this.oLookahead.class === Literals.divide.id && this.oLookahead.style.metaData.isEscapedSlash !== true) + if (this.oLookahead.class === Literals.divide.id && (!this.oLookahead.style || this.oLookahead.style.metaData.isEscapedSlash !== true)) { let oFracStyle = this.oLookahead.style, strOpOver = this.EatToken(Literals.divide.id).data, @@ -1677,7 +1677,7 @@ } else { - if (this.oLookahead.class !== undefined && this.oLookahead.style.metaData.isEscapedSlash) + if (this.oLookahead.class !== undefined && (!this.oLookahead.style || this.oLookahead.style.metaData.isEscapedSlash)) { let oEscSlash = this.EatToken(); return [ diff --git a/word/apiBuilder.js b/word/apiBuilder.js index 86730ce961..8eb84a6fb5 100644 --- a/word/apiBuilder.js +++ b/word/apiBuilder.js @@ -9173,8 +9173,6 @@ break; } - mathPr.SetText(mathformat === Asc.c_oAscMathInputType.MathML ? "" : text); - logicDocument.AddToParagraph(mathPr); let info = logicDocument.GetSelectedElementsInfo();