HTMLUtilities.st
author Claus Gittinger <cg@exept.de>
Sun, 24 Mar 2019 21:25:08 +0100
changeset 4911 fc066ca35d57
parent 4737 610d483cb00a
child 4924 b171682381a1
permissions -rw-r--r--
class: CRC32Stream class added: #hashSize comment/format in: #documentation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
     1
"{ Encoding: utf8 }"
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
     2
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
"
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
 COPYRIGHT (c) 2007 by eXept Software AG
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
              All Rights Reserved
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
 This software is furnished under a license and may be used
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
 only in accordance with the terms of that license and with the
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
 inclusion of the above copyright notice.   This software may not
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
 be provided or otherwise made available to, or used by, any
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
 other person.  No title to or ownership of the software is
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
 hereby transferred.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
"
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
"{ Package: 'stx:libbasic2' }"
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
    16
"{ NameSpace: Smalltalk }"
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
    17
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
Object subclass:#HTMLUtilities
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
	instanceVariableNames:''
4517
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    20
	classVariableNames:'EscapeControlCharacters HtmlEntityToCharacter'
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
	poolDictionaries:''
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
	category:'Net-Communication-Support'
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
!
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
!HTMLUtilities class methodsFor:'documentation'!
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
copyright
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
"
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
 COPYRIGHT (c) 2007 by eXept Software AG
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
              All Rights Reserved
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
 This software is furnished under a license and may be used
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
 only in accordance with the terms of that license and with the
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
 inclusion of the above copyright notice.   This software may not
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
 be provided or otherwise made available to, or used by, any
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
 other person.  No title to or ownership of the software is
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
 hereby transferred.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
"
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
!
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
documentation
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
"
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
    Collected support functions to deal with HTML.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
    Used both by HTML generators (DocGenerator), HTMLParsers and the webServer.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
    Therefore, it has been put into libbasic2.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
"
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
! !
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
2442
db061ff41012 added: #openLauncherOnDisplay:
sr
parents: 2436
diff changeset
    49
!HTMLUtilities class methodsFor:'common actions'!
db061ff41012 added: #openLauncherOnDisplay:
sr
parents: 2436
diff changeset
    50
db061ff41012 added: #openLauncherOnDisplay:
sr
parents: 2436
diff changeset
    51
openLauncherOnDisplay:displayName
2458
8c1955020123 changed: #openLauncherOnDisplay:
sr
parents: 2442
diff changeset
    52
    <resource: #obsolete>
2442
db061ff41012 added: #openLauncherOnDisplay:
sr
parents: 2436
diff changeset
    53
2458
8c1955020123 changed: #openLauncherOnDisplay:
sr
parents: 2442
diff changeset
    54
    "obsolete - do not use"
2442
db061ff41012 added: #openLauncherOnDisplay:
sr
parents: 2436
diff changeset
    55
2458
8c1955020123 changed: #openLauncherOnDisplay:
sr
parents: 2442
diff changeset
    56
    self obsoleteMethodWarning.
8c1955020123 changed: #openLauncherOnDisplay:
sr
parents: 2442
diff changeset
    57
    Error handle:[:ex |
8c1955020123 changed: #openLauncherOnDisplay:
sr
parents: 2442
diff changeset
    58
        ^ ex description
8c1955020123 changed: #openLauncherOnDisplay:
sr
parents: 2442
diff changeset
    59
    ] do:[
8c1955020123 changed: #openLauncherOnDisplay:
sr
parents: 2442
diff changeset
    60
        NewLauncher openLauncherOnInitializedDisplayNamed:displayName
8c1955020123 changed: #openLauncherOnDisplay:
sr
parents: 2442
diff changeset
    61
    ]
2442
db061ff41012 added: #openLauncherOnDisplay:
sr
parents: 2436
diff changeset
    62
2458
8c1955020123 changed: #openLauncherOnDisplay:
sr
parents: 2442
diff changeset
    63
    "Modified: / 01-06-2010 / 11:25:12 / sr"
2442
db061ff41012 added: #openLauncherOnDisplay:
sr
parents: 2436
diff changeset
    64
! !
db061ff41012 added: #openLauncherOnDisplay:
sr
parents: 2436
diff changeset
    65
4517
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    66
!HTMLUtilities class methodsFor:'constants'!
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    67
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    68
htmlEntityToCharacter
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    69
    |htmlEntityToCharacter|
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    70
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    71
    HtmlEntityToCharacter isNil ifTrue:[
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    72
        htmlEntityToCharacter := Dictionary new.
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    73
        htmlEntityToCharacter
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    74
            at:'quot'               put:$";
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    75
            at:'amp'                put:$&;
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    76
            at:'apos'               put:$';
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    77
            at:'lt'                 put:$<;
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    78
            at:'gt'                 put:$>;
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    79
            at:'Auml'               put:$Ä;
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    80
            at:'Ouml'               put:$Ö;
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    81
            at:'Uuml'               put:$Ü;
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    82
            at:'szlig'              put:$ß;
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    83
            at:'auml'               put:$ä;
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    84
            at:'ouml'               put:$ö;
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    85
            at:'uuml'               put:$ü.
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    86
            "/ Answer: It is a mess. A good start may be
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    87
            "/ https://www.w3.org/TR/html4/sgml/entities.html with 252 named entities.
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    88
            "/ I guess an actual lookup table would be adequate.
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    89
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    90
        HtmlEntityToCharacter := htmlEntityToCharacter.
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    91
    ].
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    92
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    93
    ^ HtmlEntityToCharacter                                      
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    94
! !
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    95
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
!HTMLUtilities class methodsFor:'helpers'!
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
3557
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
    98
characterFromHtmlEntityNamed:anHtmlEntityName
4517
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
    99
    ^ self htmlEntityToCharacter
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   100
        at:anHtmlEntityName
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   101
        ifAbsent:[
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   102
            self halt. "/ where to get the mapping???
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   103
            "/ Answer: It is a mess. A good start may be
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   104
            "/ https://www.w3.org/TR/html4/sgml/entities.html with 252 named entities.
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   105
            "/ I guess an actual lookup table would be adequate.
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   106
            $~
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   107
        ]
3557
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   108
!
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   109
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
controlCharacters
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
    EscapeControlCharacters isNil ifTrue:[
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
        EscapeControlCharacters := Dictionary new.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
        EscapeControlCharacters at:$< put:'&lt;'.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
        EscapeControlCharacters at:$> put:'&gt;'.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
        EscapeControlCharacters at:$& put:'&amp;'.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
        EscapeControlCharacters at:$" put:'&quot;'.
2436
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   118
        "/ EscapeControlCharacters at:$' put:'&apos;'.
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
    ].
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
    ^ EscapeControlCharacters.
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   121
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   122
    "Modified (comment): / 06-05-2015 / 16:17:31 / sr"
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
!
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
4517
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   125
copyReplaceCharactersWithHtmlEntitiesIn:aString
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   126
    |stream htmlEntity|
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   127
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   128
    stream := '' writeStream.
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   129
    (aString ? '') do:[:eachCharacter |
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   130
        htmlEntity := self htmlEntityForCharacter:eachCharacter.
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   131
        htmlEntity isNil ifTrue:[
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   132
            stream nextPut:eachCharacter.
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   133
        ] ifFalse:[
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   134
            stream
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   135
                nextPut:$&;
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   136
                nextPutAll:htmlEntity;
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   137
                nextPut:$;.           
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   138
        ].
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   139
    ].
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   140
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   141
    ^ stream contents
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   142
!
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   143
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   144
escapeCharacterEntities:aString
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   145
    "helper to escape invalid/dangerous characters in html strings.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   146
     These are:
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   147
        control characters, '<', '>', '&' and space -> %XX ascii as hex digits
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   148
        %     -> %%
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   149
    "
2066
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   150
    "/ TODO: this is similar to withSpecialHTMLCharactersEscaped.
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   151
    "/ we should refactor this into one method only (can we do hex escapes always ?).
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   152
    "/ Notice, that these two methods came into existance due to historic reasons
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   153
    "/ and were developed independent of each other, but later moved to this common place.
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   154
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   155
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   156
    ^self escapeCharacterEntities:aString andControlCharacters:self controlCharacters
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   157
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   158
    "
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   159
     self escapeCharacterEntities:'a<b'     
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   160
     self escapeCharacterEntities:'aöb'     
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   161
    "
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   162
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   163
    "Modified: / 06-05-2015 / 16:30:13 / sr"
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   164
!
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   165
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   166
escapeCharacterEntities:aString andControlCharacters:controlCharacters
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   167
    "helper to escape invalid/dangerous characters in html strings.
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   168
     These are:
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   169
        control characters, '<', '>', '&' and space -> %XX ascii as hex digits
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   170
        %     -> %%
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   171
    "
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   172
    "/ TODO: this is similar to withSpecialHTMLCharactersEscaped.
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   173
    "/ we should refactor this into one method only (can we do hex escapes always ?).
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   174
    "/ Notice, that these two methods came into existance due to historic reasons
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   175
    "/ and were developed independent of each other, but later moved to this common place.
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   176
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   177
4296
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   178
    ^ String 
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   179
        streamContents:[:ws |
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   180
            self escapeCharacterEntities:aString andControlCharacters:controlCharacters on:ws.
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   181
        ]
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   182
    
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   183
    "
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   184
     self escapeCharacterEntities:'a<b'     
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   185
     self escapeCharacterEntities:'aöb'     
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   186
    "
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   187
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   188
    "Created: / 06-05-2015 / 16:29:51 / sr"
4296
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   189
    "Modified (format): / 05-02-2017 / 17:59:32 / cg"
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   190
!
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   191
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   192
escapeCharacterEntities:aString andControlCharacters:controlCharacters on:aWriteStream
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   193
    "helper to escape invalid/dangerous characters in html strings.
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   194
     These are:
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   195
        control characters, '<', '>', '&' and space -> %XX ascii as hex digits
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   196
        %     -> %%
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   197
    "
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   198
    "/ TODO: this is similar to withSpecialHTMLCharactersEscaped.
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   199
    "/ we should refactor this into one method only (can we do hex escapes always ?).
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   200
    "/ Notice, that these two methods came into existance due to historic reasons
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   201
    "/ and were developed independent of each other, but later moved to this common place.
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   202
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   203
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   204
    |rs c controlString|
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   205
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   206
    rs := ReadStream on: aString.
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   207
    [ rs atEnd ] whileFalse: [
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   208
        c := rs next.
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   209
        controlString := controlCharacters notEmptyOrNil ifTrue:[controlCharacters at:c ifAbsent:nil] ifFalse:[nil].
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   210
        controlString notNil ifTrue:[
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   211
            aWriteStream nextPutAll:controlString.
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   212
        ] ifFalse:[
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   213
            c codePoint > 16r7F ifTrue:[
4333
2e428045cb82 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4302
diff changeset
   214
                aWriteStream nextPutAll:'&#'.
2e428045cb82 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4302
diff changeset
   215
                c codePoint printOn:aWriteStream.
2e428045cb82 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4302
diff changeset
   216
                aWriteStream nextPut:$;.
4296
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   217
            ] ifFalse:[
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   218
                aWriteStream nextPut:c.
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   219
            ]
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   220
        ]
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   221
    ].
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   222
    
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   223
    "
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   224
     self escapeCharacterEntities:'a<b'     
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   225
     self escapeCharacterEntities:'aöb'     
4296
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   226
    "
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   227
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   228
    "Created: / 05-02-2017 / 17:58:34 / cg"
4333
2e428045cb82 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4302
diff changeset
   229
    "Modified: / 17-02-2017 / 10:34:20 / stefan"
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   230
!
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   231
4297
0908351381fd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4296
diff changeset
   232
escapeCharacterEntities:aString on:aStream
0908351381fd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4296
diff changeset
   233
    "helper to escape invalid/dangerous characters in html strings.
0908351381fd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4296
diff changeset
   234
     These are:
0908351381fd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4296
diff changeset
   235
        control characters, '<', '>', '&' and space -> %XX ascii as hex digits
0908351381fd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4296
diff changeset
   236
        %     -> %%
0908351381fd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4296
diff changeset
   237
    "
0908351381fd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4296
diff changeset
   238
    "/ TODO: this is similar to withSpecialHTMLCharactersEscaped.
0908351381fd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4296
diff changeset
   239
    "/ we should refactor this into one method only (can we do hex escapes always ?).
0908351381fd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4296
diff changeset
   240
    "/ Notice, that these two methods came into existance due to historic reasons
0908351381fd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4296
diff changeset
   241
    "/ and were developed independent of each other, but later moved to this common place.
0908351381fd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4296
diff changeset
   242
0908351381fd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4296
diff changeset
   243
0908351381fd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4296
diff changeset
   244
    ^self escapeCharacterEntities:aString andControlCharacters:self controlCharacters on:aStream
0908351381fd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4296
diff changeset
   245
0908351381fd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4296
diff changeset
   246
    "
0908351381fd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4296
diff changeset
   247
     self escapeCharacterEntities:'a<b'     
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   248
     self escapeCharacterEntities:'aöb'     
4297
0908351381fd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4296
diff changeset
   249
    "
0908351381fd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4296
diff changeset
   250
0908351381fd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4296
diff changeset
   251
    "Created: / 05-02-2017 / 18:00:56 / cg"
0908351381fd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4296
diff changeset
   252
!
0908351381fd #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4296
diff changeset
   253
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   254
extractCharSetEncodingFromContentType:contentTypeLine
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   255
    |idx rest encoding|
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   256
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   257
    idx := contentTypeLine findString:'charset='.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   258
    idx == 0 ifTrue:[
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   259
	^ nil
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   260
    ].
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   261
    rest := (contentTypeLine copyFrom:idx+'charset=' size) withoutSeparators.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   262
    idx := (rest indexOfSeparator) min:(rest indexOf:$;).
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   263
    idx == 0 ifTrue:[
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   264
	encoding := rest
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   265
    ] ifFalse:[
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   266
	encoding := rest copyTo:idx-1.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   267
    ].
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   268
    (encoding startsWith:$") ifTrue:[
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   269
	encoding := encoding copyFrom:2 to:(encoding indexOf:$" startingAt:3)-1.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   270
    ].
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   271
    ^ encoding.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   272
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   273
    "
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   274
     self extractCharSetEncodingFromContentType:'text/html; charset=ascii'
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   275
     self extractCharSetEncodingFromContentType:'text/html; charset='
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   276
     self extractCharSetEncodingFromContentType:'text/html; fooBar=bla'
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   277
     self extractCharSetEncodingFromContentType:'text/xml; charset=utf-8'
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   278
     self extractCharSetEncodingFromContentType:'text/xml; charset=utf-8; bla=fasel'
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   279
    "
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   280
!
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   281
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   282
extractMimeTypeFromContentType:contentTypeLine
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   283
    |idx mimeAndEncoding|
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   284
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   285
    idx := contentTypeLine indexOf:$:.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   286
    mimeAndEncoding := (contentTypeLine copyFrom:idx+1) withoutSeparators.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   287
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   288
    (mimeAndEncoding includes:$;) ifFalse:[
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   289
	^ mimeAndEncoding
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   290
    ].
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   291
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   292
    idx := mimeAndEncoding indexOf:$;.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   293
    ^ mimeAndEncoding copyTo:idx-1
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   294
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   295
    "
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   296
     self extractMimeTypeFromContentType:'text/html; charset=ascii'
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   297
     self extractMimeTypeFromContentType:'text/html; '
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   298
     self extractMimeTypeFromContentType:'text/html'
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   299
     self extractMimeTypeFromContentType:'text/xml; charset=utf-8'
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   300
    "
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   301
!
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   302
4517
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   303
htmlEntityForCharacter:aCharacter
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   304
    ^ self htmlEntityToCharacter
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   305
        keyAtValue:aCharacter
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   306
        ifAbsent:nil
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   307
!
5c92422a4187 #FEATURE by sr
sr
parents: 4494
diff changeset
   308
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   309
unEscape:aString
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   310
    "Convert escaped characters in an urls arguments or post fields back to their proper characters.
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   311
     Undoes the effect of #urlEncoded: and #urlEncoded2:.
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   312
     These are:
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   313
        + -> space
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   314
        %XX ascii as hex digits
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   315
        %uXXXX unicode as hex digits   NOTE: %u is non-standard bit implemented in MS IIS
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   316
        %% -> %
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   317
    "
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   318
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   319
    |rs ws c peekC isUnicodeEscaped|
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   320
4204
481e0286fce9 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 3660
diff changeset
   321
    aString isNil ifTrue:[
481e0286fce9 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 3660
diff changeset
   322
        ^ nil.
481e0286fce9 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 3660
diff changeset
   323
    ].
481e0286fce9 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 3660
diff changeset
   324
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   325
    (aString includesAny:'+%') ifFalse:[        
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   326
        ^ aString
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   327
    ].
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   328
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   329
    rs := ReadStream on: aString.
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   330
    ws := CharacterWriteStream on: ''.
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   331
    isUnicodeEscaped := false.
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   332
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   333
    [rs atEnd] whileFalse:[
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   334
        c := rs next.
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   335
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   336
        isUnicodeEscaped ifTrue:[
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   337
            isUnicodeEscaped := false.
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   338
            c := (Integer readFrom:(rs nextAvailable:4) radix:16) asCharacter.
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   339
        ] ifFalse:[
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   340
            c == $+ ifTrue:[ 
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   341
                c := Character space.
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   342
            ] ifFalse:[
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   343
                c == $% ifTrue:[
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   344
                    peekC := rs peek.
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   345
                    (peekC notNil and:[peekC isHexDigit]) ifTrue:[
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   346
                        c := (Integer readFrom:(rs nextAvailable:2) radix:16) asCharacter. 
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   347
                    ] ifFalse:[
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   348
                        (peekC notNil and:[peekC == $u]) ifTrue:[
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   349
                            isUnicodeEscaped := true.
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   350
                            c := nil.
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   351
                        ] ifFalse:[
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   352
                            c := rs next.
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   353
                        ].
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   354
                    ].
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   355
                ].
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   356
            ].
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   357
        ].
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   358
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   359
        c notNil ifTrue:[ 
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   360
            ws nextPut:c.
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   361
        ].
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   362
    ].
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   363
    ^ ws contents
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   364
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   365
    "
2087
6a7385a63ce0 *** empty log message ***
sr
parents: 2067
diff changeset
   366
     self unEscape:'a%20b'   
6a7385a63ce0 *** empty log message ***
sr
parents: 2067
diff changeset
   367
     self unEscape:'a%%b'
6a7385a63ce0 *** empty log message ***
sr
parents: 2067
diff changeset
   368
     self unEscape:'a+b' 
6a7385a63ce0 *** empty log message ***
sr
parents: 2067
diff changeset
   369
     self unEscape:'a%+b' 
2179
c1cee8bbc1e5 unescape: care for invalid escape sequence (%, %singleDigit atEnd)
sr
parents: 2144
diff changeset
   370
     self unEscape:'a%' 
c1cee8bbc1e5 unescape: care for invalid escape sequence (%, %singleDigit atEnd)
sr
parents: 2144
diff changeset
   371
     self unEscape:'a%2' 
4287
7d7b30363fa8 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4217
diff changeset
   372
     self unEscape:'/Home/a%C3%A4%C3%B6%C3%BCa'
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   373
    "
2179
c1cee8bbc1e5 unescape: care for invalid escape sequence (%, %singleDigit atEnd)
sr
parents: 2144
diff changeset
   374
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   375
    "Modified: / 09-01-2011 / 10:44:50 / cg"
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   376
    "Modified (comment): / 06-05-2015 / 15:40:04 / sr"
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   377
    "Modified (comment): / 03-02-2017 / 17:06:32 / stefan"
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   378
!
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   379
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   380
unescapeCharacterEntities:aString
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   381
    "helper to unescape character entities in a string.
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   382
     Normally, this is done by the HTMLParser when it scans text,
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   383
     but seems to be also used in post-data fields which contain non-ascii characters
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   384
     (for example: the login postdata of expeccALM).
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   385
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   386
     Sequences are:
3557
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   387
        &<specialName>;
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   388
        &#<decimal>;            
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   389
        &#x<hex>
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   390
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   391
     From Reference:
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   392
        http://wiki.selfhtml.org/wiki/Referenz:HTML/Zeichenreferenz#HTML-eigene_Zeichen
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   393
    "
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   394
3557
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   395
    |rs ws c 
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   396
     entity entityNumberPart
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   397
     htmlEntityMatchingFailed characterFromHtmlEntity|
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   398
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   399
    (aString includes:$&) ifFalse:[        
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   400
        ^ aString
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   401
    ].
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   402
3557
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   403
    rs := ReadStream on:aString.
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   404
    ws := CharacterWriteStream on:''.
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   405
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   406
    [rs atEnd] whileFalse:[
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   407
        c := rs next.
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   408
        c == $& ifTrue:[
3557
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   409
            entity := rs upToMatching:[:ch | ch == $;].
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   410
            entity notEmpty ifTrue:[
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   411
                rs peek == $; ifTrue:[ "/ something between & and ; 
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   412
                    rs next. "/ read over semicolon
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   413
                    htmlEntityMatchingFailed := false.
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   414
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   415
                    entity first == $# ifTrue:[ "/ entity is determined as number
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   416
                        entityNumberPart := entity copyFrom:2.
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   417
                        entityNumberPart notEmpty ifTrue:[
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   418
                            entityNumberPart first == $x ifTrue:[
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   419
                                entityNumberPart := entityNumberPart copyFrom:2.
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   420
                                entityNumberPart notEmpty ifTrue:[
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   421
                                    ws nextPut:(Character value:(Integer readFrom:entityNumberPart radix:16)).
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   422
                                ] ifFalse:[
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   423
                                    htmlEntityMatchingFailed := true. 
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   424
                                ].
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   425
                            ] ifFalse:[
3557
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   426
                                entityNumberPart isNumeric ifTrue:[
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   427
                                    ws nextPut:(Character value:(Integer readFrom:entityNumberPart)).
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   428
                                ] ifFalse:[
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   429
                                    htmlEntityMatchingFailed := true. 
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   430
                                ].
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   431
                            ].
3557
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   432
                        ] ifFalse:[
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   433
                            htmlEntityMatchingFailed := true. 
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   434
                        ].
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   435
                    ] ifFalse:[
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   436
                        characterFromHtmlEntity := self characterFromHtmlEntityNamed:entity.
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   437
                        characterFromHtmlEntity notNil ifTrue:[
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   438
                            ws nextPut:characterFromHtmlEntity.
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   439
                        ] ifFalse:[
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   440
                            htmlEntityMatchingFailed := true. 
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   441
                        ].
3557
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   442
                    ].
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   443
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   444
                    htmlEntityMatchingFailed ifTrue:[
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   445
                        ws nextPut:c.
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   446
                        ws nextPutAll:entity.
4333
2e428045cb82 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4302
diff changeset
   447
                        ws nextPut:$;.
3557
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   448
                    ].
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   449
                ] ifFalse:[
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   450
                    ws nextPut:c.
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   451
                    ws nextPutAll:entity.
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   452
                ].
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   453
            ] ifFalse:[
3557
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   454
                ws nextPut:c.
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   455
            ].
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   456
        ] ifFalse:[
3557
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   457
            ws nextPut:c.
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   458
        ].
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   459
    ].
