UNPKG

@sap-ux/project-access

Version:

Library to access SAP Fiori tools projects

79 lines 2.66 kB
import { XMLParser } from 'fast-xml-parser'; /** * Recursively checks if an object (parsed XML) contains an element with the specified id attribute. * * @param obj - parsed XML object to search in * @param id - id to search for * @param attrPrefix - attribute prefix used by the parser (default: '@_') * @returns true if an element with the id is found */ function hasElementWithId(obj, id, attrPrefix = '@_') { if (typeof obj !== 'object' || obj === null) { return false; } const objRecord = obj; const idAttr = `${attrPrefix}id`; // Check if this element has the id attribute if (objRecord[idAttr] === id) { return true; } for (const key in objRecord) { if (key.startsWith(attrPrefix)) { continue; // Skip attributes } if (checkIdInValue(objRecord[key], id, attrPrefix)) { return true; } } return false; } /** * Checks if a value (object or array) contains an element with the specified id. * * @param value - value to check (can be array or object) * @param id - id to search for * @param attrPrefix - attribute prefix used by the parser * @returns true if id is found in the value */ function checkIdInValue(value, id, attrPrefix) { if (Array.isArray(value)) { return value.some((item) => hasElementWithId(item, id, attrPrefix)); } if (typeof value === 'object' && value !== null) { return hasElementWithId(value, id, attrPrefix); } return false; } /** * Checks if an element with the specified id is available (does not exist) in the XML content. * * @param id - id to check for availability * @param xmlContent - XML content as string * @returns true if the id is available (not found), false if it exists */ function checkElementIdAvailable(id, xmlContent) { const parser = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: '@_', parseAttributeValue: false }); try { const xmlDocument = parser.parse(xmlContent); return xmlDocument ? !hasElementWithId(xmlDocument, id) : true; } catch { // Parse error = no valid document = no element with id return true; } } /** * Checks if a UI5 control ID is unique across XML files (fragments and views). * * @param id - ID to check * @param files - Array of XML file contents to check * @returns true if the id is unique (available), false if it already exists */ export function isUI5IdUnique(id, files) { return files.every((content) => content === '' || checkElementIdAvailable(id, content)); } //# sourceMappingURL=ui5-xml-id-validator.js.map