OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2007 Apple Inc. All rights reserved. | 2 * Copyright (C) 2007 Apple Inc. All rights reserved. |
3 * Copyright (C) 2012 Google Inc. All rights reserved. | 3 * Copyright (C) 2012 Google Inc. All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * | 8 * |
9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
153 */ | 153 */ |
154 String.prototype.collapseWhitespace = function() { | 154 String.prototype.collapseWhitespace = function() { |
155 return this.replace(/[\s\xA0]+/g, ' '); | 155 return this.replace(/[\s\xA0]+/g, ' '); |
156 }; | 156 }; |
157 | 157 |
158 /** | 158 /** |
159 * @param {number} maxLength | 159 * @param {number} maxLength |
160 * @return {string} | 160 * @return {string} |
161 */ | 161 */ |
162 String.prototype.trimMiddle = function(maxLength) { | 162 String.prototype.trimMiddle = function(maxLength) { |
163 var indices = this.trimMiddleIndices(maxLength); | |
164 if (indices === null) | |
165 return String(this); | |
166 return this.substr(0, indices[0]) + '\u2026' + this.substr(indices[1], this.le ngth - indices[1]); | |
167 }; | |
168 | |
169 /** | |
170 * @param {number} maxLength | |
171 * @return {?Array<number>} | |
172 */ | |
173 String.prototype.trimMiddleIndices = function(maxLength) { | |
lushnikov
2017/04/17 17:57:40
Let's not introduce this method - it would be hard
luoe
2017/04/17 22:15:07
Done.
| |
163 if (this.length <= maxLength) | 174 if (this.length <= maxLength) |
164 return String(this); | 175 return null; |
165 var leftHalf = maxLength >> 1; | 176 var leftHalf = maxLength >> 1; |
166 var rightHalf = maxLength - leftHalf - 1; | 177 var rightHalf = maxLength - leftHalf - 1; |
167 if (this.codePointAt(this.length - rightHalf - 1) >= 0x10000) { | 178 if (this.codePointAt(this.length - rightHalf - 1) >= 0x10000) { |
lushnikov
2017/04/17 17:57:40
Wow, something is going on here! Do you know why w
luoe
2017/04/17 22:15:07
Emoji characters have length > 1, and we do not br
| |
168 --rightHalf; | 179 --rightHalf; |
169 ++leftHalf; | 180 ++leftHalf; |
170 } | 181 } |
171 if (leftHalf > 0 && this.codePointAt(leftHalf - 1) >= 0x10000) | 182 if (leftHalf > 0 && this.codePointAt(leftHalf - 1) >= 0x10000) |
172 --leftHalf; | 183 --leftHalf; |
173 return this.substr(0, leftHalf) + '\u2026' + this.substr(this.length - rightHa lf, rightHalf); | 184 return [leftHalf, this.length - rightHalf]; |
174 }; | 185 }; |
175 | 186 |
176 /** | 187 /** |
177 * @param {number} maxLength | 188 * @param {number} maxLength |
178 * @return {string} | 189 * @return {string} |
179 */ | 190 */ |
180 String.prototype.trimEnd = function(maxLength) { | 191 String.prototype.trimEnd = function(maxLength) { |
181 if (this.length <= maxLength) | 192 if (this.length <= maxLength) |
182 return String(this); | 193 return String(this); |
183 return this.substr(0, maxLength - 1) + '\u2026'; | 194 return this.substr(0, maxLength - 1) + '\u2026'; |
(...skipping 1253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1437 function windowLoaded() { | 1448 function windowLoaded() { |
1438 self.removeEventListener('DOMContentLoaded', windowLoaded, false); | 1449 self.removeEventListener('DOMContentLoaded', windowLoaded, false); |
1439 callback(); | 1450 callback(); |
1440 } | 1451 } |
1441 | 1452 |
1442 if (document.readyState === 'complete' || document.readyState === 'interactive ') | 1453 if (document.readyState === 'complete' || document.readyState === 'interactive ') |
1443 callback(); | 1454 callback(); |
1444 else | 1455 else |
1445 self.addEventListener('DOMContentLoaded', windowLoaded, false); | 1456 self.addEventListener('DOMContentLoaded', windowLoaded, false); |
1446 } | 1457 } |
OLD | NEW |