HTMLUtilities.st
changeset 2066 0ee2ef2d018c
parent 2058 f407ff58f780
child 2067 2fa1e3466abc
--- a/HTMLUtilities.st	Wed Dec 03 13:24:08 2008 +0100
+++ b/HTMLUtilities.st	Wed Dec 03 20:40:19 2008 +0100
@@ -63,6 +63,12 @@
         control characters, '<', '>', '&' and space -> %XX ascii as hex digits
         %     -> %%
     "
+    "/ TODO: this is similar to withSpecialHTMLCharactersEscaped.
+    "/ we should refactor this into one method only (can we do hex escapes always ?).
+    "/ Notice, that these two methods came into existance due to historic reasons
+    "/ and were developed independent of each other, but later moved to this common place.
+
+
     |rs ws c controlCharacters controlString|
 
     controlCharacters := self controlCharacters.
@@ -231,6 +237,48 @@
      self new escape:'a b'      
      self new escape:'a+b'      
     "
+!
+
+withSpecialHTMLCharactersEscaped:aStringOrCharacter
+    "replace ampersand, less and greater by html-character escapes"
+
+    "/ TODO: this is similar to escapeCharacterEntities.
+    "/ we should refactor this into one method only (can we do hex escapes always ?).
+    "/ Notice, that these two methods came into existance due to historic reasons
+    "/ and were developed independent of each other, but later moved to this common place.
+
+    |resultStream orgs repls|
+
+"/    orgs  := #( $&      $<     $>     ).
+"/    repls := #( '&amp;' '&lt;' '&gt;' ).
+
+    (aStringOrCharacter isString
+    and:[ (aStringOrCharacter includesAny:'&<>') not ]) ifTrue:[^ aStringOrCharacter].
+
+    resultStream := WriteStream on:''.
+    aStringOrCharacter asString do:[:eachCharacter |
+        "/ huh - a switch. Sorry, but this method is used heavily.
+        eachCharacter == $&
+            ifTrue:[ resultStream nextPutAll:'&amp;' ]
+            ifFalse:[
+        eachCharacter == $<
+            ifTrue:[ resultStream nextPutAll:'&lt;' ]
+            ifFalse:[
+        eachCharacter == $>
+            ifTrue:[ resultStream nextPutAll:'&gt;' ]
+            ifFalse:[
+                resultStream nextPut:eachCharacter
+            ]]].
+    ].
+    ^ resultStream contents
+
+    "
+     self withSpecialHTMLCharactersEscaped:'<>#&'
+     self withSpecialHTMLCharactersEscaped:$<
+     self withSpecialHTMLCharactersEscaped:$#
+    "
+
+    "Modified: / 05-12-2006 / 13:48:59 / cg"
 ! !
 
 !HTMLUtilities class methodsFor:'serving-helpers'!
@@ -274,5 +322,5 @@
 !HTMLUtilities class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/HTMLUtilities.st,v 1.1 2008-12-03 12:23:06 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/HTMLUtilities.st,v 1.2 2008-12-03 19:40:19 cg Exp $'
 ! !