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

Commit

Permalink
Fix a bug in modifyIndicesFromUTF16ToUnicode/modifyIndicesFromUnicode…
Browse files Browse the repository at this point in the history
…ToUTF16.
  • Loading branch information
keita committed Feb 15, 2012
1 parent 5c0be71 commit a65f65f
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions twitter-text.js
Expand Up @@ -767,25 +767,44 @@ if (typeof twttr === "undefined" || twttr === null) {
};

twttr.txt.modifyIndicesFromUnicodeToUTF16 = function(text, entities) {
twttr.txt.shiftIndices(text, entities, 1);
twttr.txt.shiftIndices(text, entities, false);
};

twttr.txt.modifyIndicesFromUTF16ToUnicode = function(text, entities) {
twttr.txt.shiftIndices(text, entities, -1);
twttr.txt.shiftIndices(text, entities, true);
};

twttr.txt.shiftIndices = function(text, entities, diff) {
for (var i = 0; i < text.length - 1; i++) {
var c1 = text.charCodeAt(i);
var c2 = text.charCodeAt(i + 1);
if (0xD800 <= c1 && c1 <= 0xDBFF && 0xDC00 <= c2 && c2 <= 0xDFFF) {
// supplementary character
i++; // skip surrogate pair character
for (var j = 0; j < entities.length; j++) {
if (entities[j].indices[0] >= i) {
entities[j].indices[0] += diff;
entities[j].indices[1] += diff;
}
twttr.txt.shiftIndices = function(text, entities, indicesInUTF16) {
if (entities.length == 0) {
return;
}

var charIndex = text.length - 1;
// replace surrogate pairs with whitespace, and then count length
var codePointIndex = text.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, " ").length - 1;
var entityIndex = entities.length - 1;
var entity = entities[entityIndex];

while (charIndex != codePointIndex) {
if (entity.indices[0] == (indicesInUTF16 ? charIndex : codePointIndex)) {
var len = entity.indices[1] - entity.indices[0];
entity.indices[0] = indicesInUTF16 ? codePointIndex : charIndex;
entity.indices[1] = entity.indices[0] + len;

if (entityIndex == 0) {
// no more entity
break;
}
entity = entities[--entityIndex];
}

codePointIndex--;
charIndex--;
var c = text.charCodeAt(charIndex);
if (0xD800 <= c && c <= 0xDBFF && charIndex < text.length - 1) {
c = text.charCodeAt(charIndex + 1);
if (0xDC00 <= c && c <= 0xDFFF) {
charIndex--;
}
}
}
Expand Down

0 comments on commit a65f65f

Please sign in to comment.