From eef80ebaf2cbe16871f2ab53f458d6e8fa7af3f3 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Sun, 21 Sep 2014 01:16:25 +0200 Subject: [PATCH] Update parser.js --- src/parser.js | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/parser.js b/src/parser.js index f2f179c7..7c4e9956 100644 --- a/src/parser.js +++ b/src/parser.js @@ -45,6 +45,9 @@ fill: 'fillOpacity' }; + fabric.cssRules = { }; + fabric.gradientDefs = { }; + function normalizeAttr(attr) { // transform attribute names if (attr in attributesMap) { @@ -349,13 +352,12 @@ /** * @private */ - function getGlobalStylesForElement(element) { + function getGlobalStylesForElement(element, svgUid) { var styles = { }; - - for (var rule in fabric.cssRules) { + for (var rule in fabric.cssRules[svgUid]) { if (elementMatchesRule(element, rule.split(' '))) { - for (var property in fabric.cssRules[rule]) { - styles[property] = fabric.cssRules[rule][property]; + for (var property in fabric.cssRules[svgUid][rule]) { + styles[property] = fabric.cssRules[svgUid][rule][property]; } } } @@ -505,7 +507,8 @@ if (!doc) { return; } - var startTime = new Date(); + var startTime = new Date(), + svgUid = fabric.Object.__uid++; parseUseDirectives(doc); /* http://www.w3.org/TR/SVG/struct.html#SVGElementWidthAttribute @@ -560,11 +563,12 @@ width: widthAttr ? widthAttr : viewBoxWidth, height: heightAttr ? heightAttr : viewBoxHeight, widthAttr: widthAttr, - heightAttr: heightAttr + heightAttr: heightAttr, + svgUid: svgUid }; - fabric.gradientDefs = fabric.getGradientDefs(doc); - fabric.cssRules = fabric.getCSSRules(doc); + fabric.gradientDefs[svgUid] = fabric.getGradientDefs(doc); + fabric.cssRules[svgUid] = fabric.getCSSRules(doc); // Precedence of rules: style > class > attribute fabric.parseElements(elements, function(instances) { @@ -688,7 +692,7 @@ * @param {Array} attributes Array of attributes to parse * @return {Object} object containing parsed attributes' names/values */ - parseAttributes: function(element, attributes) { + parseAttributes: function(element, attributes, svgUid) { if (!element) { return; @@ -697,9 +701,12 @@ var value, parentAttributes = { }; + if (typeof svgUid === 'undefined') { + svgUid = element.getAttribute('svgUid'); + } // if there's a parent container (`g` or `a` or `symbol` node), parse its attributes recursively upwards if (element.parentNode && /^symbol|[g|a]$/i.test(element.parentNode.nodeName)) { - parentAttributes = fabric.parseAttributes(element.parentNode, attributes); + parentAttributes = fabric.parseAttributes(element.parentNode, attributes, svgUid); } var ownAttributes = attributes.reduce(function(memo, attr) { @@ -716,7 +723,7 @@ // add values parsed from style, which take precedence over attributes // (see: http://www.w3.org/TR/SVG/styling.html#UsingPresentationAttributes) ownAttributes = extend(ownAttributes, - extend(getGlobalStylesForElement(element), fabric.parseStyleAttribute(element))); + extend(getGlobalStylesForElement(element, svgUid), fabric.parseStyleAttribute(element))); return _setStrokeFillOpacity(extend(parentAttributes, ownAttributes)); },