From 4b318593c2290d35ce02ef9bfb59106e64b844de Mon Sep 17 00:00:00 2001 From: Deepak Prabhakara Date: Tue, 3 Sep 2024 13:39:26 +0100 Subject: [PATCH] changes needed for xmldom update --- lib/utils.ts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/utils.ts b/lib/utils.ts index c54944ea..ca88e72a 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -1,4 +1,4 @@ -import { DOMParser } from '@xmldom/xmldom'; +import { DOMParser, MIME_TYPE } from '@xmldom/xmldom'; import crypto from 'crypto'; const countRootNodes = (xmlDoc: Document) => { @@ -9,25 +9,24 @@ const countRootNodes = (xmlDoc: Document) => { }; const parseFromString = (xmlString: string) => { - const errors = {}; + const errors: string[] = []; let multiRootErrFound = false; - const errorHandler = (key, msg) => { - if (!errors[key]) errors[key] = []; + const onError = (level, msg, context) => { if (msg.indexOf('Only one element can be added and only after doctype')) { if (!multiRootErrFound) { multiRootErrFound = true; - errors[key].push(msg); + errors.push(msg); } - } else { - errors[key].push(msg); + } else if (level !== 'warn') { + errors.push(msg); } }; - const xml = new DOMParser({ errorHandler }).parseFromString(xmlString); + const xml = new DOMParser({ onError }).parseFromString(xmlString, MIME_TYPE.XML_APPLICATION); if (multiRootErrFound) { throw new Error('multirooted xml not allowed.'); - } else if (Object.keys(errors).length > 0) { + } else if (errors.length > 0) { throw new Error('Invalid XML.'); }