diff --git a/__test__/mocks/mathmlStrings.ts b/__test__/mocks/mathmlStrings.ts index b97a5ea..b3b97db 100755 --- a/__test__/mocks/mathmlStrings.ts +++ b/__test__/mocks/mathmlStrings.ts @@ -26,10 +26,20 @@ export const mfencedWithSeparatorAttribute = ` `; -export const mfencedWithBrokenAttribute = ` +export const mfencedWithBrokenAttributeCase1 = ` - + + 3 + + + +`; + +export const mfencedWithBrokenAttributeCase2 = ` + + + 3 diff --git a/src/infra/usecases/xmldom-to-mathml-elements/error-handler.ts b/src/infra/usecases/xmldom-to-mathml-elements/error-handler.ts index 2b90414..cba95d4 100644 --- a/src/infra/usecases/xmldom-to-mathml-elements/error-handler.ts +++ b/src/infra/usecases/xmldom-to-mathml-elements/error-handler.ts @@ -9,16 +9,33 @@ export class ErrorHandler { return this._fixMissingAttribute(errorMessage, xml); } - private _fixMissingAttribute(errorMessage: string, xml: string): string { - const missingAttribute = errorMessage.split('"')[1]; - return xml.replace(this._matchAttribute(missingAttribute), `${missingAttribute}='null'`); + isThereAnyErrors(): boolean { + return this._errors.length > 0; } - private _matchAttribute(attribute: string): RegExp { - return new RegExp(`(?<=\<.*)(${attribute}=(?!(\"|\')))|(${attribute}(?!(\"|\')))(?=.*\>)`, 'g'); + cleanErrors(): void { + this._errors = []; + } + + private _fixMissingAttribute(errorMessage: string, xml: string): string { + const missingAttribute = errorMessage.split('"')[1]; + if (missingAttribute) return xml.replace(this._matchMissingValueForAttribute(missingAttribute), ''); + + return xml.replace(this._mathGenericMissingValue(), ''); + } + + private _matchMissingValueForAttribute(attribute: string): RegExp { + return new RegExp(`(?<=\<.*)(${attribute}=(?!(\"|\')))|(${attribute}(?!(\"|\')))(?=.*\>)`, 'gm'); + } + + private _mathGenericMissingValue(): RegExp { + return /(?<=\<.*)(\w+=(?!(\"|\')))/gm; } private _isMissingAttributeValueError(errorMessage: string): boolean { - return !!errorMessage.includes('attribute') && !!errorMessage.includes('missed'); + return ( + (!!errorMessage.includes('attribute') && !!errorMessage.includes('missed')) || + errorMessage.includes('attribute value missed') + ); } } diff --git a/src/infra/usecases/xmldom-to-mathml-elements/xmldom-to-mathml-element-adapter.ts b/src/infra/usecases/xmldom-to-mathml-elements/xmldom-to-mathml-element-adapter.ts index ecfea2e..872bdb7 100644 --- a/src/infra/usecases/xmldom-to-mathml-elements/xmldom-to-mathml-element-adapter.ts +++ b/src/infra/usecases/xmldom-to-mathml-elements/xmldom-to-mathml-element-adapter.ts @@ -23,6 +23,7 @@ export class XmlToMathMLAdapter { convert(xml: string): MathMLElement[] { this._xml = this._removeLineBreaks(xml); + return this._elementsConvertor.convert(this._mathMLElements); } @@ -36,7 +37,12 @@ export class XmlToMathMLAdapter { } private get _mathMLElements(): Element[] { - const elements = this._xmlDOM.parseFromString(this._xml).getElementsByTagName('math'); + let elements = this._xmlDOM.parseFromString(this._xml).getElementsByTagName('math'); + if (this._errorHandler.isThereAnyErrors()) { + this._errorHandler.cleanErrors(); + elements = this._xmlDOM.parseFromString(this._xml).getElementsByTagName('math'); + } + return Array.from(elements) as Element[]; } }