Skip to content

Instantly share code, notes, and snippets.

@beatak
Created August 19, 2011 20:12
Show Gist options
  • Save beatak/1157884 to your computer and use it in GitHub Desktop.
Save beatak/1157884 to your computer and use it in GitHub Desktop.
Force break line with using ­
var shyizeElements = function ($elms, limit) {
$elms.each(
function (n, elm) {
var $elm = $(elm),
str = shyize($elm.text(), limit);
str = str.replace('<', '&lt;', 'g').replace('>', '&gt;', 'g');
$elm.html(str);
}
);
};
var shyize = function (str, limit) {
var arr = str.split(' ');
for (var i = 0, len = arr.length; i < len; ++i) {
if (arr[i].length > limit) {
arr[i] = insertShy(arr[i], limit);
}
}
return arr.join(' ');
};
var insertShy = function (str, limit) {
var arr = [];
do {
arr[arr.length] = str.slice(0, limit);
str = str.slice(limit);
}
while (str.length > limit)
arr[arr.length] = str;
return arr.join('&shy;');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment