diff --git a/__test__/index.test.ts b/__test__/index.test.ts index d4b4690..56b8c72 100644 --- a/__test__/index.test.ts +++ b/__test__/index.test.ts @@ -239,4 +239,76 @@ describe('#convert', () => { }); }); }); + + describe('given math string with mfenced with single content and no attr', () => { + test('convert mfenced wrapping it content in dots', () => { + const mathml = ` + + + + 3 + + + + `; + + const result = MathMLToLaTeX.convert(mathml); + + expect(result).toMatch('\\left.3\\right.'); + }); + }); + + describe('given math string with mfenced with single content and open attribute in parenthesis char', () => { + test('convert mfenced wrapping it content between parenthesis and dot', () => { + const mathml = ` + + + + 3 + + + + `; + + const result = MathMLToLaTeX.convert(mathml); + + expect(result).toMatch('\\left(3\\right.'); + }); + }); + + describe('given math string with mfenced with single content and open and closes attributes in parenthesis char', () => { + test('convert mfenced wrapping it content between parenthesis', () => { + const mathml = ` + + + + 3 + + + + `; + + const result = MathMLToLaTeX.convert(mathml); + + expect(result).toMatch('\\left(3\\right)'); + }); + }); + + describe('given math string with mfenced with single content and open attribute in parenthesis char and close attribute without value', () => { + test('convert mfenced wrapping it content between parenthesis', () => { + const mathml = ` + + + + 3 + + + + `; + + const result = MathMLToLaTeX.convert(mathml); + + expect(result).toMatch('\\left(3\\right.'); + }); + }); }); diff --git a/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/Dispatcher.ts b/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/Dispatcher.ts index ea97f7d..015318b 100644 --- a/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/Dispatcher.ts +++ b/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/Dispatcher.ts @@ -1,4 +1,4 @@ -import { Math, MathMLTag, MI, MO, MN, MSqrt, MRow, MSup } from './mathml-tags'; +import { Math, MathMLTag, MI, MO, MN, MSqrt, MRow, MSup, MFenced } from './mathml-tags'; export class Dispatcher { private _name: string; @@ -29,6 +29,8 @@ export class Dispatcher { return new MRow(this._value, this._attributes, this._children); case 'msup': return new MSup(this._value, this._attributes, this._children); + case 'mfenced': + return new MFenced(this._value, this._attributes, this._children); default: return new MathMLTag(this._name, this._value, this._attributes, this._children); } diff --git a/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/mathml-tags/MFenced.ts b/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/mathml-tags/MFenced.ts new file mode 100644 index 0000000..372de1b --- /dev/null +++ b/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/mathml-tags/MFenced.ts @@ -0,0 +1,18 @@ +import { MathMLTag } from './MathMLTag'; +import { GenericWrapper } from '../../../../wrappers'; + +export class MFenced extends MathMLTag { + private _wrapper: GenericWrapper; + + constructor(value: string, attributes: Record, children: MathMLTag[]) { + super('mfenced', value, attributes, children); + + const open: string = this._attributes.open || '.'; + const close: string = this._attributes.close || '.'; + this._wrapper = new GenericWrapper(open, close); + } + + convert(): string { + return this._wrapper.wrap(this._mapChildrenToLaTeX().join(' ')); + } +} diff --git a/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/mathml-tags/index.ts b/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/mathml-tags/index.ts index bfcfa65..140ccc4 100644 --- a/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/mathml-tags/index.ts +++ b/src/converters/mathml-interfaces-to-latex/mathml-tag-to-latex/mathml-tags/index.ts @@ -6,3 +6,4 @@ export { MN } from './MN'; export { MSqrt } from './MSqrt'; export { MRow } from './MRow'; export { MSup } from './MSup'; +export { MFenced } from './MFenced'; diff --git a/src/wrappers/BracketWrapper.ts b/src/wrappers/BracketWrapper.ts index 9b36060..94a65b0 100644 --- a/src/wrappers/BracketWrapper.ts +++ b/src/wrappers/BracketWrapper.ts @@ -1,6 +1,6 @@ import { Wrapper } from './Wrapper'; export class BracketWrapper extends Wrapper { - protected _openChar = '{'; - protected _closeChar = '}'; + protected _open = '{'; + protected _close = '}'; } diff --git a/src/wrappers/GenericWrapper.ts b/src/wrappers/GenericWrapper.ts new file mode 100644 index 0000000..c7eb3fb --- /dev/null +++ b/src/wrappers/GenericWrapper.ts @@ -0,0 +1,12 @@ +import { Wrapper } from './Wrapper'; + +export class GenericWrapper extends Wrapper { + protected _open: string; + protected _close: string; + + constructor(open: string, close: string) { + super(); + this._open = '\\left' + open; + this._close = '\\right' + close; + } +} diff --git a/src/wrappers/ParenthesisWrapper.ts b/src/wrappers/ParenthesisWrapper.ts index 946936b..4b31ed8 100644 --- a/src/wrappers/ParenthesisWrapper.ts +++ b/src/wrappers/ParenthesisWrapper.ts @@ -1,6 +1,6 @@ import { Wrapper } from './Wrapper'; export class ParenthesisWrapper extends Wrapper { - protected _openChar = '\\left('; - protected _closeChar = '\\right)'; + protected _open = '\\left('; + protected _close = '\\right)'; } diff --git a/src/wrappers/Wrapper.ts b/src/wrappers/Wrapper.ts index 22c5a3d..a6ab8c2 100644 --- a/src/wrappers/Wrapper.ts +++ b/src/wrappers/Wrapper.ts @@ -1,9 +1,9 @@ export abstract class Wrapper { - protected abstract _openChar: string; - protected abstract _closeChar: string; + protected abstract _open: string; + protected abstract _close: string; wrap(str: string): string { - return this._openChar + str + this._closeChar; + return this._open + str + this._close; } wrapIfMoreThanOneChar(str: string): string { diff --git a/src/wrappers/index.ts b/src/wrappers/index.ts index dd39981..ae427fb 100644 --- a/src/wrappers/index.ts +++ b/src/wrappers/index.ts @@ -1,2 +1,3 @@ export { BracketWrapper } from './BracketWrapper'; export { ParenthesisWrapper } from './ParenthesisWrapper'; +export { GenericWrapper } from './GenericWrapper';