Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Remove inline styles that break plots in strict CSP setups #7109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix unit tests and bug applying bgcolor with relayout
Fixed unit tests by updating the expectations as appropriate to match
the changed logic in Pull Request #7109. This revealed a bug where
background color changes applied using `Plotly.relayout()` were missed.
This bug was also fixed in this change in order to get all unit tests,
as updated, to pass.
  • Loading branch information
martian111 committed Oct 20, 2024
commit a2eeaf00d3f9739c20b335abded753735d505b1e
6 changes: 5 additions & 1 deletion src/components/modebar/modebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ proto.update = function(graphInfo, buttons) {
}

var style = fullLayout.modebar;
var bgSelector = context.displayModeBar === 'hover' ? '.js-plotly-plot .plotly:hover ' : '';

// set style for modebar-group directly instead of inline CSS that's not allowed by strict CSP's
var groupSelector = '#' + modeBarId + ' .modebar-group';
document.querySelectorAll(groupSelector).forEach(function(group) {
group.style.backgroundColor = style.bgcolor;
});
// set styles on hover using event listeners instead of inline CSS that's not allowed by strict CSP's
Lib.setStyleOnHover('#' + modeBarId + ' .modebar-btn', '.active', '.icon path', 'fill: ' + style.activecolor, 'fill: ' + style.color);

Expand Down
35 changes: 2 additions & 33 deletions test/jasmine/tests/modebar_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1544,33 +1544,6 @@ describe('ModeBar', function() {
expect(style.fill).toBe(color);
}

function getStyleRule() {
var uid = gd._fullLayout._uid;
var ownerNode = document.getElementById('plotly.js-style-modebar-' + uid);
var styleSheets = document.styleSheets;
for(var i = 0; i < styleSheets.length; i++) {
var ss = styleSheets[i];
if(ss.ownerNode === ownerNode) return ss;
}
}

it('create an associated style element and destroy it on purge', function(done) {
var styleSelector, style;
Plotly.newPlot(gd, [], {})
.then(function() {
styleSelector = 'style[id*="modebar-' + gd._fullLayout._uid + '"]';

style = document.querySelector(styleSelector);
expect(style).toBeTruthy();
})
.then(function() {
Plotly.purge(gd);
style = document.querySelector(styleSelector);
expect(style).toBeNull();
})
.then(done, done.fail);
});

it('changes icon colors', function(done) {
Plotly.newPlot(gd, [], {modebar: { color: colors[0]}})
.then(function() {
Expand Down Expand Up @@ -1602,14 +1575,12 @@ describe('ModeBar', function() {
Plotly.newPlot(gd, [], {modebar: { bgcolor: colors[0]}})
.then(function() {
style = window.getComputedStyle(gd._fullLayout._modeBar.element.querySelector('.modebar-group'));
expect(style.backgroundColor).toBe('rgba(0, 0, 0, 0)');
expect(getStyleRule().rules[3].style.backgroundColor).toBe(colors[0]);
expect(style.backgroundColor).toBe(colors[0]);
})
.then(function() { return Plotly.relayout(gd, 'modebar.bgcolor', colors[1]); })
.then(function() {
style = window.getComputedStyle(gd._fullLayout._modeBar.element.querySelector('.modebar-group'));
expect(style.backgroundColor).toBe('rgba(0, 0, 0, 0)');
expect(getStyleRule().rules[3].style.backgroundColor).toBe(colors[1]);
expect(style.backgroundColor).toBe(colors[1]);
})
.then(done, done.fail);
});
Expand All @@ -1619,13 +1590,11 @@ describe('ModeBar', function() {
.then(function() {
style = window.getComputedStyle(gd._fullLayout._modeBar.element.querySelector('.modebar-group'));
expect(style.backgroundColor).toBe(colors[0]);
expect(getStyleRule().rules[3].style.backgroundColor).toBe(colors[0]);
})
.then(function() { return Plotly.relayout(gd, 'modebar.bgcolor', colors[1]); })
.then(function() {
style = window.getComputedStyle(gd._fullLayout._modeBar.element.querySelector('.modebar-group'));
expect(style.backgroundColor).toBe(colors[1]);
expect(getStyleRule().rules[3].style.backgroundColor).toBe(colors[1]);
})
.then(done, done.fail);
});
Expand Down