fix: add check for question length for parser (#226)

This commit is contained in:
Kristin Aoki
2023-02-01 15:00:03 -05:00
committed by GitHub
parent 2f1072812d
commit 74b2b76beb
2 changed files with 34 additions and 24 deletions

View File

@@ -12,30 +12,32 @@ export const state = StrictDict({
export const parseContentForLabels = ({ editor, updateQuestion }) => {
let content = editor.getContent();
const parsedLabels = content.split(/<label>|<\/label>/gm);
let updatedContent;
parsedLabels.forEach((label, i) => {
let updatedLabel = label;
if (!label.startsWith('<') && !label.endsWith('>')) {
let previousLabel = parsedLabels[i - 1];
let nextLabel = parsedLabels[i + 1];
if (!previousLabel.endsWith('<p>')) {
previousLabel = `${previousLabel}</p><p>`;
updatedContent = content.replace(parsedLabels[i - 1], previousLabel);
content = updatedContent;
if (content && content?.length > 0) {
const parsedLabels = content.split(/<label>|<\/label>/gm);
let updatedContent;
parsedLabels.forEach((label, i) => {
let updatedLabel = label;
if (!label.startsWith('<') && !label.endsWith('>')) {
let previousLabel = parsedLabels[i - 1];
let nextLabel = parsedLabels[i + 1];
if (!previousLabel.endsWith('<p>')) {
previousLabel = `${previousLabel}</p><p>`;
updatedContent = content.replace(parsedLabels[i - 1], previousLabel);
content = updatedContent;
}
if (previousLabel.endsWith('</p>') && !label.startWith('<p>')) {
updatedLabel = `<p>${label}`;
updatedContent = content.replace(label, updatedLabel);
content = updatedContent;
}
if (!nextLabel.startsWith('</p>')) {
nextLabel = `</p><p>${nextLabel}`;
updatedContent = content.replace(parsedLabels[i + 1], nextLabel);
content = updatedContent;
}
}
if (previousLabel.endsWith('</p>') && !label.startWith('<p>')) {
updatedLabel = `<p>${label}`;
updatedContent = content.replace(label, updatedLabel);
content = updatedContent;
}
if (!nextLabel.startsWith('</p>')) {
nextLabel = `</p><p>${nextLabel}`;
updatedContent = content.replace(parsedLabels[i + 1], nextLabel);
content = updatedContent;
}
}
});
});
}
updateQuestion(content);
};

View File

@@ -58,7 +58,7 @@ describe('Problem editor hooks', () => {
});
describe('parseContentForLabels', () => {
test('it calls getContent and setContent', () => {
test('it calls getContent and updateQuestion for some content', () => {
const editor = { getContent: jest.fn(() => '<p><label>Some question label</label></p><p>some content <label>around a label</label> followed by more text</p><img src="/static/soMEImagEURl1.jpeg"/>') };
const updateQuestion = jest.fn();
const content = '<p><label>Some question label</label></p><p>some content </p><p><label>around a label</label></p><p> followed by more text</p><img src="/static/soMEImagEURl1.jpeg"/>';
@@ -66,6 +66,14 @@ describe('Problem editor hooks', () => {
expect(editor.getContent).toHaveBeenCalled();
expect(updateQuestion).toHaveBeenCalledWith(content);
});
test('it calls getContent and updateQuestion for empty content', () => {
const editor = { getContent: jest.fn(() => '') };
const updateQuestion = jest.fn();
const content = '';
module.parseContentForLabels({ editor, updateQuestion });
expect(editor.getContent).toHaveBeenCalled();
expect(updateQuestion).toHaveBeenCalledWith(content);
});
});
describe('problemEditorConfig', () => {