@sap-ux/fe-fpm-writer 1.3.23 → 1.3.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -266,7 +266,11 @@ function updateViewFile(basePath, viewPath, aggregationPath, viewDocument, templ
266
266
  const targetNodes = xpathSelect(resolvedPath, viewDocument);
267
267
  if (targetNodes && Array.isArray(targetNodes) && targetNodes.length > 0) {
268
268
  const targetNode = targetNodes[0];
269
- const sourceNode = viewDocument.importNode(templateDocument.documentElement, true);
269
+ const docElement = templateDocument.documentElement;
270
+ if (!docElement) {
271
+ throw new Error(`Template document has no root element.`);
272
+ }
273
+ const sourceNode = viewDocument.importNode(docElement, true);
270
274
  if (replace) {
271
275
  targetNode.parentNode?.replaceChild(sourceNode, targetNode);
272
276
  }
@@ -336,7 +340,9 @@ export async function getSerializedFileContent(basePath, config, fs) {
336
340
  const snippetMacrosNS = getOrAddNamespace(nsDoc, 'sap.fe.macros', 'macros') || 'macros';
337
341
  const snippetContent = `${content}`.replace(new RegExp(`^<(${snippetMacrosNS}:Page)`), `<$1 xmlns:${snippetMacrosNS}="sap.fe.macros"`);
338
342
  const snippetDoc = new DOMParser(getDOMParserOptions(TEMPLATE_NAMESPACES, (level, message) => {
339
- throw new Error(`Unable to parse Page building block snippet. Details: [${level}] - ${message}`);
343
+ if (level !== 'warning') {
344
+ throw new Error(`Unable to parse Page building block snippet. Details: [${level}] - ${message}`);
345
+ }
340
346
  })).parseFromString(snippetContent, 'text/xml');
341
347
  appendPageAggregations(fs, nsDoc, snippetDoc, fnGenerateId, pageAggNames, isFullPageTemplate(buildingBlockData), stickySubheaderId);
342
348
  const resultNode = snippetDoc.documentElement;
@@ -1,3 +1,4 @@
1
+ import { type Document as XmldomDocument, type Node as XmldomNode } from '@xmldom/xmldom';
1
2
  import type { Editor } from 'mem-fs-editor';
2
3
  import { type BuildingBlock, type PageAggregationName, type GenerateBuildingBlockAggregationConfig } from './types.js';
3
4
  import type { Manifest } from '../common/types.js';
@@ -10,7 +11,7 @@ import { type IdGeneratorFunction } from '../common/file.js';
10
11
  * @param fs - the memfs editor instance
11
12
  * @returns {Document} the view xml file document
12
13
  */
13
- export declare function getUI5XmlDocument(basePath: string, viewPath: string, fs: Editor): Document;
14
+ export declare function getUI5XmlDocument(basePath: string, viewPath: string, fs: Editor): XmldomDocument;
14
15
  /**
15
16
  * Returns the aggregation names to append for a Page building block, or undefined if not a Page.
16
17
  * Full template appends all PAGE_AGGREGATIONS; basic template appends nothing.
@@ -51,7 +52,7 @@ export declare function buildAggregationIds(aggName: PageAggregationName, genera
51
52
  * @param useDefaults - when true, the items aggregation renders its default IconTabBar content
52
53
  * @param stickySubheaderId - collision-safe id for the default IconTabBar, shared with the Page's stickySubheaderProvider
53
54
  */
54
- export declare function appendPageAggregations(fs: Editor, xmlDocument: Document, templateDocument: Document, generateId: IdGeneratorFunction, aggNames?: readonly PageAggregationName[], useDefaults?: boolean, stickySubheaderId?: string): void;
55
+ export declare function appendPageAggregations(fs: Editor, xmlDocument: XmldomDocument, templateDocument: XmldomDocument, generateId: IdGeneratorFunction, aggNames?: readonly PageAggregationName[], useDefaults?: boolean, stickySubheaderId?: string): void;
55
56
  /**
56
57
  * Reorders the child elements of a macros:Page node to match the canonical PAGE_AGGREGATIONS order.
57
58
  * Preserves relative order of siblings with the same local name. Pure whitespace text nodes are dropped
@@ -59,7 +60,7 @@ export declare function appendPageAggregations(fs: Editor, xmlDocument: Document
59
60
  *
60
61
  * @param pageElement - the macros:Page DOM node whose children should be sorted
61
62
  */
62
- export declare function sortPageAggregationChildren(pageElement: Node): void;
63
+ export declare function sortPageAggregationChildren(pageElement: XmldomNode): void;
63
64
  /**
64
65
  * Ensures a namespaced aggregation element exists in the XML document at the given aggregation path.
65
66
  * If the final segment (e.g. 'macros:items') is missing from the DOM, the parent node is located
@@ -69,7 +70,7 @@ export declare function sortPageAggregationChildren(pageElement: Node): void;
69
70
  * @param xmlDocument - The XML document to mutate
70
71
  * @param aggregationPath - Full XPath to the target aggregation (e.g. '/mvc:View/macros:Page/macros:items')
71
72
  */
72
- export declare function ensureMissingAggregation(xmlDocument: Document, aggregationPath: string): void;
73
+ export declare function ensureMissingAggregation(xmlDocument: XmldomDocument, aggregationPath: string): void;
73
74
  /**
74
75
  * Appends a single Page building block aggregation template to an existing `<macros:Page>` element in a view XML file.
75
76
  *
@@ -76,7 +76,7 @@ export function validateFullPageTemplateVersion(manifest) {
76
76
  function resolveMacrosPrefix(xmlDocument) {
77
77
  const prefix = getOrAddNamespace(xmlDocument, 'sap.fe.macros', 'macros');
78
78
  if (prefix === '') {
79
- xmlDocument.documentElement.setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:macros', 'sap.fe.macros');
79
+ xmlDocument.documentElement?.setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:macros', 'sap.fe.macros');
80
80
  return 'macros';
81
81
  }
82
82
  return prefix;
@@ -151,13 +151,15 @@ export function buildAggregationIds(aggName, generateId, existingIds = []) {
151
151
  function buildPageAggregationFragment(fs, aggName, aggContext, fragMacrosNS, xmlDocument) {
152
152
  const aggPath = getTemplatePath(`/building-block/page/${aggName}.xml`);
153
153
  const aggContent = render(fs.read(aggPath), aggContext, {}); // NOSONAR - template is a controlled file on disk, not user input
154
- const extraNamespaces = Array.from(xmlDocument.documentElement.attributes)
154
+ const extraNamespaces = Array.from(xmlDocument.documentElement?.attributes ?? [])
155
155
  .filter((a) => a.name.startsWith('xmlns:') && a.name !== `xmlns:${fragMacrosNS}` && a.name !== 'xmlns:m')
156
156
  .map((a) => `${a.name}="${a.value}"`)
157
157
  .join(' ');
158
158
  const wrapped = `<root xmlns:${fragMacrosNS}="sap.fe.macros" xmlns="sap.m" xmlns:m="sap.m" ${extraNamespaces}>${aggContent}</root>`;
159
159
  return new DOMParser(getDOMParserOptions(TEMPLATE_NAMESPACES, (level, message) => {
160
- throw new Error(`Unable to parse page aggregation fragment '${aggName}'. Details: [${level}] - ${message}`);
160
+ if (level !== 'warning') {
161
+ throw new Error(`Unable to parse page aggregation fragment '${aggName}'. Details: [${level}] - ${message}`);
162
+ }
161
163
  })).parseFromString(wrapped, 'text/xml');
162
164
  }
163
165
  /**
@@ -174,6 +176,9 @@ export function appendPageAggregations(fs, xmlDocument, templateDocument, genera
174
176
  const fragMacrosNS = resolveMacrosPrefix(xmlDocument);
175
177
  const macrosPrefix = `${fragMacrosNS}:`;
176
178
  const pageElement = templateDocument.documentElement;
179
+ if (!pageElement) {
180
+ throw new Error(`Template document has no root element.`);
181
+ }
177
182
  pageElement.appendChild(templateDocument.createComment(PAGE_TEMPLATE_COMMENT));
178
183
  for (const aggName of aggNames) {
179
184
  const aggId = generateId(aggName);
@@ -181,7 +186,7 @@ export function appendPageAggregations(fs, xmlDocument, templateDocument, genera
181
186
  const ids = buildAggregationIds(aggName, generateId);
182
187
  const aggContext = { macrosPrefix, aggId, showDefaultContent, ids, aggIndex: 1, stickySubheaderId };
183
188
  const aggDoc = buildPageAggregationFragment(fs, aggName, aggContext, fragMacrosNS, xmlDocument);
184
- for (const node of Array.from(aggDoc.documentElement.childNodes)) {
189
+ for (const node of Array.from(aggDoc.documentElement?.childNodes ?? [])) {
185
190
  if (node.nodeType === 1 /* Element */) {
186
191
  pageElement.appendChild(templateDocument.importNode(node, true));
187
192
  }
@@ -308,7 +313,7 @@ export function ensureMissingAggregation(xmlDocument, aggregationPath) {
308
313
  }
309
314
  const resolvedPrefix = getOrAddNamespace(xmlDocument, namespaceUri, prefix);
310
315
  if (resolvedPrefix === '' && prefix) {
311
- xmlDocument.documentElement.setAttributeNS('http://www.w3.org/2000/xmlns/', `xmlns:${prefix}`, namespaceUri);
316
+ xmlDocument.documentElement?.setAttributeNS('http://www.w3.org/2000/xmlns/', `xmlns:${prefix}`, namespaceUri);
312
317
  }
313
318
  // Rebuild xpathSelect with the prefix explicitly mapped so XPath resolves prefixed steps
314
319
  // correctly even when the document uses sap.fe.macros as its default namespace.
@@ -333,12 +338,14 @@ function wrapLooseBuildingBlocksInItems(pageElement, xmlDocument, macrosNS, macr
333
338
  const aggregationSet = new Set(PAGE_AGGREGATIONS);
334
339
  const looseChildren = Array.from(pageElement.childNodes).filter((n) => n.nodeType === 1 /* Element */ &&
335
340
  n.namespaceURI === macrosNS &&
336
- !aggregationSet.has(n.localName));
341
+ !aggregationSet.has(n.localName ?? ''));
337
342
  if (looseChildren.length === 0) {
338
343
  return;
339
344
  }
340
345
  const itemsName = `${macrosPrefix}:items`;
341
- let itemsEl = Array.from(pageElement.childNodes).find((n) => n.nodeType === 1 && n.namespaceURI === macrosNS && n.localName === 'items');
346
+ let itemsEl = Array.from(pageElement.childNodes).find((n) => n.nodeType === 1 &&
347
+ n.namespaceURI === macrosNS &&
348
+ n.localName === 'items');
342
349
  if (!itemsEl) {
343
350
  itemsEl = xmlDocument.createElementNS(macrosNS, itemsName);
344
351
  pageElement.insertBefore(itemsEl, looseChildren[0]);
@@ -398,7 +405,7 @@ function writeXmlDocument(fs, basePath, viewPath, xmlDocument) {
398
405
  * @param xmlDocument - the owner document (used to import nodes)
399
406
  */
400
407
  function appendChildrenIntoContainer(existingContainer, aggDoc, xmlDocument) {
401
- const renderedWrapper = Array.from(aggDoc.documentElement.childNodes).find((n) => n.nodeType === 1 /* Element */);
408
+ const renderedWrapper = Array.from(aggDoc.documentElement?.childNodes ?? []).find((n) => n.nodeType === 1 /* Element */);
402
409
  if (!renderedWrapper) {
403
410
  return;
404
411
  }
@@ -476,7 +483,7 @@ export async function generateBuildingBlockAggregation(basePath, config, fs) {
476
483
  if (!hasExistingElementChildren && !hasTemplateComment) {
477
484
  pageElement.appendChild(xmlDocument.createComment(PAGE_TEMPLATE_COMMENT));
478
485
  }
479
- for (const node of Array.from(aggDoc.documentElement.childNodes)) {
486
+ for (const node of Array.from(aggDoc.documentElement?.childNodes ?? [])) {
480
487
  if (node.nodeType === 1 /* Element */) {
481
488
  pageElement.appendChild(xmlDocument.importNode(node, true));
482
489
  }
@@ -1,4 +1,5 @@
1
1
  import type { Editor } from 'mem-fs-editor';
2
+ import type { Document as XmldomDocument } from '@xmldom/xmldom';
2
3
  import { BuildingBlockType, type BuildingBlock, type EmbededFragment, type EmbeddedAction } from './types.js';
3
4
  import type { Manifest, InternalCustomElement } from '../common/types.js';
4
5
  /**
@@ -21,7 +22,7 @@ interface NamespaceConfig {
21
22
  */
22
23
  interface ProcessingContext {
23
24
  fs: Editor;
24
- xmlDocument?: Document;
25
+ xmlDocument?: XmldomDocument;
25
26
  viewPath?: string;
26
27
  embeddedFragment?: EmbeddedFragmentData;
27
28
  updatedAggregationPath?: string;
@@ -72,7 +73,7 @@ export declare function resolveAggregationPath(aggregationPath: string): string;
72
73
  * @param {Editor} fs - The memfs editor instance
73
74
  * @returns {object} Object containing updated aggregation path and processed building block data
74
75
  */
75
- export declare function processBuildingBlock<T extends BuildingBlock>(buildingBlockData: T, xmlDocument: Document, manifestPath: string, manifest: Manifest, aggregationPath: string, fs: Editor): {
76
+ export declare function processBuildingBlock<T extends BuildingBlock>(buildingBlockData: T, xmlDocument: XmldomDocument, manifestPath: string, manifest: Manifest, aggregationPath: string, fs: Editor): {
76
77
  updatedAggregationPath: string;
77
78
  processedBuildingBlockData: T;
78
79
  hasAggregation: boolean;
@@ -1,12 +1,7 @@
1
- import { type Options } from '@xmldom/xmldom';
1
+ import { type DOMParserOptions, type Document as XmldomDocument } from '@xmldom/xmldom';
2
2
  import type { Editor } from 'mem-fs-editor';
3
3
  /** Namespace map for `@xmldom/xmldom`: maps namespace prefixes to their URIs for UI5 view and fragment files. */
4
4
  export declare const TEMPLATE_NAMESPACES: Record<string, string>;
5
- /** `Options` extended with `xmlns` and `onError` */
6
- type DOMParserOptions = Options & {
7
- xmlns?: Record<string, string | null | undefined>;
8
- onError?: (level: string, message: string) => void;
9
- };
10
5
  /**
11
6
  * Builds a `DOMParser` options object.
12
7
  *
@@ -59,6 +54,5 @@ export declare function getExistingButtonGroups(xmlFilePath: string, aggregation
59
54
  * @param prefix - The preferred prefix to use if adding (e.g. 'macros', 'richtexteditor')
60
55
  * @returns The prefix bound to the namespace URI ('' for default, or the prefix)
61
56
  */
62
- export declare function getOrAddNamespace(ui5XmlDocument: Document, namespaceUri?: string, prefix?: string): string;
63
- export {};
57
+ export declare function getOrAddNamespace(ui5XmlDocument: XmldomDocument, namespaceUri?: string, prefix?: string): string;
64
58
  //# sourceMappingURL=xml.d.ts.map
@@ -20,7 +20,9 @@ export const TEMPLATE_NAMESPACES = {
20
20
  export function getDOMParserOptions(xmlns, onError) {
21
21
  const handler = onError ??
22
22
  ((level, message) => {
23
- throw new Error(`Unable to parse template file with building block data. Details: [${level}] - ${message}`);
23
+ if (level !== 'warning') {
24
+ throw new Error(`Unable to parse template file with building block data. Details: [${level}] - ${message}`);
25
+ }
24
26
  });
25
27
  return {
26
28
  onError: handler,
@@ -77,7 +79,9 @@ export function getXPathStringsForXmlFile(xmlFilePath, fs) {
77
79
  try {
78
80
  const xmlContent = fs.read(xmlFilePath);
79
81
  const xmlDocument = new DOMParser(getDOMParserOptions()).parseFromString(xmlContent, 'text/xml');
80
- const nodes = [{ parentNode: '', node: xmlDocument.firstChild }];
82
+ const nodes = [
83
+ { parentNode: '', node: xmlDocument.firstChild }
84
+ ];
81
85
  // check macros namespace and page macro definition
82
86
  const macrosNamespace = getOrAddNamespace(xmlDocument);
83
87
  pageMacroDefinition = macrosNamespace ? `${macrosNamespace}:Page` : 'macros:Page';
@@ -233,6 +237,9 @@ function findNamespacePrefix(root, namespaceUri) {
233
237
  */
234
238
  export function getOrAddNamespace(ui5XmlDocument, namespaceUri = 'sap.fe.macros', prefix = 'macros') {
235
239
  const root = ui5XmlDocument.documentElement;
240
+ if (!root) {
241
+ return prefix;
242
+ }
236
243
  // Check all namespace attributes for a matching URI
237
244
  const existingPrefix = findNamespacePrefix(root, namespaceUri);
238
245
  if (existingPrefix !== null) {
@@ -1,5 +1,6 @@
1
1
  import type { IdGeneratorFunction } from '../common/file.js';
2
2
  import type { CustomElement, CustomFragment, EventHandler, FragmentContentData, Position } from '../common/types.js';
3
+ import type { Node as XmldomNode, Element as XmldomElement } from '@xmldom/xmldom';
3
4
  export declare const PageTemplateType: {
4
5
  readonly Full: "full";
5
6
  readonly Basic: "basic";
@@ -412,8 +413,8 @@ export declare const MACROS_NAMESPACE_URI = "sap.fe.macros";
412
413
  * Used when re-ordering aggregation children under a macros:Page element.
413
414
  */
414
415
  export type XmlAggregationGroup = {
415
- comments: Node[];
416
- element: Element;
416
+ comments: XmldomNode[];
417
+ element: XmldomElement;
417
418
  originalIndex: number;
418
419
  };
419
420
  /**
@@ -1,3 +1,4 @@
1
+ import type { Logger } from '@sap-ux/logger';
1
2
  import type { Editor } from 'mem-fs-editor';
2
3
  import type { FileContentPosition, ManifestData } from './types.js';
3
4
  /**
@@ -49,12 +50,14 @@ export declare function getManifestPath(basePath: string, fs: Editor): Promise<s
49
50
  */
50
51
  export declare function getManifest(basePath: string, fs: Editor, validate?: boolean): Promise<ManifestData>;
51
52
  /**
52
- * Method validates if passed id is available.
53
+ * Validates if the given id is not already used in the specified XML view or fragment file.
54
+ * If the file cannot be parsed, falls back to a regex search on the raw content.
53
55
  *
54
- * @param fs - the file system object for reading files
56
+ * @param fs - the file system object for reading files
55
57
  * @param viewOrFragmentPath - path to fragment or view file
56
58
  * @param id - id to check/validate
57
- * @returns true if passed id is available.
59
+ * @param logger - optional logger for parse errors
60
+ * @returns true if the id is available, false if it is already in use.
58
61
  */
59
- export declare function isElementIdAvailable(fs: Editor, viewOrFragmentPath: string, id: string): boolean;
62
+ export declare function isElementIdAvailable(fs: Editor, viewOrFragmentPath: string, id: string, logger?: Logger): boolean;
60
63
  //# sourceMappingURL=utils.d.ts.map
@@ -96,16 +96,25 @@ export async function getManifest(basePath, fs, validate = true) {
96
96
  };
97
97
  }
98
98
  /**
99
- * Method validates if passed id is available.
99
+ * Validates if the given id is not already used in the specified XML view or fragment file.
100
+ * If the file cannot be parsed, falls back to a regex search on the raw content.
100
101
  *
101
- * @param fs - the file system object for reading files
102
+ * @param fs - the file system object for reading files
102
103
  * @param viewOrFragmentPath - path to fragment or view file
103
104
  * @param id - id to check/validate
104
- * @returns true if passed id is available.
105
+ * @param logger - optional logger for parse errors
106
+ * @returns true if the id is available, false if it is already in use.
105
107
  */
106
- export function isElementIdAvailable(fs, viewOrFragmentPath, id) {
108
+ export function isElementIdAvailable(fs, viewOrFragmentPath, id, logger) {
107
109
  const xmlContent = fs.read(viewOrFragmentPath).toString();
108
- const xmlDocument = new DOMParser(getDOMParserOptions(undefined, () => { })).parseFromString(xmlContent, 'text/xml');
109
- return xmlDocument.documentElement ? !xmlDocument.getElementById(id) : true;
110
+ try {
111
+ const xmlDocument = new DOMParser(getDOMParserOptions(undefined, () => { })).parseFromString(xmlContent, 'text/xml');
112
+ return xmlDocument.documentElement ? !xmlDocument.getElementById(id) : true;
113
+ }
114
+ catch (e) {
115
+ // xmldom 0.9+ throws ParseError for malformed XML — fall back to a regex search on the raw content
116
+ logger?.warn(`isElementIdAvailable: failed to parse "${viewOrFragmentPath}", falling back to text search: ${e instanceof Error ? e.message : String(e)}`);
117
+ return !new RegExp(String.raw `(?<![a-zA-Z0-9_-])id\s*=\s*["']${id.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}["']`).test(xmlContent);
118
+ }
110
119
  }
111
120
  //# sourceMappingURL=utils.js.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sap-ux/fe-fpm-writer",
3
3
  "description": "SAP Fiori elements flexible programming model writer",
4
- "version": "1.3.23",
4
+ "version": "1.3.25",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/SAP/open-ux-tools.git",
@@ -23,7 +23,7 @@
23
23
  "dependencies": {
24
24
  "@sap-ux/annotation-converter": "0.10.22",
25
25
  "@sap-ux/vocabularies-types": "0.16.0",
26
- "@xmldom/xmldom": "0.9.10",
26
+ "@xmldom/xmldom": "0.9.12",
27
27
  "ejs": "3.1.10",
28
28
  "i18next": "26.3.6",
29
29
  "mem-fs": "2.1.0",
@@ -31,9 +31,9 @@
31
31
  "semver": "7.8.5",
32
32
  "xml-formatter": "3.7.0",
33
33
  "xpath": "0.0.34",
34
- "@sap-ux/fiori-annotation-api": "1.1.3",
35
- "@sap-ux/i18n": "1.0.5",
36
- "@sap-ux/project-access": "2.3.0",
34
+ "@sap-ux/fiori-annotation-api": "1.1.4",
35
+ "@sap-ux/i18n": "1.0.6",
36
+ "@sap-ux/project-access": "2.3.1",
37
37
  "@sap-ux/logger": "1.0.3"
38
38
  },
39
39
  "devDependencies": {
@@ -42,9 +42,9 @@
42
42
  "@types/ejs": "3.1.5",
43
43
  "@types/mem-fs": "1.1.2",
44
44
  "@types/mem-fs-editor": "7.0.1",
45
- "@types/semver": "7.7.1",
45
+ "@types/semver": "7.8.0",
46
46
  "@types/vinyl": "2.0.12",
47
- "@sap-ux/ui-prompting": "1.0.16"
47
+ "@sap-ux/ui-prompting": "1.0.17"
48
48
  },
49
49
  "engines": {
50
50
  "node": ">=22.x"