mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-01 06:36:40 +02:00
Remove clone css style code
This commit is contained in:
80
src/utils.js
80
src/utils.js
@@ -47,85 +47,6 @@ export const detectType = function (text) {
|
||||
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 {object} Hash table of class definitions from the graph definition
|
||||
*/
|
||||
export const cloneCssStyles = function (svg, classes) {
|
||||
let usedStyles = ''
|
||||
const sheets = document.styleSheets
|
||||
let rule
|
||||
for (let i = 0; i < sheets.length; i++) {
|
||||
// Avoid multiple inclusion on pages with multiple graphs
|
||||
if (sheets[i].title !== 'mermaid-svg-internal-css') {
|
||||
try {
|
||||
const rules = sheets[i].cssRules
|
||||
if (rules !== null) {
|
||||
for (let j = 0; j < rules.length; j++) {
|
||||
rule = rules[j]
|
||||
if (typeof (rule.style) !== 'undefined') {
|
||||
const elems = svg.querySelectorAll(rule.selectorText)
|
||||
if (elems.length > 0) {
|
||||
usedStyles += rule.selectorText + ' { ' + rule.style.cssText + '}\n'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (typeof (rule) !== 'undefined') {
|
||||
logger.warn('Invalid CSS selector "' + rule.selectorText + '"', err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let defaultStyles = ''
|
||||
let embeddedStyles = ''
|
||||
for (const className in classes) {
|
||||
if (classes.hasOwnProperty(className) && typeof (className) !== 'undefined') {
|
||||
if (className === 'default') {
|
||||
if (classes.default.styles instanceof Array) {
|
||||
defaultStyles += '#' + svg.id.trim() + ' .node' + '>rect { ' + classes[className].styles.join('; ') + '; }\n'
|
||||
}
|
||||
if (classes.default.nodeLabelStyles instanceof Array) {
|
||||
defaultStyles += '#' + svg.id.trim() + ' .node text ' + ' { ' + classes[className].nodeLabelStyles.join('; ') + '; }\n'
|
||||
}
|
||||
if (classes.default.edgeLabelStyles instanceof Array) {
|
||||
defaultStyles += '#' + svg.id.trim() + ' .edgeLabel text ' + ' { ' + classes[className].edgeLabelStyles.join('; ') + '; }\n'
|
||||
}
|
||||
if (classes.default.clusterStyles instanceof Array) {
|
||||
defaultStyles += '#' + svg.id.trim() + ' .cluster rect ' + ' { ' + classes[className].clusterStyles.join('; ') + '; }\n'
|
||||
}
|
||||
} else {
|
||||
if (classes[className].styles instanceof Array) {
|
||||
embeddedStyles += '#' + svg.id.trim() + ' .' + className + '>rect, .' + className + '>polygon, .' + className + '>circle, .' + className + '>ellipse { ' + classes[className].styles.join('; ') + '; }\n'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (usedStyles !== '' || defaultStyles !== '' || embeddedStyles !== '') {
|
||||
const s = document.createElement('style')
|
||||
s.setAttribute('type', 'text/css')
|
||||
s.setAttribute('title', 'mermaid-svg-internal-css')
|
||||
s.innerHTML = '/* <![CDATA[ */\n'
|
||||
// Make this CSS local to this SVG
|
||||
if (defaultStyles !== '') {
|
||||
s.innerHTML += defaultStyles
|
||||
}
|
||||
if (usedStyles !== '') {
|
||||
s.innerHTML += usedStyles
|
||||
}
|
||||
if (embeddedStyles !== '') {
|
||||
s.innerHTML += embeddedStyles
|
||||
}
|
||||
s.innerHTML += '/* ]]> */\n'
|
||||
svg.insertBefore(s, svg.firstChild)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @function isSubstringInArray
|
||||
* Detects whether a substring in present in a given array
|
||||
@@ -142,6 +63,5 @@ export const isSubstringInArray = function (str, arr) {
|
||||
|
||||
export default {
|
||||
detectType,
|
||||
cloneCssStyles,
|
||||
isSubstringInArray
|
||||
}
|
||||
|
Reference in New Issue
Block a user