From 70581c54abde2ae4b475bb7b94fdda49c141f32e Mon Sep 17 00:00:00 2001 From: Jesper Hodge <19345795+jesperhodge@users.noreply.github.com> Date: Tue, 2 Jan 2024 15:04:03 -0500 Subject: [PATCH] refactor: abstract XML parser output operations (#445) This is a refactoring for a part of the ReactStateXMLParser. I wanted to use functions that are more generic and not just handle a list of edge cases. So I encapsulated the operation that was done in this part of the code to a function `findNodesAndRemoveTheirParentNodes` which is more generic and could be used for different operations. --- package-lock.json | 4 +- package.json | 1 + .../ProblemEditor/data/ReactStateOLXParser.js | 33 ++-- .../data/reactStateOLXHelpers.js | 116 +++++++++++ .../data/reactStateOLXHelpers.test.js | 182 ++++++++++++++++++ 5 files changed, 317 insertions(+), 19 deletions(-) create mode 100644 src/editors/containers/ProblemEditor/data/reactStateOLXHelpers.js create mode 100644 src/editors/containers/ProblemEditor/data/reactStateOLXHelpers.test.js diff --git a/package-lock.json b/package-lock.json index bc25d5879..eedecadb4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25,6 +25,7 @@ "fast-xml-parser": "^4.0.10", "frontend-components-tinymce-advanced-plugins": "^1.0.2", "lodash-es": "^4.17.21", + "lodash.flatten": "^4.4.0", "moment": "^2.29.4", "moment-shortformat": "^2.1.0", "react-dropzone": "^14.2.3", @@ -17212,8 +17213,7 @@ "node_modules/lodash.flatten": { "version": "4.4.0", "resolved": "https://registry.npmjs.org/lodash.flatten/-/lodash.flatten-4.4.0.tgz", - "integrity": "sha512-C5N2Z3DgnnKr0LOpv/hKCgKdb7ZZwafIrsesve6lmzvZIRZRGaZ/l6Q8+2W7NaT+ZwO3fFlSCzCzrDCFdJfZ4g==", - "dev": true + "integrity": "sha512-C5N2Z3DgnnKr0LOpv/hKCgKdb7ZZwafIrsesve6lmzvZIRZRGaZ/l6Q8+2W7NaT+ZwO3fFlSCzCzrDCFdJfZ4g==" }, "node_modules/lodash.flattendeep": { "version": "4.4.0", diff --git a/package.json b/package.json index 9f563d1ac..c55eeacc9 100644 --- a/package.json +++ b/package.json @@ -79,6 +79,7 @@ "fast-xml-parser": "^4.0.10", "frontend-components-tinymce-advanced-plugins": "^1.0.2", "lodash-es": "^4.17.21", + "lodash.flatten": "^4.4.0", "moment": "^2.29.4", "moment-shortformat": "^2.1.0", "react-dropzone": "^14.2.3", diff --git a/src/editors/containers/ProblemEditor/data/ReactStateOLXParser.js b/src/editors/containers/ProblemEditor/data/ReactStateOLXParser.js index b40971e7c..f320cdd82 100644 --- a/src/editors/containers/ProblemEditor/data/ReactStateOLXParser.js +++ b/src/editors/containers/ProblemEditor/data/ReactStateOLXParser.js @@ -2,6 +2,9 @@ import _ from 'lodash-es'; import { XMLParser, XMLBuilder } from 'fast-xml-parser'; import { ProblemTypeKeys } from '../../../data/constants/problem'; import { ToleranceTypes } from '../components/EditProblemView/SettingsWidget/settingsComponents/Tolerance/constants'; +import { findNodesAndRemoveTheirParentNodes } from './reactStateOLXHelpers'; + +const HtmlBlockTags = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'pre', 'blockquote', 'ol', 'ul', 'li', 'dl', 'dt', 'dd', 'hr', 'table', 'thead', 'caption', 'tbody', 'tr', 'th', 'td', 'colgroup', 'col', 'address', 'fieldset', 'legend']; class ReactStateOLXParser { constructor(problemState) { @@ -193,33 +196,29 @@ class ReactStateOLXParser { /** addQuestion() * The editorObject saved to the class constuctor is parsed for the attribute question. The question is parsed and - * checked for label and em tags. After the question is fully updated, the questionObject is returned. - * TODO: this is very brittle and unreliable. Needs improvement. + * checked for label tags. label tags are extracted from block-type tags like

or

, and the block-type tag is + * deleted while label is kept. For example,

becomes , while + *

Text

remains

Text

. The question is returned as an object representation. * @return {object} object representaion of question */ addQuestion() { const { question } = this.editorObject; - const questionObject = this.richTextParser.parse(question); - /* Removes block tags like

or

that surround the