Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into require-gemoji-2
Browse files Browse the repository at this point in the history
  • Loading branch information
jch committed Feb 24, 2015
2 parents 0234418 + 2f52b08 commit 39bba6f
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## 1.11.0

* Search for text nodes on DocumentFragments without root tags #146 Razer6
* Don't filter @mentions in <style> tags #145 jch
* Don't filter @mentions in `<style>` tags #145 jch
* Prefer `http_url` in HttpsFilter. `base_url` still works. #142 bkeepers
* Remove duplicate check in EmojiFilter #141 Razer6

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# HTML::Pipeline [![Build Status](https://secure.travis-ci.org/jch/html-pipeline.png)](http://travis-ci.org/jch/html-pipeline)
# HTML::Pipeline [![Build Status](https://travis-ci.org/jch/html-pipeline.svg?branch=master)](https://travis-ci.org/jch/html-pipeline)

GitHub HTML processing filters and utilities. This module includes a small
framework for defining DOM based content filters and applying them to user
Expand Down Expand Up @@ -243,6 +243,8 @@ Here are some extensions people have built:
* [html-pipeline-cite](https://github.com/lifted-studios/html-pipeline-cite)
* [tilt-html-pipeline](https://github.com/bradgessler/tilt-html-pipeline)
* [html-pipeline-wiki-link'](https://github.com/lifted-studios/html-pipeline-wiki-link) - WikiMedia-style wiki links
* [task_list](https://github.com/github/task_list) - GitHub flavor Markdown Task List
* [html-pipeline-rouge_filter](https://github.com/JuanitoFatas/html-pipeline-rouge_filter) - Syntax highlight with [Rouge](https://github.com/jneen/rouge/)

## Instrumenting

Expand Down
2 changes: 1 addition & 1 deletion html-pipeline.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Gem::Specification.new do |gem|
gem.test_files = gem.files.grep(%r{^test})
gem.require_paths = ["lib"]

gem.add_dependency "nokogiri", "~> 1.4"
gem.add_dependency "nokogiri", [">= 1.4", "<= 1.6.5"]
gem.add_dependency "activesupport", ">= 2"

gem.post_install_message = <<msg
Expand Down
8 changes: 5 additions & 3 deletions lib/html/pipeline/@mention_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,13 @@ def link_to_mention_info(text, info_url=nil)

def link_to_mentioned_user(login)
result[:mentioned_usernames] |= [login]
url = File.join(base_url, login)
"<a href='#{url}' class='user-mention'>" +

base_url << "/" unless base_url =~ /[\/~]\z/

"<a href='#{base_url << login}' class='user-mention'>" +
"@#{login}" +
"</a>"
end
end
end
end
end
17 changes: 16 additions & 1 deletion lib/html/pipeline/emoji_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ class Pipeline
# Context:
# :asset_root (required) - base url to link to emoji sprite
# :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.
# :ignored_ancestor_tags (optional) - Tags to stop the emojification. Node has matched ancestor HTML tags will not be emojified. Default to pre, code, and tt tags. Extra tags please pass in the form of array, e.g., %w(blockquote summary).
class EmojiFilter < Filter

DEFAULT_IGNORED_ANCESTOR_TAGS = %w(pre code tt).freeze

def call
doc.search('text()').each do |node|
content = node.to_html
next unless content.include?(':')
next if has_ancestor?(node, %w(pre code tt))
next if has_ancestor?(node, ignored_ancestor_tags)
html = emoji_image_filter(content)
next if html == content
node.replace(html)
Expand Down Expand Up @@ -86,6 +90,17 @@ def self.emoji_names
def emoji_filename(name)
Emoji.find_by_alias(name).image_filename
end

# Return ancestor tags to stop the emojification.
#
# @return [Array<String>] Ancestor tags.
def ignored_ancestor_tags
if context[:ignored_ancestor_tags]
DEFAULT_IGNORED_ANCESTOR_TAGS | context[:ignored_ancestor_tags]
else
DEFAULT_IGNORED_ANCESTOR_TAGS
end
end
end
end
end
6 changes: 3 additions & 3 deletions lib/html/pipeline/sanitization_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class SanitizationFilter < Filter
:elements => %w(
h1 h2 h3 h4 h5 h6 h7 h8 br b i strong em a pre code img tt
div ins del sup sub p ol ul table thead tbody tfoot blockquote
dl dt dd kbd q samp var hr ruby rt rp li tr td th s strike
dl dt dd kbd q samp var hr ruby rt rp li tr td th s strike summary details
),
:remove_contents => ['script'],
:attributes => {
Expand All @@ -57,13 +57,13 @@ class SanitizationFilter < Filter
'border', 'cellpadding', 'cellspacing', 'char',
'charoff', 'charset', 'checked', 'cite',
'clear', 'cols', 'colspan', 'color',
'compact', 'coords', 'datetime', 'details', 'dir',
'compact', 'coords', 'datetime', 'dir',
'disabled', 'enctype', 'for', 'frame',
'headers', 'height', 'hreflang',
'hspace', 'ismap', 'label', 'lang',
'longdesc', 'maxlength', 'media', 'method',
'multiple', 'name', 'nohref', 'noshade',
'nowrap', 'prompt', 'readonly', 'rel', 'rev',
'nowrap', 'open', 'prompt', 'readonly', 'rel', 'rev',
'rows', 'rowspan', 'rules', 'scope',
'selected', 'shape', 'size', 'span',
'start', 'summary', 'tabindex', 'target',
Expand Down
14 changes: 14 additions & 0 deletions test/html/pipeline/emoji_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,18 @@ def test_not_emojify_in_pre_tags
doc = filter.call
assert_equal body, doc.to_html
end

def test_not_emojify_in_custom_single_tag_foo
body = "<foo>:shipit:</foo>"
filter = EmojiFilter.new(body, {:asset_root => 'https://foo.com', ignored_ancestor_tags: %w(foo)})
doc = filter.call
assert_equal body, doc.to_html
end

def test_not_emojify_in_custom_multiple_tags_foo_and_bar
body = "<bar>:shipit:</bar>"
filter = EmojiFilter.new(body, {:asset_root => 'https://foo.com', ignored_ancestor_tags: %w(foo bar)})
doc = filter.call
assert_equal body, doc.to_html
end
end
21 changes: 21 additions & 0 deletions test/html/pipeline/mention_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,27 @@ def test_links_to_more_info_when_info_url_given
filter(body, '/', 'https://github.com/blog/821').to_html
end

def test_base_url_slash
body = "<p>Hi, @jch!</p>"
link = "<a href=\"/jch\" class=\"user-mention\">@jch</a>"
assert_equal "<p>Hi, #{link}!</p>",
filter(body, '/').to_html
end

def test_base_url_under_custom_route
body = "<p>Hi, @jch!</p>"
link = "<a href=\"/userprofile/jch\" class=\"user-mention\">@jch</a>"
assert_equal "<p>Hi, #{link}!</p>",
filter(body, '/userprofile').to_html
end

def test_base_url_slash_with_tilde
body = "<p>Hi, @jch!</p>"
link = "<a href=\"/~jch\" class=\"user-mention\">@jch</a>"
assert_equal "<p>Hi, #{link}!</p>",
filter(body, '/~').to_html
end

MarkdownPipeline =
HTML::Pipeline.new [
HTML::Pipeline::MarkdownFilter,
Expand Down
24 changes: 24 additions & 0 deletions test/html/pipeline/sanitization_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,28 @@ def test_table_sections_are_not_removed
</table>)
assert_equal orig, SanitizationFilter.call(orig).to_s
end

def test_summary_tag_are_not_removed
orig = %(<summary>Foo</summary>)
assert_equal orig, SanitizationFilter.call(orig).to_s
end

def test_details_tag_and_open_attribute_are_not_removed
orig = %(<details open>Foo</details>)
assert_equal orig, SanitizationFilter.call(orig).to_s
end

def test_nested_details_tag_are_not_removed
orig = <<-NESTED
<details>
<summary>Foo</summary>
<details>
Bar
<summary>Baz</summary>
</details>
Qux
</details>
NESTED
assert_equal orig, SanitizationFilter.call(orig).to_s
end
end

0 comments on commit 39bba6f

Please sign in to comment.