Skip to content

Commit

Permalink
soyutils: Fix invalid comparison of char (string) with numbers
Browse files Browse the repository at this point in the history
The code in truncate that avoids truncating in the middle of a
surrogate pair was just plain broken.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=131167671
  • Loading branch information
msamuel authored and mikesamuel committed Aug 25, 2016
1 parent 7a1cb41 commit 5b1e01c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
20 changes: 10 additions & 10 deletions javascript/soyutils_usegoog.js
Expand Up @@ -1747,8 +1747,8 @@ soy.$$truncate = function(str, maxLen, doAddEllipsis) {
}

// Make sure truncating at maxLen doesn't cut up a unicode surrogate pair.
if (soy.$$isHighSurrogate_(str.charAt(maxLen - 1)) &&
soy.$$isLowSurrogate_(str.charAt(maxLen))) {
if (soy.$$isHighSurrogate_(str.charCodeAt(maxLen - 1)) &&
soy.$$isLowSurrogate_(str.charCodeAt(maxLen))) {
maxLen -= 1;
}

Expand All @@ -1765,22 +1765,22 @@ soy.$$truncate = function(str, maxLen, doAddEllipsis) {

/**
* Private helper for $$truncate() to check whether a char is a high surrogate.
* @param {string} ch The char to check.
* @return {boolean} Whether the given char is a unicode high surrogate.
* @param {number} cc The codepoint to check.
* @return {boolean} Whether the given codepoint is a unicode high surrogate.
* @private
*/
soy.$$isHighSurrogate_ = function(ch) {
return 0xD800 <= ch && ch <= 0xDBFF;
soy.$$isHighSurrogate_ = function(cc) {
return 0xD800 <= cc && cc <= 0xDBFF;
};

/**
* Private helper for $$truncate() to check whether a char is a low surrogate.
* @param {string} ch The char to check.
* @return {boolean} Whether the given char is a unicode low surrogate.
* @param {number} cc The codepoint to check.
* @return {boolean} Whether the given codepoint is a unicode low surrogate.
* @private
*/
soy.$$isLowSurrogate_ = function(ch) {
return 0xDC00 <= ch && ch <= 0xDFFF;
soy.$$isLowSurrogate_ = function(cc) {
return 0xDC00 <= cc && cc <= 0xDFFF;
};


Expand Down
24 changes: 12 additions & 12 deletions testdata/javascript/soy_usegoog_lib.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5b1e01c

Please sign in to comment.