1 // This file is part of Moodle - http://moodle.org/
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 * Module for text truncation.
19 * Implementation provided by Pathable (thanks!).
20 * See: https://github.com/pathable/truncate
22 * @module core/truncate
25 * @copyright 2017 Pathable
27 * 2017 Ryan Wyllie <ryan@moodle.com>
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 define(['jquery'], function($) {
32 // Matches trailing non-space characters.
33 var chop = /(\s*\S+|\s)$/;
35 // Matches the first word in the string.
38 // Matches any space characters.
41 // Special thanks to Mathias Bynens for the multi-byte char
42 // implementation. Much love.
43 // see: https://github.com/mathiasbynens/String.prototype.at/blob/master/at.js
44 var charLengthAt = function(text, position) {
48 var string = String(text);
49 var size = string.length;
51 var index = position ? Number(position) : 0;
52 if (index != index) { // better `isNaN`
55 // Account for out-of-bounds indices
56 // The odd lower bound is because the ToInteger operation is
57 // going to round `n` to `0` for `-1 < n <= 0`.
58 if (index <= -1 || index >= size) {
61 // Second half of `ToInteger`
63 // Get the first code unit and code unit value
64 var cuFirst = string.charCodeAt(index);
66 var nextIndex = index + 1;
68 if ( // Check if it’s the start of a surrogate pair.
69 cuFirst >= 0xD800 && cuFirst <= 0xDBFF && // high surrogate
70 size > nextIndex // there is a next code unit
72 cuSecond = string.charCodeAt(nextIndex);
73 if (cuSecond >= 0xDC00 && cuSecond <= 0xDFFF) { // low surrogate
80 var lengthMultiByte = function(text) {
83 for (var i = 0; i < text.length; i += charLengthAt(text, i)) {
90 var getSliceLength = function(text, amount) {
99 length += charLengthAt(text, length);
101 } while (length < text.length && count < amount);
106 // Return a truncated html string. Delegates to $.fn.truncate.
107 $.truncate = function(html, options) {
108 return $('<div></div>').append(html).truncate(options).html();
111 // Truncate the contents of an element in place.
112 $.fn.truncate = function(options) {
113 if ($.isNumeric(options)) options = {length: options};
114 var o = $.extend({}, $.truncate.defaults, options);
116 return this.each(function() {
119 if (o.noBreaks) self.find('br').replaceWith(' ');
121 var ellipsisLength = o.ellipsis.length;
122 var text = self.text();
123 var textLength = lengthMultiByte(text);
124 var excess = textLength - o.length + ellipsisLength;
126 if (textLength < o.length) return;
127 if (o.stripTags) self.text(text);
129 // Chop off any partial words if appropriate.
130 if (o.words && excess > 0) {
131 var sliced = text.slice(0, getSliceLength(text, o.length - ellipsisLength) + 1);
132 var replaced = sliced.replace(chop, '');
133 var truncated = lengthMultiByte(replaced);
134 var oneWord = sliced.match(space) ? false : true;
136 if (o.keepFirstWord && truncated === 0) {
137 excess = textLength - lengthMultiByte(start.exec(text)[0]) - ellipsisLength;
138 } else if (oneWord && truncated === 0) {
139 excess = textLength - o.length + ellipsisLength;
141 excess = textLength - truncated - 1;
145 // The requested length is larger than the text. No need for ellipsis.
146 if (excess > textLength) {
147 excess = textLength - o.length;
150 if (excess < 0 || !excess && !o.truncated) return;
152 // Iterate over each child node in reverse, removing excess text.
153 $.each(self.contents().get().reverse(), function(i, el) {
155 var text = $el.text();
156 var length = lengthMultiByte(text);
158 // If the text is longer than the excess, remove the node and continue.
159 if (length <= excess) {
166 // Remove the excess text and append the ellipsis.
167 if (el.nodeType === 3) {
168 var splitAmount = length - excess;
169 splitAmount = splitAmount >= 0 ? getSliceLength(text, splitAmount) : 0;
170 $(el.splitText(splitAmount)).replaceWith(o.ellipsis);
174 // Recursively truncate child nodes.
175 $el.truncate($.extend(o, {length: length - excess + ellipsisLength}));
181 $.truncate.defaults = {
183 // Strip all html elements, leaving only plain text.
186 // Only truncate at word boundaries.
189 // When 'words' is active, keeps the first word in the string
190 // even if it's longer than a target length.
191 keepFirstWord: false,
193 // Replace instances of <br> with a single space.
196 // The maximum length of the truncated html.
199 // The character to use as the ellipsis. The word joiner (U+2060) can be
200 // used to prevent a hanging ellipsis, but displays incorrectly in Chrome
202 // http://code.google.com/p/chromium/issues/detail?id=68323
203 //ellipsis: '\u2026' // '\u2060\u2026'
204 ellipsis: '\u2026' // '\u2060\u2026'
209 truncate: $.truncate,