Skip to content

Commit

Permalink
Do a second xpath search to match elements without parent node
Browse files Browse the repository at this point in the history
  • Loading branch information
Razer6 committed Sep 10, 2014
1 parent 74f7dc9 commit 7b2b4ea
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
26 changes: 19 additions & 7 deletions lib/html/pipeline/emoji_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,28 @@ 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|
content = node.to_html
next if !content.include?(':')
next if has_ancestor?(node, %w(pre code))
html = emoji_image_filter(content)
next if html == content
node.replace(html)
doc.xpath('.//text()').each do |node|
process_node(node)
end

# Second search to match text nodes which don't have a parent node
doc.xpath('text()').each do |node|
process_node(node)
end
doc
end

# Process a given node and replaces all :emoji: with corresponding image.
#
# node - Text node to be processed
def process_node(node)
content = node.to_html
return unless content.include?(':')
return if has_ancestor?(node, %w(pre code))
html = emoji_image_filter(content)
return if html == content
node.replace(html)
end

# Implementation of validate hook.
# Errors should raise exceptions or use an existing validator.
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 7b2b4ea

Please sign in to comment.