Code coverage report for src/utils.js

Statements: 90.7% (39 / 43)      Branches: 83.87% (26 / 31)      Functions: 100% (2 / 2)      Lines: 90.7% (39 / 43)      Ignored: none     

All files » src/ » utils.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87                  1 6         6   3     3         3                 1 9 9 9   13 12 12 12 24 24 24 24 17               9 9   9 5 5 3   2         9 7 7 7 7 7 3   7 6   7 1   7 7      
/**
 * Created by knut on 14-11-23.
 */
/**
 * Detects the type of the graph text.
 * @param {string} text The text defining the graph
 * @param {string} text The second text defining the graph
 * @returns {string} A graph definition key
 */
module.exports.detectType = function(text,a){
    Iif(text.match(/^\s*sequenceDiagram/)){
        console.log('Detected sequenceDiagram syntax');
        return "sequenceDiagram";
    }
 
    if(text.match(/^\s*sequence/)){
        //console.log('Detected sequence syntax');
        return "sequence";
    }
 
    Iif(text.match(/^\s*digraph/)) {
        console.log('Detected flow syntax');
        return "dotGraph";
    }
 
    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
 */
module.exports.cloneCssStyles = function(svg, classes){
    var usedStyles = "";
    var sheets = document.styleSheets;
    for (var i = 0; i < sheets.length; i++) {
        // Avoid multiple inclusion on pages with multiple graphs
        if (sheets[i].title !== 'mermaid-svg-internal-css') {
            var rules = sheets[i].cssRules;
            Eif(rules !== null) {
                for (var j = 0; j < rules.length; j++) {
                    var rule = rules[j];
                    Eif (typeof(rule.style) !== 'undefined') {
                        var elems = svg.querySelectorAll(rule.selectorText);
                        if (elems.length > 0) {
                            usedStyles += rule.selectorText + " { " + rule.style.cssText + " }\n";
                        }
                    }
                }
            }
        } 
    }
 
    var defaultStyles = "";
    var embeddedStyles = "";
 
    for (var className in classes) {
        Eif (classes.hasOwnProperty(className) && typeof(className) != "undefined") {
            if (className === 'default') {
                defaultStyles = '.node' + ' { ' + classes[className].styles.join("; ") + '; }\n';
            } else {
                embeddedStyles += '.' + className + ' { ' + classes[className].styles.join("; ") + '; }\n';            
            }
        }
    }
 
    if (usedStyles !== "" || defaultStyles !== "" || embeddedStyles !== "") {
        var s = document.createElement('style');
        s.setAttribute('type', 'text/css');
        s.setAttribute('title', 'mermaid-svg-internal-css');
        s.innerHTML = "/* <![CDATA[ */\n";
        if (defaultStyles !== "") {
            s.innerHTML += defaultStyles;
        }
        if (usedStyles !== "") {
            s.innerHTML += usedStyles;
        }
        if (embeddedStyles !== "") {
            s.innerHTML += embeddedStyles;
        }
        s.innerHTML += "/* ]]> */\n";
        svg.insertBefore(s, svg.firstChild);
    }
};