diff --git a/lib/oga/xml/document.rb b/lib/oga/xml/document.rb index 988f1d5..053b67d 100644 --- a/lib/oga/xml/document.rb +++ b/lib/oga/xml/document.rb @@ -12,7 +12,7 @@ module Oga # The XML declaration of the document. # @return [Oga::XML::XmlDeclaration] # - # @!attribute [rw] type + # @!attribute [r] type # The document type, either `:xml` or `:html`. # @return [Symbol] # @@ -20,7 +20,9 @@ module Oga include Querying include Traversal - attr_accessor :doctype, :xml_declaration, :type + attr_accessor :doctype, :xml_declaration + + attr_reader :type ## # @param [Hash] options diff --git a/lib/oga/xml/node.rb b/lib/oga/xml/node.rb index 59e8a08..8fc4cf4 100644 --- a/lib/oga/xml/node.rb +++ b/lib/oga/xml/node.rb @@ -5,13 +5,13 @@ module Oga # {Oga::XML::NodeSet} and can be used to query surrounding and parent # nodes. # - # @!attribute [rw] node_set + # @!attribute [r] node_set # @return [Oga::XML::NodeSet] # class Node include Traversal - attr_accessor :node_set + attr_reader :node_set ## # @param [Hash] options @@ -23,11 +23,19 @@ module Oga # the current node. # def initialize(options = {}) - @node_set = options[:node_set] - + self.node_set = options[:node_set] self.children = options[:children] if options[:children] end + ## + # @param [Oga::XML::NodeSet] set + # + def node_set=(set) + @node_set = set + @root_node = nil + @html_p = nil + end + ## # Returns the child nodes of the current node. # @@ -120,17 +128,21 @@ module Oga # @return [Oga::XML::Document|Oga::XML::Node] # def root_node - node = self + unless @root_node + node = self - loop do - if !node.is_a?(Document) and node.node_set - node = node.node_set.owner - else - break + loop do + if !node.is_a?(Document) and node.node_set + node = node.node_set.owner + else + break + end end + + @root_node = node end - return node + return @root_node end ## @@ -168,9 +180,13 @@ module Oga # @return [TrueClass|FalseClass] # def html? - root = root_node + if @html_p.nil? + root = root_node - return root.is_a?(Document) && root.html? + @html_p = root.is_a?(Document) && root.html? + end + + return @html_p end ## diff --git a/spec/oga/xml/node_spec.rb b/spec/oga/xml/node_spec.rb index e1c6c86..b52621f 100644 --- a/spec/oga/xml/node_spec.rb +++ b/spec/oga/xml/node_spec.rb @@ -144,13 +144,19 @@ describe Oga::XML::Node do @doc = Oga::XML::Document.new(:children => [@n1]) end - it 'returns the root document of an element' do + it 'returns the root document of a Node' do @n2.root_node.should == @doc end - it 'returns the root element of another element' do + it 'returns the root Node of another Node' do @n4.root_node.should == @n3 end + + it 'flushes the cache when changing the NodeSet of a Node' do + @n1.children << @n4 + + @n4.root_node.should == @doc + end end describe '#remove' do @@ -219,6 +225,18 @@ describe Oga::XML::Node do node.html?.should == false end + + it 'flushes the cache when changing the NodeSet of a Node' do + node = described_class.new + html_doc = Oga::XML::Document.new(:type => :html) + xml_doc = Oga::XML::Document.new(:type => :xml, :children => [node]) + + node.html?.should == false + + html_doc.children << node + + node.html?.should == true + end end describe '#xml?' do