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