-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathhtmutils.pas
More file actions
382 lines (308 loc) · 8.54 KB
/
htmutils.pas
File metadata and controls
382 lines (308 loc) · 8.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
{ modified from jsFastHtmlParser for use with freepascal
Original Author:
James Azarja
Contributor:
Lars aka L505
http://z505.com }
unit HtmUtils; {$ifdef fpc} {$MODE OBJFPC} {$H+}{$endif}
interface
{ most commonly used }
function GetVal(const tag, attribname_ci: string): string;
function GetTagName(const Tag: string): string;
function GetUpTagName(const tag: string): string;
{ less commonly used, but useful }
function GetNameValPair(const tag, attribname_ci: string): string;
function GetValFromNameVal(const namevalpair: string): string;
{ rarely needed nAmE= case sensitivity }
function GetNameValPair_cs(const tag, attribname: string): string;
{ old code with bugs from James }
function GetVal_JAMES(const tag, attribname_ci: string): string;
function GetNameValPair_JAMES(const tag, attribname_ci: string): string;
{ other utilities }
function CopyBuffer(StartIndex: PChar; Len: integer): string;
function Ucase(s: string): string;
implementation
// Note: use sysutils if you have problems with your Win64/other platform
uses
{$IFDEF FPC}CompactSysUtils{$ELSE}Sysutils{$ENDIF};
function CopyBuffer(StartIndex: PChar; Len: integer): string;
var s : String;
begin
SetLength(s, Len);
StrLCopy(@s[1], StartIndex, Len);
result:= s;
end;
{ upper case }
function Ucase(s: string): string;
begin
{$IFDEF FPC}result:= upcase(s);{$ELSE}result:= uppercase(s);{$ENDIF}
end;
{ return value of attrib, NAME case ignored, VALUE case preserved }
function GetVal(const tag, attribname_ci: string): string;
var nameval: string;
begin
result:= '';
if tag = '' then exit;
if attribname_ci = '' then exit;
// returns full name=value pair
nameval:= GetNameValPair(tag, attribname_ci);
// extracts value portion only
result:= GetValFromNameVal(nameval);
end;
{ Return tag name, case preserved }
function GetTagName(const tag: string): string;
var
P : Pchar;
S : Pchar;
begin
P := Pchar(tag);
while P^ in ['<',' ',#9] do inc(P);
S := P;
while Not (P^ in [' ','>',#0]) do inc(P);
if P > S then
Result := CopyBuffer( S, P-S);
end;
{ Return tag name in uppercase }
function GetUpTagName(const tag: string): string;
var
P : Pchar;
S : Pchar;
begin
result:= '';
if tag = '' then exit;
P := Pchar(Ucase(tag));
while P^ in ['<',' ',#9] do inc(P);
S := P;
while Not (P^ in [' ','>',#0]) do inc(P);
if P > S then
Result := CopyBuffer( S, P-S);
end;
{ Return name=value pair ignore case of NAME, preserve case of VALUE
Lars' fixed version }
function GetNameValPair(const tag, attribname_ci: string): string;
var
P : Pchar;
S : Pchar;
UpperTag,
UpperAttrib : string;
Start: integer;
L : integer;
C : char;
begin
result:= '';
if tag = '' then exit;
if attribname_ci = '' then exit;
// must be space before case insensitive NAME, i.e. <a HREF= STYLE=
UpperAttrib:= ' ' + Ucase(attribname_ci);
UpperTag:= Ucase(Tag);
P:= Pchar(UpperTag);
S:= StrPos(P, Pchar(UpperAttrib));
if S <> nil then
begin
inc(S); // skip space
P:= S;
// Skip until hopefully equal sign
while not (P^ in ['=', ' ', '>', #0]) do
inc(P);
if (P^ = '=') then inc(P);
while not (P^ in [' ','>',#0]) do
begin
if (P^ in ['"','''']) then
begin
C:= P^;
inc(P); { Skip quote }
end else
C:= ' ';
{ thanks to Dmitry [mail@vader.ru] }
while not (P^ in [C, '>', #0]) do
Inc(P);
if (P^ <> '>') then inc(P); { Skip current character, except '>' }
break;
end;
L:= P - S;
Start:= S - Pchar(UpperTag);
P:= Pchar(Tag);
S:= P;
inc(S, Start);
result:= CopyBuffer(S, L);
end;
end;
{ Get value of attribute, e.g WIDTH=36 returns 36, preserves case of value }
function GetValFromNameVal(const namevalpair: string): string;
var
P: Pchar;
S: Pchar;
C: Char;
begin
result:= '';
if namevalpair = '' then exit;
P:= Pchar(namevalpair);
S:= StrPos(P, '=');
if S <> nil then
begin
inc(S); // skip equal
P:= S; // set P to a character after =
if (P^ in ['"','''']) then
begin
C:= P^;
Inc(P); { Skip current character }
end else
C:= ' ';
S:= P;
while not (P^ in [C, #0]) do
inc(P);
if (P <> S) then { Thanks to Dave Keighan (keighand@yahoo.com) }
Result:= CopyBuffer(S, P - S)
else
Result:= '';
end;
end;
{ return name=value portion, case sensitive, case preserved, rarely useful }
function GetNameValPair_cs(const Tag, attribname: string): string;
var
P : Pchar;
S : Pchar;
C : Char;
begin
result:= '';
if tag = '' then exit;
if attribname = '' then exit;
P := Pchar(Tag);
S := StrPos(P, Pchar(attribname));
if S<>nil then
begin
P := S;
// Skip attribute name
while not (P^ in ['=',' ','>',#0]) do
inc(P);
if (P^ = '=') then inc(P);
while not (P^ in [' ','>',#0]) do
begin
if (P^ in ['"','''']) then
begin
C:= P^;
inc(P); { Skip current character }
end else
C:= ' ';
{ thanks to Dmitry [mail@vader.ru] }
while not (P^ in [C, '>', #0]) do
inc(P);
if (P^<>'>') then inc(P); { Skip current character, except '>' }
break;
end;
if P > S then
Result:= CopyBuffer(S, P - S)
else
Result:= '';
end;
end;
{ ----------------------------------------------------------------------------
BELOW FUNCTIONS ARE OBSOLETE OR RARELY NEEDED SINCE THEY EITHER CONTAIN BUGS
OR THEY ARE TOO CASE SENSITIVE (FOR THE TAG NAME PORTION OF THE ATTRIBUTE }
{ James old buggy code for testing purposes.
Bug: when finding 'ID', function finds "width", even though width <> "id" }
function GetNameValPair_JAMES(const tag, attribname_ci: string): string;
var
P : Pchar;
S : Pchar;
UT,
UA : string;
Start: integer;
L : integer;
C : char;
begin
UA:= Ucase(attribname_ci);
UT:= Ucase(Tag);
P:= Pchar(UT);
S:= StrPos(P, Pchar(UA));
if S <> nil then
begin
P := S;
// Skip attribute name
while not (P^ in ['=',' ','>',#0]) do
inc(P);
if (P^ = '=') then inc(P);
while not (P^ in [' ','>',#0]) do
begin
if (P^ in ['"','''']) then
begin
C:= P^;
inc(P); { Skip current character }
end else
C:= ' ';
{ thanks to Dmitry [mail@vader.ru] }
while not (P^ in [C, '>', #0]) do
Inc(P);
if (P^ <> '>') then inc(P); { Skip current character, except '>' }
break;
end;
L:= P - S;
Start:= S - Pchar(UT);
P:= Pchar(Tag);
S:= P;
inc(S, Start);
result:= CopyBuffer(S, L);
end;
end;
{ James old buggy code for testing purposes }
function GetVal_JAMES(const tag, attribname_ci: string): string;
var namevalpair: string;
begin
namevalpair:= GetNameValPair_JAMES(tag, attribname_ci);
result:= GetValFromNameVal(namevalpair);
end;
end.
(* alternative, not needed
{ return value (case preserved) from a name=value pair, ignores case in given NAME= portion }
function GetValFromNameVal(namevalpair: string): string;
type
TAttribPos = record
startpos: longword; // start pos of value
len: longword; // length of value
end;
{ returns case insensitive start position and length of just the value
substring in name=value pair}
function ReturnPos(attribute: string): TAttribPos;
var
P : Pchar;
S : Pchar;
C : Char;
begin
result.startpos:= 0;
result.len:= 0;
P:= Pchar(uppercase(Attribute));
// get substring including and everything after equal
S:= StrPos(P, '=');
result.startpos:= pos('=', P);
if S <> nil then
begin
inc(S);
// set to character after =
inc(result.startpos);
P:= S;
if (P^ in ['"','''']) then
begin
C:= P^;
// skip quote
inc(P);
inc(result.startpos);
end else
C:= ' ';
S:= P;
// go to end quote or end of value
while not (P^ in [C, #0]) do
inc(P);
if (P <> S) then
begin
result.len:= p - s;
end;
end;
end;
var
found: TAttribPos;
begin
found:= ReturnPos(namevalpair);
// extract using coordinates
result:= MidStr(namevalpair, found.startpos, found.len);
end;
*)