Make Docs::Parser return the entire document instead of <body>

pull/566/merge
Thibaut Courouble 8 years ago
parent eac233caf9
commit 6f0214eaf3

@ -9,14 +9,16 @@ module Docs
private private
DOCUMENT_RGX = /\A(?:\s|(?:<!--.*?-->))*<(?:\!doctype|html)/i
def document? def document?
@content =~ /\A\s*<(?:\!doctype|html)/i @content =~ DOCUMENT_RGX
end end
def parse_as_document def parse_as_document
document = Nokogiri::HTML.parse @content, nil, 'UTF-8' document = Nokogiri::HTML.parse @content, nil, 'UTF-8'
@title = document.at_css('title').try(:content) @title = document.at_css('title').try(:content)
document.at_css 'body' document
end end
def parse_as_fragment def parse_as_fragment

@ -8,6 +8,8 @@ module Docs
if container if container
doc.at_css(container) || raise(ContainerNotFound, "element '#{container}' could not be found in the document, url=#{current_url}") doc.at_css(container) || raise(ContainerNotFound, "element '#{container}' could not be found in the document, url=#{current_url}")
elsif doc.name == 'document'
doc.at_css('body')
else else
doc doc
end end

@ -14,17 +14,23 @@ class DocsParserTest < MiniTest::Spec
context "with an HTML fragment" do context "with an HTML fragment" do
it "returns the fragment" do it "returns the fragment" do
body = '<div>Test</div>' body = '<div>Test</div>'
assert_equal body, parser(body).html.inner_html html = parser(body).html
assert_equal '#document-fragment', html.name
assert_equal body, html.inner_html
end end
end end
context "with an HTML document" do context "with an HTML document" do
it "returns the <body>" do it "returns the document" do
body = '<!doctype html><meta charset=utf-8><title></title><div>Test</div>' body = '<!-- foo --> <!doctype html><meta charset=utf-8><title></title><div>Test</div>'
assert_equal '<div>Test</div>', parser(body).html.inner_html html = parser(body).html
assert_equal 'document', html.name
assert_equal '<div>Test</div>', html.at_css('body').inner_html
body = '<html><meta charset=utf-8><title></title><div>Test</div></html>' body = '<html><meta charset=utf-8><title></title><div>Test</div></html>'
assert_equal '<div>Test</div>', parser(body).html.inner_html html = parser(body).html
assert_equal 'document', html.name
assert_equal '<div>Test</div>', html.at_css('body').inner_html
end end
end end
end end

@ -4,6 +4,7 @@ require 'docs'
class ContainerFilterTest < MiniTest::Spec class ContainerFilterTest < MiniTest::Spec
include FilterTestHelper include FilterTestHelper
self.filter_class = Docs::ContainerFilter self.filter_class = Docs::ContainerFilter
self.filter_type = 'html'
before do before do
@body = '<div>Test</div>' @body = '<div>Test</div>'
@ -56,8 +57,17 @@ class ContainerFilterTest < MiniTest::Spec
end end
context "when context[:container] is nil" do context "when context[:container] is nil" do
it "returns the document" do context "and the document is an HTML fragment" do
assert_equal @body, filter_output.inner_html it "returns the document" do
assert_equal @body, filter_output.inner_html
end
end
context "and the document is an HTML document" do
it "returns the <body>" do
@body = '<html><meta charset=utf-8><title></title><div>Test</div></html>'
assert_equal '<div>Test</div>', filter_output.inner_html
end
end end
end end
end end

@ -3,15 +3,16 @@ module FilterTestHelper
included do included do
class_attribute :filter_class class_attribute :filter_class
class_attribute :filter_type
end end
def filter def filter
@filter ||= filter_class.new @body || '', context, result @filter ||= filter_class.new prepare_body(@body || ''), context, result
end end
def filter_output def filter_output
@filter_output ||= begin @filter_output ||= begin
filter.instance_variable_set :@html, @body if @body filter.instance_variable_set :@html, prepare_body(@body) if @body
filter.call filter.call
end end
end end
@ -41,4 +42,12 @@ module FilterTestHelper
def link_to(href) def link_to(href)
%(<a href="#{href}">Link</a>) %(<a href="#{href}">Link</a>)
end end
def prepare_body(body)
if self.class.filter_type == 'html'
Docs::Parser.new(body).html
else
body
end
end
end end

Loading…
Cancel
Save