Skip to content

Instantly share code, notes, and snippets.

@asha23
Created November 18, 2013 11:31
Show Gist options
  • Save asha23/7526355 to your computer and use it in GitHub Desktop.
Save asha23/7526355 to your computer and use it in GitHub Desktop.
Widow fix (jQuery)
(function($) {
jQuery.fn.widowFix = function(userOptions) {
var defaults = {
letterLimit: null,
prevLimit: null,
linkFix: false,
dashes: false
};
var wfOptions = $.extend(defaults, userOptions);
if (this.length) {
return this.each(function() {
var $this = $(this);
var linkFixLastWord;
if (wfOptions.linkFix) {
var $linkHolder = $this.find('a:last');
//find the anchors and wrap them up with a <var> tag to find it later
$linkHolder.wrap('<var>');
//store the anchor inside
var $lastLink = $('var').html();
//get the real length of the last word
linkFixLastWord = $linkHolder.contents()[0];
//remove the anchor
$linkHolder.contents().unwrap();
}
var contentArray = $(this).html().split(' '),
lastWord = contentArray.pop();
if (contentArray.length <= 1) {
// it's a one word element, abort!
return;
}
function checkSpace() {
if (lastWord === '') {
// trailing space found, pop it off and check again
lastWord = contentArray.pop();
checkSpace();
}
}
checkSpace();
// if contains a dash, use white-space nowrap to stop breaking
if (wfOptions.dashes) {
// all 3 dash types: regular, en, em
var dashes = ['-', '–', '—'];
$.each(dashes, function(index, dash) {
if (lastWord.indexOf(dash) > 0) {
lastWord = '<span style="white-space:nowrap;">' + lastWord + '</span>';
return false; // break out early
}
});
}
var prevWord = contentArray[contentArray.length - 1];
//if linkFix is on, check for the letter limit
if (wfOptions.linkFix) {
//if the last word is longer than the limit, stop the script
if (wfOptions.letterLimit !== null && linkFixLastWord.length >= wfOptions.letterLimit) {
$this.find('var').each(function() {
$(this).contents().replaceWith($lastLink);
$(this).contents().unwrap();
});
return;
//or if the prev word is longer than the limit
} else if (wfOptions.prevLimit !== null && prevWord.length >= wfOptions.prevLimit) {
$this.find('var').each(function() {
$(this).contents().replaceWith($lastLink);
$(this).contents().unwrap();
});
return;
}
} else {
//if the last word is longer than the limit, stop the script
if (wfOptions.letterLimit !== null && lastWord.length >= wfOptions.letterLimit) {
return;
} else if (wfOptions.prevLimit !== null && prevWord.length >= wfOptions.prevLimit) {
return;
}
}
var content = contentArray.join(' ') + '&nbsp;' + lastWord;
$this.html(content);
if (wfOptions.linkFix) {
//find the var and put the anchor back in, then unwrap the <var>
$this.find('var').each(function() {
$(this).contents().replaceWith($lastLink);
$(this).contents().unwrap();
});
}
});
}
};
})(jQuery);
// Apply widow fix to H1 tag
$('h1').widowFix();
$('p').widowFix();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment