Skip to content

Commit 6f0214e

Browse files
committed
Make Docs::Parser return the entire document instead of <body>
1 parent eac233c commit 6f0214e

File tree

5 files changed

+40
-11
lines changed

5 files changed

+40
-11
lines changed

lib/docs/core/parser.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ def initialize(content)
99

1010
private
1111

12+
DOCUMENT_RGX = /\A(?:\s|(?:<!--.*?-->))*<(?:\!doctype|html)/i
13+
1214
def document?
13-
@content =~ /\A\s*<(?:\!doctype|html)/i
15+
@content =~ DOCUMENT_RGX
1416
end
1517

1618
def parse_as_document
1719
document = Nokogiri::HTML.parse @content, nil, 'UTF-8'
1820
@title = document.at_css('title').try(:content)
19-
document.at_css 'body'
21+
document
2022
end
2123

2224
def parse_as_fragment

lib/docs/filters/core/container.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ def call
88

99
if container
1010
doc.at_css(container) || raise(ContainerNotFound, "element '#{container}' could not be found in the document, url=#{current_url}")
11+
elsif doc.name == 'document'
12+
doc.at_css('body')
1113
else
1214
doc
1315
end

test/lib/docs/core/parser_test.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,23 @@ def parser(content)
1414
context "with an HTML fragment" do
1515
it "returns the fragment" do
1616
body = '<div>Test</div>'
17-
assert_equal body, parser(body).html.inner_html
17+
html = parser(body).html
18+
assert_equal '#document-fragment', html.name
19+
assert_equal body, html.inner_html
1820
end
1921
end
2022

2123
context "with an HTML document" do
22-
it "returns the <body>" do
23-
body = '<!doctype html><meta charset=utf-8><title></title><div>Test</div>'
24-
assert_equal '<div>Test</div>', parser(body).html.inner_html
24+
it "returns the document" do
25+
body = '<!-- foo --> <!doctype html><meta charset=utf-8><title></title><div>Test</div>'
26+
html = parser(body).html
27+
assert_equal 'document', html.name
28+
assert_equal '<div>Test</div>', html.at_css('body').inner_html
2529

2630
body = '<html><meta charset=utf-8><title></title><div>Test</div></html>'
27-
assert_equal '<div>Test</div>', parser(body).html.inner_html
31+
html = parser(body).html
32+
assert_equal 'document', html.name
33+
assert_equal '<div>Test</div>', html.at_css('body').inner_html
2834
end
2935
end
3036
end

test/lib/docs/filters/core/container_test.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
class ContainerFilterTest < MiniTest::Spec
55
include FilterTestHelper
66
self.filter_class = Docs::ContainerFilter
7+
self.filter_type = 'html'
78

89
before do
910
@body = '<div>Test</div>'
@@ -56,8 +57,17 @@ class ContainerFilterTest < MiniTest::Spec
5657
end
5758

5859
context "when context[:container] is nil" do
59-
it "returns the document" do
60-
assert_equal @body, filter_output.inner_html
60+
context "and the document is an HTML fragment" do
61+
it "returns the document" do
62+
assert_equal @body, filter_output.inner_html
63+
end
64+
end
65+
66+
context "and the document is an HTML document" do
67+
it "returns the <body>" do
68+
@body = '<html><meta charset=utf-8><title></title><div>Test</div></html>'
69+
assert_equal '<div>Test</div>', filter_output.inner_html
70+
end
6171
end
6272
end
6373
end

test/support/filter_test_helper.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ module FilterTestHelper
33

44
included do
55
class_attribute :filter_class
6+
class_attribute :filter_type
67
end
78

89
def filter
9-
@filter ||= filter_class.new @body || '', context, result
10+
@filter ||= filter_class.new prepare_body(@body || ''), context, result
1011
end
1112

1213
def filter_output
1314
@filter_output ||= begin
14-
filter.instance_variable_set :@html, @body if @body
15+
filter.instance_variable_set :@html, prepare_body(@body) if @body
1516
filter.call
1617
end
1718
end
@@ -41,4 +42,12 @@ def result
4142
def link_to(href)
4243
%(<a href="#{href}">Link</a>)
4344
end
45+
46+
def prepare_body(body)
47+
if self.class.filter_type == 'html'
48+
Docs::Parser.new(body).html
49+
else
50+
body
51+
end
52+
end
4453
end

0 commit comments

Comments
 (0)