Skip to content

Commit a3d129a

Browse files
panthonykt3k
authored andcommitted
Fix hovering state when target contains special characters
1 parent 3d6037e commit a3d129a

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

spec/class-spec.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,42 @@ describe('c3 chart class', function () {
1717
chart = window.initChart(chart, args, done);
1818
});
1919

20-
describe('internal.getTargetSelectorSuffix', function () {
20+
describe('internal.generateTargetClass', function () {
2121

2222
it('should not replace any characters', function () {
2323
var input = 'data1',
2424
expected = '-' + input,
25-
suffix = chart.internal.getTargetSelectorSuffix(input);
25+
suffix = chart.internal.generateTargetClass(input);
2626
expect(suffix).toBe(expected);
2727
});
2828

2929
it('should replace space to "-"', function () {
3030
var input = 'data1 suffix',
3131
expected = '-data1-suffix',
32-
suffix = chart.internal.getTargetSelectorSuffix(input);
32+
suffix = chart.internal.generateTargetClass(input);
3333
expect(suffix).toBe(expected);
3434
});
3535

3636
it('should replace space to "-" with multibyte characters', function () {
3737
var input = 'data1 suffix 日本語',
3838
expected = '-data1-suffix-日本語',
39-
suffix = chart.internal.getTargetSelectorSuffix(input);
39+
suffix = chart.internal.generateTargetClass(input);
4040
expect(suffix).toBe(expected);
4141
});
4242

43-
it('should replace special charactors to "-"', function () {
43+
it('should not replace special characters', function () {
44+
var input = 'data1 !@#$%^&*()_=+,.<>"\':;[]/|?~`{}\\',
45+
expected = '-data1-!@#$%^&*()_=+,.<>"\':;[]/|?~`{}\\',
46+
suffix = chart.internal.generateTargetClass(input);
47+
expect(suffix).toBe(expected);
48+
});
49+
});
50+
51+
describe('internal.getTargetSelectorSuffix', function () {
52+
53+
it('should escape special characters', function () {
4454
var input = 'data1 !@#$%^&*()_=+,.<>"\':;[]/|?~`{}\\',
45-
expected = '-data1--------------------------------',
55+
expected = '-data1-\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\_\\=\\+\\,\\.\\<\\>\\"\\\'\\:\\;\\[\\]\\/\\|\\?\\~\\`\\{\\}\\\\',
4656
suffix = chart.internal.getTargetSelectorSuffix(input);
4757
expect(suffix).toBe(expected);
4858
});

src/class-utils.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import CLASS from './class';
22
import { c3_chart_internal_fn } from './core';
33

4+
5+
c3_chart_internal_fn.generateTargetClass = function (targetId) {
6+
return targetId || targetId === 0 ? ('-' + targetId).replace(/\s/g, '-') : '';
7+
};
48
c3_chart_internal_fn.generateClass = function (prefix, targetId) {
5-
return " " + prefix + " " + prefix + this.getTargetSelectorSuffix(targetId);
9+
return " " + prefix + " " + prefix + this.generateTargetClass(targetId);
610
};
711
c3_chart_internal_fn.classText = function (d) {
812
return this.generateClass(CLASS.text, d.index);
@@ -82,7 +86,8 @@ c3_chart_internal_fn.classChartArc = function (d) {
8286
return CLASS.chartArc + this.classTarget(d.data.id);
8387
};
8488
c3_chart_internal_fn.getTargetSelectorSuffix = function (targetId) {
85-
return targetId || targetId === 0 ? ('-' + targetId).replace(/[\s?!@#$%^&*()_=+,.<>'":;\[\]\/|~`{}\\]/g, '-') : '';
89+
return this.generateTargetClass(targetId)
90+
.replace(/([?!@#$%^&*()_=+,.<>'":;\[\]\/|~`{}\\])/g, '\\$1');
8691
};
8792
c3_chart_internal_fn.selectorTarget = function (id, prefix) {
8893
return (prefix || '') + '.' + CLASS.target + this.getTargetSelectorSuffix(id);

0 commit comments

Comments
 (0)