mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-11-17 03:04:07 +01:00
Added class definitions to nodes
Removed the inline styling of nodes with classes, replaced with class attributes and a common CSS Style section at the start of the SVG.
This commit is contained in:
28
src/utils.js
28
src/utils.js
@@ -25,3 +25,31 @@ module.exports.detectType = function(text,a){
|
||||
|
||||
return "graph";
|
||||
};
|
||||
|
||||
/**
|
||||
* Copies all relevant CSS content into the graph SVG.
|
||||
* This allows the SVG to be copied as is while keeping class based styling
|
||||
* @param {element} svg The root element of the SVG
|
||||
* @param {string} defaultStyle Default style definitions (for elements without classes)
|
||||
*/
|
||||
module.exports.cloneCssStyles = function(svg, defaultStyle){
|
||||
var used = "";
|
||||
var sheets = document.styleSheets;
|
||||
for (var i = 0; i < sheets.length; i++) {
|
||||
var rules = sheets[i].cssRules;
|
||||
for (var j = 0; j < rules.length; j++) {
|
||||
var rule = rules[j];
|
||||
if (typeof(rule.style) != "undefined") {
|
||||
var elems = svg.querySelectorAll(rule.selectorText);
|
||||
if (elems.length > 0) {
|
||||
used += rule.selectorText + " { " + rule.style.cssText + " }\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var s = document.createElement('style');
|
||||
s.setAttribute('type', 'text/css');
|
||||
s.innerHTML = "/* <![CDATA[ */\n" + defaultStyle + "\n" + used + "\n/* ]]> */";
|
||||
svg.insertBefore(s, svg.firstChild);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user