From 213cbea31c016b69ef3d7aff77185c2e1408da96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?As=C4=B1m=20Mirza=20=C3=87iftler?= Date: Fri, 4 Dec 2026 09:45:04 +0300 Subject: [PATCH] Split "]]>" across CDATA sections when writing A CDATA section ends at the first "]]>", so a REXML::CData whose value contains "]]>" was written as malformed XML: REXML::Document.new on the output keeps only the part before "]]>" as CDATA and turns the rest into text, and other parsers (expat, libxml2) reject it. Write each "]]>" as "]]]]>" instead, ending the section after "]]" and opening a new one that starts with ">". Parsers read the sections back as the original text. The CData value itself is not changed. Co-Authored-By: Claude Opus 5.5 --- lib/rexml/cdata.rb | 13 ++++++++++++- lib/rexml/formatters/default.rb | 2 +- test/formatter/test_default.rb | 27 +++++++++++++++++++++++++++ test/formatter/test_pretty.rb | 9 +++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/lib/rexml/cdata.rb b/lib/rexml/cdata.rb index d7a55cb0..92ea319c 100644 --- a/lib/rexml/cdata.rb +++ b/lib/rexml/cdata.rb @@ -7,6 +7,17 @@ class CData < Text STOP = ']]>' ILLEGAL = /(\]\]>)/ + # A CDATA section can't contain its own end marker, so a "]]>" in the + # content is written by ending the section after "]]" and starting a new + # one with ">". A parser reads the two sections back as the same text. + # + # _Examples_ + # CData.escape_stop( "a]]>b" ) # -> "a]]]]>b" + def self.escape_stop( string ) + return string unless string.include?( STOP ) + string.gsub( STOP, "]]#{STOP}#{START}>" ) + end + # Constructor. CData is data between # # _Examples_ @@ -61,7 +72,7 @@ def write( output=$stdout, indent=-1, transitive=false, ie_hack=false ) Kernel.warn( "#{self.class.name}#write is deprecated", uplevel: 1) indent( output, indent ) output << START - output << @string + output << CData.escape_stop(@string) output << STOP end end diff --git a/lib/rexml/formatters/default.rb b/lib/rexml/formatters/default.rb index 8c8b73b5..c8779ead 100644 --- a/lib/rexml/formatters/default.rb +++ b/lib/rexml/formatters/default.rb @@ -102,7 +102,7 @@ def write_comment( node, output ) def write_cdata( node, output ) output << CData::START - output << node.to_s + output << CData.escape_stop(node.to_s) output << CData::STOP end diff --git a/test/formatter/test_default.rb b/test/formatter/test_default.rb index aa403dbe..2ffbb6d5 100644 --- a/test/formatter/test_default.rb +++ b/test/formatter/test_default.rb @@ -13,5 +13,32 @@ def test_content_nil assert_equal("", format(instruction)) end end + + class CDataTest < self + def test_end_marker + cdata = REXML::CData.new("a]]>b") + assert_equal("b]]>", format(cdata)) + end + + def test_multiple_end_markers + cdata = REXML::CData.new("]]>]]]>>") + assert_equal("]]]]]>>]]>", + format(cdata)) + end + + def test_round_trip + element = REXML::Element.new("a") + element.add(REXML::CData.new("if (x[y[0]]>1) {}")) + document = REXML::Document.new(format(element)) + assert_equal("if (x[y[0]]>1) {}", + document.root.cdatas.collect(&:value).join) + end + + def test_value_is_not_modified + cdata = REXML::CData.new("a]]>b") + format(cdata) + assert_equal("a]]>b", cdata.value) + end + end end end diff --git a/test/formatter/test_pretty.rb b/test/formatter/test_pretty.rb index a3e97d54..4757eda7 100644 --- a/test/formatter/test_pretty.rb +++ b/test/formatter/test_pretty.rb @@ -30,5 +30,14 @@ def test_consecutive_spaces_are_squeezed assert_equal("\n \n x y\n \n", format(document)) end end + + class CDataTest < self + def test_end_marker + document = REXML::Document.new("") + document.root.add(REXML::CData.new("a]]>b")) + assert_equal("\n b]]>\n", + format(document)) + end + end end end