feat: add missing attribute erro handler

This commit is contained in:
Alexandre Nunes 2020-10-03 13:09:07 -03:00
parent c6a65e22ba
commit 896f28e3fe
3 changed files with 42 additions and 9 deletions

@ -26,10 +26,20 @@ export const mfencedWithSeparatorAttribute = `
</root>
`;
export const mfencedWithBrokenAttribute = `
export const mfencedWithBrokenAttributeCase1 = `
<root>
<math>
<mfenced open='{' close >
<mfenced open='{' close >
<mn>3</mn>
</mfenced>
</math>
</root>
`;
export const mfencedWithBrokenAttributeCase2 = `
<root>
<math>
<mfenced open='{' close= >
<mn>3</mn>
</mfenced>
</math>

@ -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')
);
}
}

@ -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[];
}
}