Fix node value when empty tag attributes is given

This commit is contained in:
Alexandre Nunes 2020-09-02 09:56:02 -03:00
parent fc8a7bf53a
commit 4264de136e
3 changed files with 120 additions and 3 deletions

View File

@ -1,5 +1,11 @@
import { MathMLStringToMathMLInterfaces } from '../../../src/converters/mathml-string-to-mathml-interfaces';
import { singleMi, singleMiNoRoot } from '../../mocks/mathmlStrings';
import {
singleMi,
singleMiNoRoot,
mrow,
mfencedWithSeparatorAttribute,
mfencedWithBrokenAttribute,
} from '../../mocks/mathmlStrings';
describe('#convert', () => {
describe('given math string with mi tag', () => {
@ -21,7 +27,7 @@ describe('#convert', () => {
describe('given math tag outside any other tag', () => {
test('return array with single mathml interface of name math and child mi', () => {
const mathmlString = singleMiNoRoot;
const result = new MathMLStringToMathMLInterfaces(mathmlString).convert();
expect(result.length).toBe(1);
@ -33,4 +39,81 @@ describe('#convert', () => {
});
});
});
describe('given math string with nested tags', () => {
test('return content in a three and keep value on each child', () => {
const mathmlString = mrow;
const result = new MathMLStringToMathMLInterfaces(mathmlString).convert();
expect(result.length).toBe(1);
expect(result[0]).toMatchObject({
attributes: {},
children: [
{
attributes: {},
children: [
{ attributes: {}, children: [], name: 'mn', value: '2' },
{ attributes: {}, children: [], name: 'mo', value: '+' },
{ attributes: {}, children: [], name: 'mn', value: '2' },
],
name: 'mrow',
value: '',
},
],
name: 'math',
value: '',
});
});
});
describe("given math string with mfenced with three contents with separator attribute ';;;'", () => {
test('add attributes to children related with name mfenced', () => {
const mathmlString = mfencedWithSeparatorAttribute;
const result = new MathMLStringToMathMLInterfaces(mathmlString).convert();
expect(result.length).toBe(1);
expect(result[0]).toMatchObject({
attributes: {},
children: [
{
attributes: { separators: ';;;' },
children: [
{ attributes: {}, children: [], name: 'mn', value: '3' },
{ attributes: {}, children: [], name: 'mn', value: '2' },
{ attributes: {}, children: [], name: 'mn', value: '1' },
],
name: 'mfenced',
value: '',
},
],
name: 'math',
value: '',
});
});
});
describe('given math string with mfenced with single content, open attr settled as { and void close tag', () => {
test('add attributes to children related with name mfenced', () => {
const mathmlString = mfencedWithBrokenAttribute;
const result = new MathMLStringToMathMLInterfaces(mathmlString).convert();
expect(result.length).toBe(1);
expect(result[0]).toMatchObject({
attributes: {},
children: [
{
attributes: { open: '{', close: '' },
children: [{ attributes: {}, children: [], name: 'mn', value: '3' }],
name: 'mfenced',
value: '',
},
],
name: 'math',
value: '',
});
});
});
});

View File

@ -1,3 +1,37 @@
export const singleMi = '<root><math><mi>a</mi></math></root>';
export const singleMiNoRoot = '<math><mi>b</mi></math>';
export const mrow = `
<root>
<math>
<mrow>
<mn>2</mn>
<mo>+</mo>
<mn>2</mn>
</mrow>
</math>
</root>
`;
export const mfencedWithSeparatorAttribute = `
<root>
<math>
<mfenced separators=';;;'>
<mn>3</mn>
<mn>2</mn>
<mn>1</mn>
</mfenced>
</math>
</root>
`;
export const mfencedWithBrokenAttribute = `
<root>
<math>
<mfenced open='{' close >
<mn>3</mn>
</mfenced>
</math>
</root>
`;

View File

@ -16,7 +16,7 @@ export class ElementsToMathMLInterfacesConvertor {
private _convertElementAttributes(attributes: NamedNodeMap): Record<string, string> {
return Array.from(attributes).reduce(
(acc, attr: Attr) => Object.assign({ [attr.nodeName]: attr.nodeValue }, acc),
(acc, attr: Attr) => Object.assign({ [attr.nodeName]: attr.nodeValue === attr.nodeName ? '' : attr.nodeValue }, acc),
{},
);
}