diff --git a/src/tools/xml-formatter/xml-formatter.service.test.ts b/src/tools/xml-formatter/xml-formatter.service.test.ts
index a58a1d7b..2b14558c 100644
--- a/src/tools/xml-formatter/xml-formatter.service.test.ts
+++ b/src/tools/xml-formatter/xml-formatter.service.test.ts
@@ -1,18 +1,27 @@
import { describe, expect, it } from 'vitest';
import { formatXml } from './xml-formatter.service';
-const options = {
- indentation: ' ',
- collapseContent: true,
- lineSeparator: '\n',
-};
-const initString = 'foobar';
-const endString = `
- foo
- bar
-`;
-describe('xml-formatter', () => {
- it('Should generate same string', () => {
- expect(formatXml(initString, options)).toEqual(endString);
+describe('xml-formatter service', () => {
+ describe('formatXml', () => {
+ it('converts XML into a human readable format', () => {
+ const initString = 'foobar';
+
+ expect(formatXml(initString)).toMatchInlineSnapshot(`
+ "
+
+ foo
+
+
+ bar
+
+ "
+ `);
+ });
+
+ it('returns an empty string if the input is not valid XML', () => {
+ const initString = 'hello world';
+
+ expect(formatXml(initString)).toEqual('');
+ });
});
});
diff --git a/src/tools/xml-formatter/xml-formatter.service.ts b/src/tools/xml-formatter/xml-formatter.service.ts
index f1b787f8..3441f0bb 100644
--- a/src/tools/xml-formatter/xml-formatter.service.ts
+++ b/src/tools/xml-formatter/xml-formatter.service.ts
@@ -1,5 +1,28 @@
import xmlFormat, { type XMLFormatterOptions } from 'xml-formatter';
+import { withDefaultOnError } from '@/utils/defaults';
-export function formatXml(value: string, options?: XMLFormatterOptions): string {
- return xmlFormat(value, options) || '';
+export { formatXml, isValidXML };
+
+function cleanRawXml(rawXml: string): string {
+ return rawXml.trim();
+}
+
+function formatXml(rawXml: string, options?: XMLFormatterOptions): string {
+ return withDefaultOnError(() => xmlFormat(cleanRawXml(rawXml), options) ?? '', '');
+}
+
+function isValidXML(rawXml: string): boolean {
+ const cleanedRawXml = cleanRawXml(rawXml);
+
+ if (cleanedRawXml === '') {
+ return true;
+ }
+
+ try {
+ xmlFormat(cleanedRawXml);
+ return true;
+ }
+ catch (e) {
+ return false;
+ }
}
diff --git a/src/tools/xml-formatter/xml-formatter.vue b/src/tools/xml-formatter/xml-formatter.vue
index efe0b798..768656ce 100644
--- a/src/tools/xml-formatter/xml-formatter.vue
+++ b/src/tools/xml-formatter/xml-formatter.vue
@@ -1,41 +1,37 @@
-