Skip to content

Commit

Permalink
Merge pull request #375 from gjtorikian/convert-no-need
Browse files Browse the repository at this point in the history
req convert_filter if `text/node`filter present
  • Loading branch information
gjtorikian committed Feb 15, 2023
2 parents 492ab84 + 04cec11 commit e7bc803
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
5 changes: 3 additions & 2 deletions lib/html_pipeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ def initialize(text_filters: [], convert_filter: nil, sanitization_config: Sanit
validate_filters(@node_filters, HTMLPipeline::NodeFilter)

@convert_filter = convert_filter
if @convert_filter.nil? && !@node_filters.empty?
raise InvalidFilterError, "Must provide `convert_filter` if `node_filter`s is also provided"

if @convert_filter.nil? && (!@text_filters.empty? && !@node_filters.empty?)
raise InvalidFilterError, "Must provide `convert_filter` if `text_filters` and `node_filters` are also provided"
elsif !@convert_filter.nil?
validate_filter(@convert_filter, HTMLPipeline::ConvertFilter)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/html_pipeline/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def has_ancestor?(element, ancestor)
def needs(*keys)
missing = keys.reject { |key| context.include?(key) }

return unless missing.any?
return if missing.none?

raise ArgumentError,
"Missing context keys for #{self.class.name}: #{missing.map(&:inspect).join(", ")}"
Expand Down
2 changes: 1 addition & 1 deletion lib/html_pipeline/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

class HTMLPipeline
VERSION = "3.0.0.pre2"
VERSION = "3.0.0.pre3"
end
24 changes: 14 additions & 10 deletions test/html_pipeline_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,9 @@
require "helpers/mocked_instrumentation_service"

class HTMLPipelineTest < Minitest::Test
class TestFilter < HTMLPipeline::TextFilter
class << self
def call(input, context: {}, result: {})
input.reverse
end
end
end

def setup
@default_context = {}
@pipeline = HTMLPipeline.new(text_filters: [TestFilter], default_context: @default_context)
@pipeline = HTMLPipeline.new(text_filters: [TestTextFilter], default_context: @default_context)
end

def test_filter_instrumentation
Expand All @@ -27,7 +19,7 @@ def test_filter_instrumentation

assert(event, "event expected")
assert_equal("call_filter.html_pipeline", event)
assert_equal(TestFilter.name, payload[:filter])
assert_equal(TestTextFilter.name, payload[:filter])
assert_equal(@pipeline.class.name, payload[:pipeline])
assert_equal(body.reverse, payload[:result][:output])
end
Expand Down Expand Up @@ -90,6 +82,18 @@ def test_incorrect_convert_filter
end
end

def test_convert_filter_needed_for_text_and_html_filters
assert_raises(HTMLPipeline::InvalidFilterError) do
HTMLPipeline.new(
text_filters: [TestTextFilter],
node_filters: [
HTMLPipeline::NodeFilter::MentionFilter.new,
],
default_context: @default_context,
)
end
end

def test_incorrect_node_filters
assert_raises(HTMLPipeline::InvalidFilterError) do
HTMLPipeline.new(node_filters: [HTMLPipeline::ConvertFilter::MarkdownFilter], default_context: @default_context)
Expand Down
8 changes: 8 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ module TestHelpers
end

Minitest::Test.include(TestHelpers)

class TestTextFilter < HTMLPipeline::TextFilter
class << self
def call(input, context: {}, result: {})
input.reverse
end
end
end

0 comments on commit e7bc803

Please sign in to comment.