3557
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   460
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   461
    ^ ws contents
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   462
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   463
    "
3557
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   464
     self unescapeCharacterEntities:'&;'            
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   465
     self unescapeCharacterEntities:'&16368;'            
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   466
     self unescapeCharacterEntities:'&16368;&16368'            
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   467
     self unescapeCharacterEntities:'&16368;&lt;'            
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   468
     self unescapeCharacterEntities:'&16368;&lt'            
3557
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   469
     self unescapeCharacterEntities:'&#xaffe;'    
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   470
     self unescapeCharacterEntities:'&quot;&lt;foo'      
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   471
     self unescapeCharacterEntities:'&funny;&lt;foo'     
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   472
    "
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   473
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   474
    "Created: / 06-05-2015 / 16:56:14 / sr"
3557
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   475
    "Modified: / 18-05-2015 / 12:13:35 / sr"
4333
2e428045cb82 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 4302
diff changeset
   476
    "Modified: / 17-02-2017 / 10:18:35 / stefan"
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   477
!
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   478
4712
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   479
urlDecoded:aString
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   480
    "Convert escaped characters in an urls arguments or post fields back to their proper characters.
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   481
     Undoes the effect of #urlEncoded: and #urlEncoded2:.
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   482
     These are:
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   483
        + -> space
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   484
        %XX ascii as hex digits
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   485
        %uXXXX unicode as hex digits   NOTE: %u is non-standard bit implemented in MS IIS
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   486
        %% -> %
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   487
    "
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   488
    ^ (self unEscape:aString) utf8Decoded
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   489
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   490
    "
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   491
     self urlDecoded:'a%20b'   
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   492
     self urlDecoded:'a%%b'
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   493
     self urlDecoded:'a+b' 
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   494
     self urlDecoded:'a%+b' 
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   495
     self urlDecoded:'a%' 
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   496
     self urlDecoded:'a%2' 
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   497
     self urlDecoded:'/Home/a%C3%A4%C3%B6%C3%BCa'
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   498
    "
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   499
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   500
    "Created: / 26-08-2018 / 12:49:24 / Claus Gittinger"
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   501
!
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   502
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   503
urlEncode2:aStringOrStream on:ws
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   504
    <resource: #obsolete>
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   505
    "helper to escape invalid/dangerous characters in an urls arguments.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   506
     Similar to urlEncode, but treats '*','~' and spaces differently.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   507
     (some clients, such as bitTorrent seem to require this - time will tell...)
2523
cae6bc936653 changed: #urlEncode2:on:
Claus Gittinger <cg@exept.de>
parents: 2522
diff changeset
   508
     Any byte not in the set 0-9, a-z, A-Z, '.', '-', '_', is encoded using 
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   509
     the '%nn' format, where nn is the hexadecimal value of the byte.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   510
        see: RFC1738"
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   511
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   512
    |rs c space|
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   513
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   514
    space := Character space.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   515
    rs := aStringOrStream readStream.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   516
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   517
    [rs atEnd] whileFalse: [
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   518
        c := rs next.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   519
2523
cae6bc936653 changed: #urlEncode2:on:
Claus Gittinger <cg@exept.de>
parents: 2522
diff changeset
   520
        (c isLetterOrDigit or:[ ('-_.' includes:c) ]) ifTrue:[
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   521
            ws nextPut:c.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   522
        ] ifFalse:[
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   523
            ws nextPut: $%.
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   524
            c codePoint > 16rFF ifTrue:[
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   525
                ws nextPut: $u.
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   526
                c codePoint printOn:ws base:16 size:4 fill:$0.
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   527
            ] ifFalse:[
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   528
                c codePoint printOn:ws base:16 size:2 fill:$0.
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   529
            ]
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   530
        ].
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   531
    ].
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   532
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   533
    "Created: / 09-01-2011 / 10:32:27 / cg"
