From 6f0214eaf3ed241e182ce228f1c0c40d796b8522 Mon Sep 17 00:00:00 2001 From: Thibaut Courouble Date: Sun, 22 Jan 2017 10:22:07 -0500 Subject: [PATCH] Make Docs::Parser return the entire document instead of --- lib/docs/core/parser.rb | 6 ++++-- lib/docs/filters/core/container.rb | 2 ++ test/lib/docs/core/parser_test.rb | 16 +++++++++++----- test/lib/docs/filters/core/container_test.rb | 14 ++++++++++++-- test/support/filter_test_helper.rb | 13 +++++++++++-- 5 files changed, 40 insertions(+), 11 deletions(-) diff --git a/lib/docs/core/parser.rb b/lib/docs/core/parser.rb index 7ab02634..21edffcd 100644 --- a/lib/docs/core/parser.rb +++ b/lib/docs/core/parser.rb @@ -9,14 +9,16 @@ module Docs private + DOCUMENT_RGX = /\A(?:\s|(?:))*<(?:\!doctype|html)/i + def document? - @content =~ /\A\s*<(?:\!doctype|html)/i + @content =~ DOCUMENT_RGX end def parse_as_document document = Nokogiri::HTML.parse @content, nil, 'UTF-8' @title = document.at_css('title').try(:content) - document.at_css 'body' + document end def parse_as_fragment diff --git a/lib/docs/filters/core/container.rb b/lib/docs/filters/core/container.rb index 840d3bbb..c3e89843 100644 --- a/lib/docs/filters/core/container.rb +++ b/lib/docs/filters/core/container.rb @@ -8,6 +8,8 @@ module Docs if container 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 doc end diff --git a/test/lib/docs/core/parser_test.rb b/test/lib/docs/core/parser_test.rb index 1a83a02b..21f62c39 100644 --- a/test/lib/docs/core/parser_test.rb +++ b/test/lib/docs/core/parser_test.rb @@ -14,17 +14,23 @@ class DocsParserTest < MiniTest::Spec context "with an HTML fragment" do it "returns the fragment" do body = '
Test
' - 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 context "with an HTML document" do - it "returns the " do - body = '
Test
' - assert_equal '
Test
', parser(body).html.inner_html + it "returns the document" do + body = '
Test
' + html = parser(body).html + assert_equal 'document', html.name + assert_equal '
Test
', html.at_css('body').inner_html body = '
Test
' - assert_equal '
Test
', parser(body).html.inner_html + html = parser(body).html + assert_equal 'document', html.name + assert_equal '
Test
', html.at_css('body').inner_html end end end diff --git a/test/lib/docs/filters/core/container_test.rb b/test/lib/docs/filters/core/container_test.rb index fbd9771a..cdf08c0d 100644 --- a/test/lib/docs/filters/core/container_test.rb +++ b/test/lib/docs/filters/core/container_test.rb @@ -4,6 +4,7 @@ require 'docs' class ContainerFilterTest < MiniTest::Spec include FilterTestHelper self.filter_class = Docs::ContainerFilter + self.filter_type = 'html' before do @body = '
Test
' @@ -56,8 +57,17 @@ class ContainerFilterTest < MiniTest::Spec end context "when context[:container] is nil" do - it "returns the document" do - assert_equal @body, filter_output.inner_html + context "and the document is an HTML fragment" do + 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 " do + @body = '
Test
' + assert_equal '
Test
', filter_output.inner_html + end end end end diff --git a/test/support/filter_test_helper.rb b/test/support/filter_test_helper.rb index cf9b01cc..1446a487 100644 --- a/test/support/filter_test_helper.rb +++ b/test/support/filter_test_helper.rb @@ -3,15 +3,16 @@ module FilterTestHelper included do class_attribute :filter_class + class_attribute :filter_type end def filter - @filter ||= filter_class.new @body || '', context, result + @filter ||= filter_class.new prepare_body(@body || ''), context, result end def filter_output @filter_output ||= begin - filter.instance_variable_set :@html, @body if @body + filter.instance_variable_set :@html, prepare_body(@body) if @body filter.call end end @@ -41,4 +42,12 @@ module FilterTestHelper def link_to(href) %(Link) end + + def prepare_body(body) + if self.class.filter_type == 'html' + Docs::Parser.new(body).html + else + body + end + end end