Created
September 22, 2009 15:33
-
-
Save collectiveidea/191160 to your computer and use it in GitHub Desktop.
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
jQuery.fn.wordWrap = function () { | |
// This splits any text node by spaces, and any 'word' that is longer than 26 characters it | |
// splits apart each character by a <wbr> tag so it breaks if it needs to. | |
return this.each(function () { | |
var walker = document.createTreeWalker(this, NodeFilter.SHOW_TEXT, null, false); | |
while (walker.nextNode()) { | |
var node = walker.currentNode; | |
var parts = node.nodeValue.split(' '); | |
for (i = 0; i < parts.length; i++) { | |
if (parts[i].length > 26) parts[i] = parts[i].split('').join(String.fromCharCode('8203')) | |
} | |
node.nodeValue = parts.join(' '); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment