Browse Source

* Patch from Ido Kanner to add HTMLEncode and HTMLDecode

git-svn-id: trunk@17718 -
michael 14 years ago
parent
commit
8f49adf3c8
1 changed files with 23 additions and 2 deletions
  1. 23 2
      packages/fcl-xml/src/htmlelements.pp

+ 23 - 2
packages/fcl-xml/src/htmlelements.pp

@@ -128,8 +128,30 @@ type
     procedure WriteToStream (const aStream : TStream);  override;
   end;
 
+function EscapeHTML(const s : String) : String;
+function UnescapeHTML(const s : String) : String;
+
 implementation
 
+function EscapeHTML ( const S : String ) : String;
+begin
+  Result := StringReplace(s,      '&', '&',  [rfReplaceAll]);
+  Result := StringReplace(Result, '<', '&lt;',   [rfReplaceAll]);
+  Result := StringReplace(Result, '>', '&gt;',   [rfReplaceAll]);
+  Result := StringReplace(Result, '"', '&quot;', [rfReplaceAll]);
+  Result := StringReplace(Result, #39, '&#39;',  [rfReplaceAll]); // ' - &apos; does not work on ie :(
+end;
+
+function UnescapeHTML ( const S : String ) : String;
+begin
+  Result := StringReplace(s,      '&lt;',   '<', [rfReplaceAll]);
+  Result := StringReplace(Result, '&gt;',   '>', [rfReplaceAll]);
+  Result := StringReplace(Result, '&quot;', '"', [rfReplaceAll]);
+  Result := StringReplace(Result, '&#39;',  #39, [rfReplaceAll]); // '
+  Result := StringReplace(Result, '&apos;', #39, [rfReplaceAll]); // '
+  Result := StringReplace(Result, '&amp;',  '&', [rfReplaceAll]);
+end; 
+
 
 { THtmlCustomElement }
 
@@ -201,8 +223,7 @@ end;
 
 function THtmlCustomElement.EscapeString(s: string): string;
 begin
-  result := s;
-  //TODO: Needs to convert all the special signs to their html names ("<" has to be "&lt;" etc.)
+  result := EscapeHTML(s);
 end;
 
 constructor THtmlCustomElement.create(AOwner: TDOMDocument);