-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4837 from mermaid-js/refactor/unifyEdgeMarkers
refactor: Unify the edgeMarker adding logic
- Loading branch information
Showing
4 changed files
with
141 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import type { Mocked } from 'vitest'; | ||
import type { SVG } from '../diagram-api/types.js'; | ||
import { addEdgeMarkers } from './edgeMarker.js'; | ||
|
||
describe('addEdgeMarker', () => { | ||
const svgPath = { | ||
attr: vitest.fn(), | ||
} as unknown as Mocked<SVG>; | ||
const url = 'http://example.com'; | ||
const id = 'test'; | ||
const diagramType = 'test'; | ||
|
||
beforeEach(() => { | ||
svgPath.attr.mockReset(); | ||
}); | ||
|
||
it('should add markers for arrow_cross:arrow_point', () => { | ||
const arrowTypeStart = 'arrow_cross'; | ||
const arrowTypeEnd = 'arrow_point'; | ||
addEdgeMarkers(svgPath, { arrowTypeStart, arrowTypeEnd }, url, id, diagramType); | ||
expect(svgPath.attr).toHaveBeenCalledWith( | ||
'marker-start', | ||
`url(${url}#${id}_${diagramType}-crossStart)` | ||
); | ||
expect(svgPath.attr).toHaveBeenCalledWith( | ||
'marker-end', | ||
`url(${url}#${id}_${diagramType}-pointEnd)` | ||
); | ||
}); | ||
|
||
it('should add markers for aggregation:arrow_point', () => { | ||
const arrowTypeStart = 'aggregation'; | ||
const arrowTypeEnd = 'arrow_point'; | ||
addEdgeMarkers(svgPath, { arrowTypeStart, arrowTypeEnd }, url, id, diagramType); | ||
expect(svgPath.attr).toHaveBeenCalledWith( | ||
'marker-start', | ||
`url(${url}#${id}_${diagramType}-aggregationStart)` | ||
); | ||
expect(svgPath.attr).toHaveBeenCalledWith( | ||
'marker-end', | ||
`url(${url}#${id}_${diagramType}-pointEnd)` | ||
); | ||
}); | ||
|
||
it('should add markers for arrow_point:aggregation', () => { | ||
const arrowTypeStart = 'arrow_point'; | ||
const arrowTypeEnd = 'aggregation'; | ||
addEdgeMarkers(svgPath, { arrowTypeStart, arrowTypeEnd }, url, id, diagramType); | ||
expect(svgPath.attr).toHaveBeenCalledWith( | ||
'marker-start', | ||
`url(${url}#${id}_${diagramType}-pointStart)` | ||
); | ||
expect(svgPath.attr).toHaveBeenCalledWith( | ||
'marker-end', | ||
`url(${url}#${id}_${diagramType}-aggregationEnd)` | ||
); | ||
}); | ||
|
||
it('should add markers for aggregation:composition', () => { | ||
const arrowTypeStart = 'aggregation'; | ||
const arrowTypeEnd = 'composition'; | ||
addEdgeMarkers(svgPath, { arrowTypeStart, arrowTypeEnd }, url, id, diagramType); | ||
expect(svgPath.attr).toHaveBeenCalledWith( | ||
'marker-start', | ||
`url(${url}#${id}_${diagramType}-aggregationStart)` | ||
); | ||
expect(svgPath.attr).toHaveBeenCalledWith( | ||
'marker-end', | ||
`url(${url}#${id}_${diagramType}-compositionEnd)` | ||
); | ||
}); | ||
|
||
it('should not add invalid markers', () => { | ||
const arrowTypeStart = 'this is an invalid marker'; | ||
const arrowTypeEnd = ') url(https://my-malicious-site.example)'; | ||
addEdgeMarkers(svgPath, { arrowTypeStart, arrowTypeEnd }, url, id, diagramType); | ||
expect(svgPath.attr).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import type { SVG } from '../diagram-api/types.js'; | ||
import { log } from '../logger.js'; | ||
import type { EdgeData } from '../types.js'; | ||
/** | ||
* Adds SVG markers to a path element based on the arrow types specified in the edge. | ||
* | ||
* @param svgPath - The SVG path element to add markers to. | ||
* @param edge - The edge data object containing the arrow types. | ||
* @param url - The URL of the SVG marker definitions. | ||
* @param id - The ID prefix for the SVG marker definitions. | ||
* @param diagramType - The type of diagram being rendered. | ||
*/ | ||
export const addEdgeMarkers = ( | ||
svgPath: SVG, | ||
edge: Pick<EdgeData, 'arrowTypeStart' | 'arrowTypeEnd'>, | ||
url: string, | ||
id: string, | ||
diagramType: string | ||
) => { | ||
if (edge.arrowTypeStart) { | ||
addEdgeMarker(svgPath, 'start', edge.arrowTypeStart, url, id, diagramType); | ||
} | ||
if (edge.arrowTypeEnd) { | ||
addEdgeMarker(svgPath, 'end', edge.arrowTypeEnd, url, id, diagramType); | ||
} | ||
}; | ||
|
||
const arrowTypesMap = { | ||
arrow_cross: 'cross', | ||
arrow_point: 'point', | ||
arrow_barb: 'barb', | ||
arrow_circle: 'circle', | ||
aggregation: 'aggregation', | ||
extension: 'extension', | ||
composition: 'composition', | ||
dependency: 'dependency', | ||
lollipop: 'lollipop', | ||
} as const; | ||
|
||
const addEdgeMarker = ( | ||
svgPath: SVG, | ||
position: 'start' | 'end', | ||
arrowType: string, | ||
url: string, | ||
id: string, | ||
diagramType: string | ||
) => { | ||
const endMarkerType = arrowTypesMap[arrowType as keyof typeof arrowTypesMap]; | ||
|
||
if (!endMarkerType) { | ||
log.warn(`Unknown arrow type: ${arrowType}`); | ||
return; // unknown arrow type, ignore | ||
} | ||
|
||
const suffix = position === 'start' ? 'Start' : 'End'; | ||
svgPath.attr(`marker-${position}`, `url(${url}#${id}_${diagramType}-${endMarkerType}${suffix})`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters