diff --git a/lib/oga/xml/element.rb b/lib/oga/xml/element.rb index 384177c..32cab3a 100644 --- a/lib/oga/xml/element.rb +++ b/lib/oga/xml/element.rb @@ -8,7 +8,7 @@ module Oga # The name of the element. # @return [String] # - # @!attribute [ww] namespace_name + # @!attribute [r] namespace_name # The name of the namespace. # @return [String] # @@ -23,7 +23,9 @@ module Oga class Element < Node include Querying - attr_accessor :name, :namespace_name, :attributes + attr_reader :namespace_name + + attr_accessor :name, :attributes attr_writer :namespaces @@ -56,6 +58,14 @@ module Oga register_namespaces_from_attributes end + ## + # @param [String] name + # + def namespace_name=(name) + @namespace_name = name + @namespace = nil + end + ## # Returns an attribute matching the given name (with or without the # namespace). diff --git a/spec/oga/xml/element_spec.rb b/spec/oga/xml/element_spec.rb index daf6ebc..fb04e80 100644 --- a/spec/oga/xml/element_spec.rb +++ b/spec/oga/xml/element_spec.rb @@ -42,6 +42,16 @@ describe Oga::XML::Element do end end + describe '#namespace_name=' do + it 'sets the namepace name' do + element = described_class.new(:name => 'a') + + element.namespace_name = 'foo' + + element.namespace_name.should == 'foo' + end + end + describe '#attribute' do before do attributes = [ @@ -209,6 +219,17 @@ describe Oga::XML::Element do element.namespace.should == namespace end + it 'flushes the cache when changing the namespace name' do + namespace = Oga::XML::Namespace.new(:name => 'bar') + element = described_class.new( + :namespaces => {'bar' => namespace} + ) + + element.namespace_name = 'foo' + + element.namespace.should be_nil + end + describe 'in an HTML document' do it 'returns nil' do ns = Oga::XML::Namespace.new(:name => 'xmlns')