Skip to content

Instantly share code, notes, and snippets.

@paulgroves
Last active December 31, 2015 00:38
Show Gist options
  • Save paulgroves/7908278 to your computer and use it in GitHub Desktop.
Save paulgroves/7908278 to your computer and use it in GitHub Desktop.
Coffeescript to prevent widows in title elements, can cope with nested HTML such as a or span elements
jQuery.fn.justtext = ->
$(this).clone().children().remove().end().text()
prevent_widows = () ->
$("h1,h2,h3,h4,h5,h6").each (i, e) =>
t = $.trim($(e).justtext()).replace(/\s([^\s]*)$/,'\xa0'+'$1')
cache = $(e).children()
$(e).text(t).append cache
$(e).find('*').each (i, e) =>
t = $.trim($(e).justtext()).replace(/\s([^\s]*)$/,'\xa0'+'$1')
cache = $(e).children()
$(e).text(t).append cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment