diff --git a/lib/html/pipeline/@mention_filter.rb b/lib/html/pipeline/@mention_filter.rb index 683f513f..7b754a88 100644 --- a/lib/html/pipeline/@mention_filter.rb +++ b/lib/html/pipeline/@mention_filter.rb @@ -62,7 +62,7 @@ def self.mentioned_logins_in(text) def call result[:mentioned_usernames] ||= [] - doc.search('text()').each do |node| + search_text_nodes(doc).each do |node| content = node.to_html next if !content.include?('@') next if has_ancestor?(node, IGNORE_PARENTS) @@ -118,4 +118,4 @@ def link_to_mentioned_user(login) end end end -end \ No newline at end of file +end diff --git a/lib/html/pipeline/emoji_filter.rb b/lib/html/pipeline/emoji_filter.rb index 4fc551d5..712c4fec 100644 --- a/lib/html/pipeline/emoji_filter.rb +++ b/lib/html/pipeline/emoji_filter.rb @@ -15,7 +15,7 @@ class Pipeline # :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. class EmojiFilter < Filter def call - doc.search('text()').each do |node| + search_text_nodes(doc).each do |node| content = node.to_html next if !content.include?(':') next if has_ancestor?(node, %w(pre code)) diff --git a/lib/html/pipeline/filter.rb b/lib/html/pipeline/filter.rb index 0fe5a831..5467bc06 100644 --- a/lib/html/pipeline/filter.rb +++ b/lib/html/pipeline/filter.rb @@ -59,6 +59,13 @@ def doc @doc ||= parse_html(html) end + # Searches a Nokogiri::HTML::DocumentFragment for text nodes. If no elements + # are found, a second search without root tags is invoked. + def search_text_nodes(doc) + nodes = doc.xpath('.//text()') + nodes.empty? ? doc.xpath('text()') : nodes + end + # The String representation of the document. If a DocumentFragment was # provided to the Filter, it is serialized into a String when this method is # called. diff --git a/test/html/pipeline/emoji_filter_test.rb b/test/html/pipeline/emoji_filter_test.rb index e46ac32b..298775ed 100644 --- a/test/html/pipeline/emoji_filter_test.rb +++ b/test/html/pipeline/emoji_filter_test.rb @@ -8,6 +8,12 @@ def test_emojify doc = filter.call assert_match "https://foo.com/emoji/shipit.png", doc.search('img').attr('src').value end + + def test_emojify_on_string + filter = EmojiFilter.new(":shipit:", {:asset_root => 'https://foo.com'}) + doc = filter.call + assert_match "https://foo.com/emoji/shipit.png", doc.search('img').attr('src').value + end def test_uri_encoding filter = EmojiFilter.new("

:+1:

", {:asset_root => 'https://foo.com'})