2523
cae6bc936653 changed: #urlEncode2:on:
Claus Gittinger <cg@exept.de>
parents: 2522
diff changeset
   534
    "Modified: / 09-01-2011 / 13:11:17 / cg"
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   535
    "Modified: / 06-05-2015 / 15:43:39 / sr"
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   536
!
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   537
2500
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   538
urlEncode:aStringOrStream on:ws
4712
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   539
    "helper to escape invalid/dangerous characters in an urlÄs argument or post-fields.
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   540
4712
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   541
     Any byte not in the set 0-9, a-z, A-Z, '.', '-', '_' and '~', 
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   542
     is encoded using the '%nn' format, where nn is the hexadecimal value of the byte.
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   543
     Characters outside the ASCII range are encoded into utf8 first.
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   544
     Spaces are encoded as '+'.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   545
        see: application/x-www-form-urlencoded  
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   546
        see: https://tools.ietf.org/html/rfc3986 (obsoletes RFC1738)"
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   547
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   548
    |rs c|
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   549
2500
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   550
    rs := aStringOrStream readStream.
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   551
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   552
    [(c := rs nextOrNil) notNil] whileTrue: [
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   553
        |cp|
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   554
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   555
        (c isLetterOrDigit or:['-_.~' includes:c]) ifTrue:[
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   556
            ws nextPut:c.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   557
        ] ifFalse:[
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   558
            c == Character space ifTrue:[
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   559
                ws nextPut:$+.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   560
            ] ifFalse:[
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   561
                cp := c codePoint.
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   562
                cp > 16r7F ifTrue:[
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   563
                    c utf8Encoded do:[:eachUtf8Char|
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   564
                        ws nextPut: $%.
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   565
                        eachUtf8Char codePoint printOn:ws base:16 size:2 fill:$0.
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   566
                    ].
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   567
                ] ifFalse:[
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   568
                    ws nextPut: $%.
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   569
                    cp printOn:ws base:16 size:2 fill:$0.
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   570
                ].
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   571
            ].
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   572
        ].
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   573
    ].
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   574
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   575
    "
4712
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   576
     self urlEncoded:'hokus pokus fidibus*-/~'
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   577
     self urlEncoded:'Ützel Brötzel*-/~'
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   578
     self urlEncoded:'χαιρε'
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   579
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   580
     self urlDecoded:(self urlEncoded:'hokus pokus fidibus*-/~')
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   581
     self urlDecoded:(self urlEncoded:'Ützel Brötzel*-/~')
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   582
     self urlDecoded:(self urlEncoded:'χαιρε')
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   583
    "
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   584
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   585
    "Modified: / 09-01-2011 / 10:43:30 / cg"
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   586
    "Modified: / 06-05-2015 / 16:06:52 / sr"
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   587
    "Modified (comment): / 07-02-2017 / 14:51:42 / stefan"
4712
530912590b7f #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4517
diff changeset
   588
    "Modified (comment): / 26-08-2018 / 12:50:04 / Claus Gittinger"
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   589
!
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   590
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   591
urlEncoded2: aString
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   592
    <resource: #obsolete>
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   593
    "helper to escape invalid/dangerous characters in an urls arguments or post-fields.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   594
     Similar to urlEncoded, but treats '*','~' and spaces differently.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   595
     (some clients, such as bitTorrent seem to require this - time will tell...)
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   596
     Any byte not in the set 0-9, a-z, A-Z, '.', '-', '_' and '~', is encoded using 
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   597
     the '%nn' format, where nn is the hexadecimal value of the byte.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   598
        see: application/x-www-form-urlencoded  
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   599
        see: RFC1738"
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   600
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   601
    |ws|
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   602
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   603
    ws := String writeStreamWithInitialSize:aString size.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   604
    self urlEncode2:aString on:ws.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   605
    ^ ws contents
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   606
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   607
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   608
    "
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   609
      self unEscape:(self urlEncoded:'_-.*Frankfurt(Main) Hbf')
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   610
      self urlEncoded2:'_-.*Frankfurt(Main) Hbf'
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   611
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   612
      self unEscape:(self urlEncoded:'-_.*%exept;')
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   613
      self urlEncoded2:'-_.*%exept;'  
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   614
      self urlEncoded:'-_.*%exept;'    
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   615
    "
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   616
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   617
    "Created: / 09-01-2011 / 10:34:50 / cg"
2500
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   618
!
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   619
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   620
urlEncoded: aString
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   621
    "helper to escape invalid/dangerous characters in an urls arguments or post-fields.
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   622
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   623
     Any byte not in the set 0-9, a-z, A-Z, '.', '-', '_' and '~', is encoded using 
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   624
     the '%nn' format, where nn is the hexadecimal value of the byte.
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   625
     Characters outside the ASCII range are encoded into utf8 first.
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   626
     Spaces are encoded as '+'.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   627
        see: application/x-www-form-urlencoded  
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   628
        see: https://tools.ietf.org/html/rfc3986 (obsoletes RFC1738)"
2500
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   629
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   630
    |ws|
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   631
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   632
    ws := WriteStream on:(String new:aString size + 20).
2500
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   633
    self urlEncode:aString on:ws.
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   634
    ^ ws contents
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   635
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   636
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   637
    "
2500
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   638
      self unEscape:(self urlEncoded:'_-.*Frankfurt(Main) Hbf')
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   639
      self urlEncoded:'_-.*Frankfurt(Main) Hbf'
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   640
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   641
      self unEscape:(self urlEncoded:'-_.*%exept;')
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   642
      self urlEncoded:'-_.*%exept;'
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   643
    "
2464
ebff59707514 Patch from CG for UBS
Stefan Vogel <sv@exept.de>
parents: 2458
diff changeset
   644
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   645
    "Modified: / 09-01-2011 / 10:43:37 / cg"
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   646
    "Modified: / 07-02-2017 / 14:54:12 / stefan"
