Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

improve pseudo-html entity conversion #2932

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 4 commits into from
Sep 4, 2018
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
test numeric HTML entity conversion in IE
  • Loading branch information
alexcjohnson committed Aug 21, 2018
commit f57449f62000d5d2e516b557fa9539cc9078b8b5
5 changes: 2 additions & 3 deletions 5 src/lib/svg_text_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,17 +301,16 @@ function convertEntities(_str) {
}
exports.convertEntities = convertEntities;

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

// IE doesn't have String.fromCodePoint
Copy link
Contributor

Choose a reason for hiding this comment

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

To be fair, not just IE doesn't support fromCodePoint, 2015 Chrome and 2014 FF didn't have it either.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fair enough - though we don't support such old versions of Chrome and FF, do we?

Copy link
Contributor

Choose a reason for hiding this comment

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

do we?

We used to have a blob on https://plot.ly/javascript/ (that I can't find right now) saying plotly.js is supported in all SVG-enabled browsers. But yeah, the fraction of users still using Chrome41 and FF29 must be tiny.

// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint
var stringFromCharCode = String.fromCharCode;
if(code <= 0xFFFF) return stringFromCharCode(code);
return stringFromCharCode(
(code >> 10) + 0xD7C0,
Expand Down
32 changes: 32 additions & 0 deletions 32 test/jasmine/tests/svg_text_utils_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ describe('svg+text utils', function() {

describe('convertToTspans', function() {

var stringFromCodePoint;

beforeAll(function() {
stringFromCodePoint = String.fromCodePoint;
});

afterEach(function() {
String.fromCodePoint = stringFromCodePoint;
});

function mockTextSVGElement(txt) {
return d3.select('body')
.append('svg')
Expand Down Expand Up @@ -330,6 +340,28 @@ describe('svg+text utils', function() {
expect(i).toBe(355);
});

it('decodes arbitrary decimal and hex number entities (IE case)', function() {
// IE does not have String.fromCodePoint
String.fromCodePoint = undefined;
expect(String.fromCodePoint).toBeUndefined();

var i = 0;
for(var n = 33; n < 0x10FFFF; n = Math.round(n * 1.03)) {
var node = mockTextSVGElement(
'&#x' + n.toString(16) +
'; = &#' + n.toString() +
'; = &#x' + n.toString(16).toUpperCase() + ';'
);
var char = stringFromCodePoint(n);
expect(node.text()).toBe(char + ' = ' + char + ' = ' + char, n);
i++;
}
// not really necessary to assert this, but we tested 355 characters,
// weighted toward the low end but continuing all the way to the
// end of the unicode definition
expect(i).toBe(355);
});

it('does not decode entities prematurely', function() {
var testCases = [
'&lt;b>not bold</b&gt;',
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.