@sap-ux/fe-fpm-writer 1.2.0 → 1.3.0

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.
@@ -0,0 +1,408 @@
1
+ import { create as createStorage } from 'mem-fs';
2
+ import { create } from 'mem-fs-editor';
3
+ import { render } from 'ejs';
4
+ import { join } from 'node:path';
5
+ import { DOMParser, XMLSerializer } from '@xmldom/xmldom';
6
+ import format from 'xml-formatter';
7
+ import * as xpath from 'xpath';
8
+ import { coerce, lt } from 'semver';
9
+ import { getMinimumUI5Version } from '@sap-ux/project-access';
10
+ import { BuildingBlockType, PAGE_AGGREGATIONS, MIN_UI5_VERSION_PAGE_BUILDING_BLOCK_FULL_LAYOUT, PAGE_TEMPLATE_COMMENT, PageTemplateType } from './types.js';
11
+ import { getTemplatePath } from '../templates.js';
12
+ import { createIdGenerator } from '../common/file.js';
13
+ import { getErrorMessage } from '../common/validate.js';
14
+ import { i18nNamespaces, translate } from '../i18n.js';
15
+ import { getOrAddNamespace } from './prompts/utils/xml.js';
16
+ import { resolveAggregationPath } from './processor.js';
17
+ /**
18
+ * Returns the UI5 xml file document (view/fragment).
19
+ *
20
+ * @param {string} basePath - the base path
21
+ * @param {string} viewPath - the path of the xml view relative to the base path
22
+ * @param {Editor} fs - the memfs editor instance
23
+ * @returns {Document} the view xml file document
24
+ */
25
+ export function getUI5XmlDocument(basePath, viewPath, fs) {
26
+ let viewContent;
27
+ try {
28
+ viewContent = fs.read(join(basePath, viewPath));
29
+ }
30
+ catch (error) {
31
+ throw new Error(`Unable to read xml view file. Details: ${getErrorMessage(error)}`);
32
+ }
33
+ const errorHandler = (level, message) => {
34
+ throw new Error(`Unable to parse xml view file. Details: [${level}] - ${message}`);
35
+ };
36
+ let viewDocument;
37
+ try {
38
+ viewDocument = new DOMParser({ errorHandler }).parseFromString(viewContent, 'text/xml');
39
+ }
40
+ catch (error) {
41
+ throw new Error(`Unable to parse xml view file. Details: ${getErrorMessage(error)}`);
42
+ }
43
+ return viewDocument;
44
+ }
45
+ /**
46
+ * Returns the aggregation names to append for a Page building block, or undefined if not a Page.
47
+ * Full template appends all PAGE_AGGREGATIONS; basic template appends nothing.
48
+ *
49
+ * @param data - the building block data
50
+ * @returns aggregation names array, or undefined if not a Page building block
51
+ */
52
+ export function getPageAggregationNames(data) {
53
+ if (data.buildingBlockType !== BuildingBlockType.Page) {
54
+ return undefined;
55
+ }
56
+ return data.templateType === PageTemplateType.Full ? PAGE_AGGREGATIONS : undefined;
57
+ }
58
+ /**
59
+ * Throws if the manifest's minUI5Version is set and does not meet the 1.145.0 requirement for the full Page template.
60
+ * If minUI5Version is not set, it is treated as latest and passes the check.
61
+ *
62
+ * @param manifest - the manifest content, or undefined if not available
63
+ */
64
+ export function validateFullPageTemplateVersion(manifest) {
65
+ const minUI5Version = manifest ? coerce(getMinimumUI5Version(manifest)) : undefined;
66
+ if (minUI5Version && lt(minUI5Version, MIN_UI5_VERSION_PAGE_BUILDING_BLOCK_FULL_LAYOUT)) {
67
+ const t = translate(i18nNamespaces.buildingBlock, 'pageBuildingBlock.');
68
+ throw new Error(`${t('fullTemplateMinUi5VersionRequirement', { minUI5Version: minUI5Version.version })}`);
69
+ }
70
+ }
71
+ /**
72
+ * Resolves the sap.fe.macros namespace prefix from the view document.
73
+ * If sap.fe.macros is the default namespace (no prefix), declares xmlns:macros on the document element
74
+ * so that generated prefixed elements like <macros:items> remain valid.
75
+ *
76
+ * @param xmlDocument - the view XML document
77
+ * @returns the resolved namespace prefix string (e.g. 'macros')
78
+ */
79
+ function resolveMacrosPrefix(xmlDocument) {
80
+ const prefix = getOrAddNamespace(xmlDocument, 'sap.fe.macros', 'macros');
81
+ if (prefix === '') {
82
+ xmlDocument.documentElement.setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:macros', 'sap.fe.macros');
83
+ return 'macros';
84
+ }
85
+ return prefix;
86
+ }
87
+ /** IDs needed per aggregation template, keyed by the variable name used in the EJS template. */
88
+ /** Controls to generate IDs for per aggregation. `key` is the EJS template variable name, `base` is passed to generateId. */
89
+ export const AGGREGATION_ID_KEYS = {
90
+ breadcrumbs: [
91
+ { key: 'Breadcrumbs', base: 'breadcrumbs_breadcrumbs' },
92
+ { key: 'Link', base: 'breadcrumbs_link' },
93
+ { key: 'Link1', base: 'breadcrumbs_link_1' },
94
+ { key: 'Link2', base: 'breadcrumbs_link_2' }
95
+ ],
96
+ navigationActions: [{ key: 'Button', base: 'navigationActions_button' }],
97
+ titleContent: [{ key: 'GenericTag', base: 'titleContent_genericTag' }],
98
+ actions: [
99
+ { key: 'Button', base: 'actions_button' },
100
+ { key: 'Button1', base: 'actions_button_1' }
101
+ ],
102
+ headerContent: [
103
+ { key: 'VBox', base: 'headerContent_vbox' },
104
+ { key: 'Title', base: 'headerContent_title' }
105
+ ],
106
+ footer: [
107
+ { key: 'OverflowToolbar', base: 'footer_overflowToolbar' },
108
+ { key: 'ToolbarSpacer', base: 'footer_toolbarSpacer' },
109
+ { key: 'Button', base: 'footer_button' },
110
+ { key: 'Button1', base: 'footer_button_1' }
111
+ ]
112
+ };
113
+ /**
114
+ * Generates a map of unique IDs for all controls in a given Page aggregation template.
115
+ * Keys match the `ids.*` variable names referenced in the EJS templates.
116
+ *
117
+ * @param aggName - the aggregation name
118
+ * @param generateId - the project-aware ID generator
119
+ * @returns an object mapping each template variable name to a unique ID string
120
+ */
121
+ export function buildAggregationIds(aggName, generateId) {
122
+ const entries = AGGREGATION_ID_KEYS[aggName] ?? [];
123
+ const validatedIds = [];
124
+ const ids = {};
125
+ for (const { key, base } of entries) {
126
+ const id = generateId(base, validatedIds);
127
+ ids[key] = id;
128
+ validatedIds.push(id);
129
+ }
130
+ return ids;
131
+ }
132
+ /**
133
+ * Renders a Page aggregation EJS template and parses it as an XML fragment document.
134
+ * Inherits all xmlns:* declarations from the view root so inner content can use any view-declared prefix.
135
+ *
136
+ * @param fs - the memfs editor instance
137
+ * @param aggName - the aggregation name (e.g. 'footer', 'items')
138
+ * @param aggContext - the EJS template context
139
+ * @param aggContext.macrosPrefix - the namespace prefix string (e.g. 'macros:')
140
+ * @param aggContext.aggId - the generated unique ID for the aggregation element
141
+ * @param aggContext.showDefaultContent - when true, the items template renders the default IconTabBar
142
+ * @param aggContext.ids - map of unique IDs for named controls in the template (e.g. ids.Button, ids.Link)
143
+ * @param fragMacrosNS - the namespace prefix resolved for sap.fe.macros
144
+ * @param xmlDocument - the view XML document (used to inherit namespace declarations)
145
+ * @returns parsed XML document whose documentElement contains the aggregation child nodes
146
+ */
147
+ function buildPageAggregationFragment(fs, aggName, aggContext, fragMacrosNS, xmlDocument) {
148
+ const aggPath = getTemplatePath(`/building-block/page/${aggName}.xml`);
149
+ const aggContent = render(fs.read(aggPath), aggContext, {}); // NOSONAR - template is a controlled file on disk, not user input
150
+ const extraNamespaces = Array.from(xmlDocument.documentElement.attributes)
151
+ .filter((a) => a.name.startsWith('xmlns:') && a.name !== `xmlns:${fragMacrosNS}` && a.name !== 'xmlns:m')
152
+ .map((a) => `${a.name}="${a.value}"`)
153
+ .join(' ');
154
+ const wrapped = `<root xmlns:${fragMacrosNS}="sap.fe.macros" xmlns="sap.m" xmlns:m="sap.m" ${extraNamespaces}>${aggContent}</root>`;
155
+ const errorHandler = (level, message) => {
156
+ throw new Error(`Unable to parse page aggregation fragment '${aggName}'. Details: [${level}] - ${message}`);
157
+ };
158
+ return new DOMParser({ errorHandler }).parseFromString(wrapped, 'text/xml');
159
+ }
160
+ /**
161
+ *
162
+ * @param {Editor} fs - the memfs editor instance
163
+ * @param {Document} xmlDocument - the view XML document (used to resolve namespace prefixes)
164
+ * @param {Document} templateDocument - the template document whose root element receives the children
165
+ * @param {IdGeneratorFunction} generateId - function to generate unique IDs
166
+ * @param {readonly PageAggregationName[]} [aggNames] - aggregation names to append; defaults to all PAGE_AGGREGATIONS
167
+ * @param useDefaults - when true, the items aggregation renders its default IconTabBar content
168
+ */
169
+ export function appendPageAggregations(fs, xmlDocument, templateDocument, generateId, aggNames = PAGE_AGGREGATIONS, useDefaults = true) {
170
+ const fragMacrosNS = resolveMacrosPrefix(xmlDocument);
171
+ const macrosPrefix = `${fragMacrosNS}:`;
172
+ const pageElement = templateDocument.documentElement;
173
+ pageElement.appendChild(templateDocument.createComment(PAGE_TEMPLATE_COMMENT));
174
+ for (const aggName of aggNames) {
175
+ const aggId = generateId(aggName);
176
+ const showDefaultContent = aggName === 'items' && useDefaults;
177
+ const ids = buildAggregationIds(aggName, generateId);
178
+ const aggContext = { macrosPrefix, aggId, showDefaultContent, ids };
179
+ const aggDoc = buildPageAggregationFragment(fs, aggName, aggContext, fragMacrosNS, xmlDocument);
180
+ for (const node of Array.from(aggDoc.documentElement.childNodes)) {
181
+ if (node.nodeType === 1 /* Element */) {
182
+ pageElement.appendChild(templateDocument.importNode(node, true));
183
+ }
184
+ }
185
+ }
186
+ }
187
+ /**
188
+ * Returns the local name of an Element if it belongs to the sap.fe.macros namespace, otherwise an empty string.
189
+ * This ensures only Page aggregation elements are sorted by position; non-macros elements fall back to the items slot.
190
+ *
191
+ * @param el - the DOM Element
192
+ * @returns the local name string, or '' if not a sap.fe.macros element
193
+ */
194
+ function getElementLocalName(el) {
195
+ if (el.namespaceURI !== 'sap.fe.macros') {
196
+ return '';
197
+ }
198
+ return typeof el.localName === 'string' ? el.localName : '';
199
+ }
200
+ /**
201
+ * Builds a comparator for sorting XmlAggregationGroups by their position in aggNames.
202
+ * Unknown elements fall back to the position of 'items'. Ties are broken by original index.
203
+ *
204
+ * @param aggNames - ordered list of aggregation names
205
+ * @returns comparator function for Array.prototype.sort
206
+ */
207
+ function buildAggregationComparator(aggNames) {
208
+ const itemsIdx = aggNames.indexOf('items');
209
+ const fallbackIdx = itemsIdx === -1 ? aggNames.length : itemsIdx;
210
+ return (a, b) => {
211
+ const aIdx = aggNames.indexOf(getElementLocalName(a.element));
212
+ const bIdx = aggNames.indexOf(getElementLocalName(b.element));
213
+ const aOrder = aIdx === -1 ? fallbackIdx : aIdx;
214
+ const bOrder = bIdx === -1 ? fallbackIdx : bIdx;
215
+ return aOrder === bOrder ? a.originalIndex - b.originalIndex : aOrder - bOrder;
216
+ };
217
+ }
218
+ /**
219
+ * Reorders the child elements of a macros:Page node to match the canonical PAGE_AGGREGATIONS order.
220
+ * Preserves relative order of siblings with the same local name. Pure whitespace text nodes are dropped
221
+ * because the xml-formatter call that follows will regenerate proper indentation.
222
+ *
223
+ * @param pageElement - the macros:Page DOM node whose children should be sorted
224
+ */
225
+ export function sortPageAggregationChildren(pageElement) {
226
+ const allChildren = Array.from(pageElement.childNodes);
227
+ const aggNames = PAGE_AGGREGATIONS;
228
+ // Build pairs of [preceding comments, element] to preserve user comments.
229
+ // Comments that appear before the first element are treated as leading and will remain before all aggregation elements.
230
+ const groups = [];
231
+ const leadingComments = [];
232
+ let pendingComments = [];
233
+ let firstElementSeen = false;
234
+ for (const node of allChildren) {
235
+ if (node.nodeType === 8 /* Comment */) {
236
+ // Comments before the first element are leading; after, they are pending
237
+ (firstElementSeen ? pendingComments : leadingComments).push(node);
238
+ }
239
+ else if (node.nodeType === 1 /* Element */) {
240
+ firstElementSeen = true;
241
+ groups.push({ comments: pendingComments, element: node, originalIndex: groups.length });
242
+ pendingComments = [];
243
+ }
244
+ else if (node.nodeType === 3 /* Text */ && node.data?.trim()) {
245
+ // Preserve non-whitespace text nodes with their surrounding group
246
+ pendingComments.push(node);
247
+ }
248
+ // Pure whitespace text nodes are intentionally dropped (xml-formatter regenerates indentation)
249
+ }
250
+ groups.sort(buildAggregationComparator(aggNames));
251
+ while (pageElement.firstChild) {
252
+ pageElement.removeChild(pageElement.firstChild); // NOSONAR - xmldom nodes do not implement Node.remove()
253
+ }
254
+ // Re-insert leading comments first (always before any element)
255
+ for (const comment of leadingComments) {
256
+ pageElement.appendChild(comment);
257
+ }
258
+ for (const { comments, element } of groups) {
259
+ for (const comment of comments) {
260
+ pageElement.appendChild(comment);
261
+ }
262
+ pageElement.appendChild(element);
263
+ }
264
+ // Trailing orphan comments (after the last element)
265
+ for (const comment of pendingComments) {
266
+ pageElement.appendChild(comment);
267
+ }
268
+ }
269
+ /**
270
+ * Ensures a namespaced aggregation element exists in the XML document at the given aggregation path.
271
+ * If the final segment (e.g. 'macros:items') is missing from the DOM, the parent node is located
272
+ * via the path prefix and the element is inserted in place. This allows generateBuildingBlock to
273
+ * handle missing aggregation containers in a single write pass, avoiding a separate commit.
274
+ *
275
+ * @param {Document} xmlDocument - The XML document to mutate
276
+ * @param {string} aggregationPath - Full XPath to the target aggregation (e.g. '/mvc:View/macros:Page/macros:items')
277
+ */
278
+ export function ensureMissingAggregation(xmlDocument, aggregationPath) {
279
+ const nsMap = xmlDocument.documentElement?._nsMap ?? {};
280
+ const xpathSelect = xpath.useNamespaces(nsMap);
281
+ if (xpathSelect(resolveAggregationPath(aggregationPath), xmlDocument).length > 0) {
282
+ return;
283
+ }
284
+ const lastSlash = aggregationPath.lastIndexOf('/');
285
+ if (lastSlash <= 0) {
286
+ return;
287
+ }
288
+ const lastSegment = aggregationPath.slice(lastSlash + 1);
289
+ const colonIdx = lastSegment.indexOf(':');
290
+ if (colonIdx === -1) {
291
+ return;
292
+ }
293
+ const prefix = lastSegment.slice(0, colonIdx);
294
+ const localName = lastSegment.slice(colonIdx + 1);
295
+ // getOrAddNamespace resolves the URI from the document and declares it if missing.
296
+ // Fall back to 'sap.fe.macros' for the 'macros' prefix in case it's declared as a default
297
+ // namespace (xmlns="sap.fe.macros") and therefore absent from the prefix→URI nsMap.
298
+ let namespaceUri = nsMap[prefix];
299
+ if (!namespaceUri && prefix === 'macros') {
300
+ namespaceUri = 'sap.fe.macros';
301
+ }
302
+ if (!namespaceUri) {
303
+ return;
304
+ }
305
+ const resolvedPrefix = getOrAddNamespace(xmlDocument, namespaceUri, prefix);
306
+ if (resolvedPrefix === '' && prefix) {
307
+ xmlDocument.documentElement.setAttributeNS('http://www.w3.org/2000/xmlns/', `xmlns:${prefix}`, namespaceUri);
308
+ }
309
+ // Rebuild xpathSelect with the prefix explicitly mapped so XPath resolves prefixed steps
310
+ // correctly even when the document uses sap.fe.macros as its default namespace.
311
+ const xpathSelectWithPrefix = xpath.useNamespaces({ ...nsMap, [prefix]: namespaceUri });
312
+ const parentNodes = xpathSelectWithPrefix(resolveAggregationPath(aggregationPath.slice(0, lastSlash)), xmlDocument);
313
+ if (parentNodes.length === 0) {
314
+ return;
315
+ }
316
+ parentNodes[0].appendChild(xmlDocument.createElementNS(namespaceUri, `${prefix}:${localName}`));
317
+ }
318
+ /**
319
+ * Wraps any direct sap.fe.macros children of pageElement that are not named Page aggregations
320
+ * (i.e. loose building blocks like macros:Form, macros:Table) inside a <macros:items> element.
321
+ * Called before inserting a new named aggregation so the DOM stays well-formed.
322
+ *
323
+ * @param pageElement - the macros:Page DOM element
324
+ * @param xmlDocument - the owner document
325
+ * @param macrosNS - the sap.fe.macros namespace URI
326
+ * @param macrosPrefix - the resolved prefix string (e.g. 'macros')
327
+ */
328
+ function wrapLooseBuildingBlocksInItems(pageElement, xmlDocument, macrosNS, macrosPrefix) {
329
+ const aggregationSet = new Set(PAGE_AGGREGATIONS);
330
+ const looseChildren = Array.from(pageElement.childNodes).filter((n) => n.nodeType === 1 /* Element */ &&
331
+ n.namespaceURI === macrosNS &&
332
+ !aggregationSet.has(n.localName));
333
+ if (looseChildren.length === 0) {
334
+ return;
335
+ }
336
+ const itemsName = `${macrosPrefix}:items`;
337
+ let itemsEl = Array.from(pageElement.childNodes).find((n) => n.nodeType === 1 && n.namespaceURI === macrosNS && n.localName === 'items');
338
+ if (!itemsEl) {
339
+ itemsEl = xmlDocument.createElementNS(macrosNS, itemsName);
340
+ pageElement.insertBefore(itemsEl, looseChildren[0]);
341
+ }
342
+ for (const child of looseChildren) {
343
+ itemsEl.appendChild(child);
344
+ }
345
+ }
346
+ /**
347
+ * Appends a single Page building block aggregation template to an existing `<macros:Page>` element in a view XML file.
348
+ *
349
+ * @param {string} basePath - the base path of the application
350
+ * @param {GenerateBuildingBlockAggregationConfig} config - the aggregation configuration containing aggregationName
351
+ * @param {Editor} [fs] - the memfs editor instance
352
+ * @returns {Editor} the updated memfs editor instance
353
+ */
354
+ export async function generateBuildingBlockAggregation(basePath, config, fs) {
355
+ const { viewPath, buildingBlockType, aggregationName: aggName } = config;
356
+ fs ??= create(createStorage());
357
+ if (buildingBlockType !== BuildingBlockType.Page) {
358
+ throw new Error(`generateBuildingBlockAggregation: unsupported building block type '${buildingBlockType}'. Only 'Page' is currently supported.`);
359
+ }
360
+ const xmlDocument = getUI5XmlDocument(basePath, viewPath, fs);
361
+ const generateId = await createIdGenerator({ basePath, fsEditor: fs });
362
+ const aggId = generateId(aggName);
363
+ const fragMacrosNS = resolveMacrosPrefix(xmlDocument);
364
+ const macrosPrefix = `${fragMacrosNS}:`;
365
+ const ids = buildAggregationIds(aggName, generateId);
366
+ const aggContext = { macrosPrefix, aggId, showDefaultContent: false, ids };
367
+ const aggDoc = buildPageAggregationFragment(fs, aggName, aggContext, fragMacrosNS, xmlDocument);
368
+ const nsMap = xmlDocument.documentElement?._nsMap ?? {};
369
+ // Prefix-agnostic XPath — works regardless of the alias used in the view for sap.fe.macros.
370
+ const xpathSelect = xpath.useNamespaces(nsMap);
371
+ const pageNodes = xpathSelect(`//*[local-name()='Page' and namespace-uri()='sap.fe.macros']`, xmlDocument);
372
+ if (!pageNodes || !Array.isArray(pageNodes) || pageNodes.length === 0) {
373
+ throw new Error(`Page element (sap.fe.macros) not found in view ${viewPath}.`);
374
+ }
375
+ const pageElement = pageNodes[0];
376
+ if (aggName === 'footer' && pageElement.nodeType === 1 /* Element */) {
377
+ pageElement.setAttribute('showFooter', 'true');
378
+ }
379
+ const childNodes = Array.from(pageElement.childNodes);
380
+ const hasExistingAggregation = childNodes.some((node) => node.nodeType === 1 /* Element */ &&
381
+ node.localName === aggName &&
382
+ node.namespaceURI === (nsMap[fragMacrosNS] ?? 'sap.fe.macros'));
383
+ if (hasExistingAggregation) {
384
+ sortPageAggregationChildren(pageElement);
385
+ const existingXmlContent = new XMLSerializer().serializeToString(xmlDocument);
386
+ fs.write(join(basePath, viewPath), format(existingXmlContent));
387
+ return fs;
388
+ }
389
+ const hasExistingElementChildren = childNodes.some((n) => n.nodeType === 1 /* Element */);
390
+ const hasTemplateComment = childNodes.some((n) => n.nodeType === 8 /* Comment */ && n.data?.includes(PAGE_TEMPLATE_COMMENT));
391
+ // Move any loose macros building blocks (e.g. macros:Form, macros:Table) into macros:items
392
+ // before inserting the new named aggregation so the Page DOM stays well-formed.
393
+ const macrosNsUri = nsMap[fragMacrosNS] ?? 'sap.fe.macros';
394
+ wrapLooseBuildingBlocksInItems(pageElement, xmlDocument, macrosNsUri, fragMacrosNS);
395
+ if (!hasExistingElementChildren && !hasTemplateComment) {
396
+ pageElement.appendChild(xmlDocument.createComment(PAGE_TEMPLATE_COMMENT));
397
+ }
398
+ for (const node of Array.from(aggDoc.documentElement.childNodes)) {
399
+ if (node.nodeType === 1 /* Element */) {
400
+ pageElement.appendChild(xmlDocument.importNode(node, true));
401
+ }
402
+ }
403
+ sortPageAggregationChildren(pageElement);
404
+ const newXmlContent = new XMLSerializer().serializeToString(xmlDocument);
405
+ fs.write(join(basePath, viewPath), format(newXmlContent));
406
+ return fs;
407
+ }
408
+ //# sourceMappingURL=processAggregation.js.map
@@ -51,6 +51,16 @@ export declare const BUTTON_GROUP_CONFIGS: {
51
51
  * Configuration map for building block types.
52
52
  */
53
53
  export declare const BUILDING_BLOCK_CONFIG: Partial<Record<BuildingBlockType, BuildingBlockTemplateConfig>>;
54
+ /**
55
+ * Rewrites an XPath aggregation path so that unprefixed steps match elements in any namespace.
56
+ * XPath 1.0 with useNamespaces does not support default namespaces: an unprefixed name only
57
+ * matches no-namespace nodes. Steps like "IconTabBar" or "items" (in the sap.m default ns)
58
+ * would never match. This converts them to *[local-name()='step'] predicates.
59
+ *
60
+ * @param {string} aggregationPath - The original aggregation XPath
61
+ * @returns {string} The rewritten XPath safe for use with xpath.useNamespaces
62
+ */
63
+ export declare function resolveAggregationPath(aggregationPath: string): string;
54
64
  /**
55
65
  * Processes building block configuration.
56
66
  *
@@ -291,7 +291,7 @@ function processRichTextEditorButtonGroups(buildingBlockData, context) {
291
291
  }
292
292
  const existingButtonGroupsMap = new Map();
293
293
  if (hasAggregation && xmlDocument && updatedAggregationPath) {
294
- const xpathSelect = xpath.useNamespaces(xmlDocument.firstChild._nsMap);
294
+ const xpathSelect = xpath.useNamespaces(xmlDocument.documentElement?._nsMap ?? {});
295
295
  // Example: [<Element: richtexteditor:buttonGroups>] containing all ButtonGroup children
296
296
  const buttonGroupsElements = xpathSelect(updatedAggregationPath, xmlDocument);
297
297
  if (buttonGroupsElements.length > 0) {
@@ -314,6 +314,23 @@ function processRichTextEditorButtonGroups(buildingBlockData, context) {
314
314
  }
315
315
  buildingBlockData.buttonGroups = mergeButtonGroups(existingButtonGroupsMap, buildingBlockData);
316
316
  }
317
+ /**
318
+ * Rewrites an XPath aggregation path so that unprefixed steps match elements in any namespace.
319
+ * XPath 1.0 with useNamespaces does not support default namespaces: an unprefixed name only
320
+ * matches no-namespace nodes. Steps like "IconTabBar" or "items" (in the sap.m default ns)
321
+ * would never match. This converts them to *[local-name()='step'] predicates.
322
+ *
323
+ * @param {string} aggregationPath - The original aggregation XPath
324
+ * @returns {string} The rewritten XPath safe for use with xpath.useNamespaces
325
+ */
326
+ export function resolveAggregationPath(aggregationPath) {
327
+ return aggregationPath.replace(/\/([A-Za-z_][A-Za-z0-9_.-]*)(?=\/|$)/g, (_match, step) => {
328
+ if (step.includes(':')) {
329
+ return `/${step}`;
330
+ }
331
+ return `/*[local-name()='${step}']`;
332
+ });
333
+ }
317
334
  /**
318
335
  * Updates aggregation path based on XML document structure.
319
336
  *
@@ -326,9 +343,11 @@ function processRichTextEditorButtonGroups(buildingBlockData, context) {
326
343
  * @returns {object} Object containing the updated aggregation path
327
344
  */
328
345
  function updateAggregationPath(xmlDocument, aggregationPath, config, namespace) {
329
- const xpathSelect = xpath.useNamespaces(xmlDocument.firstChild._nsMap);
346
+ const nsMap = xmlDocument.documentElement?._nsMap ?? {};
347
+ const xpathSelect = xpath.useNamespaces(nsMap);
348
+ const resolvedPath = resolveAggregationPath(aggregationPath);
330
349
  // First, get the target element from the aggregationPath
331
- const targetElement = xpathSelect(aggregationPath, xmlDocument);
350
+ const targetElement = xpathSelect(resolvedPath, xmlDocument);
332
351
  if (!targetElement || !Array.isArray(targetElement) || targetElement.length === 0) {
333
352
  return { updatedAggregationPath: aggregationPath, hasElement: false };
334
353
  }
@@ -1,7 +1,10 @@
1
1
  import { i18nNamespaces, translate } from '../../../i18n.js';
2
2
  import { getBuildingBlockIdPrompt, getViewOrFragmentPathPrompt, getAggregationPathPrompt } from '../utils/index.js';
3
- import { BuildingBlockType, PAGE_TEMPLATE_TYPE_BASIC, PAGE_TEMPLATE_TYPE_FULL } from '../../types.js';
3
+ import { BuildingBlockType, MIN_UI5_VERSION_PAGE_BUILDING_BLOCK_FULL_LAYOUT, PageTemplateType } from '../../types.js';
4
4
  import { SapShortTextType, SapLongTextType } from '@sap-ux/i18n';
5
+ import { getMinimumUI5Version } from '@sap-ux/project-access';
6
+ import { coerce, lt } from 'semver';
7
+ import { getManifest } from '../../../common/utils.js';
5
8
  /**
6
9
  * Returns a list of prompts required to generate a page building block.
7
10
  *
@@ -10,19 +13,26 @@ import { SapShortTextType, SapLongTextType } from '@sap-ux/i18n';
10
13
  */
11
14
  export async function getPageBuildingBlockPrompts(context) {
12
15
  const t = translate(i18nNamespaces.buildingBlock, 'prompts.page.');
16
+ const { content: manifest } = await getManifest(context.appPath, context.fs, false);
17
+ const minUI5Version = manifest ? coerce(getMinimumUI5Version(manifest)) : undefined;
18
+ const hideTemplateType = !!minUI5Version && lt(minUI5Version, MIN_UI5_VERSION_PAGE_BUILDING_BLOCK_FULL_LAYOUT);
13
19
  return {
14
20
  questions: [
15
- {
16
- type: 'list',
17
- name: 'buildingBlockData.templateType',
18
- message: t('templateType.message'),
19
- default: PAGE_TEMPLATE_TYPE_BASIC,
20
- choices: [
21
- { value: PAGE_TEMPLATE_TYPE_BASIC, name: t('templateType.basic') },
22
- { value: PAGE_TEMPLATE_TYPE_FULL, name: t('templateType.full') }
23
- ],
24
- guiOptions: { mandatory: true }
25
- },
21
+ ...(hideTemplateType
22
+ ? []
23
+ : [
24
+ {
25
+ type: 'list',
26
+ name: 'buildingBlockData.templateType',
27
+ message: t('templateType.message'),
28
+ default: PageTemplateType.Basic,
29
+ choices: [
30
+ { value: PageTemplateType.Basic, name: t('templateType.basic') },
31
+ { value: PageTemplateType.Full, name: t('templateType.full') }
32
+ ],
33
+ guiOptions: { mandatory: true }
34
+ }
35
+ ]),
26
36
  getViewOrFragmentPathPrompt(context, t('viewOrFragmentPath.validate'), {
27
37
  message: t('viewOrFragmentPath.message'),
28
38
  guiOptions: {
@@ -69,7 +79,8 @@ export async function getPageBuildingBlockPrompts(context) {
69
79
  ],
70
80
  initialAnswers: {
71
81
  buildingBlockData: {
72
- buildingBlockType: BuildingBlockType.Page
82
+ buildingBlockType: BuildingBlockType.Page,
83
+ ...(hideTemplateType ? { templateType: PageTemplateType.Basic } : {})
73
84
  }
74
85
  }
75
86
  };
@@ -1,5 +1,6 @@
1
1
  import { DOMParser } from '@xmldom/xmldom';
2
2
  import * as xpath from 'xpath';
3
+ import { MACROS_NAMESPACE_URI } from '../../types.js';
3
4
  /**
4
5
  * Converts the provided xpath string from `/mvc:View/Page/content` to
5
6
  * `/mvc:View/*[local-name()='Page']/*[local-name()='content']`.
@@ -14,6 +15,29 @@ export const augmentXpathWithLocalNames = (path) => {
14
15
  }
15
16
  return result.join('/');
16
17
  };
18
+ /**
19
+ * If the given node is a macros:Page element that has no macros:items child, adds a synthesized
20
+ * macros:items path to the result map so callers can target it as an aggregation path.
21
+ * ensureMissingAggregation in generateBuildingBlock will create the DOM element when needed.
22
+ *
23
+ * @param node - the current DOM node being visited
24
+ * @param parentNode - accumulated XPath prefix up to (but not including) this node
25
+ * @param macrosNamespace - the resolved macros namespace prefix (e.g. 'macros')
26
+ * @param result - mutable map of XPath choices to augment
27
+ */
28
+ function addMacrosItemsPathIfMissing(node, parentNode, macrosNamespace, result) {
29
+ if (node.localName !== 'Page' || node.namespaceURI !== MACROS_NAMESPACE_URI) {
30
+ return;
31
+ }
32
+ const resolvedPrefix = macrosNamespace || 'macros';
33
+ const macrosItemsName = `${resolvedPrefix}:items`;
34
+ const hasItemsChild = Array.from(node.childNodes).some((child) => child.nodeType === child.ELEMENT_NODE && child.localName === 'items');
35
+ if (!hasItemsChild) {
36
+ const pageStep = node.prefix ? node.nodeName : `${resolvedPrefix}:Page`;
37
+ const itemsPath = `${parentNode}/${pageStep}/${macrosItemsName}`;
38
+ result[itemsPath] = augmentXpathWithLocalNames(itemsPath);
39
+ }
40
+ }
17
41
  /**
18
42
  * Returns a list of xpath strings for each element of the xml file provided.
19
43
  *
@@ -44,10 +68,11 @@ export function getXPathStringsForXmlFile(xmlFilePath, fs) {
44
68
  // This prevents suggesting insertion points outside macros:Page when a macros:Page is present.
45
69
  hasPageMacroChild = Array.from(node.childNodes).some((child) => child.nodeType === child.ELEMENT_NODE &&
46
70
  child.localName === 'Page' &&
47
- child.nodeName === pageMacroDefinition);
71
+ child.namespaceURI === MACROS_NAMESPACE_URI);
48
72
  if (!hasPageMacroChild) {
49
73
  result[`${parentNode}/${node.nodeName}`] = augmentXpathWithLocalNames(`${parentNode}/${node.nodeName}`);
50
74
  }
75
+ addMacrosItemsPathIfMissing(node, parentNode, macrosNamespace || 'macros', result);
51
76
  const childNodes = Array.from(node.childNodes);
52
77
  for (const childNode of childNodes) {
53
78
  if (childNode.nodeType === childNode.ELEMENT_NODE) {
@@ -1,5 +1,14 @@
1
1
  import type { IdGeneratorFunction } from '../common/file.js';
2
2
  import type { CustomElement, CustomFragment, EventHandler, FragmentContentData, Position } from '../common/types.js';
3
+ export declare const PageTemplateType: {
4
+ readonly Full: "full";
5
+ readonly Basic: "basic";
6
+ };
7
+ export type PageTemplateType = (typeof PageTemplateType)[keyof typeof PageTemplateType];
8
+ /** Minimum UI5 version required for page building blocks. */
9
+ export declare const MIN_UI5_VERSION_PAGE_BUILDING_BLOCK: "1.136.0";
10
+ /** Minimum UI5 version required for full layout page building blocks. */
11
+ export declare const MIN_UI5_VERSION_PAGE_BUILDING_BLOCK_FULL_LAYOUT: "1.145.0";
3
12
  /**
4
13
  * Building block type.
5
14
  *
@@ -380,8 +389,8 @@ export interface Page extends BuildingBlock {
380
389
  description?: string;
381
390
  /**
382
391
  * The template type for the page building block.
383
- * 'full' generates a full page template with all aggregations and controller stubs.
384
- * 'basic' generates a minimal self-closing tag (default behavior).
392
+ * 'full' generates a full page template with all aggregations.
393
+ * 'basic' generates a minimal self-closing <macros:Page/> with no aggregations.
385
394
  */
386
395
  templateType?: PageTemplateType;
387
396
  /**
@@ -390,9 +399,8 @@ export interface Page extends BuildingBlock {
390
399
  */
391
400
  aggregations?: Partial<Record<PageAggregationName, string>>;
392
401
  }
393
- export declare const PAGE_TEMPLATE_TYPE_FULL: "full";
394
- export declare const PAGE_TEMPLATE_TYPE_BASIC: "basic";
395
- export type PageTemplateType = typeof PAGE_TEMPLATE_TYPE_FULL | typeof PAGE_TEMPLATE_TYPE_BASIC;
402
+ export declare const PAGE_TEMPLATE_COMMENT = "This is a sample template, event handlers should be added for implementation";
403
+ export declare const MACROS_NAMESPACE_URI = "sap.fe.macros";
396
404
  /**
397
405
  * A group of XML nodes representing one Page aggregation element and its preceding sibling comments.
398
406
  * Used when re-ordering aggregation children under a macros:Page element.
@@ -412,8 +420,6 @@ export interface GenerateBuildingBlockAggregationConfig {
412
420
  buildingBlockType: BuildingBlockType;
413
421
  /** Name of the aggregation to append. */
414
422
  aggregationName: PageAggregationName;
415
- /** Optional inner XML content for the aggregation. */
416
- mContent?: string;
417
423
  }
418
424
  /**
419
425
  * Represents a custom filter to be used inside the FilterBar.
@@ -1,3 +1,11 @@
1
+ export const PageTemplateType = {
2
+ Full: 'full',
3
+ Basic: 'basic'
4
+ };
5
+ /** Minimum UI5 version required for page building blocks. */
6
+ export const MIN_UI5_VERSION_PAGE_BUILDING_BLOCK = '1.136.0';
7
+ /** Minimum UI5 version required for full layout page building blocks. */
8
+ export const MIN_UI5_VERSION_PAGE_BUILDING_BLOCK_FULL_LAYOUT = '1.145.0';
1
9
  /**
2
10
  * Building block type.
3
11
  *
@@ -37,6 +45,6 @@ export const PAGE_AGGREGATIONS = [
37
45
  'items',
38
46
  'footer'
39
47
  ];
40
- export const PAGE_TEMPLATE_TYPE_FULL = 'full';
41
- export const PAGE_TEMPLATE_TYPE_BASIC = 'basic';
48
+ export const PAGE_TEMPLATE_COMMENT = 'This is a sample template, event handlers should be added for implementation';
49
+ export const MACROS_NAMESPACE_URI = 'sap.fe.macros';
42
50
  //# sourceMappingURL=types.js.map
package/dist/index.d.ts CHANGED
@@ -21,9 +21,8 @@ export { enableFPM } from './app/index.js';
21
21
  export type { FPMConfig } from './app/index.js';
22
22
  export { validateBasePath, validateVersion } from './common/validate.js';
23
23
  export { createIdGenerator, type IdGeneratorFunction, getRelativeTemplateComponentPath } from './common/file.js';
24
- export { BuildingBlockType } from './building-block/types.js';
25
- export type { FilterBar, Form, Chart, Field, FieldFormatOptions, Table, BuildingBlockConfig, Page, CustomColumn, CustomFilterField, CustomFormField, RichTextEditor, ButtonGroupConfig, Action, PageTemplateType } from './building-block/types.js';
26
- export { PAGE_TEMPLATE_TYPE_FULL, PAGE_TEMPLATE_TYPE_BASIC, PAGE_AGGREGATIONS } from './building-block/types.js';
24
+ export { BuildingBlockType, PageTemplateType, PAGE_AGGREGATIONS, MIN_UI5_VERSION_PAGE_BUILDING_BLOCK, MIN_UI5_VERSION_PAGE_BUILDING_BLOCK_FULL_LAYOUT } from './building-block/types.js';
25
+ export type { FilterBar, Form, Chart, Field, FieldFormatOptions, Table, BuildingBlockConfig, Page, CustomColumn, CustomFilterField, CustomFormField, RichTextEditor, ButtonGroupConfig, Action } from './building-block/types.js';
27
26
  export type { PageAggregationName, GenerateBuildingBlockAggregationConfig } from './building-block/types.js';
28
27
  export { generateBuildingBlock, getSerializedFileContent, generateBuildingBlockAggregation } from './building-block/index.js';
29
28
  export type { ChartPromptsAnswer, FilterBarPromptsAnswer, FormPromptsAnswer, TablePromptsAnswer, PagePromptsAnswer, RichTextEditorPromptsAnswer, RichTextEditorButtonGroupsPromptsAnswer, BuildingBlockTypePromptsAnswer } from './building-block/prompts/questions/index.js';