2066
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   647
!
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   648
2436
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   649
withAllSpecialHTMLCharactersEscaped:aStringOrCharacter
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   650
    "replace ampersand, less, greater and quotes by html-character escapes"
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   651
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   652
    "/ TODO: this is similar to escapeCharacterEntities.
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   653
    "/ we should refactor this into one method only (can we do hex escapes always ?).
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   654
    "/ Notice, that these two methods came into existance due to historic reasons
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   655
    "/ and were developed independent of each other, but later moved to this common place.
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   656
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   657
    |resultStream|
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   658
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   659
"/    orgs  := #( $&      $<     $>     $"   $').
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   660
"/    repls := #( '&amp;' '&lt;' '&gt;' &quot; &apos;).
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   661
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   662
    (aStringOrCharacter isString
3098
2ae8f1b57bc1 class: HTMLUtilities
sr
parents: 2866
diff changeset
   663
    and:[ (aStringOrCharacter includesAny:'&<>''"') not ]) ifTrue:[^ aStringOrCharacter].
2436
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   664
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   665
    resultStream := CharacterWriteStream on:''.
2436
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   666
    aStringOrCharacter asString do:[:eachCharacter |
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   667
        "/ huh - a switch. Sorry, but this method is used heavily.
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   668
        eachCharacter == $&
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   669
            ifTrue:[ resultStream nextPutAll:'&amp;' ]
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   670
            ifFalse:[
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   671
        eachCharacter == $<
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   672
            ifTrue:[ resultStream nextPutAll:'&lt;' ]
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   673
            ifFalse:[
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   674
        eachCharacter == $>
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   675
            ifTrue:[ resultStream nextPutAll:'&gt;' ]
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   676
            ifFalse:[
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   677
        eachCharacter == $"
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   678
            ifTrue:[ resultStream nextPutAll:'&quot;' ]
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   679
            ifFalse:[
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   680
        eachCharacter == $'
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   681
            ifTrue:[ resultStream nextPutAll:'&apos;' ]
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   682
            ifFalse:[
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   683
                resultStream nextPut:eachCharacter
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   684
            ]]]]].
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   685
    ].
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   686
    ^ resultStream contents
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   687
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   688
    "
3312
fe3d83508353 class: HTMLUtilities
sr
parents: 3098
diff changeset
   689
     self withAllSpecialHTMLCharactersEscaped:'<>#&'     
fe3d83508353 class: HTMLUtilities
sr
parents: 3098
diff changeset
   690
     self withAllSpecialHTMLCharactersEscaped:$<
fe3d83508353 class: HTMLUtilities
sr
parents: 3098
diff changeset
   691
     self withAllSpecialHTMLCharactersEscaped:$#
2436
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   692
    "
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   693
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   694
    "Modified: / 05-12-2006 / 13:48:59 / cg"
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   695
    "Modified: / 06-05-2015 / 15:41:06 / sr"
2436
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   696
!
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   697
2066
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   698
withSpecialHTMLCharactersEscaped:aStringOrCharacter
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   699
    "replace ampersand, less and greater by html-character escapes"
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   700
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   701
    "/ TODO: this is similar to escapeCharacterEntities.
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   702
    "/ we should refactor this into one method only (can we do hex escapes always ?).
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   703
    "/ Notice, that these two methods came into existance due to historic reasons
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   704
    "/ and were developed independent of each other, but later moved to this common place.
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   705
2866
259f841e2554 class: HTMLUtilities
Stefan Vogel <sv@exept.de>
parents: 2554
diff changeset
   706
    |resultStream|
2066
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   707
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   708
"/    orgs  := #( $&      $<     $>     ).
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   709
"/    repls := #( '&amp;' '&lt;' '&gt;' ).
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   710
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   711
    (aStringOrCharacter isString
2866
259f841e2554 class: HTMLUtilities
Stefan Vogel <sv@exept.de>
parents: 2554
diff changeset
   712
     and:[ (aStringOrCharacter isWideString not)
259f841e2554 class: HTMLUtilities
Stefan Vogel <sv@exept.de>
parents: 2554
diff changeset
   713
     and:[ (aStringOrCharacter includesAny:'&<>') not ]]) ifTrue:[^ aStringOrCharacter].
2066
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   714
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   715
    resultStream := CharacterWriteStream on:''.
2066
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   716
    aStringOrCharacter asString do:[:eachCharacter |
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   717
        "/ huh - a switch. Sorry, but this method is used heavily.
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   718
        eachCharacter == $&
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   719
            ifTrue:[ resultStream nextPutAll:'&amp;' ]
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   720
            ifFalse:[
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   721
        eachCharacter == $<
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   722
            ifTrue:[ resultStream nextPutAll:'&lt;' ]
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   723
            ifFalse:[
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   724
        eachCharacter == $>
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   725
            ifTrue:[ resultStream nextPutAll:'&gt;' ]
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   726
            ifFalse:[
2554
7cd0f7a16fad changed: #withSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2523
diff changeset
   727
"/        eachCharacter codePoint > 16r7F
7cd0f7a16fad changed: #withSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2523
diff changeset
   728
"/            ifTrue:[ 
7cd0f7a16fad changed: #withSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2523
diff changeset
   729
"/                resultStream
7cd0f7a16fad changed: #withSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2523
diff changeset
   730
"/                    nextPutAll:'&#';
7cd0f7a16fad changed: #withSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2523
diff changeset
   731
"/                    nextPutAll:(eachCharacter codePoint printString);
7cd0f7a16fad changed: #withSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2523
diff changeset
   732
"/                    nextPutAll:';']
7cd0f7a16fad changed: #withSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2523
diff changeset
   733
"/            ifFalse:[
2066
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   734
                resultStream nextPut:eachCharacter
2554
7cd0f7a16fad changed: #withSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2523
diff changeset
   735
"/            ]
2066
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   736
            ]]].
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   737
    ].
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   738
    ^ resultStream contents
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   739
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   740
    "
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   741
     self withSpecialHTMLCharactersEscaped:'<>#&'
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   742
     self withSpecialHTMLCharactersEscaped:$<
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   743
     self withSpecialHTMLCharactersEscaped:$#
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   744
    "
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   745
2554
7cd0f7a16fad changed: #withSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2523
diff changeset
   746
    "Modified: / 13-04-2011 / 23:13:32 / cg"
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   747
    "Modified: / 06-05-2015 / 15:41:16 / sr"
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   748
! !
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   749
3647
738252558e04 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3640
diff changeset
   750
