|
| 1 | +/* |
| 2 | + * strict-d3: wrap selection.style to prohibit specific incorrect style values |
| 3 | + * that are known to cause problems in IE (at least IE9) |
| 4 | + */ |
| 5 | + |
| 6 | +/* global Plotly:false */ |
| 7 | +(function() { |
| 8 | + 'use strict'; |
| 9 | + |
| 10 | + var selProto = Plotly.d3.selection.prototype; |
| 11 | + |
| 12 | + var originalSelStyle = selProto.style; |
| 13 | + |
| 14 | + selProto.style = function() { |
| 15 | + var sel = this, |
| 16 | + obj = arguments[0]; |
| 17 | + |
| 18 | + if(sel.size()) { |
| 19 | + if(typeof obj === 'string') { |
| 20 | + checkVal(obj, arguments[1]); |
| 21 | + } |
| 22 | + else { |
| 23 | + Object.keys(obj).forEach(function(key) { checkVal(key, obj[key]); }); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + return originalSelStyle.apply(sel, arguments); |
| 28 | + }; |
| 29 | + |
| 30 | + function checkVal(key, val) { |
| 31 | + if(typeof val === 'string') { |
| 32 | + // in case of multipart styles (stroke-dasharray, margins, etc) |
| 33 | + // test each part separately |
| 34 | + val.split(/[, ]/g).forEach(function(valPart) { |
| 35 | + var pxSplit = valPart.length - 2; |
| 36 | + if(valPart.substr(pxSplit) === 'px' && !isNumeric(valPart.substr(0, pxSplit))) { |
| 37 | + throw new Error('d3 selection.style called with value: ' + val); |
| 38 | + } |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + // below ripped from fast-isnumeric so I don't need to build this file |
| 45 | + |
| 46 | + function allBlankCharCodes(str) { |
| 47 | + var l = str.length, |
| 48 | + a; |
| 49 | + for(var i = 0; i < l; i++) { |
| 50 | + a = str.charCodeAt(i); |
| 51 | + if((a < 9 || a > 13) && (a !== 32) && (a !== 133) && (a !== 160) && |
| 52 | + (a !== 5760) && (a !== 6158) && (a < 8192 || a > 8205) && |
| 53 | + (a !== 8232) && (a !== 8233) && (a !== 8239) && (a !== 8287) && |
| 54 | + (a !== 8288) && (a !== 12288) && (a !== 65279)) { |
| 55 | + return false; |
| 56 | + } |
| 57 | + } |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + function isNumeric(n) { |
| 62 | + var type = typeof n; |
| 63 | + if(type === 'string') { |
| 64 | + var original = n; |
| 65 | + n = +n; |
| 66 | + // whitespace strings cast to zero - filter them out |
| 67 | + if(n === 0 && allBlankCharCodes(original)) return false; |
| 68 | + } |
| 69 | + else if(type !== 'number') return false; |
| 70 | + |
| 71 | + return n - n < 1; |
| 72 | + } |
| 73 | + |
| 74 | +})(); |
0 commit comments