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

String is truncated even when shorter than length #54

Open
rafaelpetry opened this issue Jul 28, 2014 · 2 comments
Open

String is truncated even when shorter than length #54

rafaelpetry opened this issue Jul 28, 2014 · 2 comments

Comments

@rafaelpetry
Copy link

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 to html_truncator_spec.rb ...

it 'does not truncate a string shorter than length' do
  truncate('some string', length: 12, omission: '...').should == 'some string'
end

... it will fail with the following error:

1) TruncateHtml::HtmlTruncator does not truncate a string shorter than length
   Failure/Error: truncate('some string', length: 12, omission: '...').should == 'some string'
     expected: "some string"
          got: "some..." (using ==)
   # ./spec/truncate_html/html_truncator_spec.rb:62:in `block (2 levels) in <top (required)>'

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".

@AlexGunslinger
Copy link

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.

@AlexGunslinger
Copy link

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 :length (in both cases, the string after removing the tags are less than 25 and 10 characters).

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants