Skip to content

Commit

Permalink
Improves code readability for image traces
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinerg committed Aug 18, 2020
1 parent d91e0c1 commit fe1d208
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/traces/image/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = function supplyDefaults(traceIn, traceOut) {
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
}
coerce('source');
// sanitize source to only allow for data URI representing images
if(traceOut.source && !traceOut.source.match(dataUri)) delete traceOut.source;
traceOut._isFromSource = !!traceOut.source;

Expand Down
4 changes: 3 additions & 1 deletion src/traces/image/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ var Lib = require('../../lib');
var xmlnsNamespaces = require('../../constants/xmlns_namespaces');
var constants = require('./constants');

var unsupportedBrowsers = Lib.isIOS() || Lib.isSafari() || Lib.isIE();

function compatibleAxis(ax) {
return ax.type === 'linear' &&
// y axis must be reversed but x axis mustn't be
Expand All @@ -23,7 +25,7 @@ module.exports = function plot(gd, plotinfo, cdimage, imageLayer) {
var xa = plotinfo.xaxis;
var ya = plotinfo.yaxis;

var supportsPixelatedImage = !Lib.isIOS() && !Lib.isSafari() && !Lib.isIE() && !gd._context._exportedPlot;
var supportsPixelatedImage = !(unsupportedBrowsers || gd._context._exportedPlot);

This comment has been minimized.

Copy link
@archmoj

archmoj Aug 18, 2020

Contributor

@antoinerg do we still need this gd._context._exportedPlot fall back?

This comment has been minimized.

Copy link
@antoinerg

antoinerg Aug 18, 2020

Author Contributor

Yes, we do! If the exported SVG image ends up being loaded in Inkscape or some other SVG renderer, we can't assume it supports pixelated images.


Lib.makeTraceGroups(imageLayer, cdimage, 'im').each(function(cd) {
var plotGroup = d3.select(this);
Expand Down
1 change: 0 additions & 1 deletion src/traces/image/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ var d3 = require('d3');
module.exports = function style(gd) {
d3.select(gd).selectAll('.im image')
.style('opacity', function(d) {
if(d && d.hidden) return 0;
return d[0].trace.opacity;
});
};
9 changes: 9 additions & 0 deletions test/jasmine/tests/image_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ describe('image supplyDefaults', function() {
expect(traceOut.visible).toBe(true);
});

it('should not accept source attribute that is not a data URI of an image', function() {
traceIn = {
source: 'javascript:alert(\'attack\')'
};

supplyDefaults(traceIn, traceOut);
expect(traceOut.source).toBe(undefined);
});

it('should set proper zmin/zmax depending on colormodel', function() {
var tests = [
['rgb', [0, 0, 0], [255, 255, 255]],
Expand Down

0 comments on commit fe1d208

Please sign in to comment.