Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve info HTML and Document assignment #4514

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/config/setup/modules/mermaidAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ mermaid.initialize(config);

#### Defined in

[mermaidAPI.ts:667](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L667)
[mermaidAPI.ts:669](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L669)

## Functions

Expand Down Expand Up @@ -127,7 +127,7 @@ Return the last node appended

#### Defined in

[mermaidAPI.ts:308](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L308)
[mermaidAPI.ts:310](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L310)

---

Expand Down Expand Up @@ -320,4 +320,4 @@ Remove any existing elements from the given document

#### Defined in

[mermaidAPI.ts:358](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L358)
[mermaidAPI.ts:360](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L360)
6 changes: 4 additions & 2 deletions packages/mermaid/src/diagram-api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ export type DrawDefinition = (
*/
export type ParseDirectiveDefinition = (statement: string, context: string, type: string) => void;

export type HTML = d3.Selection<HTMLIFrameElement, unknown, Element, unknown>;
export type HTML = d3.Selection<HTMLIFrameElement, unknown, Element | null, unknown>;

export type SVG = d3.Selection<SVGSVGElement, unknown, Element, unknown>;
export type SVG = d3.Selection<SVGSVGElement, unknown, Element | null, unknown>;
Yokozuna59 marked this conversation as resolved.
Show resolved Hide resolved

export type Group = d3.Selection<SVGGElement, unknown, Element | null, unknown>;
nirname marked this conversation as resolved.
Show resolved Hide resolved

export type DiagramStylesProvider = (options?: any) => string;
50 changes: 15 additions & 35 deletions packages/mermaid/src/diagrams/info/infoRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { select } from 'd3';
import { log } from '../../logger.js';
import { getConfig } from '../../config.js';
import type { DrawDefinition, HTML, SVG } from '../../diagram-api/types.js';
import { configureSvgSize } from '../../setupGraphViewbox.js';
import type { DrawDefinition, Group, SVG } from '../../diagram-api/types.js';
import { selectSvgElement } from '../../rendering-util/selectSvgElement.js';

/**
* Draws a an info picture in the tag with id: id based on the graph definition in text.
Expand All @@ -11,40 +11,20 @@ import type { DrawDefinition, HTML, SVG } from '../../diagram-api/types.js';
* @param version - MermaidJS version.
*/
const draw: DrawDefinition = (text, id, version) => {
try {
log.debug('rendering info diagram\n' + text);
log.debug('rendering info diagram\n' + text);

nirname marked this conversation as resolved.
Show resolved Hide resolved
const { securityLevel } = getConfig();
// handle root and document for when rendering in sandbox mode
let sandboxElement: HTML | undefined;
let document: Document | null | undefined;
if (securityLevel === 'sandbox') {
sandboxElement = select('#i' + id);
document = sandboxElement.nodes()[0].contentDocument;
}
const svg: SVG = selectSvgElement(id);
configureSvgSize(svg, 100, 400, true);

// @ts-ignore - figure out how to assign HTML to document type
const root: HTML =
sandboxElement !== undefined && document !== undefined && document !== null
? select(document)
: select('body');

const svg: SVG = root.select('#' + id);
svg.attr('height', 100);
svg.attr('width', 400);

const g = svg.append('g');

g.append('text') // text label for the x axis
.attr('x', 100)
.attr('y', 40)
.attr('class', 'version')
.attr('font-size', '32px')
.style('text-anchor', 'middle')
.text('v ' + version);
} catch (e) {
log.error('error while rendering info diagram', e);
}
const group: Group = svg.append('g');
group
.append('text')
.attr('x', 100)
.attr('y', 40)
.attr('class', 'version')
.attr('font-size', 32)
.style('text-anchor', 'middle')
.text(`v${version}`);
};

export const renderer = { draw };
4 changes: 3 additions & 1 deletion packages/mermaid/src/mermaidAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,9 @@
* TODO replace btoa(). Replace with buf.toString('base64')?
*/
export const putIntoIFrame = (svgCode = '', svgElement?: D3Element): string => {
const height = svgElement ? svgElement.viewBox.baseVal.height + 'px' : IFRAME_HEIGHT;
const height = svgElement?.viewBox?.baseVal?.height
? svgElement.viewBox.baseVal.height + 'px'
: IFRAME_HEIGHT;

Check warning on line 290 in packages/mermaid/src/mermaidAPI.ts

View check run for this annotation

Codecov / codecov/patch

packages/mermaid/src/mermaidAPI.ts#L289-L290

Added lines #L289 - L290 were not covered by tests
Yokozuna59 marked this conversation as resolved.
Show resolved Hide resolved
const base64encodedSrc = btoa('<body style="' + IFRAME_BODY_STYLE + '">' + svgCode + '</body>');
return `<iframe style="width:${IFRAME_WIDTH};height:${height};${IFRAME_STYLES}" src="data:text/html;base64,${base64encodedSrc}" sandbox="${IFRAME_SANDBOX_OPTS}">
${IFRAME_NOT_SUPPORTED_MSG}
Expand Down
22 changes: 22 additions & 0 deletions packages/mermaid/src/rendering-util/selectSvgElement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { select } from 'd3';
import { getConfig } from '../config.js';
import type { HTML, SVG } from '../diagram-api/types.js';

/**
* Selects the SVG element using {@link id}.
*
* @param id - The diagram ID.
* @returns The selected {@link SVG} element using {@link id}.
*/
export const selectSvgElement = (id: string): SVG => {
const { securityLevel } = getConfig();
// handle root and document for when rendering in sandbox mode
let root: HTML = select('body');
if (securityLevel === 'sandbox') {
const sandboxElement: HTML = select(`#i${id}`);

Check warning on line 16 in packages/mermaid/src/rendering-util/selectSvgElement.ts

View check run for this annotation

Codecov / codecov/patch

packages/mermaid/src/rendering-util/selectSvgElement.ts#L16

Added line #L16 was not covered by tests
const doc: Document = sandboxElement.node()?.contentDocument ?? document;
root = select(doc.body as HTMLIFrameElement);

Check warning on line 18 in packages/mermaid/src/rendering-util/selectSvgElement.ts

View check run for this annotation

Codecov / codecov/patch

packages/mermaid/src/rendering-util/selectSvgElement.ts#L18

Added line #L18 was not covered by tests
}
const svg: SVG = root.select(`#${id}`);
return svg;
};
3 changes: 2 additions & 1 deletion packages/mermaid/src/setupGraphViewbox.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { log } from './logger.js';
import { SVG } from './diagram-api/types.js';

/**
* Applies d3 attributes
Expand Down Expand Up @@ -35,7 +36,7 @@ export const calculateSvgSizeAttrs = function (height, width, useMaxWidth) {
/**
* Applies attributes from `calculateSvgSizeAttrs`
*
* @param {SVGSVGElement} svgElem The SVG Element to configure
* @param {SVG} svgElem The SVG Element to configure
* @param {number} height The height of the SVG
* @param {number} width The width of the SVG
* @param {boolean} useMaxWidth Whether or not to use max-width and set width to 100%
Expand Down