diff --git a/lib/oga/xml/attribute.rb b/lib/oga/xml/attribute.rb index a734403..0087585 100644 --- a/lib/oga/xml/attribute.rb +++ b/lib/oga/xml/attribute.rb @@ -42,8 +42,17 @@ module Oga # @return [String] # def inspect - return "Attribute(name: #{name.inspect} " \ - "namespace: #{namespace.inspect} value: #{value.inspect})" + segments = [] + + [:name, :namespace, :value].each do |attr| + value = send(attr) + + if value and !value.empty? + segments << "#{attr}: #{value.inspect}" + end + end + + return "Attribute(#{segments.join(' ')})" end end # Attribute end # XML diff --git a/lib/oga/xml/doctype.rb b/lib/oga/xml/doctype.rb index dca2f8c..41a681d 100644 --- a/lib/oga/xml/doctype.rb +++ b/lib/oga/xml/doctype.rb @@ -64,22 +64,20 @@ module Oga ## # Inspects the doctype. # - # @param [Fixnum] indent The indentation level for each line. # @return [String] # - def inspect(indent = 0) - class_name = self.class.to_s.split('::').last - spacing = ' ' * indent + def inspect + segments = [] - return <<-EOF.strip -#{class_name}( -#{spacing} name: #{name.inspect} -#{spacing} type: #{type.inspect} -#{spacing} public_id: #{public_id.inspect} -#{spacing} system_id: #{system_id.inspect} -#{spacing} inline_rules: #{inline_rules.inspect} -#{spacing}) - EOF + [:name, :type, :public_id, :system_id, :inline_rules].each do |attr| + value = send(attr) + + if value and !value.empty? + segments << "#{attr}: #{value.inspect}" + end + end + + return "Doctype(#{segments.join(' ')})" end ## diff --git a/lib/oga/xml/document.rb b/lib/oga/xml/document.rb index 2f223a4..ea1b939 100644 --- a/lib/oga/xml/document.rb +++ b/lib/oga/xml/document.rb @@ -115,29 +115,20 @@ module Oga # @return [String] # def inspect - class_name = self.class.to_s.split('::').last + segments = [] child_lines = children.map { |child| child.inspect(4) }.join("\n") - if doctype - dtd = doctype.inspect(2) - else - dtd = doctype.inspect + [:doctype, :xml_declaration].each do |attr| + value = send(attr) + + if value + segments << "#{attr}: #{value.inspect}" + end end - if xml_declaration - decl = xml_declaration.inspect(2) - else - decl = xml_declaration.inspect - end + segments << "children: [\n#{child_lines}\n]" - return <<-EOF.strip -#{class_name}( - doctype: #{dtd} - xml_declaration: #{decl} - children: [ -#{child_lines} -]) - EOF + return "Document(\n #{segments.join("\n ")})" end end # Document end # XML diff --git a/lib/oga/xml/element.rb b/lib/oga/xml/element.rb index 6e3c880..31f09a0 100644 --- a/lib/oga/xml/element.rb +++ b/lib/oga/xml/element.rb @@ -114,11 +114,19 @@ module Oga child_lines = children.map { |child| child.inspect(indent + 4) } .join("\n") + segments = [] + + [:name, :namespace, :attributes].each do |attr| + value = send(attr) + + if value and !value.empty? + segments << "#{attr}: #{value.inspect}" + end + end + return <<-EOF.chomp -#{spacing} name: #{name.inspect} -#{spacing} namespace: #{namespace.inspect} -#{spacing} attributes: #{attributes.inspect} +#{spacing} #{segments.join("\n#{spacing} ")} #{spacing} children: [ #{child_lines} #{spacing}] diff --git a/lib/oga/xml/xml_declaration.rb b/lib/oga/xml/xml_declaration.rb index 56f7d12..93fe19e 100644 --- a/lib/oga/xml/xml_declaration.rb +++ b/lib/oga/xml/xml_declaration.rb @@ -49,20 +49,20 @@ module Oga end ## - # @param [Fixnum] indent # @return [String] # - def inspect(indent = 0) - class_name = self.class.to_s.split('::').last - spacing = ' ' * indent + def inspect + segments = [] - return <<-EOF.strip -#{class_name}( -#{spacing} version: #{version.inspect} -#{spacing} encoding: #{encoding.inspect} -#{spacing} standalone: #{standalone.inspect} -#{spacing}) - EOF + [:version, :encoding, :standalone].each do |attr| + value = send(attr) + + if value and !value.empty? + segments << "#{attr}: #{value.inspect}" + end + end + + return "XmlDeclaration(#{segments.join(' ')})" end ## diff --git a/spec/oga/xml/doctype_spec.rb b/spec/oga/xml/doctype_spec.rb index 6eaf96d..8be3be2 100644 --- a/spec/oga/xml/doctype_spec.rb +++ b/spec/oga/xml/doctype_spec.rb @@ -67,13 +67,7 @@ describe Oga::XML::Doctype do example 'pretty-print the node' do @instance.inspect.should == <<-EOF.strip -Doctype( - name: "html" - type: "PUBLIC" - public_id: nil - system_id: nil - inline_rules: "" -) +Doctype(name: "html" type: "PUBLIC" inline_rules: "") EOF end end diff --git a/spec/oga/xml/document_spec.rb b/spec/oga/xml/document_spec.rb index 18efebd..6d54929 100644 --- a/spec/oga/xml/document_spec.rb +++ b/spec/oga/xml/document_spec.rb @@ -146,18 +146,8 @@ describe Oga::XML::Document do example 'pretty-print the node' do @instance.inspect.should == <<-EOF.strip Document( - doctype: Doctype( - name: "html" - type: nil - public_id: nil - system_id: nil - inline_rules: nil - ) - xml_declaration: XmlDeclaration( - version: "1.0" - encoding: "UTF-8" - standalone: nil - ) + doctype: Doctype(name: "html") + xml_declaration: XmlDeclaration(version: "1.0" encoding: "UTF-8") children: [ Comment(text: "foo") ]) @@ -167,8 +157,6 @@ Document( example 'pretty-print a document without a doctype and XML declaration' do described_class.new.inspect.should == <<-EOF.strip Document( - doctype: nil - xml_declaration: nil children: [ ]) diff --git a/spec/oga/xml/element_spec.rb b/spec/oga/xml/element_spec.rb index 89703c0..2c56ee1 100644 --- a/spec/oga/xml/element_spec.rb +++ b/spec/oga/xml/element_spec.rb @@ -141,7 +141,6 @@ describe Oga::XML::Element do @instance.inspect.should == <<-EOF.strip Element( name: "p" - namespace: nil attributes: {"class"=>"foo"} children: [ Comment(text: "foo") diff --git a/spec/oga/xml/xml_declaration_spec.rb b/spec/oga/xml/xml_declaration_spec.rb index 3ef2938..743dcd1 100644 --- a/spec/oga/xml/xml_declaration_spec.rb +++ b/spec/oga/xml/xml_declaration_spec.rb @@ -50,11 +50,7 @@ describe Oga::XML::XmlDeclaration do example 'pretty-print the node' do @instance.inspect.should == <<-EOF.strip -XmlDeclaration( - version: "1.0" - encoding: "UTF-8" - standalone: nil -) +XmlDeclaration(version: "1.0" encoding: "UTF-8") EOF end end