Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Hack around absence of String.codePointAt on PhantomJS
Browse files Browse the repository at this point in the history
I've been trying to get some tests working under PhantomJS, which appears not
to support String.codePointAt (which is, to be fair, an ES6 addition). For our
limited usecase, it's easier to implement the functionality from first
principles than to try to polyfill support.
  • Loading branch information
richvdh committed Apr 13, 2016
1 parent f9785f6 commit 5706a87
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions src/components/views/avatars/BaseAvatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,36 @@ module.exports = React.createClass({
}
},

_getInitialLetter: function() {
var name = this.props.name;
//For large characters (exceeding 2 bytes), this function will get the correct character.
//However, this does NOT get the second character correctly if a large character is before it.
var initial = String.fromCodePoint(name.codePointAt(0));
/**
* returns the first (non-sigil) character of 'name',
* converted to uppercase
*/
_getInitialLetter: function(name) {
if (name.length < 1) {
return undefined;
}

var idx = 0;
var initial = name[0];
if ((initial === '@' || initial === '#') && name[1]) {
initial = String.fromCodePoint(name.codePointAt(1));
idx++;
}

// string.codePointAt(0) would do this, but that isn't supported by
// some browsers (notably PhantomJS).
var chars = 1;
var first = name.charCodeAt(idx);

// check if it’s the start of a surrogate pair
if (first >= 0xD800 && first <= 0xDBFF && name[idx+1]) {
var second = name.charCodeAt(idx+1);
if (second >= 0xDC00 && second <= 0xDFFF) {
chars++;
}
}
return initial.toUpperCase();

var firstChar = name.substring(idx, idx+chars);
return firstChar.toUpperCase();
},

render: function() {
Expand All @@ -116,7 +137,7 @@ module.exports = React.createClass({
var imageUrl = this.state.imageUrls[this.state.urlsIndex];

if (imageUrl === this.state.defaultImageUrl) {
var initialLetter = this._getInitialLetter();
var initialLetter = this._getInitialLetter(this.props.name);
return (
<span className="mx_BaseAvatar" {...this.props}>
<span className="mx_BaseAvatar_initial" aria-hidden="true"
Expand Down

0 comments on commit 5706a87

Please sign in to comment.