Skip to content

Commit f57449f

Browse files
committed
test numeric HTML entity conversion in IE
1 parent 3860295 commit f57449f

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/lib/svg_text_utils.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,17 +301,16 @@ function convertEntities(_str) {
301301
}
302302
exports.convertEntities = convertEntities;
303303

304-
// but also in other browsers we don't want to overflow
305-
var stringFromCodePoint = String.fromCodePoint;
306-
var stringFromCharCode = String.fromCharCode;
307304
function fromCodePoint(code) {
308305
// Don't allow overflow. In Chrome this turns into � but I feel like it's
309306
// more useful to just not convert it at all.
310307
if(code > 0x10FFFF) return;
308+
var stringFromCodePoint = String.fromCodePoint;
311309
if(stringFromCodePoint) return stringFromCodePoint(code);
312310

313311
// IE doesn't have String.fromCodePoint
314312
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint
313+
var stringFromCharCode = String.fromCharCode;
315314
if(code <= 0xFFFF) return stringFromCharCode(code);
316315
return stringFromCharCode(
317316
(code >> 10) + 0xD7C0,

test/jasmine/tests/svg_text_utils_test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ describe('svg+text utils', function() {
88

99
describe('convertToTspans', function() {
1010

11+
var stringFromCodePoint;
12+
13+
beforeAll(function() {
14+
stringFromCodePoint = String.fromCodePoint;
15+
});
16+
17+
afterEach(function() {
18+
String.fromCodePoint = stringFromCodePoint;
19+
});
20+
1121
function mockTextSVGElement(txt) {
1222
return d3.select('body')
1323
.append('svg')
@@ -330,6 +340,28 @@ describe('svg+text utils', function() {
330340
expect(i).toBe(355);
331341
});
332342

343+
it('decodes arbitrary decimal and hex number entities (IE case)', function() {
344+
// IE does not have String.fromCodePoint
345+
String.fromCodePoint = undefined;
346+
expect(String.fromCodePoint).toBeUndefined();
347+
348+
var i = 0;
349+
for(var n = 33; n < 0x10FFFF; n = Math.round(n * 1.03)) {
350+
var node = mockTextSVGElement(
351+
'&#x' + n.toString(16) +
352+
'; = &#' + n.toString() +
353+
'; = &#x' + n.toString(16).toUpperCase() + ';'
354+
);
355+
var char = stringFromCodePoint(n);
356+
expect(node.text()).toBe(char + ' = ' + char + ' = ' + char, n);
357+
i++;
358+
}
359+
// not really necessary to assert this, but we tested 355 characters,
360+
// weighted toward the low end but continuing all the way to the
361+
// end of the unicode definition
362+
expect(i).toBe(355);
363+
});
364+
333365
it('does not decode entities prematurely', function() {
334366
var testCases = [
335367
'&lt;b>not bold</b&gt;',

0 commit comments

Comments
 (0)