Commit 7172b33e authored by Ryan Wyllie's avatar Ryan Wyllie
Browse files

MDL-57972 javascript: Change truncate.js behaviour

Updated truncate.js to behave closer to the moodle implementation of
shorten_text.

Part of MDL-55611
parent d74d91f4
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
......@@ -23,6 +23,8 @@
* @package core
* @class truncate
* @copyright 2017 Pathable
* 2017 Mathias Bynens
* 2017 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(['jquery'], function($) {
......@@ -33,6 +35,74 @@ define(['jquery'], function($) {
// Matches the first word in the string.
var start = /^(\S*)/;
// Matches any space characters.
var space = /\s/;
// Special thanks to Mathias Bynens for the multi-byte char
// implementation. Much love.
// see: https://github.com/mathiasbynens/String.prototype.at/blob/master/at.js
var charLengthAt = function(text, position) {
if (this == null) {
throw TypeError();
}
var string = String(text);
var size = string.length;
// `ToInteger`
var index = position ? Number(position) : 0;
if (index != index) { // better `isNaN`
index = 0;
}
// Account for out-of-bounds indices
// The odd lower bound is because the ToInteger operation is
// going to round `n` to `0` for `-1 < n <= 0`.
if (index <= -1 || index >= size) {
return '';
}
// Second half of `ToInteger`
index = index | 0;
// Get the first code unit and code unit value
var cuFirst = string.charCodeAt(index);
var cuSecond;
var nextIndex = index + 1;
var len = 1;
if ( // Check if it’s the start of a surrogate pair.
cuFirst >= 0xD800 && cuFirst <= 0xDBFF && // high surrogate
size > nextIndex // there is a next code unit
) {
cuSecond = string.charCodeAt(nextIndex);
if (cuSecond >= 0xDC00 && cuSecond <= 0xDFFF) { // low surrogate
len = 2;
}
}
return len;
};
var lengthMultiByte = function(text) {
var count = 0;
for (var i = 0; i < text.length; i += charLengthAt(text, i)) {
count++;
}
return count;
};
var getSliceLength = function(text, amount) {
if (!text.length) {
return 0;
}
var length = 0;
var count = 0;
do {
length += charLengthAt(text, length);
count++;
} while (length < text.length && count < amount);
return length;
};
// Return a truncated html string. Delegates to $.fn.truncate.
$.truncate = function(html, options) {
return $('<div></div>').append(html).truncate(options).html();
......@@ -48,29 +118,42 @@ define(['jquery'], function($) {
if (o.noBreaks) self.find('br').replaceWith(' ');
var ellipsisLength = o.ellipsis.length;
var text = self.text();
var excess = text.length - o.length;
var textLength = lengthMultiByte(text);
var excess = textLength - o.length + ellipsisLength;
if (textLength < o.length) return;
if (o.stripTags) self.text(text);
// Chop off any partial words if appropriate.
if (o.words && excess > 0) {
var truncated = text.slice(0, o.length).replace(chop, '').length;
var sliced = text.slice(0, getSliceLength(text, o.length - ellipsisLength) + 1);
var replaced = sliced.replace(chop, '');
var truncated = lengthMultiByte(replaced);
var oneWord = sliced.match(space) ? false : true;
if (o.keepFirstWord && truncated === 0) {
excess = text.length - start.exec(text)[0].length - 1;
excess = textLength - lengthMultiByte(start.exec(text)[0]) - ellipsisLength;
} else if (oneWord && truncated === 0) {
excess = textLength - o.length + ellipsisLength;
} else {
excess = text.length - truncated - 1;
excess = textLength - truncated - 1;
}
}
// The requested length is larger than the text. No need for ellipsis.
if (excess > textLength) {
excess = textLength - o.length;
}
if (excess < 0 || !excess && !o.truncated) return;
// Iterate over each child node in reverse, removing excess text.
$.each(self.contents().get().reverse(), function(i, el) {
var $el = $(el);
var text = $el.text();
var length = text.length;
var length = lengthMultiByte(text);
// If the text is longer than the excess, remove the node and continue.
if (length <= excess) {
......@@ -82,12 +165,14 @@ define(['jquery'], function($) {
// Remove the excess text and append the ellipsis.
if (el.nodeType === 3) {
$(el.splitText(length - excess - 1)).replaceWith(o.ellipsis);
var splitAmount = length - excess;
splitAmount = splitAmount >= 0 ? getSliceLength(text, splitAmount) : 0;
$(el.splitText(splitAmount)).replaceWith(o.ellipsis);
return false;
}
// Recursively truncate child nodes.
$el.truncate($.extend(o, {length: length - excess}));
$el.truncate($.extend(o, {length: length - excess + ellipsisLength}));
return false;
});
});
......@@ -115,6 +200,7 @@ define(['jquery'], function($) {
// used to prevent a hanging ellipsis, but displays incorrectly in Chrome
// on Windows 7.
// http://code.google.com/p/chromium/issues/detail?id=68323
//ellipsis: '\u2026' // '\u2060\u2026'
ellipsis: '\u2026' // '\u2060\u2026'
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment