Skip to content
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

Build HTML via DOM manipulation (vs string manipulation) #6179

Closed
wants to merge 11 commits into from
Closed
18 changes: 9 additions & 9 deletions src/controllers/controller.doughnut.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ defaults._set('doughnut', {
mode: 'single'
},
legendCallback: function(chart) {
var text = [];
text.push('<ul class="' + chart.id + '-legend">');

var list = document.createElement('ul');
var data = chart.data;
var datasets = data.datasets;
var labels = data.labels;
var i, ilen, listItem, listItemSpan;

if (datasets.length) {
for (var i = 0; i < datasets[0].data.length; ++i) {
text.push('<li><span style="background-color:' + datasets[0].backgroundColor[i] + '"></span>');
list.setAttribute('class', chart.id + '-legend');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class is not set when datasets is empty. This line should be outside the if to mimic old behavior.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed. if we could fix this during the rebase that'd be great

for (i = 0, ilen = datasets[0].data.length; i < ilen; ++i) {
listItem = list.appendChild(document.createElement('li'));
listItemSpan = listItem.appendChild(document.createElement('span'));
listItemSpan.style.backgroundColor = datasets[0].backgroundColor[i];
if (labels[i]) {
text.push(labels[i]);
listItem.appendChild(document.createTextNode(labels[i]));
}
text.push('</li>');
}
}

text.push('</ul>');
return text.join('');
return list.outerHTML;
},
legend: {
labels: {
Expand Down
18 changes: 9 additions & 9 deletions src/controllers/controller.polarArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,25 @@ defaults._set('polarArea', {

startAngle: -0.5 * Math.PI,
legendCallback: function(chart) {
var text = [];
text.push('<ul class="' + chart.id + '-legend">');

var list = document.createElement('ul');
var data = chart.data;
var datasets = data.datasets;
var labels = data.labels;
var i, ilen, listItem, listItemSpan;

if (datasets.length) {
for (var i = 0; i < datasets[0].data.length; ++i) {
text.push('<li><span style="background-color:' + datasets[0].backgroundColor[i] + '"></span>');
list.setAttribute('class', chart.id + '-legend');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

for (i = 0, ilen = datasets[0].data.length; i < ilen; ++i) {
listItem = list.appendChild(document.createElement('li'));
listItemSpan = listItem.appendChild(document.createElement('span'));
listItemSpan.style.backgroundColor = datasets[0].backgroundColor[i];
if (labels[i]) {
text.push(labels[i]);
listItem.appendChild(document.createTextNode(labels[i]));
}
text.push('</li>');
}
}

text.push('</ul>');
return text.join('');
return list.outerHTML;
},
legend: {
labels: {
Expand Down
20 changes: 12 additions & 8 deletions src/plugins/plugin.legend.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,21 @@ defaults._set('global', {
},

legendCallback: function(chart) {
var text = [];
text.push('<ul class="' + chart.id + '-legend">');
for (var i = 0; i < chart.data.datasets.length; i++) {
text.push('<li><span style="background-color:' + chart.data.datasets[i].backgroundColor + '"></span>');
var list = document.createElement('ul');
var i, ilen, listItem, listItemSpan;

list.setAttribute('class', chart.id + '-legend');

for (i = 0, ilen = chart.data.datasets.length; i < ilen; i++) {
listItem = list.appendChild(document.createElement('li'));
listItemSpan = listItem.appendChild(document.createElement('span'));
listItemSpan.style.backgroundColor = chart.data.datasets[i].backgroundColor;
if (chart.data.datasets[i].label) {
text.push(chart.data.datasets[i].label);
listItem.appendChild(document.createTextNode(chart.data.datasets[i].label));
}
text.push('</li>');
}
text.push('</ul>');
return text.join('');

return list.outerHTML;
}
});

Expand Down
4 changes: 2 additions & 2 deletions test/specs/global.defaults.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('Default Configs', function() {
options: config
});

var expectedLegend = '<ul class="' + chart.id + '-legend"><li><span style="background-color:red"></span>label1</li><li><span style="background-color:green"></span>label2</li></ul>';
var expectedLegend = '<ul class="' + chart.id + '-legend"><li><span style="background-color: red;"></span>label1</li><li><span style="background-color: green;"></span>label2</li></ul>';
expect(chart.generateLegend()).toBe(expectedLegend);
});

Expand Down Expand Up @@ -218,7 +218,7 @@ describe('Default Configs', function() {
options: config
});

var expectedLegend = '<ul class="' + chart.id + '-legend"><li><span style="background-color:red"></span>label1</li><li><span style="background-color:green"></span>label2</li></ul>';
var expectedLegend = '<ul class="' + chart.id + '-legend"><li><span style="background-color: red;"></span>label1</li><li><span style="background-color: green;"></span>label2</li></ul>';
expect(chart.generateLegend()).toBe(expectedLegend);
});

Expand Down