Skip to content

Commit a959c05

Browse files
authored
fix: removal of content after problem type tags (#479)
* fix: removal of content after problem type tags * fix: readability and error handling
1 parent 6c743f8 commit a959c05

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

src/editors/containers/ProblemEditor/data/OLXParser.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,39 @@ export class OLXParser {
491491
return res;
492492
}
493493

494+
/** hasOLXAfterProblemTypeTag(problemType)
495+
* checkTextAfterProblemTypeTag takes a problemType. The problem type is used to determine
496+
* if there is olx after the answer choices the problem. Simple problems are not expected
497+
* to have olx after the answer choices and returns false. In the event that a problem has
498+
* olx after the answer choices it returns true and will raise an error.
499+
* @param {string} problemType - string of the olx problem type
500+
* @return {bool}
501+
*/
502+
hasOLXAfterProblemTypeTag(problemType) {
503+
let problemTagIndex = this.richTextProblem.length - 1;
504+
let hasExtraOLX = false;
505+
Object.entries(this.richTextProblem).forEach(([i, value]) => {
506+
if (Object.keys(value).includes(problemType)) {
507+
problemTagIndex = i;
508+
}
509+
});
510+
511+
if (problemTagIndex < this.richTextProblem.length - 1) {
512+
const olxAfterProblemType = this.richTextProblem.slice(problemTagIndex + 1);
513+
Object.values(olxAfterProblemType).forEach(value => {
514+
const currentKey = Object.keys(value)[0];
515+
const invalidText = currentKey === '#text' && value[currentKey] !== '\n';
516+
const invalidKey = !nonQuestionKeys.includes(currentKey) && currentKey !== '#text';
517+
if (invalidText) {
518+
hasExtraOLX = true;
519+
} else if (invalidKey) {
520+
hasExtraOLX = true;
521+
}
522+
});
523+
}
524+
return hasExtraOLX;
525+
}
526+
494527
replaceOlxDescriptionTag(questionString) {
495528
return questionString.replace(/<description>/gm, '<em class="olx_description">').replace(/<\/description>/gm, '</em>');
496529
}
@@ -634,14 +667,18 @@ export class OLXParser {
634667
throw new Error('Misc Attributes asscoiated with problem, opening in advanced editor');
635668
}
636669

670+
const problemType = this.getProblemType();
671+
672+
if (this.hasOLXAfterProblemTypeTag(problemType)) {
673+
throw new Error(`OLX was found after the ${problemType} tags, opening in advanced editor`);
674+
}
675+
637676
let answersObject = {};
638677
let additionalAttributes = {};
639678
let groupFeedbackList = [];
640-
const problemType = this.getProblemType();
641679
const hints = this.getHints();
642680
const question = this.parseQuestions(problemType);
643681
const solutionExplanation = this.getSolutionExplanation(problemType);
644-
645682
switch (problemType) {
646683
case ProblemTypeKeys.DROPDOWN:
647684
answersObject = this.parseMultipleChoiceAnswers(ProblemTypeKeys.DROPDOWN, 'optioninput', 'option');

src/editors/containers/ProblemEditor/data/OLXParser.test.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
solutionExplanationWithoutDivTest,
2828
tablesInRichTextTest,
2929
parseOutExplanationTests,
30+
unexpectOlxAfterProblemTypeTags,
3031
} from './mockData/olxTestData';
3132
import { ProblemTypeKeys } from '../../../data/constants/problem';
3233

@@ -87,7 +88,7 @@ describe('OLXParser', () => {
8788
}
8889
});
8990
});
90-
describe('when multi select problem finds partial_credit attribute', () => {
91+
describe('when numerical problem finds partial_credit attribute', () => {
9192
it('should throw error and contain message regarding opening advanced editor', () => {
9293
try {
9394
numericalProblemPartialCreditParser.getParsedOLXData();
@@ -97,7 +98,7 @@ describe('OLXParser', () => {
9798
}
9899
});
99100
});
100-
describe('when multi select problem finds partial_credit attribute', () => {
101+
describe('when single select problem finds partial_credit attribute', () => {
101102
it('should throw error and contain message regarding opening advanced editor', () => {
102103
try {
103104
singleSelectPartialCreditParser.getParsedOLXData();
@@ -107,6 +108,17 @@ describe('OLXParser', () => {
107108
}
108109
});
109110
});
111+
describe('when signle select problem has unexpected olx after multiplechoiceresponse tag', () => {
112+
it('should throw error and contain message regarding opening advanced editor', () => {
113+
const unexpectOlxAfterProblemTypeTagsParser = new OLXParser(unexpectOlxAfterProblemTypeTags.rawOLX);
114+
try {
115+
unexpectOlxAfterProblemTypeTagsParser.getParsedOLXData();
116+
} catch (e) {
117+
expect(e).toBeInstanceOf(Error);
118+
expect(e.message).toBe('OLX found after the multiplechoiceresponse tags, opening in advanced editor');
119+
}
120+
});
121+
});
110122
});
111123
describe('getProblemType()', () => {
112124
describe('given a blank problem', () => {

src/editors/containers/ProblemEditor/data/mockData/olxTestData.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,3 +1154,21 @@ export const numericalProblemPartialCredit = {
11541154
</numericalresponse>
11551155
</problem>`
11561156
}
1157+
1158+
export const unexpectOlxAfterProblemTypeTags = {
1159+
rawOLX: `<problem>
1160+
<multiplechoiceresponse>
1161+
<label>What Apple device competed with the portable CD player?</label>
1162+
<choicegroup type="MultipleChoice">
1163+
<choice correct="false">The iPad</choice>
1164+
<choice correct="false">Napster</choice>
1165+
<choice correct="true">The iPod</choice>
1166+
</choicegroup>
1167+
</multiplechoiceresponse>
1168+
<a href="#">Check out Apple's history for more information.</a>
1169+
<demandhint>
1170+
<hint><p>You can add an optional hint like this. Problems that have a hint include a hint button, and this text appears the first time learners select the button.</p></hint>
1171+
<hint><p>If you add more than one hint, a different hint appears each time learners select the hint button.</p></hint>
1172+
</demandhint>
1173+
</problem>`
1174+
}

0 commit comments

Comments
 (0)