Skip to content

Commit 4aa1a45

Browse files
committed
strict-d3: prevent stuff like 'nanpx' from selection.style
1 parent 12aeb9d commit 4aa1a45

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

devtools/test_dashboard/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
<script type="text/javascript" src="../../dist/extras/mathjax/MathJax.js?config=TeX-AMS-MML_SVG"></script>
2323
<script id="source" type="text/javascript" src="../../build/plotly.js"></script>
24+
<script type="text/javascript" src="../../test/image/strict-d3.js" charset="utf-8"></script>
2425
<script type="text/javascript" src="../../build/test_dashboard-bundle.js"></script>
2526
</body>
2627
</html>

test/image/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<script type="text/javascript" src="../plotly.js/dist/extras/mathjax/MathJax.js?config=TeX-AMS-MML_SVG"></script>
66
<script type="text/javascript" src="../plotly.js/build/plotly.js" charset="utf-8"></script>
77
<script type="text/javascript" src="../plotly.js/dist/plotly-geo-assets.js" charset="utf-8"></script>
8+
<script type="text/javascript" src="../plotly.js/test/image/strict-d3.js" charset="utf-8"></script>
89
<script type="text/javascript" src="./main.js"></script>
910
</body>
1011
</html>

test/image/strict-d3.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)