From 286d2209cb116e986bf6499f0f59e124639d2c8f Mon Sep 17 00:00:00 2001 From: Raymond Zhou <56318341+rayzhou-bit@users.noreply.github.com> Date: Thu, 13 Apr 2023 09:25:43 -0700 Subject: [PATCH] feat: XMLBuilder and XMLParser performing unwanted processing in encoding / parsing (#304) --- .../ProblemEditor/data/OLXParser.js | 20 +++++++ .../ProblemEditor/data/OLXParser.test.js | 15 ++++++ .../data/mockData/olxTestData.js | 54 +++++++++++++++++++ 3 files changed, 89 insertions(+) diff --git a/src/editors/containers/ProblemEditor/data/OLXParser.js b/src/editors/containers/ProblemEditor/data/OLXParser.js index 34fd9346c..3fdd5eb30 100644 --- a/src/editors/containers/ProblemEditor/data/OLXParser.js +++ b/src/editors/containers/ProblemEditor/data/OLXParser.js @@ -67,14 +67,29 @@ export class OLXParser { const questionOptions = { ignoreAttributes: false, alwaysCreateTextNode: true, + numberParseOptions: { + leadingZeros: false, + hex: false, + }, preserveOrder: true, + processEntities: false, }; const parserOptions = { ignoreAttributes: false, alwaysCreateTextNode: true, + numberParseOptions: { + leadingZeros: false, + hex: false, + }, + processEntities: false, }; const builderOptions = { ignoreAttributes: false, + numberParseOptions: { + leadingZeros: false, + hex: false, + }, + processEntities: false, }; // There are two versions of the parsed XLM because the question requires the order of the // parsed data to be preserved. However, all the other widgets need the data grouped by @@ -331,7 +346,12 @@ export class OLXParser { parseQuestions(problemType) { const options = { ignoreAttributes: false, + numberParseOptions: { + leadingZeros: false, + hex: false, + }, preserveOrder: true, + processEntities: false, }; const builder = new XMLBuilder(options); const problemArray = _.get(this.questionData[0], problemType) || this.questionData; diff --git a/src/editors/containers/ProblemEditor/data/OLXParser.test.js b/src/editors/containers/ProblemEditor/data/OLXParser.test.js index 2d1939b85..9a46ae1ae 100644 --- a/src/editors/containers/ProblemEditor/data/OLXParser.test.js +++ b/src/editors/containers/ProblemEditor/data/OLXParser.test.js @@ -18,6 +18,7 @@ import { shuffleProblemOLX, scriptProblemOlX, labelDescriptionQuestionOLX, + encodingTestOLX, } from './mockData/olxTestData'; import { ProblemTypeKeys } from '../../../data/constants/problem'; @@ -231,3 +232,17 @@ describe('OLXParser for problem with solution tag', () => { }); }); }); + +describe('Check OLXParser for proper encoding', () => { + it('should not encode html entities', () => { + const olxparser = new OLXParser(encodingTestOLX.rawOLX); + const problemType = olxparser.getProblemType(); + const question = olxparser.parseQuestions(problemType); + expect(question).toBe(encodingTestOLX.question); + }); + it('should not parse hex numbers and leading zeros', () => { + const olxparser = new OLXParser(encodingTestOLX.rawOLX); + const answer = olxparser.parseMultipleChoiceAnswers('multiplechoiceresponse', 'choicegroup', 'choice'); + expect(answer).toEqual(encodingTestOLX.data); + }); +}); diff --git a/src/editors/containers/ProblemEditor/data/mockData/olxTestData.js b/src/editors/containers/ProblemEditor/data/mockData/olxTestData.js index f5b7fb466..e7b21557f 100644 --- a/src/editors/containers/ProblemEditor/data/mockData/olxTestData.js +++ b/src/editors/containers/ProblemEditor/data/mockData/olxTestData.js @@ -694,3 +694,57 @@ export const labelDescriptionQuestionOLX = { question: '

boiling eggs: water system

Watch out, boiling water is hot', }; + +export const encodingTestOLX = { + rawOLX: + ` + +

What is the content of the register x2 after executing the following three lines of instructions?

+

Address          assembly instructions
0x0              addi x1, x0, 1
0x4              slli x2, x1, 4
0x8              sub x1, x2, x1

+ + 0x10 + 0x0f + 0x07 + 0009 + + +
+

Explanation

+

Address          assembly instructions    comment
0x0              addi x1, x0, 1           x1 = 0x1
0x4              slli x2, x1, 4           x2 = x1 << 4 = 0x10
0x8              sub x1, x2, x1           x1 = x2 - x1 = 0x10 - 0x01 = 0xf

+
+
+
+
`, + data: { + answers: [ + { + id: 'A', + // eslint-disable-next-line + title: `0x10`, + correct: false, + }, + { + id: 'B', + // eslint-disable-next-line + title: `0x0f`, + correct: true, + }, + { + id: 'C', + // eslint-disable-next-line + title: `0x07`, + correct: false, + }, + { + id: 'D', + // eslint-disable-next-line + title: `0009`, + correct: false, + }, + ], + }, + // eslint-disable-next-line + question: `

What is the content of the register x2 after executing the following three lines of instructions?

Address          assembly instructions

0x0              addi x1, x0, 1

0x4              slli x2, x1, 4

0x8              sub x1, x2, x1

`, + // eslint-disable-next-line + solutionExplanation: `

Address          assembly instructions    comment

0x0              addi x1, x0, 1           x1 = 0x1

0x4              slli x2, x1, 4           x2 = x1 << 4 = 0x10

0x8              sub x1, x2, x1           x1 = x2 - x1 = 0x10 - 0x01 = 0xf

`, +};