diff --git a/src/pages/tools/json/json-to-xml/service.ts b/src/pages/tools/json/json-to-xml/service.ts index 0d06e28d..f4935998 100644 --- a/src/pages/tools/json/json-to-xml/service.ts +++ b/src/pages/tools/json/json-to-xml/service.ts @@ -1,4 +1,6 @@ import { InitialValuesType } from './types'; +import { escapeMarkup } from '@utils/string'; +import { normalizeXmlTagName } from '@utils/xml'; type JsonObject = Record; @@ -52,7 +54,7 @@ const convertArrayItemToXml = ( )}${indentation}${newline}`; } - return `${indentation}${escapeXml(String(item))}${newline}`; + return `${indentation}${escapeMarkup(String(item))}${newline}`; }; const convertObjectToXml = ( @@ -85,7 +87,7 @@ const convertObjectToXml = ( chunks.push(convertObjectToXml(item, options, depth + 1)); chunks.push(indentation); } else { - chunks.push(escapeXml(String(item))); + chunks.push(escapeMarkup(String(item))); } chunks.push(`${newline}`); @@ -105,7 +107,7 @@ const convertObjectToXml = ( } chunks.push( - `${indentation}<${tagName}>${escapeXml( + `${indentation}<${tagName}>${escapeMarkup( String(value) )}${newline}` ); @@ -124,20 +126,3 @@ const getIndentation = (options: InitialValuesType, depth: number): string => { return ''; } }; - -const escapeXml = (value: string): string => { - return value - .replace(/&/g, '&') - .replace(//g, '>') - .replace(/"/g, '"') - .replace(/'/g, '''); -}; - -const normalizeXmlTagName = (key: string): string => { - const tagName = key !== '' && !isNaN(Number(key)) ? `row-${key}` : key; - - const sanitized = tagName.replace(/[^a-zA-Z0-9_.-]/g, '_'); - - return /^[a-zA-Z_]/.test(sanitized) ? sanitized : `_${sanitized}`; -};