Fix a bug in CSS cloning error handler

Firefox throws SecurityError when `cssRules` is accessed, which exposes
this bug.

Fixes #245
This commit is contained in:
Tomasz Szczęśniak-Szlagowski
2015-11-05 19:38:36 +00:00
parent 17be68f5d9
commit ef290796e0
2 changed files with 17 additions and 3 deletions

View File

@@ -80,8 +80,8 @@ var cloneCssStyles = function(svg, classes){
} }
} }
} }
catch(err) { catch (err) {
if(rule !== 'undefined'){ if (typeof(rule) !== 'undefined') {
log.warn('Invalid CSS selector "' + rule.selectorText + '"', err); log.warn('Invalid CSS selector "' + rule.selectorText + '"', err);
} }
} }
@@ -134,4 +134,4 @@ var cloneCssStyles = function(svg, classes){
} }
}; };
export {cloneCssStyles}; export {cloneCssStyles};

View File

@@ -104,6 +104,20 @@ describe('when cloning CSS ', function () {
document.styleSheets[styleSheetCount].title = 'mermaid-svg-internal-css'; 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 () { it('should handle an empty set of classes', function () {
var svg = document.createElement('svg'); var svg = document.createElement('svg');
svg.setAttribute('id', 'mermaid-01'); svg.setAttribute('id', 'mermaid-01');