Browse Source

XML writer: split CDATA sections at the ']]>' sequence.

git-svn-id: trunk@14186 -
sergei 15 years ago
parent
commit
5c60f0e9e2
1 changed files with 15 additions and 1 deletions
  1. 15 1
      packages/fcl-xml/src/xmlwrite.pp

+ 15 - 1
packages/fcl-xml/src/xmlwrite.pp

@@ -316,6 +316,7 @@ end;
 const
   AttrSpecialChars = ['<', '"', '&', #9, #10, #13];
   TextSpecialChars = ['<', '>', '&'];
+  CDSectSpecialChars = [']'];
 
 procedure TXMLWriter.ConvWrite(const s: WideString; const SpecialChars: TSetOfChar;
   const SpecialCharCallback: TSpecialCharCallback);
@@ -372,6 +373,19 @@ begin
   end;
 end;
 
+procedure CDSectSpecialCharCallback(Sender: TXMLWriter; const s: DOMString;
+  var idx: Integer);
+begin
+  if (idx <= Length(s)-2) and (s[idx+1] = ']') and (s[idx+2] = '>') then
+  begin
+    Sender.wrtStr(']]]]><![CDATA[>');
+    Inc(idx, 2);
+    // TODO: emit warning 'cdata-section-splitted'
+  end
+  else
+    Sender.wrtChr(s[idx]);
+end;
+
 procedure TXMLWriter.WriteNode(node: TDOMNode);
 begin
   case node.NodeType of
@@ -598,7 +612,7 @@ begin
   else
   begin
     wrtChars('<![CDATA[', 9);
-    wrtStr(TDOMCharacterData(node).Data);
+    ConvWrite(TDOMCharacterData(node).Data, CDSectSpecialChars, @CDSectSpecialCharCallback);
     wrtChars(']]>', 3);
   end;
 end;