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