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