Don't convert <script> text to XML entities.

Fixes #79.
This commit is contained in:
Yorick Peterse 2015-03-02 17:32:19 +01:00
parent 9a586363e9
commit 874d7124af
2 changed files with 35 additions and 1 deletions

View File

@ -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

View File

@ -26,6 +26,32 @@ describe Oga::XML::Text do
node.to_xml.should == '&amp;&lt;&gt;'
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 &gt; y'
end
end
describe 'inside an HTML <script> element' do
it 'does not encode special characters as XML entities' do
document = Oga::XML::Document.new(:type => :html)
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
end
describe '#inspect' do