Skip to content

Commit

Permalink
changes needed for xmldom update
Browse files Browse the repository at this point in the history
  • Loading branch information
deepakprabhakara committed Sep 3, 2024
1 parent 10ed036 commit 4b31859
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DOMParser } from '@xmldom/xmldom';
import { DOMParser, MIME_TYPE } from '@xmldom/xmldom';
import crypto from 'crypto';

const countRootNodes = (xmlDoc: Document) => {
Expand All @@ -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) => {

Check failure on line 14 in lib/utils.ts

View workflow job for this annotation

GitHub Actions / build (20)

'context' is defined but never used
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.');
}

Expand Down

0 comments on commit 4b31859

Please sign in to comment.