diff --git a/externals/tXml.js b/externals/tXml.js index 3a706c2233..db6c43296a 100644 --- a/externals/tXml.js +++ b/externals/tXml.js @@ -52,11 +52,11 @@ export const XML_ENTITIES = { /** * Translates XML predefined entities and character references to their respective characters. - * @param {Object} entitiesList - * @param {String} str + * @param {Object} entitiesList + * @param {String} str * @returns {String} */ -function translateEntitiesAndCharacterReferences(entitiesList, str) { +export function translateEntitiesAndCharacterReferences(entitiesList, str) { const entitySplit = str.split(/(&[#a-zA-Z0-9]+;)/); if (entitySplit.length <= 1) { // No entities. Skip the rest of the function. return str; @@ -64,7 +64,7 @@ function translateEntitiesAndCharacterReferences(entitiesList, str) { for (let i = 1; i < entitySplit.length; i += 2) { const reference = entitySplit[i]; - + /* * Check if it is a character reference of the form * /&#[0-9]+;/ - Encoded in decimal, or @@ -83,7 +83,7 @@ function translateEntitiesAndCharacterReferences(entitiesList, str) { if (!isNaN(code) && code >= 0 && code <= 0x10FFFF) { entitySplit[i] = String.fromCodePoint(code); } - } + } /* * Translate entity references using a dictionary. */ diff --git a/test/unit/externals.tXml.js b/test/unit/externals.tXml.js new file mode 100644 index 0000000000..69d99086db --- /dev/null +++ b/test/unit/externals.tXml.js @@ -0,0 +1,62 @@ +import {expect} from 'chai'; +import {translateEntitiesAndCharacterReferences, XML_ENTITIES} from '../../externals/tXml.js' + +describe('tXml', () => { + + describe('translateEntitiesAndCharacterReferences', () => { + + it('should not change string without XML entities', () => { + expect(translateEntitiesAndCharacterReferences(XML_ENTITIES, 'PT634')).to.be.equal('PT634') + }) + + it('should translate general entity amp', () => { + expect(translateEntitiesAndCharacterReferences(XML_ENTITIES, 'PT634&')).to.be.equal('PT634&') + }) + + it('should translate general entity lt', () => { + expect(translateEntitiesAndCharacterReferences(XML_ENTITIES, 'PT634<')).to.be.equal('PT634<') + }) + + it('should translate general entity gt', () => { + expect(translateEntitiesAndCharacterReferences(XML_ENTITIES, 'PT634>')).to.be.equal('PT634>') + }) + + it('should translate general entity apos', () => { + expect(translateEntitiesAndCharacterReferences(XML_ENTITIES, 'PT634'')).to.be.equal('PT634\'') + }) + + it('should translate general entity quot', () => { + expect(translateEntitiesAndCharacterReferences(XML_ENTITIES, 'PT634"')).to.be.equal('PT634"') + }) + + it('should translate all general entities', () => { + const input = '& > < " ''; + const expectedOutput = '& > < " \''; + expect(translateEntitiesAndCharacterReferences(XML_ENTITIES, input)).to.be.equal(expectedOutput) + }) + + it('should correctly translate decimal character references', () => { + const input = 'A B C'; + const expectedOutput = 'A B C'; + expect(translateEntitiesAndCharacterReferences(XML_ENTITIES, input)).to.be.equal(expectedOutput) + }) + + it('should correctly translate hexadecimal character references', () => { + const input = 'A B C'; + const expectedOutput = 'A B C'; + expect(translateEntitiesAndCharacterReferences(XML_ENTITIES, input)).to.be.equal(expectedOutput) + }) + + it('should correctly translate character references mixed with other text', () => { + const input = 'This is a test: A B C'; + const expectedOutput = 'This is a test: A B C'; + expect(translateEntitiesAndCharacterReferences(XML_ENTITIES, input)).to.be.equal(expectedOutput) + }) + + it('should correctly translate character references mixed with general entities and other text', () => { + const input = 'This is a test: A B C & > < " ''; + const expectedOutput = 'This is a test: A B C & > < " \''; + expect(translateEntitiesAndCharacterReferences(XML_ENTITIES, input)).to.be.equal(expectedOutput) + }) + }) +})