diff --git a/src/utils.js b/src/utils.js index 4f661946c..6a00bf02e 100644 --- a/src/utils.js +++ b/src/utils.js @@ -80,8 +80,8 @@ var cloneCssStyles = function(svg, classes){ } } } - catch(err) { - if(rule !== 'undefined'){ + catch (err) { + if (typeof(rule) !== 'undefined') { log.warn('Invalid CSS selector "' + rule.selectorText + '"', err); } } @@ -134,4 +134,4 @@ var cloneCssStyles = function(svg, classes){ } }; -export {cloneCssStyles}; \ No newline at end of file +export {cloneCssStyles}; diff --git a/src/utils.spec.js b/src/utils.spec.js index 95cda44cd..a42771821 100644 --- a/src/utils.spec.js +++ b/src/utils.spec.js @@ -104,6 +104,20 @@ describe('when cloning CSS ', function () { document.styleSheets[styleSheetCount].title = 'mermaid-svg-internal-css'; } + it('should handle errors thrown when accessing CSS rules', function() { + var svg = document.createElement('svg'); + svg.setAttribute('id', 'mermaid-01'); + + // Firefox throws a SecurityError when trying to access cssRules + document.styleSheets[document.styleSheets.length++] = { + get cssRules() { throw new Error('SecurityError'); } + }; + + expect(function() { + utils.cloneCssStyles(svg, {}); + }).not.toThrow(); + }); + it('should handle an empty set of classes', function () { var svg = document.createElement('svg'); svg.setAttribute('id', 'mermaid-01');