!HTMLUtilities class methodsFor:'queries'!
738252558e04 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3640
diff changeset
   751
738252558e04 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3640
diff changeset
   752
isUtilityClass
738252558e04 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3640
diff changeset
   753
    ^ self == HTMLUtilities
738252558e04 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3640
diff changeset
   754
! !
738252558e04 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3640
diff changeset
   755
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   756
!HTMLUtilities class methodsFor:'serving-helpers'!
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   757
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   758
escape:aString
2436
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   759
    "helper to escape invalid/dangerous characters in an url's arguments or post-fields.
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   760
     These are:
3456
8a3302fd3cce class: HTMLUtilities
Claus Gittinger <cg@exept.de>
parents: 3312
diff changeset
   761
        control characters, dQuote, '+', ';', '?', '&' and space -> %XX ascii as hex digits
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   762
        %     -> %%
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   763
    "
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   764
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   765
    | rs ws c cp|
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   766
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   767
    rs := ReadStream on: aString.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   768
    ws := WriteStream on: ''.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   769
    [ rs atEnd ] whileFalse: [
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   770
        c := rs next.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   771
        c == $% ifTrue:[
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   772
            ws nextPutAll: '%%'.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   773
        ] ifFalse:[
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   774
            (((cp := c codePoint) < 16r7F)
3456
8a3302fd3cce class: HTMLUtilities
Claus Gittinger <cg@exept.de>
parents: 3312
diff changeset
   775
             and:[ ('+;?&" ' includes:c) not ]) ifTrue: [ 
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   776
                ws nextPut: c.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   777
            ] ifFalse:[
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   778
                ws nextPut: $%.
4217
1dac9014b77a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4204
diff changeset
   779
                cp printOn:ws base:16 size:(cp > 16rFF ifTrue:[4] ifFalse:[2]) fill:$0.
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   780
            ]
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   781
        ]
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   782
    ].
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   783
    ^ ws contents
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   784
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   785
    "
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   786
     self escape:'a b'      
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   787
     self escape:'a%b'    
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   788
     self escape:'a b'      
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   789
     self escape:'a+b'      
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   790
     self escape:'aäüöb'      
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   791
    "
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   792
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   793
    "Modified: / 06-05-2015 / 16:07:18 / sr"
4217
1dac9014b77a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4204
diff changeset
   794
    "Modified: / 25-11-2016 / 16:37:53 / cg"
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   795
! !
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   796
2144
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   797
!HTMLUtilities class methodsFor:'text processing helpers'!
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   798
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   799
plainTextOfHTML:htmlString
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   800
    "given some HTML, extract the raw text. 
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   801
     Can be used to search for strings in some html text."
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   802
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   803
    |parser doc s first|
2144
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   804
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   805
    parser := HTMLParser new.
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   806
    doc := parser parseText:htmlString.
3660
628279cf644c #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3659
diff changeset
   807
    s := CharacterWriteStream on:(String new:100).
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   808
    first := true.
2144
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   809
    doc markUpElementsDo:[:el |
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   810
        |t|
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   811
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   812
        el isTextElement ifTrue:[
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   813
            t := el text withoutSeparators.
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   814
            t notEmpty ifTrue:[
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   815
                first ifFalse:[    
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   816
                    s space.
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   817
                ].
2144
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   818
                s nextPutAll:t.
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   819
                first := false    
2144
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   820
            ].
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   821
        ] ifFalse:[
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   822
            "/ ignore non-text; however, we could care for text in info-titles
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   823
            "/ or scripts as well...
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   824
        ].
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   825
    ].
3659
a226a9108bce #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3647
diff changeset
   826
    ^ s contents
2144
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   827
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   828
    "
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   829
     self plainTextOfHTML:'
4737
610d483cb00a #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4712
diff changeset
   830
            bla1 bla2 <br>bla3 <table><tr><td>bla4</td></tr></table> bla5<p>bla6'
610d483cb00a #DOCUMENTATION by stefan
Stefan Vogel <sv@exept.de>
parents: 4712
diff changeset
   831
     self plainTextOfHTML:'Hello World'        
2144
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   832
    "
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   833
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   834
    "Modified: / 06-05-2015 / 17:02:36 / sr"
2144
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   835
! !
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   836
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   837
!HTMLUtilities class methodsFor:'documentation'!
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   838
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   839
version
3640
098175b79b25 #BUGFIX
sr
parents: 3557
diff changeset
   840
    ^ '$Header$'
2434
5625df4b6119 comment/format in: #escapeCharacterEntities:
Claus Gittinger <cg@exept.de>
parents: 2179
diff changeset
   841
!
5625df4b6119 comment/format in: #escapeCharacterEntities:
Claus Gittinger <cg@exept.de>
parents: 2179
diff changeset
   842
5625df4b6119 comment/format in: #escapeCharacterEntities:
Claus Gittinger <cg@exept.de>
parents: 2179
diff changeset
   843
version_CVS
3640
098175b79b25 #BUGFIX
sr
parents: 3557
diff changeset
   844
    ^ '$Header$'
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   845
! !
3098
2ae8f1b57bc1 class: HTMLUtilities
sr
parents: 2866
diff changeset
   846