Skip to content

Commit

Permalink
Merge pull request #146 from Razer6/search_text_nodes
Browse files Browse the repository at this point in the history
Search for text nodes on DocumentFragments without root tags
  • Loading branch information
jch committed Sep 15, 2014
2 parents 987bd3e + 7c61750 commit 5db22e1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/html/pipeline/@mention_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -118,4 +118,4 @@ def link_to_mentioned_user(login)
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/html/pipeline/emoji_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
7 changes: 7 additions & 0 deletions lib/html/pipeline/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions test/html/pipeline/emoji_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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("<p>:+1:</p>", {:asset_root => 'https://foo.com'})
Expand Down

0 comments on commit 5db22e1

Please sign in to comment.