HTMLUtilities.st
author Claus Gittinger <cg@exept.de>
Wed, 15 Feb 2017 22:15:41 +0100
changeset 4331 6b08efe6d794
parent 4302 f50a1263f3ce
child 4333 2e428045cb82
permissions -rw-r--r--
#OTHER by cg added: #copyright
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:[
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   168
                aWriteStream
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   169
                    nextPutAll:'&#';
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   170
                    nextPutAll:(c codePoint printString);
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   171
                    nextPutAll:';'.
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   172
            ] ifFalse:[
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   173
                aWriteStream nextPut:c.
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
    "
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   179
     self escapeCharacterEntities:'a<b'     
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   180
     self escapeCharacterEntities:'aöb'     
4296
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
0da79cbe040b #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4287
diff changeset
   183
    "Created: / 05-02-2017 / 17:58:34 / cg"
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.
21e099fb879e class: HTMLUtilities
sr
parents: 3545
diff changeset
   395
                        ws nextPutAll:$;.
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"
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   424
!
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   425
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   426
urlEncode2:aStringOrStream on:ws
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   427
    <resource: #obsolete>
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   428
    "helper to escape invalid/dangerous characters in an urls arguments.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   429
     Similar to urlEncode, but treats '*','~' and spaces differently.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   430
     (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
   431
     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
   432
     the '%nn' format, where nn is the hexadecimal value of the byte.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   433
        see: RFC1738"
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   434
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   435
    |rs c space|
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   436
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   437
    space := Character space.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   438
    rs := aStringOrStream readStream.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   439
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   440
    [rs atEnd] whileFalse: [
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   441
        c := rs next.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   442
2523
cae6bc936653 changed: #urlEncode2:on:
Claus Gittinger <cg@exept.de>
parents: 2522
diff changeset
   443
        (c isLetterOrDigit or:[ ('-_.' includes:c) ]) ifTrue:[
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   444
            ws nextPut:c.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   445
        ] ifFalse:[
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   446
            ws nextPut: $%.
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   447
            c codePoint > 16rFF ifTrue:[
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   448
                ws nextPut: $u.
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   449
                c codePoint printOn:ws base:16 size:4 fill:$0.
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   450
            ] ifFalse:[
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   451
                c codePoint printOn:ws base:16 size:2 fill:$0.
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   452
            ]
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   453
        ].
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
    "Created: / 09-01-2011 / 10:32:27 / cg"
2523
cae6bc936653 changed: #urlEncode2:on:
Claus Gittinger <cg@exept.de>
parents: 2522
diff changeset
   457
    "Modified: / 09-01-2011 / 13:11:17 / cg"
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   458
    "Modified: / 06-05-2015 / 15:43:39 / sr"
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   459
!
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   460
2500
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   461
urlEncode:aStringOrStream on:ws
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   462
    "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
   463
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   464
     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
   465
     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
   466
     Characters outside the ASCII range are encoded into utf8 first.
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   467
     Spaces are encoded as '+'.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   468
        see: application/x-www-form-urlencoded  
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   469
        see: https://tools.ietf.org/html/rfc3986 (obsoletes RFC1738)"
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   470
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   471
    |rs c|
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   472
2500
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   473
    rs := aStringOrStream readStream.
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   474
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   475
    [(c := rs nextOrNil) notNil] whileTrue: [
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   476
        |cp|
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   477
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   478
        (c isLetterOrDigit or:['-_.~' includes:c]) ifTrue:[
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   479
            ws nextPut:c.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   480
        ] ifFalse:[
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   481
            c == Character space ifTrue:[
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   482
                ws nextPut:$+.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   483
            ] ifFalse:[
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   484
                cp := c codePoint.
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   485
                cp > 16r7F ifTrue:[
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   486
                    c utf8Encoded do:[:eachUtf8Char|
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   487
                        ws nextPut: $%.
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   488
                        eachUtf8Char codePoint printOn:ws base:16 size:2 fill:$0.
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   489
                    ].
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   490
                ] ifFalse:[
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   491
                    ws nextPut: $%.
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   492
                    cp printOn:ws base:16 size:2 fill:$0.
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   493
                ].
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   494
            ].
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   495
        ].
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   496
    ].
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   497
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   498
    "
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   499
        self urlEncoded:'hokus pokus fidibus*-/~'
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   500
        self urlEncoded:'Ützel Brötzel*-/~'
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   501
        self urlEncoded:'χαιρε'
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   502
    "
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   503
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   504
    "Modified: / 09-01-2011 / 10:43:30 / cg"
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   505
    "Modified: / 06-05-2015 / 16:06:52 / sr"
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   506
    "Modified (comment): / 07-02-2017 / 14:51:42 / stefan"
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   507
!
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   508
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   509
urlEncoded2: aString
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   510
    <resource: #obsolete>
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   511
    "helper to escape invalid/dangerous characters in an urls arguments or post-fields.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   512
     Similar to urlEncoded, but treats '*','~' and spaces differently.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   513
     (some clients, such as bitTorrent seem to require this - time will tell...)
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   514
     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
   515
     the '%nn' format, where nn is the hexadecimal value of the byte.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   516
        see: application/x-www-form-urlencoded  
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   517
        see: RFC1738"
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   518
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   519
    |ws|
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   520
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   521
    ws := String writeStreamWithInitialSize:aString size.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   522
    self urlEncode2:aString on:ws.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   523
    ^ ws contents
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   524
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
      self unEscape:(self urlEncoded:'_-.*Frankfurt(Main) Hbf')
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   528
      self urlEncoded2:'_-.*Frankfurt(Main) Hbf'
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   529
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   530
      self unEscape:(self urlEncoded:'-_.*%exept;')
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   531
      self urlEncoded2:'-_.*%exept;'  
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   532
      self urlEncoded:'-_.*%exept;'    
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   533
    "
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   534
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   535
    "Created: / 09-01-2011 / 10:34:50 / cg"
2500
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   536
!
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   537
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   538
urlEncoded: aString
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   539
    "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
   540
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   541
     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
   542
     the '%nn' format, where nn is the hexadecimal value of the byte.
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   543
     Characters outside the ASCII range are encoded into utf8 first.
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   544
     Spaces are encoded as '+'.
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   545
        see: application/x-www-form-urlencoded  
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   546
        see: https://tools.ietf.org/html/rfc3986 (obsoletes RFC1738)"
2500
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   547
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   548
    |ws|
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   549
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   550
    ws := WriteStream on:(String new:aString size + 20).
2500
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   551
    self urlEncode:aString on:ws.
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   552
    ^ ws contents
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   553
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   554
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   555
    "
2500
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   556
      self unEscape:(self urlEncoded:'_-.*Frankfurt(Main) Hbf')
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   557
      self urlEncoded:'_-.*Frankfurt(Main) Hbf'
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   558
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   559
      self unEscape:(self urlEncoded:'-_.*%exept;')
Stefan Vogel <sv@exept.de>
parents: 2464
diff changeset
   560
      self urlEncoded:'-_.*%exept;'
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   561
    "
2464
ebff59707514 Patch from CG for UBS
Stefan Vogel <sv@exept.de>
parents: 2458
diff changeset
   562
2522
Claus Gittinger <cg@exept.de>
parents: 2500
diff changeset
   563
    "Modified: / 09-01-2011 / 10:43:37 / cg"
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   564
    "Modified: / 07-02-2017 / 14:54:12 / stefan"
2066
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   565
!
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   566
2436
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   567
withAllSpecialHTMLCharactersEscaped:aStringOrCharacter
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   568
    "replace ampersand, less, greater and quotes by html-character escapes"
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   569
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   570
    "/ TODO: this is similar to escapeCharacterEntities.
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   571
    "/ 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
   572
    "/ Notice, that these two methods came into existance due to historic reasons
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   573
    "/ 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
   574
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   575
    |resultStream|
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   576
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   577
"/    orgs  := #( $&      $<     $>     $"   $').
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   578
"/    repls := #( '&amp;' '&lt;' '&gt;' &quot; &apos;).
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   579
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   580
    (aStringOrCharacter isString
3098
2ae8f1b57bc1 class: HTMLUtilities
sr
parents: 2866
diff changeset
   581
    and:[ (aStringOrCharacter includesAny:'&<>''"') not ]) ifTrue:[^ aStringOrCharacter].
2436
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   582
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   583
    resultStream := CharacterWriteStream on:''.
2436
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   584
    aStringOrCharacter asString do:[:eachCharacter |
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   585
        "/ huh - a switch. Sorry, but this method is used heavily.
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   586
        eachCharacter == $&
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   587
            ifTrue:[ resultStream nextPutAll:'&amp;' ]
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   588
            ifFalse:[
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   589
        eachCharacter == $<
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   590
            ifTrue:[ resultStream nextPutAll:'&lt;' ]
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   591
            ifFalse:[
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   592
        eachCharacter == $>
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   593
            ifTrue:[ resultStream nextPutAll:'&gt;' ]
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   594
            ifFalse:[
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   595
        eachCharacter == $"
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   596
            ifTrue:[ resultStream nextPutAll:'&quot;' ]
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   597
            ifFalse:[
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   598
        eachCharacter == $'
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   599
            ifTrue:[ resultStream nextPutAll:'&apos;' ]
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   600
            ifFalse:[
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   601
                resultStream nextPut:eachCharacter
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   602
            ]]]]].
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
    ^ resultStream contents
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   605
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   606
    "
3312
fe3d83508353 class: HTMLUtilities
sr
parents: 3098
diff changeset
   607
     self withAllSpecialHTMLCharactersEscaped:'<>#&'     
fe3d83508353 class: HTMLUtilities
sr
parents: 3098
diff changeset
   608
     self withAllSpecialHTMLCharactersEscaped:$<
fe3d83508353 class: HTMLUtilities
sr
parents: 3098
diff changeset
   609
     self withAllSpecialHTMLCharactersEscaped:$#
2436
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   610
    "
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
    "Modified: / 05-12-2006 / 13:48:59 / cg"
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   613
    "Modified: / 06-05-2015 / 15:41:06 / sr"
2436
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   614
!
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   615
2066
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   616
withSpecialHTMLCharactersEscaped:aStringOrCharacter
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   617
    "replace ampersand, less and greater by html-character escapes"
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   618
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   619
    "/ TODO: this is similar to escapeCharacterEntities.
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   620
    "/ 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
   621
    "/ 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
   622
    "/ 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
   623
2866
259f841e2554 class: HTMLUtilities
Stefan Vogel <sv@exept.de>
parents: 2554
diff changeset
   624
    |resultStream|
2066
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   625
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   626
"/    orgs  := #( $&      $<     $>     ).
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   627
"/    repls := #( '&amp;' '&lt;' '&gt;' ).
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   628
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   629
    (aStringOrCharacter isString
2866
259f841e2554 class: HTMLUtilities
Stefan Vogel <sv@exept.de>
parents: 2554
diff changeset
   630
     and:[ (aStringOrCharacter isWideString not)
259f841e2554 class: HTMLUtilities
Stefan Vogel <sv@exept.de>
parents: 2554
diff changeset
   631
     and:[ (aStringOrCharacter includesAny:'&<>') not ]]) ifTrue:[^ aStringOrCharacter].
2066
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   632
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   633
    resultStream := CharacterWriteStream on:''.
2066
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   634
    aStringOrCharacter asString do:[:eachCharacter |
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   635
        "/ huh - a switch. Sorry, but this method is used heavily.
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   636
        eachCharacter == $&
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   637
            ifTrue:[ resultStream nextPutAll:'&amp;' ]
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   638
            ifFalse:[
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   639
        eachCharacter == $<
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   640
            ifTrue:[ resultStream nextPutAll:'&lt;' ]
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   641
            ifFalse:[
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   642
        eachCharacter == $>
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   643
            ifTrue:[ resultStream nextPutAll:'&gt;' ]
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   644
            ifFalse:[
2554
7cd0f7a16fad changed: #withSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2523
diff changeset
   645
"/        eachCharacter codePoint > 16r7F
7cd0f7a16fad changed: #withSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2523
diff changeset
   646
"/            ifTrue:[ 
7cd0f7a16fad changed: #withSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2523
diff changeset
   647
"/                resultStream
7cd0f7a16fad changed: #withSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2523
diff changeset
   648
"/                    nextPutAll:'&#';
7cd0f7a16fad changed: #withSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2523
diff changeset
   649
"/                    nextPutAll:(eachCharacter codePoint printString);
7cd0f7a16fad changed: #withSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2523
diff changeset
   650
"/                    nextPutAll:';']
7cd0f7a16fad changed: #withSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2523
diff changeset
   651
"/            ifFalse:[
2066
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   652
                resultStream nextPut:eachCharacter
2554
7cd0f7a16fad changed: #withSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2523
diff changeset
   653
"/            ]
2066
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   654
            ]]].
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
    ^ resultStream contents
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   657
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
     self withSpecialHTMLCharactersEscaped:'<>#&'
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
    "
0ee2ef2d018c more common code
Claus Gittinger <cg@exept.de>
parents: 2058
diff changeset
   663
2554
7cd0f7a16fad changed: #withSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2523
diff changeset
   664
    "Modified: / 13-04-2011 / 23:13:32 / cg"
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   665
    "Modified: / 06-05-2015 / 15:41:16 / sr"
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   666
! !
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   667
3647
738252558e04 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3640
diff changeset
   668
!HTMLUtilities class methodsFor:'queries'!
738252558e04 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3640
diff changeset
   669
738252558e04 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3640
diff changeset
   670
isUtilityClass
738252558e04 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3640
diff changeset
   671
    ^ self == HTMLUtilities
738252558e04 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3640
diff changeset
   672
! !
738252558e04 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3640
diff changeset
   673
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   674
!HTMLUtilities class methodsFor:'serving-helpers'!
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   675
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   676
escape:aString
2436
a5537ae7be4a added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents: 2434
diff changeset
   677
    "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
   678
     These are:
3456
8a3302fd3cce class: HTMLUtilities
Claus Gittinger <cg@exept.de>
parents: 3312
diff changeset
   679
        control characters, dQuote, '+', ';', '?', '&' and space -> %XX ascii as hex digits
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   680
        %     -> %%
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   681
    "
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   682
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   683
    | rs ws c cp|
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   684
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   685
    rs := ReadStream on: aString.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   686
    ws := WriteStream on: ''.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   687
    [ rs atEnd ] whileFalse: [
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   688
        c := rs next.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   689
        c == $% ifTrue:[
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   690
            ws nextPutAll: '%%'.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   691
        ] ifFalse:[
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   692
            (((cp := c codePoint) < 16r7F)
3456
8a3302fd3cce class: HTMLUtilities
Claus Gittinger <cg@exept.de>
parents: 3312
diff changeset
   693
             and:[ ('+;?&" ' includes:c) not ]) ifTrue: [ 
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   694
                ws nextPut: c.
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   695
            ] ifFalse:[
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   696
                ws nextPut: $%.
4217
1dac9014b77a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4204
diff changeset
   697
                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
   698
            ]
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
    ^ ws contents
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   702
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   703
    "
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   704
     self escape:'a b'      
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'      
4302
f50a1263f3ce #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4297
diff changeset
   708
     self escape:'aäüöb'      
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   709
    "
3544
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   710
73c88216a4f2 class: HTMLUtilities
sr
parents: 3456
diff changeset
   711
    "Modified: / 06-05-2015 / 16:07:18 / sr"
4217
1dac9014b77a #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4204
diff changeset
   712
    "Modified: / 25-11-2016 / 16:37:53 / cg"
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   713
! !
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   714
2144
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   715
!HTMLUtilities class methodsFor:'text processing helpers'!
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   716
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   717
plainTextOfHTML:htmlString
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   718
    "given some HTML, extract the raw text. 
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   719
     Can be used to search for strings in some html text."
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   720
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   721
    |parser doc s first|
2144
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   722
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
    parser := HTMLParser new.
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   725
    doc := parser parseText:htmlString.
3660
628279cf644c #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3659
diff changeset
   726
    s := CharacterWriteStream on:(String new:100).
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   727
    first := true.
2144
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   728
    doc markUpElementsDo:[:el |
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   729
        |t|
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   730
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   731
        el isTextElement ifTrue:[
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   732
            t := el text withoutSeparators.
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   733
            t notEmpty ifTrue:[
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   734
                first ifFalse:[    
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   735
                    s space.
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   736
                ].
2144
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   737
                s nextPutAll:t.
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   738
                first := false    
2144
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   739
            ].
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   740
        ] ifFalse:[
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   741
            "/ 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
   742
            "/ or scripts as well...
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   743
        ].
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   744
    ].
3659
a226a9108bce #REFACTORING
Stefan Vogel <sv@exept.de>
parents: 3647
diff changeset
   745
    ^ s contents
2144
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   746
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
     self plainTextOfHTML:'
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   749
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
   750
'        
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   751
    "
3545
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   752
070008476ef8 class: HTMLUtilities
sr
parents: 3544
diff changeset
   753
    "Modified: / 06-05-2015 / 17:02:36 / sr"
2144
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   754
! !
c89258333f4d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2087
diff changeset
   755
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   756
!HTMLUtilities class methodsFor:'documentation'!
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   757
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   758
version
3640
098175b79b25 #BUGFIX
sr
parents: 3557
diff changeset
   759
    ^ '$Header$'
2434
5625df4b6119 comment/format in: #escapeCharacterEntities:
Claus Gittinger <cg@exept.de>
parents: 2179
diff changeset
   760
!
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
version_CVS
3640
098175b79b25 #BUGFIX
sr
parents: 3557
diff changeset
   763
    ^ '$Header$'
2058
f407ff58f780 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   764
! !
3098
2ae8f1b57bc1 class: HTMLUtilities
sr
parents: 2866
diff changeset
   765