From 47462246a3527f67a1f676ab98276551ee267732 Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Fri, 2 Oct 2020 16:59:53 +0200 Subject: [PATCH] Restore the support for matching `html` and `body` This effectively reverts 306b8f41ba73280379bc1c6c88c615097a4091ce and adds specs for matching `html` and `body` elements. The issue reported in #62 isn't solvable using DOM assertions. DOM tree has to have a root element (`html`). Using `DocumentFragment` extracts a part of the tree, so it won't contain `html` or `body` elements, but that doesn't mean they weren't a part of the input. --- lib/rspec-html-matchers/have_tag.rb | 2 +- spec/fixtures/document.html | 5 +++++ spec/have_tag_spec.rb | 12 ++++++++++++ spec/issues_spec.rb | 10 ---------- 4 files changed, 18 insertions(+), 11 deletions(-) create mode 100644 spec/fixtures/document.html diff --git a/lib/rspec-html-matchers/have_tag.rb b/lib/rspec-html-matchers/have_tag.rb index 1eb0d58..476cfb4 100644 --- a/lib/rspec-html-matchers/have_tag.rb +++ b/lib/rspec-html-matchers/have_tag.rb @@ -79,7 +79,7 @@ def matches? src, &block case src when String - parent_scope = Nokogiri::HTML::DocumentFragment.parse(src) + parent_scope = Nokogiri::HTML(src) @document = src else parent_scope = src.current_scope diff --git a/spec/fixtures/document.html b/spec/fixtures/document.html new file mode 100644 index 0000000..d1759c4 --- /dev/null +++ b/spec/fixtures/document.html @@ -0,0 +1,5 @@ + + + test + + diff --git a/spec/have_tag_spec.rb b/spec/have_tag_spec.rb index a87517c..2f4eac9 100644 --- a/spec/have_tag_spec.rb +++ b/spec/have_tag_spec.rb @@ -669,4 +669,16 @@ end end end + + context 'html and body elements' do + asset 'document' + + it 'should find the html element' do + expect(rendered).to have_tag('html') + end + + it 'should find the body element' do + expect(rendered).to have_tag('body') + end + end end diff --git a/spec/issues_spec.rb b/spec/issues_spec.rb index f55a7c9..90cc3b5 100644 --- a/spec/issues_spec.rb +++ b/spec/issues_spec.rb @@ -4,16 +4,6 @@ require 'spec_helper' describe 'working on github issues' do - describe '#62' do # https://github.com/kucaahbe/rspec-html-matchers/issues/62 - it 'should not have html tag' do - expect('

My paragraph.

').not_to have_tag('html') - end - - it 'should not have body tag' do - expect('

My paragraph.

').not_to have_tag('body') - end - end - it '#73' do # https://github.com/kucaahbe/rspec-html-matchers/issues/73 rendered = <