Commit 2fbfb854 authored by Yorick Peterse's avatar Yorick Peterse

Returning enums in ReferenceFilter#each_node

This changes ReferenceFilter#each_node so that when it's called without
a block an Enumerator is returned.
parent acfbeced
......@@ -68,6 +68,8 @@ module Banzai
# by `ignore_ancestor_query`. Link tags are not processed if they have a
# "gfm" class or the "href" attribute is empty.
def each_node
return to_enum(__method__) unless block_given?
query = %Q{descendant-or-self::text()[not(#{ignore_ancestor_query})]
| descendant-or-self::a[
not(contains(concat(" ", @class, " "), " gfm ")) and not(@href = "")
......
require 'spec_helper'
describe Banzai::Filter::ReferenceFilter, lib: true do
let(:project) { build(:project) }
describe '#each_node' do
it 'iterates over the nodes in a document' do
document = Nokogiri::HTML.fragment('<a href="foo">foo</a>')
filter = described_class.new(document, project: project)
expect { |b| filter.each_node(&b) }.
to yield_with_args(an_instance_of(Nokogiri::XML::Element))
end
it 'returns an Enumerator when no block is given' do
document = Nokogiri::HTML.fragment('<a href="foo">foo</a>')
filter = described_class.new(document, project: project)
expect(filter.each_node).to be_an_instance_of(Enumerator)
end
it 'skips links with a "gfm" class' do
document = Nokogiri::HTML.fragment('<a href="foo" class="gfm">foo</a>')
filter = described_class.new(document, project: project)
expect { |b| filter.each_node(&b) }.not_to yield_control
end
it 'skips text nodes in pre elements' do
document = Nokogiri::HTML.fragment('<pre>foo</pre>')
filter = described_class.new(document, project: project)
expect { |b| filter.each_node(&b) }.not_to yield_control
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment