]> gitweb.factorcode.org Git - factor.git/commitdiff
xml: better support for CDATA tags
authorJohn Benediktsson <mrjbq7@gmail.com>
Fri, 11 Feb 2022 23:20:23 +0000 (15:20 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Fri, 11 Feb 2022 23:20:23 +0000 (15:20 -0800)
basis/xml/data/data.factor
basis/xml/elements/elements.factor
basis/xml/tests/cdata.factor
basis/xml/tests/test.factor
basis/xml/writer/writer-tests.factor
basis/xml/writer/writer.factor

index 645c693540baa37124bacc6ed57bf08258108db2..bb948e159beb5f06c908a0cb95f21c05401fe8cb 100644 (file)
@@ -85,6 +85,9 @@ C: <contained> contained
 TUPLE: comment { text string } ;
 C: <comment> comment
 
+TUPLE: cdata { text string } ;
+C: <cdata> cdata
+
 TUPLE: directive ;
 
 TUPLE: element-decl < directive
@@ -220,7 +223,7 @@ TUPLE: unescaped string ;
 C: <unescaped> unescaped
 
 UNION: xml-data
-    tag comment string directive instruction unescaped ;
+    tag comment cdata string directive instruction unescaped ;
 
 TUPLE: xml-chunk seq ;
 C: <xml-chunk> xml-chunk
index 042cde931e7926136c58bbfca63c56f629926b7e..24d2f68467d13161b75af4a6815714d78b88736c 100644 (file)
@@ -98,9 +98,9 @@ IN: xml.elements
         [ "?>" take-string append <instruction> ]
     } cond ;
 
-: take-cdata ( -- string )
+: take-cdata ( -- cdata )
     depth get zero? [ bad-cdata ] when
-    "[CDATA[" expect "]]>" take-string ;
+    "[CDATA[" expect "]]>" take-string <cdata> ;
 
 DEFER: make-tag ! Is this unavoidable?
 
index 56f344610c05923a14511701a264c9e66d67694e..8eddb4bcf056133e5aed5c7407509402d0d36e9c 100644 (file)
@@ -6,7 +6,7 @@ IN: xml.tests
 <rss version=\"2.0\">
   <channel>
     <item>
-      <description>Python has a n class property in [&amp;#8230;]</description>
+      <description><![CDATA[Python has a n class property in [&#8230;]]]></description>
     </item>
   </channel>
 </rss>"
index e8ece4fad68cf96ac1818931ed8a91d2866bfdba..1ee7ecc166a3735d546662959d14d9e4113b8b6f 100644 (file)
@@ -52,7 +52,6 @@ SYMBOL: xml-file
     dup dup "y" attr "z" set-attr
     T{ name { space "blah" } { main "z" } } attr
 ] unit-test
-[ "foo" ] [ "<boo><![CDATA[foo]]></boo>" string>xml children>string ] unit-test
 [ "<!-- B+, B, or B--->" string>xml ] must-fail
 [ ] [ "<?xml version='1.0'?><!-- declarations for <head> & <body> --><foo/>" string>xml drop ] unit-test
 
index f4ebe2616a31e568bc12a697d1fb82e5d9e80108..d12e530ee7dafd96ffa8ec442957536e02fe4aa8 100644 (file)
@@ -58,6 +58,7 @@ IN: xml.writer.tests
 { "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<foo>\n  bar\n</foo>" }
 [ "<foo>         bar            </foo>" string>xml pprint-xml>string ] unit-test
 { "<foo'>" } [ "<foo'>" <unescaped> xml>string ] unit-test
+{ "<![CDATA[<&'\"]]>" } [ "<&'\"" <cdata> xml>string ] unit-test
 
 : test-file ( -- path )
     "test.xml" temp-file ;
index bd0259d6ba1332bf4e5cc5323291ec686f260417..36647ddef04aff0b76e7aa04640cb57cd4404e9a 100644 (file)
@@ -102,6 +102,9 @@ M: unescaped write-xml
 M: comment write-xml
     "<!--" write text>> write "-->" write ;
 
+M: cdata write-xml
+    "<![CDATA[" write text>> write "]]>" write ;
+
 : write-decl ( decl name quot: ( decl -- slot ) -- )
     "<!" write swap write bl
     [ name>> write bl ]