-
Notifications
You must be signed in to change notification settings - Fork 115
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
String is truncated even when shorter than length #54
Comments
My guess is that this is not supposed to be the expected behavior. The method should only truncate the string if the string length is bigger than the desired length, and right now is concatenating the string and the omission before getting the length. Im going to try to work on a fix for this. |
Well, now Im doubting of my own guess... Was looking at the current tests, and found this couple: context 'when the word_boundary option is set to false' do
...
it 'retains the tags within the text' do
html = 'some text <span class="caps">CAPS</span> some text'
truncate(html, :length => 25, :word_boundary => false).should == 'some text <span class="caps">CAPS</span> some te...'
end
context 'and a custom omission value is passed' do
it 'retains the omission text' do
truncate("testtest", :length => 10, :omission => '..', :word_boundary => false).should == 'testtest..'
end
..
end
end This tests are not suppose to make sure that the gem have this specific behavior, but looking at them, it gives the impression that it is expected to include the omission characters even if the given string is shorter than the I think that the behavior should be the one @rafaelpetry was suggesting, and that the test he wrote should pass. (If you have a string like "testtest", truncated to 10 characters, I think that anyone would expect to get "testtest" instead of "testtest.."). Im going to start working in some changes to get this new behavior and to make this couple of tests work with it. @hgmnz, please let me know if you still think that you want to keep the same behavior. |
If I try to truncate a string using an omission, the string may get truncated even when it is shorter than the length. This happens when the combined length of the string and the omission is greater than the
:length
option. For instance, if you add the following test tohtml_truncator_spec.rb
...... it will fail with the following error:
This happens even though
"some string"
is shorter than 12 characters. Is this expected behavior? It differs from the behavior of truncate from Active Support, where"some string".truncate(12, omission: '...') == "some string"
.The text was updated successfully, but these errors were encountered: