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 * Description of import/upgrade into Moodle:
18 * 1.) Download from https://github.com/pathable/truncate
19 * 2.) Copy jquery.truncate.js into lib/amd/src/truncate.js
20 * 3.) Edit truncate.js to return the $.truncate function as truncate
21 * 4.) Apply Moodle changes from git commit 7172b33e241c4d42cff01f78bf8570408f43fdc2
25 * Module for text truncation.
27 * Implementation provided by Pathable (thanks!).
28 * See: https://github.com/pathable/truncate
30 * @module core/truncate
33 * @copyright 2017 Pathable
35 * 2017 Ryan Wyllie <ryan@moodle.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 define(['jquery'], function($) {
40 // Matches trailing non-space characters.
41 var chop = /(\s*\S+|\s)$/;
43 // Matches the first word in the string.
46 // Matches any space characters.
49 // Special thanks to Mathias Bynens for the multi-byte char
50 // implementation. Much love.
51 // see: https://github.com/mathiasbynens/String.prototype.at/blob/master/at.js
52 var charLengthAt = function(text, position) {
56 var string = String(text);
57 var size = string.length;
59 var index = position ? Number(position) : 0;
60 if (index != index) { // better `isNaN`
63 // Account for out-of-bounds indices
64 // The odd lower bound is because the ToInteger operation is
65 // going to round `n` to `0` for `-1 < n <= 0`.
66 if (index <= -1 || index >= size) {
69 // Second half of `ToInteger`
71 // Get the first code unit and code unit value
72 var cuFirst = string.charCodeAt(index);
74 var nextIndex = index + 1;
76 if ( // Check if it’s the start of a surrogate pair.
77 cuFirst >= 0xD800 && cuFirst <= 0xDBFF && // high surrogate
78 size > nextIndex // there is a next code unit
80 cuSecond = string.charCodeAt(nextIndex);
81 if (cuSecond >= 0xDC00 && cuSecond <= 0xDFFF) { // low surrogate
88 var lengthMultiByte = function(text) {
91 for (var i = 0; i < text.length; i += charLengthAt(text, i)) {
98 var getSliceLength = function(text, amount) {
107 length += charLengthAt(text, length);
109 } while (length < text.length && count < amount);
114 // Return a truncated html string. Delegates to $.fn.truncate.
115 $.truncate = function(html, options) {
116 return $('<div></div>').append(html).truncate(options).html();
119 // Truncate the contents of an element in place.
120 $.fn.truncate = function(options) {
121 if ($.isNumeric(options)) options = {length: options};
122 var o = $.extend({}, $.truncate.defaults, options);
124 return this.each(function() {
127 if (o.noBreaks) self.find('br').replaceWith(' ');
129 var ellipsisLength = o.ellipsis.length;
130 var text = self.text();
131 var textLength = lengthMultiByte(text);
132 var excess = textLength - o.length + ellipsisLength;
134 if (textLength < o.length) return;
135 if (o.stripTags) self.text(text);
137 // Chop off any partial words if appropriate.
138 if (o.words && excess > 0) {
139 var sliced = text.slice(0, getSliceLength(text, o.length - ellipsisLength) + 1);
140 var replaced = sliced.replace(chop, '');
141 var truncated = lengthMultiByte(replaced);
142 var oneWord = sliced.match(space) ? false : true;
144 if (o.keepFirstWord && truncated === 0) {
145 excess = textLength - lengthMultiByte(start.exec(text)[0]) - ellipsisLength;
146 } else if (oneWord && truncated === 0) {
147 excess = textLength - o.length + ellipsisLength;
149 excess = textLength - truncated - 1;
153 // The requested length is larger than the text. No need for ellipsis.
154 if (excess > textLength) {
155 excess = textLength - o.length;
158 if (excess < 0 || !excess && !o.truncated) return;
160 // Iterate over each child node in reverse, removing excess text.
161 $.each(self.contents().get().reverse(), function(i, el) {
163 var text = $el.text();
164 var length = lengthMultiByte(text);
166 // If the text is longer than the excess, remove the node and continue.
167 if (length <= excess) {
174 // Remove the excess text and append the ellipsis.
175 if (el.nodeType === 3) {
176 var splitAmount = length - excess;
177 splitAmount = splitAmount >= 0 ? getSliceLength(text, splitAmount) : 0;
178 $(el.splitText(splitAmount)).replaceWith(o.ellipsis);
182 // Recursively truncate child nodes.
183 $el.truncate($.extend(o, {length: length - excess + ellipsisLength}));
189 $.truncate.defaults = {
191 // Strip all html elements, leaving only plain text.
194 // Only truncate at word boundaries.
197 // When 'words' is active, keeps the first word in the string
198 // even if it's longer than a target length.
199 keepFirstWord: false,
201 // Replace instances of <br> with a single space.
204 // The maximum length of the truncated html.
207 // The character to use as the ellipsis. The word joiner (U+2060) can be
208 // used to prevent a hanging ellipsis, but displays incorrectly in Chrome
210 // http://code.google.com/p/chromium/issues/detail?id=68323
211 //ellipsis: '\u2026' // '\u2060\u2026'
212 ellipsis: '\u2026' // '\u2060\u2026'
217 truncate: $.truncate,