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