Skip to content

Commit

Permalink
Merge pull request #18 from mattly/widont-short-headlines
Browse files Browse the repository at this point in the history
Widont short headlines
  • Loading branch information
ekalinin committed Jun 2, 2014
2 parents cc76505 + b1411ef commit 4751715
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 8 additions & 2 deletions test/typogr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@ module.exports = {
assert.equal(tp.widont(' Test'), ' Test');
assert.equal(tp.widont('<ul><li>Test</p></li><ul>'), '<ul><li>Test</p></li><ul>');
assert.equal(tp.widont('<ul><li> Test</p></li><ul>'), '<ul><li> Test</p></li><ul>');
assert.equal(tp.widont('<p>In a couple of paragraphs</p><p>paragraph two</p>'),
'<p>In a couple of&nbsp;paragraphs</p><p>paragraph&nbsp;two</p>');
// neither should two or three word items
assert.equal(tp.widont('<h1>One Two</h1>'), '<h1>One Two</h1>')
assert.equal(tp.widont('<h1>One Two Three</h1>'), '<h1>One Two Three</h1>')
assert.equal(tp.widont('<h1>One Two Three</h1>'), '<h1>One Two Three</h1>')
assert.equal(tp.widont('<h1><a href="#">Links</a> should work</h1>'), '<h1><a href="#">Links</a> should work</h1>')

assert.equal(tp.widont('<p>In a couple of paragraphs</p><p>the paragraph number two</p>'),
'<p>In a couple of&nbsp;paragraphs</p><p>the paragraph number&nbsp;two</p>');
assert.equal(tp.widont('<h1><a href="#">In a link inside a heading</i> </a></h1>'),
'<h1><a href="#">In a link inside a&nbsp;heading</i> </a></h1>');
assert.equal(tp.widont('<h1><a href="#">In a link</a> followed by other text</h1>'),
Expand Down
7 changes: 6 additions & 1 deletion typogr.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,13 @@
*
*/
var widont = typogr.widont = function(text) {
var inline_tags = 'a|em|span|strong|i|b'
var word = '(?:<(?:'+inline_tags+')[^>]*?>)*?[^\\s<>]+(?:</(?:'+inline_tags+')[^>]*?>)*?'
var re_widont = re(
'([\\w,])'+ // a word boundry character or comma, matching group 1
'('+ // matching group 1
'\\s+'+word+ // space and a word with a possible bordering tag
'\\s+'+word+ // space and a word with a possible bordering tag
')'+
'(?:\\s+)'+ // one or more space characters
'('+ // matching group 2
'[^<>\\s]+'+ // nontag/nonspace characters
Expand Down

0 comments on commit 4751715

Please sign in to comment.