From 9a586363e901cfacb679fe37ac89da7e4e62ea84 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Mon, 2 Mar 2015 16:38:51 +0100 Subject: [PATCH] Added XML::Document#html? --- lib/oga/xml/document.rb | 7 +++++++ lib/oga/xml/element.rb | 2 +- spec/oga/xml/document_spec.rb | 10 ++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/oga/xml/document.rb b/lib/oga/xml/document.rb index 0c824a2..988f1d5 100644 --- a/lib/oga/xml/document.rb +++ b/lib/oga/xml/document.rb @@ -77,6 +77,13 @@ module Oga return xml end + ## + # @return [TrueClass|FalseClass] + # + def html? + return type == :html + end + ## # Inspects the document and its child nodes. Child nodes are indented for # each nesting level. diff --git a/lib/oga/xml/element.rb b/lib/oga/xml/element.rb index 209d809..26eabff 100644 --- a/lib/oga/xml/element.rb +++ b/lib/oga/xml/element.rb @@ -307,7 +307,7 @@ module Oga self_closing = children.empty? root = root_node - if root.is_a?(Document) and root.type == :html \ + if root.is_a?(Document) and root.html? \ and !HTML_VOID_ELEMENTS.include?(name) self_closing = false end diff --git a/spec/oga/xml/document_spec.rb b/spec/oga/xml/document_spec.rb index bec8865..29ac158 100644 --- a/spec/oga/xml/document_spec.rb +++ b/spec/oga/xml/document_spec.rb @@ -97,6 +97,16 @@ describe Oga::XML::Document do end end + describe '#html?' do + it 'returns false for an XML document' do + described_class.new(:type => :xml).html?.should == false + end + + it 'returns true for an HTML document' do + described_class.new(:type => :html).html?.should == true + end + end + describe '#inspect' do before do @instance = described_class.new(