1 | unit UHTMLClasses;
|
---|
2 |
|
---|
3 | {$mode Delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | UXmlClasses, Classes, SysUtils, UCommon, UNetworkAddress;
|
---|
9 |
|
---|
10 | type
|
---|
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 |
|
---|
99 | implementation
|
---|
100 |
|
---|
101 | { THtmlPage }
|
---|
102 |
|
---|
103 | constructor THtmlPage.Create;
|
---|
104 | begin
|
---|
105 | Body := THtmlBlock.Create;
|
---|
106 | end;
|
---|
107 |
|
---|
108 | destructor THtmlPage.Destroy;
|
---|
109 | begin
|
---|
110 | Body.Free;
|
---|
111 | inherited;
|
---|
112 | end;
|
---|
113 |
|
---|
114 | function THtmlPage.GetAsXmlDocument: TXmlDocument;
|
---|
115 | begin
|
---|
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;
|
---|
146 | end;
|
---|
147 |
|
---|
148 | { THtmlBlock }
|
---|
149 |
|
---|
150 | constructor THtmlBlock.Create;
|
---|
151 | begin
|
---|
152 | SubItems := TList.Create;
|
---|
153 | end;
|
---|
154 |
|
---|
155 | destructor THtmlBlock.Destroy;
|
---|
156 | var
|
---|
157 | I: Integer;
|
---|
158 | begin
|
---|
159 | for I := 0 to SubItems.Count - 1 do THtmlElement(SubItems[I]).Free;
|
---|
160 | SubItems.Free;
|
---|
161 | inherited;
|
---|
162 | end;
|
---|
163 |
|
---|
164 | function THtmlBlock.GetAsXmlElement: TXmlElement;
|
---|
165 | var
|
---|
166 | I: Integer;
|
---|
167 | begin
|
---|
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;
|
---|
178 | end;
|
---|
179 |
|
---|
180 | { THtmlElement }
|
---|
181 |
|
---|
182 | function THtmlElement.GetAsXmlElement: TXmlElement;
|
---|
183 | begin
|
---|
184 |
|
---|
185 | end;
|
---|
186 |
|
---|
187 | constructor TURL.Create;
|
---|
188 | begin
|
---|
189 | Host := THostAddress.Create;
|
---|
190 | end;
|
---|
191 |
|
---|
192 | destructor TURL.Destroy;
|
---|
193 | begin
|
---|
194 | Host.Free;
|
---|
195 | inherited;
|
---|
196 | end;
|
---|
197 |
|
---|
198 | function TURL.GetAsString: string;
|
---|
199 | begin
|
---|
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;
|
---|
212 | end;
|
---|
213 |
|
---|
214 | procedure TURL.SetAsString(Value: string);
|
---|
215 | var
|
---|
216 | HostAddr: string;
|
---|
217 | HostPort: string;
|
---|
218 | begin
|
---|
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, '', '/.');
|
---|
230 | end;
|
---|
231 |
|
---|
232 | { THtmlLink }
|
---|
233 |
|
---|
234 | constructor THtmlLink.Create;
|
---|
235 | begin
|
---|
236 | Target := TURL.Create;
|
---|
237 | end;
|
---|
238 |
|
---|
239 | destructor THtmlLink.Destroy;
|
---|
240 | begin
|
---|
241 | Target.Free;
|
---|
242 | inherited;
|
---|
243 | end;
|
---|
244 |
|
---|
245 | function THtmlLink.GetAsXmlElement: TXmlElement;
|
---|
246 | begin
|
---|
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;
|
---|
253 | end;
|
---|
254 |
|
---|
255 | { THtmlString }
|
---|
256 |
|
---|
257 | function THtmlString.GetAsXmlElement: TXmlElement;
|
---|
258 | begin
|
---|
259 | Result := TXmlString.Create;
|
---|
260 | TXmlString(Result).Text := Text;
|
---|
261 | end;
|
---|
262 |
|
---|
263 | { THtmlImage }
|
---|
264 |
|
---|
265 | constructor THtmlImage.Create;
|
---|
266 | begin
|
---|
267 | Source := TURL.Create;
|
---|
268 | end;
|
---|
269 |
|
---|
270 | destructor THtmlImage.Destroy;
|
---|
271 | begin
|
---|
272 | Source.Free;
|
---|
273 | inherited;
|
---|
274 | end;
|
---|
275 |
|
---|
276 | function THtmlImage.GetAsXmlElement: TXmlElement;
|
---|
277 | begin
|
---|
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;
|
---|
286 | end;
|
---|
287 |
|
---|
288 | end.
|
---|