Skip to content

Commit 0ea071d

Browse files
committed
Correctly expect classes for TextFilter
1 parent b7f8e5f commit 0ea071d

File tree

7 files changed

+26
-23
lines changed

7 files changed

+26
-23
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ ALLOWLIST = {
171171

172172
pipeline = HTMLPipeline.new \
173173
text_filters: [
174-
HTMLPipeline::MarkdownFilter,
174+
HTMLPipeline::TextFilter::ImageFilter.new,
175175
],
176176
convert_filter: HTMLPipeline::ConvertFilter::MarkdownFilter.new,
177177
sanitization_config: ALLOWLIST
@@ -199,7 +199,7 @@ the config:
199199
```ruby
200200
pipeline = HTMLPipeline.new \
201201
text_filters: [
202-
HTMLPipeline::MarkdownFilter,
202+
HTMLPipeline::TextFilter::ImageFilter.new,
203203
],
204204
convert_filter: HTMLPipeline::ConvertFilter::MarkdownFilter.new,
205205
sanitization_config: nil

lib/html_pipeline.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def call(text, context: {}, result: {})
153153

154154
if @text_filters.any?
155155
payload = default_payload({
156-
text_filters: @text_filters.map(&:name),
156+
text_filters: @text_filters.map { |f| f.class.name },
157157
context: context,
158158
result: result,
159159
})
@@ -204,7 +204,7 @@ def call(text, context: {}, result: {})
204204
# Returns the result of the filter.
205205
def perform_filter(filter, doc, context: {}, result: {})
206206
payload = default_payload({
207-
filter: filter.name,
207+
filter: filter.class.name,
208208
context: context,
209209
result: result,
210210
})

lib/html_pipeline/text_filter.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ class HTMLPipeline
44
class TextFilter < Filter
55
attr_reader :text
66

7-
def initialize(text, context: {}, result: {})
8-
raise TypeError, "text must be a String" unless text.is_a?(String)
9-
10-
# Ensure that this is always a string
11-
@text = text.respond_to?(:to_str) ? text.to_str : text.to_s
7+
def initialize(context: {}, result: {})
128
super(context: context, result: result)
139
end
1410

1511
class << self
16-
def call(input, context: {}, result: {})
17-
new(input, context: context, result: result).call
12+
def call(text, context: {}, result: {})
13+
raise TypeError, "text must be a String" unless text.is_a?(String)
14+
15+
# Ensure that this is always a string
16+
text = text.respond_to?(:to_str) ? text.to_str : text.to_s
17+
new(context: context, result: result).call(text)
1818
end
1919
end
2020
end

lib/html_pipeline/text_filter/image_filter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class TextFilter
99
# <img src="http://example.com/test.jpg" alt=""/>.
1010

1111
class ImageFilter < TextFilter
12-
def call
13-
@text.gsub(%r{(https|http)?://.+\.(jpg|jpeg|bmp|gif|png)(\?\S+)?}i) do |match|
12+
def call(text)
13+
text.gsub(%r{(https|http)?://.+\.(jpg|jpeg|bmp|gif|png)(\?\S+)?}i) do |match|
1414
%(<img src="#{match}" alt=""/>)
1515
end
1616
end

lib/html_pipeline/text_filter/plain_text_input_filter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ class TextFilter
55
# Simple filter for plain text input. HTML escapes the text input and wraps it
66
# in a div.
77
class PlainTextInputFilter < TextFilter
8-
def call
9-
"<div>#{CGI.escapeHTML(@text)}</div>"
8+
def call(text)
9+
"<div>#{CGI.escapeHTML(text)}</div>"
1010
end
1111
end
1212
end

test/html_pipeline_test.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
require "test_helper"
44
require "helpers/mocked_instrumentation_service"
55
require "html_pipeline/node_filter/image_max_width_filter"
6+
require "html_pipeline/node_filter/mention_filter"
7+
require "html_pipeline/convert_filter/markdown_filter"
68

79
class HTMLPipelineTest < Minitest::Test
810
def setup
911
@default_context = {}
10-
@pipeline = HTMLPipeline.new(text_filters: [TestTextFilter], default_context: @default_context)
12+
@pipeline = HTMLPipeline.new(text_filters: [TestTextFilter.new], default_context: @default_context)
1113
end
1214

1315
def test_filter_instrumentation
@@ -35,7 +37,8 @@ def test_pipeline_instrumentation
3537

3638
assert(event, "event expected")
3739
assert_equal("call_text_filters.html_pipeline", event)
38-
assert_equal(@pipeline.text_filters.map(&:name), payload[:text_filters])
40+
41+
assert_equal(@pipeline.text_filters.map { |x| x.class.name }, payload[:text_filters])
3942
assert_equal(@pipeline.class.name, payload[:pipeline])
4043
assert_equal(body.reverse, payload[:result][:output])
4144
end
@@ -73,7 +76,7 @@ def test_setup_instrumentation
7376

7477
def test_incorrect_text_filters
7578
assert_raises(HTMLPipeline::InvalidFilterError) do
76-
HTMLPipeline.new(text_filters: [HTMLPipeline::NodeFilter::MentionFilter], default_context: @default_context)
79+
HTMLPipeline.new(text_filters: [HTMLPipeline::NodeFilter::MentionFilter.new], default_context: @default_context)
7780
end
7881
end
7982

@@ -86,7 +89,7 @@ def test_incorrect_convert_filter
8689
def test_convert_filter_needed_for_text_and_html_filters
8790
assert_raises(HTMLPipeline::InvalidFilterError) do
8891
HTMLPipeline.new(
89-
text_filters: [TestTextFilter],
92+
text_filters: [TestTextFilter.new],
9093
node_filters: [
9194
HTMLPipeline::NodeFilter::MentionFilter.new,
9295
],

test/test_helper.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ module TestHelpers
1717
Minitest::Test.include(TestHelpers)
1818

1919
class TestTextFilter < HTMLPipeline::TextFilter
20-
class << self
21-
def call(input, context: {}, result: {})
22-
input.reverse
23-
end
20+
# class << self
21+
def call(input, context: {}, result: {})
22+
input.reverse
2423
end
24+
# end
2525
end

0 commit comments

Comments
 (0)