diff --git a/lib/oga/xml/text.rb b/lib/oga/xml/text.rb index 86bc4b5..c51edae 100644 --- a/lib/oga/xml/text.rb +++ b/lib/oga/xml/text.rb @@ -9,7 +9,15 @@ module Oga # @see [Oga::XML::CharacterNode#to_xml] # def to_xml - return Entities.encode(super) + node = parent + root = root_node + + if root.is_a?(Document) and node.is_a?(Element) \ + and node.name == Lexer::SCRIPT_TAG and root.html? + return super + else + return Entities.encode(super) + end end end # Text end # XML diff --git a/spec/oga/xml/text_spec.rb b/spec/oga/xml/text_spec.rb index d3cd76b..f8f5d89 100644 --- a/spec/oga/xml/text_spec.rb +++ b/spec/oga/xml/text_spec.rb @@ -26,6 +26,32 @@ describe Oga::XML::Text do node.to_xml.should == '&<>' end + + describe 'inside an XML document' do + it 'encodes special characters as XML entities' do + document = Oga::XML::Document.new + script = Oga::XML::Element.new(:name => 'script') + text = described_class.new(:text => 'x > y') + + script.children << text + document.children << script + + text.to_xml.should == 'x > y' + end + end + + describe 'inside an HTML