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