Created
August 19, 2011 20:12
-
-
Save beatak/1157884 to your computer and use it in GitHub Desktop.
Force break line with using ­
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var shyizeElements = function ($elms, limit) { | |
$elms.each( | |
function (n, elm) { | |
var $elm = $(elm), | |
str = shyize($elm.text(), limit); | |
str = str.replace('<', '<', 'g').replace('>', '>', '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('­'); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment