source: Network/HTTP/UHTMLClasses.pas

Last change on this file was 35, checked in by george, 14 years ago
  • Přidáno: Třídy pro běh web serveru, asociativní pole, dávkový přenos paketů.
File size: 6.3 KB
Line 
1unit UHTMLClasses;
2
3{$mode Delphi}{$H+}
4
5interface
6
7uses
8 UXmlClasses, Classes, SysUtils, UCommon, UNetworkAddress;
9
10type
11 TURL = class(TPersistent)
12 private
13 function GetAsString: string;
14 procedure SetAsString(Value: string);
15 public
16 Scheme: string;
17 UserName: string;
18 Password: string;
19 Host: THostAddress;
20 Port: Word;
21 Path: string;
22 Query: string;
23 Fragment: string;
24 constructor Create;
25 destructor Destroy; override;
26 property AsString: string read GetAsString write SetAsString;
27 end;
28
29 THtmlElement = class
30 private
31 function GetAsXmlElement: TXmlElement; virtual;
32 public
33 Id: string;
34 Name: string;
35 ClassId: string;
36 Style: string;
37 property AsXmlElement: TXmlElement read GetAsXmlElement;
38 end;
39
40 TBlockType = (btNoTag, btBlockLevel, btInline);
41
42 THtmlString = class(THtmlElement)
43 private
44 function GetAsXmlElement: TXmlElement; override;
45 public
46 Text: string;
47 end;
48
49 THtmlBlock = class(THtmlElement)
50 private
51 function GetAsXmlElement: TXmlElement; override;
52 public
53 BlockType: TBlockType;
54 SubItems: TList; // of THtmlElement;
55 constructor Create;
56 destructor Destroy; override;
57 end;
58
59 THtmlLink = class(THtmlElement)
60 private
61 function GetAsXmlElement: TXmlElement; override;
62 public
63 Target: TURL;
64 Content: THtmlElement;
65 constructor Create;
66 destructor Destroy; override;
67 end;
68
69 TSizeUnits = (suPixels, suPercents);
70 THtmlSize = record
71 Width: Integer;
72 Height: Integer;
73 Units: TSizeUnits;
74 end;
75
76 THtmlImage = class(THtmlElement)
77 private
78 function GetAsXmlElement: TXmlElement; override;
79 public
80 Size: THtmlSize;
81 Source: TURL;
82 AlternateText: string;
83 constructor Create;
84 destructor Destroy; override;
85 end;
86
87 THtmlPage = class
88 private
89 function GetAsXmlDocument: TXmlDocument;
90 public
91 Title: string;
92 Charset: string;
93 Body: THtmlBlock;
94 property AsXmlDocument: TXmlDocument read GetAsXmlDocument;
95 constructor Create;
96 destructor Destroy; override;
97 end;
98
99implementation
100
101{ THtmlPage }
102
103constructor THtmlPage.Create;
104begin
105 Body := THtmlBlock.Create;
106end;
107
108destructor THtmlPage.Destroy;
109begin
110 Body.Free;
111 inherited;
112end;
113
114function THtmlPage.GetAsXmlDocument: TXmlDocument;
115begin
116 Result := TXmlDocument.Create;
117 with Result, Content do begin
118 Formated := True;
119 TagName := 'html';
120 with TXmlTag(SubElements[SubElements.Add(TXmlTag.Create)]) do begin
121 TagName := 'head';
122 with TXmlTag(SubElements[SubElements.Add(TXmlTag.Create)]) do begin
123 TagName := 'title';
124 with TXmlString(SubElements[SubElements.Add(TXmlString.Create)]) do begin
125 Text := Title;
126 end;
127 end;
128 with TXmlTag(SubElements[SubElements.Add(TXmlTag.Create)]) do begin
129 TagName := 'meta';
130 Attributes.AddNameValue('http-equiv', 'Content-Language');
131 Attributes.AddNameValue('content', 'cs');
132 end;
133 with TXmlTag(SubElements[SubElements.Add(TXmlTag.Create)]) do begin
134 TagName := 'meta';
135 Attributes.AddNameValue('http-equiv', 'Content-Type');
136 Attributes.AddNameValue('content', 'text/html; charset=' + Charset);
137 end;
138 end;
139 with TXmlTag(SubElements[SubElements.Add(TXmlTag.Create)]) do begin
140 TagName := 'body';
141 SubElements.Add(Body.AsXmlElement);
142
143
144 end;
145 end;
146end;
147
148{ THtmlBlock }
149
150constructor THtmlBlock.Create;
151begin
152 SubItems := TList.Create;
153end;
154
155destructor THtmlBlock.Destroy;
156var
157 I: Integer;
158begin
159 for I := 0 to SubItems.Count - 1 do THtmlElement(SubItems[I]).Free;
160 SubItems.Free;
161 inherited;
162end;
163
164function THtmlBlock.GetAsXmlElement: TXmlElement;
165var
166 I: Integer;
167begin
168 Result := TXmlTag.Create;
169 with TXmlTag(Result) do begin
170 case BlockType of
171 btBlockLevel: TagName := 'div';
172 btInline: TagName := 'span';
173 btNoTag: TagName := '';
174 end;
175 for I := 0 to SubItems.Count - 1 do
176 SubElements.Add(THtmlElement(SubItems[I]).AsXmlElement);
177 end;
178end;
179
180{ THtmlElement }
181
182function THtmlElement.GetAsXmlElement: TXmlElement;
183begin
184
185end;
186
187constructor TURL.Create;
188begin
189 Host := THostAddress.Create;
190end;
191
192destructor TURL.Destroy;
193begin
194 Host.Free;
195 inherited;
196end;
197
198function TURL.GetAsString: string;
199begin
200 Result := '';
201 if Scheme <> '' then Result := Scheme + '://';
202 if UserName <> '' then begin
203 Result := Result + UserName;
204 if UserName <> '' then Result := Result + ':' + Password;
205 Result := Result + '@';
206 end;
207 if Host.AsString <> '' then Result := Result + Host.AsString;
208 if Port <> 0 then Result := Result + ':' + IntToStr(Port);
209 if Path <> '' then Result := Result + Path;
210 if Query <> '' then Result := Result + '?' + Query;
211 if Fragment <> '' then Result := Result + '#' + Fragment;
212end;
213
214procedure TURL.SetAsString(Value: string);
215var
216 HostAddr: string;
217 HostPort: string;
218begin
219 LeftCutString(Value, Scheme, '://');
220 if LeftCutString(Value, UserName, ':') then LeftCutString(Value, Password, '@')
221 else LeftCutString(Value, UserName, '@');
222 RightCutString(Value, Fragment, '#');
223 RightCutString(Value, Query, '?', '=&');
224 if LeftCutString(Value, HostAddr, ':', '.') then begin
225 LeftCutString(Value, HostPort, '');
226 Port := StrToInt(HostPort);
227 end else LeftCutString(Value, HostAddr, '', '.');
228 Host.AsString := HostAddr;
229 LeftCutString(Value, Path, '', '/.');
230end;
231
232{ THtmlLink }
233
234constructor THtmlLink.Create;
235begin
236 Target := TURL.Create;
237end;
238
239destructor THtmlLink.Destroy;
240begin
241 Target.Free;
242 inherited;
243end;
244
245function THtmlLink.GetAsXmlElement: TXmlElement;
246begin
247 Result := TXmlTag.Create;
248 with TXmlTag(Result) do begin
249 TagName := 'a';
250 Attributes.Add('href='+Target.AsString);
251 if Assigned(Content) then SubElements.Add(Content.AsXmlElement);
252 end;
253end;
254
255{ THtmlString }
256
257function THtmlString.GetAsXmlElement: TXmlElement;
258begin
259 Result := TXmlString.Create;
260 TXmlString(Result).Text := Text;
261end;
262
263{ THtmlImage }
264
265constructor THtmlImage.Create;
266begin
267 Source := TURL.Create;
268end;
269
270destructor THtmlImage.Destroy;
271begin
272 Source.Free;
273 inherited;
274end;
275
276function THtmlImage.GetAsXmlElement: TXmlElement;
277begin
278 Result := TXmlTag.Create;
279 with TXmlTag(Result) do begin
280 TagName := 'img';
281 Attributes.AddNameValue('src', Source.AsString);
282 Attributes.AddNameValue('width', IntToStr(Size.Width));
283 Attributes.AddNameValue('height', IntToStr(Size.Height));
284 Attributes.AddNameValue('alt', AlternateText);
285 end;
286end;
287
288end.
Note: See TracBrowser for help on using the repository browser.