Skip to content

Commit d3063a3

Browse files
Assemble links (#19)
* Assembling links instead of providing pre-rendered renderedLabel for class relationships * Assembling links instead of providing pre-rendered URL for package relationships and Disharmony charts * Assembling links instead of providing pre-rendered URL for package relationships and Disharmony tables * Assembling links instead of providing pre-rendered URL for the largest cycle breakdown table and applied Coderabbit suggestions * Applied Coderabbit suggestions
1 parent 97a7a86 commit d3063a3

12 files changed

Lines changed: 5475 additions & 3480 deletions

‎assets/refactor-first-report.mustache‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@
458458
<caption>Class relationships to remove, in priority order</caption>
459459
<thead>
460460
<tr>
461-
<th scope="col"{{#classRelationshipsToRemove.tableUi.sortable}} aria-sort="{{classRelationshipsToRemove.tableUi.colSort.0}}"{{/classRelationshipsToRemove.tableUi.sortable}}><button type="button" class="rf-sort-btn" data-rf-sort="class-relationships" data-sort-key="renderedLabel">Class Relationship <span class="rf-sort-indicator" aria-hidden="true">{{classRelationshipsToRemove.tableUi.colIndicator.0}}</span></button></th>
461+
<th scope="col"{{#classRelationshipsToRemove.tableUi.sortable}} aria-sort="{{classRelationshipsToRemove.tableUi.colSort.0}}"{{/classRelationshipsToRemove.tableUi.sortable}}><button type="button" class="rf-sort-btn" data-rf-sort="class-relationships" data-sort-key="classRelationship">Class Relationship <span class="rf-sort-indicator" aria-hidden="true">{{classRelationshipsToRemove.tableUi.colIndicator.0}}</span></button></th>
462462
<th scope="col"{{#classRelationshipsToRemove.tableUi.sortable}} aria-sort="{{classRelationshipsToRemove.tableUi.colSort.1}}"{{/classRelationshipsToRemove.tableUi.sortable}}><button type="button" class="rf-sort-btn" data-rf-sort="class-relationships" data-sort-key="priority">Priority <span class="rf-sort-indicator" aria-hidden="true">{{classRelationshipsToRemove.tableUi.colIndicator.1}}</span></button></th>
463463
<th scope="col"{{#classRelationshipsToRemove.tableUi.sortable}} aria-sort="{{classRelationshipsToRemove.tableUi.colSort.2}}"{{/classRelationshipsToRemove.tableUi.sortable}}><button type="button" class="rf-sort-btn" data-rf-sort="class-relationships" data-sort-key="cycleCount">In Class<br>Cycles <span class="rf-sort-indicator" aria-hidden="true">{{classRelationshipsToRemove.tableUi.colIndicator.2}}</span></button></th>
464464
<th scope="col"{{#classRelationshipsToRemove.tableUi.sortable}} aria-sort="{{classRelationshipsToRemove.tableUi.colSort.3}}"{{/classRelationshipsToRemove.tableUi.sortable}}><button type="button" class="rf-sort-btn" data-rf-sort="class-relationships" data-sort-key="effortRank">Relationship<br>Strength <span class="rf-sort-indicator" aria-hidden="true">{{classRelationshipsToRemove.tableUi.colIndicator.3}}</span></button></th>
@@ -569,7 +569,7 @@
569569
<caption>Package relationships to remove, in priority order</caption>
570570
<thead>
571571
<tr>
572-
<th scope="col"{{#packageRelationshipsToRemove.tableUi.sortable}} aria-sort="{{packageRelationshipsToRemove.tableUi.colSort.0}}"{{/packageRelationshipsToRemove.tableUi.sortable}}><button type="button" class="rf-sort-btn" data-rf-sort="package-relationships" data-sort-key="renderedLabel">Package Relationship <span class="rf-sort-indicator" aria-hidden="true">{{packageRelationshipsToRemove.tableUi.colIndicator.0}}</span></button></th>
572+
<th scope="col"{{#packageRelationshipsToRemove.tableUi.sortable}} aria-sort="{{packageRelationshipsToRemove.tableUi.colSort.0}}"{{/packageRelationshipsToRemove.tableUi.sortable}}><button type="button" class="rf-sort-btn" data-rf-sort="package-relationships" data-sort-key="packageRelationship">Package Relationship <span class="rf-sort-indicator" aria-hidden="true">{{packageRelationshipsToRemove.tableUi.colIndicator.0}}</span></button></th>
573573
<th scope="col"{{#packageRelationshipsToRemove.tableUi.sortable}} aria-sort="{{packageRelationshipsToRemove.tableUi.colSort.1}}"{{/packageRelationshipsToRemove.tableUi.sortable}}><button type="button" class="rf-sort-btn" data-rf-sort="package-relationships" data-sort-key="priority">Priority <span class="rf-sort-indicator" aria-hidden="true">{{packageRelationshipsToRemove.tableUi.colIndicator.1}}</span></button></th>
574574
<th scope="col"{{#packageRelationshipsToRemove.tableUi.sortable}} aria-sort="{{packageRelationshipsToRemove.tableUi.colSort.2}}"{{/packageRelationshipsToRemove.tableUi.sortable}}><button type="button" class="rf-sort-btn" data-rf-sort="package-relationships" data-sort-key="cycleCount">In Pkg<br>Cycles <span class="rf-sort-indicator" aria-hidden="true">{{packageRelationshipsToRemove.tableUi.colIndicator.2}}</span></button></th>
575575
<th scope="col"{{#packageRelationshipsToRemove.tableUi.sortable}} aria-sort="{{packageRelationshipsToRemove.tableUi.colSort.3}}"{{/packageRelationshipsToRemove.tableUi.sortable}}><button type="button" class="rf-sort-btn" data-rf-sort="package-relationships" data-sort-key="effortRank">Relationship<br>Strength <span class="rf-sort-indicator" aria-hidden="true">{{packageRelationshipsToRemove.tableUi.colIndicator.3}}</span></button></th>
@@ -805,7 +805,7 @@
805805
<tbody>
806806
{{#classCycles.largestCycle.breakdown}}
807807
<tr>
808-
<td class="rf-text-left">{{{className}}}</td>
808+
<td class="rf-text-left">{{{safeClassName}}}</td>
809809
<td class="rf-text-left">{{{edgesHtml}}}</td>
810810
</tr>
811811
{{/classCycles.largestCycle.breakdown}}

‎lib/renderer.js‎

Lines changed: 200 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// sanitizes the final HTML.
66
import Mustache from 'mustache';
77
import DOMPurify from 'dompurify';
8+
import { sourceUrl } from './utils.js';
89
import {
910
TABLE_CONFIG,
1011
REPORT_TABLES,
@@ -67,6 +68,157 @@ function sanitizeRelationshipLabel(label) {
6768
});
6869
}
6970

71+
/**
72+
* Escapes a value for safe interpolation into HTML text or a double-quoted
73+
* attribute. Relationship fields (repoUrl, class paths, simple names) are
74+
* untrusted report data, so they are escaped before the markup is built.
75+
*
76+
* @param {*} value - Raw value from the report JSON.
77+
* @returns {string} HTML-escaped text.
78+
*/
79+
function escapeHtml(value) {
80+
return String(value ?? '')
81+
.replace(/&/g, '&amp;')
82+
.replace(/</g, '&lt;')
83+
.replace(/>/g, '&gt;')
84+
.replace(/"/g, '&quot;')
85+
.replace(/'/g, '&#39;');
86+
}
87+
88+
const RELATIONSHIP_ARROW = ' &#8594; ';
89+
90+
/**
91+
* Builds the two-anchor markup shared by class and package relationships:
92+
* linked source name (plus removal marker), the &#8594; arrow, then the linked
93+
* target name (plus removal marker). All values are untrusted report data and
94+
* are escaped before the markup is assembled.
95+
*
96+
* @param {string} repoUrl - Repository URL prefix from project metadata.
97+
* @param {{path: *, name: *, marked: *}} source - Source path, name and marker.
98+
* @param {{path: *, name: *, marked: *}} target - Target path, name and marker.
99+
* @returns {string} The unsanitized relationship markup.
100+
*/
101+
function relationshipAnchorsMarkup(repoUrl, source, target) {
102+
const anchor = (relationshipPath, name) =>
103+
'<a href="' + escapeHtml(sourceUrl(repoUrl, String(relationshipPath ?? '')) ?? '') + '" target="_blank">'
104+
+ escapeHtml(name) + '</a>';
105+
return anchor(source.path, source.name)
106+
+ (source.marked ? '*' : '')
107+
+ RELATIONSHIP_ARROW
108+
+ anchor(target.path, target.name)
109+
+ (target.marked ? '*' : '');
110+
}
111+
112+
/**
113+
* Builds the raw markup of one class relationship from the structured
114+
* ClassRelationshipDTO fields: the hyperlink targets are project.repoUrl plus
115+
* sourceClassPath/targetClassPath (paths relative to the project root), the
116+
* anchor texts are the simple class names, and the two anchors are separated
117+
* by the &#8594; arrow. A class marked for removal is followed by *.
118+
*
119+
* Entries without the structured fields (pre-DTO strings, legacy entries
120+
* carrying only a renderedLabel) yield null so callers can drop them.
121+
*
122+
* @param {*} relationship - Raw ClassRelationshipDTO entry.
123+
* @param {string} repoUrl - Repository URL prefix from project metadata.
124+
* @returns {string|null} The unsanitized relationship markup, or null when the
125+
* entry is not a structured ClassRelationshipDTO.
126+
*/
127+
export function classRelationshipMarkup(relationship, repoUrl) {
128+
if (!relationship || typeof relationship !== 'object') return null;
129+
const hasStructuredFields = relationship.simpleSourceClassName != null
130+
|| relationship.simpleTargetClassName != null;
131+
if (!hasStructuredFields) return null;
132+
return relationshipAnchorsMarkup(
133+
repoUrl,
134+
{ path: relationship.sourceClassPath, name: relationship.simpleSourceClassName, marked: relationship.sourceMarked },
135+
{ path: relationship.targetClassPath, name: relationship.simpleTargetClassName, marked: relationship.targetMarked });
136+
}
137+
138+
/**
139+
* Builds the raw markup of one package relationship from the structured
140+
* PackageRelationshipDTO fields: the hyperlink targets are project.repoUrl plus
141+
* sourcePackagePath/targetPackagePath (package directories relative to the
142+
* project root), and the anchor texts are the package names themselves. As with
143+
* class relationships, the two anchors are separated by the &#8594; arrow and
144+
* a package marked for removal is followed by *.
145+
*
146+
* @param {*} relationship - Raw PackageRelationshipDTO entry.
147+
* @param {string} repoUrl - Repository URL prefix from project metadata.
148+
* @returns {string|null} The unsanitized relationship markup, or null when the
149+
* entry is not a structured PackageRelationshipDTO.
150+
*/
151+
export function packageRelationshipMarkup(relationship, repoUrl) {
152+
if (!relationship || typeof relationship !== 'object') return null;
153+
const hasStructuredFields = relationship.sourcePackage != null
154+
|| relationship.targetPackage != null;
155+
if (!hasStructuredFields) return null;
156+
return relationshipAnchorsMarkup(
157+
repoUrl,
158+
{ path: relationship.sourcePackagePath, name: relationship.sourcePackage, marked: relationship.sourceMarked },
159+
{ path: relationship.targetPackagePath, name: relationship.targetPackage, marked: relationship.targetMarked });
160+
}
161+
162+
/**
163+
* Builds the sanitized class-relationship label for template interpolation.
164+
* The markup is derived from untrusted report data, so it is still
165+
* restricted to links and markers before it reaches the template.
166+
*
167+
* @param {*} relationship - Raw ClassRelationshipDTO entry.
168+
* @param {string} repoUrl - Repository URL prefix from project metadata.
169+
* @returns {string} Sanitized label markup ('' when the entry has no fields).
170+
*/
171+
function safeClassRelationshipLabel(relationship, repoUrl) {
172+
registerLinkSafetyHook();
173+
const markup = classRelationshipMarkup(relationship, repoUrl);
174+
return markup == null ? '' : sanitizeRelationshipLabel(markup);
175+
}
176+
177+
/**
178+
* Builds the source-link markup for one disharmony table cell that carries a
179+
* path relative to the project root: the hyperlink target is project.repoUrl
180+
* plus the path, and the cell's existing text becomes the anchor text. The
181+
* markup is sanitized down to a hardened link before it reaches the template;
182+
* cells without a path are passed through untouched.
183+
*
184+
* @param {object} cell - Raw disharmony table cell.
185+
* @param {string} repoUrl - Repository URL prefix from project metadata.
186+
* @returns {object} The cell, or a copy whose content is the sanitized link.
187+
*/
188+
function prepareDisharmonyCell(cell, repoUrl) {
189+
if (!cell || typeof cell !== 'object' || cell.path == null) return cell;
190+
registerLinkSafetyHook();
191+
const markup = '<a href="' + escapeHtml(sourceUrl(repoUrl, String(cell.path)) ?? '') + '" target="_blank">'
192+
+ escapeHtml(cell.content) + '</a>';
193+
return { ...cell, content: sanitizeRelationshipLabel(markup) };
194+
}
195+
196+
/**
197+
* Builds the sanitized class-cell markup for one largest-cycle breakdown row
198+
* from the structured CycleBreakdownRowDTO fields: the hyperlink target is
199+
* project.repoUrl plus classPath, the anchor text is the simple class name,
200+
* and a class marked for removal is followed by *. Rows without a path render
201+
* as plain names (with the marker); the markup is sanitized to links and
202+
* markers before it reaches the template.
203+
*
204+
* @param {object} row - Raw breakdown row.
205+
* @param {string} repoUrl - Repository URL prefix from project metadata.
206+
* @returns {object} Row copy with a restricted safeClassName label.
207+
*/
208+
function prepareCycleBreakdownRow(row, repoUrl) {
209+
if (!row || typeof row !== 'object') return row;
210+
registerLinkSafetyHook();
211+
let markup;
212+
if (row.classPath != null) {
213+
markup = '<a href="' + escapeHtml(sourceUrl(repoUrl, String(row.classPath)) ?? '') + '" target="_blank">'
214+
+ escapeHtml(row.className) + '</a>';
215+
} else {
216+
markup = escapeHtml(row.className);
217+
}
218+
if (row.marked) markup += '*';
219+
return { ...row, safeClassName: sanitizeRelationshipLabel(markup) };
220+
}
221+
70222
/**
71223
* Adapts one "class relationship to break a package cycle" entry for the
72224
* template. Reports serialize structured ClassRelationshipDTO objects, and
@@ -75,30 +227,50 @@ function sanitizeRelationshipLabel(label) {
75227
* of any other shape (including pre-DTO rendered-label strings) are dropped.
76228
*
77229
* @param {*} entry - Raw class-break entry.
230+
* @param {string} repoUrl - Repository URL prefix from project metadata.
78231
* @returns {string|null} Sanitized label markup, or null when the entry is
79232
* not a valid ClassRelationshipDTO object.
80233
*/
81-
function prepareClassBreakEntry(entry) {
234+
function prepareClassBreakEntry(entry, repoUrl) {
82235
if (!entry || typeof entry !== 'object') return null;
83-
return sanitizeRelationshipLabel(entry.renderedLabel);
236+
const markup = classRelationshipMarkup(entry, repoUrl);
237+
return markup == null ? null : sanitizeRelationshipLabel(markup);
238+
}
239+
240+
/**
241+
* Copies a class relationship with its label markup built from repoUrl, the
242+
* class paths and the simple class names, restricted to links and markers.
243+
* Never trust a precomputed value from JSON.
244+
*
245+
* @param {object} relationship - Raw class relationship data.
246+
* @param {string} repoUrl - Repository URL prefix from project metadata.
247+
* @returns {object} Relationship with a restricted HTML label.
248+
*/
249+
function prepareClassRelationship(relationship, repoUrl) {
250+
return {
251+
...relationship,
252+
safeRenderedLabel: safeClassRelationshipLabel(relationship, repoUrl)
253+
};
84254
}
85255

86256
/**
87-
* Copies a relationship with label markup limited to source links and markers,
88-
* and normalizes its class-break entries the same way. Never trust a
89-
* precomputed value from JSON.
257+
* Copies a package relationship with label markup limited to links and
258+
* markers, and normalizes its class-break entries the same way. Never trust
259+
* a precomputed value from JSON.
90260
*
91-
* @param {object} relationship - Raw relationship data.
261+
* @param {object} relationship - Raw package relationship data.
262+
* @param {string} repoUrl - Repository URL prefix from project metadata.
92263
* @returns {object} Relationship with restricted HTML labels.
93264
*/
94-
function prepareRelationship(relationship) {
265+
function preparePackageRelationship(relationship, repoUrl) {
95266
registerLinkSafetyHook();
267+
const markup = packageRelationshipMarkup(relationship, repoUrl);
96268
return {
97269
...relationship,
98-
safeRenderedLabel: sanitizeRelationshipLabel(relationship.renderedLabel),
270+
safeRenderedLabel: markup == null ? '' : sanitizeRelationshipLabel(markup),
99271
classRelationshipsToBreakPackage: Array.isArray(relationship.classRelationshipsToBreakPackage)
100272
? relationship.classRelationshipsToBreakPackage
101-
.map(prepareClassBreakEntry)
273+
.map(entry => prepareClassBreakEntry(entry, repoUrl))
102274
.filter(entry => entry !== null)
103275
: []
104276
};
@@ -218,13 +390,15 @@ export function prepareReportData(data, tableStates = {}, config = TABLE_CONFIG)
218390
const result = { ...data };
219391
const stateOf = tableId => tableStates?.[tableId];
220392

393+
const repoUrl = data.project?.repoUrl;
394+
221395
if (Array.isArray(data.classRelationshipsToRemove?.relationships)) {
222396
const descriptor = REPORT_TABLES['class-relationships'];
223397
const { rows, ui } = buildTableSlice(
224398
descriptor, data.classRelationshipsToRemove.relationships, stateOf(descriptor.id), config);
225399
result.classRelationshipsToRemove = {
226400
...data.classRelationshipsToRemove,
227-
relationships: rows.map(prepareRelationship),
401+
relationships: rows.map(row => prepareClassRelationship(row, repoUrl)),
228402
tableUi: ui
229403
};
230404
}
@@ -235,7 +409,7 @@ export function prepareReportData(data, tableStates = {}, config = TABLE_CONFIG)
235409
descriptor, data.packageRelationshipsToRemove.relationships, stateOf(descriptor.id), config);
236410
result.packageRelationshipsToRemove = {
237411
...data.packageRelationshipsToRemove,
238-
relationships: rows.map(prepareRelationship),
412+
relationships: rows.map(row => preparePackageRelationship(row, repoUrl)),
239413
tableUi: ui
240414
};
241415
}
@@ -252,7 +426,20 @@ export function prepareReportData(data, tableStates = {}, config = TABLE_CONFIG)
252426
indicator: ui.colIndicator[columnIndex] ?? '',
253427
tableId: ui.tableId
254428
}));
255-
return { ...disharmony, ui, table: { ...disharmony.table, headerObjs, rows } };
429+
return {
430+
...disharmony,
431+
ui,
432+
table: {
433+
...disharmony.table,
434+
headerObjs,
435+
rows: rows.map(row => ({
436+
...row,
437+
cells: Array.isArray(row.cells)
438+
? row.cells.map(cell => prepareDisharmonyCell(cell, repoUrl))
439+
: row.cells
440+
}))
441+
}
442+
};
256443
});
257444
}
258445

@@ -268,7 +455,7 @@ export function prepareReportData(data, tableStates = {}, config = TABLE_CONFIG)
268455
breakdownDescriptor, largestCycle.breakdown, stateOf(breakdownDescriptor.id), config);
269456
classCycles.largestCycle = {
270457
...largestCycle,
271-
breakdown: breakdown.rows,
458+
breakdown: breakdown.rows.map(row => prepareCycleBreakdownRow(row, repoUrl)),
272459
breakdownUi: breakdown.ui
273460
};
274461
}

0 commit comments

Comments
 (0)