Skip to content

Commit

Permalink
Use stylis to prepend idSelector
Browse files Browse the repository at this point in the history
code previously manually inserted idSelector before the generated CSS.
This could produce invalid CSS. Stylis seems permissive about this,
but it fails if there is no selector at all. (e.g. "{...props...}")

Adding & in front of rules will ensure that it behaves properly.

We should probably do this for each diagram's style.ts files as well
  • Loading branch information
DanInProgress committed Nov 21, 2022
1 parent 09ed41b commit 0c3be9f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
10 changes: 5 additions & 5 deletions docs/config/setup/modules/mermaidAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ mermaid.initialize(config);

#### Defined in

[mermaidAPI.ts:949](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L949)
[mermaidAPI.ts:950](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L950)

## Functions

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

#### Defined in

[mermaidAPI.ts:292](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L292)
[mermaidAPI.ts:293](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L293)

---

Expand All @@ -137,7 +137,7 @@ the cleaned up svgCode

#### Defined in

[mermaidAPI.ts:243](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L243)
[mermaidAPI.ts:244](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L244)

---

Expand Down Expand Up @@ -279,7 +279,7 @@ Put the svgCode into an iFrame. Return the iFrame code

#### Defined in

[mermaidAPI.ts:271](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L271)
[mermaidAPI.ts:272](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L272)

---

Expand All @@ -305,4 +305,4 @@ Remove any existing elements from the given document

#### Defined in

[mermaidAPI.ts:343](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L343)
[mermaidAPI.ts:344](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L344)
5 changes: 3 additions & 2 deletions packages/mermaid/src/mermaidAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export const createUserStyles = (
const userCSSstyles = createCssStyles(config, graphType, classDefs);
const allStyles = getStyles(graphType, userCSSstyles, config.themeVariables);


// Now turn all of the styles into a (compiled) string that starts with the id
// use the stylis library to compile the css, turn the results into a valid CSS string (serialize(...., stringify))
// @see https://github.com/thysultan/stylis
Expand Down Expand Up @@ -510,7 +511,7 @@ const render = function (
);

const style1 = document.createElement('style');
style1.innerHTML = `${idSelector} ` + rules;
style1.innerHTML = rules;
svg.insertBefore(style1, firstChild);

// -------------------------------------------------------------------------------
Expand Down Expand Up @@ -706,7 +707,7 @@ const renderAsync = async function (
);

const style1 = document.createElement('style');
style1.innerHTML = `${idSelector} ` + rules;
style1.innerHTML = rules;
svg.insertBefore(style1, firstChild);

// -------------------------------------------------------------------------------
Expand Down
20 changes: 10 additions & 10 deletions packages/mermaid/src/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,48 +53,48 @@ const getStyles = (
} else {
log.warn(`No theme found for ${type}`);
}
return ` {
return ` & {
font-family: ${options.fontFamily};
font-size: ${options.fontSize};
fill: ${options.textColor}
}
/* Classes common for multiple diagrams */
.error-icon {
& .error-icon {
fill: ${options.errorBkgColor};
}
.error-text {
& .error-text {
fill: ${options.errorTextColor};
stroke: ${options.errorTextColor};
}
.edge-thickness-normal {
& .edge-thickness-normal {
stroke-width: 2px;
}
.edge-thickness-thick {
& .edge-thickness-thick {
stroke-width: 3.5px
}
.edge-pattern-solid {
& .edge-pattern-solid {
stroke-dasharray: 0;
}
.edge-pattern-dashed{
& .edge-pattern-dashed{
stroke-dasharray: 3;
}
.edge-pattern-dotted {
stroke-dasharray: 2;
}
.marker {
& .marker {
fill: ${options.lineColor};
stroke: ${options.lineColor};
}
.marker.cross {
& .marker.cross {
stroke: ${options.lineColor};
}
svg {
& svg {
font-family: ${options.fontFamily};
font-size: ${options.fontSize};
}
Expand Down

0 comments on commit 0c3be9f

Please sign in to comment.