Added ReferenceFilter#nodes

This method returns an Array of the HTML nodes as yielded by
ReferenceFilter#each_node. The method's return value is memoized to
allow multiple calls without having to re-query the input document.
This commit is contained in:
Yorick Peterse 2016-05-30 15:56:05 +02:00
parent 2fbfb85492
commit 8a6c3f27e9
No known key found for this signature in database
GPG Key ID: EDD30D2BEB691AC9
2 changed files with 14 additions and 0 deletions

View File

@ -80,6 +80,11 @@ module Banzai
end
end
# Returns an Array containing all HTML nodes.
def nodes
@nodes ||= each_node.to_a
end
# Yields the link's URL and text whenever the node is a valid <a> tag.
def yield_valid_link(node)
link = CGI.unescape(node.attr('href').to_s)

View File

@ -33,4 +33,13 @@ describe Banzai::Filter::ReferenceFilter, lib: true do
expect { |b| filter.each_node(&b) }.not_to yield_control
end
end
describe '#nodes' do
it 'returns an Array of the HTML nodes' do
document = Nokogiri::HTML.fragment('<a href="foo">foo</a>')
filter = described_class.new(document, project: project)
expect(filter.nodes).to eq([document.children[0]])
end
end
end