html-pipeline 1.10.0 → 1.11.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a78d7a993503d1652b0143b5c5cc5314251a1b1e
4
- data.tar.gz: 1d5e8f1286c4dbb564b42824d8b950f296a18652
3
+ metadata.gz: e02d528270e77bac532de323b85833d8cd1e291f
4
+ data.tar.gz: 34b71d6b7e4cc37953be770f478f4c8a61406494
5
5
  SHA512:
6
- metadata.gz: a41f0686fe77ab68a026926840865f5ac81a3065657919724a4df3c6fe5f906125dbcd5e8b91949d8d4a666954566272cff512de81cacf5998cb61ed5970b3d1
7
- data.tar.gz: 66e61ab2aba3d69f442fd537477c1350d7d1c6b2eb65aa6814c38276918162b8bb120b3a0e5c08777d08d9d23a2838734651a71600c339fbb161c0c4618e2534
6
+ metadata.gz: bce2524133b63fd81edb803e16e4f33be361a02bbec16c1be5d81bb045b0fc85df90e380e0a1b36b9ae37beb06b2f08302f8ed964bd6ff2138fee1d798992db0
7
+ data.tar.gz: 0dd07655ca6e4efd6bfe74be9282722c52add045543f34b26c4fdb5a4cec7d32687d7d43d03854e0477c3db1479adbdf27b33b6860334f52bffbf78931665743
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.11.0
4
+
5
+ * Search for text nodes on DocumentFragments without root tags #146 Razer6
6
+ * Don't filter @mentions in <style> tags #145 jch
7
+ * Prefer `http_url` in HttpsFilter. `base_url` still works. #142 bkeepers
8
+ * Remove duplicate check in EmojiFilter #141 Razer6
9
+
3
10
  ## 1.10.0
4
11
 
5
12
  * Anchor TOCFilter with id's instead of name's #140 bkeepers
@@ -57,12 +57,12 @@ module HTML
57
57
  )
58
58
 
59
59
  # Don't look for mentions in text nodes that are children of these elements
60
- IGNORE_PARENTS = %w(pre code a).to_set
60
+ IGNORE_PARENTS = %w(pre code a style).to_set
61
61
 
62
62
  def call
63
63
  result[:mentioned_usernames] ||= []
64
64
 
65
- doc.search('text()').each do |node|
65
+ search_text_nodes(doc).each do |node|
66
66
  content = node.to_html
67
67
  next if !content.include?('@')
68
68
  next if has_ancestor?(node, IGNORE_PARENTS)
@@ -118,4 +118,4 @@ module HTML
118
118
  end
119
119
  end
120
120
  end
121
- end
121
+ end
@@ -15,9 +15,9 @@ module HTML
15
15
  # :asset_path (optional) - url path to link to emoji sprite. :file_name can be used as a placeholder for the sprite file name. If no asset_path is set "emoji/:file_name" is used.
16
16
  class EmojiFilter < Filter
17
17
  def call
18
- doc.search('text()').each do |node|
18
+ search_text_nodes(doc).each do |node|
19
19
  content = node.to_html
20
- next if !content.include?(':')
20
+ next unless content.include?(':')
21
21
  next if has_ancestor?(node, %w(pre code))
22
22
  html = emoji_image_filter(content)
23
23
  next if html == content
@@ -38,8 +38,6 @@ module HTML
38
38
  #
39
39
  # Returns a String with :emoji: replaced with images.
40
40
  def emoji_image_filter(text)
41
- return text unless text.include?(':')
42
-
43
41
  text.gsub(emoji_pattern) do |match|
44
42
  name = $1
45
43
  "<img class='emoji' title=':#{name}:' alt=':#{name}:' src='#{emoji_url(name)}' height='20' width='20' align='absmiddle' />"
@@ -59,6 +59,13 @@ module HTML
59
59
  @doc ||= parse_html(html)
60
60
  end
61
61
 
62
+ # Searches a Nokogiri::HTML::DocumentFragment for text nodes. If no elements
63
+ # are found, a second search without root tags is invoked.
64
+ def search_text_nodes(doc)
65
+ nodes = doc.xpath('.//text()')
66
+ nodes.empty? ? doc.xpath('text()') : nodes
67
+ end
68
+
62
69
  # The String representation of the document. If a DocumentFragment was
63
70
  # provided to the Filter, it is serialized into a String when this method is
64
71
  # called.
@@ -1,21 +1,26 @@
1
1
  module HTML
2
2
  class Pipeline
3
- # HTML Filter for replacing http references to :base_url with https versions.
3
+ # HTML Filter for replacing http references to :http_url with https versions.
4
4
  # Subdomain references are not rewritten.
5
5
  #
6
6
  # Context options:
7
- # :base_url - The url to force https
7
+ # :http_url - The HTTP url to force HTTPS. Falls back to :base_url
8
8
  class HttpsFilter < Filter
9
9
  def call
10
- doc.css(%Q(a[href^="#{context[:base_url]}"])).each do |element|
10
+ doc.css(%Q(a[href^="#{http_url}"])).each do |element|
11
11
  element['href'] = element['href'].sub(/^http:/,'https:')
12
12
  end
13
13
  doc
14
14
  end
15
15
 
16
- # Raise error if :base_url undefined
16
+ # HTTP url to replace. Falls back to :base_url
17
+ def http_url
18
+ context[:http_url] || context[:base_url]
19
+ end
20
+
21
+ # Raise error if :http_url undefined
17
22
  def validate
18
- needs :base_url
23
+ needs :http_url unless http_url
19
24
  end
20
25
  end
21
26
  end
@@ -1,5 +1,5 @@
1
1
  module HTML
2
2
  class Pipeline
3
- VERSION = "1.10.0"
3
+ VERSION = "1.11.0"
4
4
  end
5
5
  end
@@ -8,6 +8,12 @@ class HTML::Pipeline::EmojiFilterTest < Minitest::Test
8
8
  doc = filter.call
9
9
  assert_match "https://foo.com/emoji/shipit.png", doc.search('img').attr('src').value
10
10
  end
11
+
12
+ def test_emojify_on_string
13
+ filter = EmojiFilter.new(":shipit:", {:asset_root => 'https://foo.com'})
14
+ doc = filter.call
15
+ assert_match "https://foo.com/emoji/shipit.png", doc.search('img').attr('src').value
16
+ end
11
17
 
12
18
  def test_uri_encoding
13
19
  filter = EmojiFilter.new("<p>:+1:</p>", {:asset_root => 'https://foo.com'})
@@ -3,8 +3,12 @@ require "test_helper"
3
3
  HttpsFilter = HTML::Pipeline::HttpsFilter
4
4
 
5
5
  class HTML::Pipeline::AutolinkFilterTest < Minitest::Test
6
- def filter(html, base_url="http://github.com")
7
- HttpsFilter.to_html(html, :base_url => base_url)
6
+ def filter(html)
7
+ HttpsFilter.to_html(html, @options)
8
+ end
9
+
10
+ def setup
11
+ @options = {:base_url => "http://github.com"}
8
12
  end
9
13
 
10
14
  def test_http
@@ -27,8 +31,23 @@ class HTML::Pipeline::AutolinkFilterTest < Minitest::Test
27
31
  filter(%(<a href="http://github.io">github.io</a>))
28
32
  end
29
33
 
30
- def test_validation
31
- exception = assert_raises(ArgumentError) { HttpsFilter.call(nil, {}) }
32
- assert_match "HTML::Pipeline::HttpsFilter: :base_url", exception.message
34
+ def test_uses_http_url_over_base_url
35
+ @options = {:http_url => "http://github.com", :base_url => "https://github.com"}
36
+
37
+ assert_equal %(<a href="https://github.com">github.com</a>),
38
+ filter(%(<a href="http://github.com">github.com</a>))
39
+ end
40
+
41
+ def test_only_http_url
42
+ @options = {:http_url => "http://github.com"}
43
+
44
+ assert_equal %(<a href="https://github.com">github.com</a>),
45
+ filter(%(<a href="http://github.com">github.com</a>))
46
+ end
47
+
48
+ def test_validates_http_url
49
+ @options.clear
50
+ exception = assert_raises(ArgumentError) { filter("") }
51
+ assert_match "HTML::Pipeline::HttpsFilter: :http_url", exception.message
33
52
  end
34
53
  end
@@ -36,6 +36,11 @@ class HTML::Pipeline::MentionFilterTest < Minitest::Test
36
36
  assert_equal body, filter(body).to_html
37
37
  end
38
38
 
39
+ def test_not_replacing_mentions_in_style_tags
40
+ body = "<style>@media (min-width: 768px) { color: red; }</style>"
41
+ assert_equal body, filter(body).to_html
42
+ end
43
+
39
44
  def test_not_replacing_mentions_in_links
40
45
  body = "<p><a>@kneath</a> okay</p>"
41
46
  assert_equal body, filter(body).to_html
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html-pipeline
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.10.0
4
+ version: 1.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Tomayko
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-09-05 00:00:00.000000000 Z
12
+ date: 2014-09-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri