author | sr |
Wed, 08 Jul 2009 12:40:24 +0200 | |
changeset 2179 | c1cee8bbc1e5 |
parent 2144 | c89258333f4d |
child 2434 | 5625df4b6119 |
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 |
||
14 |
Object subclass:#HTMLUtilities |
|
15 |
instanceVariableNames:'' |
|
16 |
classVariableNames:'EscapeControlCharacters' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'Net-Communication-Support' |
|
19 |
! |
|
20 |
||
21 |
!HTMLUtilities class methodsFor:'documentation'! |
|
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 2007 by eXept Software AG |
|
26 |
All Rights Reserved |
|
27 |
||
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
37 |
documentation |
|
38 |
" |
|
39 |
Collected support functions to deal with HTML. |
|
40 |
Used both by HTML generators (DocGenerator), HTMLParsers and the webServer. |
|
41 |
Therefore, it has been put into libbasic2. |
|
42 |
" |
|
43 |
! ! |
|
44 |
||
45 |
!HTMLUtilities class methodsFor:'helpers'! |
|
46 |
||
47 |
controlCharacters |
|
48 |
||
49 |
EscapeControlCharacters isNil ifTrue:[ |
|
50 |
EscapeControlCharacters := Dictionary new. |
|
51 |
"/ EscapeControlCharacters at:Character space put:' '. |
|
52 |
EscapeControlCharacters at:$< put:'<'. |
|
53 |
EscapeControlCharacters at:$> put:'>'. |
|
54 |
EscapeControlCharacters at:$& put:'&'. |
|
55 |
EscapeControlCharacters at:$" put:'"'. |
|
56 |
]. |
|
57 |
^ EscapeControlCharacters. |
|
58 |
! |
|
59 |
||
60 |
escapeCharacterEntities:aString |
|
61 |
"helper to escape invalid/dangerous characters in html strings. |
|
62 |
These are: |
|
63 |
control characters, '<', '>', '&' and space -> %XX ascii as hex digits |
|
64 |
% -> %% |
|
65 |
" |
|
2066 | 66 |
"/ TODO: this is similar to withSpecialHTMLCharactersEscaped. |
67 |
"/ we should refactor this into one method only (can we do hex escapes always ?). |
|
68 |
"/ Notice, that these two methods came into existance due to historic reasons |
|
69 |
"/ and were developed independent of each other, but later moved to this common place. |
|
70 |
||
71 |
||
2058 | 72 |
|rs ws c controlCharacters controlString| |
73 |
||
74 |
controlCharacters := self controlCharacters. |
|
75 |
rs := ReadStream on: aString. |
|
76 |
ws := WriteStream on: ''. |
|
77 |
[ rs atEnd ] whileFalse: [ |
|
78 |
c := rs next. |
|
79 |
controlString := controlCharacters at:c ifAbsent:nil. |
|
80 |
controlString notNil ifTrue:[ |
|
81 |
ws nextPutAll:controlString. |
|
82 |
] ifFalse:[ |
|
83 |
c codePoint > 16r7F ifTrue:[ |
|
84 |
ws |
|
85 |
nextPutAll:'&#'; |
|
86 |
nextPutAll:(c codePoint printString); |
|
87 |
nextPutAll:';'. |
|
88 |
] ifFalse:[ |
|
89 |
ws nextPut:c. |
|
90 |
] |
|
91 |
] |
|
92 |
]. |
|
93 |
^ ws contents |
|
94 |
||
95 |
||
96 |
" |
|
97 |
self escapeCharacterEntities:'a<b' |
|
98 |
self escapeCharacterEntities:'aöb' |
|
99 |
" |
|
100 |
! |
|
101 |
||
102 |
extractCharSetEncodingFromContentType:contentTypeLine |
|
103 |
|idx rest encoding| |
|
104 |
||
105 |
idx := contentTypeLine findString:'charset='. |
|
106 |
idx == 0 ifTrue:[ |
|
107 |
^ nil |
|
108 |
]. |
|
109 |
rest := (contentTypeLine copyFrom:idx+'charset=' size) withoutSeparators. |
|
110 |
idx := (rest indexOfSeparator) min:(rest indexOf:$;). |
|
111 |
idx == 0 ifTrue:[ |
|
112 |
encoding := rest |
|
113 |
] ifFalse:[ |
|
114 |
encoding := rest copyTo:idx-1. |
|
115 |
]. |
|
116 |
(encoding startsWith:$") ifTrue:[ |
|
117 |
encoding := encoding copyFrom:2 to:(encoding indexOf:$" startingAt:3)-1. |
|
118 |
]. |
|
119 |
^ encoding. |
|
120 |
||
121 |
" |
|
122 |
self extractCharSetEncodingFromContentType:'text/html; charset=ascii' |
|
123 |
self extractCharSetEncodingFromContentType:'text/html; charset=' |
|
124 |
self extractCharSetEncodingFromContentType:'text/html; fooBar=bla' |
|
125 |
self extractCharSetEncodingFromContentType:'text/xml; charset=utf-8' |
|
126 |
self extractCharSetEncodingFromContentType:'text/xml; charset=utf-8; bla=fasel' |
|
127 |
" |
|
128 |
! |
|
129 |
||
130 |
extractMimeTypeFromContentType:contentTypeLine |
|
131 |
|idx mimeAndEncoding| |
|
132 |
||
133 |
idx := contentTypeLine indexOf:$:. |
|
134 |
mimeAndEncoding := (contentTypeLine copyFrom:idx+1) withoutSeparators. |
|
135 |
||
136 |
(mimeAndEncoding includes:$;) ifFalse:[ |
|
137 |
^ mimeAndEncoding |
|
138 |
]. |
|
139 |
||
140 |
idx := mimeAndEncoding indexOf:$;. |
|
141 |
^ mimeAndEncoding copyTo:idx-1 |
|
142 |
||
143 |
" |
|
144 |
self extractMimeTypeFromContentType:'text/html; charset=ascii' |
|
145 |
self extractMimeTypeFromContentType:'text/html; ' |
|
146 |
self extractMimeTypeFromContentType:'text/html' |
|
147 |
self extractMimeTypeFromContentType:'text/xml; charset=utf-8' |
|
148 |
" |
|
149 |
! |
|
150 |
||
151 |
unEscape:aString |
|
152 |
"Convert escaped characters in an urls arguments or post fields to their proper characters. |
|
153 |
These are: |
|
154 |
+ -> space |
|
155 |
%XX ascii as hex digits |
|
156 |
%% -> % |
|
157 |
" |
|
158 |
||
159 |
|rs ws c peekC| |
|
160 |
||
161 |
(aString indexOfAny:'+%') == 0 ifTrue:[ |
|
162 |
^ aString |
|
163 |
]. |
|
164 |
||
165 |
rs := ReadStream on: aString. |
|
166 |
ws := WriteStream on: ''. |
|
167 |
[rs atEnd] whileFalse:[ |
|
168 |
c := rs next. |
|
169 |
c == $+ |
|
170 |
ifTrue:[ c := Character space ] |
|
171 |
ifFalse:[ |
|
172 |
c == $% |
|
173 |
ifTrue: [ |
|
174 |
peekC := rs peek. |
|
175 |
(peekC notNil and:[peekC isHexDigit]) ifTrue:[ |
|
2179
c1cee8bbc1e5
unescape: care for invalid escape sequence (%, %singleDigit atEnd)
sr
parents:
2144
diff
changeset
|
176 |
c := (Integer readFrom:(rs nextAvailable:2) radix:16) asCharacter |
2058 | 177 |
] ifFalse:[ |
178 |
c := rs next. |
|
179 |
] |
|
180 |
] |
|
181 |
]. |
|
2179
c1cee8bbc1e5
unescape: care for invalid escape sequence (%, %singleDigit atEnd)
sr
parents:
2144
diff
changeset
|
182 |
c notNil ifTrue:[ ws nextPut: c ]. |
2058 | 183 |
]. |
184 |
^ ws contents |
|
185 |
||
186 |
" |
|
2087 | 187 |
self unEscape:'a%20b' |
188 |
self unEscape:'a%%b' |
|
189 |
self unEscape:'a+b' |
|
190 |
self unEscape:'a%+b' |
|
2179
c1cee8bbc1e5
unescape: care for invalid escape sequence (%, %singleDigit atEnd)
sr
parents:
2144
diff
changeset
|
191 |
self unEscape:'a%' |
c1cee8bbc1e5
unescape: care for invalid escape sequence (%, %singleDigit atEnd)
sr
parents:
2144
diff
changeset
|
192 |
self unEscape:'a%2' |
2058 | 193 |
" |
2179
c1cee8bbc1e5
unescape: care for invalid escape sequence (%, %singleDigit atEnd)
sr
parents:
2144
diff
changeset
|
194 |
|
c1cee8bbc1e5
unescape: care for invalid escape sequence (%, %singleDigit atEnd)
sr
parents:
2144
diff
changeset
|
195 |
"Modified: / 08-07-2009 / 12:40:56 / sr" |
2058 | 196 |
! |
197 |
||
198 |
urlEncoded: aString |
|
199 |
"helper to escape invalid/dangerous characters in an urls arguments or post-fields. |
|
200 |
see: application/x-www-form-urlencoded |
|
201 |
" |
|
202 |
||
203 |
" |
|
204 |
self unEscape:(self urlEncoded:'_-.*Frankfurt(Main) Hbf') |
|
205 |
self urlEncoded:'_-.*Frankfurt(Main) Hbf') unescape |
|
206 |
||
207 |
self unEscape:(self urlEncoded:'-_.*%exept;') |
|
208 |
self urlEncoded:'-_.*%exept;' |
|
209 |
" |
|
210 |
||
211 |
| rs ws c space| |
|
212 |
||
213 |
space := Character space. |
|
214 |
rs := ReadStream on: aString. |
|
215 |
ws := WriteStream on: ''. |
|
216 |
||
217 |
[ rs atEnd ] whileFalse: [ |
|
218 |
c := rs next. |
|
219 |
||
220 |
c isLetterOrDigit ifTrue:[ |
|
221 |
ws nextPut:c. |
|
222 |
] ifFalse:[ |
|
223 |
c == space ifTrue:[ |
|
224 |
ws nextPut:$+. |
|
225 |
] ifFalse:[ |
|
226 |
('-_.*' includes:c) ifTrue:[ |
|
227 |
ws nextPut:c. |
|
228 |
] ifFalse:[ |
|
229 |
ws nextPut: $%. |
|
230 |
c codePoint printOn:ws base:16. |
|
231 |
]. |
|
232 |
]. |
|
233 |
]. |
|
234 |
]. |
|
235 |
^ ws contents |
|
236 |
||
237 |
||
238 |
" |
|
2087 | 239 |
self escape:'a b' |
240 |
self escape:'a%b' |
|
241 |
self escape:'a b' |
|
242 |
self escape:'a+b' |
|
2058 | 243 |
" |
2066 | 244 |
! |
245 |
||
246 |
withSpecialHTMLCharactersEscaped:aStringOrCharacter |
|
247 |
"replace ampersand, less and greater by html-character escapes" |
|
248 |
||
249 |
"/ TODO: this is similar to escapeCharacterEntities. |
|
250 |
"/ we should refactor this into one method only (can we do hex escapes always ?). |
|
251 |
"/ Notice, that these two methods came into existance due to historic reasons |
|
252 |
"/ and were developed independent of each other, but later moved to this common place. |
|
253 |
||
254 |
|resultStream orgs repls| |
|
255 |
||
256 |
"/ orgs := #( $& $< $> ). |
|
257 |
"/ repls := #( '&' '<' '>' ). |
|
258 |
||
259 |
(aStringOrCharacter isString |
|
260 |
and:[ (aStringOrCharacter includesAny:'&<>') not ]) ifTrue:[^ aStringOrCharacter]. |
|
261 |
||
262 |
resultStream := WriteStream on:''. |
|
263 |
aStringOrCharacter asString do:[:eachCharacter | |
|
264 |
"/ huh - a switch. Sorry, but this method is used heavily. |
|
265 |
eachCharacter == $& |
|
266 |
ifTrue:[ resultStream nextPutAll:'&' ] |
|
267 |
ifFalse:[ |
|
268 |
eachCharacter == $< |
|
269 |
ifTrue:[ resultStream nextPutAll:'<' ] |
|
270 |
ifFalse:[ |
|
271 |
eachCharacter == $> |
|
272 |
ifTrue:[ resultStream nextPutAll:'>' ] |
|
273 |
ifFalse:[ |
|
274 |
resultStream nextPut:eachCharacter |
|
275 |
]]]. |
|
276 |
]. |
|
277 |
^ resultStream contents |
|
278 |
||
279 |
" |
|
280 |
self withSpecialHTMLCharactersEscaped:'<>#&' |
|
281 |
self withSpecialHTMLCharactersEscaped:$< |
|
282 |
self withSpecialHTMLCharactersEscaped:$# |
|
283 |
" |
|
284 |
||
285 |
"Modified: / 05-12-2006 / 13:48:59 / cg" |
|
2058 | 286 |
! ! |
287 |
||
288 |
!HTMLUtilities class methodsFor:'serving-helpers'! |
|
289 |
||
290 |
escape:aString |
|
291 |
"helper to escape invalid/dangerous characters in an urls arguments or post-fields. |
|
292 |
These are: |
|
293 |
control characters, '+', ';', '?', '&' and space -> %XX ascii as hex digits |
|
294 |
% -> %% |
|
295 |
" |
|
296 |
||
297 |
| rs ws c | |
|
298 |
||
299 |
rs := ReadStream on: aString. |
|
300 |
ws := WriteStream on: ''. |
|
301 |
[ rs atEnd ] whileFalse: [ |
|
302 |
c := rs next. |
|
303 |
c == $% ifTrue:[ |
|
304 |
ws nextPutAll: '%%'. |
|
305 |
] ifFalse:[ |
|
306 |
((c codePoint < 16r7F) |
|
307 |
and:[ ('+;?& ' includes:c) not ]) ifTrue: [ |
|
308 |
ws nextPut: c. |
|
309 |
] ifFalse:[ |
|
310 |
ws nextPut: $%. |
|
311 |
c codePoint printOn:ws base:16. |
|
312 |
] |
|
313 |
] |
|
314 |
]. |
|
315 |
^ ws contents |
|
316 |
||
317 |
" |
|
318 |
self escape:'a b' |
|
319 |
self escape:'a%b' |
|
320 |
self escape:'a b' |
|
321 |
self escape:'a+b' |
|
322 |
self escape:'aäüöb' |
|
323 |
" |
|
324 |
! ! |
|
325 |
||
2144 | 326 |
!HTMLUtilities class methodsFor:'text processing helpers'! |
327 |
||
328 |
plainTextOfHTML:htmlString |
|
329 |
"given some HTML, extract the raw text. |
|
330 |
Can be used to search for strings in some html text." |
|
331 |
||
332 |
|parser doc s| |
|
333 |
||
334 |
s := CharacterWriteStream on:(String new:100). |
|
335 |
||
336 |
parser := HTMLParser new. |
|
337 |
doc := parser parseText:htmlString. |
|
338 |
doc markUpElementsDo:[:el | |
|
339 |
|t| |
|
340 |
||
341 |
el isTextElement ifTrue:[ |
|
342 |
t := el text withoutSeparators. |
|
343 |
t notEmpty ifTrue:[ |
|
344 |
s nextPutAll:t. |
|
345 |
s space. |
|
346 |
]. |
|
347 |
] ifFalse:[ |
|
348 |
"/ ignore non-text; however, we could care for text in info-titles |
|
349 |
"/ or scripts as well... |
|
350 |
]. |
|
351 |
]. |
|
352 |
^ s contents asSingleByteStringIfPossible |
|
353 |
||
354 |
" |
|
355 |
self plainTextOfHTML:' |
|
356 |
bla1 bla2 <br>bla3 <table><tr><td>bla4</td></tr></table> bla5<p>bla6 |
|
357 |
' |
|
358 |
" |
|
359 |
! ! |
|
360 |
||
2058 | 361 |
!HTMLUtilities class methodsFor:'documentation'! |
362 |
||
363 |
version |
|
2179
c1cee8bbc1e5
unescape: care for invalid escape sequence (%, %singleDigit atEnd)
sr
parents:
2144
diff
changeset
|
364 |
^ '$Header: /cvs/stx/stx/libbasic2/HTMLUtilities.st,v 1.6 2009-07-08 10:40:24 sr Exp $' |
2058 | 365 |
! ! |