author | Claus Gittinger <cg@exept.de> |
Sat, 20 Mar 2010 16:59:29 +0100 | |
changeset 2436 | a5537ae7be4a |
parent 2434 | 5625df4b6119 |
child 2442 | db061ff41012 |
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:'"'. |
|
2436
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
56 |
"/ EscapeControlCharacters at:$' put:'''. |
2058 | 57 |
]. |
58 |
^ EscapeControlCharacters. |
|
59 |
! |
|
60 |
||
61 |
escapeCharacterEntities:aString |
|
62 |
"helper to escape invalid/dangerous characters in html strings. |
|
63 |
These are: |
|
64 |
control characters, '<', '>', '&' and space -> %XX ascii as hex digits |
|
65 |
% -> %% |
|
66 |
" |
|
2066 | 67 |
"/ TODO: this is similar to withSpecialHTMLCharactersEscaped. |
68 |
"/ we should refactor this into one method only (can we do hex escapes always ?). |
|
69 |
"/ Notice, that these two methods came into existance due to historic reasons |
|
70 |
"/ and were developed independent of each other, but later moved to this common place. |
|
71 |
||
72 |
||
2058 | 73 |
|rs ws c controlCharacters controlString| |
74 |
||
75 |
controlCharacters := self controlCharacters. |
|
76 |
rs := ReadStream on: aString. |
|
77 |
ws := WriteStream on: ''. |
|
78 |
[ rs atEnd ] whileFalse: [ |
|
79 |
c := rs next. |
|
80 |
controlString := controlCharacters at:c ifAbsent:nil. |
|
81 |
controlString notNil ifTrue:[ |
|
82 |
ws nextPutAll:controlString. |
|
83 |
] ifFalse:[ |
|
84 |
c codePoint > 16r7F ifTrue:[ |
|
85 |
ws |
|
86 |
nextPutAll:'&#'; |
|
87 |
nextPutAll:(c codePoint printString); |
|
88 |
nextPutAll:';'. |
|
89 |
] ifFalse:[ |
|
90 |
ws nextPut:c. |
|
91 |
] |
|
92 |
] |
|
93 |
]. |
|
94 |
^ ws contents |
|
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 |
||
2436
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
246 |
withAllSpecialHTMLCharactersEscaped:aStringOrCharacter |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
247 |
"replace ampersand, less, greater and quotes by html-character escapes" |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
248 |
|
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
249 |
"/ TODO: this is similar to escapeCharacterEntities. |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
250 |
"/ 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
|
251 |
"/ Notice, that these two methods came into existance due to historic reasons |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
252 |
"/ 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
|
253 |
|
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
254 |
|resultStream| |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
255 |
|
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
256 |
"/ orgs := #( $& $< $> $" $'). |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
257 |
"/ repls := #( '&' '<' '>' " '). |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
258 |
|
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
259 |
(aStringOrCharacter isString |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
260 |
and:[ (aStringOrCharacter includesAny:'&<>') not ]) ifTrue:[^ aStringOrCharacter]. |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
261 |
|
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
262 |
resultStream := WriteStream on:''. |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
263 |
aStringOrCharacter asString do:[:eachCharacter | |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
264 |
"/ huh - a switch. Sorry, but this method is used heavily. |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
265 |
eachCharacter == $& |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
266 |
ifTrue:[ resultStream nextPutAll:'&' ] |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
267 |
ifFalse:[ |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
268 |
eachCharacter == $< |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
269 |
ifTrue:[ resultStream nextPutAll:'<' ] |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
270 |
ifFalse:[ |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
271 |
eachCharacter == $> |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
272 |
ifTrue:[ resultStream nextPutAll:'>' ] |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
273 |
ifFalse:[ |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
274 |
eachCharacter == $" |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
275 |
ifTrue:[ resultStream nextPutAll:'"' ] |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
276 |
ifFalse:[ |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
277 |
eachCharacter == $' |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
278 |
ifTrue:[ resultStream nextPutAll:''' ] |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
279 |
ifFalse:[ |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
280 |
resultStream nextPut:eachCharacter |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
281 |
]]]]]. |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
282 |
]. |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
283 |
^ resultStream contents |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
284 |
|
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
285 |
" |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
286 |
self withSpecialHTMLCharactersEscaped:'<>#&' |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
287 |
self withSpecialHTMLCharactersEscaped:$< |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
288 |
self withSpecialHTMLCharactersEscaped:$# |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
289 |
" |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
290 |
|
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
291 |
"Modified: / 05-12-2006 / 13:48:59 / cg" |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
292 |
! |
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
293 |
|
2066 | 294 |
withSpecialHTMLCharactersEscaped:aStringOrCharacter |
295 |
"replace ampersand, less and greater by html-character escapes" |
|
296 |
||
297 |
"/ TODO: this is similar to escapeCharacterEntities. |
|
298 |
"/ we should refactor this into one method only (can we do hex escapes always ?). |
|
299 |
"/ Notice, that these two methods came into existance due to historic reasons |
|
300 |
"/ and were developed independent of each other, but later moved to this common place. |
|
301 |
||
302 |
|resultStream orgs repls| |
|
303 |
||
304 |
"/ orgs := #( $& $< $> ). |
|
305 |
"/ repls := #( '&' '<' '>' ). |
|
306 |
||
307 |
(aStringOrCharacter isString |
|
308 |
and:[ (aStringOrCharacter includesAny:'&<>') not ]) ifTrue:[^ aStringOrCharacter]. |
|
309 |
||
310 |
resultStream := WriteStream on:''. |
|
311 |
aStringOrCharacter asString do:[:eachCharacter | |
|
312 |
"/ huh - a switch. Sorry, but this method is used heavily. |
|
313 |
eachCharacter == $& |
|
314 |
ifTrue:[ resultStream nextPutAll:'&' ] |
|
315 |
ifFalse:[ |
|
316 |
eachCharacter == $< |
|
317 |
ifTrue:[ resultStream nextPutAll:'<' ] |
|
318 |
ifFalse:[ |
|
319 |
eachCharacter == $> |
|
320 |
ifTrue:[ resultStream nextPutAll:'>' ] |
|
321 |
ifFalse:[ |
|
322 |
resultStream nextPut:eachCharacter |
|
323 |
]]]. |
|
324 |
]. |
|
325 |
^ resultStream contents |
|
326 |
||
327 |
" |
|
328 |
self withSpecialHTMLCharactersEscaped:'<>#&' |
|
329 |
self withSpecialHTMLCharactersEscaped:$< |
|
330 |
self withSpecialHTMLCharactersEscaped:$# |
|
331 |
" |
|
332 |
||
333 |
"Modified: / 05-12-2006 / 13:48:59 / cg" |
|
2058 | 334 |
! ! |
335 |
||
336 |
!HTMLUtilities class methodsFor:'serving-helpers'! |
|
337 |
||
338 |
escape:aString |
|
2436
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
339 |
"helper to escape invalid/dangerous characters in an url's arguments or post-fields. |
2058 | 340 |
These are: |
341 |
control characters, '+', ';', '?', '&' and space -> %XX ascii as hex digits |
|
342 |
% -> %% |
|
343 |
" |
|
344 |
||
345 |
| rs ws c | |
|
346 |
||
347 |
rs := ReadStream on: aString. |
|
348 |
ws := WriteStream on: ''. |
|
349 |
[ rs atEnd ] whileFalse: [ |
|
350 |
c := rs next. |
|
351 |
c == $% ifTrue:[ |
|
352 |
ws nextPutAll: '%%'. |
|
353 |
] ifFalse:[ |
|
354 |
((c codePoint < 16r7F) |
|
355 |
and:[ ('+;?& ' includes:c) not ]) ifTrue: [ |
|
356 |
ws nextPut: c. |
|
357 |
] ifFalse:[ |
|
358 |
ws nextPut: $%. |
|
359 |
c codePoint printOn:ws base:16. |
|
360 |
] |
|
361 |
] |
|
362 |
]. |
|
363 |
^ ws contents |
|
364 |
||
365 |
" |
|
366 |
self escape:'a b' |
|
367 |
self escape:'a%b' |
|
368 |
self escape:'a b' |
|
369 |
self escape:'a+b' |
|
370 |
self escape:'aäüöb' |
|
371 |
" |
|
372 |
! ! |
|
373 |
||
2144 | 374 |
!HTMLUtilities class methodsFor:'text processing helpers'! |
375 |
||
376 |
plainTextOfHTML:htmlString |
|
377 |
"given some HTML, extract the raw text. |
|
378 |
Can be used to search for strings in some html text." |
|
379 |
||
380 |
|parser doc s| |
|
381 |
||
382 |
s := CharacterWriteStream on:(String new:100). |
|
383 |
||
384 |
parser := HTMLParser new. |
|
385 |
doc := parser parseText:htmlString. |
|
386 |
doc markUpElementsDo:[:el | |
|
387 |
|t| |
|
388 |
||
389 |
el isTextElement ifTrue:[ |
|
390 |
t := el text withoutSeparators. |
|
391 |
t notEmpty ifTrue:[ |
|
392 |
s nextPutAll:t. |
|
393 |
s space. |
|
394 |
]. |
|
395 |
] ifFalse:[ |
|
396 |
"/ ignore non-text; however, we could care for text in info-titles |
|
397 |
"/ or scripts as well... |
|
398 |
]. |
|
399 |
]. |
|
400 |
^ s contents asSingleByteStringIfPossible |
|
401 |
||
402 |
" |
|
403 |
self plainTextOfHTML:' |
|
404 |
bla1 bla2 <br>bla3 <table><tr><td>bla4</td></tr></table> bla5<p>bla6 |
|
405 |
' |
|
406 |
" |
|
407 |
! ! |
|
408 |
||
2058 | 409 |
!HTMLUtilities class methodsFor:'documentation'! |
410 |
||
411 |
version |
|
2436
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
412 |
^ '$Header: /cvs/stx/stx/libbasic2/HTMLUtilities.st,v 1.8 2010-03-20 15:59:29 cg Exp $' |
2434
5625df4b6119
comment/format in: #escapeCharacterEntities:
Claus Gittinger <cg@exept.de>
parents:
2179
diff
changeset
|
413 |
! |
5625df4b6119
comment/format in: #escapeCharacterEntities:
Claus Gittinger <cg@exept.de>
parents:
2179
diff
changeset
|
414 |
|
5625df4b6119
comment/format in: #escapeCharacterEntities:
Claus Gittinger <cg@exept.de>
parents:
2179
diff
changeset
|
415 |
version_CVS |
2436
a5537ae7be4a
added: #withAllSpecialHTMLCharactersEscaped:
Claus Gittinger <cg@exept.de>
parents:
2434
diff
changeset
|
416 |
^ '$Header: /cvs/stx/stx/libbasic2/HTMLUtilities.st,v 1.8 2010-03-20 15:59:29 cg Exp $' |
2058 | 417 |
! ! |