Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Long unbroken runs of text without whitespace in comments are now bro…
Browse files Browse the repository at this point in the history
…ken up with <wbr /> tags to make them wrap without breaking the site layout.
  • Loading branch information
rgrove committed Feb 2, 2009
1 parent 7e16447 commit c53ca1d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
2 changes: 2 additions & 0 deletions HISTORY
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Version 0.3.0 (?)
* Twitter plugin now excludes replies by default.
* Comment HTML sanitization was separated out into a new gem, Sanitize.
* Comment titles are now limited to 100 characters instead of 255.
* Long unbroken runs of text without whitespace in comments are now broken up
with <wbr /> tags to make them wrap without breaking the site layout.
* Fixed a bug that caused an invalid path to be used for media files uploaded
via Internet Explorer.

Expand Down
47 changes: 45 additions & 2 deletions lib/thoth/model/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#++

require 'digest/md5'
require 'strscan'

module Thoth
class Comment < Sequel::Model
Expand Down Expand Up @@ -103,14 +104,14 @@ def body=(body)
redcloth = RedCloth.new(body, [:filter_styles])

self[:body] = body
self[:body_rendered] = Sanitize.clean(redcloth.to_html(
self[:body_rendered] = insert_breaks(Sanitize.clean(redcloth.to_html(
:refs_textile,
:block_textile_lists,
:inline_textile_link,
:inline_textile_code,
:glyphs_textile,
:inline_textile_span
), CONFIG_SANITIZE)
), CONFIG_SANITIZE))
end

# Gets the creation time of this comment. If _format_ is provided, the time
Expand Down Expand Up @@ -158,5 +159,47 @@ def updated_at(format = nil)
def url
new? ? '#' : post.url + "#comment-#{id}"
end

protected

# Inserts <wbr /> tags in long strings without spaces, while being careful
# not to break HTML tags.
def insert_breaks(str, length = 30)
scanner = StringScanner.new(str)

char = ''
count = 0
in_tag = 0
new_str = ''

while char = scanner.getch do
case char
when '<'
in_tag += 1

when '>'
in_tag -= 1
in_tag = 0 if in_tag < 0

when /\s/
count = 0 if in_tag == 0

else
if in_tag == 0
if count == length
new_str << '<wbr />'
count = 0
end

count += 1
end
end

new_str << char
end

return new_str
end

end
end

0 comments on commit c53ca1d

Please sign in to comment.