Skip to content

Instantly share code, notes, and snippets.

@r4fx
Created April 21, 2016 07:33
Show Gist options
  • Save r4fx/21b2e1fc4d2a6902a6f7fa8c219b0549 to your computer and use it in GitHub Desktop.
Save r4fx/21b2e1fc4d2a6902a6f7fa8c219b0549 to your computer and use it in GitHub Desktop.
Orphans fix
/*
* Usage
*
$(function () {
$(".todayMenu article").removeOrphans();
});
*/
// using regexp to every single char
// having spaces before and after of its
// we replace a space with a hard space -  
// which - naturally - merges this char with the next word / char etc. which have 1,2 or 3 signs
$.fn.removeOrphans = function () {
$(this).each(function () {
if ($(this).length > 0) {
var $html = $(this).html();
$html = $html.replace(/(\s)([^\s]{1,3})[\s]+/g, "$1$2 ");
$(this).empty().html($html);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment