mirror of
https://github.com/moodle/moodle.git
synced 2025-01-19 06:18:28 +01:00
1 line
9.4 KiB
Plaintext
1 line
9.4 KiB
Plaintext
{"version":3,"file":"truncate.min.js","sources":["../src/truncate.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Description of import/upgrade into Moodle:\n * 1.) Download from https://github.com/pathable/truncate\n * 2.) Copy jquery.truncate.js into lib/amd/src/truncate.js\n * 3.) Edit truncate.js to return the $.truncate function as truncate\n * 4.) Apply Moodle changes from git commit 7172b33e241c4d42cff01f78bf8570408f43fdc2\n */\n\n/**\n * Module for text truncation.\n *\n * Implementation provided by Pathable (thanks!).\n * See: https://github.com/pathable/truncate\n *\n * @module core/truncate\n * @copyright 2017 Pathable\n * 2017 Mathias Bynens\n * 2017 Ryan Wyllie <ryan@moodle.com>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine(['jquery'], function($) {\n\n // Matches trailing non-space characters.\n var chop = /(\\s*\\S+|\\s)$/;\n\n // Matches the first word in the string.\n var start = /^(\\S*)/;\n\n // Matches any space characters.\n var space = /\\s/;\n\n // Special thanks to Mathias Bynens for the multi-byte char\n // implementation. Much love.\n // see: https://github.com/mathiasbynens/String.prototype.at/blob/master/at.js\n var charLengthAt = function(text, position) {\n var string = String(text);\n var size = string.length;\n // `ToInteger`\n var index = position ? Number(position) : 0;\n if (index != index) { // better `isNaN`\n index = 0;\n }\n // Account for out-of-bounds indices\n // The odd lower bound is because the ToInteger operation is\n // going to round `n` to `0` for `-1 < n <= 0`.\n if (index <= -1 || index >= size) {\n return '';\n }\n // Second half of `ToInteger`\n index = index | 0;\n // Get the first code unit and code unit value\n var cuFirst = string.charCodeAt(index);\n var cuSecond;\n var nextIndex = index + 1;\n var len = 1;\n if ( // Check if it’s the start of a surrogate pair.\n cuFirst >= 0xD800 && cuFirst <= 0xDBFF && // high surrogate\n size > nextIndex // there is a next code unit\n ) {\n cuSecond = string.charCodeAt(nextIndex);\n if (cuSecond >= 0xDC00 && cuSecond <= 0xDFFF) { // low surrogate\n len = 2;\n }\n }\n return len;\n };\n\n var lengthMultiByte = function(text) {\n var count = 0;\n\n for (var i = 0; i < text.length; i += charLengthAt(text, i)) {\n count++;\n }\n\n return count;\n };\n\n var getSliceLength = function(text, amount) {\n if (!text.length) {\n return 0;\n }\n\n var length = 0;\n var count = 0;\n\n do {\n length += charLengthAt(text, length);\n count++;\n } while (length < text.length && count < amount);\n\n return length;\n };\n\n // Return a truncated html string. Delegates to $.fn.truncate.\n $.truncate = function(html, options) {\n return $('<div></div>').append(html).truncate(options).html();\n };\n\n // Truncate the contents of an element in place.\n $.fn.truncate = function(options) {\n if (!isNaN(parseFloat(options))) options = {length: options};\n var o = $.extend({}, $.truncate.defaults, options);\n\n return this.each(function() {\n var self = $(this);\n\n if (o.noBreaks) self.find('br').replaceWith(' ');\n\n var ellipsisLength = o.ellipsis.length;\n var text = self.text();\n var textLength = lengthMultiByte(text);\n var excess = textLength - o.length + ellipsisLength;\n\n if (textLength < o.length) return;\n if (o.stripTags) self.text(text);\n\n // Chop off any partial words if appropriate.\n if (o.words && excess > 0) {\n var sliced = text.slice(0, getSliceLength(text, o.length - ellipsisLength) + 1);\n var replaced = sliced.replace(chop, '');\n var truncated = lengthMultiByte(replaced);\n var oneWord = sliced.match(space) ? false : true;\n\n if (o.keepFirstWord && truncated === 0) {\n excess = textLength - lengthMultiByte(start.exec(text)[0]) - ellipsisLength;\n } else if (oneWord && truncated === 0) {\n excess = textLength - o.length + ellipsisLength;\n } else {\n excess = textLength - truncated - 1;\n }\n }\n\n // The requested length is larger than the text. No need for ellipsis.\n if (excess > textLength) {\n excess = textLength - o.length;\n }\n\n if (excess < 0 || !excess && !o.truncated) return;\n\n // Iterate over each child node in reverse, removing excess text.\n $.each(self.contents().get().reverse(), function(i, el) {\n var $el = $(el);\n var text = $el.text();\n var length = lengthMultiByte(text);\n\n // If the text is longer than the excess, remove the node and continue.\n if (length <= excess) {\n o.truncated = true;\n excess -= length;\n $el.remove();\n return;\n }\n\n // Remove the excess text and append the ellipsis.\n if (el.nodeType === 3) {\n var splitAmount = length - excess;\n splitAmount = splitAmount >= 0 ? getSliceLength(text, splitAmount) : 0;\n $(el.splitText(splitAmount)).replaceWith(o.ellipsis);\n return false;\n }\n\n // Recursively truncate child nodes.\n $el.truncate($.extend(o, {length: length - excess + ellipsisLength}));\n return false;\n });\n });\n };\n\n $.truncate.defaults = {\n\n // Strip all html elements, leaving only plain text.\n stripTags: false,\n\n // Only truncate at word boundaries.\n words: false,\n\n // When 'words' is active, keeps the first word in the string\n // even if it's longer than a target length.\n keepFirstWord: false,\n\n // Replace instances of <br> with a single space.\n noBreaks: false,\n\n // The maximum length of the truncated html.\n length: Infinity,\n\n // The character to use as the ellipsis. The word joiner (U+2060) can be\n // used to prevent a hanging ellipsis, but displays incorrectly in Chrome\n // on Windows 7.\n // http://code.google.com/p/chromium/issues/detail?id=68323\n //ellipsis: '\\u2026' // '\\u2060\\u2026'\n ellipsis: '\\u2026' // '\\u2060\\u2026'\n\n };\n\n return {\n truncate: $.truncate,\n };\n});\n"],"names":["define","$","chop","start","space","charLengthAt","text","position","string","String","size","length","index","Number","cuSecond","cuFirst","charCodeAt","nextIndex","len","lengthMultiByte","count","i","getSliceLength","amount","truncate","html","options","append","fn","isNaN","parseFloat","o","extend","defaults","this","each","self","noBreaks","find","replaceWith","ellipsisLength","ellipsis","textLength","excess","stripTags","words","sliced","slice","replaced","replace","truncated","oneWord","match","keepFirstWord","exec","contents","get","re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