demos/Collage/fabric.js
function getCSSRules(doc) {
    var styles = doc.getElementsByTagName('style'),
        allRules = { },
        rules;

    // very crude parsing of style contents
    for (var i = 0, len = styles.length; i < len; i++) {
      var styleContents = styles[0].textContent;

      // remove comments
      styleContents = styleContents.replace(/\/\*[\s\S]*?\*\//g, '');

      rules = styleContents.match(/[^{]*\{[\s\S]*?\}/g);
      rules = rules.map(function(rule) { return rule.trim() });

      rules.forEach(function(rule) {
        var match = rule.match(/([\s\S]*?)\s*\{([^}]*)\}/),
            rule = match[1],
            declaration = match[2].trim(),
            propertyValuePairs = declaration.replace(/;$/, '').split(/\s*;\s*/);

        if (!allRules[rule]) {
          allRules[rule] = { };
        }

        for (var i = 0, len = propertyValuePairs.length; i < len; i++) {
          var pair = propertyValuePairs[i].split(/\s*:\s*/),
              property = pair[0],
              value = pair[1];

          allRules[rule][property] = value;
        }
      });
    }

    return allRules;
  }
function getCSSRules(doc) {
    var styles = doc.getElementsByTagName('style'),
        allRules = { },
        rules;

    // very crude parsing of style contents
    for (var i = 0, len = styles.length; i < len; i++) {
      var styleContents = styles[0].textContent;

      // remove comments
      styleContents = styleContents.replace(/\/\*[\s\S]*?\*\//g, '');

      rules = styleContents.match(/[^{]*\{[\s\S]*?\}/g);
      rules = rules.map(function(rule) { return rule.trim() });

      rules.forEach(function(rule) {
        var match = rule.match(/([\s\S]*?)\s*\{([^}]*)\}/),
            rule = match[1],
            declaration = match[2].trim(),
            propertyValuePairs = declaration.replace(/;$/, '').split(/\s*;\s*/);

        if (!allRules[rule]) {
          allRules[rule] = { };
        }

        for (var i = 0, len = propertyValuePairs.length; i < len; i++) {
          var pair = propertyValuePairs[i].split(/\s*:\s*/),
              property = pair[0],
              value = pair[1];

          allRules[rule][property] = value;
        }
      });
    }

    return allRules;
  }
function getCSSRules ( doc ) { var styles = doc . getElementsByTagName ( 'style' ) , allRules = { } , rules ; // very crude parsing of style contents for ( var i = 0 , len = styles . length ; i < len ; i ++ ) { var styleContents = styles [ 0 ] . textContent ; // remove comments styleContents = styleContents . replace ( / \/\*[\s\S]*?\*\/ / g , '' ) ; rules = styleContents . match ( / [^{]*\{[\s\S]*?\} / g ) ; rules = rules . map ( function ( rule ) { return rule . trim ( ) } ) ; rules . forEach ( function ( rule ) { var match = rule . match ( / ([\s\S]*?)\s*\{([^}]*)\} / ) , rule = match [ 1 ] , declaration = match [ 2 ] . trim ( ) , propertyValuePairs = declaration . replace ( / ;$ / , '' ) . split ( / \s*;\s* / ) ; if ( ! allRules [ rule ] ) { allRules [ rule ] = { } ; } for ( var i = 0 , len = propertyValuePairs . length ; i < len ; i ++ ) { var pair = propertyValuePairs [ i ] . split ( / \s*:\s* / ) , property = pair [ 0 ] , value = pair [ 1 ] ; allRules [ rule ] [ property ] = value ; } } ) ; } return allRules ; }
Returns CSS rules for a given SVG document
@static
@function
@memberOf fabric
@method getCSSRules
@param {SVGDocument} doc SVG document to parse
@return {Object} CSS rules of this document
Returns CSS rules for a given SVG document