Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

treat words divided by no-break space as a single token #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/truncate_html/html_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module TruncateHtml
class HtmlString < String

UNPAIRED_TAGS = %w(br hr img).freeze
REGEX = /(?:<script.*>.*<\/script>)+|<\/?[^>]+>|[[[:alpha:]][0-9]\|`~!@#\$%^&*\(\)\-_\+=\[\]{}:;'²³§",\.\/?]+|\s+|[[:punct:]]/.freeze
REGEX = /(?:<script.*>.*<\/script>)+|<\/?[^>]+>|[[[:alpha:]][0-9]\|`~!@#\$%^&*\(\)\-_\+=\[\]{}:;'²³§",\.\/?\u00a0]+|\s+|[[:punct:]]/.freeze

def initialize(original_html)
super(original_html)
Expand Down
4 changes: 2 additions & 2 deletions spec/truncate_html/html_string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ def html_string(original_string)

describe '#html_tokens' do
it 'returns each token in the string as an array element removing any consecutive whitespace from the string' do
html = '<h1>Hi there</h1> <p>This is sweet!</p> <p> squaremeter m² </p>'
html = "<h1>Hi there</h1> <p>This is sweet!</p> <p> squaremeter m² and no\u00a0break space </p>"
html_string(html).html_tokens.should == ['<h1>', 'Hi', ' ', 'there', '</h1>', ' ', '<p>', 'This', ' ', 'is', ' ', 'sweet!', '</p>',
' ', '<p>', ' ', 'squaremeter', ' ', 'm²', ' ', '</p>']
' ', '<p>', ' ', 'squaremeter', ' ', 'm²', ' ', 'and', ' ', "no break", ' ', 'space', ' ', '</p>']
end
end

Expand Down