Skip to content

Commit

Permalink
Added HTML::Mixin#html_open (closes #12).
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Oct 13, 2023
1 parent 802c268 commit eecec10
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/ronin/support/web/html/mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,35 @@ def html_parse(html,&block)
HTML.parse(html,&block)
end

#
# Opens an HTML file.
#
# @param [String] path
# The path to the HTML file.
#
# @yield [doc]
# If a block is given, it will be passed the newly created document
# object.
#
# @yieldparam [Nokogiri::HTML::Document] doc
# The new HTML document object.
#
# @return [Nokogiri::HTML::Document]
# The parsed HTML file.
#
# @example
# doc = HTML.open('index.html')
# # => #<Nokogiri::HTML::Document:...>
#
# @see http://rubydoc.info/gems/nokogiri/Nokogiri/HTML/Document
# @see HTML.open
#
def html_open(path,&block)
HTML.open(path,&block)
end

alias open_html html_open

#
# Creates a new `Nokogiri::HTML::Builder`.
#
Expand Down
33 changes: 33 additions & 0 deletions spec/html/mixin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,39 @@
end
end

describe "#html_open" do
it "must open and parse the given path, and return a Nokogiri::HTML::Document" do
doc = subject.html_open(html_file)

expect(doc).to be_kind_of(Nokogiri::HTML::Document)

# XXX: nokogiri's java extensions behave differently from libxml2
if RUBY_ENGINE == 'jruby'
expect(doc.to_s).to eq(
<<~HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
#{html}
HTML
)
else
expect(doc.to_s).to eq(
<<~HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
#{html.chomp}
HTML
)
end
end

context "when given a block" do
it "must yield the Nokogiri::HTML::Document object" do
expect { |b|
subject.html_open(html_file,&b)
}.to yield_with_args(Nokogiri::HTML::Document)
end
end
end

describe "#html_build" do
it "must build an HTML document" do
doc = subject.html_build do |html|
Expand Down

0 comments on commit eecec10

Please sign in to comment.