UNPKG

typesxml

Version:

Open source XML library written in TypeScript

120 lines 4.05 kB
"use strict"; /******************************************************************************* * Copyright (c) 2023 - 2024 Maxprograms. * * This program and the accompanying materials * are made available under the terms of the Eclipse License 1.0 * which accompanies this distribution, and is available at * https://www.eclipse.org/org/documents/epl-v10.html * * Contributors: * Maxprograms - initial API and implementation *******************************************************************************/ Object.defineProperty(exports, "__esModule", { value: true }); exports.XMLUtils = void 0; class XMLUtils { static SPACES = ' \t\r\n'; static cleanString(text) { let result = XMLUtils.replaceAll(text, '&', '&amp;'); result = XMLUtils.replaceAll(result, '<', '&lt;'); return XMLUtils.replaceAll(result, '>', '&gt;'); } static unquote(text) { return XMLUtils.replaceAll(text, '"', '&quot;'); } static normalizeLines(text) { let result = XMLUtils.replaceAll(text, '\r\n', '\n'); return XMLUtils.replaceAll(result, '\r', '\n'); } static isXmlSpace(char) { return this.SPACES.indexOf(char) > -1; } static hasParameterEntity(text) { let index = text.indexOf('%'); if (index === -1) { return false; } let length = text.length; for (let i = index + 1; i < length; i++) { let c = text.charAt(i); if (this.isXmlSpace(c)) { return false; } if (c === ';') { return true; } } return false; } static normalizeSpaces(text) { return String(text).replace(/\s+/g, ' '); } static replaceAll(text, search, replacement) { return String(text).split(search).join(replacement); } static escapeRegExpChars(text) { let result = ''; let length = text.length; for (let i = 0; i < length; i++) { let c = text.charAt(i); if ('[]{}()^$?*+.'.indexOf(c) > -1) { result += '\\'; } result += c; } return result; } static validXml10Chars(text) { let result = ''; let length = text.length; for (let i = 0; i < length; i++) { let c = text.charCodeAt(i); if (XMLUtils.isValidXml10Char(c)) { result += String.fromCharCode(c); } } return result; } static isValidXml10Char(c) { // From XML 1.0 spec valid chars: // #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] // any Unicode character, excluding the surrogate blocks, FFFE, and FFFF. return c === 0x9 || c == 0xA || c === 0xD || (c >= 0x20 && c <= 0xD7FF) || (c >= 0xE000 && c <= 0xFFFD) || (c >= 0x10000 && c <= 0x10FFFF); } static validXml11Chars(text) { let result = ''; let length = text.length; for (let i = 0; i < length; i++) { let c = text.charCodeAt(i); if (XMLUtils.isValidXml11Char(c)) { result += String.fromCharCode(c); } } return result; } static isValidXml11Char(c) { // From XML 1.1 spec valid chars: // [#x1-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] // any Unicode character, excluding the surrogate blocks, FFFE, and FFFF. return (c >= 0x1 && c <= 0xD7FF) || (c >= 0xE000 && c <= 0xFFFD) || (c >= 0x10000 && c <= 0x10FFFF); } static lookingAt(search, text, start) { let length = search.length; if (length + start > text.length) { return false; } for (let i = 0; i < length; i++) { if (text[start + i] !== search[i]) { return false; } } return true; } } exports.XMLUtils = XMLUtils; //# sourceMappingURL=XMLUtils.js.map