truncate_html 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- truncate_html (0.9.1)
4
+ truncate_html (0.9.2)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/README.md CHANGED
@@ -4,10 +4,9 @@ TruncateHtml
4
4
  [![Build Status](https://secure.travis-ci.org/hgmnz/truncate_html.png?branch=master)](http://travis-ci.org/hgmnz/truncate_html)
5
5
  [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/hgmnz/truncate_html)
6
6
 
7
- truncate_html is a Rails helper plugin that [cuts off](http://www.youtube.com/watch?v=6XG4DIOA7nU) a string of HTML and takes care of closing any lingering open tags. There are many possible solutions to this. This plugin does not have any dependencies, and does all it's work via [regular expressions](http://xkcd.com/208/).
8
-
9
- The API is very similar to Rails' own <code>truncate()</code> method.
7
+ truncate_html cuts off a string of HTML and takes care of closing any lingering open tags. There are many ways to solve this. This library does not have any dependencies, and [parses HTML using regular expressions](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454).
10
8
 
9
+ It can be used with or without Rails.
11
10
 
12
11
  Example
13
12
  -------
@@ -21,13 +21,17 @@ module TruncateHtml
21
21
  end
22
22
 
23
23
  def html_tag?
24
- /<\/?[^>]+>/ === self
24
+ /<\/?[^>]+>/ === self && !html_comment?
25
25
  end
26
26
 
27
27
  def open_tag?
28
28
  /<(?!(?:#{UNPAIRED_TAGS.join('|')}|script|\/))[^>]+>/i === self
29
29
  end
30
30
 
31
+ def html_comment?
32
+ /<\s?!--.*-->/ === self
33
+ end
34
+
31
35
  def matching_close_tag
32
36
  gsub(/<(\w+)\s?.*>/, '</\1>').strip
33
37
  end
@@ -51,7 +51,7 @@ module TruncateHtml
51
51
  else
52
52
  remove_latest_open_tag(token)
53
53
  end
54
- else
54
+ elsif !token.html_comment?
55
55
  @chars_remaining -= (@word_boundary ? token.length : token[0, @chars_remaining].length)
56
56
  if @chars_remaining <= 0
57
57
  @truncated_html[-1] = @truncated_html[-1].rstrip + @omission
@@ -60,7 +60,7 @@ module TruncateHtml
60
60
  end
61
61
 
62
62
  def append_to_result(token)
63
- if token.html_tag?
63
+ if token.html_tag? || token.html_comment?
64
64
  @truncated_html << token
65
65
  elsif @word_boundary
66
66
  @truncated_html << token if (@chars_remaining - token.length) >= 0
@@ -1,3 +1,3 @@
1
1
  module TruncateHtml
2
- VERSION = "0.9.1"
2
+ VERSION = "0.9.2"
3
3
  end
@@ -22,6 +22,10 @@ describe TruncateHtml::HtmlString do
22
22
  html_string('<img src="foo">').should be_html_tag
23
23
  html_string('</img>').should be_html_tag
24
24
  end
25
+
26
+ it 'is false for html comments' do
27
+ html_string('<!-- hi -->').should_not be_html_tag
28
+ end
25
29
  end
26
30
 
27
31
  describe '#open_tag?' do
@@ -64,4 +68,13 @@ describe TruncateHtml::HtmlString do
64
68
  end
65
69
  end
66
70
  end
71
+
72
+ describe '#html_comment?' do
73
+ it 'is true for HTML comments' do
74
+ html_string('<!-- hi -->').should be_html_comment
75
+ html_string('<a>').should_not be_html_comment
76
+ html_string('</a>').should_not be_html_comment
77
+ html_string('foo').should_not be_html_comment
78
+ end
79
+ end
67
80
  end
@@ -194,4 +194,10 @@ describe TruncateHtml::HtmlTruncator do
194
194
  end
195
195
  end
196
196
 
197
+ context 'a string with comments' do
198
+ it 'does not duplicate comments (issue #32)' do
199
+ truncate('<h1>hello <!-- stuff --> and <!-- la --> goodbye</h1>', length: 15).should ==
200
+ '<h1>hello <!-- stuff --> and <!-- la -->...</h1>'
201
+ end
202
+ end
197
203
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: truncate_html
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-05 00:00:00.000000000 Z
12
+ date: 2013-01-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec-rails
16
- requirement: &70114780358240 !ruby/object:Gem::Requirement
16
+ requirement: &70161209065700 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '2.9'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70114780358240
24
+ version_requirements: *70161209065700
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rails
27
- requirement: &70114780357540 !ruby/object:Gem::Requirement
27
+ requirement: &70161209065240 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 3.0.3
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70114780357540
35
+ version_requirements: *70161209065240
36
36
  description: Truncates html so you don't have to
37
37
  email:
38
38
  - harold.gimenez@gmail.com
@@ -105,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
105
  version: '0'
106
106
  segments:
107
107
  - 0
108
- hash: -1260152275599267568
108
+ hash: -2711224977886415095
109
109
  requirements: []
110
110
  rubyforge_project:
111
111
  rubygems_version: 1.8.10