Skip to content

Commit

Permalink
Improve CSP compliance for gfx and charting (#314)
Browse files Browse the repository at this point in the history
Remove usage of new Function()
  • Loading branch information
msssk authored Feb 12, 2020
1 parent abd033a commit 5491eff
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 110 deletions.
52 changes: 34 additions & 18 deletions charting/Chart.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
define(["../main", "dojo/_base/lang", "dojo/_base/array","dojo/_base/declare", "dojo/dom-style",
"dojo/dom", "dojo/dom-geometry", "dojo/dom-construct","dojo/_base/Color", "dojo/sniff",
"./Element", "./SimpleTheme", "./Series", "./axis2d/common", "dojox/gfx/shape",
"./Element", "./SimpleTheme", "./Series", "./axis2d/common", "./plot2d/common", "dojox/gfx/shape",
"dojox/gfx", "dojo/has!dojo-bidi?./bidi/Chart", "dojox/lang/functional", "dojox/lang/functional/fold", "dojox/lang/functional/reversed"],
function(dojox, lang, arr, declare, domStyle,
dom, domGeom, domConstruct, Color, has,
Element, SimpleTheme, Series, common, shape,
Element, SimpleTheme, Series, common, plot2dCommon, shape,
g, BidiChart, func){
/*=====
var __ChartCtorArgs = {
Expand Down Expand Up @@ -44,14 +44,30 @@ define(["../main", "dojo/_base/lang", "dojo/_base/array","dojo/_base/declare", "
=====*/

var dc = lang.getObject("charting", true, dojox),
clear = func.lambda("item.clear()"),
purge = func.lambda("item.purgeGroup()"),
destroy = func.lambda("item.destroy()"),
makeClean = func.lambda("item.dirty = false"),
makeDirty = func.lambda("item.dirty = true"),
getName = func.lambda("item.name"),
defaultMargins = {l: 10, t: 10, r: 10, b: 10};

function clear (item) {
return item.clear();
}

function destroy (item) {
return item.destroy();
}

function makeClean (item) {
item.dirty = false;
return false;
}

function makeDirty (item) {
item.dirty = true;
return true;
}

function getName (item) {
return item.name;
}

var Chart = declare(has("dojo-bidi")? "dojox.charting.NonBidiChart" : "dojox.charting.Chart", null, {
// summary:
// The main chart object in dojox.charting. This will create a two dimensional
Expand Down Expand Up @@ -124,7 +140,7 @@ define(["../main", "dojo/_base/lang", "dojo/_base/array","dojo/_base/declare", "
// | )
// | .render();
// | });

// theme: dojox/charting/SimpleTheme?
// An optional theme to use for styling the chart.
// axes: dojox/charting/axis2d/Base{}?
Expand Down Expand Up @@ -171,7 +187,7 @@ define(["../main", "dojo/_base/lang", "dojo/_base/array","dojo/_base/declare", "
this.titlePos = kwArgs.titlePos;
this.titleFont = kwArgs.titleFont;
this.titleFontColor = kwArgs.titleFontColor;
this.titleAlign = kwArgs.titleAlign; // This can be middle, left, right, or edge
this.titleAlign = kwArgs.titleAlign; // This can be middle, left, right, or edge
// edge is left or right aligned with chart plot edge depending on bidi.
this.chartTitle = null;
this.htmlLabels = true;
Expand Down Expand Up @@ -887,7 +903,7 @@ define(["../main", "dojo/_base/lang", "dojo/_base/array","dojo/_base/declare", "
clearTimeout(this._delayedRenderHandle);
this._delayedRenderHandle = null;
}

if(this.theme){
this.theme.clear();
}
Expand Down Expand Up @@ -926,9 +942,9 @@ define(["../main", "dojo/_base/lang", "dojo/_base/array","dojo/_base/declare", "
//this.theme.defineColors({num: requiredColors, cache: false});

// clear old shapes
arr.forEach(this.series, purge);
func.forIn(this.axes, purge);
arr.forEach(this.stack, purge);
arr.forEach(this.series, plot2dCommon.purgeGroup);
func.forIn(this.axes, plot2dCommon.purgeGroup);
arr.forEach(this.stack, plot2dCommon.purgeGroup);
var children = this.surface.children;
// starting with 1.9 the registry is optional and thus dispose is
if(shape.dispose){
Expand Down Expand Up @@ -982,7 +998,7 @@ define(["../main", "dojo/_base/lang", "dojo/_base/array","dojo/_base/declare", "
labelType = forceHtmlLabels || !has("ie") && !has("opera") && this.htmlLabels ? "html" : "gfx",
tsize = g.normalizedLength(g.splitFontString(this.titleFont).size),
tBox = g._base._getTextBox(this.title,{ font: this.titleFont });

var titleAlign = this.titleAlign;
var isRtl = has("dojo-bidi") && this.isRightToLeft();
var posX = dim.width/2; // Default is middle.
Expand Down Expand Up @@ -1200,11 +1216,11 @@ define(["../main", "dojo/_base/lang", "dojo/_base/array","dojo/_base/declare", "
}
},
setDir : function(dir){
return this;
return this;
},
_resetLeftBottom: function(axis){
},
formatTruncatedLabel: function(element, label, labelType){
formatTruncatedLabel: function(element, label, labelType){
}
});

Expand Down Expand Up @@ -1256,6 +1272,6 @@ define(["../main", "dojo/_base/lang", "dojo/_base/array","dojo/_base/declare", "
plot.initializeScalers(plotArea, stats);
});
}

return has("dojo-bidi")? declare("dojox.charting.Chart", [Chart, BidiChart]) : Chart;
});
33 changes: 16 additions & 17 deletions charting/plot2d/Bars.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
define(["dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "dojo/has", "./CartesianBase", "./_PlotEvents", "./common",
"dojox/gfx/fx", "dojox/lang/utils", "dojox/lang/functional", "dojox/lang/functional/reversed"],
function(lang, arr, declare, has, CartesianBase, _PlotEvents, dc, fx, du, df, dfr){
"dojox/gfx/fx", "dojox/lang/utils", "dojox/lang/functional"],
function(lang, arr, declare, has, CartesianBase, _PlotEvents, dc, fx, du, df){

/*=====
declare("dojox.charting.plot2d.__BarCtorArgs", dojox.charting.plot2d.__DefaultCtorArgs, {
// summary:
// Additional keyword arguments for bar charts.
// minBarSize: Number?
// The minimum size for a bar in pixels. Default is 1.
minBarSize: 1,
// maxBarSize: Number?
// The maximum size for a bar in pixels. Default is 1.
maxBarSize: 1,
Expand Down Expand Up @@ -47,15 +47,14 @@ define(["dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "dojo/has",
// fontColor: String|dojo.Color?
// The color to be used for any text-based elements on the plot.
fontColor: "",
// enableCache: Boolean?
// Whether the bars rect are cached from one rendering to another. This improves the rendering performance of
// successive rendering but penalize the first rendering. Default false.
enableCache: false
});
=====*/
var purgeGroup = dfr.lambda("item.purgeGroup()");


var alwaysFalse = function(){ return false; }

return declare("dojox.charting.plot2d.Bars", [CartesianBase, _PlotEvents], {
Expand Down Expand Up @@ -106,7 +105,7 @@ define(["dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "dojo/has",
t = stats.hmax, stats.hmax = stats.vmax, stats.vmax = t;
return stats; // Object
},

createRect: function(run, creator, params){
var rect;
if(this.opt.enableCache && run._rectFreePool.length > 0){
Expand Down Expand Up @@ -149,7 +148,7 @@ define(["dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "dojo/has",
this.resetEvents();
var s;
if(this.dirty){
arr.forEach(this.series, purgeGroup);
arr.forEach(this.series, dc.purgeGroup);
this._eventSeries = {};
this.cleanGroup();
s = this.getGroup();
Expand All @@ -159,19 +158,19 @@ define(["dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "dojo/has",
ht = this._hScaler.scaler.getTransformerFromModel(this._hScaler),
vt = this._vScaler.scaler.getTransformerFromModel(this._vScaler),
baseline = Math.max(this._hScaler.bounds.lower,
this._hAxis ? this._hAxis.naturalBaseline : 0),
this._hAxis ? this._hAxis.naturalBaseline : 0),
baselineWidth = ht(baseline),
events = this.events();
var bar = this.getBarProperties();

var actualLength = this.series.length;
arr.forEach(this.series, function(serie){if(serie.hidden){actualLength--;}});
var z = actualLength;

// Collect and calculate all values
var extractedValues = this.extractValues(this._vScaler);
extractedValues = this.rearrangeValues(extractedValues, ht, baselineWidth);

for(var i = 0; i < this.series.length; i++){
var run = this.series[i];
if(!this.dirty && !run.dirty){
Expand All @@ -193,7 +192,7 @@ define(["dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "dojo/has",
z--;

var eventSeries = new Array(run.data.length);
s = run.group;
s = run.group;
var indexed = arr.some(run.data, function(item){
return typeof item == "number" || (item && !item.hasOwnProperty("x"));
});
Expand Down Expand Up @@ -231,13 +230,13 @@ define(["dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "dojo/has",
this._animateBar(sshape, offsets.l + baselineWidth, -w);
}
}

var specialFill = this._plotFill(finalTheme.series.fill, dim, offsets);
specialFill = this._shapeFill(specialFill, rect);
var shape = this.createRect(run, s, rect).setFill(specialFill).setStroke(finalTheme.series.stroke);
if(shape.setFilter && finalTheme.series.filter){
shape.setFilter(finalTheme.series.filter);
}
}
run.dyn.fill = shape.getFill();
run.dyn.stroke = shape.getStroke();
if(events){
Expand Down Expand Up @@ -340,7 +339,7 @@ define(["dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "dojo/has",
return v(0.5) || h(value);
}
return v(isNaN(value.x) ? 0.5 : value.x + 0.5) || value.y === null || h(value.y);
},
},
getBarProperties: function(){
var f = dc.calculateBarSize(this._vScaler.bounds.scale, this.opt);
return {gap: f.gap, height: f.size, thickness: 0};
Expand Down
14 changes: 6 additions & 8 deletions charting/plot2d/Bubble.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
define(["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/array", "dojo/has",
"./CartesianBase", "./_PlotEvents", "./common", "dojox/lang/functional", "dojox/lang/functional/reversed",
"dojox/lang/utils", "dojox/gfx/fx"],
function(lang, declare, arr, has, CartesianBase, _PlotEvents, dc, df, dfr, du, fx){

var purgeGroup = dfr.lambda("item.purgeGroup()");
define(["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/array", "dojo/has",
"./CartesianBase", "./_PlotEvents", "./common", "dojox/lang/functional",
"dojox/lang/utils", "dojox/gfx/fx"],
function(lang, declare, arr, has, CartesianBase, _PlotEvents, dc, df, du, fx){

return declare("dojox.charting.plot2d.Bubble", [CartesianBase, _PlotEvents], {
// summary:
Expand Down Expand Up @@ -60,7 +58,7 @@ define(["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/array", "dojo/has",
this.resetEvents();
this.dirty = this.isDirty();
if(this.dirty){
arr.forEach(this.series, purgeGroup);
arr.forEach(this.series, dc.purgeGroup);
this._eventSeries = {};
this.cleanGroup();
s = this.getGroup();
Expand Down Expand Up @@ -106,7 +104,7 @@ define(["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/array", "dojo/has",
continue;
}
s = run.group;

var frontCircles = null, outlineCircles = null, shadowCircles = null, styleFunc = this.opt.styleFunc;

var getFinalTheme = function(item){
Expand Down
10 changes: 4 additions & 6 deletions charting/plot2d/Candlesticks.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
define(["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/array", "dojo/has", "./CartesianBase", "./_PlotEvents", "./common",
"dojox/lang/functional", "dojox/lang/functional/reversed", "dojox/lang/utils", "dojox/gfx/fx"],
function(lang, declare, arr, has, CartesianBase, _PlotEvents, dc, df, dfr, du, fx){

var purgeGroup = dfr.lambda("item.purgeGroup()");
"dojox/lang/functional", "dojox/lang/utils", "dojox/gfx/fx"],
function(lang, declare, arr, has, CartesianBase, _PlotEvents, dc, df, du, fx){

// Candlesticks are based on the Bars plot type; we expect the following passed
// as values in a series:
Expand Down Expand Up @@ -106,7 +104,7 @@ define(["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/array", "dojo/has",
this.dirty = this.isDirty();
var s;
if(this.dirty){
arr.forEach(this.series, purgeGroup);
arr.forEach(this.series, dc.purgeGroup);
this._eventSeries = {};
this.cleanGroup();
s = this.getGroup();
Expand Down Expand Up @@ -136,7 +134,7 @@ define(["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/array", "dojo/has",
continue;
}
s = run.group;

for(var j = 0; j < run.data.length; ++j){
var v = run.data[j];
if(!this.isNullValue(v)){
Expand Down
8 changes: 3 additions & 5 deletions charting/plot2d/Columns.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
define(["dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "dojo/has", "./CartesianBase", "./_PlotEvents", "./common",
"dojox/lang/functional", "dojox/lang/functional/reversed", "dojox/lang/utils", "dojox/gfx/fx"],
function(lang, arr, declare, has, CartesianBase, _PlotEvents, dc, df, dfr, du, fx){

var purgeGroup = dfr.lambda("item.purgeGroup()");
"dojox/lang/functional", "dojox/lang/utils", "dojox/gfx/fx"],
function(lang, arr, declare, has, CartesianBase, _PlotEvents, dc, df, du, fx){

var alwaysFalse = function(){ return false; };

Expand Down Expand Up @@ -85,7 +83,7 @@ define(["dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "dojo/has",
this.dirty = this.isDirty();
var s;
if(this.dirty){
arr.forEach(this.series, purgeGroup);
arr.forEach(this.series, dc.purgeGroup);
this._eventSeries = {};
this.cleanGroup();
s = this.getGroup();
Expand Down
8 changes: 3 additions & 5 deletions charting/plot2d/Default.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
define(["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/array", "dojo/has",
"./CartesianBase", "./_PlotEvents", "./common", "dojox/lang/functional", "dojox/lang/functional/reversed", "dojox/lang/utils", "dojox/gfx/fx"],
function(lang, declare, arr, has, CartesianBase, _PlotEvents, dc, df, dfr, du, fx){
"./CartesianBase", "./_PlotEvents", "./common", "dojox/lang/functional", "dojox/lang/utils", "dojox/gfx/fx"],
function(lang, declare, arr, has, CartesianBase, _PlotEvents, dc, df, du, fx){

/*=====
declare("dojox.charting.plot2d.__DefaultCtorArgs", dojox.charting.plot2d.__CartesianCtorArgs, {
Expand Down Expand Up @@ -105,8 +105,6 @@ define(["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/array", "dojo/has",
});
=====*/

var purgeGroup = dfr.lambda("item.purgeGroup()");

var DEFAULT_ANIMATION_LENGTH = 1200; // in ms

return declare("dojox.charting.plot2d.Default", [CartesianBase, _PlotEvents], {
Expand Down Expand Up @@ -219,7 +217,7 @@ define(["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/array", "dojo/has",
this.dirty = this.isDirty();
var s;
if(this.dirty){
arr.forEach(this.series, purgeGroup);
arr.forEach(this.series, dc.purgeGroup);
this._eventSeries = {};
this.cleanGroup();
this.getGroup().setTransform(null);
Expand Down
8 changes: 3 additions & 5 deletions charting/plot2d/OHLC.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
define(["dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "dojo/has", "./CartesianBase", "./_PlotEvents", "./common",
"dojox/lang/functional", "dojox/lang/functional/reversed", "dojox/lang/utils", "dojox/gfx/fx"],
function(lang, arr, declare, has, CartesianBase, _PlotEvents, dc, df, dfr, du, fx){

var purgeGroup = dfr.lambda("item.purgeGroup()");
"dojox/lang/functional", "dojox/lang/utils", "dojox/gfx/fx"],
function(lang, arr, declare, has, CartesianBase, _PlotEvents, dc, df, du, fx){

// Candlesticks are based on the Bars plot type; we expect the following passed
// as values in a series:
Expand Down Expand Up @@ -105,7 +103,7 @@ define(["dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "dojo/has",
this.resetEvents();
this.dirty = this.isDirty();
if(this.dirty){
arr.forEach(this.series, purgeGroup);
arr.forEach(this.series, dc.purgeGroup);
this._eventSeries = {};
this.cleanGroup();
var s = this.getGroup();
Expand Down
8 changes: 3 additions & 5 deletions charting/plot2d/Scatter.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
define(["dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "dojo/has", "./CartesianBase", "./_PlotEvents", "./common",
"dojox/lang/functional", "dojox/lang/functional/reversed", "dojox/lang/utils", "dojox/gfx/fx", "dojox/gfx/gradutils"],
function(lang, arr, declare, has, CartesianBase, _PlotEvents, dc, df, dfr, du, fx, gradutils){

var purgeGroup = dfr.lambda("item.purgeGroup()");
"dojox/lang/functional", "dojox/lang/utils", "dojox/gfx/fx", "dojox/gfx/gradutils"],
function(lang, arr, declare, has, CartesianBase, _PlotEvents, dc, df, du, fx, gradutils){

return declare("dojox.charting.plot2d.Scatter", [CartesianBase, _PlotEvents], {
// summary:
Expand Down Expand Up @@ -51,7 +49,7 @@ define(["dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "dojo/has",
this.dirty = this.isDirty();
var s;
if(this.dirty){
arr.forEach(this.series, purgeGroup);
arr.forEach(this.series, dc.purgeGroup);
this._eventSeries = {};
this.cleanGroup();
s = this.getGroup();
Expand Down
Loading

0 comments on commit 5491eff

Please sign in to comment.