From ee1425d0dd78fb2b230c76b4d3d70bccfcb2ba6a Mon Sep 17 00:00:00 2001 From: Akash K <57758277+amk-dev@users.noreply.github.com> Date: Mon, 20 Nov 2023 15:02:07 +0530 Subject: [PATCH] fix: XML body disappearing with invalid XML (#3567) fix: catch xmlformatter errors --- .../src/composables/codemirror.ts | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/packages/hoppscotch-common/src/composables/codemirror.ts b/packages/hoppscotch-common/src/composables/codemirror.ts index 3c1f06ef4..3babd3686 100644 --- a/packages/hoppscotch-common/src/composables/codemirror.ts +++ b/packages/hoppscotch-common/src/composables/codemirror.ts @@ -44,6 +44,8 @@ import { invokeAction } from "~/helpers/actions" import { useDebounceFn } from "@vueuse/core" // TODO: Migrate from legacy mode +import * as E from "fp-ts/Either" + type ExtendedEditorConfig = { mode: string placeholder: string @@ -160,6 +162,21 @@ const getLanguage = (langMime: string): Language | null => { return null } +const formatXML = (doc: string) => { + try { + const formatted = xmlFormat(doc, { + indentation: " ", + collapseContent: true, + lineSeparator: "\n", + whiteSpaceAtEndOfSelfclosingTag: true, + }) + + return E.right(formatted) + } catch (e) { + return E.left(e) + } +} + /** * Uses xml-formatter to format the XML document * @param doc Document to parse @@ -171,14 +188,11 @@ const parseDoc = ( langMime: string ): string | undefined => { if (langMime === "application/xml" && doc) { - return xmlFormat(doc, { - indentation: " ", - collapseContent: true, - lineSeparator: "\n", - }) - } else { - return doc + const xmlFormatingResult = formatXML(doc) + if (E.isRight(xmlFormatingResult)) return xmlFormatingResult.right } + + return doc } const getEditorLanguage = (