Last active
December 31, 2015 00:38
-
-
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
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.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