Skip to content

Commit e84701f

Browse files
committed
replace strict-d3 IIFE with browserify 'require' transform
- which replaces all `require('d3')` with `require('./strict-d3')` - so that strict-d3.js can more easily be used across all our tools - activate when bundling plotly.js in jasmine tests, `npm run watch` and `npm run cibuild` - 🔪 <script> injecting old IIFE - require('fast-isnumeric') in strict-d3.js
1 parent e44aa4d commit e84701f

File tree

9 files changed

+71
-70
lines changed

9 files changed

+71
-70
lines changed

devtools/test_dashboard/index.html

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
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>
2524
<script type="text/javascript" src="../../build/test_dashboard-bundle.js"></script>
2625
</body>
2726
</html>

tasks/cibundle.js

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var _bundle = require('./util/browserify_wrapper');
1616
_bundle(constants.pathToPlotlyIndex, constants.pathToPlotlyBuild, {
1717
standalone: 'Plotly',
1818
pathToMinBundle: constants.pathToPlotlyDistMin,
19+
debug: true
1920
});
2021

2122
// Browserify the geo assets

tasks/util/browserify_wrapper.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var UglifyJS = require('uglify-js');
77
var constants = require('./constants');
88
var compressAttributes = require('./compress_attributes');
99
var patchMinified = require('./patch_minified');
10+
var strictD3 = require('./strict_d3');
1011

1112
/** Convenience browserify wrapper
1213
*
@@ -32,16 +33,20 @@ module.exports = function _bundle(pathToIndex, pathToBundle, opts) {
3233
opts = opts || {};
3334

3435
// do we output a minified file?
35-
var pathToMinBundle = opts.pathToMinBundle,
36-
outputMinified = !!pathToMinBundle && !opts.debug;
36+
var pathToMinBundle = opts.pathToMinBundle;
37+
var outputMinified = !!pathToMinBundle;
3738

3839
var browserifyOpts = {};
3940
browserifyOpts.standalone = opts.standalone;
4041
browserifyOpts.debug = opts.debug;
4142
browserifyOpts.transform = outputMinified ? [compressAttributes] : [];
4243

43-
var b = browserify(pathToIndex, browserifyOpts),
44-
bundleWriteStream = fs.createWriteStream(pathToBundle);
44+
if(opts.debug) {
45+
browserifyOpts.transform.push(strictD3);
46+
}
47+
48+
var b = browserify(pathToIndex, browserifyOpts);
49+
var bundleWriteStream = fs.createWriteStream(pathToBundle);
4550

4651
bundleWriteStream.on('finish', function() {
4752
logger(pathToBundle);

tasks/util/constants.js

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ module.exports = {
5858
pathToTestDashboardBundle: path.join(pathToBuild, 'test_dashboard-bundle.js'),
5959
pathToImageViewerBundle: path.join(pathToBuild, 'image_viewer-bundle.js'),
6060

61+
pathToImageTest: pathToImageTest,
6162
pathToTestImageMocks: path.join(pathToImageTest, 'mocks/'),
6263
pathToTestImageBaselines: path.join(pathToImageTest, 'baselines/'),
6364
pathToTestImages: path.join(pathToBuild, 'test_images/'),

tasks/util/strict_d3.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var path = require('path');
2+
var transformTools = require('browserify-transform-tools');
3+
var constants = require('./constants');
4+
5+
var pathToStrictD3Module = path.join(
6+
constants.pathToImageTest,
7+
'strict-d3.js'
8+
);
9+
10+
/**
11+
* Transform `require('d3')` expressions to `require(/path/to/strict-d3.js)`
12+
*/
13+
14+
module.exports = transformTools.makeRequireTransform('requireTransform',
15+
{ evaluateArguments: true, jsFilesOnly: true },
16+
function(args, opts, cb) {
17+
var pathIn = args[0];
18+
var pathOut;
19+
20+
if(pathIn === 'd3' && opts.file !== pathToStrictD3Module) {
21+
pathOut = 'require(\'' + pathToStrictD3Module + '\')';
22+
}
23+
24+
if(pathOut) return cb(null, pathOut);
25+
else return cb();
26+
});

tasks/util/watchified_bundle.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var prettySize = require('prettysize');
77
var constants = require('./constants');
88
var common = require('./common');
99
var compressAttributes = require('./compress_attributes');
10+
var strictD3 = require('./strict_d3');
1011

1112
/**
1213
* Make a plotly.js browserify bundle function watched by watchify.
@@ -22,7 +23,7 @@ module.exports = function makeWatchifiedBundle(onFirstBundleCallback) {
2223
var b = browserify(constants.pathToPlotlyIndex, {
2324
debug: true,
2425
standalone: 'Plotly',
25-
transform: [compressAttributes],
26+
transform: [strictD3, compressAttributes],
2627
cache: {},
2728
packageCache: {},
2829
plugin: [watchify]

test/image/index.html

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
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>
98
<script type="text/javascript" src="./main.js"></script>
109
</body>
1110
</html>

test/image/strict-d3.js

+30-62
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,45 @@
22
* strict-d3: wrap selection.style to prohibit specific incorrect style values
33
* that are known to cause problems in IE (at least IE9)
44
*/
5+
'use strict';
56

6-
/* global Plotly:false */
7-
(function() {
8-
'use strict';
7+
var d3 = require('d3');
8+
var isNumeric = require('fast-isnumeric');
99

10-
var selProto = Plotly.d3.selection.prototype;
10+
var selProto = d3.selection.prototype;
11+
var originalSelStyle = selProto.style;
1112

12-
var originalSelStyle = selProto.style;
13+
selProto.style = function() {
14+
var sel = this;
15+
var obj = arguments[0];
1316

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]); });
4522
}
4623
}
4724

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);
6036
}
61-
}
62-
return true;
37+
});
6338
}
6439

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);
7643
}
44+
}
7745

78-
})();
46+
module.exports = d3;

test/jasmine/karma.conf.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ if(isFullSuite) {
9595
}
9696

9797
var pathToShortcutPath = path.join(__dirname, '..', '..', 'tasks', 'util', 'shortcut_paths.js');
98+
var pathToStrictD3 = path.join(__dirname, '..', '..', 'tasks', 'util', 'strict_d3.js');
9899
var pathToMain = path.join(__dirname, '..', '..', 'lib', 'index.js');
99100
var pathToJQuery = path.join(__dirname, 'assets', 'jquery-1.8.3.min.js');
100101
var pathToIE9mock = path.join(__dirname, 'assets', 'ie9_mock.js');
@@ -193,7 +194,7 @@ func.defaultConfig = {
193194
},
194195

195196
browserify: {
196-
transform: [pathToShortcutPath],
197+
transform: [pathToStrictD3, pathToShortcutPath],
197198
extensions: ['.js'],
198199
watch: !argv.nowatch,
199200
debug: true

0 commit comments

Comments
 (0)