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